[์šด์˜์ฒด์ œ] ํ”„๋กœ์„ธ์Šค (1) | Process State, Context Switch, Scheduler
๐Ÿ–ฅ CS/์šด์˜์ฒด์ œ

[์šด์˜์ฒด์ œ] ํ”„๋กœ์„ธ์Šค (1) | Process State, Context Switch, Scheduler

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

⇒ ์‹คํ–‰ ์ค‘์ธ ํ”„๋กœ๊ทธ๋žจ

 

ํ”„๋กœ์„ธ์Šค์˜ ๊ตฌ์„ฑ

: ํ”„๋กœ๊ทธ๋žจ ์ฝ”๋“œ(text section), ํ”„๋กœ๊ทธ๋žจ ์นด์šดํ„ฐ, ํ”„๋กœ์„ธ์„œ ๋ ˆ์ง€์Šคํ„ฐ

 

  • Stack: ๋ฐ์ดํ„ฐ๋ฅผ ์ผ์‹œ์ ์œผ๋กœ ์ €์žฅํ•˜๋Š” ์˜์—ญ
  • Data section: ํ”„๋กœ๊ทธ๋žจ์˜ ๊ฐ€์ƒ ์ฃผ์†Œ ๊ณต๊ฐ„
  • Heap: ์ฝ”๋“œ ์˜์—ญ๊ณผ๋Š” ๋ณ„๋„๋กœ ์œ ์ง€๋˜๋Š” ์ž์œ  ์˜์—ญ
  • Code: ์‹คํ–‰ ๋ช…๋ น์„ ํฌํ•จํ•˜๋Š” ๋ฉ”๋ชจ๋ฆฌ์ด๊ฑฐ๋‚˜ ๋ชฉ์  ํŒŒ์ผ์— ์žˆ๋Š” ํ”„๋กœ๊ทธ๋žจ ์˜์—ญ
  • ํ”„๋กœ์„ธ์Šค์˜ ๋ฌธ๋งฅ(context)
    • CPU ์ˆ˜ํ–‰ ์ƒํƒœ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ํ•˜๋“œ์›จ์–ด ๋ฌธ๋งฅ
      • Program Counter
      • ๊ฐ์ข… register
    • ํ”„๋กœ์„ธ์Šค์˜ ์ฃผ์†Œ ๊ณต๊ฐ„
      • code, data, stack
    • ํ”„๋กœ์„ธ์Šค ๊ด€๋ จ ์ปค๋„ ์ž๋ฃŒ๊ตฌ์กฐ
      • PCB
      • Kernel stack

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

  • ํ”„๋กœ์„ธ์Šค๋Š” ์ƒํƒœ(state)๊ฐ€ ๋ณ€๊ฒฝ๋˜๋ฉฐ ์‹คํ–‰๋œ๋‹ค.
  1. Running
    1. CPU๋ฅผ ์žก๊ณ  instruction์„ ์ˆ˜ํ–‰ ์ค‘์ธ ์ƒํƒœ
  2. Ready
    1. CPU๋ฅผ ๊ธฐ๋‹ค๋ฆฌ๋Š” ์ƒํƒœ(๋ฉ”๋ชจ๋ฆฌ ๋“ฑ ๋‹ค๋ฅธ ์กฐ๊ฑด์„ ๋ชจ๋‘ ๋งŒ์กฑํ•˜๊ณ )
  3. Blocked(wait, sleep)
    1. CPU๋ฅผ ์ฃผ์–ด๋„ ๋‹น์žฅ instruction์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์—†๋Š” ์ƒํƒœ
    2. Process ์ž์‹ ์ด ์š”์ฒญํ•œ event(ex. I/O)๊ฐ€ ์ฆ‰์‹œ ๋งŒ์กฑ๋˜์ง€ ์•Š์•„ ์ด๋ฅผ ๊ธฐ๋‹ค๋ฆฌ๋Š” ์ƒํƒœ
    3. ์ž์‹ ์ด ์š”์ฒญํ•œ event๊ฐ€ ๋งŒ์กฑ๋˜๋ฉด Ready
    ex) ๋””์Šคํฌ์—์„œ file์„ ์ฝ์–ด์™€์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ
  4. Suspended (stopped)
    1. ์™ธ๋ถ€์ ์ธ ์ด์œ ๋กœ ํ”„๋กœ์„ธ์Šค์˜ ์ˆ˜ํ–‰์ด ์ •์ง€๋œ ์ƒํƒœ
    2. ํ”„๋กœ์„ธ์Šค๋Š” ํ†ต์งธ๋กœ ๋””์Šคํฌ์— swap out๋œ๋‹ค.
    ex) ์‚ฌ์šฉ์ž๊ฐ€ ํ”„๋กœ๊ทธ๋žจ์„ ์ผ์‹œ ์ •์ง€์‹œํ‚จ ๊ฒฝ์šฐ (break key)
    ์‹œ์Šคํ…œ์ด ์—ฌ๋Ÿฌ ์ด์œ ๋กœ ํ”„๋กœ์„ธ์Šค๋ฅผ ์ž ์‹œ ์ค‘๋‹จ์‹œํ‚ด
    (๋ฉ”๋ชจ๋ฆฌ์— ๋„ˆ๋ฌด ๋งŽ์€ ํ”„๋กœ์„ธ์Šค๊ฐ€ ์˜ฌ๋ผ์™€ ์žˆ์„ ๋•Œ)
  5. New
    1. ํ”„๋กœ์„ธ์Šค๊ฐ€ ์ƒ์„ฑ ์ค‘์ธ ์ƒํƒœ
  6. Terminated
    1. ์ˆ˜ํ–‰(execution)์ด ๋๋‚œ ์ƒํƒœ

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

