Concurrency: Mutual Exclusion and Synchronization - PowerPoint PPT Presentation

1 / 88
About This Presentation
Title:

Concurrency: Mutual Exclusion and Synchronization

Description:

Lecture #5 Concurrency: Mutual Exclusion and Synchronization ( ) * The central themes of operating system design are all concerned with the ... – PowerPoint PPT presentation

Number of Views:132
Avg rating:3.0/5.0
Slides: 89
Provided by: mclabSil
Category:

less

Transcript and Presenter's Notes

Title: Concurrency: Mutual Exclusion and Synchronization


1
Lecture 5
  • Concurrency Mutual Exclusion and Synchronization
  • (????? ???)

2
?? ???? ??
  • Central to the design of modern Operating Systems
    is managing multiple processes
  • Multiprogramming
  • Multiprocessing
  • Distributed Processing
  • Big Issue is Concurrency
  • Managing the interaction of all of these processes

3
???(Concurrency)
  • Concurrency arises in
  • Multiple applications
  • Sharing time
  • Structured applications
  • Extension of modular design
  • Operating system structure
  • OS themselves implemented as a set of processes
    or threads

4
Interleaving and Overlapping Processes (1)
  • processes may be interleaved on uniprocessors

5
Interleaving and Overlapping Processes (2)
  • And not only interleaved but overlapped on
    multi-processors

6
Difficulties of Concurrency
  • Sharing of global resources
  • Optimally managing the allocation of resources
  • Difficult to locate programming errors as results
    are not deterministic and reproducible.

7
???? ????(Process Interaction)
8
???? ?? (Program Execution)
  • ????? ????? ???? ?????
  • ???? ??(Program Execution)
  • ?? ??(Sequential Execution)
  • ?? ??(Concurrent Execution)
  • ?? ??(Parallel Execution)
  • ???(???Concurrency) gt ???(Parallelism)
  • ???? ?? ??? ?? ?? ???? ??

9
?? ??(Concurrent Execution)? ???
  • ?? ?????(Concurrent processes (or threads))?
    ????? ???? ??? ????
  • ?? ?? ???? ?? ??? ???? ???? ??? ?? ????? ??? ????
    ???? ??
  • ?? ????? ?? ??? ?? ???? ?? ??? ??? ??? ??? ???? ??

10
Race Condition (?? ??)
  • ?? ?? (Race Condition)
  • ??? ????? ?? ????? ????? ??? ??? ??? ?? ??
  • ?? ?? ??? ????? ???? ?? ??? ?? ???.

????(Resource Sharing) ????(Resource Competition)
11
?? ??? ??? ?
  • 3 variables A, B, C which are shared by thread
    T1 and thread T2
  • T1 computes C AB
  • T2 transfers amount X from A to B
  • T2 must do A A -X and B BX (so that AB is
    unchanged)
  • But if T1 computes AB after T2 has done A A-X
    but before B BX then T1 will not obtain the
    correct result for C A B
  • The resource sharing requires the synchronization
    between threads

12
?? ??? ?(1)
  • ??? ??? ????

1
n
n-1
2
3

4
13
?? ??? ?(2)
  • ??? ???? ??
  • repeat
  • while count 0 do no-op
  • nextc bufferout
  • out (out 1) mod n
  • count count - 1
  • . . .
  • nextc?? ? ??? ??
  • . . .
  • until FALSE
  • ??? ???? ??
  • repeat
  • . . .
  • nextp?? ? ??? ??
  • . . .
  • while count n do no-op
  • bufferin nextp
  • in (in 1) mod n
  • count count 1
  • until FALSE

14
?? ??? ?(3)
  • ??? ???? ??
  • count count - 1
  • ? register2 count
  • register2 register2 - 1
  • count register2
  • ??? ???? ??
  • count count 1
  • ? register1 count
  • register1 register1 1
  • count register1

15
?? ??? ?(4)
  • ??? ????? ??? ????? ?? ???? ??
  • T0 ??? ? register1 count register1 5
  • T1 ??? ? register1 register11 register1 6
  • T2 ??? ? register2 count register2 5
  • T3 ??? ? register2 register2-1 register2 4
  • T4 ??? ? count register2 count 4
  • T5 ??? ? count register1 count 6
  • ??? ? ?? ????? ??? count ??? ??
  • ???
  • count ??? ???? ??? ??, ? ??? ??? ?????? count ???
    ????? ??
  • ???? ???(Process Synchronization)? ??

