Back to index

glibc  2.9
Classes | Defines | Functions
ex5.c File Reference
#include <stdio.h>
#include "pthread.h"
#include "semaphore.h"

Go to the source code of this file.

Classes

struct  prodcons

Defines

#define BUFFER_SIZE   16
#define OVER   (-1)

Functions

static void init (struct prodcons *b)
static void put (struct prodcons *b, int data)
static int get (struct prodcons *b)
static void * producer (void *data)
static void * consumer (void *data)
int main (void)

Class Documentation

struct prodcons

Definition at line 13 of file ex2.c.

Collaboration diagram for prodcons:
Class Members
int buffer
pthread_mutex_t lock
pthread_cond_t notempty
pthread_cond_t notfull
int readpos
sem_t sem_read
sem_t sem_write
int writepos

Define Documentation

#define BUFFER_SIZE   16

Definition at line 9 of file ex5.c.

#define OVER   (-1)

Definition at line 69 of file ex5.c.


Function Documentation

static void* consumer ( void *  data) [static]

Definition at line 87 of file ex5.c.

{
  int d;
  while (1)
    {
      d = get (&buffer);
      if (d == OVER)
       break;
      printf ("---> %d\n", d);
    }
  return NULL;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int get ( struct prodcons b) [static]

Definition at line 51 of file ex5.c.

{
  int data;
  /* Wait until buffer is not empty */
  sem_wait (&b->sem_read);
  /* Read the data and advance read pointer */
  data = b->buffer[b->readpos];
  b->readpos++;
  if (b->readpos >= BUFFER_SIZE)
    b->readpos = 0;
  /* Signal that the buffer has now one more location for writing */
  sem_post (&b->sem_write);
  return data;
}

Here is the call graph for this function:

static void init ( struct prodcons b) [static]

Definition at line 24 of file ex5.c.

{
  sem_init (&b->sem_write, 0, BUFFER_SIZE - 1);
  sem_init (&b->sem_read, 0, 0);
  b->readpos = 0;
  b->writepos = 0;
}

Here is the call graph for this function:

int main ( void  )

Definition at line 101 of file ex5.c.

{
  pthread_t th_a, th_b;
  void *retval;

  init (&buffer);
  /* Create the threads */
  pthread_create (&th_a, NULL, producer, 0);
  pthread_create (&th_b, NULL, consumer, 0);
  /* Wait until producer and consumer finish. */
  pthread_join (th_a, &retval);
  pthread_join (th_b, &retval);
  return 0;
}

Here is the call graph for this function:

static void* producer ( void *  data) [static]

Definition at line 74 of file ex5.c.

{
  int n;
  for (n = 0; n < 10000; n++)
    {
      printf ("%d --->\n", n);
      put (&buffer, n);
    }
  put (&buffer, OVER);
  return NULL;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void put ( struct prodcons b,
int  data 
) [static]

Definition at line 35 of file ex5.c.

{
  /* Wait until buffer is not full */
  sem_wait (&b->sem_write);
  /* Write the data and advance write pointer */
  b->buffer[b->writepos] = data;
  b->writepos++;
  if (b->writepos >= BUFFER_SIZE)
    b->writepos = 0;
  /* Signal that the buffer contains one more element for reading */
  sem_post (&b->sem_read);
}

Here is the call graph for this function:

Here is the caller graph for this function: