6 Processe Synchronization2 2 - - PowerPoint PPT Presentation

6 processe synchronization2 2
SMART_READER_LITE
LIVE PREVIEW

6 Processe Synchronization2 2 - - PowerPoint PPT Presentation

. . . . . . . . . Synchronization Examples Monitors Classical Problems of Synchronization . . . . 6 Processe Synchronization2 2


slide-1
SLIDE 1

. . . . . .

Classical Problems of Synchronization Monitors Synchronization Examples 小结和作业

. . . . . . .

操作系统原理与设计

第6章 Processe Synchronization2(进程同步2) 陈香兰

中国科学技术大学计算机学院

2009年10月28日

陈香兰 操作系统原理与设计

slide-2
SLIDE 2

. . . . . .

Classical Problems of Synchronization Monitors Synchronization Examples 小结和作业

. . 提纲

. ..

1

Classical Problems of Synchronization . ..

2

Monitors . ..

3

Synchronization Examples . ..

4

小结和作业

陈香兰 操作系统原理与设计

slide-3
SLIDE 3

. . . . . .

Classical Problems of Synchronization Monitors Synchronization Examples 小结和作业

. . Outline

. ..

1

Classical Problems of Synchronization . ..

2

Monitors . ..

3

Synchronization Examples . ..

4

小结和作业

陈香兰 操作系统原理与设计

slide-4
SLIDE 4

. . . . . .

Classical Problems of Synchronization Monitors Synchronization Examples 小结和作业

. . Classical Problems of Synchronization

Use semaphores to solve

Bounded-Buffer Problem,生产者-消费者问题(PC Problem) Readers and Writers Problem,读者-写者问题 Dining-Philosophers Problem ,哲学家就餐问题

陈香兰 操作系统原理与设计

slide-5
SLIDE 5

. . . . . .

Classical Problems of Synchronization Monitors Synchronization Examples 小结和作业

. . Solution to Bounded-Buffer Problem I

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. The structure of the producer process

