Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

What is probate? Describe the probate process.

Answered: 1 week ago

Question

What is meant by organisational theory ?

Answered: 1 week ago

Question

What is meant by decentralisation of authority ?

Answered: 1 week ago

Question

Briefly explain the qualities of an able supervisor

Answered: 1 week ago

Question

Define policy making?

Answered: 1 week ago

Question

Define co-ordination?

Answered: 1 week ago

Question

6. Conclude with the same strength as in the introduction

Answered: 1 week ago

Question

7. Prepare an effective outline

Answered: 1 week ago