?????????????????????? Process Synchronization - PowerPoint PPT Presentation

1 / 60
About This Presentation
Title:

?????????????????????? Process Synchronization

Description:

Title: Module 7: Process Synchronization Author: Marilyn Turnamian Last modified by: Windows User Created Date: 7/23/1999 1:31:00 PM Document presentation format – PowerPoint PPT presentation

Number of Views:9
Avg rating:3.0/5.0
Slides: 61
Provided by: Maril90
Category:

less

Transcript and Presenter's Notes

Title: ?????????????????????? Process Synchronization


1
??????????????????????Process Synchronization
  • ???????? ?????? ??????? ???????

2
Objectives
  • ??????????????????????????????????????????????????
    ???? ??????????????????????????????????????????
  • ??????????????????????????????????????????????????
    ??????????????????????????? ??????????
    ??????????????????? ????????
  • ??????????????????????????????????????
    ?????????????????????????????????? ?????????????

3
Agenda
  • Basic Concepts
  • Scheduling Criteria
  • Scheduling Algorithms
  • Multiple-Processor Scheduling
  • Real-Time Scheduling
  • Algorithm Evaluation

4
Agenda Process Synchronization
  • Background
  • The Critical-Section Problem
  • Petersons Solution
  • Synchronization Hardware
  • Semaphores
  • Classic Problems of Synchronization
  • Monitors
  • Synchronization Examples
  • Atomic Transactions


5
Background
  • ??????????????????????? ??????????????????????????
    ?????????????????? ?????????????????????
  • ??????????????? (Multiprogramming)
  • ?????????????? (Multiprocessing)
  • ???????????????????? (Distributed processing)
    ???? ????????? (cluster)
  • ??????????????????????
  • ????????????????????? (concurrency)
  • ????????????? (synchronization)

6
Background (2)
  • ??????????????????????????????????????????????????
    ??? ??????
  • ??????????????????
  • ??????????????????????????????????????????????????
    ?????????????????????????????????????
  • ???????????????????????
  • ??????????????????????????????????????????????????
    ???? ?????????????????????????????????????????????
    ???????????????????????????????????
  • ???????????????????????
  • ??????????????????????????????????????????????????
    ?????????????????????? ??????

7
Background (3)
  • ???????????????????????????? ?????????????????????
    ??????????????????????????
  • ??????????????????????????????????????????????????
    ??????????????????????????????????????????????????
    ????????????
  • ???????????????????????? consumer-producer
    problem ????????????????????? ????????????????????
    ??? integer count ????????????????? track
    ?????????????????????????
  • ??????????????????????? 0 ????????????????????
    producer ?????????????????????????????
  • ??????????????? consumer ??????????????????

8
Producer
  • while (true)
  • / produce an item
    and put in nextProduced
  • while (count BUFFER_SIZE)
  • // do nothing
  • buffer in nextProduced
  • in (in 1) BUFFER_SIZE
  • count

9
Consumer
  • while (1)
  • while (count 0)
  • // do nothing
  • nextConsumed bufferout
  • out (out 1) BUFFER_SIZE
  • count--
  • / consume the item in nextConsumed

10
???????????????(race condition)
  • ??????????????????????????????????????????????????
    ???????????????????????????????????? ????????
    ??????????????? (race condition)
  • ???????????????????????????????????????????
    (scheduling policy) ??????
  • ??????????????????????????? ??????????????????????
    ????????? ? ??????????????????????????????????????
    ???? counter ??? ?????????????????????????????????
    ???????? (synchronized) ??????????

11
Race Condition
  • count could be implemented as register1
    count register1 register1 1 count
    register1
  • count-- could be implemented as register2
    count register2 register2 - 1 count
    register2
  • Consider this execution interleaving with count
    5 initially
  • S0 producer execute register1 count
    register1 5S1 producer execute register1
    register1 1 register1 6 S2 consumer
    execute register2 count register2 5 S3
    consumer execute register2 register2 - 1
    register2 4 S4 producer execute count
    register1 count 6 S5 consumer execute
    count register2 count 4
  • count ??????? 4 ???? 6 ????? ???????????????????
    5

