SlideShare uma empresa Scribd logo
1 de 105
Operating Systems Principles Process Management and Coordination Lecture 3: Higher-Level Synchronization and Communication 主講人:虞台文
Content ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Operating Systems Principles Process Management and Coordination Lecture 3: Higher-Level Synchronization and Communication Motivation
Motivation ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Solutions ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Operating Systems Principles Process Management and Coordination Lecture 3: Higher-Level Synchronization and Communication Share Memory Methods
Monitors ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
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
Example: Queue Handler Queue AddToQueue RemoveFromQueue
Example: Queue Handler ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Using C-like Pseudo code. Since only one process may be executing a procedure within a given time,  mutual exclusion  is assured.
Process Synchronization ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],How about a process call  RemoveFromQueue  when the  queue  is empty?
Condition Variables ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Question: How about the procedure that makes the  c.signal  ( c.notify )  call? sleep  or keep  running ?
Variations on  Semantics ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],wait/signal wait/notify
More on Condition Variables ,[object Object],[object Object],[object Object],[object Object],[object Object]
Example: Queue Handler ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],One must ensure that the queue is  not  full before adding the item. An item is available here. One must ensure that the queue is  nonempty  before remove an item. A free node is available here.
Example: Queue Handler ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],One must ensure that the queue is  not  full before adding the item. An item is available here. One must ensure that the queue is  nonempty  before remove an item. A free node is available here. An event denotes that data item is available in the queue. An event denotes that some more item can be added to the queue.
Example: Queue Handler ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],An item is available here. One must ensure that the queue is  nonempty  before remove an item. A free node is available here.
Example: Queue Handler ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],An item is available here. A free node is available here.
Example: Queue Handler ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],A free node is available here.
Example: Queue Handler ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Hoare Monitors ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Queue is full p1  call AddtoQueue 1 p2  call RemoveFromQueue 3 2 p1  is blocked on freenodeAvail 5 P1 continues 4 P2  Signals  freenodeAvail  event 5’ P2  is blocked 6 P1  terminates 7 P2 continues
Example: Bounded Buffer Deposit Remove . . . . . . . . . 0 1 2 n  1 n  2
Example: Bounded Buffer Deposit Remove . . . . . . . . . 0 1 2 n  1 n  2 . . . nextin nextout count
Example: Bounded Buffer Deposit Remove count nextout nextin
Example: Bounded Buffer ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Priority Waits ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
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 ) .
Example: Alarm Clock p1: . . . AlarmClock.Wakeme(100); . . . Wakeup Queue p1(250) Call at time 150
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
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
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
Example: Alarm Clock ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
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
Mesa and Java Monitors ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Mesa and Java Monitors P1: . . . . . . . . if(!B1) c1.wait; . . . . . . . . P2: . . . . . . . . if(!B2) c2.wait; . . . . . . . . B1=FASLE; . . . . . . . .
Mesa and Java Monitors P1: . . . . . . . . if(!B1) c1.wait; . . . . . . . . P2: . . . . . . . . if(!B2) c2.wait; . . . . . . . . B1=FASLE; return; P3: . . . . . . . . B1=B2=TRUE; c1.notify; c2.notify; . . . . . . . . return; B1=FALSE
Mesa and Java Monitors P1: . . . . . . . . if(!B1) c1.wait; . . . . . . . . P2: . . . . . . . . if(!B2) c2.wait; . . . . . . . . B1=FASLE; return; P3: . . . . . . . . B1=B2=TRUE; c1.notify; c2.notify; . . . . . . . . return; B1=FALSE What action should  P1  take? Continue  or  wait  again?
Mesa and Java Monitors P1: . . . . . . . . if(!B1) c1.wait; . . . . . . . . P2: . . . . . . . . if(!B2) c2.wait; . . . . . . . . B1=FASLE; return; P3: . . . . . . . . B1=B2=TRUE; c1.notify; c2.notify; . . . . . . . . return; B1=FALSE What action should  P1  take? Continue  or  wait  again? 
Solution P1: . . . . . . . . if(!B1) c1.wait; . . . . . . . . P2: . . . . . . . . if(!B2) c2.wait; . . . . . . . . B1=FASLE; return; P3: . . . . . . . . B1=B2=TRUE; c1.notify; c2.notify; . . . . . . . . return; B1=FALSE What action should  P1  take? Continue  or  wait  again?    Replace   if   to   while . while while
Example: Queue Handler Queue AddToQueue RemoveFromQueue
Example: Queue Handler ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Hoare monitors use ` if ’.
Example: Queue Handler ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Mesa monitors use ` while ’.
Protected Types ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Defined in the  Ada95  language (ADA 1995).
Example: Bounded Buffer ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Operating Systems Principles Process Management and Coordination Lecture 3: Higher-Level Synchronization and Communication Distributed Synchronization and Communication
Distributed Synchronization ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Questions of  Send/Receive ,[object Object],[object Object],[object Object],[object Object]
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
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
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
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
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
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
Channels, Ports, and Mailboxes ,[object Object],[object Object],[object Object],[object Object],[object Object]
Named Message Channels Named Pipe (Win32) P1: . . . . . . . . send(ch1, msg1); . . . . . . . . P2: . . . . . . . . send(ch2, msg2); . . . . . . . . P3: . . . . . . . . receive(ch1, x); . . . . . . . . receive(ch2, y); . . . . . . . . ch1 ch2
CSP/Occam ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],CSP: Communicating Sequential Processes Occam: a Language
Bounded buffer with CSP ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],B P C
Bounded buffer with CSP Buffer Producer Consumer deposit request remove
Bounded buffer with CSP Buffer Producer Consumer deposit request remove send(deposit, data) send(request) receive(remove,data) receive(request) send(remove,data) receive(deposit, data)
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.
Bounded buffer with CSP ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Put data into buffer if buffer is not full and producer’s data is available. Pass data to the consumer if it has requested one.
Bounded buffer with CSP ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Pass data to the consumer if it has requested one.
Bounded buffer with CSP ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
More on Named Channels ,[object Object],[object Object],Buffer Producer Consumer deposit request remove
Ports and Mailboxes Port Mailboxes
Ports and Mailboxes ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Ports and Mailboxes
Procedure-Based Communication ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Procedure-Based Communication ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],In fact, it ` can ’ by doing  marshalling  on parameters.
RPC ,[object Object],[object Object],res = f( params ) // caller // client process ... send(RP, f , params ); receive(RP,res); ... // callee // server process process RP_server {  while (1) {  receive(C, f , params );  res= f( params ) ;  send(C,res);  } }
Rendezvous ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],“ Rendezvous ” is French for “ meeting .” Pronunciation    “ RON-day-voo .”
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
Semantics of a Rendezvous ,[object Object],[object Object]
Semantics of a Rendezvous p q Rendezvous p q Rendezvous q . f () accept  f () S q . f () accept  f () S
Rendezvous: Selective Accept ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Rendezvous: Selective Accept [. . .] : optional ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Example: Bounded Buffer ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Example: Bounded Buffer ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Example: Bounded Buffer BoundedBuffer .deposit(data) BoundedBuffer .remove(data)
Distributed Mutual Exclusion ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Distributed Mutual Exclusion with  Token Ring A practical and elegant compromise version of fully distributed approach.
Distributed Mutual Exclusion with  Token Ring Controler[2]: P[2]: CS2; Program2; Controler[3]: P[3]: CS3; Program3; Controler[1]: P[1]: CS1; Program1; token token RequestCS ReleaseCS RequestCS ReleaseCS RequestCS ReleaseCS
Distributed Mutual Exclusion with  Token Ring Controler[2]: P[2]: CS2; Program2; Controler[3]: P[3]: CS3; Program3; Controler[1]: P[1]: CS1; Program1; process  controller[i]  {  while(1) {  accept Token;  select {  accept  Request_CS () {busy=1;}  else null;  }  if (busy) accept  Release_CS () {busy=0;} controller[(i+1) % n].Token;  }  } process  p[i ] {  while(1) {  controller[i]. Request_CS ();  CSi;  controller[i]. Release_CS ();  programi;  } } ,[object Object],[object Object],[object Object],[object Object],[object Object],token token RequestCS ReleaseCS RequestCS ReleaseCS RequestCS ReleaseCS
Distributed Mutual Exclusion with  Token Ring Controler[2]: P[2]: CS2; Program2; Controler[3]: P[3]: CS3; Program3; Controler[1]: P[1]: CS1; Program1; process  controller[i]  {  while(1) {  accept Token;  select {  accept  Request_CS () {busy=1;}  else null;  }  if (busy) accept  Release_CS () {busy=0;} controller[(i+1) % n].Token;  }  } process  p[i ] {  while(1) {  controller[i]. Request_CS ();  CSi;  controller[i]. Release_CS ();  programi;  } } token token RequestCS ReleaseCS RequestCS ReleaseCS RequestCS ReleaseCS
Operating Systems Principles Process Management and Coordination Lecture 3: Higher-Level Synchronization and Communication Other Classical Problems
Readers/Writers Problem Database
Readers/Writers Problem Database Writers can work only when  no   reader  activated. One Writer can work at a time.
Readers/Writers Problem Database Readers can work only when  no   writer  activated. Allows infinite number of readers.
Readers/Writers Problem ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
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.
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.
Dining Philosophers
Dining Philosophers ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Dining Philosophers p 1 p 2 p 3 p 4 p 5 f 1 f 2 f 3 f 4 f 5 p(i) { while (1) { think(i); grab_forks (i); eat(i); return_forks (i); } } grab_forks (i): P(f[i]); P(f[(i+1)%5]); ,[object Object],Easily lead to  deadlock .
Solutions to deadlock ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Logical Clocks ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
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!
Logical Clocks e i e s e k e r send receive p 1 p 2 e j
Logical Clocks in Action u v x y 4 5 13 15 6 14
The Elevator Algorithm ,[object Object],[object Object],[object Object],[object Object]
The Elevator Algorithm
The Elevator Algorithm ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],request ( i ) request ( i ) direction  =  up driection  =  down
The Elevator Algorithm Using Priority Waits monitor elevator {  int dir=1, up=1, down=-1, pos=1, busy=0; condition  upsweep ,  downsweep ; request (int dest) {  /*Called when button pushed*/   if (busy) {  if ((pos<dest) || ((pos==dest) && (dir==up)))  upsweep .wait(dest);  else  downsweep .wait(-dest);  }  busy = 1;  pos = dest;  } release () {  /*Called when door closes*/ . . . . . . . . . . . . . . . }  } Put the request into the proper  priority queue  and  wait .
The Elevator Algorithm Using Priority Waits monitor elevator {  int dir=1, up=1, down=-1, pos=1, busy=0; condition  upsweep ,  downsweep ; request (int dest) {  /*Called when button pushed*/   if (busy) {  if ((pos<dest) || ((pos==dest) && (dir==up)))  upsweep .wait(dest);  else  downsweep .wait(-dest);  }  busy = 1;  pos = dest;  } release () {  /*Called when door closes*/ . . . . . . . . . . . . . . . }  }
The Elevator Algorithm Using Priority Waits monitor elevator {  int dir=1, up=1, down=-1, pos=1, busy=0; condition upsweep, downsweep; . . . . . . . . . . . . . . . release () {  /*Called when door closes*/ busy = 0;  if (dir==up) {  if (!empty( upsweep ))  upsweep .signal;  else {  dir = down;  downsweep .signal;  } }  else {  /*direction==down*/ if (!empty(downsweep))  downsweep .signal; else { dir = up;  upsweep .signal;  }  }  } }

Mais conteúdo relacionado

Mais procurados

storage class
storage classstorage class
storage class
student
 
The System of Automatic Searching for Vulnerabilities or how to use Taint Ana...
The System of Automatic Searching for Vulnerabilities or how to use Taint Ana...The System of Automatic Searching for Vulnerabilities or how to use Taint Ana...
The System of Automatic Searching for Vulnerabilities or how to use Taint Ana...
Positive Hack Days
 
Looping statements in Java
Looping statements in JavaLooping statements in Java
Looping statements in Java
Jin Castor
 

Mais procurados (20)

Storage class in C Language
Storage class in C LanguageStorage class in C Language
Storage class in C Language
 
Semaphore
SemaphoreSemaphore
Semaphore
 
C++ loop
C++ loop C++ loop
C++ loop
 
Checking Wine with PVS-Studio and Clang Static Analyzer
Checking Wine with PVS-Studio and Clang Static AnalyzerChecking Wine with PVS-Studio and Clang Static Analyzer
Checking Wine with PVS-Studio and Clang Static Analyzer
 
Storage classes
Storage classesStorage classes
Storage classes
 
storage class
storage classstorage class
storage class
 
Looping
LoopingLooping
Looping
 
Loop control in c++
Loop control in c++Loop control in c++
Loop control in c++
 
Storage class
Storage classStorage class
Storage class
 
Storage class in c
Storage class in cStorage class in c
Storage class in c
 
The System of Automatic Searching for Vulnerabilities or how to use Taint Ana...
The System of Automatic Searching for Vulnerabilities or how to use Taint Ana...The System of Automatic Searching for Vulnerabilities or how to use Taint Ana...
The System of Automatic Searching for Vulnerabilities or how to use Taint Ana...
 
C++ control loops
C++ control loopsC++ control loops
C++ control loops
 
Storage classes in C
Storage classes in CStorage classes in C
Storage classes in C
 
Java loops
Java loopsJava loops
Java loops
 
06 Loops
06 Loops06 Loops
06 Loops
 
Storage classes in C
Storage classes in C Storage classes in C
Storage classes in C
 
Storage classes
Storage classesStorage classes
Storage classes
 
Storage Class in C Progrmming
Storage Class in C Progrmming Storage Class in C Progrmming
Storage Class in C Progrmming
 
Looping statements in Java
Looping statements in JavaLooping statements in Java
Looping statements in Java
 
STORAGE CLASSES
STORAGE CLASSESSTORAGE CLASSES
STORAGE CLASSES
 

Destaque

COORDINATION OF ACTUATED SIGNALS FOR A CORRIDOR
COORDINATION OF ACTUATED SIGNALS FOR A CORRIDORCOORDINATION OF ACTUATED SIGNALS FOR A CORRIDOR
COORDINATION OF ACTUATED SIGNALS FOR A CORRIDOR
Rakesh Venkateswaran
 
Machine Learning and Optimization For Traffic and Emergency ...
Machine Learning and Optimization For Traffic and Emergency ...Machine Learning and Optimization For Traffic and Emergency ...
Machine Learning and Optimization For Traffic and Emergency ...
butest
 
Os4 2
Os4 2Os4 2
Os4 2
issbp
 
Class5
 Class5 Class5
Class5
issbp
 
Ch11 input output systems
Ch11 input output systemsCh11 input output systems
Ch11 input output systems
issbp
 
Os5 2
Os5 2Os5 2
Os5 2
issbp
 
Os2 2
Os2 2Os2 2
Os2 2
issbp
 
Os6 2
Os6 2Os6 2
Os6 2
issbp
 
Class9
 Class9 Class9
Class9
issbp
 

Destaque (20)

adaptive signal control technology along sw 8th street pilot project
adaptive signal control technology along sw 8th street pilot projectadaptive signal control technology along sw 8th street pilot project
adaptive signal control technology along sw 8th street pilot project
 
COORDINATION OF ACTUATED SIGNALS FOR A CORRIDOR
COORDINATION OF ACTUATED SIGNALS FOR A CORRIDORCOORDINATION OF ACTUATED SIGNALS FOR A CORRIDOR
COORDINATION OF ACTUATED SIGNALS FOR A CORRIDOR
 
Machine Learning and Optimization For Traffic and Emergency ...
Machine Learning and Optimization For Traffic and Emergency ...Machine Learning and Optimization For Traffic and Emergency ...
Machine Learning and Optimization For Traffic and Emergency ...
 
Brandon Signals Council Presentation
Brandon Signals Council PresentationBrandon Signals Council Presentation
Brandon Signals Council Presentation
 
Urban Traffic Estimation & Optimization: An Overview
Urban Traffic Estimation & Optimization: An OverviewUrban Traffic Estimation & Optimization: An Overview
Urban Traffic Estimation & Optimization: An Overview
 
PTV Vissig Optimisation Share
PTV Vissig Optimisation SharePTV Vissig Optimisation Share
PTV Vissig Optimisation Share
 
Future of intelligent transportation CIO Roundtable 080214
Future of intelligent transportation   CIO Roundtable 080214Future of intelligent transportation   CIO Roundtable 080214
Future of intelligent transportation CIO Roundtable 080214
 
Os4 2
Os4 2Os4 2
Os4 2
 
Os6
Os6Os6
Os6
 
Class5
 Class5 Class5
Class5
 
Os4
Os4Os4
Os4
 
Ch11 input output systems
Ch11 input output systemsCh11 input output systems
Ch11 input output systems
 
Os5 2
Os5 2Os5 2
Os5 2
 
Os2 2
Os2 2Os2 2
Os2 2
 
Lecture 7: Definite Clause Grammars
Lecture 7: Definite Clause GrammarsLecture 7: Definite Clause Grammars
Lecture 7: Definite Clause Grammars
 
Os6 2
Os6 2Os6 2
Os6 2
 
Os2
Os2Os2
Os2
 
Class9
 Class9 Class9
Class9
 
Design1
Design1Design1
Design1
 
Adaptive Traffic Control Systems Overview
Adaptive Traffic Control Systems OverviewAdaptive Traffic Control Systems Overview
Adaptive Traffic Control Systems Overview
 

Semelhante a Os3 2

Chapter 6 - Process Synchronization
Chapter 6 - Process SynchronizationChapter 6 - Process Synchronization
Chapter 6 - Process Synchronization
Wayne Jones Jnr
 
Java 5 concurrency
Java 5 concurrencyJava 5 concurrency
Java 5 concurrency
priyank09
 
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docxIn Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
bradburgess22840
 
Objectives In this lab you will review passing arrays to methods and.pdf
Objectives In this lab you will review passing arrays to methods and.pdfObjectives In this lab you will review passing arrays to methods and.pdf
Objectives In this lab you will review passing arrays to methods and.pdf
f3apparelsonline
 

Semelhante a Os3 2 (20)

Chapter 6 - Process Synchronization
Chapter 6 - Process SynchronizationChapter 6 - Process Synchronization
Chapter 6 - Process Synchronization
 
Algo>Queues
Algo>QueuesAlgo>Queues
Algo>Queues
 
Thread
ThreadThread
Thread
 
Java 5 concurrency
Java 5 concurrencyJava 5 concurrency
Java 5 concurrency
 
Java tut1
Java tut1Java tut1
Java tut1
 
Tutorial java
Tutorial javaTutorial java
Tutorial java
 
Java Tut1
Java Tut1Java Tut1
Java Tut1
 
Java Tutorial
Java TutorialJava Tutorial
Java Tutorial
 
Mca ii os u-2 process management & communication
Mca  ii  os u-2 process management & communicationMca  ii  os u-2 process management & communication
Mca ii os u-2 process management & communication
 
Fpga 12-event-control
Fpga 12-event-controlFpga 12-event-control
Fpga 12-event-control
 
Data Structures - Lecture 6 [queues]
Data Structures - Lecture 6 [queues]Data Structures - Lecture 6 [queues]
Data Structures - Lecture 6 [queues]
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docxIn Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
 
Os3
Os3Os3
Os3
 
Asterisk: PVS-Studio Takes Up Telephony
Asterisk: PVS-Studio Takes Up TelephonyAsterisk: PVS-Studio Takes Up Telephony
Asterisk: PVS-Studio Takes Up Telephony
 
Objectives In this lab you will review passing arrays to methods and.pdf
Objectives In this lab you will review passing arrays to methods and.pdfObjectives In this lab you will review passing arrays to methods and.pdf
Objectives In this lab you will review passing arrays to methods and.pdf
 
Ext J S Observable
Ext J S ObservableExt J S Observable
Ext J S Observable
 
OS Process Synchronization, semaphore and Monitors
OS Process Synchronization, semaphore and MonitorsOS Process Synchronization, semaphore and Monitors
OS Process Synchronization, semaphore and Monitors
 
Exception handling
Exception handlingException handling
Exception handling
 

Mais de issbp

Os10 2
Os10 2Os10 2
Os10 2
issbp
 
Os10
Os10Os10
Os10
issbp
 
Os9 2
Os9 2Os9 2
Os9 2
issbp
 
Os8 2
Os8 2Os8 2
Os8 2
issbp
 
Os7 2
Os7 2Os7 2
Os7 2
issbp
 
Class8
 Class8 Class8
Class8
issbp
 
Class7
 Class7 Class7
Class7
issbp
 
Class6
 Class6 Class6
Class6
issbp
 
Class4
 Class4 Class4
Class4
issbp
 
Class3
 Class3 Class3
Class3
issbp
 
Class2
 Class2 Class2
Class2
issbp
 
Class1
 Class1 Class1
Class1
issbp
 
0227 regularlanguages
 0227 regularlanguages 0227 regularlanguages
0227 regularlanguages
issbp
 

Mais de issbp (17)

Os10 2
Os10 2Os10 2
Os10 2
 
Os10
Os10Os10
Os10
 
Os9 2
Os9 2Os9 2
Os9 2
 
Os9
Os9Os9
Os9
 
Os8 2
Os8 2Os8 2
Os8 2
 
Os8
Os8Os8
Os8
 
Os7 2
Os7 2Os7 2
Os7 2
 
Os7
Os7Os7
Os7
 
Os5
Os5Os5
Os5
 
Class8
 Class8 Class8
Class8
 
Class7
 Class7 Class7
Class7
 
Class6
 Class6 Class6
Class6
 
Class4
 Class4 Class4
Class4
 
Class3
 Class3 Class3
Class3
 
Class2
 Class2 Class2
Class2
 
Class1
 Class1 Class1
Class1
 
0227 regularlanguages
 0227 regularlanguages 0227 regularlanguages
0227 regularlanguages
 

Os3 2

  • 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
  • 9. Example: Queue Handler Queue AddToQueue RemoveFromQueue
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22. Example: Bounded Buffer Deposit Remove . . . . . . . . . 0 1 2 n  1 n  2
  • 23. Example: Bounded Buffer Deposit Remove . . . . . . . . . 0 1 2 n  1 n  2 . . . nextin nextout count
  • 24. Example: Bounded Buffer Deposit Remove count nextout nextin
  • 25.
  • 26.
  • 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
  • 34.
  • 35. Mesa and Java Monitors P1: . . . . . . . . if(!B1) c1.wait; . . . . . . . . P2: . . . . . . . . if(!B2) c2.wait; . . . . . . . . B1=FASLE; . . . . . . . .
  • 36. Mesa and Java Monitors P1: . . . . . . . . if(!B1) c1.wait; . . . . . . . . P2: . . . . . . . . if(!B2) c2.wait; . . . . . . . . B1=FASLE; return; P3: . . . . . . . . B1=B2=TRUE; c1.notify; c2.notify; . . . . . . . . return; B1=FALSE
  • 37. Mesa and Java Monitors P1: . . . . . . . . if(!B1) c1.wait; . . . . . . . . P2: . . . . . . . . if(!B2) c2.wait; . . . . . . . . B1=FASLE; return; P3: . . . . . . . . B1=B2=TRUE; c1.notify; c2.notify; . . . . . . . . return; B1=FALSE What action should P1 take? Continue or wait again?
  • 38. Mesa and Java Monitors P1: . . . . . . . . if(!B1) c1.wait; . . . . . . . . P2: . . . . . . . . if(!B2) c2.wait; . . . . . . . . B1=FASLE; return; P3: . . . . . . . . B1=B2=TRUE; c1.notify; c2.notify; . . . . . . . . return; B1=FALSE What action should P1 take? Continue or wait again? 
  • 39. Solution P1: . . . . . . . . if(!B1) c1.wait; . . . . . . . . P2: . . . . . . . . if(!B2) c2.wait; . . . . . . . . B1=FASLE; return; P3: . . . . . . . . B1=B2=TRUE; c1.notify; c2.notify; . . . . . . . . return; B1=FALSE What action should P1 take? Continue or wait again?    Replace if to while . while while
  • 40. Example: Queue Handler Queue AddToQueue RemoveFromQueue
  • 41.
  • 42.
  • 43.
  • 44.
  • 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
  • 54.
  • 55. Named Message Channels Named Pipe (Win32) P1: . . . . . . . . send(ch1, msg1); . . . . . . . . P2: . . . . . . . . send(ch2, msg2); . . . . . . . . P3: . . . . . . . . receive(ch1, x); . . . . . . . . receive(ch2, y); . . . . . . . . ch1 ch2
  • 56.
  • 57.
  • 58. Bounded buffer with CSP Buffer Producer Consumer deposit request remove
  • 59. Bounded buffer with CSP Buffer Producer Consumer deposit request remove send(deposit, data) send(request) receive(remove,data) receive(request) send(remove,data) receive(deposit, data)
  • 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.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65. Ports and Mailboxes Port Mailboxes
  • 66.
  • 68.
  • 69.
  • 70.
  • 71.
  • 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
  • 75.
  • 76.
  • 77.
  • 78.
  • 79. Example: Bounded Buffer BoundedBuffer .deposit(data) BoundedBuffer .remove(data)
  • 80.
  • 81. Distributed Mutual Exclusion with Token Ring A practical and elegant compromise version of fully distributed approach.
  • 82. Distributed Mutual Exclusion with Token Ring Controler[2]: P[2]: CS2; Program2; Controler[3]: P[3]: CS3; Program3; Controler[1]: P[1]: CS1; Program1; token token RequestCS ReleaseCS RequestCS ReleaseCS RequestCS ReleaseCS
  • 83.
  • 84. Distributed Mutual Exclusion with Token Ring Controler[2]: P[2]: CS2; Program2; Controler[3]: P[3]: CS3; Program3; Controler[1]: P[1]: CS1; Program1; process controller[i] { while(1) { accept Token; select { accept Request_CS () {busy=1;} else null; } if (busy) accept Release_CS () {busy=0;} controller[(i+1) % n].Token; } } process p[i ] { while(1) { controller[i]. Request_CS (); CSi; controller[i]. Release_CS (); programi; } } token token RequestCS ReleaseCS RequestCS ReleaseCS RequestCS ReleaseCS
  • 85. Operating Systems Principles Process Management and Coordination Lecture 3: Higher-Level Synchronization and Communication Other Classical Problems
  • 87. Readers/Writers Problem Database Writers can work only when no reader activated. One Writer can work at a time.
  • 88. Readers/Writers Problem Database Readers can work only when no writer activated. Allows infinite number of readers.
  • 89.
  • 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.
  • 93.
  • 94.
  • 95.
  • 96.
  • 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!
  • 98. Logical Clocks e i e s e k e r send receive p 1 p 2 e j
  • 99. Logical Clocks in Action u v x y 4 5 13 15 6 14
  • 100.
  • 102.
  • 103. The Elevator Algorithm Using Priority Waits monitor elevator { int dir=1, up=1, down=-1, pos=1, busy=0; condition upsweep , downsweep ; request (int dest) { /*Called when button pushed*/ if (busy) { if ((pos<dest) || ((pos==dest) && (dir==up))) upsweep .wait(dest); else downsweep .wait(-dest); } busy = 1; pos = dest; } release () { /*Called when door closes*/ . . . . . . . . . . . . . . . } } Put the request into the proper priority queue and wait .
  • 104. The Elevator Algorithm Using Priority Waits monitor elevator { int dir=1, up=1, down=-1, pos=1, busy=0; condition upsweep , downsweep ; request (int dest) { /*Called when button pushed*/ if (busy) { if ((pos<dest) || ((pos==dest) && (dir==up))) upsweep .wait(dest); else downsweep .wait(-dest); } busy = 1; pos = dest; } release () { /*Called when door closes*/ . . . . . . . . . . . . . . . } }
  • 105. The Elevator Algorithm Using Priority Waits monitor elevator { int dir=1, up=1, down=-1, pos=1, busy=0; condition upsweep, downsweep; . . . . . . . . . . . . . . . release () { /*Called when door closes*/ busy = 0; if (dir==up) { if (!empty( upsweep )) upsweep .signal; else { dir = down; downsweep .signal; } } else { /*direction==down*/ if (!empty(downsweep)) downsweep .signal; else { dir = up; upsweep .signal; } } } }