Process Control Block(PCB): task control block

  • ์šด์˜์ฒด์ œ๊ฐ€ ๊ฐ ํ”„๋กœ์„ธ์Šค๋ฅผ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ํ”„๋กœ์„ธ์Šค ๋‹น ์œ ์ง€ํ•˜๋Š” ์ •๋ณด
  • ๊ฐ process๋Š” OS์—์„œ PCB์— ์˜ํ•ด ํ‘œํ˜„

  • ๋‹ค์Œ์˜ ๊ตฌ์„ฑ ์š”์†Œ๋ฅผ ๊ฐ€์ง„๋‹ค. (๊ตฌ์กฐ์ฒด๋กœ ์œ ์ง€)
    1. OS๊ฐ€ ๊ด€๋ฆฌ์ƒ ์‚ฌ์šฉํ•˜๋Š” ์ •๋ณด
      1. Process state, Process ID
      2. scheduling information, priority
        1. scheduling information: priorites, scheduling queue pointers
    2. CPU ์ˆ˜ํ–‰ ๊ด€๋ จ ํ•˜๋“œ์›จ์–ด ๊ฐ’
      1. Program counter, registers
        1. program counter: location of instruction to next execute
        2. CPU registers: contents of all process - centric registers
    3. ๋ฉ”๋ชจ๋ฆฌ ๊ด€๋ จ
      1. Code, data, stack์˜ ์œ„์น˜ ์ •๋ณด
    4. ํŒŒ์ผ ๊ด€๋ จ
      1. Open file descriptors

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

: Switching of execution information

  • CPU๋ฅผ ํ•œ ํ”„๋กœ์„ธ์Šค(or ์Šค๋ ˆ๋“œ) ์—์„œ ๋‹ค๋ฅธ ํ”„๋กœ์„ธ์Šค(or ์Šค๋ ˆ๋“œ)๋กœ ๋„˜๊ฒจ์ฃผ๋Š” ๊ณผ์ •
  • ํ•˜๋‚˜์˜ ์ž‘์—… (or ํ”„๋กœ์„ธ์Šค)์—์„œ ๋‹ค๋ฅธ ์ž‘์—… (or ํ”„๋กœ์„ธ์Šค)๋กœ ๋ณ€๊ฒฝํ•˜๊ธฐ ์œ„ํ•ด ์ปดํ“จํ„ฐ์˜ CPU๊ฐ€ ๋”ฐ๋ฅด๋Š” ์ž‘์—…์ด ์ถฉ๋Œํ•˜์ง€ ์•Š๋„๋ก ํ•˜๋Š” ์ ˆ์ฐจ
  • CPU๊ฐ€ ๋‹ค๋ฅธ ํ”„๋กœ์„ธ์Šค์—๊ฒŒ ๋„˜์–ด๊ฐˆ ๋•Œ ์šด์˜์ฒด์ œ๋Š” ๋‹ค์Œ์„ ์ˆ˜ํ–‰
    • CPU๋ฅผ ๋‚ด์–ด์ฃผ๋Š” ํ”„๋กœ์„ธ์Šค์˜ ์ƒํƒœ๋ฅผ ๊ทธ ํ”„๋กœ์„ธ์Šค์˜ PCB์— ์ €์žฅ
    • CPU๋ฅผ ์ƒˆ๋กญ๊ฒŒ ์–ป๋Š” ํ”„๋กœ์„ธ์Šค์˜ ์ƒํƒœ๋ฅผ PCB์—์„œ ์ฝ์–ด์˜ด

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

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

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

  • Job Queue
    • ํ˜„์žฌ ์‹œ์Šคํ…œ ๋‚ด์— ์žˆ๋Š” ๋ชจ๋“  ํ”„๋กœ์„ธ์Šค์˜ ์ง‘ํ•ฉ
  • Ready queue
    • ํ˜„์žฌ ๋ฉ”๋ชจ๋ฆฌ ๋‚ด์— ์žˆ์œผ๋ฉด์„œ CPU๋ฅผ ์žก์•„์„œ ์‹คํ–‰๋˜๊ธฐ๋ฅผ ๊ธฐ๋‹ค๋ฆฌ๋Š” ํ”„๋กœ์„ธ์Šค์˜ ์ง‘ํ•ฉ
  • Device queues
    • I/O device์˜ ์ฒ˜๋ฆฌ๋ฅผ ๊ธฐ๋‹ค๋ฆฌ๋Š” ํ”„๋กœ์„ธ์Šค์˜ ์ง‘ํ•ฉ
  • ํ”„๋กœ์„ธ์Šค๋“ค์€ ๊ฐ ํ๋“ค์„ ์˜ค๊ฐ€๋ฉฐ ์ˆ˜ํ–‰๋œ๋‹ค.

์Šค์ผ€์ค„๋Ÿฌ

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