Notes for OSC

Ch7 Process Synchronization

2018-04-09  本文已影响16人  pc99

Chapter 7: Process Synchronization

Bacground

Bound-buffer

Race condition

To prevent race condition, concurrent processes must be synchronized.

The Critical-section problem

Solution Critical

2-Processes:

Algorithm 0:

Algorithm 1:

Algorithm 2:

Algorithm 3

N-processes — barkery algorithm

Hardware

TestAndSet

Swap

Bounded-Waiting Mutual Exclusion with TestAndSet

Semaphores

Critical section of N processed

Implementation

Deadblock starvetion

Types

Binary semaphores implementing ==S== as a counting semaphore

Classical problems of synchronization

Bounded-buffer problem

shared data

// the producter produce the item

// which is consumed by the consumer.

// The number of buffers is bounded.

Semaphore

mutex = 1; // To protect the buffer pool

full = 0; // To count the number of full buffers

empty = 0; // To count the number of empty buffers.

Why do not just make one parameter to count the number of buffer?

To protect the buffer which is in the condition between full and empty?

producer process

while(1){
    ...
    produce an item in next p
    ...
    wait(empty); //wait--
    
    wait(mutex); //protect the buffer pool
    ...
    add next p to buffer;
    ...
    signal(mutex);
    
    signal(full); //full++
}

consumer process

while(1){
    wait(full); //full--
    
    wait(mutex); //protect the buffer pool
    ...;
    remove one buffer to next c;
    signal(mutex);
    
    signal(empty);//empty++
    ...
    comsume the item in next c;
    ...
}

Readers-writers problem

Readers First

Dinging-Philosophers problem

ch7_2.png

上一篇下一篇

猜你喜欢

热点阅读