Process Synchronization

Process synchronization as designed to provide an efficient use of shared data between more than one thread among 1 process or from different processes. It resolves the problem of Race condition.

Types of Processes

There are two types of processes

1. Independent process

Independent process is a process that cannot affect by other processes and don’t affect the other processes as well. It does not not share any data, variable, or any resource like Memory, CPU and printer etc.

2. Co-operative process

Co-operative process is a process that can be affected by other processes.They can also affect the other processes as well. Independent processes share any data, variable, or any resource like Memory, CPU and printer etc.

Race Condition

Race Condition occurs when two or more threads try to read, write or access memory concurrently.

Critical Section

Critical section is a specific region of program which contains the shared resources of cooperative processes. It allows only one process to enter into critical section. So that, race conditions can be eliminated and process synchronization can be achieved.

Purpose of process synchronization: To handle multiple cooperative processes which are running parallel to eliminate race condition.

Why process synchronization is important?

With the help of process synchronization we can handle cooperative processes. If we don’t care and execute all cooperative process at the same time without any process synchronization rules then cooperative process can cause problem like deadlock or incorrect results etc.

Explain with Example

When multiple cooperative processes run parallel or run at the same time then problem create as given below.

P1 and p2 run parallel

if system works without process synchronization policy in uni-processor environment then the problem could be like that,

Explanation:

Here, there are two processes using same variable. Value of this variable exists in RAM. So, RAM is the common Resource.

                                                          INT  Shared =5

// Process 1                                                   //Process 2          
INT X= Shared               
X++                                               
Sleep(1)
Shared = X
// Answer will be 6
INT Y= Shared              
Y- -                                         
Sleep(1)
Shared = Y
// Answer will be 4

INT SHARED=5 is variable. Both process are using this variable. If both processes run parallel then answer should be 5. As, one process increment the value by 1 and other decrement the value by 1 but the answer is not 5

If we execute P1 first then answer will be 6 and if P2 execute first then answer will be 4. But answer should be 5. It is the race condition.

So we use the some methods i.e semaphore, north variables and other methods to remove the race condition and to get the process synchronization.

Note: Sleep (1) mean pause the process for 1 second. Information of All executed instruction of process P1 (till now) is saved in program control block (PCB). Process will pause but CPU shift to another process Because CPU is not control by user program. As, OS handles the CPU.

How to get Process synchronization?

Process synchronization can be achieved by using critical section. To get process synchronization every process follows the following diagram.

  • Initial section: it contains Non-shared data .
  • Entry section: Entry code contains conditions and lock the allocated resources
  • Critical section: All shared data is placed in critical section.
  • Exit Section: Contains exit code which release the locked resources.
  • Remainder Section: it contains Non-shared data .
  Process()
   { 
       while()
        {   
            initial section  
           Entry section
           Critical section
           Exit section
            Remainder section
   } 

}

Conditions for Entrance Section to Get Process Synchronization

1. Mutual Exclusion (mutual separation):

When one process is executing in critical section then no other process can enter the critical section. It is called Mutual Exclusion.

critical section

2. Progress:

If one process does not need to execute the critical section then it should not stop any other processes to get execute into the critical section is called progress.

If one process does not need to execute the critical section and it stop the  other processes to get execute into the critical section then progress will not be achieved.

3. Bound wait:

Every process should not be too long waiting for getting into the critical section.

To make sure that every process gets the chance to enter into critical section so that no process go for starvation.

If P2, P3, P4, P5 are using critical section 5, 10, 20, 15 times then p1 must be use the critical section one or two time. It is bound wait.

In NON BOUND wait case, some processes use critical section 1000, 5000, or infinity times and other use 1, 2 or no times. It is called starvation problem. Starvation means that a process will wait indefinitely.

4. Portability

No Assumption related to H/W

If we create a solution then it must be flexible, reliable and run on all the hardware.

As we create a solution for any problem then it must be run on window, linux OS etc. No limitation with it as if it runs properly on 32 bit OS but not on 64 bit OS, cause problem.

How to achieve above all conditions?

Answer: Place all condition in entrance section just before the critical section.

When process are not synchronized then there may be many problems some of them are given under and will explain in further lectures

  1. Producer Consumer problem
  2. Printer spooler problem
  3. Dinning Philosopher problem

 To eliminate above problems we use the following methods, their explanation will be given is upcoming lectures.

  1. Semaphore
  • Binary Semaphore
  • Counting Semaphore
  1. Mutex
  2. Peterson solution

Help Other’s By Sharing…

Contact Us

Burewala, Vehari, Punjab, Pakistan

cstaleem1@gmail.com

Website: CStaleem.com

Pin It on Pinterest