Process

bbookngยท2023๋…„ 6์›” 5์ผ
0

์šด์˜์ฒด์ œ

๋ชฉ๋ก ๋ณด๊ธฐ
2/10

๐Ÿ“Œ Process

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

  • Process ia a program in execution

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

  • ํŠน์ • ์‹œ์ ์„ ๋†“๊ณ  ๋ณด์•˜์„๋•Œ ํ•ด๋‹น ํ”„๋กœ์„ธ์Šค๊ฐ€ ์–ด๋””๊นŒ์ง€ ์‹คํ–‰ํ–ˆ๊ณ  ์–ด๋–ค ์ƒํƒœ์ธ๊ฐ€
  • ํ”„๋กœ์„ธ์Šค๊ฐ€ ์ƒ์„ฑ๋˜๋ฉด ์ฃผ์†Œ๋ฅผ ๋งŒ๋“ค๊ณ  CPU๊ฐ€ ํ•ด๋‹น ํ”„๋กœ์„ธ์Šค ์ˆ˜ํ–‰ ์‹œ์ž‘
  • ์–ด๋Š ์‹œ์ ์— ํ”„๋กœ์„ธ์Šค๋Š” ์–ด๋””๊นŒ์ง€ ์™€์žˆ๋Š”๊ฐ€?
  • CPU ์ˆ˜ํ–‰ ์ƒํƒœ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ํ•˜๋“œ์›จ์–ด ๋ฌธ๋งฅ
    • Program Counter - ์–ด๋”œ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ๋Š”๊ฐ€? (ํ”„๋กœ์„ธ์Šค์˜ ์–ด๋Š ๋ถ€๋ถ„์ธ๊ฐ€?)
    • ๊ฐ์ข… register - ์–ด๋–ค instruction๊นŒ์ง€ ์‹คํ–‰ํ–ˆ๋Š”๊ฐ€?
  • ํ”„๋กœ์„ธ์Šค์˜ ์ฃผ์†Œ ๊ณต๊ฐ„
    • code, data, stack ์— ์–ด๋–ค ๋‚ด์šฉ์ด ๋“ค์–ด์žˆ๋Š”๊ฐ€ ?
      • data : queue, PCB
  • ํ”„๋กœ์„ธ์Šค ๊ด€๋ จ ์ปค๋„ ์ž๋ฃŒ ๊ตฌ์กฐ
    • PCB (Process Control Block)
      • Process๊ฐ€ ํ•˜๋‚˜ ์ƒ๊ธธ ๋•Œ ๋งˆ๋‹ค ์šด์˜์ฒด์ œ๋Š” PCB๋Š” ํ•˜๋‚˜์”ฉ ๋‘๊ณ  ์žˆ์œผ๋ฉด์„œ CPU, memory๋ฅผ ์–ผ๋งˆ๋‚˜ ์ฃผ์–ด์•ผ ํ• ์ง€ ๊ด€๋ฆฌ.
      • ์šด์˜์ฒด์ œ๊ฐ€ PCB๋ฅผ ์–ด๋–ป๊ฒŒ ํ‰๊ฐ€ํ•˜๋Š”์ง€ ์•Œ์•„์•ผ ํ•จ
      • OS์—์„œ ํ”„๋กœ์„ธ์Šค๋“ค์„ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ์ž๋ฃŒ๊ตฌ์กฐ
    • Kernel stack
      • ๊ฐ ํ”„๋กœ์„ธ์Šค๊ฐ€ ์ˆ˜ํ–‰ ๋ถˆ๊ฐ€๋Šฅํ•œ ์ž‘์—…์„ ์œ„ํ•ด ์‹œ์Šคํ…œ ์ฝœ์„ ํ•  ๋•Œ ์Œ“์ด๋Š” ์Šคํƒ
        • ํ”„๋กœ์„ธ์Šค๋ณ„๋กœ ์ปค๋„ ์Šคํƒ์„ ๋”ฐ๋กœ ๋‘ 
  • ๋ฉ€ํ‹ฐ ํƒœ์Šคํ‚น์„ ์œ„ํ•ด ํ”„๋กœ์„ธ์Šค๊ฐ€ ๋ณ€๊ฒฝ๋  ๋•Œ ๋ฌธ๋งฅ์„ ๋ฐฑ์—… ํ•ด๋‘์–ด์•ผ ํ•จ.

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

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

  • Running
    • CPU๋ฅผ ์žก๊ณ  instruction์„ ์ˆ˜ํ–‰์ค‘์ธ ์ƒํƒœ
  • Ready
    • CPU๋ฅผ ๊ธฐ๋‹ค๋ฆฌ๋Š” ์ƒํƒœ(๋ฉ”๋ชจ๋ฆฌ ๋“ฑ ๋‹ค๋ฅธ ์กฐ๊ฑด์„ ๋ชจ๋‘ ๋งŒ์กฑํ•˜๊ณ )
    • ๋ฌผ๋ฆฌ์ ์ธ ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ผ์™€ ์žˆ๋Š” ์ƒํƒœ. CPU๋งŒ ์–ป์œผ๋ฉด ๋˜๋Š” ์ƒํƒœ
  • Blocked (wait, sleep)
    • CPU๋ฅผ ์ฃผ์–ด๋„ ๋‹น์žฅ instruction์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์—†๋Š” ์ƒํƒœ
    • Process ์ž์‹ ์ด ์š”์ฒญํ•œ event(์˜ˆ: I/O)๊ฐ€ ์ฆ‰์‹œ ๋งŒ์กฑ๋˜์ง€ ์•Š์•„ ์ด๋ฅผ ๊ธฐ๋‹ค๋ฆฌ๋Š” ์ƒํƒœ
    • ์˜ˆ ) ๋””์Šคํฌ์—์„œ file์„ ์ฝ์–ด์™€์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ
  • Suspended (stopped)
    • ์™ธ๋ถ€์ ์ธ ์ด์œ (์ค‘๊ธฐ ์Šค์ผ€์ค„๋Ÿฌ, ์‚ฌ์šฉ์ž etc)๋กœ ํ”„๋กœ์„ธ์Šค์˜ ์ˆ˜ํ–‰์ด ์ •์ง€๋œ ์ƒํƒœ
    • ํ”„๋กœ์„ธ์Šค๋Š” ํ†ต์งธ๋กœ ๋””์Šคํฌ์— swop out ๋œ๋‹ค.
    • ex) ์‚ฌ์šฉ์ž๊ฐ€ ํ”„๋กœ๊ทธ๋žจ์„ ์ผ์‹œ ์ •์ง€ ์‹œํ‚จ ๊ฒฝ์šฐ ์‚ฌ์šฉ์ž๊ฐ€ ๋‹ค์‹œ ์žฌ๊ฐœํ•  ๋•Œ๊นŒ์ง€ Activeํ•œ ์ƒํƒœ๊ฐ€ ๋  ์ˆ˜ ์—†์Œ
  • New
    • ํ”„๋กœ์„ธ์Šค๊ฐ€ ์ƒ์„ฑ์ค‘์ธ ์ƒํƒœ
  • Terminated
    • ์ˆ˜ํ–‰(excution)์ด ๋๋‚œ ์ƒํƒœ (์•„์ง ํ”„๋กœ์„ธ์Šค๋Š” kill ๋˜์ง€ ์•Š์Œ)

