Ch5 Concurrency(๋ณ‘๋ ฌ์„ฑ): Synchronization(๋™๊ธฐํ™”) #์šด์˜์ฒด์ œ #์ž„๊ณ„๊ตฌ์—ญ

2022. 12. 13. 19:37ใ†_Study/OperationSystem

728x90

Concurrency & Critical Section  ๐Ÿ‡¸.•*¨*•¸.•*¨*•¸.•*¨*•¸.•*¨*•

ํ•ด๋‹น ์ž๋ฃŒ๋Š” ๊ฐ•์˜ ํ•™์Šต์ž๋ฃŒ์ž…๋‹ˆ๋‹ค.


๋ณ‘๋ ฌ์ฒ˜๋ฆฌ๋ฅผ ์™œ ํ•˜๋Š” ๊ฑธ๊นŒ? -> speed & economics ์—์„œ ํฌ๊ฒŒ ์ด์ ์„ ๋ณผ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

๊ทธ๋Ÿฌ๋‚˜ ๋ณ‘๋ ฌ ํ”„๋กœ๊ทธ๋žจ์€ ๋””๋ฒ„๊น…ํ•˜๊ธฐ๋„ ํž˜๋“ค๊ณ  ํ”„๋กœ๊ทธ๋žจ์„ ๊ตฌํ˜„ํ•˜๊ธฐ๋„ ํž˜๋“ค๋‹ค. ๋ณ‘๋ ฌ์„ฑ์„ ๋ณด์žฅํ•˜๊ธฐ์œ„ํ•œ ๋™๊ธฐํ™”์— ๋Œ€ํ•ด ๋ฐฐ์›Œ๋ณด์ž.

 

Critical Section (๋ณ‘๋ ฌ์„ฑ & ์ž„๊ณ„๊ตฌ์—ญ, ์น˜๋ช…์  ์˜์—ญ) 

์šด์˜์ฒด์ œ๊ฐ€ ์ง€์›ํ•˜๋Š” ๋™๊ธฐํ™” ๋ฐฉ๋ฒ•์˜ ํ•˜๋‚˜๋กœ “์ž„๊ณ„ ๊ตฌ์—ญ” , “์น˜๋ช…์  ์˜์—ญ” ์œผ๋กœ ๋ณดํ˜ธ๋˜์–ด์•ผํ•  ์˜์—ญ์„ ์ด๋ฆ„. ๊ณต์œ ์ž์›์˜ ๋…์ ์„ ๋ณด์žฅํ•ด์ฃผ๋Š” ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.

  • ์ปค๋„ ์˜ค๋ธŒ์ ํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ๋™๊ธฐํ™” ํ•˜๋Š” ๋ฐฉ๋ฒ•
    • ์ปค๋„ ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  (๊ฐ€๋ณ๊ณ  ๋น ๋ฅด๋‹ค, ํ•œ ํ”„๋กœ์„ธ์Šค ๋‚ด์˜ ์“ฐ๋ ˆ๋“œ ์‚ฌ์ด์—์„œ๋งŒ ๋™๊ธฐํ™”๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค.)
    • ์„œ๋กœ ๋‹ค๋ฅธ ํ”„๋กœ์„ธ์Šค ๊ฐ„์— ์ ‘๊ทผ์ด ๋ถˆ๊ฐ€
  • ๋ฎคํ…์Šค (mutex)
    • ์ปค๋„ ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉ, ๋ฌด๊ฑฐ์›€
    • ํฌ๋ฆฌํ‹ฐ์ปฌ ์„น์…˜์ด ํ•œ ํ”„๋กœ์„ธ์Šค ๋‚ด์˜ ์“ฐ๋ ˆ๋“œ ์‚ฌ์ด์—์„œ๋งŒ ๋™๊ธฐํ™”๊ฐ€ ๊ฐ€๋Šฅํ•œ ๋ฐ˜๋ฉด, ๋ฎคํ…์Šค๋Š” ์—ฌ๋Ÿฌ ํ”„๋กœ์„ธ์Šค์˜ ์Šค๋ ˆ๋“œ ์‚ฌ์— ๋™๊ธฐํ™”๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค.
    • ํ”„๋กœ์„ธ์Šค ๋‹ค์ค‘ ์‹คํ–‰์„ ๋ง‰์„ ๋•Œ
  • ์„ธ๋งˆํฌ์–ด(Semaphore)
    • ์„ธ๋งˆํฌ์–ด๋Š” ์ง€์ •๋œ ์ˆ˜ ๋งŒํผ ์“ฐ๋ ˆ๋“œ๊ฐ€ ๋™์‹œ์— ์‹คํ–‰๋˜๋„๋ก ๋™๊ธฐํ™”ํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•˜๋‹ค (ex/ ํ™”์žฅ์‹ค์˜ ๊ฐœ์ˆ˜)

 

 

  • multiple application (๋‹ค์ค‘์‘์šฉ): invented to allow procession time to be shared among active applications : ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค์—์„œ ํ•˜๋‚˜์˜ ๋ฐ์ดํ„ฐ ๋˜๋Š” ๊ทธ ์ง‘ํ•ฉ์ด ์—ฌ๋Ÿฌ ๋ถ„์•ผ์—์„œ ์„œ๋กœ ๊ณตํ†ต์œผ๋กœ ์‚ฌ์šฉ ๋˜๋Š” ๊ฒƒ. ์—ฌ๋Ÿฌ ํ”„๋กœ๊ทธ๋žจ์ด ๋™์‹œ์— ๋™์ž‘
  • Structured Application : extension of modular design and structured programming: ํ”„๋กœ๊ทธ๋žจ ๋ชจ๋“ˆํ™” → ๊ตฌ์กฐํ™”๊ฐ€ ๋˜์–ด์žˆ์Œ
  • Operation System Structure : OS themselves implemented as a set of processes or threads: OS ์ž์ฒด๋„ ๋ณ‘๋ ฌ์„ฑ์„ ๊ฐ€์ง€๊ณ  ์—ฌ๋Ÿฌ ํ”„๋กœ๊ทธ๋žจ(processes, threads)์ด ๋Œ์•„๊ฐ (๋ฐฑ๊ทธ๋ผ์šด๋“œ)

 

 

  • ๋””๋ฒ„๊น…์ด ๋„ˆ๋ฌด ์–ด๋ ค์šด ๋‹จ์  : Scheduler์˜ ๊ฒฐ๊ณผ๋ฅผ ์˜ˆ์ธก์„ ํ•  ์ˆ˜๊ฐ€ ์—†์Œ. Problem : the relative speed of execution of processes cannot be predicted

