Docsity

Process Synchronization - Operating Systems, Distributed Computation - Lecture Slides, Slides of Operating Systems

Partial preview of the text

Download Process Synchronization - Operating Systems, Distributed Computation - Lecture Slides and more Slides Operating Systems in PDF only on Docsity!

PROCESS SYNCHRONIZATION

Background The Critical-Section Problem Synchronization Hardware Semaphores Classical Problems of Synchronization Critical Regions Monitors

Background

  • Concurrent access to shared data may result in data inconsistency.
  • Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes.

Bounded-Buffer (Old)

  • Shared data #define BUFFER_SIZE 10 typedef struct { ... } item; item buffer[BUFFER_SIZE]; int in = 0; int out = 0;
  • Solution is correct, but can only use BUFFER_SIZE-1 elements

Bounded-Buffer (Old)

  • Producer Process

item nextProduced;

while (1) { while (((in + 1) % BUFFER_SIZE) == out);/* do nothing */ buffer[in] = nextProduced; in = (in + 1) % BUFFER_SIZE; }

Bounded-Buffer (Proper)

  • Shared data

#define BUFFER_SIZE 10 typedef struct {

... } item; item buffer[BUFFER_SIZE]; int in = 0; int out = 0; int counter = 0;

Bounded-Buffer

Producer process

item nextProduced; while (1) { while (counter == BUFFER_SIZE); //spin buffer[in] = nextProduced; in = (in + 1) % BUFFER_SIZE; counter++; }

Bounded Buffer

  • The statement “count++ ” may be implemented in machine language as:

register1 = counter register1 = register1 + 1 counter = register

  • The statement “count-- ” may be implemented as: register2 = counter register2 = register2 – 1 counter = register

Bounded Buffer

  • If both the producer and consumer attempt to update the buffer concurrently, the assembly language statements may get interleaved.
  • Interleaving depends upon how the producer and consumer processes are scheduled.

Race Condition

  • Race condition : The situation where several processes access – and manipulate shared data concurrently. The final value of the shared data depends upon which process finishes last.
  • To prevent race conditions, concurrent processes must be synchronized.

Disable Interrupts?

  • Uniprocessor:
    • Disable interrupts to solve lock race condition.
  • Multiprocessor:
    • Disabling interrupts:
    • Send interrupt message to all processors.
    • Delay in message passing.
    • System degradation.
  • Solution?
    • Handle lock variable with atomic assignment.
    • Swap variable in hardware. Docsity.com

The Critical-Section Problem

  • n processes, all competing for some shared data
  • Each process has a code segment, called critical section, in which the shared data is accessed.
  • Problem:
    • ensure that when one process is executing in its critical section, no other process is allowed to execute in its critical section. Docsity.com

General Structure

General structure of process Pi (other process Pj) do { entry section critical section exit section remainder section } while (1);

  • Processes may share some common variables to synchronize their actions.

Critical-Section Solution

Mutual Exclusion

  • If process Pi is executing in its critical section

 no other processes can be executing in their critical sections.

Critical-Section Solution

Progress

a) if no process is executing in its critical section and b) there exist some processes that wish to enter their critical section

 only those processes that are not executing in the remainder section can participate in the decision on which will enter the critical section next.

The selection of the processes cannot be postponed indefinitely. Docsity.com