โœจ Process Control Block (PCB)

  • ์šด์˜์ฒด์ œ๊ฐ€ ๊ฐ ํ”„๋กœ์„ธ์Šค๋ฅผ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ํ”„๋กœ์„ธ์Šค๋‹น ์œ ์ง€ํ•˜๋Š” ์ •๋ณด
  • ๋‹ค์Œ์˜ ๊ตฌ์„ฑ ์š”์†Œ๋ฅผ ๊ฐ€์ง„๋‹ค. (๊ตฌ์กฐ์ฒด๋กœ ์œ ์ง€)

(1) OS๊ฐ€ ๊ด€๋ฆฌ์ƒ ์‚ฌ์šฉํ•˜๋Š” ์ •๋ณด

  • Process state, Process ID
  • scheduiling information, Priority

(2) CPU ์ˆ˜ํ–‰ ๊ด€๋ จ ํ•˜๋“œ์›จ์–ด ๊ฐ’

  • Program counter, registers

(3) ๋ฉ”๋ชจ๋ฆฌ ๊ด€๋ จ

  • Code, data, stack์˜ ์œ„์น˜ ์ •๋ณด

(4) ํŒŒ์ผ ๊ด€๋ จ

  • Open file descriptros ..

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

  • CPU๋ฅผ ํ•œ ํ”„๋กœ์„ธ์Šค์—์„œ ๋‹ค๋ฅธ ํ”„๋กœ์„ธ์Šค๋กœ ๋„˜๊ฒจ์ฃผ๋Š” ๊ณผ์ •
  • CPU๊ฐ€ ๋‹ค๋ฅธ ํ”„๋กœ์„ธ์Šค์—๊ฒŒ ๋„˜์–ด๊ฐˆ ๋•Œ ์šด์˜์ฒด์ œ๋Š” ๋‹ค์Œ์„ ์ˆ˜ํ–‰ (๋ฉ”๋ชจ๋ฆฌ ์ปค๋„์—์„œ ์ˆ˜ํ–‰)
    • CPU๋ฅผ ๋‚ด์–ด์ฃผ๋Š” ํ”„๋กœ์„ธ์Šค์˜ ์ƒํƒœ๋ฅผ ๊ทธ ํ”„๋กœ์„ธ์Šค์˜ PCB์— ์ €์žฅ
    • CPU๋ฅผ ์ƒˆ๋กญ๊ฒŒ ์–ป๋Š” ํ”„๋กœ์„ธ์Šค์˜ ์ƒํƒœ๋ฅผ PCB์—์„œ ์ฝ์–ด์˜ด
  • System call์ด๋‚˜ (ํ•˜๋“œ์›จ์–ด) Interrupt ๋ฐœ์ƒ์‹œ ๋ฐ˜๋“œ์‹œ context switch๊ฐ€ ์ผ์–ด๋‚˜๋Š” ๊ฒƒ์€ ์•„๋‹˜
    • not context swtich: process A โ†’ OS โ†’ process A
      • ์ด๋•Œ๋„ CPU ์ˆ˜ํ–‰ ์ •๋ณด ๋“ฑ context์˜ ์ผ๋ถ€๋ฅผ PCB์— saveํ•ด์•ผ ํ•˜์ง€๋งŒ Overhead๊ฐ€ ์ ์Œ
    • context switch: process A โ†’ process B
      • ์บ์‹œ ๋ฉ”๋ชจ๋ฆฌ ๋ชจ๋‘ ์ง€์›€(flush) โ†’ ํฐ Overhead
    • ์‚ฌ์šฉ์ž ํ”„๋กœ์„ธ์Šค๋กœ๋ถ€ํ„ฐ CPU๊ฐ€ ํ”„๋กœ์„ธ์Šค๋กœ ๋„˜์–ด๊ฐ€๋Š” ๊ฒƒ์„ context switch๋ผ๊ณ  ํ•˜์ง€ ์•Š์Œ

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

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

