๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ป CS/์šด์˜์ฒด์ œ

[์šด์˜์ฒด์ œ] 3. Process

by nitronium102 2022. 11. 20.

ํ”„๋กœ์„ธ์Šค์˜ ๊ฐœ๋…

a program in execution

ํ”„๋กœ์„ธ์Šค์˜ ๋ฌธ๋งฅ(context)

ํŠน์ • ์‹œ์ ์—์„œ ํ•ด๋‹น ํ”„๋กœ์„ธ์Šค๊ฐ€ ์–ด๋””๊นŒ์ง€ ์‹คํ–‰์„ ํ–ˆ๋Š”์ง€ ๊ทœ๋ช…ํ•˜๋Š” ์š”์†Œ

  • 1) CPU ์ˆ˜ํ–‰ ์ƒํƒœ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ํ•˜๋“œ์›จ์–ด ๋ฌธ๋งฅ : Program Counter, ๊ฐ์ข… register
    • register๊ฐ€ ํ˜„์žฌ ์–ด๋–ค ๊ฐ’์„ ๊ฐ€์ง€๋Š”๊ฐ€
  • 2) ํ”„๋กœ์„ธ์Šค์˜ ์ฃผ์†Œ ๊ณต๊ฐ„ : code, data, stack
    • code : ํ˜„์žฌ ์–ด๋””๊นŒ์ง€ ์ˆ˜ํ–‰ํ–ˆ๋Š”๊ฐ€
    • data : ํ˜„์žฌ ๋ณ€์ˆ˜์˜ ๊ฐ’์ด ๋ฌด์—‡์ธ๊ฐ€
    • stack : ๋ฌด์Šจ ๋‚ด์šฉ์„ ์–ด๋””๊นŒ์ง€ ์ €์žฅํ–ˆ๋Š”๊ฐ€
  •  3) ํ”„๋กœ์„ธ์Šค ๊ด€๋ จ ์ปค๋„ ์ž๋ฃŒ ๊ตฌ์กฐ : PCB(Process Control Block), Kernel stack
    • process ์ƒ์„ฑ๋  ๋•Œ๋งˆ๋‹ค pcb๋ฅผ ๋‘์–ด ๊ด€๋ฆฌ
    • kernel stack : system call ์‹œ kernel(์—ฌ๋Ÿฌ ํ”„๋กœ์„ธ์Šค๊ฐ€ ๊ณต์œ ํ•˜๋Š” ์ฝ”๋“œ)์—์„œ ์ผ ์ฒ˜๋ฆฌ
      -> ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‹œ ํ”„๋กœ์„ธ์Šค๋งˆ๋‹ค ๋ณ„๋„๋กœ stack ๋‘์–ด ์ •๋ณด ๊ผฌ์ž„์„ ๋ง‰์Œ
    • multitasking ๋“ฑ์„ ํ†ตํ•ด cpu๋ฅผ ์—ฌ๋Ÿฌ ํ”„๋กœ์„ธ์Šค๊ฐ€ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์Œ ->  ํ”„๋กœ์„ธ์Šค์˜ ๋ฌธ๋งฅ ํŒŒ์•…์„ ํ†ตํ•ด ์ •ํ™•ํ•œ time sharing์„ ๋ชฉํ‘œ๋กœ ํ•จ.

 

ํ”„๋กœ์„ธ์Šค์˜ ์ƒํƒœ Process State

ํ”„๋กœ์„ธ์Šค๋Š” ์ƒํƒœ(state)๊ฐ€ ๋ณ€๊ฒฝ๋˜๋ฉฐ ์ˆ˜ํ–‰๋œ๋‹ค

1) Running : CPU๋ฅผ ์žก๊ณ  instruction์„ ์ˆ˜ํ–‰์ค‘์ธ ์ƒํƒœ

2) Ready : CPU๋ฅผ ๊ธฐ๋‹ค๋ฆฌ๋Š” ์ƒํƒœ(๋ฉ”๋ชจ๋ฆฌ ๋“ฑ ๋‹ค๋ฅธ ์กฐ๊ฑด์„ ๋ชจ๋‘ ๋งŒ์กฑ -> ํ•„์š”ํ•œ ๋ถ€๋ถ„์€ ๋ฌผ๋ฆฌ์  ๋ฉ”๋ชจ๋ฆฌ์— ๋ชจ๋‘ ์˜ฌ๋ผ์™€ ์žˆ์Œ)

