下載/瀏覽Download
Download
Report
Transcript 下載/瀏覽Download
The Critical-Section(臨界區)Problem
n processes all competing to use some
shared data.
Each process has a code segment, called
critical section, in which the shared data is
accessed.
Problem – ensure that when one process is
executing in its critical section, no other
process is allowed to execute in its critical
section.
EOS STUT
1
進入臨界區(Critical-Section)間的條件
1. 互斥(Mutual Exclusion)
當某個處理元在臨界區間內存取某些資源,則其他處
理元不能進入臨界區間去存取相同資源。
2. 行進(Progress)
當臨界區間內沒有處理元在執行,若有多個處理元要
求進入臨界區間執行,則只有那些不在執行剩餘程
式碼(Remainder Code)的處理元,才有資格被挑
選為下一個進入臨界區間的處理元,而且這個挑選
工作不能無限期的延遲下去。
3. 有限等待(Bounded Waiting)
當某個處理元要求進入臨界區間,一直到它獲得進入
臨界區間這段時間,允許其他處理元進入臨界區間
的次數有限制。
EOS STUT
2
Initial Attempts to Solve Problem –
2 processes
Only 2 processes, P0 and P1
General structure of process Pi (other
process Pj)
do {
entry section
//critical section
exit section
//remainder section
} while (1);
Processes may share some common
variables to synchronize their actions.
EOS STUT
3
Ref. only Algorithm 1 - (2 processes)
Shared variables:
Ref. only
int turn;
initially turn = 0
turn = i Pi can enter its critical section
Process Pi
do {
while (turn != i) ;
//critical section
turn = j;
//reminder section
} while (1);
Wrong way! : Satisfies mutual exclusion, but not
progress
EOS STUT
4
Algorithm 2 - (2 processes)
Shared variables
boolean flag[2];
initially flag [0] = flag [1] = false.
flag [i] = true Pi ready to enter its critical section
Process Pi
do {
flag[i] = true;
while (flag[j]) ;
//critical section
flag [i] = false;
//remainder section
} while (1);
Satisfies mutual exclusion, but not progress
requirement.
EOS STUT
5
Ref. only Algorithm 2 - (2 processes)
Consider the following trace:
P0 sets flag[0] to true
A context-switch occurs
P1 sets flag[1] to true
P1 loops in while
A context-switch occurs
P0 loops in while
Both P0 and P1 loop forever. This is the
livelock
EOS STUT
6
Algorithm 3 - Peterson's Algorithm
Combined shared variables of algorithms 1 and 2.
Process Pi
Ref. only
do {
flag [i] = true;
turn = j;
while (flag [j] && turn == j) ;
//critical section
flag [i] = false;
//remainder section
} while (1);
Meets all three requirements; solves the criticalsection problem for two processes.
EOS STUT
7
Bakery Algorithm
Ref. only
A Multiple-Process Solution.
Pertain to a centralized environment.
Notation <≡ lexicographical order (ticket #, process id
#)
(a,b) < (c,d) if a < c or if a = c and b < d
max (a0,…, an-1) is a number, k, such that k ≡ ai for i = 0, …, n –
1
Shared data
boolean choosing[n];
int number[n];
Data structures are initialized to false and 0 respectively
EOS STUT
8
Bakery Algorithm
Ref. only
保證達到互斥、行進、及有限等待條件。
就如同到銀行或商店接受服務一樣,任何一個顧
客欲接受服務,均需依序取一張號碼牌,並依號
碼牌由小至大依序接受服務;但是萬一很多人均
有相同號碼牌時,則依客戶姓名排序依序接受服
務。
EOS STUT
9
Bakery Algorithm
do {
Ref. only
choosing[i] = true;
number[i] = max(number[0], number[1], …, number [n – 1])+1;
choosing[i] = false;
for (j = 0; j < n; j++) {
while (choosing[j]) ;
while ((number[j] != 0) && ( number[j], j ) < ( number[i], i ) ) ;
}
//critical section
number[i] = 0;
//remainder section
} while (1);
//errata
If Pi is in its critical section and Pk (k != i) has already chosen its
number k != 0, then (number[i], i) < number[k], k)
EOS STUT
10
Bakery演算法
互斥
Ref. only
任何要求進入臨界區間的處理元,均會使用while迴圈查核
(number[j] != 0) && ( number[j], j ) < ( number[i], i )。
若最小號碼牌(number)的處理元僅有一個,則輪由這個處
理元進入臨界區間,其他處理元只好繼續在迴圈內等待;
若有多個處理元擁有最小號碼牌(均同號碼),則因為每個
處理元編號不同,故僅會有一個處理元進入臨界區間,其他
處理元繼續在迴圈中等待。因此這些處理元進入臨界區間是
互斥的。
EOS STUT
11
Bakery演算法
行進
Ref. only
由互斥的證明中,我們可以知道只有一個處
理元進入臨界區間,其他在迴圈等待之處理
元因為各擁有自己的號碼牌,且之後欲進入
臨界區間之處理元,其號碼牌均較大,所以
處理元進入臨界區間的次序是完全順序
(Totally Order),因此漸漸會有機會進
入臨界區間。
EOS STUT
12
Bakery演算法
有限等待
Ref. only
當某個處理元要求進入臨界區間時,此處理
元取得號碼牌具有完全順序之後,它在迴圈
中等待時,若有別的處理元亦要進入臨界區
間,則別的處理元取到的號碼牌一定比此處
理元大,別的處理元不可能插隊在完全順序
之內,因此這是一種先到先服務(First
Come First Serve)的排程,也就是在此處
理元之前進入臨界區間的其他處理元,總共
進入臨界區間的次數是有限的。
EOS STUT
13
Synchronization Hardware
Many systems provide hardware support for critical
section code
Uniprocessors – could disable interrupts
Currently running code would execute without preemption
Generally too inefficient on multiprocessor systems
Operating systems using this not broadly scalable
Modern machines provide special atomic hardware
instructions
Atomic = non-interruptable
Either test memory word and set value
Or swap contents of two memory words
EOS STUT
14
TestAndSet Instruction
Definition:
Ref. only
boolean TestAndSet (boolean *target)
{
boolean rv = *target;
*target = TRUE;
return rv:
}
EOS STUT
15