16
?? ????? ?? ??
  • ?? ????? ?? ??? ?? ???
  • ?? ??(Mutual Exclusion)
  • ?? ?????? ?? ??? ??? ?????? ?? ??? ??? ??? ??? ?
    ??
  • ? ??? ?? ??? ???? ?? ??(???? ?? ???)? ?? ???
    ????? ??
  • ?? ??? (Execution Synchronization)
  • ??? ????? ???? ?? ?????? ???? ??? ?? ??? ???
    ????? ??
  • ? ???-??? ???? ???? ??? ???? ????? ???? ??? ???
    ?????

17
????(critical section) ??(1)
  • ????(Critical Section CS)
  • ??? ????? ?? ???? ???? ??? ??? ?? ? ????? ?????
    ???? ??
  • ?? ??(Mutual Exclusion)
  • The execution of critical sections must be
    mutually exclusive
  • ?? ???? ????? ?????? ???? ?? ????? ?? ??? ??? ??
  • ?? ??? ?? ???
  • ?? ???? ? ????? ????? ???? ???? ???? ??? ??
    ??(permission)? ??? ??

18
????(critical section) ??(2)
  • ?? ??(critical section) ??
  • ???? ??? ????? ??
  • ?? ????? ?? ??? ?? ??? ???? ????? ???? ???? ????
    ????? ??

19
?? ?? ??? ?? ??(1)
  • ?? ????? ???? ??
  • ?? ??(entry section)
  • ?? ??? ???? ?? ??? ???? ?? ??
  • ?? ??(exit section)
  • ?? ?? ??? ??? ???? ?? ??? ???? ?? ?? ??
  • ?? ??(remainder section)
  • ????? ??? ?? ??

20
?? ?? ??? ?? ??(2)
  • ?? ????? ???? ??(??)

Repeat entry section critical section
exit section remainder section until FALSE
21
?? ?? ??? ?? ??(3)
  • ? ????? 0? ?? ??? ????
  • n?? ????? ???? ?? ??? ?? ??? ??
  • n ?? ????? ????? ?? ??? ??
  • ?? CPU? ??? ? ??
  • ??? ??? ??? ??? ??? ? ??? ???? ??? ????? ??
  • ?? ?? ??? ?? ???? ?? ?? ?? ??? ?? ??? ??? ???? ???

22
?? ?? ??? ???? ?? ? ?? ????(1)
  • ????(Mutual Exclusion)
  • ?? ??? ??? ?????? ?????? ??? ? ??
  • ? ????? ????? ??? ?? ????? ????? ??? ? ??
  • ??(Progress)
  • ????? ?? ????? ?? ?? ?????? ?? ?? ??? ??? ?? ??
    ?? ???? ???? ?? ?? ???? ?? ?? ??? ??? ? ??
  • ?? ?? ?? ???? ??? ??? ??? ? ??
  • Deadlock Avoidance

23
?? ?? ??? ???? ?? ? ?? ????(2)
  • ?? ??(Bounded Waiting)
  • ??? ????? ?? ?? ??? ??? ?? ??? ??? ??? ?? ?????
    ?? ??? ???? ??(??)? ??? ??? ??
  • ??? ??? ?? ??? ????? ????(starvation)? ? ? ??

24
?? ?? ?? ???? ??
  • ????? ???(Software solutions)
  • software algorithms whos correctness does not
    rely on any other assumptions
  • ???? ???(Hardware solutions)
  • rely on some special machine instructions
  • ???? ???(Operation System solutions)
  • OS provide some functions and data structures to
    the programmer

25
????? ???
  • 2?? ????? ?? ???
  • Algorithm 1 and 2 are incorrect
  • Algorithm 3 is correct (Petersons algorithm)
  • n ?? ????? ?? ???
  • the bakery algorithm
  • Notation
  • 2 processes P0 and P1
  • When presenting processes, Pi, Pj always denote
    the other processes (i ! j)

