Read x into register

Download Report

Transcript Read x into register

제05강 : 병렬프로세스 동기화
병행 프로세스 동기화
프로세스 간 데이터 공유
multi-processor
x++ CPU #0
CPU #1 x++
bus
Shared
Memory
x=11
프로세스 간 데이터 공유
multi-processor
CPU #0
CPU #1
bus
1.
2.
3.
Read x into register
Operation with ALU register
Write back to storage box
Shared
Memory
x=11
Also access x
Interleaved execution
Race Condition (시간에 따라 다른 결과)
multi-processor
X++
CPU #0
CPU #1
X++
bus
Read x into register
Increment ALU register
Write back to storage box t4
t1
t3
Shared
Memory
x=11
t2 Read x into register
t3 Increment ALU register
t4
t5 Write back to storage box
Interleaved execution
Race Condition (시간에 따라 다른 결과)
multi-processor
X++
CPU #0
CPU #1
X++
bus
Read x into register (11)
Increment ALU register
Write back to storage box t4
t1
t2
t3
t5
Shared
Memory
x=11
t1
t2 Read x into register
t3 Increment ALU register
t4
t5 Write back to storage box
Interleaved execution
Race Condition (시간에 따라 다른 결과)
multi-processor
X++
CPU #0
CPU #1
X++
bus
Read x into register (11)
Increment ALU register
Write back to storage box t4
t1
t2
t3
t5
Shared
Memory
x=11
t1
t2 Read x into register (11)
t3 Increment ALU register
t4
t5 Write back to storage box
Interleaved execution
Race Condition (시간에 따라 다른 결과)
multi-processor
X++
CPU #0
CPU #1
X++
bus
Read x into register (11)
Increment ALU register (12)
Write back to storage box t4
t1
t2
t3
t5
Shared
Memory
x=11
t1
t2 Read x into register (11)
t3 Increment ALU register
t4
t5 Write back to storage box
Interleaved execution
Race Condition (시간에 따라 다른 결과)
multi-processor
X++
CPU #0
CPU #1
X++
bus
Read x into register (11)
Increment ALU register (12)
Write back to storage box t4
t1
t2
t3
t5
Shared
Memory
x=11
t1
t2 Read x into register (11)
t3 Increment ALU register (12)
t4
t5 Write back to storage box
Interleaved execution
Race Condition (시간에 따라 다른 결과)
multi-processor
X++
CPU #0
CPU #1
X++
bus
Read x into register (11)
t1
t2
t3
Increment ALU register (12)
Write back to storage box t4 (12)
t5
Shared
Memory
x=11
t1
t2 Read x into register (11)
t3 Increment ALU register (12)
t4
t5 Write back to storage box
Interleaved execution
Race Condition (시간에 따라 다른 결과)
multi-processor
X++
CPU #0
CPU #1
X++
bus
Read x into register (11)
t1
t2
t3
Increment ALU register (12)
Write back to storage box t4 (12)
t5
Shared
Memory
x=11
t1
t2 Read x into register (11)
t3 Increment ALU register (12)
t4
t5 Write back to storage box (12)
Incorrect result
Mutual Exclusion
multi-processor
CPU #0
CPU #1
bus
Read x into register (11)
Operation with ALU register (12)
Write back to storage box (12)
Shared
Memory
x
critical section
공유변수를
액세스 하는 code 부분
Mutual Exclusion
multi-processor
CPU #0
CPU #1
bus
Read x into register (11)
Operation with ALU register (12)
Write back to storage box (12)
Shared
Memory
한 순간에는 한 프로세스만
critical section 내에서 작업토록
 상호배제 (mutual exclusion) 원칙
