Operating Systems Principles Process Management and Coordination Lecture 2: Processes and Their Interaction 主講人:虞台文
Content <ul><li>The Process Notion  </li></ul><ul><li>Defining and Instantiating Processes </li></ul><ul><ul><li>Precedenc...
The Process Notion Operating Systems Principles Process Management and Coordination Lecture 2: Processes and Their Interac...
What is a process? <ul><li>A process is  a program in execution .  </li></ul><ul><ul><li>Also, called a  task . </li></ul>...
Virtualization <ul><li>Conceptually,  </li></ul><ul><ul><li>each process has its  own  CPU and main memory; </li></ul></ul...
Physical/Logical Concurrencies <ul><li>An OS must handle a high degree of  parallelism . </li></ul><ul><li>Physical  concu...
Interaction among Processes <ul><li>The  OS  and  users applications  are viewed as  a collection of processes , all  runn...
Why Use Process Structure? <ul><li>Hardware-independent solutions </li></ul><ul><ul><li>Processes cooperate and compete co...
Defining and Instantiating Processes Operating Systems Principles Process Management and Coordination Lecture 2: Processes...
A Process Flow Graph User session at a workstation
Serial and Parallel Processes Serial Parallel S/P Notation: execution of process  p 1  through  p n . serial parallel
Properly Nested Process Flow Graphs S/P Notation: execution of process  p 1  through  p n . A process flow graph is  prope...
Properly Nested Process Flow Graphs Properly Nested improperly Nested
Example: Evaluation of Arithmetic Expressions Expression tree Process flow graph
Implicit Process Creation  <ul><li>Processes are created dynamically using  language constructs   </li></ul><ul><ul><li>no...
Implicit Process Creation cobegin   C1   //   C2   //   C3   //   Cn   coend C1 ;   C2 ;   C3 ;   C4 ;
Example: Use of  cobegin/coend User session  at a workstation Initialize; cobegin Time_Date //  Mail  // Edit; cobegin Com...
Data Parallelism <ul><li>Same code is applied to different data </li></ul><ul><li>The  forall  statement </li></ul><ul><ul...
Example: Matrix Multiplication <ul><li>Each inner product is computed sequentially </li></ul><ul><li>All inner products ar...
Explicit Process Creation <ul><li>cobegin/coend </li></ul><ul><ul><li>limited to  properly nested  graphs </li></ul></ul><...
The  fork/join/quit  primitives <ul><li>Syntax:  fork   x </li></ul><ul><li>Meaning:  </li></ul><ul><ul><li>create new pro...
Example t1 t2 Synchronization needed here. Use down-counter  t1=2  and  t2=3  for synchronization.
Example The starting point of process  p i  has label  L i . t1 t2 <ul><ul><li>t1 = 2; t2 = 3;  </li></ul></ul><ul><ul><li...
The Unix  fork procid =  fork (); if (procid==0) do_ child _processing else  do_ parent _processing
The Unix  fork procid =  fork (); if (procid==0) do_ child _processing else  do_ parent _processing <ul><li>Replicates cal...
Explicit Process Declarations <ul><li>Designate piece of code as a unit of execution  </li></ul><ul><ul><li>Facilitates pr...
Explicit Process Declarations Syntax: process  p { declarations_for_p ; executable_code_for_p ; };  process  type  p { dec...
Example: Explicit Process Declarations <ul><li>process  p{ </li></ul><ul><li>process  p1{   </li></ul><ul><li>declarations...
Example: Explicit Process Declarations <ul><li>process  p{ </li></ul><ul><li>process  p1{   </li></ul><ul><li>declarations...
Basic Process Interactions Operating Systems Principles Process Management and Coordination Lecture 2: Processes and Their...
Competition and Cooperation <ul><li>Competition </li></ul><ul><ul><li>Processes compete for resources </li></ul></ul><ul><...
Resource Competition Shared  source, e.g., common data
Resource Competition <ul><li>When several process may  asynchronously access  a  common data area , it is necessary to  pr...
Race Conditions <ul><li>When two or more  processes / threads  are executing  concurrently , the result can depend on the ...
Example x = 0; cobegin p1:  ...  x = x + 1; ... // p2:  ... x = x + 1; ... coend What value of  x  should be after both pr...
Example p1: . . . R1 = x; R1 = R1 + 1;  x = R1; . . .  p2: . . . R2 = x; R2 = R2 + 1;  x = R2; . . .  R1 = 0 R1 = 1 x = 1 ...
Example p1: . . . R1 = x; R1 = R1 + 1;  x = R1; . . .  p2: . . . R2 = x; R2 = R2 + 1;  x = R2; . . .  R1 = 0 R1 = 1 x = 1 ...
The Critical Section (CS) <ul><li>Any  section of code  involved in  reading  and  writing  a  share data area  is called ...
<ul><li>Guarantee  mutual exclusion :  At any time, at most one process executing within its  CSi . </li></ul>The Critical...
The Critical Problem <ul><li>Guarantee  mutual exclusion :  At any time, at most one process executing within its  CSi . <...
Software Solutions <ul><li>Solve the problem without taking advantage of special machine instructions and other hardware. ...
Algorithm 1 <ul><ul><li>int  turn  = 1; </li></ul></ul><ul><ul><li>cobegin   </li></ul></ul><ul><ul><li>p1:  while (1) {  ...
Algorithm 1 <ul><li>Mutual Exclusion </li></ul><ul><li>No mutual blocking </li></ul><ul><ul><li>No dog in manger </li></ul...
Algorithm 2 <ul><ul><li>int c1 = 0, c2 = 0; </li></ul></ul><ul><ul><li>cobegin   </li></ul></ul><ul><ul><li>p1:  while (1)...
Algorithm 2 <ul><li>Mutual Exclusion </li></ul><ul><li>No mutual blocking </li></ul><ul><ul><li>No dog in manger </li></ul...
Algorithm 3 <ul><ul><li>int c1 = 0, c2 = 0; </li></ul></ul><ul><ul><li>cobegin   </li></ul></ul><ul><ul><li>p1:  while (1)...
Algorithm 3 <ul><li>Mutual Exclusion </li></ul><ul><li>No mutual blocking </li></ul><ul><ul><li>No dog in manger </li></ul...
Algorithm 3 <ul><ul><li>p1:  while (1) {  </li></ul></ul><ul><ul><li>c1 = 1; </li></ul></ul><ul><ul><li>if (c2) c1=0; </li...
Algorithm 3 <ul><ul><li>p1:  while (1) {  </li></ul></ul><ul><ul><li>c1 = 1; </li></ul></ul><ul><ul><li>if (c2) c1=0; </li...
Algorithm 4 (Peterson) <ul><ul><li>int c1 = 0, c2 = 0, WillWait; </li></ul></ul><ul><ul><li>cobegin   </li></ul></ul><ul><...
Algorithm 4 (Peterson) <ul><li>Mutual Exclusion </li></ul><ul><li>No mutual blocking  </li></ul><ul><ul><li>No dog in mang...
Cooperation: Producer/Consumer Buffer Producer Consumer Producer must  not   overwrite  any data before the Consumer can r...
Client/Server Architecture Processes communicate by  message passing .
Competition and Cooperation <ul><li>Problems with software solutions: </li></ul><ul><ul><li>Difficult  to  program  and to...
Process States running ready running ready ready_queue time-out dispatch
Process States running ready ready blocked running ready_queue time-out dispatch wait(si) wait-queues s1 sn . . . . . . . ...
Process States running ready ready blocked ready_queue time-out dispatch wait(si) wait-queues s1 sn . . . . . . . . .
Process States running ready ready blocked A running process calls  signal(si) . running ready_queue time-out dispatch wai...
P/V Operators running ready ready blocked running P  operation V  operation ready_queue time-out dispatch wait(si) wait-qu...
Dijkstra’s Semaphores <ul><li>Dijkstra’s (1968) introduced two new operations, called  P  and  V , that considerably  simp...
Semaphores <ul><li>A semaphore  s  is an  non-negative  integer, a down counter,  with two operations  P  and  V . </li></...
Semaphores There is a  wait-queue  associated with each semaphore. P(s) CS1 V(s) Program1 P(s) CS2 V(s) Program2 P(s) CS3 ...
Semaphores There is a  wait-queue  associated with each semaphore. P(s) CS1 V(s) Program1 P(s) CS2 V(s) Program2 P(s) CS3 ...
Semaphores There is a  wait-queue  associated with each semaphore. P(s) CS1 V(s) Program1 P(s) CS2 V(s) Program2 P(s) CS3 ...
Semaphores There is a  wait-queue  associated with each semaphore. P(s) CS1 V(s) Program1 P(s) CS2 V(s) Program2 P(s) CS3 ...
Semaphores There is a  wait-queue  associated with each semaphore. P(s) CS1 V(s) Program1 P(s) CS2 V(s) Program2 P(s) CS3 ...
Semaphores There is a  wait-queue  associated with each semaphore. P(s) CS1 V(s) Program1 P(s) CS2 V(s) Program2 P(s) CS3 ...
Semaphores There is a  wait-queue  associated with each semaphore. P(s) CS1 V(s) Program1 P(s) CS2 V(s) Program2 P(s) CS3 ...
Semaphores There is a  wait-queue  associated with each semaphore. P(s) CS1 V(s) Program1 P(s) CS2 V(s) Program2 P(s) CS3 ...
Semaphores There is a  wait-queue  associated with each semaphore. P(s) CS1 V(s) Program1 P(s) CS2 V(s) Program2 P(s) CS3 ...
Semaphores There is a  wait-queue  associated with each semaphore. P(s) CS1 V(s) Program1 P(s) CS2 V(s) Program2 P(s) CS3 ...
Semaphores There is a  wait-queue  associated with each semaphore. P(s) CS1 V(s) Program1 P(s) CS2 V(s) Program2 P(s) CS3 ...
Semaphores <ul><li>A semaphore  s  is an  non-negative  integer with two operations  P  and  V . </li></ul><ul><li>P  and ...
Mutual Exclusion with Semaphores semaphore  mutex  = 1;  /* binary semaphore */  cobegin   p1: while (1) {  P ( mutex ); C...
Producer/Consumer with Semaphores // counting semaphore semaphore  s  = 0;  /* zero item produced */   cobegin   p1: ... P...
Producer/Consumer with Semaphores // counting semaphore semaphore  s  = 0;  /* zero item produced */   cobegin   p1: ... P...
The Bounded Buffer Problem semaphore  e  = n,  f  = 0,  b  = 1;  cobegin  Producer:  while (1) {  Produce_next_record;  P ...
Event Synchronization Operating Systems Principles Process Management and Coordination Lecture 2: Process and Their Intera...
Events <ul><li>An event denotes some  change  in the  state  of the system that is of interest to a process. </li></ul><ul...
Two Type of Events <ul><li>Synchronous event (e.g. I/O completion) </li></ul><ul><ul><li>Process  waits  for it explicitly...
Case study: Windows 2000 <ul><li>WaitForSingleObject </li></ul><ul><li>WaitForMultipleObjects </li></ul>Process blocks unt...
Upcoming SlideShare
Loading in …5
×

Os2 2

837 views

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
837
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
16
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Os2 2

  1. 1. Operating Systems Principles Process Management and Coordination Lecture 2: Processes and Their Interaction 主講人:虞台文
  2. 2. Content <ul><li>The Process Notion </li></ul><ul><li>Defining and Instantiating Processes </li></ul><ul><ul><li>Precedence Relations </li></ul></ul><ul><ul><li>Implicit Process Creation </li></ul></ul><ul><ul><li>Dynamic Creation With fork And join </li></ul></ul><ul><ul><li>Explicit Process Declarations </li></ul></ul><ul><li>Basic Process Interactions </li></ul><ul><ul><li>Competition: The Critical Problem </li></ul></ul><ul><ul><li>Cooperation </li></ul></ul><ul><li>Semaphores </li></ul><ul><ul><li>Semaphore Operations and Data </li></ul></ul><ul><ul><li>Mutual Exclusion </li></ul></ul><ul><ul><li>Producer/Consumer Situations </li></ul></ul><ul><li>Event Synchronization </li></ul>
  3. 3. The Process Notion Operating Systems Principles Process Management and Coordination Lecture 2: Processes and Their Interaction
  4. 4. What is a process? <ul><li>A process is a program in execution . </li></ul><ul><ul><li>Also, called a task . </li></ul></ul><ul><li>It includes </li></ul><ul><ul><li>Program itself (i.e., code or text) </li></ul></ul><ul><ul><li>Data </li></ul></ul><ul><ul><li>a thread of execution (possibly several threads) </li></ul></ul><ul><ul><li>Resources (such as files) </li></ul></ul><ul><ul><li>Execution info (process relation information kept by OS) </li></ul></ul><ul><li>Multiple processes are simultaneously existent in a system. </li></ul>
  5. 5. Virtualization <ul><li>Conceptually, </li></ul><ul><ul><li>each process has its own CPU and main memory; </li></ul></ul><ul><ul><li>processes are running concurrently. </li></ul></ul><ul><li>Many computers are equipped with a single CPU . </li></ul><ul><li>To achieve concurrency, the followings are needed </li></ul><ul><ul><li>CPU sharing  to virtualize the CPU </li></ul></ul><ul><ul><li>Virtual memory  to virtualize the memory </li></ul></ul><ul><li>Usually done by the kernel of OS </li></ul><ul><ul><li>Each process may be viewed in isolation . </li></ul></ul><ul><ul><li>The kernel provides few simple primitives for process interaction. </li></ul></ul>
  6. 6. Physical/Logical Concurrencies <ul><li>An OS must handle a high degree of parallelism . </li></ul><ul><li>Physical concurrency </li></ul><ul><ul><li>Multiple CPUs or Processors required </li></ul></ul><ul><li>Logical concurrency </li></ul><ul><ul><li>Time-share CPU </li></ul></ul>
  7. 7. Interaction among Processes <ul><li>The OS and users applications are viewed as a collection of processes , all running concurrently . </li></ul><ul><li>These processes </li></ul><ul><ul><li>almost operate independently of one another; </li></ul></ul><ul><ul><li>cooperate by sharing memory or by sending messages and synchronization signals to each other; and </li></ul></ul><ul><ul><li>compete for resources . </li></ul></ul>
  8. 8. Why Use Process Structure? <ul><li>Hardware-independent solutions </li></ul><ul><ul><li>Processes cooperate and compete correctly, regardless of the number of CPUs </li></ul></ul><ul><li>Structuring mechanism </li></ul><ul><ul><li>Tasks are isolated with well-defined interfaces </li></ul></ul>
  9. 9. Defining and Instantiating Processes Operating Systems Principles Process Management and Coordination Lecture 2: Processes and Their Interaction
  10. 10. A Process Flow Graph User session at a workstation
  11. 11. Serial and Parallel Processes Serial Parallel S/P Notation: execution of process p 1 through p n . serial parallel
  12. 12. Properly Nested Process Flow Graphs S/P Notation: execution of process p 1 through p n . A process flow graph is properly nested if it can be described by the functions S and P , and only function composition . Properly Nested serial parallel
  13. 13. Properly Nested Process Flow Graphs Properly Nested improperly Nested
  14. 14. Example: Evaluation of Arithmetic Expressions Expression tree Process flow graph
  15. 15. Implicit Process Creation <ul><li>Processes are created dynamically using language constructs </li></ul><ul><ul><li>no process declaration. </li></ul></ul><ul><li>cobegin/coend </li></ul><ul><ul><li>syntax: </li></ul></ul><ul><ul><li>cobegin C1 // C2 // … // Cn coend </li></ul></ul><ul><ul><li>meaning: </li></ul></ul><ul><ul><ul><li>All C i may proceed concurrently </li></ul></ul></ul><ul><ul><ul><li>When all terminate, the statement following cobegin/coend continues. </li></ul></ul></ul>
  16. 16. Implicit Process Creation cobegin C1 // C2 // C3 // Cn coend C1 ; C2 ; C3 ; C4 ;
  17. 17. Example: Use of cobegin/coend User session at a workstation Initialize; cobegin Time_Date // Mail // Edit; cobegin Complile; Load; Execute // Edit; cobegin Print // Web coend coend coend ; Terminate
  18. 18. Data Parallelism <ul><li>Same code is applied to different data </li></ul><ul><li>The forall statement </li></ul><ul><ul><li>syntax: </li></ul></ul><ul><ul><li>forall ( parameters ) statements </li></ul></ul><ul><ul><li>Meaning: </li></ul></ul><ul><ul><ul><li>Parameters specify set of data items </li></ul></ul></ul><ul><ul><ul><li>Statements are executed for each item concurrently </li></ul></ul></ul>
  19. 19. Example: Matrix Multiplication <ul><li>Each inner product is computed sequentially </li></ul><ul><li>All inner products are computed in parallel </li></ul><ul><ul><li>forall ( i:1..n, j:1..m ){ </li></ul></ul><ul><ul><li>A[i][j] = 0; </li></ul></ul><ul><ul><li>for ( k=1; k<=r; ++k ) </li></ul></ul><ul><ul><li>A[i][j] = A[i][j] + B[i][k]*C[k][j]; </li></ul></ul><ul><ul><li>} </li></ul></ul>
  20. 20. Explicit Process Creation <ul><li>cobegin/coend </li></ul><ul><ul><li>limited to properly nested graphs </li></ul></ul><ul><li>forall </li></ul><ul><ul><li>limited to data parallelism </li></ul></ul><ul><li>fork/join </li></ul><ul><ul><li>can express arbitrary functional parallelism </li></ul></ul>implicit process creation explicit process creation
  21. 21. The fork/join/quit primitives <ul><li>Syntax: fork x </li></ul><ul><li>Meaning: </li></ul><ul><ul><li>create new process that begins executing at label x </li></ul></ul><ul><li>Syntax: join t,y </li></ul><ul><li>Meaning: t = t –1; </li></ul><ul><li>if ( t ==0) goto y ; </li></ul><ul><ul><li>The operation must be indivisible . (Why?) </li></ul></ul><ul><li>Syntax: quit </li></ul><ul><li>Meaning: </li></ul><ul><ul><li>Process termination </li></ul></ul>
  22. 22. Example t1 t2 Synchronization needed here. Use down-counter t1=2 and t2=3 for synchronization.
  23. 23. Example The starting point of process p i has label L i . t1 t2 <ul><ul><li>t1 = 2; t2 = 3; </li></ul></ul><ul><ul><li>L1: p1 ; fork L2; fork L5; fork L7; quit ; </li></ul></ul><ul><ul><li>L2: p2 ; fork L3; fork L4; quit ; </li></ul></ul><ul><ul><li>L5: p5 ; join t1,L6; quit ; </li></ul></ul><ul><ul><li>L7: p7 ; join t2,L8; quit ; </li></ul></ul><ul><ul><li>L4: p4 ; join t1,L6; quit ; </li></ul></ul><ul><ul><li>L3: p3 ; join t2,L8; quit ; </li></ul></ul><ul><ul><li>L6: p6 ; join t2,L8; quit ; </li></ul></ul><ul><ul><li>L8: p8 ; quit; </li></ul></ul>
  24. 24. The Unix fork procid = fork (); if (procid==0) do_ child _processing else do_ parent _processing
  25. 25. The Unix fork procid = fork (); if (procid==0) do_ child _processing else do_ parent _processing <ul><li>Replicates calling process. </li></ul><ul><li>Parent and child are identical </li></ul><ul><li>except for the value of procid . </li></ul>Use procid to diverge parent and child.
  26. 26. Explicit Process Declarations <ul><li>Designate piece of code as a unit of execution </li></ul><ul><ul><li>Facilitates program structuring </li></ul></ul><ul><li>Instantiate: </li></ul><ul><ul><li>Statically (like cobegin ) or </li></ul></ul><ul><ul><li>Dynamically (like fork ) </li></ul></ul>
  27. 27. Explicit Process Declarations Syntax: process p { declarations_for_p ; executable_code_for_p ; }; process type p { declarations_for_p ; executable_code_for_p ; };
  28. 28. Example: Explicit Process Declarations <ul><li>process p{ </li></ul><ul><li>process p1{ </li></ul><ul><li>declarations_for_p1 ; </li></ul><ul><li>executable_code_for_p1; </li></ul><ul><li>} </li></ul><ul><li>process type p2{ </li></ul><ul><li>declarations_for_p2; </li></ul><ul><li>executable_code_for_p2; </li></ul><ul><li>} </li></ul><ul><li>other_declaration_for_p ; </li></ul><ul><li>... </li></ul><ul><li>q = new p2; </li></ul><ul><li>... </li></ul><ul><li>} </li></ul>declarations_for_p; executable_code_for_p;
  29. 29. Example: Explicit Process Declarations <ul><li>process p{ </li></ul><ul><li>process p1{ </li></ul><ul><li>declarations_for_p1 ; </li></ul><ul><li>executable_code_for_p1; </li></ul><ul><li>} </li></ul><ul><li>process type p2{ </li></ul><ul><li>declarations_for_p2; </li></ul><ul><li>executable_code_for_p2; </li></ul><ul><li>} </li></ul><ul><li>other_declaration_for_p ; </li></ul><ul><li>... </li></ul><ul><li>q = new p2; </li></ul><ul><li>... </li></ul><ul><li>} </li></ul>similar to cobegin/coend similar to fork
  30. 30. Basic Process Interactions Operating Systems Principles Process Management and Coordination Lecture 2: Processes and Their Interaction
  31. 31. Competition and Cooperation <ul><li>Competition </li></ul><ul><ul><li>Processes compete for resources </li></ul></ul><ul><ul><li>Each process could exist without the other </li></ul></ul><ul><li>Cooperation </li></ul><ul><ul><li>Each process aware of the other </li></ul></ul><ul><ul><li>Processes Synchronization </li></ul></ul><ul><ul><li>Exchange information with one another </li></ul></ul><ul><ul><ul><li>Share memory </li></ul></ul></ul><ul><ul><ul><li>Message passing </li></ul></ul></ul>
  32. 32. Resource Competition Shared source, e.g., common data
  33. 33. Resource Competition <ul><li>When several process may asynchronously access a common data area , it is necessary to protect the data from simultaneous change by two or more processes. </li></ul><ul><li>If not , the updated area may be left in an inconsistent state. </li></ul>
  34. 34. Race Conditions <ul><li>When two or more processes / threads are executing concurrently , the result can depend on the precise interleaving of the two instruction streams. </li></ul><ul><li>Race conditions may cause: </li></ul><ul><ul><li>undesired computation results . </li></ul></ul><ul><ul><li>bugs which are hard to reproduce ! </li></ul></ul>
  35. 35. Example x = 0; cobegin p1: ... x = x + 1; ... // p2: ... x = x + 1; ... coend What value of x should be after both processes execute?
  36. 36. Example p1: . . . R1 = x; R1 = R1 + 1; x = R1; . . . p2: . . . R2 = x; R2 = R2 + 1; x = R2; . . . R1 = 0 R1 = 1 x = 1 R2 = 1 R2 = 2 x = 2
  37. 37. Example p1: . . . R1 = x; R1 = R1 + 1; x = R1; . . . p2: . . . R2 = x; R2 = R2 + 1; x = R2; . . . R1 = 0 R1 = 1 x = 1 x = 1 R2 = 0 R2 = 1
  38. 38. The Critical Section (CS) <ul><li>Any section of code involved in reading and writing a share data area is called a critical section. </li></ul><ul><li>Mutual Exclusion  At most one process is allowed to enter critical section. </li></ul>
  39. 39. <ul><li>Guarantee mutual exclusion : At any time, at most one process executing within its CSi . </li></ul>The Critical Problem <ul><ul><ul><li>cobegin </li></ul></ul></ul><ul><ul><ul><li>p1: while(1) { CS1 ; program1;} </li></ul></ul></ul><ul><ul><ul><li>// </li></ul></ul></ul><ul><ul><ul><li>p2: while(1) { CS2 ; program2;} </li></ul></ul></ul><ul><ul><ul><li>// </li></ul></ul></ul><ul><ul><ul><li>... </li></ul></ul></ul><ul><ul><ul><li>// </li></ul></ul></ul><ul><ul><ul><li>pn: while(1) { CSn ; programn;} </li></ul></ul></ul><ul><ul><ul><li>coend </li></ul></ul></ul>
  40. 40. The Critical Problem <ul><li>Guarantee mutual exclusion : At any time, at most one process executing within its CSi . </li></ul><ul><li>In addition, we need to prevent mutual blocking : </li></ul><ul><ul><li>Process outside of its CS must not prevent other processes from entering its CS. (No “ dog in manger ” ) </li></ul></ul><ul><ul><li>Process must not be able to repeatedly reenter its CS and starve other processes ( fairness ) </li></ul></ul><ul><ul><li>Processes must not block each other forever ( deadlock ) </li></ul></ul><ul><ul><li>Processes must not repeatedly yield to each other (“after you”--“after you” livelock ) </li></ul></ul>
  41. 41. Software Solutions <ul><li>Solve the problem without taking advantage of special machine instructions and other hardware. </li></ul>
  42. 42. Algorithm 1 <ul><ul><li>int turn = 1; </li></ul></ul><ul><ul><li>cobegin </li></ul></ul><ul><ul><li>p1: while (1) { </li></ul></ul><ul><ul><li>while ( turn ==2); /*wait*/ </li></ul></ul><ul><ul><li>CS1; </li></ul></ul><ul><ul><li>turn = 2; </li></ul></ul><ul><ul><li>program1; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>// </li></ul></ul><ul><ul><li>p2: while (1) { </li></ul></ul><ul><ul><li>while ( turn ==1); /*wait*/ </li></ul></ul><ul><ul><li>CS2; </li></ul></ul><ul><ul><li>turn = 1; </li></ul></ul><ul><ul><li>program2; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>coend </li></ul></ul>
  43. 43. Algorithm 1 <ul><li>Mutual Exclusion </li></ul><ul><li>No mutual blocking </li></ul><ul><ul><li>No dog in manger </li></ul></ul><ul><ul><li>Fairness </li></ul></ul><ul><ul><li>No deadlock </li></ul></ul><ul><ul><li>No livelock </li></ul></ul><ul><ul><li>int turn = 1; </li></ul></ul><ul><ul><li>cobegin </li></ul></ul><ul><ul><li>p1: while (1) { </li></ul></ul><ul><ul><li>while ( turn ==2); /*wait*/ </li></ul></ul><ul><ul><li>CS1; </li></ul></ul><ul><ul><li>turn = 2; </li></ul></ul><ul><ul><li>program1; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>// </li></ul></ul><ul><ul><li>p2: while (1) { </li></ul></ul><ul><ul><li>while ( turn ==1); /*wait*/ </li></ul></ul><ul><ul><li>CS2; </li></ul></ul><ul><ul><li>turn = 1; </li></ul></ul><ul><ul><li>program2; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>coend </li></ul></ul>     What happens if p1 fail?
  44. 44. Algorithm 2 <ul><ul><li>int c1 = 0, c2 = 0; </li></ul></ul><ul><ul><li>cobegin </li></ul></ul><ul><ul><li>p1: while (1) { </li></ul></ul><ul><ul><li>c1 = 1; </li></ul></ul><ul><ul><li>while (c2); /*wait*/ </li></ul></ul><ul><ul><li>CS1; </li></ul></ul><ul><ul><li>c1 = 0; </li></ul></ul><ul><ul><li>program1; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>// </li></ul></ul><ul><ul><li>p2: while (1) { </li></ul></ul><ul><ul><li>c2 = 1; </li></ul></ul><ul><ul><li>while (c1); /*wait*/ </li></ul></ul><ul><ul><li>CS2; </li></ul></ul><ul><ul><li>c2 = 0; </li></ul></ul><ul><ul><li>program2; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>coend </li></ul></ul>
  45. 45. Algorithm 2 <ul><li>Mutual Exclusion </li></ul><ul><li>No mutual blocking </li></ul><ul><ul><li>No dog in manger </li></ul></ul><ul><ul><li>Fairness </li></ul></ul><ul><ul><li>No deadlock </li></ul></ul><ul><ul><li>No livelock </li></ul></ul><ul><ul><li>int c1 = 0, c2 = 0; </li></ul></ul><ul><ul><li>cobegin </li></ul></ul><ul><ul><li>p1: while (1) { </li></ul></ul><ul><ul><li>c1 = 1; </li></ul></ul><ul><ul><li>while (c2); /*wait*/ </li></ul></ul><ul><ul><li>CS1; </li></ul></ul><ul><ul><li>c1 = 0; </li></ul></ul><ul><ul><li>program1; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>// </li></ul></ul><ul><ul><li>p2: while (1) { </li></ul></ul><ul><ul><li>c2 = 1; </li></ul></ul><ul><ul><li>while (c1); /*wait*/ </li></ul></ul><ul><ul><li>CS2; </li></ul></ul><ul><ul><li>c2 = 0; </li></ul></ul><ul><ul><li>program2; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>coend </li></ul></ul>     What happens if c1=1 and c2=1 ?
  46. 46. Algorithm 3 <ul><ul><li>int c1 = 0, c2 = 0; </li></ul></ul><ul><ul><li>cobegin </li></ul></ul><ul><ul><li>p1: while (1) { </li></ul></ul><ul><ul><li>c1 = 1; </li></ul></ul><ul><ul><li>if (c2) c1=0; </li></ul></ul><ul><ul><li>else{ </li></ul></ul><ul><ul><li> CS1; </li></ul></ul><ul><ul><li> c1 = 0; </li></ul></ul><ul><ul><li> program1; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>// </li></ul></ul><ul><ul><li>p2: while (1) { </li></ul></ul><ul><ul><li>c2 = 1; </li></ul></ul><ul><ul><li>if (c1) c2=0; </li></ul></ul><ul><ul><li>else{ </li></ul></ul><ul><ul><li> CS2; </li></ul></ul><ul><ul><li> c2 = 0; </li></ul></ul><ul><ul><li> program2; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>coend </li></ul></ul>
  47. 47. Algorithm 3 <ul><li>Mutual Exclusion </li></ul><ul><li>No mutual blocking </li></ul><ul><ul><li>No dog in manger </li></ul></ul><ul><ul><li>Fairness </li></ul></ul><ul><ul><li>No deadlock </li></ul></ul><ul><ul><li>No livelock </li></ul></ul><ul><ul><li>int c1 = 0, c2 = 0; </li></ul></ul><ul><ul><li>cobegin </li></ul></ul><ul><ul><li>p1: while (1) { </li></ul></ul><ul><ul><li>c1 = 1; </li></ul></ul><ul><ul><li>if (c2) c1=0; </li></ul></ul><ul><ul><li>else{ </li></ul></ul><ul><ul><li> CS1; </li></ul></ul><ul><ul><li> c1 = 0; </li></ul></ul><ul><ul><li> program1; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>// </li></ul></ul><ul><ul><li>p2: while (1) { </li></ul></ul><ul><ul><li>c2 = 1; </li></ul></ul><ul><ul><li>if (c1) c2=0; </li></ul></ul><ul><ul><li>else{ </li></ul></ul><ul><ul><li> CS2; </li></ul></ul><ul><ul><li> c2 = 0; </li></ul></ul><ul><ul><li> program2; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>coend </li></ul></ul>    When timing is critical, fairness and livelock requirements may be violated. 
  48. 48. Algorithm 3 <ul><ul><li>p1: while (1) { </li></ul></ul><ul><ul><li>c1 = 1; </li></ul></ul><ul><ul><li>if (c2) c1=0; </li></ul></ul><ul><ul><li>else{ </li></ul></ul><ul><ul><li> CS1; </li></ul></ul><ul><ul><li> c1 = 0; </li></ul></ul><ul><ul><li> program1; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>p2: while (1) { </li></ul></ul><ul><ul><li>c2 = 1; </li></ul></ul><ul><ul><li>if (c1) c2=0; </li></ul></ul><ul><ul><li>else{ </li></ul></ul><ul><ul><li> CS2; </li></ul></ul><ul><ul><li> c2 = 0; </li></ul></ul><ul><ul><li> program2; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul>May violate the fairness requirement.
  49. 49. Algorithm 3 <ul><ul><li>p1: while (1) { </li></ul></ul><ul><ul><li>c1 = 1; </li></ul></ul><ul><ul><li>if (c2) c1=0; </li></ul></ul><ul><ul><li>else{ </li></ul></ul><ul><ul><li> CS1; </li></ul></ul><ul><ul><li> c1 = 0; </li></ul></ul><ul><ul><li> program1; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>p2: while (1) { </li></ul></ul><ul><ul><li>c2 = 1; </li></ul></ul><ul><ul><li>if (c1) c2=0; </li></ul></ul><ul><ul><li>else{ </li></ul></ul><ul><ul><li> CS2; </li></ul></ul><ul><ul><li> c2 = 0; </li></ul></ul><ul><ul><li> program2; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul>May violate the livelock requirement.
  50. 50. Algorithm 4 (Peterson) <ul><ul><li>int c1 = 0, c2 = 0, WillWait; </li></ul></ul><ul><ul><li>cobegin </li></ul></ul><ul><ul><li>p1: while (1) { </li></ul></ul><ul><ul><li>c1 = 1; </li></ul></ul><ul><ul><li>willWait = 1; </li></ul></ul><ul><ul><li>while (c2 && (WillWait==1)); /*wait*/ </li></ul></ul><ul><ul><li>CS1; </li></ul></ul><ul><ul><li>c1 = 0; </li></ul></ul><ul><ul><li>program1; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>// </li></ul></ul><ul><ul><li>p2: while (1) { </li></ul></ul><ul><ul><li>c2 = 1; </li></ul></ul><ul><ul><li>willWait = 2; </li></ul></ul><ul><ul><li>while (c1 && (WillWait==2)); /*wait*/ </li></ul></ul><ul><ul><li>CS2; </li></ul></ul><ul><ul><li>c2 = 0; </li></ul></ul><ul><ul><li>program2; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>coend </li></ul></ul>
  51. 51. Algorithm 4 (Peterson) <ul><li>Mutual Exclusion </li></ul><ul><li>No mutual blocking </li></ul><ul><ul><li>No dog in manger </li></ul></ul><ul><ul><li>Fairness </li></ul></ul><ul><ul><li>No deadlock </li></ul></ul><ul><ul><li>No livelock </li></ul></ul><ul><ul><li>int c1 = 0, c2 = 0, WillWait; </li></ul></ul><ul><ul><li>cobegin </li></ul></ul><ul><ul><li>p1: while (1) { </li></ul></ul><ul><ul><li>c1 = 1; </li></ul></ul><ul><ul><li>willWait = 1; </li></ul></ul><ul><ul><li>while (c2 && (WillWait==1)); /*wait*/ </li></ul></ul><ul><ul><li>CS1; </li></ul></ul><ul><ul><li>c1 = 0; </li></ul></ul><ul><ul><li>program1; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>// </li></ul></ul><ul><ul><li>p2: while (1) { </li></ul></ul><ul><ul><li>c2 = 1; </li></ul></ul><ul><ul><li>willWait = 2; </li></ul></ul><ul><ul><li>while (c1 && (WillWait==2)); /*wait*/ </li></ul></ul><ul><ul><li>CS2; </li></ul></ul><ul><ul><li>c2 = 0; </li></ul></ul><ul><ul><li>program2; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>coend </li></ul></ul>    
  52. 52. Cooperation: Producer/Consumer Buffer Producer Consumer Producer must not overwrite any data before the Consumer can remove it. Consumer must be able to wait for the Producer when the latter falls behind and does not fill the buffer on time. Deposit Remove
  53. 53. Client/Server Architecture Processes communicate by message passing .
  54. 54. Competition and Cooperation <ul><li>Problems with software solutions: </li></ul><ul><ul><li>Difficult to program and to verify </li></ul></ul><ul><ul><li>Competition and cooperation use entirely different solutions </li></ul></ul><ul><ul><li>Processes loop while waiting (busy-wait) </li></ul></ul>
  55. 55. Process States running ready running ready ready_queue time-out dispatch
  56. 56. Process States running ready ready blocked running ready_queue time-out dispatch wait(si) wait-queues s1 sn . . . . . . . . .
  57. 57. Process States running ready ready blocked ready_queue time-out dispatch wait(si) wait-queues s1 sn . . . . . . . . .
  58. 58. Process States running ready ready blocked A running process calls signal(si) . running ready_queue time-out dispatch wait(si) wait-queues s1 sn . . . . . . . . . signal(si)
  59. 59. P/V Operators running ready ready blocked running P operation V operation ready_queue time-out dispatch wait(si) wait-queues s1 sn . . . . . . . . . signal(si)
  60. 60. Dijkstra’s Semaphores <ul><li>Dijkstra’s (1968) introduced two new operations, called P and V , that considerably simplify the coordination of concurrent processes . </li></ul><ul><ul><li>Universally applicable to competition and cooperation among any number of processes. </li></ul></ul><ul><ul><li>Avoid the performance degradation resulting from waiting. </li></ul></ul>
  61. 61. Semaphores <ul><li>A semaphore s is an non-negative integer, a down counter, with two operations P and V . </li></ul><ul><li>P and V are indivisible operations (atomic) </li></ul>P(s) /* wait(s) */ { if (s>0) s-- ; else{ queue the process on s , change its state to blocked, schedule another process } } V(s) /* signal(s) */ { if (s==0 && queue is not empty){ pick a process from the queue on s , change its state from blocked to ready; } else s++ ; } There is a wait-queue associated with each semaphore. s
  62. 62. Semaphores There is a wait-queue associated with each semaphore. P(s) CS1 V(s) Program1 P(s) CS2 V(s) Program2 P(s) CS3 V(s) Program3 P(s) CS4 V(s) Program4 Process 4 Process 3 Process 2 Process 1 s s
  63. 63. Semaphores There is a wait-queue associated with each semaphore. P(s) CS1 V(s) Program1 P(s) CS2 V(s) Program2 P(s) CS3 V(s) Program3 P(s) CS4 V(s) Program4 P(s) Process 4 Process 3 Process 2 Process 1 s s
  64. 64. Semaphores There is a wait-queue associated with each semaphore. P(s) CS1 V(s) Program1 P(s) CS2 V(s) Program2 P(s) CS3 V(s) Program3 P(s) CS4 V(s) Program4 Program2 P(s) Process 4 Process 3 Process 2 Process 1 s s
  65. 65. Semaphores There is a wait-queue associated with each semaphore. P(s) CS1 V(s) Program1 P(s) CS2 V(s) Program2 P(s) CS3 V(s) Program3 P(s) CS4 V(s) Program4 Process 4 Process 3 Process 2 Process 1 s s
  66. 66. Semaphores There is a wait-queue associated with each semaphore. P(s) CS1 V(s) Program1 P(s) CS2 V(s) Program2 P(s) CS3 V(s) Program3 P(s) CS4 V(s) Program4 Program4 Process 4 Process 3 Process 2 Process 1 s s Process 3
  67. 67. Semaphores There is a wait-queue associated with each semaphore. P(s) CS1 V(s) Program1 P(s) CS2 V(s) Program2 P(s) CS3 V(s) Program3 P(s) CS4 V(s) Program4 Process 4 Process 3 Process 2 Process 1 s s Process 3 Process 3 Process 4
  68. 68. Semaphores There is a wait-queue associated with each semaphore. P(s) CS1 V(s) Program1 P(s) CS2 V(s) Program2 P(s) CS3 V(s) Program3 P(s) CS4 V(s) Program4 Process 4 Process 3 Process 2 Process 1 s s Process 3 Process 4
  69. 69. Semaphores There is a wait-queue associated with each semaphore. P(s) CS1 V(s) Program1 P(s) CS2 V(s) Program2 P(s) CS3 V(s) Program3 P(s) CS4 V(s) Program4 Process 4 Process 3 Process 2 Process 1 s s Process 3 Process 4
  70. 70. Semaphores There is a wait-queue associated with each semaphore. P(s) CS1 V(s) Program1 P(s) CS2 V(s) Program2 P(s) CS3 V(s) Program3 P(s) CS4 V(s) Program4 Process 4 Process 3 Process 2 Process 1 s s Process 4
  71. 71. Semaphores There is a wait-queue associated with each semaphore. P(s) CS1 V(s) Program1 P(s) CS2 V(s) Program2 P(s) CS3 V(s) Program3 P(s) CS4 V(s) Program4 Process 4 Process 3 Process 2 Process 1 s s Process 4
  72. 72. Semaphores There is a wait-queue associated with each semaphore. P(s) CS1 V(s) Program1 P(s) CS2 V(s) Program2 P(s) CS3 V(s) Program3 P(s) CS4 V(s) Program4 V(s) V(s) CS4 Process 4 Process 3 Process 2 Process 1 s s
  73. 73. Semaphores <ul><li>A semaphore s is an non-negative integer with two operations P and V . </li></ul><ul><li>P and V are indivisible operations (atomic) </li></ul>P(s) /* wait(s) */ { if (s>0) s-- ; else{ queue the process on s , change its state to blocked, schedule another process } } V(s) /* signal(s) */ { if (s==0 && queue is not empty){ pick a process from the queue on s , change its state from blocked to ready; } else s++ ; } There is a wait-queue associated with each semaphore. s
  74. 74. Mutual Exclusion with Semaphores semaphore mutex = 1; /* binary semaphore */ cobegin p1: while (1) { P ( mutex ); CS1; V ( mutex ); program1; } // p2: while (1) { P ( mutex ); CS2; V ( mutex ); program2; } // . . . // pn: while (1) { P ( mutex ); CSn; V ( mutex ); programn; } coend ;
  75. 75. Producer/Consumer with Semaphores // counting semaphore semaphore s = 0; /* zero item produced */ cobegin p1: ... P ( s ); /* wait for signal from other process if needed */ ... // p2: ... V ( s ); /* send signal to wakeup an idle process*/ ... coend ; Consumer Producer
  76. 76. Producer/Consumer with Semaphores // counting semaphore semaphore s = 0; /* zero item produced */ cobegin p1: ... P ( s ); /* wait for signal from other process if needed */ ... // p2: ... V ( s ); /* send signal to wakeup an idle process*/ ... coend ;
  77. 77. The Bounded Buffer Problem semaphore e = n, f = 0, b = 1; cobegin Producer: while (1) { Produce_next_record; P ( e ); /* wait if zero cell empty */ P ( b ); /* mutually excusive on access the buffer */ Add_to_buf; V ( b ); /* release the buffer */ V ( f ); /* signal data available */ } // Consumer: while (1) { P ( f ); /* wait if data not available */ P ( b ); /* mutually excusive on access the buffer */ Take_from_buf; V ( b ); /* release the buffer */ V ( e ); /* signal empty cell available*/ Process_record; } coend e : the number of empty cells f : the number of items available b : mutual exclusion
  78. 78. Event Synchronization Operating Systems Principles Process Management and Coordination Lecture 2: Process and Their Interaction
  79. 79. Events <ul><li>An event denotes some change in the state of the system that is of interest to a process. </li></ul><ul><li>Usually principally through hardware interrupts and traps , either directly or indirectly. </li></ul>
  80. 80. Two Type of Events <ul><li>Synchronous event (e.g. I/O completion) </li></ul><ul><ul><li>Process waits for it explicitly </li></ul></ul><ul><ul><li>Constructs: E.wait, E.post </li></ul></ul><ul><li>Asynchronous event (e.g. arithmetic error) </li></ul><ul><ul><li>Process provides event handler </li></ul></ul><ul><ul><li>Invoked whenever event is posted </li></ul></ul>
  81. 81. Case study: Windows 2000 <ul><li>WaitForSingleObject </li></ul><ul><li>WaitForMultipleObjects </li></ul>Process blocks until object is signaled I/O operation terminates file item placed on queue queue expires timer posted event released mutex incremented semaphore terminates thread all threads complete process signaled when: object type

×