Back to index

glibc  2.9
ex5.c
Go to the documentation of this file.
00001 /* The classic producer-consumer example, implemented with semaphores.
00002    All integers between 0 and 9999 should be printed exactly twice,
00003    once to the right of the arrow and once to the left. */
00004 
00005 #include <stdio.h>
00006 #include "pthread.h"
00007 #include "semaphore.h"
00008 
00009 #define BUFFER_SIZE 16
00010 
00011 /* Circular buffer of integers. */
00012 
00013 struct prodcons
00014 {
00015   int buffer[BUFFER_SIZE];  /* the actual data */
00016   int readpos, writepos;    /* positions for reading and writing */
00017   sem_t sem_read;           /* number of elements available for reading */
00018   sem_t sem_write;          /* number of locations available for writing */
00019 };
00020 
00021 /* Initialize a buffer */
00022 
00023 static void
00024 init (struct prodcons *b)
00025 {
00026   sem_init (&b->sem_write, 0, BUFFER_SIZE - 1);
00027   sem_init (&b->sem_read, 0, 0);
00028   b->readpos = 0;
00029   b->writepos = 0;
00030 }
00031 
00032 /* Store an integer in the buffer */
00033 
00034 static void
00035 put (struct prodcons *b, int data)
00036 {
00037   /* Wait until buffer is not full */
00038   sem_wait (&b->sem_write);
00039   /* Write the data and advance write pointer */
00040   b->buffer[b->writepos] = data;
00041   b->writepos++;
00042   if (b->writepos >= BUFFER_SIZE)
00043     b->writepos = 0;
00044   /* Signal that the buffer contains one more element for reading */
00045   sem_post (&b->sem_read);
00046 }
00047 
00048 /* Read and remove an integer from the buffer */
00049 
00050 static int
00051 get (struct prodcons *b)
00052 {
00053   int data;
00054   /* Wait until buffer is not empty */
00055   sem_wait (&b->sem_read);
00056   /* Read the data and advance read pointer */
00057   data = b->buffer[b->readpos];
00058   b->readpos++;
00059   if (b->readpos >= BUFFER_SIZE)
00060     b->readpos = 0;
00061   /* Signal that the buffer has now one more location for writing */
00062   sem_post (&b->sem_write);
00063   return data;
00064 }
00065 
00066 /* A test program: one thread inserts integers from 1 to 10000,
00067    the other reads them and prints them. */
00068 
00069 #define OVER (-1)
00070 
00071 struct prodcons buffer;
00072 
00073 static void *
00074 producer (void *data)
00075 {
00076   int n;
00077   for (n = 0; n < 10000; n++)
00078     {
00079       printf ("%d --->\n", n);
00080       put (&buffer, n);
00081     }
00082   put (&buffer, OVER);
00083   return NULL;
00084 }
00085 
00086 static void *
00087 consumer (void *data)
00088 {
00089   int d;
00090   while (1)
00091     {
00092       d = get (&buffer);
00093       if (d == OVER)
00094        break;
00095       printf ("---> %d\n", d);
00096     }
00097   return NULL;
00098 }
00099 
00100 int
00101 main (void)
00102 {
00103   pthread_t th_a, th_b;
00104   void *retval;
00105 
00106   init (&buffer);
00107   /* Create the threads */
00108   pthread_create (&th_a, NULL, producer, 0);
00109   pthread_create (&th_b, NULL, consumer, 0);
00110   /* Wait until producer and consumer finish. */
00111   pthread_join (th_a, &retval);
00112   pthread_join (th_b, &retval);
00113   return 0;
00114 }