OS์— ๋”ฐ๋ผ Scheduling ๋‹ค๋ฅด๊ฒŒ ์ฒ˜๋ฆฌํ•  ์ˆ˜๋„ ์žˆ์Œ, interrupts → global value์˜ ๋ฌธ์ œ(shared memory)

  • Critical Section (๊ต์ฐจ๋กœ, ์‚ฌ๊ฑฐ๋ฆฌ)
    • shared double balance ์„ ์˜ˆ๋กœ ์„ค๋ช…, Race condition: there is a race to execute critical sections

 

(๋™์‹œ์—) ์ถœ๊ธˆ ๋‚ด์šฉ์„ ์ €์žฅํ•˜๊ธฐ ์ „์— ์ž…๊ธˆํ•˜๋ฉด ์ถœ๊ธˆ๋‚ด์—ญ์ด.. ์‚ฌ๋ผ์ง„๋‹ค! ์ด๋Š” ๋™์‹œ์— compile ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฐœ์ƒํ•œ๋‹ค.

๋ฐ”๋€ ๊ฐ’์„ ์—…๋ฐ์ดํŠธํ•˜๊ธฐ ์ „์— ์˜ˆ์ „ ๊ฐ’์„ ๋ถˆ๋Ÿฌ์™€ ์˜ˆ์ „ ๋ฉ”๋ชจ๋ฆฌ์— store ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

i++ ๊ณผ ๊ฐ™์€ ๊ฒฝ์šฐ๋„ ๋™์ผํ•˜๊ฒŒ ๋ฐœ์ƒํ•œ๋‹ค. load i, i+=1 store i ์˜ 3๋‹จ๊ณ„๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์–ด i++๊ณผ ๊ฐ™์€ ์ฝ”๋“œ์—๋„ ๊ฐ’์ด ์‚ฌ๋ผ์ง€๋Š” ํ˜„์ƒ์ด ์ผ์–ด๋‚œ๋‹ค.

 

 

 

Race condition ๊ฒฝ์Ÿ์ƒํƒœ : there is a race to execute critical sections

The sections may be defined by different code in different processes : cannot easily detect with analysis.

์—ฌ๋Ÿฌ ํ”„๋กœ๊ทธ๋žจ์ด ๊ณต์œ  ์ž์›์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ์›€์ง์ด๋Š” ์ผ์ข…์˜ ๊ฒฝ์Ÿ ์ƒํƒœ๋ฅผ ์˜๋ฏธํ•œ๋‹ค.

 

Mutual exclusion  : only one process can be in the critical section at a time

ํ•œ ํ”„๋กœ์„ธ์Šค๋งŒ ์‹คํ–‰๋  ์ˆ˜ ์žˆ๋‹ค. ๊ต์ฐจ๋กœ์— ์ฐจ๊ฐ€ 1๋Œ€๋งŒ ๋‹ค๋‹ˆ๊ฒŒ ํ•˜๊ธฐ

without mutual exclusion, results of multiple execution are not determinate

์ƒํ˜ธ ๋ฐฐ์ œ๊ฐ€ ์—†๋‹ค๋ฉด, ๊ฒฐ๊ณผ๋ฅผ ์˜ˆ์ธกํ•  ์ˆ˜ ์—†๋‹ค. (์ž ์žฌ์ ์ธ ๋ฒ„๊ทธ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค.)

