Question: Task 1 . Producer Consumer Threads This task is intended to provide a solution to the Producer Consumer problem using threads, mutex, and a condition
Task Producer Consumer Threads
This task is intended to provide a solution to the Producer Consumer problem using threads, mutex, and a condition variable. Reference Figure in the textbook. Note that the code in Figure is written in C but your program can be developed in C
Instructions
Develop a solution to the Producer Consumer problem using threads, mutex, and a condition variable. The program will Produce and Consume items. Create two threads, one for the Producer and one for the Consumer
Use a mutex and condition variable to prevent the race condition and enable synchronization so that only one thread updates the buffer at any given time
For the output, display the buffer value in the Producer and Consumer threads whenever it is changed.
Include the header for the C thread class.
Include the header for the C mutex class.
Include the header for the C conditionvariable class.
Note that you will need to compile with the pthread option to link with the pthread library. For example:
$ gcc o pthread pthread pcthread.c
$ go pcthread pthread pcthread.cpp
Sample output:
Threads started
In producer: buffer
In consumer: buffer
In producer: buffer
In consumer: buffer
In producer: buffer
In consumer: buffer
In producer: buffer
In consumer: buffer
In producer: buffer
In consumer: buffer
In producer: buffer
In consumer: buffer
In producer: buffer
In consumer: buffer
In producer: buffer
In consumer: buffer
In producer: buffer
In consumer: buffer
In producer: buffer
In consumer: buffer
Ending main: buffer
Task Locking and Unlocking a Mutex
Modify the C program given below so that two created threads can avoid race conditions using mutual exclusion.
threadincr.c:
#include
#include
#include
#include
#include
#include
static volatile int glob ;
static void Loop 'arg' times incrementing 'glob'
threadFuncvoid arg
int loops int arg;
int loc, j;
for j ; j loops; j
loc glob;
loc;
glob loc;
return NULL;
int mainint argc, char argv
pthreadt t t;
int loops, s;
loops argc atoiargv : ;
s pthreadcreate&t NULL, threadFunc, &loops;
if s
printfpthreadcreate";
s pthreadcreate&t NULL, threadFunc, &loops;
if s
printfpthreadcreate";
s pthreadjoint NULL;
if s
printfpthreadjoin";
s pthreadjoint NULL;
if s
printfpthreadjoin";
printfglob d
glob;
exitEXITSUCCESS;
Use following two POSIX thread functions:
int pthreadmutexlockpthreadmutext mutex;
int pthreadmutexunlockpthreadmutext mutex;
Compile and run your code Note that you will need to compile with the pthread option to link with the pthread library.:
$ gcc o threadincr pthread threadincr.c
$ threadincr
When you run your code, it should show:
$ glob
Submit followings in the Canvas:
Task : pcthreadyourname.c or pcthreadyourname.cpp
Task : threadincryourname.c or threadincryourname.cpp
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
