1 / 30

병행 프로세스 동기화

제 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

xuan
Download Presentation

병행 프로세스 동기화

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. 제05강 : 병렬프로세스 동기화 병행 프로세스 동기화

  2. 프로세스 간 데이터 공유 multi-processor x++ x++ CPU #0 CPU #1 bus Shared Memory x=11

  3. 프로세스 간 데이터 공유 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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)

  10. 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

  11. 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

  12. 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

  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 한 프로세스가 critical section을 나올때 x

  14. 프로세스 동기화(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

  15. 프로세스 동기화(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으로 들어감

  16. Semaphore

  17. 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++;

  18. 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++;

  19. 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++;

  20. 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

  21. 동시에 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 …)

  22. CPU CPU control line bus arbitrator data line Memory Critical Section of n Processes semaphore S; /* 초기값은 1 */ • Process:{/* */ critical section /* */일반 code } while (1);

  23. 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);

  24. 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 직후 –타 프로세스가 진입토록

  25. 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

  26. 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

  27. 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”

  28. busy-wait 대 block-wakeup semaphore • block-wakeup 시간과 비교해볼 때 • Critical section이 짧으면  Busy-wait • Critical section이 길면  Block-wakeup

  29. Asynchronous/ SynchronousConcurrent Processes Asynchronous Concurrent 아무때나 X 를 access 아무때나 X 를 access CPU #0 CPU #1 bus Shared Memory

  30. Asynchronous/ SynchronousConcurrent Processes Synchronous Concurrent access X 마치면 타 프로세스에게 X 넘김 X가 넘어오면 access 마치면 타 프로세스에게 X를 넘김 CPU #0 CPU #1 bus Shared Memory

More Related