Need an OS mechanism so programmer can resolve races : ์ƒํ˜ธ ๋ฐฐ์ œ, Mutex ๋™์‹œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ ๊ณต์œ  ๋ถˆ๊ฐ€๋Šฅํ•œ ์ž์›์˜ ๋™์‹œ ์‚ฌ์šฉ์„ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜, ์ž„๊ณ„๊ตฌ์—ญ์œผ๋กœ ๋ถˆ๋ฆฌ๋Š” ์ฝ”๋“œ ์˜์—ญ์— ์˜ํ•ด ๊ตฌํ˜„๋œ๋‹ค.

 

 

 

๊ทธ๋ ‡๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ๋ณ‘๋ ฌ์„ฑ์„ ๋ณด์žฅํ•  ์ˆ˜ ์žˆ์„๊นŒ?

 

1. Disabling Interrupts : ์›์ธ์ธ ์ธํ„ฐ๋ŸฝํŠธ๋ฅผ ์ œ๊ฑฐํ•˜์ž.

disableInterrupts();
balance = balance + amount;
enableInterrupts();

์ธํ„ฐ๋ŸฝํŠธ๋ฅผ ์ž„์˜๋กœ ๋น„ํ™œ์„ฑํ™” ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ์–ธ์ œ๊นŒ์ง€ ์ด๋ฅผ ๋ง‰์•„์•ผ ํ•˜๋Š”์ง€๋„ ๋ชจ๋ฅด๊ณ  ์šฐ์„ ์ˆœ์œ„์— ๋ฐ€๋ฆฌ๋ฉด ์•„์˜ˆ ์‹คํ–‰๋˜์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ๋‹ค. ๋˜ํ•œ ์ธํ„ฐ๋ŸฝํŠธ๋ฅผ ์ค‘์ง€ํ•˜๋ฉด, ๋‹ค๋ฅธ ์ธํ„ฐ๋ŸฝํŠธ๋„ ์ค‘์ง€๋˜๊ธฐ ๋•Œ๋ฌธ์— ์‹œ์Šคํ…œ ์ „์ฒด๊ฐ€ ๋ธ”๋ฝ๋  ์ˆ˜ ์žˆ๋‹ค.

2. Using a Lock Variable : Lock ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์ž

์‚ฌ์šฉํ•  ๋•Œ Lock์œผ๋กœ ์ž ๊ตฌ๊ณ  ์‚ฌ์šฉํ•˜๋ฉด ํ‘ธ๋Š” ๋ฐฉ๋ฒ•์„ ์ด์šฉํ•œ๋‹ค.

while(lock); // do nothing, busy wait

lock = True ๋ฉด ๋ˆ„๊ตฐ๊ฐ€๊ฐ€ ์ผ์„ ํ•˜๊ณ  ์žˆ๋Š”๊ฒƒ!

 

Busy Wait Condition : ์Šค์ผ€์ค„๋ง์— ์˜ํ•ด ๋„˜์–ด๊ฐ”์ง€๋งŒ lock์ด ๊ฑธ๋ ค ์•„๋ฌด๊ฒƒ๋„ ํ•˜์ง€ ์•Š๋Š” ์ƒํƒœ

 

 

Unsafe Solution

์ž์›์„ ์‚ฌ์šฉํ•˜๋ ค๊ณ  ์ง„์ž…ํ•œํ›„ lock์„ true๋กœ ๋งŒ๋“ค๊ธฐ๋„ ์ „์— time interrupt๋ฅผ ๋ฐ›์•„ ๋‹ค๋ฅธ ํ”„๋กœ์„ธ์Šค๋กœ ๋„˜์–ด๊ฐ„๋‹ค๋ฉด ๋‹ค๋ฅธ ํ”„๋กœ์„ธ์Šค๋„ lock์ด ๊ฑธ๋ ค์žˆ์ง€ ์•Š์•„ ๋‘ ํ”„๋กœ์„ธ์Šค ๋ชจ๋‘ ๋™์‹œ์— ์ž์›์„ ์‚ฌ์šฉํ•˜์—ฌ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

๋”ฐ๋ผ์„œ lock๋„ ๊ณต์œ ๋ณ€์ˆ˜์ด๊ธฐ ๋•Œ๋ฌธ์— ์ƒˆ๋กœ์šด critical section์ด ์ƒ๊ธธ ์ˆ˜ ์žˆ๋‹ค. 

 

 

Atomic Lock Manipulation - System call, ์ชผ๊ฐœ์ง€์ง€ ์•Š๋Š” ํ•จ์ˆ˜(time interrupt๋กœ ์ชผ๊ฐœ์ง€์ง€์•Š์Œ)

OS์— ์˜ํ•ด์„œ๋งŒ block๋˜๋Š” system call ํ•จ์ˆ˜์ด๊ณ  enter(), exit() ํ•จ์ˆ˜๋กœ ์ •์˜ํ•˜์—ฌ ํ•ญ์ƒ ๋™์ผํ•œ ์ฝ”๋“œ๋ฅผ ์ˆ˜ํ–‰ํ•œ๋‹ค. interrupts๊ฐ€ ๋น„ํ™œ์„ฑํ™”๋˜๋Š” ์‹œ๊ฐ„์ด ์ œํ•œ๋˜์–ด ์žˆ์–ด ๋ช‡๊ฐœ์˜ ๋ช…๋ น์–ด๊ฐ€ ์ˆ˜ํ–‰๋˜๋Š” ๋™์•ˆ๋งŒ block๋œ๋‹ค.