3) Blocked(wait, sleep)
- CPU๋ฅผ ์ฃผ์–ด๋„ ๋‹น์žฅ instruction์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์—†๋Š” ์ƒํƒœ.
- process ์ž์‹ ์ด ์š”์ฒญํ•œ event(I/O)๊ฐ€ ์ฆ‰์‹œ ๋งŒ์กฑ๋˜์ง€ ์•Š์•„ ์ด๋ฅผ ๊ธฐ๋‹ค๋ฆฌ๋Š” ์ƒํƒœ(ํ”„๋กœ์„ธ์Šค๋Š” ์•Œ์•„์„œ ์ผ์„ ํ•˜๊ณ  ์žˆ์Œ)
ex1) code๊ฐ€ ๋ฌผ๋ฆฌ์  ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ผ์™€ ์žˆ์ง€ ์•Š๊ณ  disk์— ์žˆ๋Š” ์ƒํƒœ
ex2) disk์—์„œ file์„ ์ฝ์–ด์™€์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ

4) Suspended(stopped)

- ์ค‘๊ธฐ ์Šค์ผ€์ค„๋Ÿฌ๋กœ ์ธํ•ด ์ƒ๊ธด ์ƒํƒœ

- ์™ธ๋ถ€์ ์ธ ์ด์œ ๋กœ ํ”„๋กœ์„ธ์Šค์˜ ์ˆ˜ํ–‰์ด ์ •์ง€๋œ ์ƒํƒœ

- ํ”„๋กœ์„ธ์Šค๋Š” ํ†ต์งธ๋กœ ๋””์Šคํฌ์— swap out๋œ๋‹ค

ex1) ์‚ฌ์šฉ์ž๊ฐ€ ํ”„๋กœ๊ทธ๋žจ์„ ์ผ์‹œ ์ •์ง€์‹œํ‚จ ๊ฒฝ์šฐ(break key)

ex2) ์‹œ์Šคํ…œ swapper์ด ์—ฌ๋Ÿฌ ์ด์œ ๋กœ ํ”„๋กœ์„ธ์Šค๋ฅผ ์ž ์‹œ ์ค‘๋‹จ์‹œํ‚ด(๋ฉ”๋ชจ๋ฆฌ์— ๋„ˆ๋ฌด ๋งŽ์€ ํ”„๋กœ์„ธ์Šค๊ฐ€ ์˜ฌ๋ผ์™€ ์žˆ์„ ๋•Œ)


- new : ํ”„๋กœ์„ธ์Šค๊ฐ€ ์ƒ์„ฑ์ค‘์ธ ์ƒํƒœ
- terminated : ์ˆ˜ํ–‰ execution์ด ๋๋‚œ ์ƒํƒœ(์ •๋ฆฌํ•  ๊ฒƒ์ด ๋‚จ์•„์žˆ์Œ)

* Blocked : ์ž์‹ ์ด ์š”์ฒญํ•œ event๊ฐ€ ๋งŒ์กฑ๋˜๋ฉด ready (์ผ์„ ํ•˜๊ณ  ์žˆ์Œ)

* Suspended : ์™ธ๋ถ€์—์„œ resumeํ•ด์ฃผ์–ด์•ผ active (์ˆ˜ํ–‰์ด ์ •์ง€๋˜์–ด ์žˆ์Œ)

 

ํ”„๋กœ์„ธ์Šค ์ƒํƒœ๋„

- running(user mode) : ํ”„๋กœ์„ธ์Šค๊ฐ€ ์ž์‹ ์˜ ์ฝ”๋“œ๋ฅผ ์ˆ˜ํ–‰

- running(monitor mode) : ์šด์˜์ฒด์ œ์˜ ์ฝ”๋“œ๋ฅผ ์ˆ˜ํ–‰(์šด์˜์ฒด์ œ๊ฐ€ runningํ•˜๊ณ  ์žˆ๋‹ค X. kernel ์ฝ”๋“œ๊ฐ€ ์ˆ˜ํ–‰๋œ๋‹ค)

- blocked : I/O๋ฅผ ๊ธฐ๋‹ค๋ฆฌ๋Š” ๊ฒฝ์šฐ & ๊ณต์œ  ๋ฐ์ดํ„ฐ ์ ‘๊ทผ ๋ง‰๋Š” ๊ฒฝ์šฐ

 

 

Process Control Block(PCB)

์šด์˜์ฒด์ œ๊ฐ€ ๊ฐ ํ”„๋กœ์„ธ์Šค๋ฅผ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ํ”„๋กœ์„ธ์Šค ๋‹น ์œ ์ง€ํ•˜๋Š” ์ •๋ณด