12
??????????Critical Section
  • ?????????????????????????????????????????????? n
    T0, T1, , Tn-1
  • ??????????????????????????????????????????????????
    ????????????????????????????????
    ??????????????????????????????????????????????????
    ??????? ?????????? (critical section)
    ??????????????????????????????????????,
    ?????????????, ????????? ????????
  • ????????????????????????????? ????????????????????
    ????????????????????????? ??????????????????
    ?????????????????????????????????????????

13
?????????????????????
  • 1. ?????????????? (mutual exclusion)
  • ????????? Ti ????????????????????????????????????
    ? ???????????????????????????????????????????????
    ???
  • 2. ???????????? (Progress)
  • ??????????????????????????????????????????
    ???????????? ???????????????????
    ??????????????????????????????????????????????????
    ????????????????????????????
  • 3. ???????????????? (Bounded waiting)
  • ???????????? ?????????????????????
    ??????????????????????????????????????????????????
    ??????????????????????????????????????????
    ??????????????????????????????????????????
    ??????? ???????????????????????? (starvation)

14
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
  • reminder section
  • while (1)
  • ??????????????????????????????????????????????????
    ??????????
  • ???????????????????????????????????? 3 ???

15
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
  • reminder section
  • while (1)
  • ????????????????????????????????? (Mutual
    Exclusion) ???????????????????????? (Progress)

16
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)
  • ????????????? ???????????????????? (Mutual
    Exclusion) ???????????????????????? (Progress)

17
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)
  • ????????????? ????????????????????? 3 ???
    ????????????????????????????????? 2 ????????????

18
??????????????????? Synchronization Hardware
  • ???????????????????????????????????????
    interrupts
  • ??????????????????????????????????????????????????
    ??????????????????????????????????????
  • ?????????????????? ??????????????????
  • ??????????????????????????????????????????????????
    ????????????????????????????????????????
  • ??????????????????????????????????????????????????
    ??????????????????????????????????????????????????
    ???
  • ??????????????????????????????????????????????????
    ????????????????????????????????????

19
??????????????????? Synchronization Hardware
  • ??????????????????????????????????????????????????
    ?????????????????????????????????????
    ?????????????????????????? 2 ?????????????????
    (atomic instruction)
  • Atomic non-interruptable
  • ??????????????????????????????????????????????????
    ???????????????????
  • Test-and-Set Instruction
  • Swap Instruction

20
Test-and-Set Instruction
  • Definition
  • boolean TestAndSet (boolean target)
  • boolean rv target
  • target TRUE
  • return rv

21
Solution using TestAndSet
  • Shared boolean variable lock., initialized to
    false.
  • Solution
  • do
  • while ( TestAndSet (lock ))
  • / do nothing
  • // critical section
  • lock FALSE
  • // remainder section
  • while ( TRUE)

22
Swap Instruction
  • Definition
  • void Swap (boolean a, boolean b)
  • boolean temp a
  • a b
  • b temp

23
Solution using Swap
  • Shared Boolean variable lock initialized to
    FALSE Each process has a local Boolean variable
    key.
  • Solution
  • do
  • key TRUE
  • while ( key TRUE)
  • Swap (lock, key )
  • // critical section
  • lock FALSE
  • // remainder section
  • while ( TRUE)

24
????????Semaphore
  • ??????????????????????????????????????????????
  • ???????? S ??????????????????? (integer)
    ????????????????? ????????????????????????????????
    ??????????? 2 ???????????????????????????????
    (atomic operation) ??????
  • wait ??? signal
  • ??????????????????????????????? (atomic
    operation)
  • ????????????????????????????????????????
    ??????????????????????????????????????????????????
    ????????????????????????
  • ??????? wait(S) ??????????????????????? S (S ? 0)
    ?????????????? (S--) ?????????????????????????????
    ????

