Process Synchronization

  • Post author:
  • Post comments:0 Comments
Download PDF
Advertisement

Process Synchronization

Process synchronization defined as the sharing of system resources by multiple processes so that simultaneous access to shared data is controlled, reducing the risk of incompatible data. Maintaining data consistency requires mechanisms to confirm synchronized execution of cooperative processes. Processes may execute concurrently but can be interrupted at any time, partially completing execution. data inconsistency can be resulted in case of concurrent access to the share data.

Problem Illustration

Let suppose that we needed to provide a solution to the consumer-producer problem which fills all the buffers. We can do that by having an integer counter that has track of the number of full buffers. Primarily, counter is set to 0. it is incremented by the producer after producing a new

buffer and is decremented by the consumer after it consumes a buffer.

Producer

   while (true)
   {
     while (Counter == BUFFER_SIZE) ;
     buffer[in] = Next_Produced;
     in =  (in + 1) % BUFFER_SIZE;
     counter++;
   } 
Advertisement

 

Consumer

   while (true)
   {
      while (counter == 0)
      Next_Consumed = buffer[out];
      out = (out + 1) % BUFFER_SIZE;
      counter--;
   }

Race Condition

A condition in which multiple processes or threads read and write a shared data item and the absolute result depends on the comparative timing of their execution. To repel the race condition above, we need to make sure that only one single process at a time can be operating the variable counter.

  • Counter++(increment) could be implemented as
    register1 = counter
    register1 = register1 + 1
    counter = register1
  • Counter—(decrement) could be implemented as
    register2 = counter
    register2 = register2 – 1
    counter = register2
  • Consider this execution interleaving with “count = 5” initially:
    S0: producer execute register1 = counter {register1 = 5}
    S1: producer execute register1 = register1 + 1 {register1 = 6}
    S2: consumer execute register2 = counter {register2 = 5}
    S3: consumer execute register2 = register2 – 1 {register2 = 4}
    S4: producer execute counter = register1 {counter = 6 }
    S5: consumer execute counter = register2 {counter = 4}

 

Context Switching

Advertisement

Leave a Reply

Please rate us