Wednesday 10 April 2013

Critical Section and Mutual Exclusion

Concurrent processes within a system may have to cooperate or interact in many situations to share the common resources such as data structure or physical devices. Cooperating processes must synchronize with each other to prevent the concurrency related timing problem due to concurrent accessing of shared devices. Without adequate inter-process synchronization, updating of shared variables can induce concurrency related timing errors that are often difficult to debug. One of the primary causes of this problem is the possibility that concurrent process may observe temporarily inconsistent value of a shared variable while it is being accessed and/or updated.

A critical section for a data item d is a section of code which cannot be executed concurrently with itself or other critical section for d. Thus, critical section is a sequence of instructions with a marked beginning and end. When a process enters a critical section, it must complete all instructions therein before any other process is allowed to enter the same critical section.  Only the process executing the critical section is allowed access to the shared variables and all other processes should be prevented from doing so until the completion of the critical section. This is often referred to as mutual exclusion, in which a single process temporarily excludes all other from using a shared resource in order to ensure the system`s integrity.

If the shared resource is a variable, mutual exclusion ensures that at most one process at a time has access to it during the critical updates that lead to temporarily inconsistent values. As a result, the other processes see only consistent values of shared variables. With serially reusable shared devices, at any point, only one process is allowed to control such devices.
A critical section for a data item d is thus a mutual exclusion region with respect to d. If a process Pi  is executing a critical section on d,  any other process wishing to enter the critical section for d  will have wait till Pi exits from the critical section.
As an example, consider two concurrent processes Pi an Pj in an airline reservation section. Critical sections are marked with dotted lines.

      Process Pi
       If nextseatno < capacity
            then
                  critical section
          else
                display “ Sorry! No seats are available”;

Process Pj
       If nextseatno < capacity
            then
                  Mutual exclusion      
  else
                display “ Sorry! No seats are available”;

Consistency of the shared variable nextseatno is maintained since each processes queries and updates the value of the data inside a critical section.    

      Index : Operating System 


No comments:

Post a Comment