26
Algorithm 1
  • The shared variable turn is initialized (to 0 or
    1) before executing any Pi
  • Pis critical section is executed iff turn i
  • Pi is busy waiting if Pj is in CS mutual
    exclusion is satisfied
  • Progress requirement is not satisfied

Process Pi repeat while(turn!i) CS
turnj RS until FALSE
  • Ex P0 has a large RS and P1 has a small RS. If
    turn0, P0 enter its CS and then its long RS
    (turn1). P1 enter its CS and then its RS
    (turn0) and tries again to enter its CS request
    refused! He has to wait that P0 leaves its RS.

27
Algorithm 2
  • Keep one BOOL variable for each process flag0
    and flag1
  • Pi signals that it is ready to enter its CS by
    flagitrue
  • Mutual Exclusion is satisfied but not the
    progress requirement
  • If we have the sequence
  • T0 flag0true
  • T1 flag1true
  • Both process will wait forever to enter their CS
    we have a deadlock

Process Pi repeat flagitrue
while(flagj) CS flagifalse
RS until FALSE
28
Algorithm 3 (Petersons algorithm)
Process Pi repeat flagitrue turnj
do while (flagjand turnj) CS
flagifalse RS until FALSE
  • Initialization flag0flag1false
  • turn 0 or 1
  • Willingness to enter CS is specified by
    flagitrue
  • If both processes attempt to enter their CS
    simultaneously, only one turn value will last
  • Exit section specifies that Pi is unwilling to
    enter CS

29
Algorithm 3 proof of correctness(1)
  • Mutual exclusion is preserved since
  • P0 and P1 are both in CS only if flag0
    flag1 true and only if turn i for each Pi
    (impossible)
  • The progress and bounded waiting requirements are
    satisfied
  • Pi cannot enter CS only if stuck in while() with
    condition flag j true and turn j.
  • If Pj is not ready to enter CS then flag j
    false and Pi can then enter its CS

30
Algorithm 3 proof of correctness(2)
  • If Pj has set flag jtrue and is in its
    while(), then either turni or turnj
  • If turni, then Pi enters CS. If turnj then Pj
    enters CS but will then reset flag jfalse on
    exit allowing Pi to enter CS
  • but if Pj has time to reset flag jtrue, it
    must also set turni
  • since Pi does not change value of turn while
    stuck in while(), Pi will enter CS after at most
    one CS entry by Pj (bounded waiting)

31
n-process solution The bakery algorithm (1)
  • Before entering their CS, each Pi receives a
    number. Holder of smallest number enter CS
  • (like in bakeries, ice-cream stores...)
  • When Pi and Pj receives the same number
  • if I lt j then Pi is served first, else Pj is
    served first
  • Pi resets its number to 0 in the exit section
  • Notation
  • (a, b) lt (c, d) if a lt c or if a c and b lt d
  • max(a0,,ak) is a number b such that b gt ai for
    i0,..k

32
The bakery algorithm (2)
  • Shared data
  • choosing array0..n-1 of boolean
  • initialized to false
  • number array0..n-1 of integer
  • initialized to 0
  • Correctness relies on the following fact
  • If Pi is in CS and Pk has already chosen its
    numberk! 0, then (numberi, i) lt (numberk,
    k)

33
The bakery algorithm (3)
Process Pi repeat choosingitrue
numberimax(number0..numbern-1)1
choosingifalse for j0 to n-1 do
while (choosingj) while (numberj!0
and (numberj,j)lt(numberi,i))
CS numberi0 RS until FALSE
34
????? ???? ??
  • ?? ??? ????? ??? ????? busy waiting ??
  • ?? ?? CPU ??? ????
  • ?? ??? ? ???? ?? ?? ??? ???? ????? ?? ??? ???? ?(
    blocking)? ?????

35
???? ??? interrupt disabling
  • On a uniprocessor mutual exclusion is preserved
    but efficiency of execution is degraded
  • while in CS, we cannot interleave execution with
    other processes that are in RS
  • On a multiprocessor mutual exclusion is not
    preserved
  • Generally not an acceptable solution

