Producer-Consumer Problem - Semaphore - Systems Lab - C Program



//Producer Consumer Problem -OS Lab

Program:

#include<stdio.h>
#include<pthread.h>
#include<semaphore.h>
int buf[5],f,r;
sem_t mutex,full,empty;
void *produce(void *arg)
{
    int i;
    for(i=0;i<10;i++)
    {
        sem_wait(&empty);
        sem_wait(&mutex);
        printf("produced item is %d\n",i);
        buf[(++r)%5]=i;
        sleep(1);
        sem_post(&mutex);
                sem_post(&full);
        printf("full %u\n",full);
    }
}
void *consume(void *arg)
{
        int item,i;
        for(i=0;i<10;i++)
        {
                sem_wait(&full);
        printf("full %u\n",full);
                sem_wait(&mutex);
                item=buf[(++f)%5];
                printf("consumed item is %d\n",item);
                sleep(1);
                sem_post(&mutex);
                sem_post(&empty);
        }
}
main()
{
    pthread_t tid1,tid2;
    sem_init(&mutex,0,1);
    sem_init(&full,0,1);
    sem_init(&empty,0,5);
    pthread_create(&tid1,NULL,produce,NULL);
        pthread_create(&tid2,NULL,consume,NULL);
    pthread_join(tid1,NULL);
    pthread_join(tid2,NULL);
}



6 comments:

  1. this program isnt working in turboc++
    pl.. tell me the code for semaphore.h and alternative for pthread.h
    in windows

    ReplyDelete
  2. thanks a lot .... this is of great help :)

    ReplyDelete
  3. thank you so much...i owe u :)

    ReplyDelete
  4. sem_init(&full,0,1);
    sem_init(&empty,0,5);

    why are we initializing full to one?

    ReplyDelete
  5. Really sprb code :What a website<3

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...