Question
(C language) Write a program to simulate 100 people (threads) that enter a stadium. They get up a few times, let's say 1000 times (for
(C language)
Write a program to simulate 100 people (threads) that enter a stadium. They get up a few times, let's say 1000 times (for food, etc.), and return to their seats. They pass through a counter each time they return to their seat. Write a program that consists of one threaded function that you invoke via 100 pthreads. That threaded function should increment a shared variable (initialized to 0, and declared as a volatile variable), 1000 times each. Note - if your system does not allow you to spawn so many threads, you may use fewer threads and/or a larger number of "times they get up."
Run this program 10 times, and time the runs. What is the average runtime of these runs? Also, record the final output of the counter. It should be 100000, but it likely will not be. What was it on each run?
What is the problem with this program? Now, create a new version of your program, and using mutex locks (lock inside the for loop), fix the code, and re-run it 10 times. Again, time the runs, and give the average runtime. As before, give the final output of the counter (this time, it really should be 100000). How much slower was this program than the one without mutex locks?
Now, in a new version of the program, move the locks so that they lock and unlock only once per thread (outside the for loop). Verify the correct value is still obtained, and find the average runtime over 10 runs again. What do you observe as compared to the other two versions?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started