Process Pi repeat disable interrupts
critical section enable interrupts remainder
section until FALSE
36
???? ??? special machine instructions
  • ????? ??? ??? ??? ?? ??? ?????
  • ??? ???? ?? ??? ?? ?? ??? ???? ? ??
  • ??? ?? ?? ??? ??? 2 ??? ??? ????? (atomically)
    ???? ??? ??
  • ? - reading and writing a memory location
  • test-and-set, xchg(swap) instructions
  • The execution of such an instruction is mutually
    exclusive (even with multiple CPUs)
  • ???? ??? ???? ?? ??

37
test-and-set instruction(1)
  • A C description of test-and-set instruction

bool testset(int i) if (i0)
i1 return true else
return false
38
test-and-set instruction(2)
  • An algorithm that uses testset instruction for
    Mutual Exclusion
  • Shared variable b is initialized to 0
  • Only the first Pi who sets b enter CS

Process Pi repeat repeat until
testset(b) CS b0 RS until FALSE
39
test-and-set instruction (3)
  • Mutual exclusion is preserved
  • if Pi enter CS, the other Pj are busy waiting
  • Problem still using busy waiting
  • When Pi exit CS, the selection of the Pj who will
    enter CS is arbitrary no bounded waiting
  • Hence starvation is possible

40
xchg instruction
  • Processors (ex Pentium) often provide an atomic
    xchg(a,b) instruction that swaps the content of a
    and b.
  • void xchg(int a, int b)
  • int temp
  • temp a
  • a b
  • b temp
  • xchg(a,b) suffers from the same drawbacks as
    test-and-set instruction

41
Using xchg for mutual exclusion
  • Shared variable b is initialized to 0
  • Each Pi has a local variable k
  • The only Pi that can enter CS is the one who
    finds b0
  • This Pi excludes all the other Pj by setting b to
    1

Process Pi repeat k1 repeat xchg(k,b)
until k0 CS b0 RS until FALSE
42
???? ???
  • ????? ???? ???? ?? ??? ??
  • ????(Semaphores) / ???(Mutex)
  • ???(Monitors)
  • ??? ??(Message Passing)

43
????(Semaphores) (1)
  • ?? definition
  • Busy waiting? ???? ?? Synchronization tool
    (provided by the OS)
  • ????? ???? ??? ??
  • ???? S? ??? 2 atomic and mutually exclusive
    operations? ???? ??? ? ?? ??
  • wait(S)
  • signal(S)
  • Busy waiting avoidance
  • when a process has to wait, it will be put in a
    blocked queue of processes waiting for the same
    event

44
????(Semaphores) (2)
  • A semaphore is a record (structure)

type semaphore record count
integer queue list of
process end var S semaphore
  • wait ???? ????? ???? S? ???? ? ??? ?? ??? ????
    ???? ?? ????
  • signal ??? ???? ???? ??? ????? ??? ?? ?? ????

45
???? ?? (atomic)
wait(S) S.count-- if (S.countlt0)
block this process place this process in
S.queue signal(S) S.count if
(S.countlt0) remove a process P from
S.queue place this process P on ready list
  • S.count must be initialized to a nonnegative
    value
  • (depending on application)

46
Semaphores observations (1)
  • S.count gt0 ? ?
  • the number of processes that can execute wait(S)
    without being blocked S.count
  • S.countlt0 ? ?
  • the number of processes waiting on S S.count
  • Atomicity and mutual exclusion
  • no 2 process can be in wait(S) and signal(S) (on
    the same S) at the same time (even with multiple
    CPUs)
  • The blocks of code defining wait(S) and signal(S)
    are critical sections

47
Semaphores observations (2)
  • The critical sections defined by wait(S) and
    signal(S) are very short
  • typically 10 instructions
  • Solutions
  • uniprocessor disable interrupts during these
    operations (i.e for a very short period)
  • This does not work on a multiprocessor machine
  • multiprocessor use previous software or hardware
    schemes
  • The amount of busy waiting should be small.

48
Using semaphores for solving critical section
problems
  • n ?? ????? ??
  • S.count? 1? ???
  • ?? ??? ????? CS ??? ???? (mutual exclusion)
  • S.count? k? ????? k ?? ????? CS? ??? ? ??

Process Pi repeat wait(S) CS signal(S)
RS until FALSE
49
Using semaphores to synchronize processes
  • Proper synchronization is achieved by having in
    P1
  • S1
  • signal(synch)
  • And having in P2
  • wait(synch)
  • S2
  • We have 2 processes P1 and P2
  • Statement S1 in P1 needs to be performed before
    statement S2 in P2
  • Then define a semaphore synch
  • Initialize synch to 0

