Answers for Homework 4

1. No.

Assume initially a=b=3, the following is the two possible sequences that cause a!=b

(1)

a:=a+2-----P1

b:=b*3-----P2

b:=b+2-----P1

a:=a*3-----P2

The result is : a=15 b=11

(2)

b:=b*3-----P2

a:=a+2-----P1

b:=b+2-----P1

a:=a*3-----P2

The result is : a=15 b=11

2. Critical section is a segment of code in which a process utilizes some shared resources such as variables and files. Only one process can execute in the critical section at any time. So each process must request permission before entering the critical section.

Critical region is a high-level synchronization construct. It solves the critical section problem by preventing the multi-process from entering the critical section at the same time.

3. If we want to maintain a=b, we must not interrupt any execution of P1 and P2. So we define a critical section for P1, P2 and add some condition to allow only one process into the critical section. The following is an example of coding.

var flag: array[1..2] of boolean;

var turn: 1..2

Assume initially flag[1]=flag[2]=false

P1:

flag[1]:=true;

turn:=2;

while(flag[2] and turn=2) do no-op;

(enter critical section)

a:=a+2;

b:=b+2;

(exit from critical section)

flag[1]:=false;

P2:

flag[2]:=true;

turn:=1;

while(flag[1] and turn=1) do no-op;

(enter critical section)

a:=a+2;

b:=b+2;

(exit from critical section)

flag[2]:=false;




NOTE: The answer is just for reference. If you have any question about it, please email me at nxz5371@megahertz.njit.edu