# include <stdio.h>
# include <pthread.h>
# include <stdlib.h>
# define BufferSize 10 //defining buffer size
void *Producer();
void *Consumer();
int BufferIndex=0;
char *BUFFER;
pthread_cond_t Buffer_Not_Full=PTHREAD_COND_INITIALIZER;
pthread_cond_t Buffer_Not_Empty=PTHREAD_COND_INITIALIZER;
pthread_mutex_t mVar=PTHREAD_MUTEX_INITIALIZER;
int main()
{
pthread_t ptid,ctid;
BUFFER=(char *) malloc(sizeof(char) * BufferSize); //allocating buffer
pthread_create(&ptid,NULL,Producer,NULL);//creating producer
thread
pthread_create(&ctid,NULL,Consumer,NULL);//creating consumer
thread
pthread_join(ptid,NULL);
pthread_join(ctid,NULL);
return 0;
}
//Producer Code
void *Producer()
{
for(;;)
{
pthread_mutex_lock(&mVar);
if(BufferIndex==BufferSize)
{
printf("Producer found that buffer is full and waiting for the
consumer to consume");
pthread_cond_wait(&Buffer_Not_Full,&mVar);//producer thread
will wait as buffer is full
}
BUFFER[BufferIndex++]='@';
printf("Producer produced one item : %d \n",BufferIndex);
pthread_mutex_unlock(&mVar);
pthread_cond_signal(&Buffer_Not_Empty);
}
}
//Consumer Code
void *Consumer()
{
for(;;)
{
pthread_mutex_lock(&mVar);
if(BufferIndex==-1)
{
printf("Consumer found that the buffer is empty and waiting for the
producer to produce");
pthread_cond_wait(&Buffer_Not_Empty,&mVar);//consumer
thread will wait as buffer is empty
}
printf("Consumer consumed one item : %d \n",BufferIndex--);
pthread_mutex_unlock(&mVar);
pthread_cond_signal(&Buffer_Not_Full);
}
}
//output :
//for makefile you can use :
/*
ProducerConsumerMake : ProducerConsumer.c
gcc -o ProducerConsumerMake ProducerConsumer.c -I
*/
Can someone help create this program for Linux. Part 3. IPC (InterProcess Communication) Programming In Part...
Part 3. IPC (InterProcess Communication) Programming In Part 3, you are asked to develop a multithreaded program and a Makefile to automate the compilation on Linux platform. This assignment assists you for better understanding of processes and threads management, multithreaded programming, and enhancing programming skills and experience with programming on a Unix-like environment. You are asked to implement a multithreaded producer-consumer problem with PThreads library in this programming assignment. The producer-consumer is a common problem that requires cooperating processes or...
operating system engineering , please read the question and
solve on the given skeleton code .
Write a multi-threaded program with Semaphores as counters and pthread_mutex_t mutex to solve the producer-consumer problem: A bounded buffer is simply a global integer array of size N (2) which can be accessed by multiple threads. • Create two types of threads - Producer (2) and Consumer (2). Producers will write to the buffer, and the consumers will read the buffer. In this scenario,...