0% found this document useful (0 votes)
53 views

Implementation of Producer Consumer Problem Using Semaphores Program

This document implements a producer-consumer problem using semaphores in C. It initializes three semaphores - mutex, full, and empty. The producer function puts data into a shared buffer and signals full, while consumer removes data from the buffer and signals empty. Both run in separate threads and access the shared buffer using the semaphores for synchronization. The output shows the producer and consumer taking turns to produce and consume 10 items from the buffer.

Uploaded by

MMehala
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

Implementation of Producer Consumer Problem Using Semaphores Program

This document implements a producer-consumer problem using semaphores in C. It initializes three semaphores - mutex, full, and empty. The producer function puts data into a shared buffer and signals full, while consumer removes data from the buffer and signals empty. Both run in separate threads and access the shared buffer using the semaphores for synchronization. The output shows the producer and consumer taking turns to produce and consume 10 items from the buffer.

Uploaded by

MMehala
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

IMPLEMENTATION OF PRODUCER CONSUMER PROBLEM USING SEMAPHORES PROGRAM:

#include <semaphore.h> #include <sys/types.h> #include <stdio.h> #include <pthread.h> #include <semaphore.h> #include <stdlib.h> #define BUF_SIZE 10 int item; int i; int a,b,c; int X[10]; sem_t mutex, full, empty; void *producer(void *); void *consumer(void *); main() { a=sem_init (&mutex,0,1); b=sem_init (&full,0,0); c=sem_init (&empty,0,10); pthread_t tid1,tid2; pthread_create(&tid1,NULL,producer,NULL); pthread_create(&tid2,NULL,consumer,NULL); pthread_join(tid1,NULL); pthread_join(tid2,NULL); } void *producer(void *arg) { int add=0; printf("Producer Created: \n"); for(i=0;i<=10;i++) { printf("Producer produce the data is: %d\n",i); add=(add+1)%10; sem_wait(&empty); sem_wait(&mutex); X[add]=i; sem_post(&mutex); sem_post(&full); i=(2+(int)(5.0*rand()/(RAND_MAX+1.0))); } }

void *consumer(void *arg) { int del=0,j; printf("Consumer Created: \n"); for(j=0;j<=10;j++) { sem_wait(&full); sem_wait(&mutex); del=(del+1)%10; item=X[del]; printf("Consumer consume the data is:%d\n",item); sem_post(&mutex); sem_post(&full); item=(2+(int)(5.0*rand()/(RAND_MAX+1.0))); } }

OUTPUT:
[ccitlin08@networkspecial ~]$ vi 6semaphore.c [ccitlin08@networkspecial ~]$ cc -o semaphore 6semaphore.c -lpthread lm [ccitlin08@networkspecial ~]$ ./semaphore Producer Created: Producer produce the data is: 0 Producer produce the data is: 7 Producer produce the data is: 4 Producer produce the data is: 6 Producer produce the data is: 6 Producer produce the data is: 7 Producer produce the data is: 3 Producer produce the data is: 4 Producer produce the data is: 6 Producer produce the data is: 4 Consumer Created: Consumer consume the data is:0 Consumer consume the data is:7 Consumer consume the data is:4 Consumer consume the data is:6 Consumer consume the data is:6 Consumer consume the data is:7 Consumer consume the data is:3 Consumer consume the data is:4 Consumer consume the data is:6 Consumer consume the data is:4

You might also like