We define an m-section to be a sequence of code that can be run concurrently by maximum m threads. There are n threads i
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am
We define an m-section to be a sequence of code that can be run concurrently by maximum m threads. There are n threads i
the calling thread will be blocked. A thread calls leave() to indicate it has finished the m-section. Ifthere was another thread blocked (in enter()) waiting to enter the m-section, that thread will now beresumed and allowed to continue, since there are now less than m threads remaining in the m-section.
This is 1 question (Question b), I included the first screenshot to provide context. Please answer only question b and do not use any semaphores. 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 do Work() that calls doCriticalWork() 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
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 doCritical Work() 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.