Answered step by step
Verified Expert Solution
Question
1 Approved Answer
A multi - threading application ( messageprint . c ) : in this exercise, you will write a simple Message Print program ( similar to
A multithreading application messageprintc: in this exercise, you will write a simple Message Print program similar to the following example code in which multiple threads print messages with a controlled order. In particular, we want threads with an even thread ID to print first, and those threads with an odd thread ID to print after all the even threads have done the printing. Note that all threads will start in a random order, we must have odd threads wait until all the even threads have printed.
To achieve the eventsprintfirst functionality, you need to use the condition variable routines in the PThread library:
pthreadcondwaitpthreadcondt condition pthreadmutext lock;
pthreadcondsignalpthreadcondt condition;
pthreadcondbroadcastpthreadcondt condition;
The pthreadcondwait routine will put the caller thread to sleep on the condition variable condition and release the mutex lock, guaranteeing that when the subsequent line is executed after the caller has woken up the caller will hold lock. The pthreadcondsignal routine wakes up one thread off of the condition variable condition, and pthreadcondbroadcast wakes up all threads that are sleeping on condition variable condition. IMPORTANT: to call any of the above routines, the caller must first hold the lock that is associated with that condition variable.
You'll need to have these global variables in your messageprint.c file:
pthreadmutext lock PTHREADMUTEXINITIALIZER;
pthreadcondt CV PTHREADCONDINITIALIZER;
int numberevensfinished ;
You will need to use pthreadmutexlock, pthreadmutexunlock, pthreadcondwait, and pthreadcondbroadcast. Do not use pthreadcondsignal.
InputOutput requirements:
Input: your message print application messageprint should take an integer commandline augument that allows the evaluator TA to specify the number of threads to be created. Your program should support at least threads. For example:
$ messageprint
Output: each created thread should print out one or two lines of messages. Again, all odd numbered threads must print after the even threads have done the printing. Avoid printing or more lines of message for each thread.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started