lock ํ• ๋‹นํ•ด๋„ ๋˜๋Š”์ง€ ํ™•์ธํ•˜๋Š” ์ฝ”๋“œ ์ถ”๊ฐ€๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.

 

Deadlock : ๋‘ ๊ฐœ์˜ ์š”์†Œ๋ฅผ ์ฒ˜๋ฆฌํ•  ๋•Œ ์„œ๋กœ์˜ ์ž์›์ด ํ•„์š”ํ•ด ๋ฌดํ•œํžˆ ๊ธฐ๋‹ค๋ฆฌ๋Š” ์ƒํ™ฉ

์œ— ์ƒํ™ฉ์€ deadlock์ด ๋ฐœ์ƒํ•˜๋Š” ๊ฒฝ์šฐ์ด๋‹ค. lock1 ๋ณ€์ˆ˜๊ฐ€ true๋กœ ๋ฐ”๋€Œ๋ฉด ์ž ๊ธฐ๊ฒŒ ๋˜์–ด ๋‹ค๋ฅธ process๋„ ๋ง‰์•„๋ฒ„๋ฆฐ๋‹ค.

 

 

 

Semaphore : ๋ฐ๋“œ๋ฝ ํ•ด๊ฒฐ๋ฒ•

Dijkstra sepaphore : ๋‹ค์ด์—‘์ŠคํŠธ๋ผ๊ฐ€ ์šด์˜์ฒด์ œ์— ํ•„์š”ํ•œ ๊ฐœ๋…์ ์ธ ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ์ œ๊ณตํ–ˆ๋‹ค. ๊ฐœ๋…์ผ ๋ฟ์ด์ง€๋งŒ ์ตœ์‹  ์šด์˜์ฒด์ œ์˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์ด ๊ธฐ๋ณธ์ด ๋œ๋‹ค.

Critical Section : ๋ฌธ์ œ ํ•ด๊ฒฐ์„ ์œ„ํ•œ ๊ธฐ๋ณธ ์กฐ๊ฑด 4๊ฐ€์ง€

- mutual Exclusion : only one process at a time in the CS : ์˜ค์ง ํ•œ ๋ฒˆ์— ํ•œ ํ”„๋กœ์„ธ์Šค๋งŒ ์‚ฌ์šฉํ•œ๋‹ค.

- Only processes competing for a CS are involved in resolving who enters the CS : ์™ธ๋ถ€์˜ ๊ฐ„์„ญ(๊ด€๋ฆฌ์ž)์—†์ด ํ”„๋กœ์„ธ์Šค ๋‚ด์—์„œ ๊ทœ์น™์„ ์ •ํ•˜์—ฌ ๊ฒฝ์Ÿํ•ด์„œ ํ•ด๊ฒฐํ•œ๋‹ค.

- Once a process attempts to enter its CS, it cannot ve postponed indefinitely : ๋ฌดํ•œ์ • ์—ฐ๊ธฐ(์ง€์—ฐ) ๊ธˆ์ง€

- After requesting entry, only a bounded number of other processes may enter before the requsting process : ์š”์ฒญํ•˜๊ธฐ ์ „์— ๋Œ€๊ธฐ์ค‘์ด๋˜ ํ”„๋กœ์„ธ์Šค(์Œ“์—ฌ์žˆ๋Š” ํ”„๋กœ์„ธ์Šค)๋Š” ํ•œ์ •๋œ ๊ฐœ์ˆ˜๋งŒ ๋Œ€๊ธฐ ๊ฐ€๋Šฅํ•˜๋‹ค.

 

 

์œ„์˜ 4๊ฐ€์ง€ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜์—ฌ ํ•ด๊ฒฐํ•œ ๊ฒƒ์„ ์„ธ๋งˆํฌ์–ด๋ผ๊ณ  ํ•œ๋‹ค.

์„ธ๋งˆํฌ์–ด : Semaphore

s is a nonnegaive integer variable that can only be changes or tested by these two indivisible functions:

์Œ์ด ์•„๋‹Œ ์ •์ˆ˜ (1,2,3..)๋กœ ์ด๋ฃจ์–ด์ง„ ๋ณ€์ˆ˜์ด๋ฉฐ, ์ฒดํฌํ•˜๋Š” ํ•จ์ˆ˜, ์‹œ๊ทธ๋„์„ ์ฃผ๋Š” ํ•จ์ˆ˜ ๋‘ ๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค.

Verhogen : increase(์ฆ๊ฐ€, ์ •์ˆ˜๋ฅผ +) / Proberen : try(testing) ์‚ฌ์šฉํ•ด๋„ ๋˜๋Š”์ง€ ํ™•์ธ

