Description

  • 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?
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.