Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A multi-threaded program generates an incorrect answer some of the time raising

ID: 3810105 • Letter: A

Question

A multi-threaded program generates an incorrect answer some of the time raising the possibility of a race condition. Which of the following are steps that can reduce or even eliminate race conditions in the program? Underline the R, E, or U below to indicate that the given approach can Reduce race conditions, Eliminate them, or is Useless when it comes to race conditions. Write a sentence to explain your answer.

R    E    U       Separate the multithread program into multiple single-threaded programs, and run each thread in its own process. Share data between the programs via named pipes and read and write calls. No other changes to the program.

R    E    U       Same as above, but share data between the programs via shared memory segments.

R    E    U       Apply the “one-writer” rule. Here you modify the program so that each variable has only one writer, i.e. only one thread can write to a shared variable. Multiple threads can read from it.

R    E    U       Ensure that each shared variable is protected by some lock.

Explanation / Answer

R E U Separate the multithread program into multiple single-threaded programs, and run each thread in its own process. Share data between the programs via named pipes and read and write calls. No other changes to the program.
This approach will reduce race condition as the variables will be shared via named pipes and the thread can read it only after the previous thread writes it.
But there is a chance that final thread finishes earlier than other threads and wrong value is written.

R E U Same as above, but share data between the programs via shared memory segments.
This approach is useless as the shared memory can be accessed by the individual threads independently and they can read and write them whenever they want.

R E U Apply the “one-writer” rule. Here you modify the program so that each variable has only one writer, i.e. only one thread can write to a shared variable. Multiple threads can read from it.
This approach will reduce race condition as there can be situation where other threads have to read the value updated by first thread but instead reads the previous value which is wrong and finally writes the wrong value.

R E U Ensure that each shared variable is protected by some lock.
This approach will eleminate race condition as the thread having the lock will only be able to read and write it. So if we can have the threads access the locks in the way we want and output will be as desired.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote