Question

Part 3. IPC (InterProcess Communication) Programming In Part 3, you are asked to develop a multithreaded program and a Makefi

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Save the following file as ProducerConsumer.c
//Contact me in case of any help needed
//Please vote

# 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 :
roducer produced one item : l Producer produced one item : 2 Producer produced one item 3 Producer produced one item : 4 Prod

//for makefile you can use :
/*

ProducerConsumerMake : ProducerConsumer.c
gcc -o ProducerConsumerMake ProducerConsumer.c -I

*/

Add a comment
Know the answer?
Add Answer to:
Part 3. IPC (InterProcess Communication) Programming In Part 3, you are asked to develop a multit...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Can someone help create this program for Linux. Part 3. IPC (InterProcess Communication) Programming In Part...

    Can someone help create this program for Linux. 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...

  • operating system engineering , please read the question and solve on the given skeleton code ....

    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,...

  • 1. True-false or multiple choice. Provide a one-sentence explanation you will get no credit for a correct answer without a proper explanation. Suggested time to spend on these questions: 10 minutes....

    1. True-false or multiple choice. Provide a one-sentence explanation you will get no credit for a correct answer without a proper explanation. Suggested time to spend on these questions: 10 minutes. [4x5-20ptsl (a) True or false: Peterson's algorithm for mutual exclusion offers bounded wait. b) Thue or falher Cumsider a concumcent program comitingof several 1oad indctioms e instructions (or instructions with similar effects as store). Such a program can never have any race conditions (c) True or false: Solutions based...

  • In c programming The Consumer Submits processing requests to the producer by supplying a file name, its location and a character. It also outputs the contents of the file provided by the producer...

    In c programming The Consumer Submits processing requests to the producer by supplying a file name, its location and a character. It also outputs the contents of the file provided by the producer to the standard output. The Producer Accepts multiple consumer requests and processes each request by creating the following four threads. The reader thread will read an input file, one line at a time. It will pass each line of input to the character thread through a queue...

  • I must execute in C using parallel Programming techniques the following serial program: void producer_consumer(int *buffer,...

    I must execute in C using parallel Programming techniques the following serial program: void producer_consumer(int *buffer, int size, int *vec, int n) {    int i, j;    long long unsigned int sum = 0;    for(i=0;i<n;i++) {        if(i % 2 == 0) {   // PRODUCER            for(j=0;j<size;j++) {                buffer[j] = vec[i] + j*vec[i+1];            }        }        else {   // CONSUMER            for(j=0;j<size;j++) {...

  • Is This Correct? semaphore fillCount = 0; // Items produced semahore emptyCount = BUFFER_SIZE; // remaining...

    Is This Correct? semaphore fillCount = 0; // Items produced semahore emptyCount = BUFFER_SIZE; // remaining space procedure producer() {    While (true) {    item = produceItem(); down(emptyCount); // emptyCount is decremented putItemIntoBuffer(item); up(fillCount); // fillcount is incremented } } procedure consumer() { While (true) {    down(fillCount); item = removeItem(); up(emptyCount); // emptyCount is incremented consumeItem(item); } } Instructions Programming Assignment Four In computing, the producer-consumer problem (also known as the bounded-buffer problem) is a classic example of...

  • Help with a Parallel and Distributed Programming assignment. In this assignment, you will be expl...

    Help with a Parallel and Distributed Programming assignment. In this assignment, you will be exploring different methods of counting the prime numbers between 1 and N. You will use 8 threads, and each will be given a range in which to count primes. The question is: where do you store your counter? Do you use a local variable? Do you use a global variable? Please use the following function to determine whether an integer number is a prime. // Return...

  • How can we assess whether a project is a success or a failure? This case presents...

    How can we assess whether a project is a success or a failure? This case presents two phases of a large business transformation project involving the implementation of an ERP system with the aim of creating an integrated company. The case illustrates some of the challenges associated with integration. It also presents the obstacles facing companies that undertake projects involving large information technology projects. Bombardier and Its Environment Joseph-Armand Bombardier was 15 years old when he built his first snowmobile...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT