Ch05 Problems Solving
Ch05 Problems Solving
Answer:
The concept of concurrency is related to the things that happen at the same
time in a system. Concurrency leads to the following issues.
Deadlock: Two or more processes executing in parallel wait for the resource
that is held by another waiting process. These processes never run to
completion and the situation is termed as deadlock.
P1 : { P2 : {
shared int x; shared int x;
x =10 ; x = 10 ;
while (1) { while (1) {
x = x – 1; x = x – 1;
x = x + 1; x = x + 1;
if (x != 10) if (x != 10)
printf (“x is %d”,x) printf (“x is %d”,x)
} }
} }
} }
Answer:
a) The process P1 assigns the value of x to 10. The CPU executes the
instruction corresponding to P1 to decrement x then increment x
so that the value of x is 10. Then it executes the statement in P2
to decrement x so that x becomes 9. Now, it checks whether x
equals 10 corresponding to P1. It increments x and then executes
the statement to display x which displays “x is 10”.
The sequence of interleaved statements are mentioned below:
P1: x = x-1; 9
P1: x = x+1; 10
P2: x = x-1; 9
P1: if (x!= 10); 9
P2: x = x+1; 10
P1: printf (“x is %d”, x); 10
“ x is 10” is printed.
9) Consider the following program:
This software is a solution to the mutual exclusion problem for two processes .
Find a counterexample that demonstrates that this solution is incorrect.
Answer:
Consider the scenario when turn equals to 0 and P[1] sets blocked[1] to true. It
finds that blocked[0] is set to false. Now, P[0] sets blocked[0] to true. It finds
turn equals 0 and therefore, enters the critical section, since the condition while
(turn != id) is false. P[1] sets turn equals to 1 and therefore, it exits the loop
while (turn != id). So, P1 also enters the critical section. Hence, mutual
exclusion is not enforced properly in the mentioned algorithm.
10) Demonstrate the correctness of Dekker’s algorithm.
1. Show that mutual exclusion is enforced. Hint: Show that when Pi enters
its critical section, the following expression is true:
2. Show that a process requiring access to its critical section will not be
delayed indefinitely. Hint: Consider the following cases: (1) a single
process is attempting to enter the critical section; (2) both processes are
attempting to enter the critical section, and (2a) turn = 0 and
flag[0] = false, and (2b) turn = 0 and flag[0] = true.
Boolean flag[2];
Void P1()
int turn;
{
Void P0()
while (true) { Void main ()
{
flag[1] = true; {
while (true) {
while (flag[0]) { flag[0] = false;
flag[0] = true;
if (turn == 0) { flag[1] = false;
while (flag[1]) {
flag[1] = false; turn = 1;
if (turn == 1) {
while (turn ==0) parbegin (P0, P1);
flag[0] = false;
/* do nothing */; }
while (turn ==1)
flag[1] = true;
/* do nothing */;
}
flag[0] = true;
}
}
/* critical section */;
}
turn = 0;
/* critical section */;
flag[1] = false;
Turn = 1;
/* remainder */;
flag[0] = false;
}
/* remainder */;
}
}
}
Solution:
Part1:
First, consider that a process P1 is in its critical section and then prove that no
other process is allowed to enter its critical section until P1 is out of its critical
section.
Considering the Dekker’s algorithm , assume that the process P1 enters the
critical section first. Then, according to the algorithm, flag[1] must be true.
Now, another process P0 wants to enter the critical section. Since P1 is in the
critical section, P0 will be executing outside the critical section. The while loop
is the only construct that stops P0 from entering the critical section. As long as
P1 is executing the critical section, flag[1] is true. P0 won’t be able to exit the
while loop as long as flag[1] is true, that is, it won’t be able to enter the critical
section until flag[1] is set to false.
The process P1 sets flag[1] to false only after exiting the critical section. When
one process is in the critical section, other processes are not allowed to enter
the critical section. Thus, it enforces mutual exclusion.
Part2:
Prove that the process can enter the critical section whenever it wants to.
Assume process P1 wants to enter the critical section. The while loop is the
only construct that blocks a process from entering the critical section.
There are two possible outcomes of the while loop condition which are
mentioned below:
Case 1: flag[0] = false
It indicates that P0 is not in the critical section and therefore, P1 enters the
critical section. The other possibility is that P1 is not able to reach while(flag[0])
since it is trapped in the while condition while(turn-==0). This means that
flag[1]=false.
Therefore, P0 enters the critical section, and eventually when it exits, it sets
flag[0] to false and turns to 1.
Now , P1 exits the loop while(turn==0) and enters the critical section, since
flag[0] = false.
Case2 : flag[0] = true
Currently, P0 is executing the critical section. Once it finishes it sets turn=1 and
flag[0] =false. Now , process P1 exits the loop while(flag[0]) and enters the
critical section.
Thus, no process starves according to Dekker’s algorithm.
All the processes are equivalent and no priority is assigned to any process, so
eventually, any process will be able to access the critical section.