Question
Part B Synchronization (Social Distancing Problem) (60 points) At a playground, kids love to play on a play play structure. However, due to an ongoing
Part B Synchronization (Social Distancing Problem) (60 points) At a playground, kids love to play on a play play structure. However, due to an ongoing pandemic, social distancing needs to be enforced. There are two types of kidsvaccinated, and non-vaccinated. The vaccinated kids have immunity against the virus hence dont need social distancing. The non-vaccinated kids needs to maintain social distancing for their safety. The park managers came up with a solution that both the vaccinated and non-vaccinated kids cannot play at the play structure simultaneously. Below is a pseudocode for a solution using semaphores that allows any number of vaccinated kids on the play structure at the same time, but only one non-vaccinated kid. (All the kids are represented as threads.) If a non-vaccinated kid is playing at the structure, no other kid (either vaccinated or non-vaccinated) can be allowed. Any number of vaccinated kids can be at the play structure together. The solution must implement the required mutual exclusion and avoid deadlock. However, the solution does not need to be fair.
The threads share the following data structures (the following three can be global variables in your program):
Int vaccinated_kids_count = 0; //the number of vaccinated kids currently playing at the structure
Semaphore mutex = 1 // Ensures mutual exclusion when vaccinated_kids_count is updated
Semaphore play_mutex = 1 // provides mutual exclusion for accessing the play structure
Below is the pseudocode for the non-vaccinated kids threads:
Fill in the blanks to ensure the above stated mutual exclusion requirements.
do { wait(___________________); //INSTRUCTION 1
... /* play at the structure */ Play(...); ...
signal(___________________); // INSTRUCTION 2 } while (true);
Below is the pseudocode for the Vaccinated-kids threads:
do { wait(___________________); // INSTRUCTION 3 vaccinated_kids_count++; if (vaccinated_kids_count == 1) {
wait(___________________); // INSTRUCTION 4 }
signal(___________________); // INSTRUCTION 5
... /* play at the structure */ Play(...); ...
wait(___________________); // INSTRUCTION 6 vaccinated_kids_count --; if (vaccinated_kids_count == 0)
signal(___________________); // INSTRUCTION 7
signal(___________________); // INSTRUCTION 8 } while (true);
Here is the pseudocode for the Play():
Play(...)
{ Print 1 //see the note below for details on the print statement
sleep(1); //for the sleep() use #include
Print 2 //see the note below for details on the print statement }
The print statements should print the following information:
For non-vaccinated (NV) kids thread, assuming thread Id is 1:
o Print 1: NV Thread 1 playing!
o Print 2: NV Thread 1 playing done!
For vaccinated (V) kids thread, assuming thread Id is 3:
o Print1:VThread3playing!
o Print 2: V Thread 3 playing done!
You can decide the function prototype for Play() as needed for your program, but the Play() function must:
1) have a sleep() for 1 second as shown above;
2) print the thread Id and type of thread (vaccinated or non-vaccinated kid thread) executing in play, as illustrated above.
Additional directions: NOTE: Use printf(...) statements for printing; do not use cout. Otherwise, your output may not get printed together. Add the following print statements in the non-vaccinated kids threads. You will need to print the thread Id of the thread with every statement. The example statements below are assuming the thread Id of the non-vaccinated kids thread is
1. NV is abbreviation for Non-Vaccinated. Refer to the instruction numbers in the pseudocode above.
2. Just before INSTRUCTION 1 print: NV Thread 1 DESIRING to PLAY! Add the following print statements in the vaccinated kids threads. You will need to print the thread Id of the thread with every statement.
The example statements below are assuming the thread Id of the vaccinated kids thread is
3. V is abbreviation for Vaccinated.
1. Immediately after INSTRUCTION 3 print: V Thread 3 about to INCREMENT the count.
2. Just before INSTRUCTION 4 print: V Thread 3 DESIRING to PLAY!
3. Just before INSTRUCTION 5 print: V Thread 3 DONE INCREMENTing the count.
4. Immediately after INSTRUCTION 6 print: V Thread 3 about to DECREMENT the count.
5. Just before INSTRUCTION 7 print: V Thread 3; LAST one DONE PLAYING. Will SIGNAL!
6. Just before INSTRUCTION 8 print: V Thread 3 DONE DECREMENTing the count.
The above print statements will help you debug your code and verify its correctness.
Include all the above print statements as directed.
In your final submission, do not include any other print statements besides the ones mentioned above. The above print statement MUST be included in your final submission.
If we are unable to verify the correctness of your implementation, for instance, due to missing or unexpected or inaccurate print (and sleep()) statements, you will not get credit for the unverified features. Compiling and running your program:
Your program should accept the following two integers as command line inputs: first, number of vaccinated kids threads to be created; second, number of non-vaccinated kids threads to be created. o If less than two inputs are supplied or unexpected input (e.g., floats instead of int) is supplied, print out an informative message asking for the correct inputs and terminate the program.
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