1 / 53

Chapter 7: Process Synchronization

Chapter 7: Process Synchronization. Background The Critical-Section Problem Synchronization Hardware Semaphores Classical Problems of Synchronization Java Synchronization Monitors Synchronization in Solaris 2 & Windows NT. Background.

kaori
Download Presentation

Chapter 7: Process Synchronization

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. Chapter 7: Process Synchronization • Background • The Critical-Section Problem • Synchronization Hardware • Semaphores • Classical Problems of Synchronization • Java Synchronization • Monitors • Synchronization in Solaris 2 & Windows NT Applied Operating System Concepts

  2. Background • Concurrent access to shared data may result in data inconsistency. • Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes. • Race condition • The situation where several processes access and manipulate shared data concurrently. • The final value of the shared data • depends upon which process finishes last. • To prevent race conditions, concurrent processes must be synchronized. Applied Operating System Concepts

  3. “count++” is not ATOMIC E - Box S - Box 2. operand 1. data 3. 연산 4. result 예 ------------------------- E-box S-box ------------------------- CPU Memory 컴퓨터 디스크 -------------------------- Seperation of Execution Box & Storage Box Applied Operating System Concepts

  4. Race Conditioncount++ is not ATOMIC E - Box E - Box S - Box count-- count++ count Applied Operating System Concepts

  5. Example of a Race Condition CPU1 CPU2 P1 P2 Memory X == 2 X = X – 1; X = X + 1; Load X, R1 Inc R1 Store X, R1 Load X, R2 Dec R2 Store X, R2 Interleaved execution을 하면? Applied Operating System Concepts

  6. The Critical-Section Problem CPU1 CPU2 P1 P2 Memory X = 2 P2 X = X – 1; : P1 X = X + 1; : • 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 whenone process is executing in its critical section, no other processesare allowed to execute in its critical section. Applied Operating System Concepts

  7. 해결법의 충족 조건 • Mutual Exclusion (모두 함께 들어감) • If process Pi is executing in its critical section, then • no other processes can be executing in their critical sections. • Progress(모두 못 들어감 CS 밖에서 남을 못들어가게?) If no process is executing in its critical section and some processeswish to enter their critical section, then those outside C.S. competes to enter C.S. and this selection cannot be postponed indefinitely. (excessive blocking) • Bounded Waiting(한 없이 기다림) Starvation 방지 • No assumption concerning relative speed of the n processes. Applied Operating System Concepts

  8. Initial Attempts to Solve Problem • Only twoprocesses, P0 and P1 • General structure of process Pi(other process Pj) • Each process: do { entry section critical section exit section remainder section } while (1); • Processes may share some common variables to synchronize their actions. Applied Operating System Concepts

  9. Algorithm 1 • Shared variables: • int turn;initially turn = 0  Pican enter its critical section if (turn == j) • Process P0 do { while (turn !=0) ; /* My turn? */ critical section turn = 1; /* Now it’s your turn */ remainder section } while (1); • Satisfies mutual exclusion, but not progress • (즉 과잉양보 – 반드시 교대로 들어가야만 함 -- swap-turn • 그가 turn 을 내값으로 바꿔줘야만 내가 들어갈 수 있음 • 프로세스가 보다 자주 crticial section을 들어가야 한다면?) noop Applied Operating System Concepts

  10. Algorithm 2 • Shared variables • boolean flag[2];initially flag [모두] = false; /*false means – not in CS*/ • “PA ready to enter its critical section” if (flag [A] == true) • Process Pi do { flag[me] = true; /* Pretend I am in */ while (flag[him]) ; /* Is he also in? then wait*/critical section flag [me] = false; /* I am out now*/ remainder section } while (1); • Satisfies mutual exclusion, but not progress requirement. • 둘 다 끊임 없이 양보 (동시에 true 를 set --- 상대방 check) noop Applied Operating System Concepts

  11. Algorithm 3 (Peterson’s Algorithm) • Combined shared variables of algorithms 1 and 2. • Process Pi do { flag [me]= true; /* My intention is to enter …. */ turn = him; /* Set to his turn-빠를수록 양보 */ while (flag [him] and turn ==him) ;/* wait only if …*/ critical section flag [me] = false; remainder section } while (1); • Meets all three requirements; solves the critical-section problem for two processes. • Busy Waiting! (계속 CPU와 memory 를 쓰면서 wait) • Case (1) only I try (2) 동시 try (3) 그가 이미 IN (4) CPU speed 차이 Pi: noop Applied Operating System Concepts

  12. Dekker’s Algorithm • It was the first provably-correct solution to the critical section problem. • varflag: array [0..1] ofboolean;turn; • repeatflag[i] := true;whileflag[j] doifturn = jthenbeginflag[i] := false;whileturn = jdono-op;flag[i] := true;end;                critical sectionturn := j;flag[i] := false;                remainder sectionuntilfalse; Applied Operating System Concepts

  13. Synchronization Hardware TestAndSet(a) 1. Read 2. TRUE Q: Make machine instruction indivisible for every ALU operation? lock memory (load, add, store) add, sub, …Too many! • Test and modify the content of a word atomically. [1] return value = a’s original value [2] set a = true boolean TestAndSet(boolea &target) { boolean rv=target /* rv: return value */ target= true; return rv; } Memory a Applied Operating System Concepts

  14. Mutual Exclusion with Test-and-Set while(cond) do { }; • Shared data: boolean lock = false; • Process Pi do { 1. R.V. 2. TRUE while (TestAndSet(lock)) ; critical section lock = false; remainder section } T lock? noop F noop Memory a TestAndSet(a) 1. Read ** Compare the code readability: Peterson’s algorithm test & set instruction 2. TRUE Applied Operating System Concepts

  15. Synchronization Hardware • Atomically swap two variables. void Swap(boolean &a, boolean &b) { boolean temp = a; a = b; b = temp; } Applied Operating System Concepts

  16. Mutual Exclusion with Swap • Shared data (initialized to false): boolean lock; boolean waiting[n]; • Process Pi do { /* key: me lock: he */ key = true; /* My intention */ while (key == true) Swap(lock,key); critical section Did he set lock ? lock = false; remainder section } Applied Operating System Concepts

  17. Semaphores T S  0? noop F • Semaphore S • integer variable • can only be accessed via two indivisible (atomic) operations P, V P (S):while (S 0) do no-op ;S--; If positive, decrement-&-enter. Otherwise, wait until it gets positive (busy-wait) V (S): S++; i.e. wait Applied Operating System Concepts

  18. Critical Section of n Processes • Shared data: semaphore mutex; // initially 1 meaning no one is in CS*/ • Process Pi: P(mutex); /* If positive, dec-&-enter, Otherwise, wait. */ critical section V(mutex); /* Increment semaphore */remainder section } while (1); What if many tries P(S) simultaneously? ==> system arbitrates (bus, memory, OS, …) Q: busy-wait efficient? => No block it -- if he has to wait. Wakeup him later (next page) CPU CPU Memory S Applied Operating System Concepts

  19. Block / Wakeup Implementation • Define a semaphore as a record typedef struct { int value; /* semaphore */ struct process *L; /*process wait queue*/ } semaphore; • Assume two simple operations: • block kernel suspends the process that invoked P(S) itself. Put this process’ PCB into wait queue (semaphore) • wakeup(P) V(S) resumes the execution of a blocked process P. (Put this process’ PCB into ready queue) struct semaphore S : value: 0 L PCB PCB PCB Applied Operating System Concepts

  20. Implementationblock/wakeup version of P() & V() • Semaphore operations now defined as P(S): S.value--; /* prepare to enter – i.e. decrement*/ if (S.value < 0) { /* Oops, negative, I cannot enter*/ add this process to S.L;block; } integer semaphore 값 = 자원의 수, 대기 process 수 used for process synchronization V(S): S.value++; if (S.value <= 0) { /* 절대값은 queue 의 길이임 */ remove a process P from S.L;wakeup(P); } Semaphore S: value: 0 L PCB PCB PCB Applied Operating System Concepts

  21. Which is better? • 비교: Busy-wait v.s. Block-wakeup • Block-wakeup overhead v.s. Crtitical section 길이 Applied Operating System Concepts

  22. Semaphore as a General Synchronization Tool • Two processes and two critical sections A & B • (Execute B in Pj) only after (Pi executed A) • semaphoreS initialized to 1 • Code: Pi Pj P(S) : A(C.S.) P(S) V(S) B(C.S.) V(S) Here S is binary semaphore - used for mutual exclusion 1. Pi enters, making S=0 3. CS를 나가며 상대방에 알려줌 (S++) 2. 여기서 기다림(S>0?) 4. 이제 나의 c.s. B 로 진입 (진입시 S = 0 again) Applied Operating System Concepts

  23. Bounded-Buffer Shared-Memory Solution New data arrived Any empty buf? Fill it Produce full buf Any full buf exist? Get it Produce empty buf Producer in Consumer out If yes, but ... Nfull-buf If yes, but ... Can I access shared variable now? Buffer In shared memory Can I access shared variable now? Nempty-buf Shared variable: buf, count ==> Need binary semaphore Resource count: # of full buf ==> Need integer semaphore # of empty buf Applied Operating System Concepts

  24. Producer buf 에 공간? (empty-buf) buf에 기록 (critical section) integer semaphore N_empty-buf N_full-buf binary semaphore S_mutex P(N_ empty-buf) P(S_mutex) buf에 write V(S_mutex) V(N_full-buf) *** binary semaphore는 앞뒤로 *** integer semaphore는 대각선으로 Consumer buf에 일감? (full-buf) buf에서 떼감 (critical section) P(N_full-buf) P(S_mutex) buf 를 read V(S_mutex) V(N_empty-buf) Here N is integer semaphore - used for resource count Buf 有無 Buf 값 (eg link) Applied Operating System Concepts

  25. Two Types of Semaphores • Counting semaphore • integer value can range over an unrestricted domain. • Binary semaphore • integer value can range only between 0 and 1; • can be simpler to implement. • Can implement a counting semaphore S as a binary semaphore. Applied Operating System Concepts

  26. OS 에서 critical section은 언제 발생하는가? • 1. Interrupt routine과 kernel 사이에 • 2. Process 간 CPU context switch를 하려는 시점이 kernel이 system call 을 수행하고 있는 한가운데일 경우 • 3. Multiprocessor -- shared memory 내의 kernel data Applied Operating System Concepts

  27. OS 와 CS 문제발생 (1/3)interrupt handler v.s. kernel Count-- Interrupt Kernel Count++ 3. Store 2. Inc 1. read dis/enable intrp Applied Operating System Concepts

  28. OS 와 CS 문제발생 (2/3)Preempt a process running in kernel? u k u k PA System call Return from kernel System call 두 프로세스의 user space 간에는 data sharing 이 없음 그러나 system call 을 하는 동안에는 kernel data 를 access 하게 됨 (share) 이 작업 중간에서CPU를 preempt 해가면 interleaved execution 이 됨 Applied Operating System Concepts

  29. If you preempt CPU while in kernel mode ….. (2) PB needs CPU. PA는 CPU를 preempt 당함 (while in kernel !) (1) System call read() u k k u PA Co …. …. …. …. … …. … unt++ (3) CPU 되돌려 받음 u k PB Count++ **** Do not preempt CPU if CPU was executing in kernel mode (at interrupt) **** UNIX waits until exit from kernel (until system call is finished) Applied Operating System Concepts

  30. Multiprocessor PC=100 PC=3000 PC=2000 PC=7774 CPU CPU CPU CPU hwp 박 김 OS Applied Operating System Concepts

  31. OS 와 CS 문제발생 (3/3)multiprocessor CPU Memory CPU count++ count count-- P(Scount) V(Scount) Who stores count the last? – race condition interrupt en/disable does not work here CPU is never preempted here Entire kernel is one big C.S. - only one CPU can enter kernel at one time or Guard each kernel shared variable with respective semaphore then kernel consists of many small critical sections Applied Operating System Concepts

  32. Multiprocessor비대칭적 Master: request to master queue system calls execute one by one Bottleneck (성능, 신뢰성) Has all I/O devices PC=100 PC=3000 PC=2000 CPU CPU CPU CPU hwp 박 Master OS 김 All I/O Devices Master CPU 전용Local Memory OS kernel Applied Operating System Concepts

  33. Multiprocessor대칭적 (SMP) PC=100 PC=3000 PC=2000 PC=7774 CPU CPU CPU CPU Before sys call --P(S) --- system cal --- After sys call --V(S) hwp 박 Before sys call --P(S) --- system cal --- After sys call --V(S) 김 Semaphore S OS Applied Operating System Concepts

  34. Classical Problems of Synchronization • Bounded-Buffer Problem • Readers and Writers Problem scheduling • Dining-Philosophers Problem deadlock Applied Operating System Concepts

  35. Bounded-Buffer Problem Shared data semaphore full = 0, empty = n, mutex =1; Consumer Producer do { P(full) P(mutex); … remove an item from buffer to nextc … V(mutex); V(empty); … consume the item in nextc … } while (1); do { … produce an item in nextp … P(empty); P(mutex); … add nextp to buffer … V(mutex); V(full); } while (1); Applied Operating System Concepts

  36. Producer & Consumer(revisited)

  37. em in out Input R-I.H. Buffer Pool Main W-I.H. Output Applied Operating System Concepts

  38. 3 classes of buf [em] empty [in] buf with input data waiting to be processed [out] buf with output data waiting to be output 5 routines R I.H. read input data into buf W I.H. writes output data from buf Input gets next input full data for compute Main computes, generates output Output output data into buf Applied Operating System Concepts

  39. Producer M,O I M Consumer M, I M O Mutual-excl Semaphore Mem Min Mout Counting Semaphore Cem Cin Cout Buffer pointer Bem Bin Bout [em] [in] [out] Applied Operating System Concepts

  40. P(Cin) Get Input buf P(Min) Take input buf V(Min) P(Mem) Rel Empty buf release empty buf V(Mem) V(Cem) ***** main computation **** P(Cem) Get Empty buf P(Mem) Take empty buf V(Mem) P(Mout) Get Output buf Rel output input buf V(Mout) V(Cout) Main: Applied Operating System Concepts

  41. P(Cem) Any empty buf available? P(Mem) delete from empty list V(Mem) fill it now P(Min) produce input buf insert into input list V(Min) V(Cin) R IH: Applied Operating System Concepts

  42. Readers-Writers Problem R DB R W DB is accessed by many When one writes to DB - exclusive access (semaphore db) <홀로 DB access, others blocked?> Priority? Waiting: N readers (int readcount, semaphore mutex)), 1 writer <for 개수> < for mutual exclusion> A. Let all readers finish (concurrently), then writer start B. Let writer finish (urgent), then readers start(계속 도착) Try A. solution first: If one reader starts reading, let all other readers start read too. If last reader finishes reading, let the writer start. semaphore mutex = 1, db = 1; int readcount = 0 Reader: first reader -> try P(db), last reader -> do V(db) [readcount access] is critical section, surround it by P() V() R OR Applied Operating System Concepts

  43. Readers-Writers Problem Shared data R DB (db) readcount (mutex) R semaphore mutex = 1, db = 1; int readcount = 0 W R Reader Writer P(mutex); readcount++; /* this is shared data */ if (readcount == 1) P(db); /* is writer in?*/ /* no, block writer*/ V(mutex); … reading is performed … P(mutex); readcount--; if (readcount == 0) V(db); ); /* last reader?*/ /*enable writer*/ V(mutex): P(db); … writing is performed … V(db); *** Starvation 문제는? Applied Operating System Concepts

  44. Dining-Philosophers Problem[Think:Eat] -- eat 하려면 반드시 양쪽 젓갈을 가져야 Shared data semaphore chopstick[5]; // Initially all values are 1 Philosopher i do { P(chopstick[i]) P(chopstick[(i+1) % 5]) … eat … V(chopstick[i]); V(chopstick[(i+1) % 5]); … think … } while (1); ==> 문제는 deadlock! Applied Operating System Concepts

  45. Deadlock and Starvation • Deadlock processes are waiting indefinitely for an event that never occur • Let S and Q be two semaphores initialized to 1 P0P1 P(S); P(Q); 하나씩 차지 P(Q); P(S); 상대방 것을 요구   V(S); V(Q); 여기와야 release함 V(Q) V(S); Applied Operating System Concepts

  46. Deadlock - Remedies? • Pick up only both 컴퓨터는 한번에 한 Var씩 set/test • Asymmetric coding • Let Odd philosopher - try Left first • Even - try Right first • 프로세스 위치에 따라 다른 코딩 방식 • Allow at most 4 to sit together • 5 명이 각자가 한개씩만 가진 상황은 없어짐 • Eat 상태를 최대 4명만 경합하도록 제한 Applied Operating System Concepts

  47. Semaphore 의 문제점 • Difficult to code • Difficult to prove correctness ** errors are not reproducible ** error are observed rarely • Requires voluntary cooperation • Single misuse affect entire system Applied Operating System Concepts

  48. MonitorsAbstract data type과 유사private data -- public methods • High-level language synchronization construct that allows the safe sharing of an abstract data type . monitor monitor-name { shared variable declarations procedure bodyP1(…) { . . . } procedurebodyP2 (…) { . . . } { initialization code } } Private data Public Methods Applied Operating System Concepts

  49. Monitors x Insert: x.wait Delete: x.signal • To allow a process to wait within the monitor, a condition variable must be declared, as condition x, y; • Condition variable can only be used with the operations wait and signal. • The operation x.wait();means that the process invoking this operation is suspended until another process invokes x.signal(); • The x.signal operation resumes exactly one suspended process. If no process is suspended, then the signal operation has no effect. 우선순위: “I signal & I wait” “I signal & I continue” Applied Operating System Concepts

  50. Schematic View of a Monitor Only one thread (process) can be within Monitor at a time Applied Operating System Concepts

More Related