P๋Š” 0์ธ ๋™์•ˆ์—๋Š” ๊ธฐ๋‹ค๋ ค์•ผํ•œ๋‹ค. (์ดˆ๊นƒ๊ฐ’์ด 1์ด๋ฉด ๋“ค์–ด์™€์„œ -1๋กœ 0์œผ๋กœ ์ž ๊ตฌ๊ณ  ์‚ฌ์šฉํ•œ๋‹ค.)

 

semaphore mutex =1; // ์„ธ๋งˆํฌ์–ด ๋ณ€์ˆ˜์ด๋ฆ„์ด mutex์ด๋‹ค.

 

P()

์ง„ํ–‰ํ•  ์ฝ”๋“œ

V()

 

fork(proc_0,0); // run

fork(proc_1,0); // run

 

Sharing Two variables

V๋กœ ์‹ ํ˜ธ๋ฅผ ์ฃผ๋ฉด P๋กœ ๋ฐ›๋Š”๋‹ค.

proc_A(){
	while(TRUE){
    update(x);
    V(s1);
    
    P(s2);
    retrieve(y);
    }
}

 

์„ธ๋งˆํฌ์–ด ์ดˆ๊นƒ๊ฐ’์„ ์ด์šฉํ•ด ์กฐ์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

P,V ์ˆœ์„œ๊ฐ€ ๋”ฐ๋กœ ์ •ํ•ด์ ธ ์žˆ์ง€๋Š” ์•Š๋‹ค.

Counting semaphore - interger variable : ์—ฌ๋Ÿฌ ์ •์ˆ˜๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค.

Binary semaphore : 0~1 mutex lock 

 

 

Deadlock and Starvation

Deadlock : two or more processes are waiting indefinitely for an event that can be caused by only one of the waiting processes // ์š”์ฒญํ•œ ์‹œ๊ทธ๋„์„ ๋ฌดํ•œ waiting, block ๋˜์–ด ์žˆ๋Š” ์ƒํƒœ

 

Let S and Q be two semaphores initialized to 1

 

Starvation : indefinite blocking. A process may never be removed from the semaphore queue in which it is suspended

Priority Inversion - Scheduling problem when lower-priority process holds a lock nedded by higher-priority process

time scheduling์— ์˜ํ•ด ์šฐ์„ ์ˆœ์œ„ ์—ญ์ „ ํ˜„์ƒ์ด ์ผ์–ด๋‚  ์ˆ˜ ์žˆ๋‹ค.

์—ญ์‚ผ๊ฐํ˜• ๋ชจ์–‘์€ Critical Section์ธ๋ฐ ์ฆ‰, ๊ณต์œ ๋ฉ”๋ชจ๋ฆฌ์ด๋‹ค. ํ”„๋กœ์„ธ์Šค t3๊ฐ€ ์‹คํ–‰๋˜๊ณ  ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋†’์€ t1์ด ๊ณต์œ ๋ณ€์ˆ˜๋ฅผ ํ•„์š”๋กœ ํ• ๋•Œ P()ํ•จ์ˆ˜๋กœ block์ด ๊ฑธ๋ฆด ์ˆ˜ ์žˆ๋‹ค. ์ด๋•Œ t3 ๊ฐ€ ์ œ๋•Œ ๊ณต์œ ๋ณ€์ˆ˜๋ฅผ ๋Œ๋ ค์ฃผ์ง€ ์•Š์œผ๋ฉด ์šฐ์„ ์ˆœ์œ„ ์—ญ์ „ํ˜„์ƒ์ด ์ด๋Ÿฌ๋‚œ๋‹ค.

 t3๋Š” ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋‚ฎ๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค๋ฅธ ํ”„๋กœ์„ธ์Šค๊ฐ€ ์‹คํ–‰๋˜๋ฉด ๋๋‚ ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ ค์•ผํ•ด์„œ t3๊ฐ€ ๊ณต์œ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ•œ์ฐธ ๋’ค์— ๋Œ๋ ค์ฃผ๊ฒŒ ๋˜๊ธฐ ๋•Œ๋ฌธ์— t1์€ ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋†’์•„๋„ ๊ฐ€์žฅ ๋Šฆ๊ฒŒ ๋๋‚˜๊ฒŒ ๋œ๋‹ค.

 

๋”ฐ๋ผ์„œ ์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ ๊ฐ™์€ CS๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์šฐ์„ ์ˆœ์œ„๊ฐ€ ์•„๋ž˜์ธ t3์— t1์˜ ์šฐ์„ ์ˆœ์œ„๋ฅผ ์ƒ์†ํ•œ๋‹ค. ์ด๋ฅผ priority inheritance(์šฐ์„ ์ˆœ์œ„์ƒ์†) ๋ผ๊ณ  ํ•œ๋‹ค.

 


 

 

๋‹ค์ด์—‘์ŠคํŠธ๋ผ์˜ ํ•ด๊ฒฐ๋ฒ•

 

์ƒ์‚ฐ์ž์™€ ์†Œ๋น„์ž ๋ฌธ์ œ

