Process Synchronization Print

Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

We have discussed the following synchronization mechanisms-

• Lock Variable
• Test and Set Lock
• Turn Variable
• Interest Variable
Lock Variable-

• Lock variable is a synchronization mechanism.


• It uses a lock variable to provide the synchronizationamong the processes executing
concurrently.
• However, it completely fails to provide the synchronization.

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.
Test and Set Lock –

• Test and Set Lock (TSL) is a synchronization mechanism.


• It uses a testand set instruction to provide the synchronization among the processes
executing concurrently.

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-

• Turn variable is a synchronization mechanism that provides synchronization among two


processes.
• It uses a turn variable to provide the synchronization.

It is implemented as-

Initially, turn value is set to 0.


• Turn value = 0 means it is the turn of process P0 to enter the critical section.
• Turn value = 1 means it is the turn of process P1 to enter the critical section.

Interest Variable-

• Interest variable is a synchronization mechanism that provides synchronization among two


processes.
• It uses an interest variable to provide the synchronization.

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.

Thus, Option (A) is correct.


Problem-02:

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.

Method used by P1 Method used by P2

while (S1 == S2); while (S1 != S2);


Critical Section Critical Section
S1 = S 2 S2 = !S1

Which one of the following statements describes the properties achieved?


1. Mutual exclusion but not progress
2. Progress but not mutual exclusion
3. Neither mutual exclusion nor progress
4. Both mutual exclusion and progress

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-

• This case is same as case-02.

Case-04: If S1 = 1 and S2 = 1-

• This case is same as case-01.

Thus, Overall we can conclude-


• Processes P1 and P2 executes the critical section alternatively.
• 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.

Thus, Option (A) is correct.


Q1.
Best answer
Answer: A. It ensures that no process executes CODE SECTION Q before every process has
finished CODE SECTION P.
Explanation
In short, semaphore 'a' controls mutually exclusive execution of statement count+=1 and
semaphore 'b' controls entry to CODE SECTION Q when all the process have executed
CODE SECTION P. As checked by given condition if(count==n) signal(b); the semaphore 'b'
is initialized to 0 and only increments when this condition is TRUE. (Side fact, processes do
not enter the CODE SECTION Q in mutual exclusion, the moment all have executed CODE
SECTION P, process will enter CODE SECTION Q in any order.)

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

Empty denotes number of Filled slots.


Full number of empty slots.
So, Producer must dealing with Empty and Consumer deals with Full
Producer must checks Full i,e. decrease Full by 1 before entering and Consumer check with Empty
decrease Full by 1 before entering
So, (C) must be answer.
Q.5

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.

Which one of the following is TRUE?

A. The producer will be able to add an item


to the buffer, but the
consumer can never consume it.
B. The consumer will remove no more than one
item from the buffer.
C. Deadlock occurs if the consumer succeeds in
acquiring semaphore s when the buffer is empty.
D. The starting value for the semaphore n must
be 1 and not 0 for deadlock-free operation.

Q12.
Q13.

Since, initial value of semaphore is 2,


two processes can enter critical
section at a time- this is bad and we
can see why.

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.

A process acquires a lock


only when L=0.
When L is 1, the process repeats in the while loop- there is no overflow because after each increment
to L, L is again made equal to 1. So, the only chance of overflow is if a large number of processes
(larger than sizeof(int)) execute the check condition of while loop but not L=1, which is highly
improbable.

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.

To correct the implementation we have to replace Fetch_And_Add with Fetch_And_Make_Equal_1 and


remove L=1 in AcquireLock(L).

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.

Correct Answer: A, at least twice

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.

The answer is (A) only.


The solution satisfies:

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.

So, eliminating all the 3 choices remains A.


Q17.

Q18
Q19

(C) Both process can run the critical


section concorrently. Lets say p1 starts and
it enters inside if claus and just after its
entertence and before execution of
critical_flag = TRUE, a context switch
happens and p2 also gets entrance since
the flag is still false. So, now both process
are in critical section! So, (i) is true. (ii) is
false there is no way that flag is true and
no process' are inside the if clause, if
someone enters the critical section, it will
definetly make flag = false. So. no.
deadlock.

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.

When P1 is entering critical section it is


guaranteed that wants1 = true
(wants2 can be either true or false). So,
this ensures P2 won't be entering the
critical section at the same time. In the same way, when P2 is in critical section, P1 won't be able to
enter critical section. So, mutual exclusion condition satisfied.

So, D is the correct choice.


Suppose P1 first enters critical section. Now suppose P2 comes and waits for CS by making
wants2 = true. Now, P1 cannot get access to CS before P2 gets and similarly if P1 is in wait, P2 cannot
continue more than once getting access to CS. Thus, there is a bound (of 1) on the number of times
another process gets access to CS after a process requests access to it and hence bounded waiting
condition is satisfied.
Q20.
x_count is the number of processes waiting on semaphore x_sem, initially 0,
x_count is incremented and decremented in x.wait, which shows that in between them wait(x_sem)
must happen which is P(x_sem). Correspondingly V(x_sem) must happen in x.signal. So, D choice.
Or//

We need value for x.wait and x.signal


We are working on 3 semaphores, mutex, x_sem, next .
Now, check the first code
x_count = x_count + 1;
So, x_count becomes 1
but next_count still 0
So, unable to enter in the first block x.wait
if (next_count > 0)
and E1 will not execute first
So, going to execute x.signal
here x.count >0 satisfying the condition
if block permit increment next_count to be 1
next_count = next_count + 1;
Now we want to execute x(ultimate we want to execute x)
So, V(x_sem) will be executed here
Wait(next) or P(next) will stop to execute code
Now , go to first part of code
if (next_count > 0)
V(next);
else
V(mutex);
------------------------------------------------------------ E1;
x_count = x_count - 1;
Now, next_count>0 is true.
So, code gets executed. V(next) and V(mutex) gets executed here. And also x_sem already executed.
So, now,P(x_sem) will be executed for Mutual Exclusion.
Q21.

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.

If Signal(S) and Signal(F) are interchanged in


Consumer, deadlock won't happen. It will just give
priority to a producer compared to the next
consumer waiting.

So, answer (A)

Q22.

P1 is the producer. So, it must wait for full


condition. But semaphore full is initialized to 0
and semaphore empty is initialized to n,
meaning full=0 implies no item
and empty=n implies space for n items is
available. So, P1 must wait for
semaphore empty - K−P( empty ) and similarly P2 must wait for semaphore full - M−P( full ). After
accessing the critical section (producing/consuming item) they do their respective V operation. Thus
option D.

A. Q23. deadlock p1 : line1|p2:line3| p1: line2(block) |p2 :line4(block)


So, here p1 want s(x) which is held by p2 and p2 want s(y) which is held by p1.
So, its circular wait (hold and wait condition). So. there is deadlock.

B. deadlock p1 : line 1| p2 line 3|p1: line 2(block) |p2 : line 4(block)


Som here p1 wants sy which is held by p2 and p2 wants sx which is held by p1. So its circular
wait (hold and wait ) so, deadlock.

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.

You might also like