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

Explain the concepts of race condition, critical section (or region) and mutual

ID: 3833972 • Letter: E

Question

Explain the concepts of race condition, critical section (or region) and mutual exclusion in the context of communication. (b) Briefly describe the technique called "strict alternation' and discuss whether it provides a satisfactory solution to the critical section problem. (c) Consider the following pseudo-code: shared boolean flag [2]; flag [0] = flag [1] = FALSE; proc (int i) {while (TRUE) {while (flag [(i + 1) mod 2] == TRUE);//loop till FALSE flag [i] = TRUE; critical_section; flag [i] = FALSE;}} Explain whether this code solves the critical section problem for two processes.

Explanation / Answer

3(a)

RACE CONDITION : This is a condition which occurs when 2 different program tries to alter same resource. Lets consider this example:

int sum=0;

void add(int x){

sum=sum+x;

}

let say that there are 2 programs A and B who tries to add 5 and 6 to sum respectively, and lets consider the initial value of sum be 5. both the program will read the value of sum as 5, both will do respective computation and in the end A tries to modify the value of sum as 10 and B tries to modify the value of sum as 11. This will create the Race condition.

CRITICAL SECTION: The section of code where a race condition can occur is critical section.

int sum=0;

void add(int x){

sum=sum+x; //this is the critical section of the code, because of this addition, race condition can occur

}

MUTUAL EXCLUSION: It is a technique which prevents the race condition to occur.

int sum=0;

void add(int x){

sum=sum+x;

}

in the above code if the function is accessed by A then B will not be able to access it. It basicall means only one process can change the value of sum at any given time. If any other process wants to change it,it will have to wait until the previous process has completed its execution.

3(b) Strict alternation is a type of mutual exclusion. in this method, there will be a commonly shared variable, lets say mutex, which is changed to 1 if a process has entered a critical section. other process checks what is the value of mutex, if it is one it will check again and again in the loop to see whether it has changed to 0 or not. until and unless the value of mutex is changed to 0, no other process will enter the critical section.

the pseudo-code for the following can be written as::

while(mutex!=0){

mutex=1;

execute_critical_section();

mutex=0;

}

Now, the problem with this approach is, that there will be, that it increases the processing overhead and CPU usage is high and a process may have to busy wait long enough.

3(c) Yes, the following code will solve the critical section problem for two processes. Before entering the critical section, the process will check the flag status of another process. 0 will check the flag status of 1 and vice versa. Lets say that the 0 is inside the critical section, 1 will check the flag status of 0. Since it is true,while(flag[i+1%2]==true); will be kept on executing. which will hold the process1 to get inside the critical section.

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