OPERATING SYSTEMS DESIGN AND IMPLEMENTATION Third Edition ANDREW S. TANENBAUM ALBERT S. WOODHULL Chap. 2.2 Interprocess Communication Annotated by B. Hirsbrunner, University of Fribourg, 2011 Lecture 5, 18 October 2011 1
2.2.1 Race Conditions Fig. 2-8. Two processes want to access shared memory at the same time
2.2.2 Critical Regions Enter CR Critical Region (CR) Leave CR (1) Door is always open (2) Door is closed or open (1) (2) (1) (1) time Four conditions to provide mutual exclusion 1) No two processes simultaneously in critical region 2) No assumptions made about speeds or numbers of CPUs 3) No process running outside its critical region may block another process 4) No process must wait forever to enter its critical region 3
2.2.2 Critical Regions Fig. 2-9. Mutual exclusion using critical regions
2.2.3 Mutual Exclusion with Busy Waiting Proposals for achieving mutual exclusion: Disabling interrupts Lock variables Strict alternation Peterson's solution The TSL instruction
Disabling Interrupts On a single processor Disable all interrupts just before entering its critical Re-enable them just before leaving Hint: The CPU is only switched from process to process as a result of clock or other interrupts. On a multiprocessor or multicore This solution does not work! Hint: Disabling the interrupts of one CPU does not prevent other CPUs from interfering with operations the first CPU is performing. 6
Lock Variables A not working software solution Share a (lock) variable, initially set to 0. When a process wants to enter its critical region, it first tests the lock. If the lock is 0, the process sets it to 1 and enters the critical region. Hint: This solution has exactly the same fatal flow as the spooler directory example (cf. fig. 2-8). Remark: But this solution works with firmware support, e.g. TSL instruction, cf. fig. 2-12 below. 7
Strict Alternation Process 0 Process 1 Fig. 2-9. An algorithmic solution to the critical region problem 8
Peterson's Solution Fig. 2-11. Peterson s solution for achieving mutual exclusion
The TSL Instruction Fig. 2-12. Entering and leaving a critical region using the TSL instruction Remark: TSL (Test and Set Lock) reads the contents of the memory word LOCK into register REGISTER and stores a non-zero value at the memory address LOCK. These two operations of reading and storing are done atomically.
The XCHG Instruction Fig. 2-26. Entering and leaving a critical region using the XCHG instruction Remark: XCHG exchanges the contents of two locations atomically (for example a register and a memory word). Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Remark: adding a wakeup bit solves the fatal race condition for this problem, but not for all problems! 2.2.4 Sleep and Wakeup enter critical region leave critical region enter critical region leave critical region Fig. 2-13. Producer-consumer problem with fatal race condition 12
2.2.5 Semaphores enter critical region leave critical region 13
leave critical region enter critical region Fig. 2-14. The producer-consumer problem using semaphores 14
2.2.6 Mutexes Fig 2-29. Implementation of mutex lock and mutex unlock Definition A mutex is a variable that can be in one of two states: unlocked (0) or locked (1) Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Mutexes in Pthreads Fig. 2-30. Some of the Pthreads calls relating to mutexes Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Mutexes in Pthreads : conditional variables Fig. 2-31. Some of the Pthreads calls relating to condition variables Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
2.2.7 Monitors Fig. 2-15. A monitor Only one monitor procedure at a time can be active
Monitors : an example Fig. 2-16. Outline of producer-consumer problem with monitors only one monitor procedure active at one time buffer has N slots 19
Monitors : the model 20
2.2.8 Message Passing Fig. 2-17. The producer-consumer problem with N messages 21
Barriers Fig. 2-37. Use of a barrier (a) (b) (c) Processes approaching a barrier. All processes but one blocked at the barrier. When the last process arrives at the barrier, all of them are let through. Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639