Synchronization (Social Distancing Problem) C++ multithreading 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 kids—vaccinated, and non-vaccinated. The vaccinated kids have immunity against the virus hence don’t 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 nonvaccinated) 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 if the pseudocode for the non-vaccinated kids threads: do { wait(play_mutex); ... /* play at the structure */ Play(…); … signal(play_mutex); } while (true); Below is the pseudocode for the Vaccinated-kids threads: do { wait(mutex); vaccinated_kids_count++; if (vaccinated_kids_count == 1) { wait(play_mutex); } signal(mutex); ... /* play at the structure */ Play(…); ... wait(mutex); vaccinated_kids_count --; if (vaccinated_kids_count == 0) signal(play_mutex); signal(mutex); } 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 Print 1: V Thread 3 playing! 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. 1. Just before the “wait(play_mutex)” print: “NV Thread 1 trying to acquire play_mutex.” 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 the first wait(mutex) print: “V Thread 3 acquired mutex for incrementing the count.” 2. Just before the “wait(play_mutex)” print: “V Thread 3 trying to acquire play_mutex.” 3. Just before the first signal(mutex) print: “V Thread 3 about to release mutex after incrementing.” 4. Immediately after the second wait(mutex) print: “V Thread 3 acquired mutex for decrementing the count.” 5. Just before the signal(play_mutex) print: “V Thread 3 about to release play_mutex.” 6. Just before the second signal(mutex) print: “V Thread 3 about to release mutex after decrementing.” Compiling • The 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. o Above sample execution command will create three vaccinated kid threads and six nonvaccinated kid threads in your process.
Synchronization (Social Distancing Problem) C++ multithreading
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 kids—vaccinated, and non-vaccinated. The vaccinated kids have immunity against the virus hence don’t 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 nonvaccinated) 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 if the pseudocode for the non-vaccinated kids threads:
do {
wait(play_mutex);
...
/* play at the structure */
Play(…);
…
signal(play_mutex);
} while (true);
Below is the pseudocode for the Vaccinated-kids threads:
do {
wait(mutex);
vaccinated_kids_count++;
if (vaccinated_kids_count == 1) {
wait(play_mutex);
}
signal(mutex);
...
/* play at the structure */
Play(…);
...
wait(mutex);
vaccinated_kids_count --;
if (vaccinated_kids_count == 0)
signal(play_mutex);
signal(mutex);
} 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 <unistd.h>
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 Print 1: V Thread 3 playing!
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.
1. Just before the “wait(play_mutex)” print: “NV Thread 1 trying to acquire play_mutex.”
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 the first wait(mutex) print: “V Thread 3 acquired mutex for
incrementing the count.”
2. Just before the “wait(play_mutex)” print: “V Thread 3 trying to acquire play_mutex.”
3. Just before the first signal(mutex) print: “V Thread 3 about to release mutex after incrementing.”
4. Immediately after the second wait(mutex) print: “V Thread 3 acquired mutex for decrementing the count.”
5. Just before the signal(play_mutex) print: “V Thread 3 about to release play_mutex.”
6. Just before the second signal(mutex) print: “V Thread 3 about to release mutex after
decrementing.”
Compiling
• The 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.
o Above sample execution command will create three vaccinated kid threads and six nonvaccinated kid threads in your process.
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images