Chap 7 Process Synchronization进程同步 编写:李培峰 Applied Operating System Concepts 内容 Background(背景) The Critical-Section Problem (临界区问题) Semaphores (信号量) Classical Problems of Synchronization(经典同步问题) Monitors (管程) Java Synchronization (Java中的同步机制) Synchronization in Solaris 2 (Solaris 2的同步机制) Synchronization in Windows NT (Windows NT的同步机制) Summary(总结) 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 上将一起竞争条件) Applied Operating System Concepts Bounded Buffer :enter() Method有界缓冲: enter()方法 // 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; } Applied Operating System Concepts Bounded Buffer:remove() Method有界缓冲: remove ()方法 // 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; } Applied Operating System Concepts Machine Language of ++count and –count++count 和–count的机器语言 ++count: R1=count; 1 R1=R1+1; 2 count=R1; 3 --count: R2=count; 4 R2=R2-1; 5 count=R2; 6 Count初值为4,并发执行remove()和enter()进程 执行次序 结果 是否正确 123456 4 是 142536 3 否 145263 5 否 Applied Operating System Concepts Critical Resource & Critical-Section临界资源和临界区 critical resource(临界资源) 系统中某些资源一次只允许一个进程使用,称这样的资源为临界资源或互斥资源或共享变量。 Critical-Section(临界区) 涉及到临界资源的代码段叫临界区。 Applied Operating System Concepts Solution to Critical-Section Problem解决临界区问题 1. Mutual Exclusion. If process Pi is executing in its critical section,