Module 7: Process Synchronization - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Module 7: Process Synchronization

Description:

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
Category:

less

Transcript and Presenter's Notes

Title: Module 7: Process Synchronization


1
Operating SystemsLecture 20 Process
Synchronization
2
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.

3
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

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

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

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

7
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

8
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
    data
  • 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
    6)
  • T5 Consumer counter register2 (counter gets
    4)
  • 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.

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

10
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?
11
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.

12
Definitions
  • 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
    interrupted.
  • 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.

13
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
    section.

14
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
    indefinitely.
  • 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
    granted.
  • Assume that each process executes at a nonzero
    speed
  • No assumption concerning relative speed of the n
    processes.

15
Initial Attempts to Solve Problem
  • Only 2 processes, P0 and P1
  • 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.

16
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

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

18
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)
About PowerShow.com