0
1.2kviews
Approaches of Handling the process Synchronisation in MOS
1 Answer
0
8views

Process synchronisation is done by two approaches software approach and hardware approach are given below in detail.

Software Approach

The software approach known as Peterson’s Solution is best for Synchronisation. It uses two variables in the Entry Section so as to maintain consistency, like Flag (Boolean variable) and Turn variable (storing the process states). It satisfy all the three Critical Section requirements.

p1                                                      p2

f‌lag[1]=True;                                           f‌lag[2]=True;

turn = 2;                                               turn = 1;
While(f‌lag[2]&&nun=2)                                   While(f‌lag[1]&&tmn= 1)

”If true then busy waiting of p2 ”                      If true then busy waiting of p1
”Critical section execution of P1 ”                     Critical section execution of P2
f‌lag[1]=False                                           f‌lag[2]=False

Hardware Approach

The Hardware Approach of synchronization can be done through Lock & Unlock technique. Locking part is done in the Entry Section so that only one process is allowed to enter into the Critical Section, after it completes its execution, the process is moved to the Exit Section, where Unlock Operation is done so that another process in the Lock Section can repeat this process of execution. This process is designed in such a way that all the three conditions of the Critical Sections are satisfied.

P1                              P2

Lock(L); ”Entry Section         Lock(L); ”Entry Section
”Critical section ”             Critical section
Count++;                        Count»;

Unlock(L); ”Exit Section        Unlock(L); ”Exit Section

Using Interrupts

These are easy to implement. When Interrupts are disabled then no other process is allowed to perform Context Switch operation that would allow only one process to enter into the Critical State.

While(1);                     While(0);

{                             {

Disable_Interrupt();           Disable_Interrupt();
”Critical section ”            Critical section
Enable_interrupt()             Enable_interrupt0

}                             }

Test_And_Set Operation

If the processes are there in their critical section, any other processes should not interrupt them. The interrupt must be disabled. In a multiprocessor system, several processes execute concurrently. So the instruction is given to all other processes to disable their interrupt. But it will be a time-consuming process.

To solve this problem and achieve mutual exclusion, Test_And_Set instruction is used. These are hardware instructions for solving the critical section problem by providing mutual exclusion in an easy and efficient way.

Suppose all processes have a Boolean variable lock.

Initially,   lock=False.
Suppose process pi and
i = 0 to n-1 number and
n = concurrently running process.
boolean lock = false;
// Entry section
do
{
    While (Test_And_Set (&lock))
    { 
        //do nothing as another process is in critical section. (Wait)
    }
    //Critical section
    // Exit section
    lock = false        // (Let another process execute in critical section)
} while (1);

/ / Test_And_Set Function

boolean Test_And_Set(Boolean *lockvalue)
{
  boolean RValue = *lockValue;
 *lockValue = true;
  Return RValue;
}

Explanation: In the above code, a lock is not variable but it will store the address where its value is false. Suppose address is 1000. So, lock = false

lockValue = 1000 and RValue = 1000 (false)

Again we do lock value as a false so, value at 1000 will store true. i.e. lock = true. And function will return RValue which is false.

In the while section, RValue is returning false so it will not enter into a loop instead will enter the critical section.

Suppose process p1 is now entered into the critical section. Simultaneously process p2 wants to execute so, it will go into the entry section and will check lock value. But it is true this time. So, 1000 = true.

So, Rvalue = true.

So, p2 will not enter in critical section but will execute while loop. If p1 completes, execution in the critical section, lock-value will false again. p2 will get a chance to execute its critical section.

Implementation of process wait

Several processors may wait for the semaphore. This wait is implemented in 3 ways

1. Busy waiting

In this, processors are busy in continuously executing atomic instructions for checking the status of a shared variable. Busy waiting also called as spinlock. It wastes processor cycles and also consumes bandwidth of network which is connecting the memory modules to the processors. Busy waiting can also increase the traffic on the network. It can also improve the normal memory access and eventually degrades the performance.

2. Sleep-lock

In this, instead of continuously spinning, a lock process is suspended when it could not obtain the lock and suspended process relies on interrupts to reactivated when the lock is freed. When the process fails to obtain a lock, it is suspended. In this suspended state a process doesn’t relinquish its processor and all interrupts except inter-processor interrupts are disabled. When process frees the lock, it sends an inter-processor interrupt to all other suspended processes. This method substantially reduces the network traffic due to busy waiting, but still waste the processor cycles.

3. Queuing

In queuing, a process waiting for a semaphore to open it and place it in a global queue. A process which is waiting will dequeue and activated by a V – operation on the semaphore. Dequeue and en-queue operations require several instructions for execution, due to this processing overheads are introduces. Also, the queue forms a shared data structure and must be protected against concurrent access. But the advantage of queuing is that it eliminates network traffic and wastage of processor cycles due to busy waiting.

Compare-and-swap Instruction

It is used by IBM 370 for optimistic synchronization of concurrent updates to the memory location. This instruction is defined as follows.

Suppose, r1 and r2 are two registers of processor and m is memory location then

Compare-and-swap (var r1, r2, m: integer);

var temp: integer;
Begin
 Temp:=m;
If temp=r1 then {m:=r2; z:=1}
else {r1:=temp; z:=0}
end;

Explanation:

If contents of r1 and m are identical, this instruction will assign the contents of r2 to m and set z as 1. If it is not then it will assign the contents of m to r1 and set z as 0. z variable is taken as a flag which indicates the instruction is executed successfully or not.

If z=1 then the instruction is executed successfully.

Compare-and-swap instruction can be used to synchronize concurrent access to shared variable say m. In the above code, the processor first reads the value of m into a register r1. It then computes a new value which is x plus the original value, to be stored in m and stores it in register r2. The processor then performs a compare-and-swap (r1, r2, m) operation.

If z=1 after this instruction has been executed, this means no other processor has modified location m since it was read by this processor. Thus mutually exclusive access to m is maintained. If z=0, then some other processor has modified m since this processor read it. In this case, the new value of m is automatically stored in r1 by compare-and-swap instruction. So that this processor can retry its update in a loop.

Swap instruction

This instruction automatically exchanges the values of two variables like memory locations. Suppose x and y are two variables,

Procedure swap (var x, y: boolean);
var temp:boolean;
begin
temp:=x;
x:=y;
y:=temp;
end;
Please log in to add an answer.