while (true) { // produce an item wait (empty); wait (mutex); // add the item to the buffer signal (mutex); signal (full); }

The structure of the consumer process

while (true) { wait (full); wait (mutex); // remove an item from buffer signal (mutex); signal (empty); // consume the removed item }

陈香兰 操作系统原理与设计

slide-6
SLIDE 6

. . . . . .

Classical Problems of Synchronization Monitors Synchronization Examples 小结和作业

. . Sulotion to Readers-Writers Problem I

A data set is shared among a number of concurrent processes

Readers – only read the data set; they do not perform any updates Writers – can both read and write.

Problem – allow multiple readers to read at the same time. Only one single writer can access the shared data at the same time. Shared Data

Data set Semaphore mutex initialized to 1. Semaphore wrt initialized to 1. Integer readcount initialized to 0.

陈香兰 操作系统原理与设计

slide-7
SLIDE 7

. . . . . .

Classical Problems of Synchronization Monitors Synchronization Examples 小结和作业

. . Sulotion to Readers-Writers Problem II

The structure of a writer process

while (true) { wait(wrt); // writing is performed signal(wrt); }

The structure of a reader process

while (true) { wait(mutex); readcount ++; if (readcount == 1) wait(wrt); signal(mutex) // reading is performed wait(mutex); readcount - -; if (readcount == 0) signal(wrt); signal (mutex); }

陈香兰 操作系统原理与设计

slide-8
SLIDE 8

. . . . . .

Classical Problems of Synchronization Monitors Synchronization Examples 小结和作业

. . Dining-Philosophers Problem I

陈香兰 操作系统原理与设计

slide-9
SLIDE 9

. . . . . .

Classical Problems of Synchronization Monitors Synchronization Examples 小结和作业

. . Dining-Philosophers Problem II

Shared data

Bowl of rice (data set) Semaphore chopstick [5] initialized to 1

The structure of Philosopher i:

While (true) { wait ( chopstick[i] ); wait ( chopStick[ (i + 1) % 5] ); // eat signal ( chopstick[i] ); signal (chopstick[ (i + 1) % 5] ); // think }

This solution may cause a deadlock.

陈香兰 操作系统原理与设计

slide-10
SLIDE 10

. . . . . .

Classical Problems of Synchronization Monitors Synchronization Examples 小结和作业

. . Dining-Philosophers Problem III

Several possible remedies

Allow at most 4 philosophers to be sitting simultaneously at the table. Allow a philosopher to pick up her chopsticks only if both chopsticks are available Odd philosophers pick up first her left chopstick and then her right chopstick, while even philosophers pick up first her right chopstick and then her left chopstick.

注:deadlock-free & starvation-free

陈香兰 操作系统原理与设计

slide-11
SLIDE 11

. . . . . .

Classical Problems of Synchronization Monitors Synchronization Examples 小结和作业

. . Problems with Semaphores

Incorrect use of semaphore operations: signal (mutex) …. wait (mutex)

the mutual-exclusion requirement is violated, processes may in their CS simultaneously

wait (mutex) … wait (mutex)

a deadlock will occur.

Omitting of wait (mutex) or signal (mutex) (or both)

either mutual-exclusion requirement is violated, or a deadlock will occur

陈香兰 操作系统原理与设计

slide-12
SLIDE 12

. . . . . .

Classical Problems of Synchronization Monitors Synchronization Examples 小结和作业

. . Outline

. ..

1

Classical Problems of Synchronization . ..

2

Monitors . ..

3

Synchronization Examples . ..

4

小结和作业

陈香兰 操作系统原理与设计

slide-13
SLIDE 13

. . . . . .

Classical Problems of Synchronization Monitors Synchronization Examples 小结和作业

. . Monitors I

A high-level abstraction that provides a convenient and effective mechanism for process synchronization Only one process may be active within the monitor at a time . Syntax of a monitor . . . . . . . .

monitor monitor-name { // shared variable declarations procedure P1 (…) {…} … procedure Pn (…) {…} Initialization code (….) {…} }

陈香兰 操作系统原理与设计

slide-14
SLIDE 14

. . . . . .

Classical Problems of Synchronization Monitors Synchronization Examples 小结和作业

. . Monitors II

Schematic view of a Monitor

陈香兰 操作系统原理与设计

slide-15
SLIDE 15

. . . . . .

Classical Problems of Synchronization Monitors Synchronization Examples 小结和作业

. . Condition Variables I

the monitor construct is not sufficiently powerful for modeling some synchronization scheme. 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 ()

陈香兰 操作系统原理与设计

slide-16
SLIDE 16

. . . . . .

Classical Problems of Synchronization Monitors Synchronization Examples 小结和作业

. . Condition Variables II

Monitor with Condition Variables

陈香兰 操作系统原理与设计

slide-17
SLIDE 17

. . . . . .

Classical Problems of Synchronization Monitors Synchronization Examples 小结和作业

. . Condition Variables III

Problem with x.signal()

process P invokes x.signal, and a suspended process Q is allowed to resume its execution, then ?

signal and wait signal and continue

in the language Concurrent Pascal, a compromise was adopted

when P executes the signal operation, it immediately leaves the monitor, hence, Q is immediately resumed.

陈香兰 操作系统原理与设计

slide-18
SLIDE 18

. . . . . .

Classical Problems of Synchronization Monitors Synchronization Examples 小结和作业

. . A deadlock-free solution to Dining Philosophers I

the monitor

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((i + 4) % 5); test((i + 1) % 5);

陈香兰 操作系统原理与设计

slide-19
SLIDE 19

. . . . . .

Classical Problems of Synchronization Monitors Synchronization Examples 小结和作业

. . A deadlock-free solution to Dining Philosophers II

} 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; } }

陈香兰 操作系统原理与设计

slide-20
SLIDE 20

. . . . . .

Classical Problems of Synchronization Monitors Synchronization Examples 小结和作业

. . A deadlock-free solution to Dining Philosophers III

Each philosopher i invokes the operations pickup() and putdown() in the following sequence:

dp.pickup (i) EAT dp.putdown (i)

not starvation-free

陈香兰 操作系统原理与设计

slide-21
SLIDE 21

. . . . . .

Classical Problems of Synchronization Monitors Synchronization Examples 小结和作业

. . Monitor Implementation Using Semaphores I

Variables

semaphore mutex; // (initially = 1) , for enter and exit monitor semaphore next; // (initially = 0) int next-count = 0;

陈香兰 操作系统原理与设计

slide-22
SLIDE 22

. . . . . .

Classical Problems of Synchronization Monitors Synchronization Examples 小结和作业

. . Monitor Implementation Using Semaphores II

Each external procedure F will be replaced by

wait(mutex); … body of F; … if (next-count > 0) signal(next) else signal(mutex);

Mutual exclusion within a monitor is ensured.

陈香兰 操作系统原理与设计

slide-23
SLIDE 23

. . . . . .

Classical Problems of Synchronization Monitors Synchronization Examples 小结和作业

. . Monitor Implementation I

For each condition variable x, we have:

semaphore x-sem; // (initially = 0) int x-count = 0;

The operation x.wait can be implemented as:

陈香兰 操作系统原理与设计

slide-24
SLIDE 24

. . . . . .

Classical Problems of Synchronization Monitors Synchronization Examples 小结和作业

. . Monitor Implementation II

x-count++; if (next-count > 0) signal(next); else signal(mutex); wait(x-sem); x-count–;

陈香兰 操作系统原理与设计

slide-25
SLIDE 25

. . . . . .

Classical Problems of Synchronization Monitors Synchronization Examples 小结和作业

. . Monitor Implementation III

The operation x.signal can be implemented as:

if (x-count > 0) { next-count++; signal(x-sem); wait(next); next-count–; }

陈香兰 操作系统原理与设计

slide-26
SLIDE 26

. . . . . .

Classical Problems of Synchronization Monitors Synchronization Examples 小结和作业

. . Outline

. ..

1

Classical Problems of Synchronization . ..

2

Monitors . ..

3

Synchronization Examples . ..

4

小结和作业

陈香兰 操作系统原理与设计

slide-27
SLIDE 27

. . . . . .

Classical Problems of Synchronization Monitors Synchronization Examples 小结和作业

. . Synchronization Examples

Solaris Windows XP Linux Pthreads

陈香兰 操作系统原理与设计

slide-28
SLIDE 28

. . . . . .

Classical Problems of Synchronization Monitors Synchronization Examples 小结和作业

. . Solaris Synchronization

Implements a variety of locks to support multitasking, multithreading (including real-time threads), and multiprocessing Uses adaptive mutexes for efficiency when protecting data from short code segments Uses condition variables and readers-writers locks when longer sections of code need access to data Uses turnstiles to order the list of threads waiting to acquire either an adaptive mutex or reader-writer lock

陈香兰 操作系统原理与设计

slide-29
SLIDE 29

. . . . . .

Classical Problems of Synchronization Monitors Synchronization Examples 小结和作业

. . Windows XP Synchronization

Uses interrupt masks to protect access to global resources on uniprocessor systems Uses spinlocks on multiprocessor systems Also provides dispatcher objects which may act as either mutexes and semaphores Dispatcher objects may also provide events

An event acts much like a condition variable

陈香兰 操作系统原理与设计

slide-30
SLIDE 30

. . . . . .

Classical Problems of Synchronization Monitors Synchronization Examples 小结和作业

. . Linux Synchronization

before 2.6, nonpreemptive kernel now, fully preemptive Linux:

disables interrupts to implement short critical sections

Linux provides:

semaphores spin locks

陈香兰 操作系统原理与设计

slide-31
SLIDE 31

. . . . . .

Classical Problems of Synchronization Monitors Synchronization Examples 小结和作业

. . Pthreads Synchronization

Pthreads API is OS-independent It provides:

mutex locks condition variables

Non-portable extensions include:

read-write locks spin locks

陈香兰 操作系统原理与设计

slide-32
SLIDE 32

. . . . . .

Classical Problems of Synchronization Monitors Synchronization Examples 小结和作业

. . Outline

. ..

1

Classical Problems of Synchronization . ..

2

Monitors . ..

3

Synchronization Examples . ..

4

小结和作业

陈香兰 操作系统原理与设计

slide-33
SLIDE 33

. . . . . .

Classical Problems of Synchronization Monitors Synchronization Examples 小结和作业

. . 小结

. ..

1

Classical Problems of Synchronization . ..

2

Monitors . ..

3

Synchronization Examples . ..

4

小结和作业

陈香兰 操作系统原理与设计

slide-34
SLIDE 34

. . . . . .

Classical Problems of Synchronization Monitors Synchronization Examples 小结和作业

. . 作业

华夏班:6.3,6.11,6.13 非华夏班:

临界区问题的解答必须满足的三个要求是什么? 7.1,7.8

陈香兰 操作系统原理与设计

slide-35
SLIDE 35

. . . . . .

Classical Problems of Synchronization Monitors Synchronization Examples 小结和作业

. . . . . . . 谢谢!

陈香兰 操作系统原理与设计