์ƒ์‚ฐ์ž(producer): ์ •๋ณด๋ฅผ ๋งŒ๋“ค์–ด๋ƒ„, update , ex)print program

์†Œ๋น„์ž(consumer): ์ •๋ณด๋ฅผ ์‚ฌ์šฉํ•จ. ex) printer

 

 

ํ•œ์ •๋œ ๊ฐœ์ˆ˜์˜ buffer๊ฐ€ ์žˆ๋Š”๋ฐ  buffer๋Š” item์„ hold ํ•œ๋‹ค. producer๊ฐ€ full pool์˜ buffer์„ ์‚ฌ์šฉํ•˜๋ฉด ๋‹ค์‹œ empty pool์— ๋‹ด๋Š”๋‹ค.

 

์—ฌ๋Ÿฌ๋ช…์ด ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ (web)

reader : ๋…์ž, updateํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์—ฌ๋Ÿฌ ๋ช…์ด ์ฝ์„ ์ˆ˜ ์žˆ๋‹ค.

writer : ์ž‘๊ฐ€, ์ž‘์„ฑํ•  ๋•Œ๋Š” ํ•œ ๋ช…๋งŒ ์ ‘๊ทผํ•ด์•ผํ•œ๋‹ค.

์ฒซ๋ฒˆ์งธ ๋…์ž๊ฐ€ ๋ฐฉ์„ ์ž ๊ตฌ๊ณ  (๋ชจ๋“  writer ๊ณผ์˜ ๊ฒฝ์Ÿ)

๋งˆ์ง€๋ง‰ ๋…์ž๊ฐ€ signal์„ ๋ณด๋ƒ„

writer๊ฐ€ ๋ชจ๋“  reader์„ ๊ธฐ๋‹ค๋ ค์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— starvation ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค

 

๋ณดํ†ต์€ writing์„ ๋” ์šฐ์„ ์ˆœ์œ„๋ฅผ ์ฃผ๊ฑฐ๋‚˜ ๊ถŒํ•œ์„ ์ค€๋‹ค.

์œ— ๊ทธ๋ฆผ ์„ค๋ช… : ํ”„๋กœ์„ธ์Šค 1 reader๊ฐ€ ํ™”์‚ดํ‘œ๊นŒ์ง€ ์‹คํ–‰

ํ”„๋กœ์„ธ์Šค 2 reader๊ฐ€ P(readBlock)๊นŒ์ง€ ์‹คํ–‰ํ•˜๊ณ  ์‹คํ–‰์ค‘ 0์ด๋จ

ํ”„๋กœ์„ธ์Šค 3 writer ์ ‘๊ทผํ•˜์—ฌ 0์ด๋ผ ์ดํ›„ ์‹คํ–‰ ๋ชปํ•จ

ํ”„๋กœ์„ธ์Šค 4 P์— ์ ‘๊ทผํ•˜์—ฌ 0์ด๋ผ ์ดํ›„ ์‹คํ–‰ ๋ชปํ•จ

 

ํ”„๋กœ์„ธ์Šค2๊ฐ€ V(readblock)์„ ๋‘ ๋ฒˆ ์‹คํ–‰ํ•ด์•ผํ•˜๋Š” ์ƒํ™ฉ์ด ๋ฐœ์ƒ์ด๋ผ๋Š”๋ฐ ์ž˜ ์ดํ•ด๊ฐ€ ๊ฐ€์ง€์•Š์•„ ์ •๋ฆฌํ•˜๋ฉด, ํ”„๋กœ์„ธ์Šค3์ด writer๋งŒ ์ˆœ์ฐจ์ ์œผ๋กœ ์‹คํ–‰๋˜๊ฑฐ๋‚˜,  ํ”„๋กœ์„ธ์Šค2๊ฐ€ ๋๊นŒ์ง€ ์‹คํ–‰๋˜๊ธฐ๋งŒ ํ•ด๋„ V๊ฐ€ ๋ชจ์ž๋ผ๋Š” ๊ฒฝ์šฐ๊ฐ€ ์—†๋‹ค. ์•„๋ฌด๋ž˜๋„ P๊ฐ€ ๋™์‹œ ์‹คํ–‰๋˜์–ด lock์ด ๊ฑธ๋ฆฐ ๊ฒฝ์šฐ. V๋กœ ๋Œ๋ ค์ค„๋•Œ ํ•˜๋‚˜๋งŒ ๋Œ๋ ค์ฃผ๋Š”๊ฑด๋ฐ ์ด๊ฑธ ๊ด€๋ฆฌํ•˜๋Š” ๊ด€๋ฆฌ์ž๊ฐ€ ์—†์–ด ๋‘˜๋‹ค ์ž๊ธฐ๊บผ์ธ์ค„ ์•Œ๊ณ  ๋ฐ›์•„ ๊ฒฐ๊ตญ ํ‚ค์˜ ์ดํ•ฉ ๊ฐœ์ˆ˜๊ฐ€ ์—‰ํ‚ค๋Š” ๊ฑฐ๊ฐ™๋‹ค.

 