50
???/??? ??(producer/consumer problem)
  • A producer process produces information that is
    consumed by a consumer process
  • ? a print program produces characters that are
    consumed by a printer
  • ??? ??? ??? ??? ??? buffer? ????
  • A common paradigm for cooperating processes

51
P/C Problem unbounded buffer(1)
  • 1?? ??? ??? ?? ??(unbounded buffer) ? ??? ??
  • in ? ??? ??? ??? ??? ??? ??
  • Out ? ??? ??? ???? ?? ??? ??

52
P/C Problem unbounded buffer(2)
  • semaphore S to perform mutual exclusion on the
    buffer
  • only 1 process at a time can access the buffer
  • semaphore N to synchronize producer and
    consumer on the number N ( in - out) of items in
    the buffer
  • an item can be consumed only after it has been
    created

53
P/C Problem unbounded buffer(3)
  • ???? ?? ???? ??? ??? ??? ??? ? ??
  • ??? ???? ?? wait(S)
  • ??? ??? ?? signal(S)
  • ???? ??? ??? ??? ??? ?? ??? ??? ?????? ??? ?? ??
    signal(N) ??
  • ???? ?? ??? ??? ???? ???? ?? wait(N)? ????, ???
    ??? ??? ??? ???? ?? wait(S)/signal(S) ? ??

54
P/C Problem unbounded buffer(4)- Solution
append(v) binv in
Initialization S.count1 N.count0
inout0
take() wbout out return w
55
P/C Problem unbounded buffer(5)- Solution
Producer repeat produce v wait(S)
append(v) signal(S) signal(N) until FALSE
Consumer repeat wait(N) wait(S)
wtake() signal(S) consume(w) until FALSE
critical sections
56
P/C Problem unbounded buffer(6)
  • Remarks
  • The consumer must perform wait(N) before wait(S),
    otherwise deadlock occurs if consumer enter CS
    while the buffer is empty

57
P/C Problem finite circular buffer(1)
  • ??? ??? ?? N? ??? 1 ??? ?? ???? ?? ??? ????(ie. N
    ! in out)
  • ???? ??? ? ??? ? E? 1 ??? ?? ??? ??? ??? ? ??

58
P/C Problem finite circular buffer(2)
  • ??? ????
  • semaphore S to have mutual exclusion on buffer
    access
  • semaphore N to synchronize producer and
    consumer on the number of consumable items
  • semaphore E to synchronize producer and
    consumer on the number of empty spaces

59
P/C Problem finite circular buffer(3)- Solution
Initialization S.count1 in0
N.count0 out0 E.countk
append(v) binv in(in1) mod k
Producer repeat produce v wait(E)
wait(S) append(v) signal(S)
signal(N) until FALSE
Consumer repeat wait(N) wait(S)
wtake() signal(S) signal(E)
consume(w) until FALSE
take() wbout out(out1) mod
k return w
critical sections
60
The Dining Philosophers Problem(1)
  • 5 philosophers who only eat and think
  • each need to use 2 forks for eating
  • we have only 5 forks
  • A classical synchronization problem
  • Illustrates the difficulty of allocating
    resources among process without deadlock and
    starvation

61
The Dining Philosophers Problem(2)
  • Each philosopher is a process
  • One semaphore per fork
  • fork
  • array0..4 of semaphores
  • Initialization
  • forki.count1 for i0..4
  • A first attempt ?
  • Deadlock if each philosopher start by picking his
    left fork!

Process Pi repeat think wait(forki)
wait(forki1 mod 5) eat signal(forki1 mod
5) signal(forki) forever
62
The Dining Philosophers Problem(3)
  • A solution admit only 4 philosophers at a time
    that tries to eat
  • Then 1 philosopher can always eat when the other
    3 are holding 1 fork
  • Hence, we can use another semaphore T that would
    limit at 4 the number of philosophers sitting at
    the table
  • Initialize T.count4

