Server : Apache System : Linux profile 3.10.0-1160.88.1.el7.x86_64 #1 SMP Tue Mar 7 15:41:52 UTC 2023 x86_64 User : apache ( 48) PHP Version : 8.0.28 Disable Function : NONE Directory : /var/www/html/bibhas.ghoshal/lab_files/ |
//
// producer_consumer_using_semaphore.c
//
//
// Created by Bibhas Ghoshal on 02/10/19.
//
#include<stdio.h>
#include<semaphore.h>
#include<pthread.h>
#define NBUFF 10
int nitems;
struct
{
int buff[NBUFF];
sem_t mutex,empty,full;
}shared;
void *produce(void*); void *consume(void*);
int main(int argc,char**argv)
{
pthread_t tid_produce,tid_consume;
if(argc!=2)
{
printf("usage:filename<nitems>");
return 0;
}
printf("\n\nproducer_consumer problem using semaphore \n");
printf(".......................................................................................\n");
nitems=atoi(argv[1]);
sem_init(&shared.mutex,0,1); // Intializing
sem_init(&shared.empty,0,NBUFF);
sem_init(&shared.full,0,0);
pthread_setconcurrency(2);
pthread_create(&tid_produce,NULL,produce,NULL);
pthread_create(&tid_consume,NULL,consume,NULL);
pthread_join(tid_produce,NULL); pthread_join(tid_consume,NULL); sem_destroy(&shared.mutex); sem_destroy(&shared.empty); sem_destroy(&shared.full);
}