๋‹ค์Œ์˜ ๊ตฌ์„ฑ ์š”์†Œ๋ฅผ ๊ฐ€์ง„ ๊ตฌ์กฐ์ฒด๋กœ ์œ ์ง€ํ•œ๋‹ค

  • OS๊ฐ€ ๊ด€๋ฆฌ์ƒ ์‚ฌ์šฉํ•˜๋Š” ์ •๋ณด
    • Process state(ready/running/blocked), process ID
    • scheduling information, priority : ์šฐ์„ ์ˆœ์œ„ ์Šค์ผ€์ฅด๋ง
  • CPU ์ˆ˜ํ–‰ ๊ด€๋ จ ํ•˜๋“œ์›จ์–ด ๊ฐ’ : program counter, registers
  • ๋ฉ”๋ชจ๋ฆฌ ๊ด€๋ จ : code, data, stack์˜ ์œ„์น˜ ์ •๋ณด
  • ํŒŒ์ผ ๊ด€๋ จ : open file descriptors

 

๋ฌธ๋งฅ ๊ตํ™˜ Context Switch

CPU๋ฅผ ํ•œ (์‚ฌ์šฉ์ž) ํ”„๋กœ์„ธ์Šค์—์„œ ๋‹ค๋ฅธ (์‚ฌ์šฉ์ž) ํ”„๋กœ์„ธ์Šค๋กœ ๋„˜๊ฒจ์ฃผ๋Š” ๊ณผ์ •

CPU๊ฐ€ ๋‹ค๋ฅธ ํ”„๋กœ์„ธ์Šค์—๊ฒŒ ๋„˜์–ด๊ฐˆ ๋•Œ ์šด์˜์ฒด์ œ๋Š” ๋‹ค์Œ์„ ์ˆ˜ํ–‰

  • CPU๋ฅผ ๋บ๊ธธ ๋•Œ์˜ ํ”„๋กœ์„ธ์Šค์˜ ์ƒํƒœ๋ฅผ ๊ทธ ํ”„๋กœ์„ธ์Šค์˜ PCB(์ปค๋„ ์ฃผ์†Œ ๊ณต๊ฐ„)์— ์ €์žฅ
  • CPU๋ฅผ ์–ป๋Š” ํ”„๋กœ์„ธ์Šค์˜ ์ƒํƒœ๋ฅผ PCB์—์„œ ์ฝ์–ด์˜ด

System call์ด๋‚˜ interrupt(HW) ๋ฐœ์ƒ์‹œ ๋ฐ˜๋“œ์‹œ context switch๊ฐ€ ์ผ์–ด๋‚˜๋Š” ๊ฒƒ์€ ์•„๋‹˜

(์‚ฌ์šฉ์ž ํ”„๋กœ์„ธ์Šค -> ์šด์˜์ฒด์ œ๋กœ CPU ๋„˜์–ด๊ฐ)

(1) CPU ์ˆ˜ํ–‰ ์ •๋ณด ๋“ฑ context์˜ ์ผ๋ถ€๋ฅผ PCB์— saveํ•ด์•ผ ํ•˜์ง€๋งŒ ๋ฌธ๋งฅ๊ตํ™˜์„ ํ•˜๋Š” (2)์˜ ๊ฒฝ์šฐ ๊ทธ ๋ถ€๋‹ด์ด ํ›จ์”ฌ ํผ

ex) cache memory flush 

 

ํ”„๋กœ์„ธ์Šค๋ฅผ ์Šค์ผ€์ค„๋ง ํ•˜๊ธฐ ์œ„ํ•œ ํ

ํ”„๋กœ์„ธ์Šค๋“ค์€ ๊ฐ ํ๋ฅผ ์˜ค๊ฐ€๋ฉฐ ์ˆ˜ํ–‰๋œ๋‹ค.

1) Job Queue : ํ˜„์žฌ ์‹œ์Šคํ…œ ๋‚ด์— ์žˆ๋Š” ๋ชจ๋“  ํ”„๋กœ์„ธ์Šค์˜ ์ง‘ํ•ฉ(include ready queue, device queue)

2) Ready Queue : ํ˜„์žฌ ๋ฉ”๋ชจ๋ฆฌ ๋‚ด์— ์žˆ์œผ๋ฉด์„œ CPU๋ฅผ ๊ธฐ๋‹ค๋ฆฌ๋Š” ํ”„๋กœ์„ธ์Šค์˜ ์ง‘ํ•ฉ