Process Pi repeat think wait(T)
wait(forki) wait(forki1 mod 5) eat
signal(forki1 mod 5) signal(forki)
signal(T) forever
63
?? ???? (Binary semaphores) (1)
  • ?? ????(counting (or integer) semaphores)
  • ?? ????(binary semaphores)
  • counting semaphores? ???? count? Boolean ? (0
    ?? 1)? ???
  • ????? counting semaphores ?? ???? ???
  • ? k gt 1? ??? ????? ???

64
?? ????(Binary semaphores)(2)
waitB(S) if (S.value 1) S.value
0 else block this process place
this process in S.queue signalB(S) if
(S.queue is empty) S.value 1 else
remove a process P from S.queue place
this process P on ready list
65
???(Mutex)
  • ?? ????? ??? ??
  • ???
  • Mutex? ?? ?? ???(0?? ?? ???) ?????? ?? ??? ?(1?
    ?? ??? ?) ??.
  • ?? ???? ??, ?? ??? ????? ?? ???? ????? ?? ? ??.

66
Spinlocks
  • They are counting semaphores that use busy
    waiting (instead of blocking)
  • Useful on multi processors when critical sections
    last for a short time
  • We then waste a bit of CPU time but we save
    process switch

wait(S) S-- while Slt0 do signal(S)
S
67
????? ???
  • ????? ????(mutual exclusion)? ???? ???? ??? ??
  • wait(S) ? signal(S)? ?? ???? ??? ??? ? ?? ????
    ??? ????? ???
  • ?? ?????? ????? ????? ????? ??
  • ??? ?????? ???? ??? ??? ???? ?? ????? ??? ? ??

68
???(Monitors) (1)
  • ?? ????? ???? ???? ??? ?? (Synchronization
    Construct)
  • ????? ??? ??? ??
  • ?? ?? ?? ??? ??
  • ?? ?? ????? ??(Concurrent Programming Language)??
    ??
  • Concurrent Pascal, Modula-3, uC, Java...

69
???(Monitors) (2)
  • ?? ??? ???? ????? ??
  • ?? ??
  • ?? ??? ?????(procedures)
  • ??? ??(initialization sequence)
  • ?? ??

type monitor-name monitor local variable
declarations procedure entry P1() begin
end procedure entry P2() begin
end procedure entry Pn() begin
end begin initialization code end
70
???(Monitors) (3)
  • ??? ?? ??
  • ?? ??? ??? ?? ????? ???? ??? ? ??
  • ????? ???? ???? ??? ?? ???? ??? ? ??
  • ??? ?? ??? ?????? ??? ?? ?? ? ??
  • ???? ????(mutual exclusion)? ????
  • ?????? ????? ??? ??? ????? ??? ??? ??
  • ?? ??? ?? ??? ??? ?? ???? ??? ? ??
  • ?? ??? ?? ??? ??

71
?? ??(Condition variables)
  • ??? ????? ??? ??? ???? ?? ???? ?? ??? ???? ??
  • ?? ????? ??? ??
  • ?? ??? ???? ?? ????
  • ??? ??? ??? ???? ?? ? ??? ????
  • cwait(a) ?? ?? a ? ?? ??? ??? ?? ??? ????
  • csignal(a) ?? ?? a ? ?? ?? ??? ???? ? ??? ?????
    ???? ??? ????
  • ?????? ?? ???? ???? ????

72
???? ??
  • ?? ????? ?? ? ?? ?? ?? ??? ????
  • ??? ????? cwait(cn)? ???? ?? ?? cn ? ??? ???? ??
  • csignal(cn)? ???? ?? ?? cn ? ??? ??? ????? ???
    ???? ??
  • csignal(cn)? ??? ????? ??? ???? urgent queue??
    ????

73
???/??? ???? ??(1)
  • Two types of processes
  • producers
  • consumers
  • Synchronization is now confined within the
    monitor
  • append(.) and take(.) are procedures within the
    monitor
  • The procedures are the only means by which P/C
    processes can access the buffer
  • If these procedures are correct, synchronization
    will be correct for all participating processes

Producer_I repeat produce v
Append(v) until FALSE Consumer_I repeat
Take(v) consume v until FALSE
74
???/??? ???? ??(2)
  • ??? ???? ?? ???(Monitor)? ??
  • buffer array0..k-1 of items
  • ??? ?? ??
  • notfull csignal(notfull)? ??? ?? ???? ????
  • notempty csignal(notempty)? ??? ?? ?? ??? ????
  • ?? ??? ?? ???? ???
  • nextin ?? ??? ??? ?? ???
  • nextout ?? ??? ??? ?? ???
  • count ?? ??? ??? ??? ?

