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