QUESTIONS, THOSE AREFOR PART A OF THIS ASSIGNMENT. I AM ASKING FOR PART B.
I will downvote copies of part A.
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 do Work() that calls do CriticalWork() in an infinite loop. Function doCritical Work()) 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(...); // execute m-section // limit access to m threads doCriticalWork(...); // run by max. m threads // leave m-section leave(...); // 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.
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 do Work(). Use the pthread library and the API declared in the pthread.h header file. Include a screenshot with the program running.
PLEASE DO NOT COPY FROM OTHER answers PLEASE DO NOT COPY FROM OTHER answers QUESTIONS, THOSE ARE FOR PART A OF THIS ASSIGNMENT. I AM ASKING FOR PART B. I will d
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am