Process Synchronization Print
Process Synchronization Print
• Lock Variable
• Test and Set Lock
• Turn Variable
• Interest Variable
Lock Variable-
It is implemented as-
Test-and-Set Instruction
• It is an instruction that returns the old value of a memory location and sets the
memory location value to 1 as a single atomic operation.
• If one process is currently executing a test-and-set, no other process is allowed to
begin another test-and-set until the first process test-and-set is finished.
It is implemented as-
Initially, lock value is set to 0.
• Lock value = 0 means the critical section is currently vacant and no process is present inside
it.
• Lock value = 1 means the critical section is currently occupied and a process is present
inside it.
Turn Variable-
It is implemented as-
Interest Variable-
It is implemented as-
Initially, interest [0] and interest [1] are set to False.
• Interest value [0] = False means that process P0 is not interested to enter the critical section.
• Interest value [0] = True means that process P0 is interested to enter the critical section.
• Interest value [1] = False means that process P1 is not interested to enter the critical section.
• Interest value [1] = True means that process P1 is interested to enter the critical section.
Problem-01:
The enter_CS( ) and leave_CS( ) functions to implement critical section of a process are realized
using test and set instruction as follows-
void enter_CS(X)
{
while (test-and-set(X));
}
void leave_CS(X)
{
X = 0;
}
In the above solution, X is a memory location associated with the CS and is initialized to 0. Now,
consider the following statements-
1. The above solution to CS problem is deadlock-free
2. The solution is starvation free
3. The processes enter CS in FIFO order
4. More than one process can enter CS at the same time.
Which of the above statements is true?
1. I only
2. I and II
3. II and III
4. IV only
Solution-
Clearly, the given mechanism is test and set lock which has the following characteristics-
• It ensures mutual exclusion.
• It ensures freedom from deadlock.
• It may cause the process to starve for the CPU.
• It does not guarantee that processes will always execute in a FIFO order otherwise there
would be no starvation.
Consider the methods used by processes P1 and P2 for accessing their critical sections whenever
needed, as given below. The initial values of shared Boolean variables S 1 and S2 are randomly
assigned.
Solution-
The initial values of shared Boolean variables S1 and S2 are randomly assigned. The assigned
values may be-
• S1 = 0 and S2 = 0
• S1 = 0 and S2 = 1
• S1 = 1 and S2 = 0
• S1 = 1 and S2 = 1
Case-01: If S1 = 0 and S2 = 0-
In this case,
• Process P1 will be trapped inside an infinite while loop.
• However, process P2 gets the chance to execute.
• Process P2 breaks the while loop condition, executes the critical section and then sets S 2 =
1.
• Now, S1 = 0 and S2 = 1.
• Now, process P2 can not enter the critical section again but process P1 can enter the critical
section.
• Process P1 breaks the while loop condition, executes the critical section and then sets S 1 =
1.
• Now, S1 = 1 and S2 = 1.
• Now, process P1 can not enter the critical section again but process P2 can enter the critical
section.
Thus,
• Processes P1 and P2 executes the critical section alternately starting with process P 2.
• Mutual exclusion is guaranteed.
• Progress is not guaranteed because if one process does not execute, then other process
would never be able to execute again.
• Processes have to necessarily execute the critical section in strict alteration.
Case-02: If S1 = 0 and S2 = 1-
In this case,
• Process P2 will be trapped inside an infinite while loop.
• However, process P1 gets the chance to execute.
• Process P1 breaks the while loop condition, executes the critical section and then sets S 1 =
1.
• Now, S1 = 1 and S2 = 1.
• Now, process P1 can not enter the critical section again but process P2 can enter the critical
section.
• Process P2 breaks the while loop condition, executes the critical section and then sets S 2 =
0.
• Now, S1 = 1 and S2 = 0.
• Now, process P2 can not enter the critical section again but process P1 can enter the critical
section.
Thus,
• Processes P1 and P2 executes the critical section alternately starting with process P 1.
• Mutual exclusion is guaranteed.
• Progress is not guaranteed because if one process does not execute, then other process
would never be able to execute again.
• Processes have to necessarily execute the critical section in strict alteration.
Case-03: If S1 = 1 and S2 = 0-
Case-04: If S1 = 1 and S2 = 1-
Detailed explanation:-
Consider this situation as the processes need to execute three stages- Section P, then the given
code and finally Section Q.
It is evident that semaphores do not control Section P hence, There is no restriction in execution
of P.
Now, we are given 2 semaphores 'a' and 'b' initialized to '1' and '0' respectively.
Take an example of 3 processes (hence n=3, count=0(initially) ) and lets say first of them has
finished executing Section P and enters the given code. It does following changes:-
1. will execute wait(a) hence making semaphore a=0
2. increment the count from 0 to 1 (first time)
3. If(count==n) evaluates FALSE and hence signal(b) is not executed. So semaphore b
remains 0
4. signal(a) hence making semaphore a=1
5. wait(b) But since semaphore b is already 0, The process will be in blocked/waiting state.
First out of the three processes is unable to enter the CODE SECTION Q !
Now say second process completes CODE SECTION P and starts executing the given code. It can
be concluded that it will follow the same sequence (5 steps) as mentioned above and status of
variables will be:- count = 2 (still count<n), semaphore a=1, semaphore b=0 (no change)
Finally the last process finishes execution of CODE SECTION P.
It will follow same steps 1 and 2 making semaphore a=0 and count = 3
3. if(count==n) evalueates TRUE! and hence signal(b) is executed marking semaphore b = 1
FOR THE FIRST TIME.
4 and 5 will be executed the same way.
Now the moment this last process signaled b, the previously blocked process will be able to
execute wait(b) and the very next moment execute signal(b) to allow other blocked/waiting process
to proceed.
This way all the processes enter CODE SECTION Q after executing CODE SECTION P.
Q2.
Q3.
Q3.
(C) I Only is correct.
Increasing the memory size increases the rate at which requests are satisfied but can not alter the
possibility of deadlock and neither does it play any role in implementation.
Q.4
Each P operation decreases the value of counting semaphore by 1 and each V operation
increases the value of counting semaphores by 1
Here given value of counting semaphore is 7 then 20 p operation will reduces it to -13
then in order to increase it to 5 18 V operations are needed
hence x=18 ans is A.
Q6.
Let us take a smaller example where P(S) = 5 and V(S) = 2, Now we need 2 more so that atleast
one process will be blocked.So we can formulaize it asLargest initial value of S so that atleast one
get blocked = P(S) - V(S) = 1 + x and value of x is our answer.
here P(S) = 20 , V(S) = 12
thus 20 - 12 = 1 + x
x=7 is answer.
Q7.
There is strict alternation i.e. after completion of process 0 if it wants to start again.It will have to
wait until process 1 gives the lock.
This violates progress requirement which is, that no other process outside critical section can stop
any other interested process from entering the critical section.
Hence the answer is that it violates the progress requirement.
The given solution does not violate bounded waiting requirement.
Bounded waiting is : There exists a bound, or limit, on the number of times other processes are
allowed to enter their critical sections after a process has made request to enter its critical section
and before that request is granted.
Here there are only two processes and when process 0 enters CS, next entry is reserved for
process 1 and vice-versa (strict alteration). So, bounded waiting condition is satisfied here.
Correct Answer: C
Q8.
The answer is option B Currently semaphore
is 7 so, after 20 P(wait) operation it will come
to −13 then for 15 V(signal)
operation the value comes to 2.
Q9.
When both processes try to enter critical section simultaneously, both are allowed to do so
since both shared variables varP and varQ are true. So, clearly there is NO mutual
exclusion. Also, deadlock is prevented because mutual exclusion is one of the necessary
condition for deadlock to happen. Hence, answer is (A).
Or//
Q10.
Q11.
Q12.
Q13.
Say, X and Y be the processes. X increments x by 1 and Z decrements x by 2. Now, Z stores back and
after this X stores back. So, final value of x is 1 and not −1 and two Signal operations make the
semaphore value 2 again. So, now W and Z can also execute like this and the value of x can
be 2 which is the maximum possible in any order of execution of the processes.
(If the semaphore is initialized to 1, processed would execute correctly and we get the final value
of x as −2.)
Q14.
Acquire Lock gets success only when Fetch_And_Add gets executed with L = 0. Now
suppose P1 acquires lock and make L=1. P2 waits for a lock iterating the value
of L between 1 and 2 (assume no other process waiting for lock). Suppose when P1 releases lock by
making L=0, the next statement P2 executes is L=1. So, value of L becomes 1 and no process is in
critical section ensuring L can never be 0 again. Thus, (B) choice.
Q15.
First P0 will enter the while loop as S0 is 1. Now, it releases both S1 and S2 and one of them must
execute next. Let that be P1. Now, P0 will be waiting for P1 to finish. But in the mean time P2 can also
start execution. So, there is a chance that before P0 enters the second iteration both P1 and P2 would
have done release (S0) which would make S1 1 only (as it is a binary semaphore). So, P0 can do only
one more iteration printing ′0′ two times.
If P2 does release (S0) only after P0 starts its second iteration, then P0 would do three iterations
printing ′0′ three times.
If the semaphore had 3 values possible (an integer semaphore and not a binary one), exactly
three ′0′s would have been printed.
Q15
Answer is (A). In this mutual exclusion is satisfied,only one process can access the critical section at
particular time but here progress will not satisfied because suppose when S1=1 and S2=0 and
process P1 is not interested to enter into critical section but P2 want to enter critical section. P2 is not
able to enter critical section in this as only when P1 finishes execution, then only P2 can enter (then
only S1=S2 condition be satisfied).
Progress will not be satisfied when any process which is not interested to enter into the critical section
will not allow other interested process to enter into the critical section. When P1 wants to enter the
critical section it might need to wait till P2 enters and leaves the critical section (or vice verse) which
might never happen and hence progress condition is violated.
Q16.
1. Mutual Exclusion as test-and-set is an indivisible (atomic) instruction (makes option (IV) wrong)
2. Progress as at initially X is 0 and at least one process can enter critical section at any time.
But no guarantee that a process eventually will enter CS and hence option (IV) is false. Also, no
ordering of processes is maintained and hence III is also false.
Q18
Q19
Q20.
P1 can do wants1 = true and then P2 can
do wants2 = true. Now,
both P1 and P2 will be waiting in the
while loop indefinitely without any
progress of the system - deadlock.
Suppose the slots are full →F=0 . Now, if Wait(F) and Wait(S) are interchanged and Wait(S) succeeds,
The producer will wait for Wait(F) which is never going to succeed as Consumer would be waiting for
Wait(S). So, deadlock can happen.
Q22.
C. p1 :line 1|p2 : line 3|p2 line 4 (block) |p1 line 2 (block) here, p1 wants sx and p2 wants sy, but
both will not be release by its process p1 and p2 because there is no way to release them. So,
stuck in deadlock.
D. p1 :line 1 |p2 : line 3 (block because need sx ) |p1 line 2|p2 : still block |p1: execute cs then up
the value of sx |p2 :line 3 line 4(block need sy)|p1 up thesy |p2 :lin4 4 and easily get cs.
We can start from p2 also, as I answered according only p1, but we get same answer.
So, option (D) is correct
OR/
Q23.
Q24.