25
???????? (2)Semaphore
  • S wait() and signal()
  • Originally called P() and V()
  • wait (S)
  • while S lt 0
  • // no-op
  • S--
  • signal (S)
  • S

26
Semaphore as General Synchronization Tool
  • ????????????????????????? 2 ???? ??????
  • ??????????? (Counting semaphore)
    ?????????????????????????
  • ?????????????? (Binary semaphore)
    ?????????????????? 0 ??? 1 ????????????????
    ????????????? mutex locks
  • ??????????????????????????????????
    ?????????????????????????????????????????? 0 ???
    1 ?????????????????????
  • ????????? counting semaphore S ??????????????????
  • Provides mutual exclusion
  • Semaphore S // initialized to 1
  • wait (S)
  • Critical Section
  • signal (S)

27
Semaphore Implementation
  • ??????????????????????????????????????????????????
    ???????????????????????????? ?????????????????????
    ????????????????????????????????????? ?
    ?????????????
  • ??????????????????????????????????????????????????
    ??????????????????????????????????????
  • ??????????????????????????????????????????????????
    ????? ????????????????????????????????????????????
    ??????????????????? P ??????????
    (??????????????????)
  • ??????????????????????????????????????????????????
    ???? V (????????????????)
  • ????????????????????????????????????? 0
    ??????????????????????????????????????
  • ???????????? ?????????????????????????????????????
    ??????????????????????????????? 0

28
Semaphore Implementation with no Busy waiting
  • ???????????????????????????????????????????
    (waiting queue) ???????????????????????????? 2
    ??????
  • value (of type integer)
  • pointer (????????????????????????????????)
  • Two operations
  • block ??????????????????????
  • wakeup(P) ??????????????????????? P
    ???????????????? (?????????????????????????????)

29
Semaphore Implementation with no Busy waiting
(Cont.)
  • Implementation of wait
  • wait (S)
  • value--
  • if (value lt 0)
  • add this process to waiting
    queue
  • block()
  • Implementation of signal
  • Signal (S)
  • value
  • if (value lt 0)
  • remove a process P from the
    waiting queue
  • wakeup(P)

30
?????????????????????Deadlock and Starvation on
Semaphore
  • ?????????? (Deadlock) ???????????????? 2
    ??????????????????????????????????????????????????
    ????????????? ?????????????????????????????????
    ??????????????????????????????????????????????????
    ???? ????????????????????????????????????????
  • Let S and Q be two semaphores initialized to 1
  • P0 P1
  • wait (S)
    wait (Q)
  • wait (Q)
    wait (S)
  • . .
  • . .
  • . .
  • signal (S)
    signal (Q)
  • signal (Q)
    signal (S)
  • ???????????? (starvation) ???? ???????????????????
    ????? (indefinite blocking) ??????????????????????
    ??????????????????????????????????????????????????
    ? ?????????????????????????????????????????

31
???????????????????????????? Classical Problems
of Synchronization
  • ??????????????????????????????????????????????????
    ?
  • Bounded-Buffer problem (Producer/Consumer
    problem)
  • Readers and Writers problem
  • Dining-Philosophers problem

32
Bounded-Buffer Problem
  • ?????????????????????????-?????????
    (Producer/Consumer problem)
  • ??????????????????????????????????????????????????
    ????????????
  • ?????????????????????????????????????????????????
    (???????, ??????) ??????????????????
    ??????????????????????????????????????????????????
    ????????????????????????????????????
  • ??????????????????????????????????????????????????
    ?????????????????????????????????????
    ??????????? ? ????????????? ????
    (????????????????????) ???????????????????????????
    ????????????????????

33
Bounded-Buffer Problem (Cont.)
  • N buffers, each can hold one item
  • Semaphore mutex initialized to the value 1
  • Semaphore full initialized to the value 0
  • Semaphore empty initialized to the value N.

