In: Computer Science
2. Which of the generic architectures considered in lectures is most susceptible to race conditions. Explain your reasoning -- why do you believe that your chosen architecture is most susceptible?
Race Conditions: A race condition is an abnormal situation that occurs when a system attempts to perform more than one operations at the same time.
The race condition could be in the form of system, processes,
threads, etc.
The generic architecture would be writing by more than one systems,
processes, threads at a time.
For two processes - there are 4 possible architectures
1. Read by Process 1, Read by Process 2
2. Read by Process 1, Write by Process 2
3. Write by Process 1, Read by Process 2
4. Write by Process 1, Write by Process 2
Let's take a simple example to understand why
writing by more than one processes is the most susceptible to the
race condition.
// Example of a variable
/*
x = 2;
// Any process is modifying the value of x here.
if ( x == 2 ) {
x = 10;
}
'''
There are some more instructions to the program accessing variable x.
'''
*/
Suppose, there are two processes accessing the above program - named process A and process B. The program is sharing data among processes.
for both processes, they should get 10 as the value of x after if condition.
But suppose, A is accessing it at the first so A should get 10 and so do B is but when B is trying to access the data, there's a process C who is modifying the value of x before B gets to the if condition, So B won't enter in the if condition and B would get 2 as the value x.
Since A and B needed to get 10 as the value, but another process
C writes the value of x before B writes it so there would be a race
condition.
I have chosen Write-Write situation because of multiple processes
trying to modify the value of x at the same time. If only one
process is writing/modifying the value of x at one time, then there
won't be any conflict because all of the processes except the one
who is modifying x, would get the updated value of x.
The above example would get you a good idea of race condition.
Another example would be `two people are trying to book the same
ticket at the same time`.
So you should use any locking mechanism for preventing them for the
race condition such as semaphores, mutual exclusion, etc.