Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Kindly help out with the solution and program code for this. Thank you We define an m-section to be a sequence of code that can

Kindly help out with the solution and program code for this. Thank you

We define an m-section to be a sequence of code that can be run concurrently by maximum m threads. There are n threads in a process.

Each thread executes a thread function doWork() that calls doCriticalWork() in an infinite loop.

Function doCriticalWork() requires that at most m threads run it concurrently.

The enter() and leave() functions are used to limit the number of threads within the m- section to a maximum of m and are the only functions that deal with synchronization.

The pseudo-code algorithm for the thread function is this:

void doWork(...)

{ while (true)

{ enter(...); // limit access to m threads // execute m-section

doCriticalWork(...); // run by max. m threads leave(...); // leave m-section // do more work }

} Function enter() returns immediately only if there are less than m threads in the m-section. Otherwise, the calling thread will be blocked. A thread calls leave() to indicate it has finished the m-section.

If there was another thread blocked (in enter()) waiting to enter the m-section, that thread will now be resumed and allowed to continue, since there are now less than m threads remaining in the m-section.

a) Write a C program called msection-sem.c using the pthread library that implements the algorithm above. For the enter() and leave() functions use only one shared semaphore for synchronization. This is actually very easy if you know how a counting semaphore works. Write the enter() and leave() functions so that they are reusable, i.e. not depending on global variables. Declare any necessary shared and global variables as needed and also specify the parameters for the three functions.

Declare M as a global integer variable and hard-code its value to 3. The doCriticalWork() function within the m-section must print the current thread id and the number of threads currently in the msection to the terminal using printf(). The main() function should create and start N=10 threads that call doWork(). Use the pthread library and the POSIX semaphore API from the semaphore.h header file. Include a screenshot with the program running.

b) Write a C program called msection-condvar.c using the pthread library that implements the algorithm above. The enter() and leave() functions must use only one condition variable and one or more mutexes for synchronization. Do not use semaphores. Write the enter() and leave() functions so that they are reusable, i.e. not depending on global variables. Declare any necessary shared and global variables as needed and also specify the parameters for the three functions. Declare M as a global integer variable and hard-code its value to 3. The doCriticalWork() function within the m-section must print to the terminal using printf() the current thread id and the number of threads currently in the m-section. The main() function should create and start N=10 threads that call doWork(). Use the pthread library and the API declared in the pthread.h header file. Include a screenshot with the program running.

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

Recommended Textbook for

Relational Database And Transact SQL

Authors: Lucy Scott

1st Edition

1974679985, 978-1974679980

Students also viewed these Databases questions

Question

d. Who are important leaders and heroes of the group?

Answered: 1 week ago