๋”ฐ๋ผ์„œ  readblock ์กฐ์ฐจ๋„ 1๊ฐœ์”ฉ๋งŒ ์‚ฌ์šฉํ•˜๊ฒŒ ๋งŒ๋“ ๊ฒŒ writePending๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.

it is imperative that the P and V operations be implemeted as atomic primitives : atoimic์œผ๋กœ ๊ตฌ์„ฑํ•œ๋‹ค.

์†Œํ”„ํŠธ์›จ์–ด๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ํ•˜๋“œ์›จ์–ด๋กœ ์„ธ๋งˆํฌ์–ด๋ฅผ ๊ตฌ์„ฑํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

Dekker's, Perterson's ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ์„ธ๋งˆํฌ์–ด๋ฅผ ๊ตฌํ˜„ํ•˜๋ฉด ์˜ค๋ฒ„ํ—ค๋“œ๊ฐ€ ๊ต‰์žฅํžˆ ํฌ๊ฒŒ ๋ฐœ์ƒํ•œ๋‹ค.

์šด์˜์ฒด์ œ์—์„œ ์„ธ๋งˆํฌ์–ด๋ฅผ ์ง€์›ํ•˜๊ณ  ์žˆ๋‹ค.

 

 

 

์ฒ ํ•™์ž๋“ค์˜ ์ €๋…์‹์‚ฌ

์ฒ ํ•™์ž 5๋ช…, ์ “๊ฐ€๋ฝ 5๊ฐœ๋กœ ๊ตฌ์„ฑ, ์ฒ ํ•™์ž๋“ค์€ thinking, eating ๋™์ž‘์„ ์ˆ˜ํ–‰, ์–‘์˜†์˜ ์ “๊ฐ€๋ฝ 2๊ฐœ๋ฅผ ํ•จ๊ป˜ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ• ๋•Œ๋งŒ ์‹์‚ฌ๊ฐ€๋Šฅ = ์ด์›ƒํ•œ ์ฒ ํ•™์ž๋Š” ๋™์‹œ์— ์‹์‚ฌ ์ ˆ๋Œ€ ๋ถˆ๊ฐ€๋Šฅ

์ “๊ฐ€๋ฝ = shared data // CS

 

์„ธ๋งˆํฌ์–ด๋ฅผ ์ค‘์ฒฉ์—์„œ ์‚ฌ์šฉํ•œ๋‹ค. P(0) p(1) ๋‘˜๋‹ค ์ “๊ฐ€๋ฝ์ด ์žˆ์–ด์•ผ eat() ๊ฐ€๋Šฅ

๋™์‹œ์— ํ•œ์ชฝ ์ “๊ฐ€๋ฝ๋งŒ ์žก์œผ๋ฉด? -> ๋ชจ๋‘๊ฐ€ ๋‚˜๋จธ์ง€ ํ•˜๋‚˜๋ฅผ ๊ธฐ๋‹ค๋ฆฌ๋Š” ์ƒํƒœ -> deadlock

์ง์ˆ˜ ์˜ค๋ฅธ์ชฝ๋ถ€ํ„ฐ ๋“ค๊ธฐ, ํ™€์ˆ˜์ธ์‚ฌ๋žŒ์€ ์™ผ์ชฝ๋ถ€ํ„ฐ ๋“ค๊ธฐ

 

๊ฒฐ๋ก  : (Nesting)์ค‘์ฒฉ ์„ธ๋งˆํฌ์–ด๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด deadlock์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค.

์„œ๋กœ๋‹ค๋ฅธ ํ”„๋กœ๊ทธ๋ž˜๋จธ์— ์˜ํ•ด ์ž‘์„ฑ๋œ ์ฝ”๋“œ๋ผ ์ž„์˜์˜ ์ˆœ์„œ๋กœ ๊ตฌํ˜„ํ•œ๋‹ค.

 

-> P์—ฐ์‚ฐ์„ ์•ˆ์ „ํ•œ ๋ฐ์ดํ„ฐ ์‚ฌ์šฉ์„ ์œ„ํ•ด์„œ ์ถ”์ƒํ™”ํ•˜์—ฌ ํ•˜๋‚˜์˜ ์—ฐ์‚ฐ์œผ๋กœ ์ด์šฉํ•˜์ž.

 

AND synchronization : ์—ฌ๋Ÿฌ๊ฐœ semaphore์„ ๋ฌถ์–ด์„œ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค. => ๋‘๊ฐœ๊ฐ€ ๋ชจ๋‘ ๊ฐ€๋Šฅํ•ด์•ผ ์ฒ˜๋ฆฌ ๊ฐ€๋Šฅํ•˜๋‹ค.

Psimultaneous(S1,S2...Sn): P ์ „์ฒด ๋Œ€๊ธฐ๋ธ”๋Ÿญ

 

Semaphores provide a powerful tool for enforcing mutual exclusion and coordinate processes

But wait and signal are scattered among several processes. Hence, difficult to understand their effects.

Usage must be correct in all the processes

One bad process can fail the entire collection of proecesses

 


Event