โœจ ์Šค์ผ€์ค„๋Ÿฌ (Scheduler)

  • Long-term Scheduler (์žฅ๊ธฐ ์Šค์ผ€์ค„๋Ÿฌ or job scheduler)
    • ์‹œ์ž‘ ํ”„๋กœ์„ธ์Šค ์ค‘ ์–ด๋–ค ๊ฒƒ๋“ค์„ ready queue๋กœ ๋ณด๋‚ผ ์ง€ ๊ฒฐ์ •
    • ํ”„๋กœ์„ธ์Šค์— memory (๋ฐ ๊ฐ์ข… ์ž์›)์„ ์ฃผ๋Š” ๋ฌธ์ œ
      • ์‹œ์ž‘๋  ๋•Œ, ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ผ๊ฐ€๋Š” ๊ฒƒ์„ admit
    • 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์„ ์ œ์–ด

โœจ Thread

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

  • Process ๋‚ด๋ถ€์— CPU ์ˆ˜ํ–‰๋‹จ์œ„๊ฐ€ ์—ฌ๋Ÿฌ๊ฐœ ์žˆ๋Š” ๊ฒƒ

๐Ÿ“ข Thread์˜ ๊ตฌ์„ฑ

  • program counter
  • register set
  • stack space

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

  • code section
  • data section
  • OS resources

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

๐Ÿ“ข ์žฅ์ 

  • ๋‹ค์ค‘ thread๋กœ ๊ตฌ์„ฑ๋œ ํƒœ์Šคํฌ ๊ตฌ์กฐ์—์„œ๋Š” ํ•˜๋‚˜์˜ ์„œ๋ฒ„ thread๊ฐ€ blocked (waiting) ์ƒํƒœ์ธ ๋™์•ˆ์—๋„ ๋™์ผํ•œ task ๋‚ด์˜ ๋‹ค๋ฅธ thread๊ฐ€ ์‹คํ–‰(running) ๋˜์–ด ๋น ๋ฅธ ์ฒ˜๋ฆฌ๋ฅผ ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๋™์ผํ•œ ์ผ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋‹ค์ค‘ thread๊ฐ€ ํ˜‘๋ ฅํ•˜์—ฌ ๋†’์€ ์ฒ˜๋ฆฌ์œจ (throughput)๊ณผ ์„ฑ๋Šฅ ํ–ฅ์ƒ์„ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.
  • thread๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ณ‘๋ ฌ์„ฑ์„ ๋†’์ผ ์ˆ˜ ์žˆ๋‹ค.
  • Responsiveness
    • ex) multi-threaded Web - if one thread is blocked (ex. network), another thread continues (ex. display)
  • Resourece Sharing
    • n threads can share binary code, data, resource of the process
  • Economy
    • creating & CPU switching thread (rather than a process)
    • Solaris์˜ ๊ฒฝ์šฐ ์œ„ ๋‘ ๊ฐ€์ง€ overhead๊ฐ€ ๊ฐ๊ฐ 30๋ฐฐ, 5๋ฐฐ
  • Utilization of MP Architectures
    • each thread may be running in parallel on a different processor

