# 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,...
TRUE-FALSE Basic synchronization principles and multithreading 1. Java user threads can implement both busy-waiting and no-busy-waiting policy. 2. Priority inversion avoids deadlocks. 3. Spinlock mutex can be used as an adaptive mutex. 4. Java RTE can be blocked for Input/Output operation. 5. Interrupted user thread, which executes a method in a monitor, must be rolled back to undo any changes it performed. 6. The synchronization primitive by disabling interrupts can be used by an application program. 7. Bounded-waiting requirement is...
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...
Multi-threaded programming help!!! Can anyone solve this problem? It has to be written in C, runs on Linux. his time you need to rewrite a multithreaded Pthread program that works with sleep() or pthread_join() in order to print out Fibonacci sequences properly. Create a folder name, WK6 on your class Linux machine when you work. gcc assignment3.c-Wall -Werror -pthread You are required to develop your own program using C with the following information Need to declare a function that receives...
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++) {...
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...
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...