110 likes | 390 Views
CS 423 – Operating Systems Design Lecture 12 – Monitors and Barriers. Roy Campbell Spring 2008. Administration. Tanenbaum Chapter 2, 3 MP2 Understanding the LINUX Kernel, Ch 11, 8, 9, 16 Look at http://lxr.linux.no/. Monitors (1). Figure 2-33. A monitor. Monitors (2).
E N D
CS 423 – Operating Systems DesignLecture 12 – Monitors and Barriers Roy Campbell Spring 2008 CS 423 – Spring 2008
Administration Tanenbaum Chapter 2, 3 MP2 Understanding the LINUX Kernel, Ch 11, 8, 9, 16 Look at http://lxr.linux.no/
Monitors (1) Figure 2-33. A monitor. Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Monitors (2) Figure 2-34. An outline of the producer-consumer problem with monitors. Only one monitor procedure at a time is active. The buffer has N slots. Note similarity to Pthreads conditional wait.
Monitors (3) Figure 2-34. An outline of the producer-consumer problem with monitors. Only one monitor procedure at a time is active. The buffer has N slots. Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Barriers Two uses of term barriers: Linux – compiler barrier Multiprocessing – synchronization primitive. Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Compiler Barriers rmb() no loads are reordered across call wmb() no stores are reordered across call mb() no loads or stores are reordered across call barrier() prevents the compiler from optimizing stores or loads across the barrier Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Compiler Barriers Thread 1 Thread 2 - - c = b; rmb(); d = a; a = 3; mb(); b = 4; - - Without using the memory barrier, c could get the new value of b while d gets the old value of a Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Synchronization Barriers Figure 2-37. Use of a barrier. (a) Processes approaching a barrier. (b) All processes but one blocked at the barrier. (c) 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
Possible Synchronization Barrier Implementation spin_lock(barriercount); increment(count); spin_unlock(barriercount); if (count == all_processes) then {count = 0; for (k=0; k<=all_processes; K++) spin_unlock(barrierlock[k]; }; else spin_lock(barrierlock[mine]); Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639