| [4a64ef2] | 1 | /*****************************************************************************
|
|---|
| 2 | C-DAC Tech Workshop : hyPACK-2013
|
|---|
| 3 | October 15-18,2013
|
|---|
| 4 |
|
|---|
| 5 | Example : pthread-producer-consumer.c
|
|---|
| 6 |
|
|---|
| 7 | Objective : To illustrate producer-consumer problem using pthreads.
|
|---|
| 8 |
|
|---|
| 9 | Input : Nothing.
|
|---|
| 10 |
|
|---|
| 11 | Output : Sequence of Produced and Consumed items.
|
|---|
| 12 |
|
|---|
| 13 | Created : MAY-2013
|
|---|
| 14 | E-mail : hpcfte@cdac.in
|
|---|
| 15 |
|
|---|
| 16 | ****************************************************************************/
|
|---|
| 17 |
|
|---|
| 18 | #include <pthread.h>
|
|---|
| 19 | #include <stdio.h>
|
|---|
| 20 | #include <unistd.h>
|
|---|
| 21 | #include <stdlib.h>
|
|---|
| 22 |
|
|---|
| 23 | /*defining QUEUE sise */
|
|---|
| 24 | #define QUEUESIZE 10
|
|---|
| 25 | #define LOOP 20
|
|---|
| 26 |
|
|---|
| 27 | /*global variable declaration */
|
|---|
| 28 | int ret_count;
|
|---|
| 29 | /*Thread callback function prototype*/
|
|---|
| 30 | void *producer (void *args);
|
|---|
| 31 | void *consumer (void *args);
|
|---|
| 32 |
|
|---|
| 33 | typedef struct {
|
|---|
| 34 |
|
|---|
| 35 | int buf[QUEUESIZE];
|
|---|
| 36 | long head, tail;
|
|---|
| 37 | int full, empty;
|
|---|
| 38 | pthread_mutex_t *mut;
|
|---|
| 39 | pthread_cond_t *notFull, *notEmpty;
|
|---|
| 40 | } queue;
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 | queue *queueInit (void);
|
|---|
| 45 | void queueDelete (queue *q);
|
|---|
| 46 | void queueAdd (queue *q, int in);
|
|---|
| 47 | void queueDel (queue *q, int *out);
|
|---|
| 48 |
|
|---|
| 49 | /*Main function */
|
|---|
| 50 | int main ()
|
|---|
| 51 | {
|
|---|
| 52 | queue *fifo;
|
|---|
| 53 | pthread_t pro, con;
|
|---|
| 54 | /*QUEUE initialization */
|
|---|
| 55 | fifo = queueInit ();
|
|---|
| 56 | if (fifo == NULL)
|
|---|
| 57 | {
|
|---|
| 58 | fprintf (stderr, "main: Queue Init failed.\n");
|
|---|
| 59 | exit (1);
|
|---|
| 60 | }
|
|---|
| 61 | /*producer thread creation */
|
|---|
| 62 | ret_count=pthread_create (&pro, NULL, producer, fifo);
|
|---|
| 63 | if(ret_count)
|
|---|
| 64 | {
|
|---|
| 65 | printf("\n ERROR : Return code from pthread_create() is %d ",ret_count);
|
|---|
| 66 | exit(-1);
|
|---|
| 67 | }
|
|---|
| 68 | /*consumer thread creation */
|
|---|
| 69 | ret_count=pthread_create (&con, NULL, consumer, fifo);
|
|---|
| 70 | if(ret_count)
|
|---|
| 71 | {
|
|---|
| 72 | printf("\n ERROR : Return code from pthread_create() is %d ",ret_count);
|
|---|
| 73 | exit(-1);
|
|---|
| 74 | }
|
|---|
| 75 | /*joining producer thread to main thread */
|
|---|
| 76 | ret_count=pthread_join (pro, NULL);
|
|---|
| 77 | if(ret_count)
|
|---|
| 78 | {
|
|---|
| 79 | printf("\n ERROR : Return code from pthread_join() is %d ",ret_count);
|
|---|
| 80 | exit(-1);
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 | /*joining consumer thread to main thread */
|
|---|
| 85 | ret_count=pthread_join (con, NULL);
|
|---|
| 86 | if(ret_count)
|
|---|
| 87 | {
|
|---|
| 88 | printf("\n ERROR : Return code from pthread_join() is %d ",ret_count);
|
|---|
| 89 | exit(-1);
|
|---|
| 90 | }
|
|---|
| 91 | queueDelete (fifo);
|
|---|
| 92 | return 0;
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | /*producer thread callback function */
|
|---|
| 96 | void *producer (void *q)
|
|---|
| 97 | {
|
|---|
| 98 | queue *fifo;
|
|---|
| 99 | int i;
|
|---|
| 100 | fifo = (queue *)q;
|
|---|
| 101 | for (i = 0; i < LOOP; i++)
|
|---|
| 102 | {
|
|---|
| 103 | pthread_mutex_lock (fifo->mut);
|
|---|
| 104 | while (fifo->full)
|
|---|
| 105 | {
|
|---|
| 106 | printf ("producer: queue FULL.\n");
|
|---|
| 107 | pthread_cond_wait (fifo->notFull, fifo->mut);
|
|---|
| 108 | }
|
|---|
| 109 | queueAdd (fifo, i);
|
|---|
| 110 | pthread_mutex_unlock (fifo->mut);
|
|---|
| 111 | pthread_cond_signal (fifo->notEmpty);
|
|---|
| 112 | printf ("producer: add %d \n",i);
|
|---|
| 113 | usleep (100000);
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | return (NULL);
|
|---|
| 117 |
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 |
|
|---|
| 121 |
|
|---|
| 122 | /*consumer thread callback function */
|
|---|
| 123 | void *consumer (void *q)
|
|---|
| 124 | {
|
|---|
| 125 | queue *fifo;
|
|---|
| 126 | int i, d;
|
|---|
| 127 |
|
|---|
| 128 |
|
|---|
| 129 | fifo = (queue *)q;
|
|---|
| 130 | for (i = 0; i < LOOP; i++)
|
|---|
| 131 | {
|
|---|
| 132 | pthread_mutex_lock (fifo->mut);
|
|---|
| 133 | while (fifo->empty)
|
|---|
| 134 | {
|
|---|
| 135 | printf ("consumer: queue EMPTY.\n");
|
|---|
| 136 | pthread_cond_wait (fifo->notEmpty, fifo->mut);
|
|---|
| 137 | }
|
|---|
| 138 | queueDel (fifo, &d);
|
|---|
| 139 | pthread_mutex_unlock (fifo->mut);
|
|---|
| 140 | pthread_cond_signal (fifo->notFull);
|
|---|
| 141 | printf ("consumer: received %d.\n", d);
|
|---|
| 142 | usleep(500000);
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | return (NULL);
|
|---|
| 146 |
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | #ifdef o
|
|---|
| 150 | typedef struct
|
|---|
| 151 | {
|
|---|
| 152 | int buf[QUEUESIZE];
|
|---|
| 153 | long head, tail;
|
|---|
| 154 | int full, empty;
|
|---|
| 155 | pthread_mutex_t *mut;
|
|---|
| 156 | pthread_cond_t *notFull, *notEmpty;
|
|---|
| 157 | } queue;
|
|---|
| 158 | #endif
|
|---|
| 159 |
|
|---|
| 160 |
|
|---|
| 161 | queue *queueInit (void)
|
|---|
| 162 | {
|
|---|
| 163 | queue *q;
|
|---|
| 164 | q = (queue *)malloc (sizeof (queue));
|
|---|
| 165 | if (q == NULL) return (NULL);
|
|---|
| 166 | q->empty = 1;
|
|---|
| 167 | q->full = 0;
|
|---|
| 168 | q->head = 0;
|
|---|
| 169 | q->tail = 0;
|
|---|
| 170 | q->mut = (pthread_mutex_t *) malloc (sizeof (pthread_mutex_t));
|
|---|
| 171 | ret_count=pthread_mutex_init (q->mut, NULL);
|
|---|
| 172 | if(ret_count)
|
|---|
| 173 | {
|
|---|
| 174 | printf("\n ERROR : Return code from pthread_mutex_init() is %d ",ret_count);
|
|---|
| 175 | exit(-1);
|
|---|
| 176 | }
|
|---|
| 177 |
|
|---|
| 178 | q->notFull = (pthread_cond_t *) malloc (sizeof (pthread_cond_t));
|
|---|
| 179 | pthread_cond_init (q->notFull, NULL);
|
|---|
| 180 | q->notEmpty = (pthread_cond_t *) malloc (sizeof (pthread_cond_t));
|
|---|
| 181 | pthread_cond_init (q->notEmpty, NULL);
|
|---|
| 182 | return (q);
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 |
|
|---|
| 186 | void queueDelete (queue *q)
|
|---|
| 187 | {
|
|---|
| 188 | ret_count=pthread_mutex_destroy (q->mut);
|
|---|
| 189 | if(ret_count)
|
|---|
| 190 | {
|
|---|
| 191 | printf("\n ERROR : Return code from pthread_mutex_destroy() is %d ",ret_count);
|
|---|
| 192 | exit(-1);
|
|---|
| 193 | }
|
|---|
| 194 | free (q->mut);
|
|---|
| 195 | pthread_cond_destroy (q->notFull);
|
|---|
| 196 | free (q->notFull);
|
|---|
| 197 | pthread_cond_destroy (q->notEmpty);
|
|---|
| 198 | free (q->notEmpty);
|
|---|
| 199 | free (q);
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 |
|
|---|
| 203 | void queueAdd (queue *q, int in)
|
|---|
| 204 | {
|
|---|
| 205 | q->buf[q->tail] = in;
|
|---|
| 206 | q->tail++;
|
|---|
| 207 | if (q->tail == QUEUESIZE)
|
|---|
| 208 | q->tail = 0;
|
|---|
| 209 | if (q->tail == q->head)
|
|---|
| 210 | q->full = 1;
|
|---|
| 211 | q->empty = 0;
|
|---|
| 212 | return;
|
|---|
| 213 | }
|
|---|
| 214 |
|
|---|
| 215 | void queueDel (queue *q, int *out)
|
|---|
| 216 | {
|
|---|
| 217 |
|
|---|
| 218 |
|
|---|
| 219 | *out = q->buf[q->head];
|
|---|
| 220 | q->head++;
|
|---|
| 221 | if (q->head == QUEUESIZE)
|
|---|
| 222 | q->head = 0;
|
|---|
| 223 | if (q->head == q->tail)
|
|---|
| 224 | q->empty = 1;
|
|---|
| 225 | q->full = 0;
|
|---|
| 226 | return;
|
|---|
| 227 | }
|
|---|