Exact definitition is specific to each OS:์šด์˜์ฒด์ œ ๋งˆ๋‹ค ๋‹ค๋ฆ„

a process can wait on an event until another process signals the event : ๊ธฐ๋‹ค๋ฆฌ๋Š” ํ”„๋กœ์„ธ์Šค์—๊ฒŒ signal์„ ์ค„ ์ˆ˜ ์žˆ๋‹ค.

 

event descriptor : event control block : signal์€ 1๊ฐœ๋งŒ ๋ฐ›์Œ. broadcast x

Active approach

- multiple processes can wait on an event

- exactly one process is unblockes when a signal occurs

- A signal with no waiting process is ignored

 

May have a queue function that returns number of processes waiting on the event.

topOfHour.wait(): ์ •์‹œ๊ฐ€ ๋˜๋ฉด ใ…ก  signal์„ ๋ณด๋ƒ„.

 

 

Unix signal : it is raised by one process to call nother process's attention to an event

ํ”„๋กœ์„ธ์Šค๋‚˜ ์‚ฌ์šฉ์ž๊ฐ€ ์‹ ํ˜ธ๋ฅผ ์ค„๋•Œ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜, ์‹œ์Šคํ…œ์ƒ์˜ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•  ๋•Œ ์‚ฌ์šฉ๋œ๋‹ค.

it can be caught by the subject process

 

Justification for including signals was for the OS to inform a user process of an event

Ctrl + C => interrupt, 2 / or ๋‚˜๋งŒ์˜ ํ•ธ๋“ค๋Ÿฌ๋ฅผ ๋งŒ๋“ค ์ˆ˜๋„ ์žˆ๋‹ค.

Program tried to divided by zero  

  Active semaphore (if) passive semaphore (while)
๋ฐœ์ƒ ์‹œ์  signal ๋ฐœ์ƒ ์‹œ์  ๋ฐœ์ƒํ•˜๊ณ  ํ•„์š”ํ•  ๋•Œ
์„ฑ๋Šฅ ๋Š๋ฆผ : target switching -> overhead ๋น ๋ฆ„ : ๋ณ‘๋ ฌ์ปดํ“จํŒ…
์ด๋ฆ„ Hoare semantics Brinch Hansen semantics

 

 

 

Monitors

Specialized form of ADT(Abstract Data Type) : encapsulates implementation

 

ํ•œ๋ฒˆ์— ํ•˜๋‚˜์˜ balace๋ฅผ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋„๋ก

writer , reader ํ•˜๋‚˜์˜ ์ฑ… ๋“ฑ ๋ณดํ˜ธ๊ฐ€ ๋œ๋‹ค.

 

reader writer ์‚ฌ์ด์˜ deadlock์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค.

suspend ๊ฐ€ ํ•„์š”ํ•˜๊ฒ ๋‹ค.

 

 

 

suspend : ์กฐ๊ฑด์ด ๋งŒ์กฑํ•  ๋•Œ๊นŒ์ง€ (์ž ๊น์˜)์ค‘์ง€

condition variable : monitor์•ˆ์—์„œ ์‚ฌ์šฉํ•˜๋Š” event์˜ ํŠน๋ณ„ํ•œ ํ˜•ํƒœ

 

wait() : suspend invoking process until another executes a signal

signal() : resume one process if any are suspended, otherwise do nothing

queue(): return TRUE if there is at least one process suspended on the condition variable : ํ•˜๋‚˜์˜ ํ”„๋กœ์„ธ์Šค๊ฐ€ suspended๋ผ๋ฉด true๋ฅผ ๋ฐ˜ํ™˜ ์•„๋‹ˆ๋ฉด false, ๋Œ€๊ธฐํ•˜๊ณ  ์žˆ๋Š” ๋™๋ฃŒ

 

์ฒ ํ•™์ž๋“ค์˜ ์ €๋…์‹์‚ฌ์— ์ ์šฉํ•˜๊ธฐ                

                                                                                                                                                                                                                                                                                                                                          

test์—์„œ ์–‘์ชฝ์œผ๋กœ signal์„ ์คŒ wait์˜€๋˜๊ฒŒ signal ์„ ๋ฐ›์œผ๋ฉด ์‹คํ–‰๋œ๋‹ค.

 

 

pthread mutex

 

variables pthread_mutex_t
initialize pthread_mutex_init
setting pthread_mutex_lock
Unsetting pthread_mutex_unlock
Destroy pthread_mutex_destroy

mutex๋ฅผ ์‚ฌ์šฉ์‹œ ์ตœ๋Œ€ํ•œ ๋ฒ”์œ„๋Š” ์ž‘๊ฒŒ ์žก์•„์•ผํ•œ๋‹ค.

#include <pthread.h>

pthread_mutex_lock(&lck);
balace += dep[i];
pthread_mutex_unlock(&lck);

 

semaphore

 

#include <semaphore.h>

 

int sem_init(sem_t *sem, int pshared, unsigned int value);

int sem_wait(sem_t *sem);

int sem_post(sem_t *sem);

omt sem_destory(sem_t* sem);