병행 프로세스 동기화

30
병병 병병병병 병병병 제 05 제 : 제제제제제제 제제제

Upload: xuan

Post on 14-Jan-2016

47 views

Category:

Documents


0 download

DESCRIPTION

제 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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 병행 프로세스 동기화

병행 프로세스 동기화

제 05 강 : 병렬프로세스 동기화

Page 2: 병행 프로세스 동기화

프로세스 간 데이터 공유

bus

SharedMemory

multi-processor

CPU #1CPU #0

x=11

x++ x++

Page 3: 병행 프로세스 동기화

bus

SharedMemory

multi-processor

CPU #1CPU #0

1. Read x into register

2. Operation with ALU register

3. Write back to storage box

Also access x

x=11

프로세스 간 데이터 공유

Page 4: 병행 프로세스 동기화

Interleaved execution Race Condition ( 시간에 따라 다른 결과 )

bus

multi-processor

CPU #1CPU #0

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++ X++

SharedMemory

x=11

Page 5: 병행 프로세스 동기화

Interleaved execution Race Condition ( 시간에 따라 다른 결과 )

bus

multi-processor

CPU #1CPU #0

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++ X++

SharedMemory

x=11

Page 6: 병행 프로세스 동기화

Interleaved execution Race Condition ( 시간에 따라 다른 결과 )

bus

multi-processor

CPU #1CPU #0

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

X++ X++

SharedMemory

x=11

(11)(11)

Page 7: 병행 프로세스 동기화

Interleaved execution Race Condition ( 시간에 따라 다른 결과 )

bus

multi-processor

CPU #1CPU #0

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

X++ X++

SharedMemory

x=11

(11)(11)

(12)

Page 8: 병행 프로세스 동기화

Interleaved execution Race Condition ( 시간에 따라 다른 결과 )

bus

multi-processor

CPU #1CPU #0

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

X++ X++

SharedMemory

x=11

(11)(11)

(12) (12)

Page 9: 병행 프로세스 동기화

Interleaved execution Race Condition ( 시간에 따라 다른 결과 )

bus

multi-processor

CPU #1CPU #0

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

X++ X++

SharedMemory

x=11

(11)(11)

(12) (12)

(12)

Page 10: 병행 프로세스 동기화

Interleaved execution Race Condition ( 시간에 따라 다른 결과 )

bus

multi-processor

CPU #1CPU #0

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

Incorrect result

X++ X++

SharedMemory

x=11

(11)(11)

(12) (12)

(12)(12)

Page 11: 병행 프로세스 동기화

Mutual Exclusion

bus

Shared

Memory

multi-processor

CPU #1CPU #0

Read x into register (11)

Operation with ALU register (12)

Write back to storage box (12)

x

critical section공유변수를

액세스 하는 code 부분

Page 12: 병행 프로세스 동기화

Mutual Exclusion

bus

Shared

Memory

multi-processor

CPU #1CPU #0

correct result

Read x into register (11)

Operation with ALU register (12)

Write back to storage box (12) Read x into register (12)

Operation with ALU register (13)

Write back to storage box (13)

한 순간에는 한 프로세스만critical section 내에서 작업토록 상호배제 (mutual exclusion)

원칙

x

critical section공유변수를

액세스 하는 code 부분

Page 13: 병행 프로세스 동기화

프로세스 동기화(synchronization)

bus

Shared

Memory

multi-processor

CPU #1CPU #0

한 프로세스가 critical section 을 나올때 x

Read x into register (11) t1

Increment ALU register (12) t2

Write back to storage box (12) t3

Page 14: 병행 프로세스 동기화

프로세스 동기화(synchronization)

bus

Shared

Memory

multi-processor

CPU #1CPU #0

t4 Read x into register (12)

t5 Operation with ALU register (13)

t6 Write back to storage box (13)

한 프로세스가 critical section 을 나올때 x

Read x into register (11) t1

Increment ALU register (12) t2

Write back to storage box (12) t3

기다렸다가critical section 으로 들어감

Page 15: 병행 프로세스 동기화

프로세스 동기화(synchronization)

bus

Shared

Memory

multi-processor

CPU #1CPU #0

t4 Read x into register (12)

t5 Operation with ALU register (13)

t6 Write back to storage box (13)

x

기다렸다가critical section 으로 들어감

