1. Operating Systems Principles Process Management and Coordination Lecture 3: Higher-Level Synchronization and Communication 主講人:虞台文
2.
3. Operating Systems Principles Process Management and Coordination Lecture 3: Higher-Level Synchronization and Communication Motivation
4.
5.
6. Operating Systems Principles Process Management and Coordination Lecture 3: Higher-Level Synchronization and Communication Share Memory Methods
7.
8. The Monitor Abstraction Shared among processes Processes cannot access them directly wait/signal primitives for processes communication or synchronization . Processes access the internal data only through these procedures. Procedure are mutually exclusive , i.e., only one process or thread may be executing a procedure within a given time. Internal Data Condition Variables Procedure 1 Procedure 2 Procedure 3
27. Example: Alarm Clock 0 1 3 2 4 5 . . . The current time ( now ) of the alarm clock is increased periodically ( tick ). Wakeup Queue The wakeup queue is used to hold processes to be waken up orderly according to their wakeup time ( alarm ) .
28. Example: Alarm Clock p1: . . . AlarmClock.Wakeme(100); . . . Wakeup Queue p1(250) Call at time 150
29. Example: Alarm Clock p1: . . . AlarmClock.Wakeme(100); . . . Wakeup Queue p1(250) p2: . . . AlarmClock.Wakeme(150); . . . p1(250) p2(350) Call at time 150 Call at time 200
30. Example: Alarm Clock p1: . . . AlarmClock.Wakeme(100); . . . Wakeup Queue p1(250) p2: . . . AlarmClock.Wakeme(150); . . . p1(250) p2(350) p3: . . . AlarmClock.Wakeme(30); . . . p3(240) Call at time 150 Call at time 200 Call at time 210
31. Example: Alarm Clock p1: . . . AlarmClock.Wakeme(100); . . . Wakeup Queue p1(250) p2: . . . AlarmClock.Wakeme(150); . . . p1(250) p2(350) p3: . . . AlarmClock.Wakeme(30); . . . p3(240) p4: . . . AlarmClock.Wakeme(10); . . . p4(240) Call at time 150 Call at time 200 Call at time 210 Call at time 230
32.
33. Example: Alarm Clock p1: . . . AlarmClock.Wakeme(100); . . . Wakeup Queue p1(250) p2: . . . AlarmClock.Wakeme(150); . . . p1(250) p2(350) p3: . . . AlarmClock.Wakeme(30); . . . p3(240) p4: . . . AlarmClock.Wakeme(10); . . . p4(240) Call at time 150 Call at time 200 Call at time 210 Call at time 230
45. Operating Systems Principles Process Management and Coordination Lecture 3: Higher-Level Synchronization and Communication Distributed Synchronization and Communication
46.
47.
48. Types of Send/Receive broadcast message m broadcast message m wait until accepted implicit naming send message m to receiver r send message m to receiver r wait until accepted explicit naming nonblocking/asynchronous blocking/synchronous send if there is a message from any sender, then receive it; else proceed. wait message from any sender implicit naming if there is a message from sender s , then receive it; else proceed. wait message from sender s explicit naming nonblocking/asynchronous blocking/synchronous receive
49. Types of Send/Receive broadcast message m broadcast message m wait until accepted implicit naming send message m to receiver r send message m to receiver r wait until accepted explicit naming nonblocking/asynchronous blocking/synchronous send if there is a message from any sender, then receive it; else proceed. wait message from any sender implicit naming if there is a message from sender s , then receive it; else proceed. wait message from sender s explicit naming nonblocking/asynchronous blocking/synchronous receive if there is a message from any sender, then receive it; else proceed. wait message from any sender implicit naming if there is a message from sender s , then receive it; else proceed. wait message from sender s explicit naming nonblocking/asynchronous blocking/synchronous receive
50. Types of Send/Receive Little practical used Little practical used Little practical used no use, e.g., Some debugging software may like it. broadcast message m broadcast message m wait until accepted implicit naming send message m to receiver r send message m to receiver r wait until accepted explicit naming nonblocking/asynchronous blocking/synchronous send if there is a message from any sender, then receive it; else proceed. wait message from any sender implicit naming if there is a message from sender s , then receive it; else proceed. wait message from sender s explicit naming nonblocking/asynchronous blocking/synchronous receive
51. Process Coordination Little practical used Little practical used Solving a variety of process coordination problems. broadcast message m broadcast message m wait until accepted implicit naming send message m to receiver r send message m to receiver r wait until accepted explicit naming nonblocking/asynchronous blocking/synchronous send if there is a message from any sender, then receive it; else proceed. wait message from any sender implicit naming if there is a message from sender s , then receive it; else proceed. wait message from sender s explicit naming nonblocking/asynchronous blocking/synchronous receive
52. Example:Printer Sever Little practical used Little practical used broadcast message m broadcast message m wait until accepted implicit naming send message m to receiver r send message m to receiver r wait until accepted explicit naming nonblocking/asynchronous blocking/synchronous send if there is a message from any sender, then receive it; else proceed. wait message from any sender implicit naming if there is a message from sender s , then receive it; else proceed. wait message from sender s explicit naming nonblocking/asynchronous blocking/synchronous receive
53. Implementation for Asynchronous Operations Little practical used Little practical used built-in buffers are required to hold messages broadcast message m broadcast message m wait until accepted implicit naming send message m to receiver r send message m to receiver r wait until accepted explicit naming nonblocking/asynchronous blocking/synchronous send if there is a message from any sender, then receive it; else proceed. wait message from any sender implicit naming if there is a message from sender s , then receive it; else proceed. wait message from sender s explicit naming nonblocking/asynchronous blocking/synchronous receive
60. Bounded buffer with CSP Buffer Producer Consumer deposit request remove receive(request) send(remove,data) receive(deposit, data) The Bounded Buffer uses the following three primitives for synchronization.
72. Rendezvous Caller Server q . f ( param ) accept f ( param ) S Similar syntax/semantics to RPC Name of the remote process (sever) Procedure name Procedure parameter Procedure body Keyword
73.
74. Semantics of a Rendezvous p q Rendezvous p q Rendezvous q . f () accept f () S q . f () accept f () S
85. Operating Systems Principles Process Management and Coordination Lecture 3: Higher-Level Synchronization and Communication Other Classical Problems
90. Solution Using Monitor monitor Readers_Writers { int readCount=0, writing=0; condition OK_R, OK_W; start_read() { if (writing || !empty(OK_W)) OK_R.wait; readCount = readCount + 1; OK_R.signal; } end_read() { readCount = readCount - 1; if (readCount == 0) OK_W.signal; } start_write() { if ((readCount != 0)||writing) OK_W.wait; writing = 1; } end_write() { writing = 0; if (!empty(OK_R)) OK_R.signal; else OK_W.signal; } } Called by a reader that wishes to read. Called by a reader that has finished reading. Called by a writer that wishes to write. Called by a writer that has finished writing.
91. Solution Using Monitor monitor Readers_Writers { int readCount=0, writing=0; condition OK_R, OK_W; start_read() { if (writing || ! empty (OK_W)) OK_R.wait; readCount = readCount + 1; OK_R.signal; } end_read() { readCount = readCount - 1; if (readCount == 0) OK_W.signal; } start_write() { if ((readCount != 0)||writing) OK_W.wait; writing = 1; } end_write() { writing = 0; if (! empty (OK_R)) OK_R.signal; else OK_W.signal; } } Additional Primitive: empty ( c ) Return true if the associated queue of c is empty.
97. The Problem of Clock Skewness File User (U) File Server (FS) e 1 e 2 e 3 e 4 C U C FS send send receive receive delta 1 delta 2 True causality of events: The log of events: 10 15 5 20 Impossible sequence!