Question: A multi - threading application ( messageprint . c ) : in this exercise, you will write a simple Message Print program ( similar to

A multi-threading application (messageprint.c): 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 events-print-first functionality, you need to use the condition variable routines in the PThread library:
pthread_cond_wait(pthread_cond_t *condition, pthread_mutex_t *lock);
pthread_cond_signal(pthread_cond_t *condition);
pthread_cond_broadcast(pthread_cond_t *condition);
The pthread_cond_wait 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 pthread_cond_signal routine wakes up one thread off of the condition variable condition, and pthread_cond_broadcast 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:
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t CV = PTHREAD_COND_INITIALIZER;
int number_evens_finished =0;
You will need to use pthread_mutex_lock, pthread_mutex_unlock, pthread_cond_wait, and pthread_cond_broadcast. Do not use pthread_cond_signal.
Input/Output requirements:
Input: your message print application (messageprint) should take an integer command-line augument that allows the evaluator (TA) to specify the number of threads to be created. Your program should support at least 20 threads. For example:
$ messageprint 20
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 3 or more lines of message for each thread.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!