Read x into register (11) t1

Increment ALU register (12) t2

Write back to storage box (12) t3

한 프로세스가 critical section 을 나올때

Page 16: 병행 프로세스 동기화

Semaphore

Page 17: 병행 프로세스 동기화

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

F

S 0? noop

Critical Section

Page 18: 병행 프로세스 동기화

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

F

S 0? noop

Critical Section

음이면 no-op 하며 공회전 ( 양이 될 때까지 )

Page 19: 병행 프로세스 동기화

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

F

S 0? noop

Critical Section

양이면 -- 하고 진입

음이면 no-op 하며 공회전 ( 양이 될 때까지 )

Page 20: 병행 프로세스 동기화

Semaphores• Semaphore S

– integer 변수– 지정된 세 operation 만 사용가능 : P(S), V(S), Init(S)– 이 operation 들은 indivisible (atomic) 함

( 초기값 =1)

P(S): while (S 0) do no-op ;

S--;

V(S): S++;

T

F

S 0? noop

Critical Section

양이면 -- 하고 진입read/--/store atomic

음이면 no-op 하며 공회전 ( 양이 될 때까지 )

read/++/store atomic

Page 21: 병행 프로세스 동기화

bus

SharedMemory

multi-processor

CPU #1CPU #0

S=1

동시에 Semaphore 액세스 ?

busarbitrator

P(S)

둘이 동시에 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 …)

P(S)

Page 22: 병행 프로세스 동기화

Critical Section of n Processes

semaphore S; /* 초기값은 1 */

• Process:

{ /* */ critical section

/* */

일반 code } while (1);

CPU CPU

busarbitrator

Memory

control line

data line

Page 23: 병행 프로세스 동기화

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

busarbitrator

Memory

control line

data line

Page 24: 병행 프로세스 동기화

Critical Section of n Processes

semaphore S; /* 초기값은 1 */

• Process:

{ P(S); critical section

V(S);

일반 code } while (1);

CPU CPU

busarbitrator

Memory

control line

data line

critical section 직전 – 혼자만 들어가도록

critical section 직후 – 타 프로세스가 진입토록

Page 25: 병행 프로세스 동기화

Critical Section of n Processes

semaphore S; /* 초기값은 1 */

• Process:

{ P(S); critical section

V(S);

일반 code } while (1);

CPU CPU

busarbitrator

Memory

control line

data line

critical section 직전 – 혼자만 들어가도록

critical section 직후 – 타 프로세스가 진입토록

Binary Semaphore – 1/0

Page 26: 병행 프로세스 동기화

BinaryBusy-Wait Semaphore

T

F

S 0? noop

Critical Section

Busy-Wait loop

P(S): while (S 0) do no-op;

S--;V(S): S++;

S 초기값 1

Binary Semaphore

질문 : 기다리는 동안 왜 CPU, Memory 소모 ?해답 : 기다리게 되면 즉시 CPU 를 포기 (block itself) 나중에 다른 프로세스가 V(S) 하면 wakeup

Page 27: 병행 프로세스 동기화

Integer (counting)Block-Wakeup Semaphore

Zero

S < 0? block

Critical Section

Block-Wakeup

P(S): S--;

if (S < 0) do block;V(S): S++;

wakeup other process;

S 초기값 1

질문 : N 프로세스가 동시에 P(S) 를 하면 ?한 프로세스만 성공나머지 (N-1) 프로세스는 모두 S-- 하고 block 됨이때 |S| 는 block 된 프로세스의 개수 “Integer Semaphore”

S --

Page 28: 병행 프로세스 동기화

busy-wait 대 block-wakeup semaphore

• block-wakeup 시간과 비교해볼 때

– Critical section 이 짧으면 Busy-wait – Critical section 이 길면 Block-

wakeup

Page 29: 병행 프로세스 동기화

bus

SharedMemory

Asynchronous Concurrent

CPU #1CPU #0

Asynchronous/ SynchronousConcurrent Processes

아무때나X 를 access

아무때나 X 를 access

Page 30: 병행 프로세스 동기화

bus

SharedMemory

Synchronous Concurrent

CPU #1CPU #0

Asynchronous/ SynchronousConcurrent Processes

X 가 넘어오면 access마치면

타 프로세스에게 X 를 넘김

access X 마치면

타 프로세스에게 X 넘김