โœจ Implementation of Threads

๐Ÿ“ข Some are supported by kernal > kenel Threads

  • Windows 95/98/NT
  • Solaris
  • Digital UNIX, Mach

๐Ÿ“ขOthers are supported by library > User Threads

  • POSIX Pthreads
  • Mach C-threads
  • Solaris threads

๐Ÿ“ข Some are real-time threads

๐Ÿ“Œ ์งˆ๋ฌธ

โœจ Question

  1. ํ”„๋กœ์„ธ์Šค์™€ ์Šค๋ ˆ๋“œ์˜ ์ฐจ์ด๋ฅผ ์„ค๋ช…ํ•ด๋ณด์„ธ์š”.
  2. ๋™๊ธฐ์‹ ์ž…์ถœ๋ ฅ๊ณผ ๋น„๋™๊ธฐ์‹ ์ž…์ถœ๋ ฅ์˜ ์ฐจ์ด์— ๋Œ€ํ•ด ์„ค๋ช…ํ•ด๋ณด์„ธ์š”.

โœจ Answer

  1. ํ”„๋กœ์„ธ์Šค๋Š” ์‹คํ–‰์ค‘์ธ ํ”„๋กœ๊ทธ๋žจ์„ ๋งํ•˜๊ณ , ์Šค๋ ˆ๋“œ๋Š” ํ”„๋กœ์„ธ์Šค ๋‚ด๋ถ€์— CPU ์ˆ˜ํ–‰๋‹จ์œ„๊ฐ€ ์—ฌ๋Ÿฌ๊ฐœ ์žˆ๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค. ํ”„๋กœ์„ธ์Šค๋Š” ์ž์›์„ ์šด์˜์ฒด์ œ๋กœ๋ถ€ํ„ฐ ํ• ๋‹น๋ฐ›๊ณ , ์Šค๋ ˆ๋“œ๋Š” ํ”„๋กœ์„ธ์Šค๋กœ๋ถ€ํ„ฐ ํ• ๋‹น๋ฐ›๋Š”๋‹ค. ํ”„๋กœ์„ธ์Šค๋ณด๋‹ค ๋ฌธ๋งฅ ๊ตํ™˜์—๋„ ์šฉ์ดํ•˜๋ฉฐ ํ•˜๋‚˜์˜ ์Šค๋ ˆ๋“œ๊ฐ€ blocked ์ƒํƒœ์ผ ๋•Œ ๋˜ ๋‹ค๋ฅธ ์Šค๋ ˆ๋“œ๊ฐ€ ์‹คํ–‰๋  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋น ๋ฅธ ์ฒ˜๋ฆฌ๋ฅผ ํ•  ์ˆ˜ ์žˆ๋‹ค.

  1. ๋™๊ธฐ์‹ ์ž…์ถœ๋ ฅ์€ I/O ์š”์ฒญ ํ›„ ์ž…์ถœ๋ ฅ ์ž‘์—…์ด ์™„๋ฃŒ๋œ ํ›„์—์•ผ ์ œ์–ด๊ฐ€ ์‚ฌ์šฉ์ž ํ”„๋กœ๊ทธ๋žจ์— ๋„˜์–ด๊ฐ€์ง€๋งŒ ๋น„๋™๊ธฐ์‹ ์ž…์ถœ๋ ฅ์€ I/O๊ฐ€ ์‹œ์ž‘๋œ ํ›„ ์ž…์ถœ๋ ฅ ์ž‘์—…์ด ๋๋‚˜๊ธฐ๋ฅผ ๊ธฐ๋‹ค๋ฆฌ์ง€ ์•Š๊ณ  ์ œ์–ด๊ฐ€ ์‚ฌ์šฉ์ž ํ”„๋กœ๊ทธ๋žจ์— ์ฆ‰์‹œ ๋„˜์–ด๊ฐ„๋‹ค.

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