Process Synchronization in Operating System

By | October 9, 2021

Process Synchronization

  • A co-operating process is one that can affect or be affected by other processes executing in the system.
  • Such co-operating processes may either directly share a logical address space or be allowed to share data only through files.
  • When co-operating processes concurrently share the data, it may result in data inconsistency.
  • Maintaining data consistency requires mechanisms to ensure the orderly execution of co-operating processes.
  • Thus, process synchronization ensures a perfect co-ordination among the processes. When cooperating processes share data, process synchronization maintains data consistency.
  • Process synchronization can be provided by using several different tools like semaphores, mutex and monitors.
  • Synchronization is important for both user applications and implementation of operating system.

Concept of Race Condition

  • When several processes access and manipulate the same data at the same time, they may enter into a race condition.
  • A race condition is a flaw in a system of processes whereby the output of the process is dependent on the sequence of other processes.
  • Race conditions occur among processes that share common storage (a file or main memory) and each process can read and write on this shared common storage.
  • Thus, a race condition occurs due to improper synchronization of shared memory access.
  • Race conditions can occur in poorly designed systems.
  • If the race condition is allowed to happen in the system the output of the processes cannot be ascertained.

Process Synchronization

An example representing a race condition is given below :

  • Let us assume that two processes P, and P, each want to increment the value of a global integer(say i) by 1. Ideally, the following sequence of operations would take place :

int i= 10;

Process 1

P1 reads the value of i from memory into a register : 10

P1 increment the value of i in register : (register contents) +1 = 11

P1 stores the value of the register in memory : 11

Process 2

P2 reads the value of i from memory into a register : 11

P2 increment the value of i in the register : (register contents) +1 = 12

P2 stores the value of the register in memory : 12

The resultant value of i = 12

In the case shown above, the final value of i is 12, as expected.

  • However if the two processes run simultaneously, the outcome of the operation could be wrong. The sequence of operation listed below shows this scenario : int i= 10;

P1 reads the value of i from memory into a register : 10

P2 reads the value of i from memory into a register: 10

P1 increments the value of i from memory into a register : (register content) + 1 = 11

P2 stores the value of i from memory into a register : (register content) +1 = 11

P1 stores the value of the register in memory : 11

P2 the value of i the register in memory: 11

The resultant value of i = 11

  • Thus the final value of i is 11 instead of the expected result of 12. Clearly this has occurred because of difference in the order of execution of processes. The process P1 and P2 entered me race condition.
  • Thus, a race condition must be avoided in a concurrent multi-process environment they have to function correctly.

Leave a Reply

Your email address will not be published. Required fields are marked *