34
Bounded Buffer Problem (Cont.)
  • The structure of the producer process
  • do
  • // produce an item
  • wait (empty)
  • wait (mutex)
  • // add the item to the
    buffer
  • signal (mutex)
  • signal (full)
  • while (true)

35
Bounded Buffer Problem (Cont.)
  • The structure of the consumer process
  • do
  • wait (full)
  • wait (mutex)
  • // remove an item from
    buffer
  • signal (mutex)
  • signal (empty)
  • // consume the removed item
  • while (true)

36
Readers-Writers Problem
  • ????????????-?????????????????????????????????????
    ????????????????????????????????????????????
  • ??????????????????????????????????????????????????
    ?????????????
  • ??????? ?????????????????????????????, ????????
    update
  • ???????? ???????????????????????????
  • ??????????? ??????????????????????????????????????
    ????????????????? ????????????????????????????????
    ? (readers) ??????????????????????????????
    ????????????????????????????? (writers)
    ??????????????????????????????????????????????????

37
Readers-Writers Problem (Cont.)
  • ???????????????????????????????????????????????
  • 1. ??????????????????????????????????????
  • 2. ???????????????????????????????????????????????
    ???? ? ???????????????
  • 3. ?????????????????????????????
    ???????????????????????????????????????????
  • ?????????????????????????????????????????????
  • ??????? ??????????????????????????????????????
  • ???????? ?????????????????????????????????????
  • Shared Data
  • Data set
  • Semaphore mutex initialized to 1.
  • Semaphore wrt initialized to 1.
  • Integer readcount initialized to 0.

38
Readers-Writers Problem (Cont.)
  • The structure of a writer process
  • do
  • wait (wrt)
  • // writing is performed
  • signal (wrt)
  • while (true)

39
Readers-Writers Problem (Cont.)
  • The structure of a reader process
  • do
  • wait (mutex)
  • readcount
  • if (readercount 1) wait
    (wrt)
  • signal (mutex)
  • // reading is
    performed
  • wait (mutex)
  • readcount - -
  • if redacount 0) signal
    (wrt)
  • signal (mutex)
  • while (true)

40
Dining-Philosophers Problem
  • Shared data
  • Bowl of rice (data set)
  • Semaphore chopstick 5 initialized to 1

41
Dining-Philosophers Problem (Cont.)
  • ?????????????? 5 ?????????????????????????????
  • ?????????????????? 5 ????????????????
  • ?????????????????? 5 ?????????????????????????????
    ????????????????????????
  • ??????????????????????????????????????????????????
    ???????????????????????????????????????????
    ??????????????????????????????????????????????????
    ?????????????????????
  • ??????????????????????????????????????????????????
    ???????????? ?????????????????????????????????????
    ???????????????????????

42
Dining-Philosophers Problem (Cont.)
  • The structure of Philosopher i
  • Do
  • wait ( chopsticki )
  • wait ( chopStick (i 1) 5 )
  • // eat
  • signal ( chopsticki )
  • signal (chopstick (i 1) 5 )
  • // think
  • while (true)

43
Problems with Semaphores
  • ??????????????????????????????????????????????????
    ????????????????????????????? ????
    ??????????????????????????????????????????????????
    ??????????????????????????????????????????????????
    ??????????????????????
  • ????????????????????????????? ???
  • ?????????????????????????????????
  • ??????????????????????????????????????????????????
    ???????? P ??????????
  • ?????????????????????????????????????? V
    ????????????????????

44
Problems with Semaphores (Cont.)
  • Correct use of semaphore operations
  • signal (mutex) . wait (mutex)
  • wait (mutex) wait (mutex)
  • Omitting of wait (mutex) or signal (mutex) (or
    both)

