Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
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 7.1 Silberschatz, Galvin and Gagne 2002 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 7.2 Silberschatz, Galvin and Gagne 2002 “count++” is not ATOMIC S - Box E - Box 2. operand 1. 3. 연산 data 4. result Seperation of Execution Box & Storage Box Applied Operating System Concepts 예 ------------------------E-box S-box ------------------------CPU Memory 컴퓨터 디스크 -------------------------7.3 Silberschatz, Galvin and Gagne 2002 Race Condition count++ is not ATOMIC E - Box E - Box S - Box count-- count++ count Applied Operating System Concepts 7.4 Silberschatz, Galvin and Gagne 2002 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 7.5 Silberschatz, Galvin and Gagne 2002 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 processes are allowed to execute in its critical section. CPU1 CPU2 P1 P2 Memory X=2 P1 P2 X = X + 1; X = X – 1; : : Applied Operating System Concepts 7.6 Silberschatz, Galvin and Gagne 2002 해결법의 충족 조건 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 processes wish 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 (한 없이 기다림) 7.7 Silberschatz, Galvin and Gagne 2002 Initial Attempts to Solve Problem Only two processes, 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 7.8 Silberschatz, Galvin and Gagne 2002 Algorithm 1 Shared variables: int turn; initially turn = 0 Pi can enter its critical section if (turn == j) Process P0 noop do { while (turn != 0) ; /* My turn? */ critical section /* Now it’s your turn */ turn = 1; remainder section } while (1); Satisfies mutual exclusion, but not progress (즉 과잉양보 – 반드시 교대로 들어가야만 함 -- swap-turn Applied Operating System Concepts 그가 turn 을 내값으로 바꿔줘야만 내가 들어갈 수 있음 프로세스가 보다 자주 crticial section을 들어가야 한다면?) 7.9 Silberschatz, Galvin and Gagne 2002 Algorithm 2 Shared variables boolean initially flag[2]; flag [모두] = false; /*false means – not in CS*/ “PA ready to enter its critical section” if (flag [A] == true) Process Pi do { noop 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) Applied Operating System Concepts 7.10 Silberschatz, Galvin and Gagne 2002 Algorithm 3 (Peterson’s Algorithm) Combined shared variables of algorithms 1 and 2. Process Pi 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; noop 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 차이 Applied Operating System Concepts 7.11 Silberschatz, Galvin and Gagne 2002 Dekker’s Algorithm It was the first provably-correct solution to the critical section problem. var flag: array [0..1] of boolean; turn; repeat flag[i] := true; while flag[j] do if turn = j then begin flag[i] := false; while turn = j do no-op; flag[i] := true; end; critical section turn := j; flag[i] := false; remainder section until false; Applied Operating System Concepts 7.12 Silberschatz, Galvin and Gagne 2002 Synchronization Hardware 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 TestAndSet(a) Memory 1. Read a boolean TestAndSet(boolea &target) { boolean rv=target /* rv: return value */ target= true; return rv; } 2. TRUE Applied Operating System Concepts 7.13 Silberschatz, Galvin and Gagne 2002 Mutual Exclusion with Test-and-Set Shared data: while(cond) do { }; boolean lock = false; Process Pi do { 1. R.V. 2. TRUE while (TestAndSet(lock)) ; critical section noop lock = false; remainder section } lock? T F Memory 1. Read a 2. TRUE Applied Operating System Concepts TestAndSet(a) ** Compare the code readability: Peterson’s algorithm test & set instruction 7.14 Silberschatz, Galvin and Gagne 2002 noop Synchronization Hardware Atomically swap two variables. void Swap(boolean &a, boolean &b) { boolean temp = a; a = b; b = temp; } Applied Operating System Concepts 7.15 Silberschatz, Galvin and Gagne 2002 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 7.16 Silberschatz, Galvin and Gagne 2002 Semaphores Semaphore S integer variable can only be accessed via two indivisible (atomic) operations P, V P (S): while (S 0) do no-op ; S--; i.e. wait If positive, decrement-&-enter. Otherwise, wait until it gets positive (busy-wait) V (S): S++; S 0? T noop F Applied Operating System Concepts 7.17 Silberschatz, Galvin and Gagne 2002 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); CPU What if many tries P(S) simultaneously? ==> system arbitrates (bus, memory, OS, …) CPU Memory S Q: busy-wait efficient? => No block it -- if he has to wait. Wakeup him later (next page) Applied Operating System Concepts 7.18 Silberschatz, Galvin and Gagne 2002 Block / Wakeup Implementation Define a semaphore as a record typedef struct { int struct process } semaphore; value; /* semaphore */ *L; /*process wait queue*/ 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 Applied Operating System Concepts PCB 7.19 PCB Silberschatz, Galvin and Gagne 2002 Implementation block/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 Applied Operating System Concepts PCB PCB 7.20 PCB Silberschatz, Galvin and Gagne 2002 Which is better? 비교: Busy-wait v.s. Block-wakeup Block-wakeup overhead v.s. Crtitical section 길이 Applied Operating System Concepts 7.21 Silberschatz, Galvin and Gagne 2002 Semaphore as a General Synchronization Tool Two processes and two critical sections A & B (Execute B in Pj) only after (Pi executed A) semaphore S initialized to 1 Code: 1. Pi enters, making S=0 Pi P(S) 3. CS를 나가며 상대방에 알려줌 (S++) A(C.S.) V(S) Pj : 2. 여기서 기다림(S>0?) P(S) B(C.S.) 4. 이제 나의 c.s. B 로 진입 (진입시 S = 0 again) V(S) Here S is binary semaphore - used for mutual exclusion Applied Operating System Concepts 7.22 Silberschatz, Galvin and Gagne 2002 New data arrived Bounded-Buffer Shared-Memory Solution Any empty buf? Fill it Produce full buf If yes, but ... Any full buf exist? Get it Produce empty buf Producer in Can I access shared variable now? Nfull-buf Consumer out Buffer In shared memory If yes, but ... Can I access shared variable now? Nempty-buf Shared variable: buf, count ==> Need binary semaphore Resource count: # of full buf # of empty buf ==> Need integer semaphore Applied Operating System Concepts 7.23 Silberschatz, Galvin and Gagne 2002 Here N is integer semaphore - used for resource count Producer Consumer buf 에 공간? (empty-buf) buf에 일감? (full-buf) buf에 기록 (critical section) buf에서 떼감 (critical section) integer semaphore binary semaphore Buf 有無 Buf 값 (eg link) N_empty-buf N_full-buf S_mutex P(N_ empty-buf) P(N_full-buf) P(S_mutex) P(S_mutex) buf 를 read V(S_mutex) V(N_empty-buf) buf에 write V(S_mutex) V(N_full-buf) *** binary semaphore는 앞뒤로 *** integer semaphore는 대각선으로 Applied Operating System Concepts 7.24 Silberschatz, Galvin and Gagne 2002 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 7.25 Silberschatz, Galvin and Gagne 2002 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 7.26 Silberschatz, Galvin and Gagne 2002 OS 와 CS 문제발생 (1/3) interrupt handler v.s. kernel Count-- Interrupt Kernel Count++ 3. Store 2. Inc 1. read Applied Operating System Concepts 7.27 dis/enable intrp Silberschatz, Galvin and Gagne 2002 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 7.28 Silberschatz, Galvin and Gagne 2002 If you preempt CPU while in kernel mode ….. (2) PB needs CPU. PA는 CPU를 preempt 당함 (while in kernel !) (1) System call read() PA u k k u 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 7.29 Silberschatz, Galvin and Gagne 2002 Multiprocessor PC=100 PC=3000 PC=2000 PC=7774 CPU CPU CPU CPU hwp 박 김 OS Applied Operating System Concepts 7.30 Silberschatz, Galvin and Gagne 2002 OS 와 CS 문제발생 (3/3) multiprocessor CPU Memory CPU P(Scount) count++ count count-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 7.31 Silberschatz, Galvin and Gagne 2002 Multiprocessor 비대칭적 PC=100 PC=3000 PC=2000 CPU CPU CPU Master: request to master queue system calls execute one by one Bottleneck (성능, 신뢰성) Has all I/O devices CPU hwp 박 Master OS 김 All I/O Devices Master CPU 전용 Local Memory OS kernel Applied Operating System Concepts 7.32 Silberschatz, Galvin and Gagne 2002 Multiprocessor 대칭적 (SMP) PC=100 PC=3000 PC=2000 PC=7774 CPU CPU CPU CPU hwp Before sys call --P(S) --- system cal --After sys call --V(S) 박 김 Semaphore S Applied Operating System Concepts Before sys call --P(S) --- system cal --After sys call --V(S) OS 7.33 Silberschatz, Galvin and Gagne 2002 Classical Problems of Synchronization Bounded-Buffer Problem Readers and Writers Problem scheduling Dining-Philosophers Problem deadlock Applied Operating System Concepts 7.34 Silberschatz, Galvin and Gagne 2002 Bounded-Buffer Problem Shared data semaphore full = 0, empty = n, mutex =1; Producer Consumer do { … produce an item in nextp … P(empty); P(mutex); … add nextp to buffer … V(mutex); V(full); do { P(full) P(mutex); … remove an item from buffer to nextc … V(mutex); V(empty); … consume the item in nextc … } while (1); } while (1); Applied Operating System Concepts 7.35 Silberschatz, Galvin and Gagne 2002 Producer & Consumer (revisited) Input Buffer Pool R-I.H. em Main in W-I.H. out Output Applied Operating System Concepts 7.37 Silberschatz, Galvin and Gagne 2002 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 Applied Operating System Concepts R I.H. read input data 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 7.38 into buf Silberschatz, Galvin and Gagne 2002 Mutual-excl Counting Buffer Semaphore Semaphore pointer Producer Consumer [em] Mem Cem Bem M,O M, I [in] Min Cin Bin I M Mout Cout Bout M O [out] Applied Operating System Concepts 7.39 Silberschatz, Galvin and Gagne 2002 Main: P(Cin) Get Input buf P(Min) Take input buf V(Min) P(Mem) release empty buf V(Mem) Rel Empty buf V(Cem) ***** main computation **** P(Cem) P(Mem) Take empty buf V(Mem) P(Mout) Rel output input buf V(Mout) V(Cout) Applied Operating System Concepts 7.40 Get Empty buf Get Output buf Silberschatz, Galvin and Gagne 2002 R IH: 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) Applied Operating System Concepts 7.41 Silberschatz, Galvin and Gagne 2002 Readers-Writers Problem W R DB 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 OR 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 int mutex = 1, db = 1; readcount = 0 Reader: first reader -> try P(db), last reader -> do V(db) [readcount access] is critical section, surround it by P() V() Applied Operating System Concepts 7.42 Silberschatz, Galvin and Gagne 2002 R R 도착) Readers-Writers Problem R Shared data W semaphore mutex = 1, db = 1; int readcount = 0 Writer P(db); … writing is performed … V(db); *** Starvation 문제는? Applied Operating System Concepts DB (db) R readcount (mutex) R Reader 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): 7.43 Silberschatz, Galvin and Gagne 2002 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 7.44 Silberschatz, Galvin and Gagne 2002 Deadlock and Starvation Deadlock processes are waiting indefinitely for an event that never occur Let S and Q be two semaphores initialized to 1 P0 P(S); P(Q); V(S); V(Q) Applied Operating System Concepts P1 P(Q); P(S); V(Q); V(S); 7.45 하나씩 차지 상대방 것을 요구 여기와야 release 함 Silberschatz, Galvin and Gagne 2002 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 7.46 Silberschatz, Galvin and Gagne 2002 Semaphore 의 문제점 Difficult to Difficult to Requires Single misuse Applied Operating System Concepts code prove correctness ** errors are not reproducible ** error are observed rarely voluntary cooperation affect entire system 7.47 Silberschatz, Galvin and Gagne 2002 Monitors Abstract data type과 유사 private data -- public methods High-level language synchronization construct that allows the safe sharing of an abstract data type . Private data monitor monitor-name { shared variable declarations procedure body P1 (…) { ... } procedure body P2 (…) { ... } Public Methods { initialization code } } Applied Operating System Concepts 7.48 Silberschatz, Galvin and Gagne 2002 Monitors x To allow a process to wait within the monitor, Insert: x.wait Delete: x.signal 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 “I signal & I wait” “I signal & I continue” x.signal(); The x.signal operation resumes exactly one suspended process. If no process is suspended, then the signal operation has no effect. Applied Operating System Concepts 7.49 Silberschatz, Galvin and Gagne 2002 Schematic View of a Monitor Only one thread (process) can be within Monitor at a time Applied Operating System Concepts 7.50 Silberschatz, Galvin and Gagne 2002 Monitor With Condition Variables Applied Operating System Concepts 7.51 Silberschatz, Galvin and Gagne 2002 Dining Philosophers Example monitor dining_philosopher { void test (int i) { enum {thinking, eating if ( (state[(i + 4) % 5] != eating) && hungry (3rd state), } state[5]; (state[i] == hungry) && condition self[5]; /*wait here*/ (state[(i + 1) % 5] != eating)){ /OK/ void pickup(int i) { state[i] = eating; state[i] = hungry; /* 3rd state */ self[i].signal(); / *wakeup Pi */ test(i); } /* used for putdown L & R */ if (state[i] != eating) } self[i].wait(); /*wait here*/ void init() { } for (int i = 0; i < 5; i++) state[i] = thinking; void putdown(int i) { } state[i] = thinking; // test left and right neighbors Each Philosopher: test((i+4) % 5); /*if L is waiting*/ { pickup(i); test((i+1) % 5); eat(); } putdown(i); } think(); } while(1) Applied Operating System Concepts 7.52 Silberschatz, Galvin and Gagne 2002 entry queue condition variable queue shared data operations pickup() putdown() test() Applied Operating System Concepts 7.53 Silberschatz, Galvin and Gagne 2002