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: 3810457 • 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

1. Using named pipes: when this is done, multiple processes can write into a stream which is fed into the process which is reading. Named pipes have a blocking nature. So if one process writes something into the pipe then it is blocked until a process wants to read it and vice versa. So, synchronization is built into it. So, race condition won't occur.

E: Eliminates race condition. But this is slower and is mostly suitable for one process to another process communication and not for multiple process communication.

----------------------------------------------

2. Using shared memory: It is possible that while a process reads a data and does some operation that depends on a particular value of that data, some other process changes the value of that data. Our initial process is unaware of this change. So it is performing some operation that shouldn't be done. Hence, race condition is possible.

To prevent race condition, we need to implement some kind of synchronization code into our program but it is mentioned in question that no other change is made to the program.

U: useless when it comes to race conditions because both read-write and write-write conflicts can occur.

------------------------------------------------

3. One writer rules: This means that while data is read by one process, it can be written by other. So take a scenarion of process 1 reading a data and working on something that depends on a particular value of the read data. Now process 2 changes that data. Process1 is unaware of this change. So race condition arises.

R: reduce race condition because this prevents the write-write conflicts but still read-write conflicts exist.

-----------------------------------------------

4. Using locks: This can ensure that there is no other process accessing the data if it is being locked by a writing process. And no other process can modify data if it is being locked by a reading process. The rules set can vary depending on implementation. However this is a great way to ensure conflicts are prevented. So, this can prevent race condition.

E: Eliminates race condition

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