Module 7: Process Synchronization Background(背景) ° The critica|- Section Problen(临界区问题) ° Synchronization Hardware(同步的硬件实现) Semaphores(信号量) Classical Problems of Synchronization(经典同步问题) Monito(管程) Java Synchronization(Jawa中的同步机制) · Synchronization in Solaris2( Solaris2的同步机制) ° Synchronization in Windows NT( Windows nt的同步机制) Applied Operating System Concepts Silberschatz, GalVin, and Gagne @1999
Silberschatz, Galvin, and Gagne ©1999 7.1 Applied Operating System Concepts Module 7: Process Synchronization • Background(背景) • The Critical-Section Problem (临界区问题) • Synchronization Hardware (同步的硬件实现) • Semaphores (信号量) • Classical Problems of Synchronization(经典同步问题) • Monitors (管程) • Java Synchronization (Java中的同步机制) • Synchronization in Solaris 2 (Solaris 2的同步机制) • Synchronization in Windows NT (Windows NT的同步机制)
Background e Concurrent access to shared data may result in data inconsistency(对共享数据的并发访问可能导致数据的不一致性) Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes(要保持数据的 一致性,就需要一种保证并发进程的正确执行顺序的机制) Shared-memory solution to bounded-butter problem(Chapter 4) has a race condition on the class data count([第4章中]解决 有界缓冲区问题的共享内存方法在类数据 count上将一起竞争条件) Applied Operating System Concepts Silberschatz, GalVin, and Gagne @1999
Silberschatz, Galvin, and Gagne ©1999 7.2 Applied Operating System Concepts Background • Concurrent access to shared data may result in data inconsistency(对共享数据的并发访问可能导致数据的不一致性). • Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes(要保持数据的 一致性,就需要一种保证并发进程的正确执行顺序的机制). • Shared-memory solution to bounded-butter problem (Chapter 4) has a race condition on the class data count ([第4章中]解决 有界缓冲区问题的共享内存方法在类数据count 上将一起竞争条件)
Bounded Buffer public class Bounded Buffer( public void enter(object item) l/ producer calls this method public Object remove i l/ consumer calls this method l potential race condition on count private volatile int coun Applied Operating System Concepts Silberschatz, GalVin, and Gagne @1999
Silberschatz, Galvin, and Gagne ©1999 7.3 Applied Operating System Concepts Bounded Buffer public class BoundedBuffer { public void enter(Object item) { // producer calls this method } public Object remove() { // consumer calls this method } // potential race condition on count private volatile int count; }
enter( Method l/ producer calls this method public void enter(object item)i while(count== BUFFER SIZE ;∥ do nothing l/ add an item to the buffer ++count buffer[in]= item; in=(in+ 1)% BUFFER_SIzE, Applied Operating System Concepts 74 Silberschatz, GalVin, and Gagne @1999
Silberschatz, Galvin, and Gagne ©1999 7.4 Applied Operating System Concepts enter() Method // producer calls this method public void enter(Object item) { while (count == BUFFER_SIZE) ; // do nothing // add an item to the buffer ++count; buffer[in] = item; in = (in + 1) % BUFFER_SIZE; }
remove( Method l/ consumer calls this method public object remove i Object item while(count ==0) ;∥ do nothing l/ remove an item from the buffer cou item= buffer[out] out=(out 1)% BUFFER SIZE, return item Applied Operating System Concepts Silberschatz, GalVin, and Gagne @1999
Silberschatz, Galvin, and Gagne ©1999 7.5 Applied Operating System Concepts remove() Method // consumer calls this method public Object remove() { Object item; while (count == 0) ; // do nothing // remove an item from the buffer --count; item = buffer[out]; out = (out + 1) % BUFFER_SIZE; return item; }