Upload
kaori
View
71
Download
0
Embed Size (px)
DESCRIPTION
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. - PowerPoint PPT Presentation
Citation preview
Silberschatz, Galvin and Gagne 20027.1Applied Operating System Concepts
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
Silberschatz, Galvin and Gagne 20027.2Applied Operating System Concepts
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.
Silberschatz, Galvin and Gagne 20027.3Applied Operating System Concepts
E - Box S - Box
1. data3. 연산
2. operand
4. result
예-------------------------E-box S-box-------------------------
CPU Memory
컴퓨터 디스크--------------------------
“count++” is not ATOMIC
Seperationof
Execution Box&
Storage Box
Silberschatz, Galvin and Gagne 20027.4Applied Operating System Concepts
Race Conditioncount++ is not ATOMIC
E - Box
S - Box
count
count++
E - Box
count--
Silberschatz, Galvin and Gagne 20027.5Applied Operating System Concepts
Example of a Race Condition
Memory
CPU1 CPU2
P1 P2
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 을 하면 ?
Silberschatz, Galvin and Gagne 20027.6Applied Operating System Concepts
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.
P1
X = X + 1;
:
P2
X = X – 1;
:
Memory
CPU1 CPU2
P1 P2
X = 2
Silberschatz, Galvin and Gagne 20027.7Applied Operating System Concepts
해결법의 충족 조건
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.
Silberschatz, Galvin and Gagne 20027.8Applied Operating System Concepts
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.
Silberschatz, Galvin and Gagne 20027.9Applied Operating System Concepts
Algorithm 1
Shared variables: int turn;
initially turn = 0 Pi can 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
Silberschatz, Galvin and Gagne 20027.10Applied Operating System Concepts
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
Silberschatz, Galvin and Gagne 20027.11Applied Operating System Concepts
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 sectionflag [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
Silberschatz, Galvin and Gagne 20027.12Applied Operating System Concepts
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 sectionuntil false;
Silberschatz, Galvin and Gagne 20027.13Applied Operating System Concepts
Synchronization Hardware
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
1. Read
2. TRUE
TestAndSet(a)
Q: Make machine instruction indivisible for every ALU operation?lock memory (load, add, store) add, sub, …Too many!
Silberschatz, Galvin and Gagne 20027.14Applied Operating System Concepts
Mutual Exclusion with Test-and-Set
Shared data: boolean lock = false;
Process Pi
do { 1. R.V. 2. TRUE
while (TestAndSet(lock)) ;
critical section
lock = false;
remainder section
}
T
F
lock? noop
noop
Memory
a TestAndSet(a)
1. Read
2. TRUE
while(cond) do { };
** Compare the code readability: Peterson’s algorithm
test & set instruction
Silberschatz, Galvin and Gagne 20027.15Applied Operating System Concepts
Synchronization Hardware
Atomically swap two variables.
void Swap(boolean &a, boolean &b) {
boolean temp = a;
a = b;
b = temp;
}
Silberschatz, Galvin and Gagne 20027.16Applied Operating System Concepts
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
}
Silberschatz, Galvin and Gagne 20027.17Applied Operating System Concepts
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--;
If positive, decrement-&-enter.
Otherwise, wait until it gets positive (busy-wait)
V (S):
S++;
i.e. wait
T
F
S 0? noop
Silberschatz, Galvin and Gagne 20027.18Applied Operating System Concepts
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
MemoryS
CPU
Silberschatz, Galvin and Gagne 20027.19Applied Operating System Concepts
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)
value: 0
PCB PCB PCB
struct semaphore S :
L
Silberschatz, Galvin and Gagne 20027.20Applied Operating System Concepts
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);
}
PCB PCB PCB
SemaphoreS: value: 0
L
Silberschatz, Galvin and Gagne 20027.21Applied Operating System Concepts
Which is better?
비교 : Busy-wait v.s. Block-wakeup
Block-wakeup overhead v.s. Crtitical section 길이
Silberschatz, Galvin and Gagne 20027.22Applied Operating System Concepts
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:
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)
Silberschatz, Galvin and Gagne 20027.23Applied Operating System Concepts
Bounded-Buffer Shared-Memory Solution
Producer in
BufferIn
shared memory
Consumer out
Any empty buf?Fill it
Produce full buf
Any full buf exist?Get it
Produce empty buf
Shared variable: buf, count ==> Need binary semaphore
Resource count: # of full buf ==> Need integer semaphore # of empty buf
Can I accessshared variable now? Can I access
shared variable now?
If yes, but ... If yes, but ...
New data arrived
Nfull-buf
Nempty-buf
Silberschatz, Galvin and Gagne 20027.24Applied Operating System Concepts
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)
Silberschatz, Galvin and Gagne 20027.25Applied Operating System Concepts
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.
Silberschatz, Galvin and Gagne 20027.26Applied Operating System Concepts
OS 에서 critical section 은 언제 발생하는가 ?
1. Interrupt routine 과 kernel 사이에
2. Process 간 CPU context switch 를 하려는 시점이 kernel 이 system call 을 수행하고 있는 한가운데일
경우
3. Multiprocessor -- shared memory 내의 kernel data
Silberschatz, Galvin and Gagne 20027.27Applied Operating System Concepts
OS 와 CS 문제발생 (1/3) interrupt handler v.s. kernel
Kernel
Interrupt
Count++
Count--
3. S
tore
2. Inc
1. read dis/enable intrp
Silberschatz, Galvin and Gagne 20027.28Applied Operating System Concepts
u uk k
System call
System call
Return fromkernel
PA
OS 와 CS 문제발생 (2/3) Preempt a process running in kernel?
두 프로세스의 user space 간에는 data sharing 이 없음그러나 system call 을 하는 동안에는 kernel data 를 access 하게 됨 (share)이 작업 중간에서 CPU 를 preempt 해가면 interleaved execution 이 됨
Silberschatz, Galvin and Gagne 20027.29Applied Operating System Concepts
u uk
(1) System call read()
PA
(2) PB needs CPU. PA 는 CPU 를 preempt 당함 (while in kernel !)
u k
PB
(3) CPU 되돌려 받음
k
Count++
Co …. …. …. …. … …. … unt++
If you preempt CPU while in kernel mode …..
**** Do not preempt CPU if CPU was executing in kernel mode (at interrupt)
**** UNIX waits until exit from kernel (until system call is finished)
Silberschatz, Galvin and Gagne 20027.30Applied Operating System Concepts
Multiprocessor
CPU CPUCPUCPU
OS
김
박
hwp
PC=100 PC=7774PC=2000PC=3000
Silberschatz, Galvin and Gagne 20027.31Applied Operating System Concepts
OS 와 CS 문제발생 (3/3) multiprocessor
CPU Memory CPU
count++ count count--
Who stores count the last? – race conditioninterrupt en/disable does not work hereCPU is never preempted here
Entire kernel is one big C.S. - only one CPU can enter kernel at one timeor
Guard each kernel shared variable with respective semaphorethen kernel consists of many small critical sections
P(Scount)
V(Scount)
Silberschatz, Galvin and Gagne 20027.32Applied Operating System Concepts
Multiprocessor비대칭적
CPU
CPU CPUCPU
OS 김
박
hwp
PC=100 PC=2000PC=3000
Master: request to master queue system calls execute one by one
Bottleneck ( 성능 , 신뢰성 )
Has all I/O devices
Master CPU전용 Local Memory
OS kernel
All I/O Devices
Master
Silberschatz, Galvin and Gagne 20027.33Applied Operating System Concepts
Multiprocessor대칭적 (SMP)
CPU CPUCPUCPU
OS
김
박
hwp
PC=100 PC=7774PC=2000PC=3000
Before sys call --P(S)--- system cal ---After sys call --V(S)
Before sys call --P(S)--- system cal ---After sys call --V(S)Semaphore
S
Silberschatz, Galvin and Gagne 20027.34Applied Operating System Concepts
Classical Problems of Synchronization
Bounded-Buffer Problem
Readers and Writers Problem scheduling
Dining-Philosophers Problem deadlock
Silberschatz, Galvin and Gagne 20027.35Applied Operating System Concepts
Bounded-Buffer Problem
semaphore full = 0, empty = n, mutex =1;
do { …produce an item in nextp …P(empty);P(mutex); …add nextp to buffer
…V(mutex);V(full);
} while (1);
do {
P(full)P(mutex); …remove an item from buffer to nextc …V(mutex);V(empty); …consume the item in nextc …
} while (1);
Shared data
Producer Consumer
Producer & Consumer(revisited)
Silberschatz, Galvin and Gagne 20027.37Applied Operating System Concepts
Input
Main
Output
R-I.H.
W-I.H.
Buffer Pool
out
in
em
Silberschatz, Galvin and Gagne 20027.38Applied Operating System Concepts
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
Silberschatz, Galvin and Gagne 20027.39Applied Operating System Concepts
Mutual-excl
Semaphore
Mem
Min
Mout
Counting
Semaphore
Cem
Cin
Cout
Buffer
pointer
Bem
Bin
Bout
Producer
M,O
I
M
Consumer
M, I
M
O
[em]
[in]
[out]
Silberschatz, Galvin and Gagne 20027.40Applied Operating System Concepts
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:
Silberschatz, Galvin and Gagne 20027.41Applied Operating System Concepts
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:
Silberschatz, Galvin and Gagne 20027.42Applied Operating System Concepts
Readers-Writers ProblemDB is accessed by manyWhen 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 startB. 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()
OR
W DB
R
R
R
Silberschatz, Galvin and Gagne 20027.43Applied Operating System Concepts
Readers-Writers Problem
semaphore mutex = 1, db = 1; int readcount = 0
Shared data
Writer Reader
P(db); …
writing is performed …
V(db);
*** Starvation 문제는?
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):
W DB(db)
R
R
R
readcount(mutex)
Silberschatz, Galvin and Gagne 20027.44Applied Operating System Concepts
Dining-Philosophers Problem [Think:Eat] -- eat 하려면 반드시 양쪽 젓갈을 가져야
semaphore chopstick[5];
// Initially all values are 1
Shared data
Philosopher ido {
P(chopstick[i])P(chopstick[(i+1) % 5]) …eat …V(chopstick[i]);V(chopstick[(i+1) % 5]); …think …
} while (1); ==> 문제는 deadlock!
Silberschatz, Galvin and Gagne 20027.45Applied Operating System Concepts
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 P1
P(S); P(Q); 하나씩 차지P(Q); P(S); 상대방 것을 요구
V(S); V(Q); 여기와야 release
함V(Q) V(S);
Silberschatz, Galvin and Gagne 20027.46Applied Operating System Concepts
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 명만 경합하도록 제한
Silberschatz, Galvin and Gagne 20027.47Applied Operating System Concepts
Semaphore 의 문제점
Difficult to code Difficult to prove correctness
** errors are not reproducible
** error are observed rarely Requires voluntary cooperation Single misuse affect entire system
Silberschatz, Galvin and Gagne 20027.48Applied Operating System Concepts
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 body P1 (…) {. . .
}procedure body P2 (…) {
. . .}
{initialization code
}}
Private data
Public Methods
Silberschatz, Galvin and Gagne 20027.49Applied Operating System Concepts
Monitors
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”
x
Insert: x.waitDelete: x.signal
Silberschatz, Galvin and Gagne 20027.50Applied Operating System Concepts
Schematic View of a Monitor
Only one thread (process) can be within Monitor
at a time
Silberschatz, Galvin and Gagne 20027.51Applied Operating System Concepts
Monitor With Condition Variables
Silberschatz, Galvin and Gagne 20027.52Applied Operating System Concepts
void test (int i) {if ( (state[(i + 4) % 5] != eating)
&& (state[i] == hungry) && (state[(i + 1) % 5] != eating))
{ /OK/
state[i] = eating;self[i].signal(); / *wakeup
Pi */
} /* used for putdown L & R */ }void init() {
for (int i = 0; i < 5; i++)state[i] = thinking;
}
Dining Philosophers Examplemonitor dining_philosopher{
enum {thinking, eating hungry (3rd state), } state[5];condition self[5]; /*wait here*/
void pickup(int i) {state[i] = hungry; /* 3rd state */test(i);if (state[i] != eating) self[i].wait(); /*wait here*/
}
void putdown(int i) {state[i] = thinking;// test left and right neighborstest((i+4) % 5); /*if L is waiting*/test((i+1) % 5);
}}
Each Philosopher:{ pickup(i); eat(); putdown(i); think();} while(1)
Silberschatz, Galvin and Gagne 20027.53Applied Operating System Concepts
pickup putdown, ...
self[1]
self[2]
pickup()
putdown()
test()
conditionvariablequeue
entryqueue
shareddata
operations