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

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
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

Post by answerhappygod »

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 1
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 1 (45.98 KiB) Viewed 34 times
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. 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.
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 2
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 2 (53.27 KiB) Viewed 34 times
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.
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply