Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Linux adoption wikipedia , lookup
Unix security wikipedia , lookup
Spring (operating system) wikipedia , lookup
Process management (computing) wikipedia , lookup
Berkeley Software Distribution wikipedia , lookup
Copland (operating system) wikipedia , lookup
Mobile operating system wikipedia , lookup
Chapter 6: Process Synchronization Operating System Concepts – 8th Edition, Silberschatz, Galvin and Gagne ©2009 Administrivia ■ In lab Friday. ■ New assignment. ■ Exam: Monday, April 6, on Chapters 16. ■ Read 8.1—8.5 for Monday. Operating System Concepts – 8th Edition 6.2 Silberschatz, Galvin and Gagne ©2009 Outline ■ Problems with semaphores. ■ Monitors and examples. ■ Synchronization facilities in operating environments. Operating System Concepts – 8th Edition 6.3 Silberschatz, Galvin and Gagne ©2009 Problems with Semaphores ■ Correct use of semaphore operations: ● signal (mutex) …. wait (mutex) ● wait (mutex) … wait (mutex) ● Omitting of wait (mutex) or signal (mutex) (or both) Operating System Concepts – 8th Edition 6.4 Silberschatz, Galvin and Gagne ©2009 Monitors ■ A highlevel abstraction that provides a convenient and effective mechanism for process synchronization ■ Only one process may be active within the monitor at a time monitor monitorname { // shared variable declarations procedure P1 (…) { …. } … procedure Pn (…) {……} Initialization code ( ….) { … } … } } Operating System Concepts – 8th Edition 6.5 Silberschatz, Galvin and Gagne ©2009 Condition Variables ■ condition x, y; ■ Two operations on a condition variable: ● x.wait () – a process that invokes the operation is suspended. ● x.signal () – resumes one of processes (if any) that invoked x.wait () Operating System Concepts – 8th Edition 6.6 Silberschatz, Galvin and Gagne ©2009 Monitor with Condition Variables Operating System Concepts – 8th Edition 6.7 Silberschatz, Galvin and Gagne ©2009 Solution to Dining Philosophers monitor DP { enum { THINKING; HUNGRY, EATING) state [5] ; condition self [5]; void pickup (int i) { state[i] = HUNGRY; test(i); if (state[i] != EATING) self [i].wait; } void putdown (int i) { state[i] = THINKING; // test left and right neighbors test((i + 4) % 5); test((i + 1) % 5); } Operating System Concepts – 8th Edition 6.8 Silberschatz, Galvin and Gagne ©2009 Solution to Dining Philosophers (cont) void test (int i) { if ( (state[(i + 4) % 5] != EATING) && (state[i] == HUNGRY) && (state[(i + 1) % 5] != EATING) ) { state[i] = EATING ; self[i].signal () ; } } initialization_code() { for (int i = 0; i < 5; i++) state[i] = THINKING; } } Operating System Concepts – 8th Edition 6.9 Silberschatz, Galvin and Gagne ©2009 A Monitor to Allocate A Single Resource monitor ResourceAllocator { boolean busy; condition x; void acquire(int time) { // time is max usage time if (busy) x.wait(time); // wait()’s param used to order the wait queue. // Implements a “shortest time first” priority. busy = TRUE; } void release() { busy = FALSE; x.signal(); } initialization code() { busy = FALSE; } } Operating System Concepts – 8th Edition 6.10 Silberschatz, Galvin and Gagne ©2009 Synchronization Examples ■ Java ■ Windows XP ■ Linux ■ Pthreads Operating System Concepts – 8th Edition 6.11 Silberschatz, Galvin and Gagne ©2009 Java Synchronization ■ Synchronized class methods Every Java object has an associated lock. ■ If lock is held by another thread, entering thread is queued on entry set. ■ Java provides wait() and notify(), similar to wait() and signal(). ■ Java 5 provides semaphores, condition variables, and mutex locks. Operating System Concepts – 8th Edition 6.12 Silberschatz, Galvin and Gagne ©2009 Windows XP Synchronization ■ Uses interrupt masks to protect access to global resources on uniprocessor systems ■ Uses spinlocks on multiprocessor systems. Threads holding spinlocks never preempted. ■ Also provides dispatcher objects which may act as either mutexes or semaphores ■ Dispatcher objects may also provide events ● An event acts much like a condition variable Operating System Concepts – 8th Edition 6.13 Silberschatz, Galvin and Gagne ©2009 Linux Synchronization ■ Linux: ● Prior to kernel Version 2.6, kernel was nonpreemptive. ● Version 2.6 and later, fully preemptive kernel. ■ Linux provides: ● ● Semaphores. Spinlocks (SMP systems). Uniprocessor systems disable/enable kernel preemption. Operating System Concepts – 8th Edition 6.14 Silberschatz, Galvin and Gagne ©2009 Pthreads Synchronization ■ Pthreads API is OSindependent ■ It provides: ● mutex locks ● condition variables ■ Nonportable extensions include: ● readwrite locks ● spin locks Operating System Concepts – 8th Edition 6.15 Silberschatz, Galvin and Gagne ©2009 End of Chapter 6 Operating System Concepts – 8th Edition, Silberschatz, Galvin and Gagne ©2009