Module 7: Process Synchronization - PowerPoint PPT Presentation

1 / 18
About This Presentation

Module 7: Process Synchronization


Operating Systems Lecture 20 Process Synchronization Cooperating Processes and Shared Data A cooperating process can affect or be affected by other processes. – PowerPoint PPT presentation

Number of Views:86
Avg rating:3.0/5.0
Slides: 19
Provided by: Marily361


Transcript and Presenter's Notes

Title: Module 7: Process Synchronization

Operating SystemsLecture 20 Process
Cooperating Processes and Shared Data
  • A cooperating process can affect or be affected
    by other processes.
  • Cooperating processes may directly share address
    space (e.g. threads share address space).
  • Concurrent access to shared data may result in
    data inconsistency.
  • Maintaining data consistency requires
    synchronization mechanisms to ensure the orderly
    execution of cooperating processes.

Recall the Bounded Buffer problem
  • Producer/Consumer problem Producer writes to a
    buffer and the Consumer reads from the buffer.
  • E.g. cat filename lpr
  • Shared-memory solution to bounded-buffer problem
    (Chapter 4) allows at most n 1 items in buffer
    at the same time.
  • A solution, where all n locations in the buffer
    are used is not simple.
  • Suppose that we modify the producer-consumer code
    by adding a variable counter, initialized to 0
    and incremented each time a new item is added to
    the buffer

  • Shared data
  • define BUFFER_SIZE 10
  • typedef struct
  • . . .
  • item
  • item bufferBUFFER_SIZE
  • int in 0
  • int out 0
  • int counter 0

Bounded-Buffer Producer
  • Producer process
  • item nextProduced
  • while (1)
  • while (counter BUFFER_SIZE)
  • / do nothing /
  • bufferin nextProduced
  • in (in 1) BUFFER_SIZE
  • counter

Bounded-Buffer Consumer
  • Consumer process
  • item nextConsumed
  • while (1)
  • while (counter 0)
  • / do nothing /
  • nextConsumed bufferout
  • out (out 1) BUFFER_SIZE
  • counter--

Accessing count concurrently
  • What happens when the statementscountercounte
    r--are performed concurrently?
  • Execution of counter in machine code
  • register1 counter
  • register1 register1 1
  • counter register1
  • Execution of counter-- in machine code
  • register2 counter
  • registe2 register2 - 1
  • counter register2

Data inconsistency
  • Suppose counter initially set to 5. Execution of
    counter and counter-- consecutively should
    leave the value at 5.
  • Concurrent execution could leave inconsistent
  • T0 Producer register1 counter (register1
    gets 5)
  • T1 Producer register1 register1
    1 (register1 gets 6)
  • T2 Consumer register2 counter (register2
    gets 5)
  • T3 Consumer register2 register2 -
    1 (register2 gets 4)
  • T4 Producer counter register1 (counter gets
  • T5 Consumer counter register2 (counter gets
  • Could end up with counter value of 4, 5 or 6
  • There is no way to predict the relative speed of
    process execution, so you cannot guarantee that
    one will finish before the other.

Atomic Operations
  • The statementscountercounter--must be
    performed atomically.
  • Atomic operation means an operation that
    completes in its entirety without interruption.

Concurrency Problem at Program Execution level
Concurrency problems can arise at the program
execution level. Example Suppose two processes,
P1 and P2 share variables a and b. P1 a a
1 b b 1 P2 b b 2 a a
2 Assume a 7, b 2 initially. Assume atomic
program instruction execution. Question What
are the possible ending values for a and b if P1
and P2 execute concurrently?
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.

  • Concurrent Processes Process executions overlap
    in time.
  • Cooperating processes Processes that affect each
    other during execution (e.g. Parent waits for
    child parent communicates with child).
  • Critical Section (CS) Segment of code that only
    one process can be in at a time (e.g. segment of
    code that accesses shared data).
  • Mutual exclusion If a process is in its CS, then
    no other process can be in the same CS. Each CS
    access must be mutually exclusive (mutex).
  • Atomic execution Execution of code that is not
  • Busy waiting Repeated execution of a code loop
    while waiting for an event.
  • Deadlock when two or more processes are
    permanently blocked.
  • Starvation When a process is indefinitely
    delayed other processes are given the resource
    this process needs.

The Critical-Section Problem
  • n processes all competing to use 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

Solution to Critical-Section Problem
  • 1. Mutual Exclusion. If process Pi is executing
    in its critical section, then no other processes
    can be executing in their critical sections.
  • 2. Progress. If no process is executing in its
    critical section and there exist some processes
    that wish to enter their critical section, then
    the selection of the processes that will enter
    the critical section next cannot be postponed
  • 3. Bounded Waiting. (no starvation) A bound must
    exist on the number of times that other processes
    are allowed to enter their critical sections
    after a process has made a request to enter its
    critical section and before that request is
  • Assume that each process executes at a nonzero
  • No assumption concerning relative speed of the n

Initial Attempts to Solve Problem
  • Only 2 processes, P0 and P1
  • General structure of process Pi (other process
  • do
  • entry section
  • critical section
  • exit section
  • remainder section
  • while (1)
  • Processes may share some common variables to
    synchronize their actions.

Algorithm 1
  • Shared variables
  • int turninitially turn 0
  • turn i ? Pi can enter its critical section
  • Process Pi
  • do
  • while (turn ! i)
  • critical section
  • turn j
  • remainder section
  • while (1)
  • Satisfies mutual exclusion, but not progress

Algorithm 2
  • Shared variables
  • boolean flag2initially flag 0 flag 1
  • flag i true ? Pi ready to enter its critical
  • Process Pi
  • do
  • flagi true while (flagj)
    critical section
  • flag i false
  • remainder section
  • while (1)
  • Satisfies mutual exclusion, but not progress

Algorithm 3
  • Combined shared variables of algorithms 1 and 2.
  • Process Pi
  • do
  • flag i true turn j while (flag j
    and turn j)
  • critical section
  • flag i false
  • remainder section
  • while (1)
  • Meets all three requirements solves the
    critical-section problem for two processes.
Write a Comment
User Comments (0)