75
???/??? ???? ??(3)
Monitor boundedbuffer buffer array0..k-1 of
items nextin0, nextout0, count0
integer notfull, notempty condition
Append(v) if (countk) cwait(notfull)
buffernextin v nextin nextin1 mod k
count csignal(notempty) Take(v)
if (count0) cwait(notempty) v
buffernextout nextout nextout1 mod k
count-- csignal(notfull)
76
??? ??(Message Passing)
  • ?????? ??(interprocess communication IPC)? ??
    ???? ??
  • ??? ??? ?? ????? ??
  • ?? ??? ?? ????? ??
  • ???? ??? ? ???? ??? ?? ???? ??
  • ??? ???? ??
  • send(destination, message)
  • receive(source, message)
  • ?? ???? ????? ????? ??? ?? ?? ?? ?? ??

77
??? ??? ?? ??? (1)
  • ??? ????? send(.,.) ??? ??? ???
    ????(non-blocking)
  • ??? ????? ??? ??? ????
  • ?? ???? ??? ?? ??? ?? ??? ????
  • ??? ????? receive(.,.) ??? ??? ???? ??? ???
    ????? ??(blocking)
  • ?? ???? ??? ???? ?? ??? ????- ??? ??? ??? ????
  • ???? ???? ?? ?? ????? ???? ??? ???? ??

78
??? ??? ?? ??? (2)
  • ?? ???? ????
  • ? blocking send, blocking receive
  • both are blocked until the message is received
  • occurs when the communication link is unbuffered
    (no message queue)
  • provides tight synchronization (rendez-vous)

79
??? ????? ?? ??
  • ?? ?? ??(direct addressing)
  • ??? ?? ???? ???/???? ?? ?? ???? ID? ????
  • ?? ?? ??(indirect addressing)
  • ???? ??? ?? ??? mailbox? ????
  • ???? mailbox? ???? ???, ???? mailbox?? ???? ????
  • ? ? ????? ?? ?? ??

80
Mailboxes and Ports
  • A mailbox can be private to one sender/receiver
    pair
  • The same mailbox can be shared among several
    senders and receivers
  • Port a mailbox associated with one receiver and
    multiple senders
  • used for client/server applications the receiver
    is the server

81
Ownership of ports and mailboxes
  • Port? ?? ????? ?? ???? ????
  • port? ?? ????? ???? ????
  • Mailbox? ???? ??? ?? ????? ????
  • ??? ????? ???? ??
  • ???? ?? ????? ???? ??? ?? ????

82
??? ??(Message Format)
  • ??? ??? ??? ??
  • control information
  • what to do if run out of buffer space
  • sequence numbers
  • priority etc.

83
??? ??? ?? ????
  • N ?? ????? ??? mailbox mutex ? ????
  • send() ? non blocking
  • receive()? mutex ? ?? ??? blocking empty
  • ???
  • send(mutex, go)
  • receive()? ?? ???? ???? Pi? ???? (CS)? ????
  • ?? ????? Pi ? ???? ??? ?? ????

Process Pi var msg message repeat
receive(mutex,msg) CS send(mutex,msg)
RS forever
84
??? ??? ?? ???/??? ???? ??(1)
const capacity // buffering capacity
null // empty message var i
integer procedure producer var pmsg
message begin while true do begin
receive(mayproduce, pmsg) pmsg produce
send(mayconsume, pmsg) end end procedure
consumer var cmsg message begin while
true do begin receive(mayconsume,
cmsg) consume(cmsg) send(mayproduce,
null) end end
85
??? ??? ?? ???/??? ???? ??(2)
parent process begin create_mailbox(mayprodu
ce) create_mailbox(mayconsume) for i1 to
capacity do send(mayproduce, null) parbegin
producer consumer parend end
86
???/??? ??(Readers/Writers Problem)
  • Any number of readers may simultaneously read the
    file
  • Only one writer at a time may write to the file
  • If a writer is writing to the file, no reader may
    read it

87
(No Transcript)
88
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com