3) Device Queues : I/O device์˜ ์ฒ˜๋ฆฌ๋ฅผ ๊ธฐ๋‹ค๋ฆฌ๋Š” ํ”„๋กœ์„ธ์Šค์˜ ์ง‘ํ•ฉ

Process๊ฐ€ ํ์— ์„œ๋Š” ๋ชจ์Šต

๊ทธ๋ฆผ) interrupt๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ์‹œ ready ์ƒํƒœ๋กœ ๋„˜์–ด๊ฐ€๋Š” ๊ฒƒ์€ ์•„๋‹˜

 

์Šค์ผ€์ฅด๋Ÿฌ Scheduler

  • Long-term scheduler ์žฅ๊ธฐ ์Šค์ผ€์ค„๋Ÿฌ / job scheduler
    • ์‹œ์ž‘ ํ”„๋กœ์„ธ์Šค ์ค‘ ์–ด๋–ค ๊ฒƒ๋“ค์„ ready queue๋กœ ๋ณด๋‚ผ์ง€ ๊ฒฐ์ •
    • ํ”„๋กœ์„ธ์Šค์— memory(๋ฐ ๊ฐ์ข… ์ž์›)์„ ์ฃผ๋Š” ๋ฌธ์ œ(memory์— ์˜ฌ๋ผ๊ฐ€๋Š” ๊ฒƒ์„ admit)
    • degree of multiprogramming์„ ์ œ์–ด(๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ผ๊ฐ€ ์žˆ๋Š” process์˜ ์ˆ˜ -> ๋„ˆ๋ฌด ์ ๊ฑฐ๋‚˜ ๋งŽ์œผ๋ฉด ์„ฑ๋Šฅ ์ด์Šˆ)
    • time sharing system์—๋Š” ๋ณดํ†ต ์žฅ๊ธฐ ์Šค์ผ€์ค„๋Ÿฌ๊ฐ€ ์—†์Œ(๋ฌด์กฐ๊ฑด ready)
      -> ์š”์ฆ˜์€ process ์‹œ์ž‘ํ•˜๋ฉด ๊ทธ๋ƒฅ ๋ฐ”๋กœ ready ์ƒํƒœ๋กœ ๋“ค์–ด๊ฐ
  • Short-term scheduler ๋‹จ๊ธฐ ์Šค์ผ€์ค„๋Ÿฌ / CPU scheduler
    • ์–ด๋–ค ํ”„๋กœ์„ธ์Šค๋ฅผ ๋‹ค์Œ๋ฒˆ์— running ์‹œํ‚ฌ์ง€ ๊ฒฐ์ •
    • ํ”„๋กœ์„ธ์Šค์— CPU๋ฅผ ์ฃผ๋Š” ๋ฌธ์ œ
    • ์ถฉ๋ถ„ํžˆ ๋นจ๋ผ์•ผ ํ•จ(milisecond ๋‹จ์œ„)
  • Medium-term scheduler ์ค‘๊ธฐ ์Šค์ผ€์ค„๋Ÿฌ / Swapper
    • ์—ฌ์œ  ๊ณต๊ฐ„ ๋งˆ๋ จ์„ ์œ„ํ•ด ํ”„๋กœ์„ธ์Šค๋ฅผ ํ†ต์งธ๋กœ ๋ฉ”๋ชจ๋ฆฌ์—์„œ ๋””์Šคํฌ๋กœ ์ซ“์•„๋ƒ„
    • ํ”„๋กœ์„ธ์Šค์—๊ฒŒ์„œ memory๋ฅผ ๋บ๋Š” ๋ฌธ์ œ
    • degree of multiprogramming์„ ์ œ์–ด
    • ํ˜„์žฌ ๋Œ€๋‹ค์ˆ˜์˜ ํ”„๋กœ๊ทธ๋žจ์ด ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• -> suspended ์ƒํƒœ ์ถ”๊ฐ€

 

Thread

ํ”„๋กœ์„ธ์Šค ๋‚ด๋ถ€์˜ CPU ์ˆ˜ํ–‰ ๋‹จ์œ„(์—ฌ๋Ÿฌ ๊ฐœ) 

a thread (or lightweight process) is a basic unit of CPU utilization

 

Thread์˜ ๊ตฌ์„ฑ(๋ณ„๋„)

  • program counter
  • register set
  • stack space

