. . . . . . . . . Synchronization Examples Monitors Classical Problems of Synchronization . . . . 小结和作业 操作系统原理与设计 第 6 章 Processe Synchronization2 (进程同步 2 ) 陈香兰 中国科学技术大学计算机学院 2009 年 10 月 28 日 陈香兰 操作系统原理与设计
. . 4 .. . Synchronization Examples 3 .. . Monitors 2 .. . Classical Problems of Synchronization 1 .. . . Synchronization Examples Monitors Classical Problems of Synchronization . . . . 小结和作业 . 提纲 小结和作业 陈香兰 操作系统原理与设计
. . 4 .. . Synchronization Examples 3 .. . Monitors 2 .. . Classical Problems of Synchronization 1 .. . . Synchronization Examples Monitors Classical Problems of Synchronization . . . . 小结和作业 . Outline 小结和作业 陈香兰 操作系统原理与设计
. Use semaphores to solve . . . . Classical Problems of Synchronization Monitors Synchronization Examples . . 小结和作业 . Classical Problems of Synchronization Bounded-Buffer Problem ,生产者 - 消费者问题( PC Problem ) Readers and Writers Problem ,读者 - 写者问题 Dining-Philosophers Problem ,哲学家就餐问题 陈香兰 操作系统原理与设计
. process // consume the removed item signal (empty); signal (mutex); // remove an item from buffer wait (mutex); wait (full); process The structure of the consumer signal (full); signal (mutex); // add the item to the buffer wait (mutex); wait (empty); . // produce an item The structure of the producer Monitors . . . . Classical Problems of Synchronization Synchronization Examples N buffers, each can hold one item . Semaphore mutex initialized to the value 1 Semaphore full initialized to the value 0 Semaphore empty initialized to the value N. 小结和作业 . Solution to Bounded-Buffer Problem I while (true) { while (true) { } } 陈香兰 操作系统原理与设计
. . Integer readcount initialized to 0. Semaphore wrt initialized to 1. Semaphore mutex initialized to 1. Data set Shared Data time. Only one single writer can access the shared data at the same Problem – allow multiple readers to read at the same time. Writers – can both read and write. updates Readers – only read the data set; they do not perform any A data set is shared among a number of concurrent processes . Synchronization Examples Monitors Classical Problems of Synchronization . . . . 小结和作业 . Sulotion to Readers-Writers Problem I 陈香兰 操作系统原理与设计
. // writing is performed signal (mutex); if (readcount == 0) signal(wrt); readcount - -; wait(mutex); // reading is performed signal(mutex) if (readcount == 1) wait(wrt); readcount ++; wait(mutex); process The structure of a reader . signal(wrt); wait(wrt); Synchronization Examples . . . . Classical Problems of Synchronization Monitors . The structure of a writer process 小结和作业 . Sulotion to Readers-Writers Problem II while (true) { while (true) { } } 陈香兰 操作系统原理与设计
. . . . . . Classical Problems of Synchronization Monitors Synchronization Examples . 小结和作业 . Dining-Philosophers Problem I 陈香兰 操作系统原理与设计
. . This solution may cause a deadlock. // think signal (chopstick[ (i + 1) % 5] ); signal ( chopstick[i] ); // eat wait ( chopStick[ (i + 1) % 5] ); wait ( chopstick[i] ); The structure of Philosopher i: Semaphore chopstick [5] initialized to 1 Bowl of rice (data set) Shared data . Synchronization Examples Monitors Classical Problems of Synchronization . . . . 小结和作业 . Dining-Philosophers Problem II While (true) { } 陈香兰 操作系统原理与设计
. . Odd philosophers pick up first her left chopstick and then her chopsticks are available Allow a philosopher to pick up her chopsticks only if both the table. Allow at most 4 philosophers to be sitting simultaneously at Several possible remedies . right chopstick, while even philosophers pick up first her right chopstick and then her left chopstick. Synchronization Examples Monitors Classical Problems of Synchronization . . . . 小结和作业 . Dining-Philosophers Problem III 注: deadlock-free & starvation-free 陈香兰 操作系统原理与设计
. . will occur either mutual-exclusion requirement is violated, or a deadlock a deadlock will occur. their CS simultaneously the mutual-exclusion requirement is violated, processes may in . Incorrect use of semaphore operations: Synchronization Examples Monitors Classical Problems of Synchronization . . . . 小结和作业 . Problems with Semaphores signal (mutex) … . wait (mutex) wait (mutex) … wait (mutex) Omitting of wait (mutex) or signal (mutex) (or both) 陈香兰 操作系统原理与设计
. . 4 .. . Synchronization Examples 3 .. . Monitors 2 .. . Classical Problems of Synchronization 1 .. . . Synchronization Examples Monitors Classical Problems of Synchronization . . . . 小结和作业 . Outline 小结和作业 陈香兰 操作系统原理与设计
. . // shared variable declarations monitor monitor-name . . . . . . . . Syntax of a monitor . Only one process may be active within the monitor at a time Classical Problems of Synchronization . . effective mechanism for process synchronization . . Monitors Synchronization Examples . A high-level abstraction that provides a convenient and 小结和作业 . Monitors I { procedure P1 ( … ) { … } … procedure Pn ( … ) { … } Initialization code ( … .) { … } } 陈香兰 操作系统原理与设计
. . . . . . Classical Problems of Synchronization Monitors Synchronization Examples . Schematic view of a Monitor 小结和作业 . Monitors II 陈香兰 操作系统原理与设计
. a process that invokes the operation is suspended. . . . . Classical Problems of Synchronization Monitors Synchronization Examples . . the monitor construct is not sufficiently powerful for modeling some synchronization scheme. resumes one of processes (if any) that invoked x.wait () Two operations on a condition variable: 小结和作业 . Condition Variables I condition x, y; x.wait() x.signal() 陈香兰 操作系统原理与设计
. . . . . . Classical Problems of Synchronization Monitors Synchronization Examples . Monitor with Condition Variables 小结和作业 . Condition Variables II 陈香兰 操作系统原理与设计
. . in the language Concurrent Pascal, a compromise was adopted signal and continue signal and wait allowed to resume its execution, then ? process P invokes x.signal, and a suspended process Q is Problem with x.signal() . when P executes the signal operation, it immediately leaves the monitor, hence, Q is immediately resumed. Synchronization Examples Monitors Classical Problems of Synchronization . . . . 小结和作业 . Condition Variables III 陈香兰 操作系统原理与设计
. the monitor test(i); state[i] = HUNGRY; test((i + 4) % 5); condition self [5]; test((i + 1) % 5); . monitor DP . state[i] = THINKING; Synchronization Examples Monitors Classical Problems of Synchronization . . . . if (state[i] != EATING) self[i].wait; 小结和作业 . A deadlock-free solution to Dining Philosophers I { enum { THINKING; HUNGRY, EATING } state[5] ; void pickup (int i) { } void putdown (int i) { 陈香兰 操作系统原理与设计
. . state[i] = THINKING; for (int i = 0; i < 5; i++) self[i].signal () ; state[i] = EATING ; (state[i] == HUNGRY) && . if ( (state[(i + 4) % 5] != EATING) && . . . . Synchronization Examples Monitors Classical Problems of Synchronization 小结和作业 . A deadlock-free solution to Dining Philosophers II } void test (int i) { (state[(i + 1) % 5] != EATING) ) { } } initialization code() { } } 陈香兰 操作系统原理与设计
. Synchronization Examples not starvation-free putdown() in the following sequence: Each philosopher i invokes the operations pickup() and . . Monitors Classical Problems of Synchronization . . . . 小结和作业 . A deadlock-free solution to Dining Philosophers III dp.pickup (i) EAT dp.putdown (i) 陈香兰 操作系统原理与设计
. Variables . . . . Classical Problems of Synchronization Monitors Synchronization Examples . . 小结和作业 . Monitor Implementation Using Semaphores I semaphore mutex; // (initially = 1) , for enter and exit monitor semaphore next; // (initially = 0 ) int next-count = 0; 陈香兰 操作系统原理与设计
. Monitors Mutual exclusion within a monitor is ensured. . . Synchronization Examples Each external procedure F will be replaced by Classical Problems of Synchronization . . . . 小结和作业 . Monitor Implementation Using Semaphores II wait(mutex); … body of F; … if (next-count > 0) signal(next) else signal(mutex); 陈香兰 操作系统原理与设计
. For each condition variable x, we have: . . . . Classical Problems of Synchronization Monitors Synchronization Examples . . The operation x.wait can be implemented as: 小结和作业 . Monitor Implementation I semaphore x-sem; // (initially = 0 ) int x-count = 0; 陈香兰 操作系统原理与设计
More recommend