x
critical section
공유변수를
액세스 하는 code 부분
Read x into register (12)
Operation with ALU register (13)
Write back to storage box (13)
correct result
프로세스 동기화
(synchronization)
multi-processor
CPU #0
CPU #1
bus
Read x into register (11)
t1
Increment ALU register (12)
t2
Write back to storage box (12) t3
한 프로세스가
critical section을 나올때
Shared
Memory
x
프로세스 동기화
(synchronization)
multi-processor
CPU #0
CPU #1
bus
Read x into register (11)
t1
Increment ALU register (12)
t2
Write back to storage box (12) t3
한 프로세스가
critical section을 나올때
Shared
Memory
x
기다렸다가
critical section으로 들어감
t4 Read x into register (12)
t5 Operation with ALU register
(13)
t6 Write back to storage box (13)
프로세스 동기화
(synchronization)
multi-processor
CPU #0
CPU #1
bus
Read x into register (11)
t1
Increment ALU register (12)
t2
Write back to storage box (12) t3
Shared
Memory
x
기다렸다가
critical section으로 들어감
t4 Read x into register (12)
t5 Operation with ALU register (13)
t6 Write back to storage box (13)
한 프로세스가
critical section을 나올때
Semaphore
Semaphores
• Semaphore S
– integer 변수
– 지정된 세 operation만 사용가능: P(S), V(S), Init(S)
– 이 operation들은 indivisible (atomic) 함
P(S):
while (S  0) do no-op ;
S--;
S  0?
V(S):
S++;
T
F
Critical Section
noop
Semaphores
• Semaphore S
– integer 변수
– 지정된 세 operation만 사용가능: P(S), V(S), Init(S)
– 이 operation들은 indivisible (atomic) 함
음이면 no-op 하며 공회전 (양이 될 때까지)
P(S):
while (S  0) do no-op;
S--;
S  0?
V(S):
S++;
T
F
Critical Section
noop
Semaphores
• Semaphore S
– integer 변수
– 지정된 세 operation만 사용가능: P(S), V(S), Init(S)
– 이 operation들은 indivisible (atomic) 함
음이면 no-op 하며 공회전 (양이 될 때까지)
P(S):
while (S  0) do no-op ;
S--; 양이면 -- 하고 진입
S  0?
V(S):
S++;
T
F
Critical Section
noop
Semaphores
• Semaphore S
– integer 변수
– 지정된 세 operation만 사용가능: P(S), V(S), Init(S)
– 이 operation들은 indivisible (atomic) 함
(초기값=1)
음이면 no-op 하며 공회전 (양이 될 때까지)
P(S):
while (S  0) do no-op ;
S--; 양이면 -- 하고 진입
read/--/store  atomic
V(S):
S++;
read/++/store  atomic
S  0?
T
F
Critical Section
noop
동시에 Semaphore 액세스?
multi-processor
P(S)
CPU #0
CPU #1
P(S)
bus
arbitrator
bus
Shared
Memory
둘이 동시에 P(S) 수행
S=1
둘이 동시에 bus 사용권 요청 (load Register  S)
bus arbitrator가 한 CPU 에게만 bus cycle 허가 (예: CPU A)
CPU A는 bus 사용 (load S / dec / store S)  atomic! mutual exclusion
CPU A가 bus 다 쓰면 bus 사용권 해제
CPU B가 bus 사용 (load S …)
Critical Section of n Processes
CPU
CPU
control line
semaphore S;
/* 초기값은 1 */
bus
arbitrator
data line
Memory
• Process:
{
/*
*/
/*
*/
critical section
일반 code
} while (1);
Critical Section of n Processes
CPU
control line
semaphore S;
/* 초기값은 1 */
CPU
bus
arbitrator
data line
Memory
• Process:
{ P(S);
/* 양: dec & 진입*/ /* Zero: wait 후 진입 */
critical section
V(S);
일반 code
} while (1);
/* Inc S */
Critical Section of n Processes
CPU
semaphore S;
/* 초기값은 1 */
CPU
control line
bus
arbitrator
data line
Memory
• Process:
{ P(S);
critical section 직전 – 혼자만 들어가도록
critical section
V(S);
일반 code
} while (1);
critical section 직후 – 타 프로세스가 진입토록
Critical Section of n Processes
CPU
semaphore S;
/* 초기값은 1 */
CPU
control line
bus
arbitrator
data line
Memory
• Process:
{ P(S);
critical section 직전 – 혼자만 들어가도록
critical section
V(S);
critical section 직후 – 타 프로세스가 진입토록
일반 code
} while (1);
Binary Semaphore – 1/0
Binary
Busy-Wait Semaphore
Binary Semaphore
Busy-Wait loop
S  초기값 1
P(S):
V(S):
while (S  0) do no-op;
S--;
S++;
S  0?
T
F
Critical Section
질문: 기다리는 동안 왜 CPU, Memory 소모?
해답: 기다리게 되면 즉시 CPU를 포기 (block itself)
나중에 다른 프로세스가 V(S) 하면 wakeup
noop
Integer (counting)
Block-Wakeup Semaphore
S  초기값 1
P(S):
S--;
if (S < 0) do block;
V(S):
Block-Wakeup
S -음
S < 0?
block
S++;
wakeup other process;
Zero
Critical Section
질문: N 프로세스가 동시에 P(S)를 하면?
한 프로세스만 성공
나머지 (N-1) 프로세스는 모두 S-- 하고 block 됨
이때 |S|는 block 된 프로세스의 개수
 “Integer Semaphore”
busy-wait 대 block-wakeup
semaphore
• block-wakeup 시간과 비교해볼 때
– Critical section이 짧으면  Busy-wait
– Critical section이 길면  Block-wakeup
Asynchronous/ Synchronous
Concurrent Processes
Asynchronous Concurrent
아무때나
X 를 access
CPU #0
CPU #1
bus
Shared
Memory
아무때나
X 를 access
Asynchronous/ Synchronous
Concurrent Processes
Synchronous Concurrent
access X
마치면
타 프로세스에게 X 넘김
CPU #0
CPU #1
bus
Shared
Memory
X가 넘어오면 access
마치면
타 프로세스에게 X를 넘김