Thread๊ฐ€ ๋™๋ฃŒ thread์™€ ๊ณต์œ ํ•˜๋Š” ๋ถ€๋ถ„(=task)

  • code section
  • data section
  • OS resources

์ „ํ†ต์ ์ธ ๊ฐœ๋…์˜ heavyweight process๋Š” ํ•˜๋‚˜์˜ thread๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” task๋กœ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

 

Benifits of Threads

  • ๋‹ค์ค‘ ์Šค๋ ˆ๋“œ๋กœ ๊ตฌ์„ฑ๋œ task ๊ตฌ์กฐ์—์„œ๋Š” ํ•˜๋‚˜์˜ ์„œ๋ฒ„ ์Šค๋ ˆ๋“œ๊ฐ€ blocked/waiting ์ƒํƒœ์ธ ๋™์•ˆ์—๋„ ๋™์ผํ•œ task ๋‚ด์˜ ๋‹ค๋ฅธ thread๊ฐ€ ์‹คํ–‰running๋˜์–ด ๋น ๋ฅธ ์ฒ˜๋ฆฌ๋ฅผ ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์ž์› ์ ˆ์•ฝ : ๋™์ผํ•œ ์ผ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋‹ค์ค‘ thread๊ฐ€ ํ˜‘๋ ฅํ•˜์—ฌ ๋†’์€ ์ฒ˜๋ฆฌ์œจ(throughput)๊ณผ ์„ฑ๋Šฅ ํ–ฅ์ƒ์„ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค
    -> ์—ฌ๋Ÿฌ ๊ฐœ์˜ process๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ code/data/stack์„ ์ƒˆ๋กœ ๋งŒ๋“ค์–ด์•ผ ํ•จ
  • ๋ณ‘๋ ฌ์„ฑ์„ ๋†’์ผ ์ˆ˜ ์žˆ๋‹ค : ๋‹ค์ค‘ CPU ์ปดํ“จํ„ฐ์—๋งŒ ํ•œ์ •

Single and multithreaded process

 

  • 1) Responsiveness ์‘๋‹ต์„ฑ
    • multi-threaded web : ํ•˜๋‚˜์˜ thread๊ฐ€ blocked๋˜์–ด๋„ ๋‹ค๋ฅธ thread๋Š” continues
    • ๋™๊ธฐ์‹ ์ž…์ถœ๋ ฅ : I/O๋ฅผ ์–ป๋Š” ๋™์•ˆ block
    • ๋น„๋™๊ธฐ์‹ ์ž…์ถœ๋ ฅ : I/O๋ฅผ ์–ป๋Š” ๋™์•ˆ์—๋„ ํ™”๋ฉด์— ๋‹ค๋ฅธ text ๋„์›Œ์คŒ
  • 2) Resource Sharing : n threads can share binary code, data, resource of the process
  • 3) Economy
    • ํ”„๋กœ์„ธ์Šค ํ•˜๋‚˜ ๋งŒ๋“œ๋Š” ๊ฒƒ๋ณด๋‹ค thread ํ•˜๋‚˜ ๋งŒ๋“œ๋Š” ๊ฒŒ ๋” ๊ฒฝ์ œ์ ์ด๋‹ค
    • ํ”„๋กœ์„ธ์Šค ๊ฐ„์˜ ๋ฌธ๋งฅ๊ตํ™˜ overhead > thread๊ฐ„์˜ ๋ฌธ๋งฅ ๊ตํ™˜ overhead
    • creating & CPU switching thread(rather than a process)
    • solaris์˜ ๊ฒฝ์šฐ ์œ„ ๋‘ ๊ฐ€์ง€ overhead๊ฐ€ ๊ฐ๊ฐ 30๋ฐฐ, 5๋ฐฐ
  • 4) Utilization of MP Architectures : each thread may be running in parallel on a different processor

 

Implementation of threads

1) kernel thread(supported by kernel)

- thread๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ system์ด ์•Œ๊ณ  ์žˆ์Œ -> system์ด thread ๊ด€๋ฆฌ

- windows series, solaris, digital UNIX, Mach

2) user thread(supported by library)

- thread๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ system์ด ๋ชจ๋ฆ„ -> process ๋ณธ์ธ์ด thread ๊ด€๋ฆฌ -> ์ œ์•ฝ ์กฐ๊ฑด ์กด์žฌ

- POSIX Pthreads, Mach C-threads, Solaris threads

 

- some are real-time threads

๋Œ“๊ธ€