Open In App

Process Synchronization

On the basis of synchronization, processes are categorized as one of the following two types:

Process synchronization problem arises in the case of Cooperative process also because resources are shared in Cooperative processes.
 
Critical Section Problem



Critical section is a code segment that can be accessed by only one process at a time. Critical section contains shared variables which need to be synchronized to maintain consistency of data variables.

In the entry section, the process requests for entry in the Critical Section.
 
Any solution to the critical section problem must satisfy three requirements:



 
Peterson’s Solution
Peterson’s Solution is a classical software based solution to the critical section problem.

In Peterson’s solution, we have two shared variables:


 
Peterson’s Solution preserves all three conditions :

0 Unlock
1 Lock

Before entering into the critical section, a process inquires about the lock. If it is locked, it keeps on waiting till it become free and if it is not locked, it takes the lock and executes the critical section.

In TestAndSet, Mutual exclusion and progress are preserved but bounded waiting cannot be preserved.
 
Question : 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:
I. The above solution to CS problem is deadlock-free
II. The solution is starvation free.
III. The processes enter CS in FIFO order.
IV. More than one process can enter CS at the same time.
 
Which of the above statements is TRUE?
(A) I
(B) II and III
(C) II and IV
(D) IV

Click here for the Solution.
 
Semaphores

A Semaphore is an integer variable, which can be accessed only through two operations wait() and signal().
There are two types of semaphores : Binary Semaphores and Counting Semaphores

 

References
http://www.csee.wvu.edu/~jdmooney/classes/cs550/notes/tech/mutex/Peterson.html
http://iit.qau.edu.pk/books/OS_8th_Edition.pdf
 

Article Tags :