6.2. Process Synchronization 2

ofohjยท2023๋…„ 3์›” 29์ผ
0

์šด์˜์ฒด์ œ

๋ชฉ๋ก ๋ณด๊ธฐ
11/16
post-thumbnail

๐Ÿ”ป ์‚ฌ์ „ ์ง€์‹

์ถ”์ƒ ์ž๋ฃŒํ˜•

  • ์ž๋ฃŒ๊ตฌ์กฐ์—์„œ ๋“ฑ์žฅ
  • object์™€ operation์œผ๋กœ ๊ตฌ์„ฑ
  • ์ปดํ“จํ„ฐ์—์„œ ์‹ค์ œ๋กœ ํ˜•ํƒœ๋ฅผ ๊ตฌํ˜„ํ•˜๊ธฐ๋ณด๋‹ค๋Š” ๋…ผ๋ฆฌ์ ์œผ๋กœ ์ •์˜๋œ ์ž๋ฃŒํ˜•

Semaphores

์˜ค๋Š˜ ๋ฐฐ์šธ ์„ธ๋งˆํฌ์–ด๊ฐ€ ์ด ์ถ”์ƒ ์ž๋ฃŒํ˜•์— ์†ํ•œ๋‹ค.
์ด๋Š” ๋ฉ€ํ‹ฐ์Šค๋ ˆ๋“œ ํ™˜๊ฒฝ์—์„œ ๊ณต์œ ์ž์›์„ ๊ด€๋ฆฌํ•˜๋Š” ๋™๊ธฐํ™” ๋ฐฉ๋ฒ•๋ก ์ด๋‹ค.

์‚ฌ์šฉ ๊ฒฝ์šฐ

  • lock์„ ๊ฑธ ๋•Œ
  • ๊ณต์œ ์ž์›์˜ ํš๋“๊ณผ ๋ฐ˜๋‚ฉ ์ฒ˜๋ฆฌ

์—ฐ์‚ฐ

P ์—ฐ์‚ฐ

  • ๊ณต์œ ์ž์›์„ ํš๋“ํ•˜๋Š” ๊ณผ์ •
  • lock์„ ๊ฑบ

V ์—ฐ์‚ฐ

  • ๊ณต์œ ์ž์›์„ ๋‹ค ์‚ฌ์šฉํ•˜๊ณ  ๋ฐ˜๋‚ฉํ•˜๋Š” ๊ณผ์ •
  • lock์„ ํ’‚

Critical Section์—์„œ์˜ ํ™œ์šฉ

1. Busy-wait ๋ฐฉ์‹

๐Ÿ‘‰ lock์„ ์–ป์ง€ ๋ชปํ•˜๋ฉด ํšŒ์ „ํ•˜๋ฉด์„œ lock์„ ์–ป๊ธธ ๊ธฐ๋‹ค๋ฆผ

1) Synchronization variable

semaphore mutex;

2) process p

do {
	P(mutex);
	critical section
	V(mutex);
	remainder section
} while (1);

2. block & wakeup (=sleep lock)

๐Ÿ‘‰ lock์„ ์–ป์ง€ ๋ชปํ•˜๋ฉด ํ•ด๋‹น ํ”„๋กœ์„ธ์Šค block ์ฒ˜๋ฆฌ

P(S)

  • ์ž์›์„ ํš๋“ํ•˜๋Š” ๊ณผ์ •
S.value--; # ์ž์›์— ์—ฌ๋ถ„์ด ์žˆ์œผ๋ฉด ํš๋“
if(S.value < 0) # ์—†์œผ๋ฉด
{
	add this process to S.L;
	block(); # block
}

V(S)

  • ์ž์›์„ ๋ฐ˜๋‚ฉํ•˜๋Š” ๊ณผ์ •
S.value++;
if(S.value <= 0) # ์ž์›์„ ๊ธฐ๋‹ค๋ฆฌ๋ฉด์„œ ์ž ๋“ค์–ด ์žˆ๋Š” ํ”„๋กœ์„ธ์Šค๊ฐ€ ์žˆ์œผ๋ฉด
{
	remove a process P from S.L;
	wakeup(P); # ๊นจ์›Œ์ฃผ๊ธฐ
}

๋น„๊ต

  • ๋ณดํ†ต, block & wakeup ๋ฐฉ๋ฒ•์ด ๋” ํšจ์œจ์ 
  • ํ•˜์ง€๋งŒ critical section์˜ ๊ธธ์ด๊ฐ€ ์งง๋‹ค๋ฉด Busy-wait์ด ๋” ์ ํ•ฉ

Semaphore ์ข…๋ฅ˜

1. Binary semaphore (=mutex)

  • 0 ๋˜๋Š” 1 ๊ฐ’๋งŒ ๊ฐ€์ง€๋Š” semaphore
  • ์ฃผ๋กœ mutual exclusion (lock/unlock)์— ์‚ฌ์šฉ

2. Counting semaphore

  • ๋„๋ฉ”์ธ์ด 0์ด์ƒ์ธ ์ž„์˜์˜ ์ •์ˆ˜๊ฐ’
  • ์ฃผ๋กœ resource counting์— ์‚ฌ์šฉ

์ถœ์ฒ˜: http://kocw.net/home/m/search/kemView.do?kemId=1046323

0๊ฐœ์˜ ๋Œ“๊ธ€