1 / 15

The Critical-Section( 臨界區 ) Problem

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.

carver
Download Presentation

The Critical-Section( 臨界區 ) Problem

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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.

  2. 進入臨界區(Critical-Section)間的條件 1. 互斥(Mutual Exclusion) 當某個處理元在臨界區間內存取某些資源,則其他處理元不能進入臨界區間去存取相同資源。 2. 行進(Progress) 當臨界區間內沒有處理元在執行,若有多個處理元要求進入臨界區間執行,則只有那些不在執行剩餘程式碼(Remainder Code)的處理元,才有資格被挑選為下一個進入臨界區間的處理元,而且這個挑選工作不能無限期的延遲下去。 3. 有限等待(Bounded Waiting) 當某個處理元要求進入臨界區間,一直到它獲得進入臨界區間這段時間,允許其他處理元進入臨界區間的次數有限制。

  3. 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.

  4. Algorithm 1 - (2 processes) Ref. only • Shared variables: • 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 Ref. only

  5. 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.

  6. Algorithm 2 - (2 processes) Ref. only • 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

  7. Algorithm 3 - Peterson's Algorithm • Combined shared variables of algorithms 1 and 2. • Process Pi 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 critical-section problem for two processes. Ref. only

  8. Bakery Algorithm • 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 Ref. only

  9. Bakery Algorithm • 保證達到互斥、行進、及有限等待條件。 • 就如同到銀行或商店接受服務一樣,任何一個顧客欲接受服務,均需依序取一張號碼牌,並依號碼牌由小至大依序接受服務;但是萬一很多人均有相同號碼牌時,則依客戶姓名排序依序接受服務。 Ref. only

  10. Bakery Algorithm do { 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 ) ) ; } //errata //critical section number[i] = 0; //remainder section } while (1); • 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) Ref. only

  11. Bakery演算法 • 互斥 • 任何要求進入臨界區間的處理元,均會使用while迴圈查核(number[j] != 0) && ( number[j], j ) < ( number[i], i )。 • 若最小號碼牌(number)的處理元僅有一個,則輪由這個處理元進入臨界區間,其他處理元只好繼續在迴圈內等待; • 若有多個處理元擁有最小號碼牌(均同號碼),則因為每個處理元編號不同,故僅會有一個處理元進入臨界區間,其他處理元繼續在迴圈中等待。因此這些處理元進入臨界區間是互斥的。 Ref. only

  12. Bakery演算法 • 行進 • 由互斥的證明中,我們可以知道只有一個處理元進入臨界區間,其他在迴圈等待之處理元因為各擁有自己的號碼牌,且之後欲進入臨界區間之處理元,其號碼牌均較大,所以處理元進入臨界區間的次序是完全順序(Totally Order),因此漸漸會有機會進入臨界區間。 Ref. only

  13. Bakery演算法 • 有限等待 • 當某個處理元要求進入臨界區間時,此處理元取得號碼牌具有完全順序之後,它在迴圈中等待時,若有別的處理元亦要進入臨界區間,則別的處理元取到的號碼牌一定比此處理元大,別的處理元不可能插隊在完全順序之內,因此這是一種先到先服務(First Come First Serve)的排程,也就是在此處理元之前進入臨界區間的其他處理元,總共進入臨界區間的次數是有限的。 Ref. only

  14. 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

  15. TestAndSet Instruction • Definition: boolean TestAndSet (boolean *target) { boolean rv = *target; *target = TRUE; return rv: } Ref. only

More Related