45
Problems with Semaphores (Cont.)
  • ?????????????????????????????????????
    ??????????????????? ???????????
  • ??????????????????????? 4 ????????????
    ????????????????????
  • ??????????????????????????????????????????????????
    ??????????????????????? (?????????????????????????
    ??????????????)
  • ????????????????????????? ????????????
    ????????????????????????????????????????
    ???????????????????? ?????????????????????????????
    ??????????????????????????????????????????????????
  • ??????????????????????????????????????????????????
    ??????????????????????????????????????
    ?????????????????????????????????????????????

46
????????????? Monitors
  • ??????????????????????????????????????????????????
    ??????????????????? (procedure)
    ???????????????????????? ???????????????????????
  • ?????????????????????????????????
    ???????????????????????????????????????????????
  • ????????????????????????????????????????
  • ??????????????????????????????????????????????????
    ????????????????????????????????
    ??????????????????????????????????????? ?????????
  • ??????????????????????????????????????????????????
    ??????????????????????????????????
  • ??????????????????????????????????????????????????
    ??????????????????????????? ?????????????????????
    ??????????????????????????????????????????????????
    ????????????
  • ? ??????????????? ????????????????????????????????
    ?? active ??????????????????????

47
????????????? Monitors
  • monitor monitor-name
  • // shared variable declarations
  • procedure P1 () .
  • procedure Pn ()
  • Initialization code ( .)

48
Schematic view of a Monitor
49
Condition Variables
  • condition x, y
  • Two operations on a condition variable
  • x.wait () a process that invokes the operation
    is
  • suspended.
  • x.signal () resumes one of processes (if any)
    tha
  • invoked x.wait ()

50
Monitor with Condition Variables
51
Solution to Dining Philosophers
  • ????????????????????????????????????
    ???????????????????????????????????????????????
  • ??????????????????????????????????????????????????
    ??????????? pickUp() ?????????????????????????????
    ?????
  • ????????????????????????????????? putDown()
    ????????????????
  • ??????? ????????? i ???????????????????? pickUp()
    ??? putDown() ????????

52
Solution to Dining Philosophers (cont.)
  • monitor DP
  • enum THINKING HUNGRY, EATING) state 5
  • condition self 5
  • void pickup (int i)
  • statei HUNGRY
  • test(i)
  • if (statei ! EATING) self i.wait
  • void putdown (int i)
  • statei THINKING
  • // test left and right
    neighbors
  • test((i 4) 5)
  • test((i 1) 5)

53
Solution to Dining Philosophers (cont.)
  • void test (int i)
  • if ( (state(i 4) 5 ! EATING)
  • (statei HUNGRY)
  • (state(i 1) 5 ! EATING) )
  • statei EATING
  • selfi.signal ()
  • initialization_code()
  • for (int i 0 i lt 5 i)
  • statei THINKING

54
Synchronization Examples
  • Solaris
  • Windows XP
  • Linux
  • Pthreads

55
Solaris Synchronization
  • ?????? lock ??????? ?????????????????
    multitasking ??? multithreading (???????
    real-time threads) ??? multiprocessing
  • ??? adaptive mutexes ?????????????????????????????
    ???????????????????????? short code segments
  • ??? lock ??? condition variables and
    readers-writers ??????????????????????????????????
    ???????????
  • ??? turnstiles ???????????????????????????????????
    ? ?????????? adaptive mutex or reader-writer lock

56
Windows XP Synchronization
  • ??? interrupt ????????????????????????????????????
    ???????????????????????? (uniprocessor systems)
  • ??? spinlocks ?? multiprocessor systems
  • ??????????? dispatcher objects ????????????????
    mutexes ??? semaphores
  • Dispatcher objects ?????????????????????????
    (events)
  • ?????????????????????????????????????????????????
    (condition variable)

57
Linux Synchronization
  • Linux
  • ??????????? interrupts ????????????????????????
    (short critical sections) ???
  • Linux provides
  • semaphores
  • spin locks

58
Pthreads Synchronization
  • Pthreads API is OS-independent
  • It provides
  • mutex locks
  • condition variables
  • Non-portable extensions include
  • read-write locks
  • spin locks

59
End of Process Synchronization
60
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com