Home ๐Ÿ’พ [Operating System] Process & Thread ๐Ÿ’พ
Post
Cancel

๐Ÿ’พ [Operating System] Process & Thread ๐Ÿ’พ

๐Ÿ’พ Process

Process๋ž€ ํ”„๋กœ๊ทธ๋žจ์ด ๋ฉ”๋ชจ๋ฆฌ์— ์ ์žฌ๋˜์–ด ์‹คํ–‰๋˜๋Š” ๊ฒƒ์„ ๋งํ•ฉ๋‹ˆ๋‹ค.

Process์˜ ์ข…๋ฅ˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  • Foreground Process: User๊ฐ€ ๋ณผ ์ˆ˜ ์žˆ๋Š” ๊ณต๊ฐ„์—์„œ ์‹คํ–‰๋˜๋Š” Process
  • Background Process: User๊ฐ€ ๋ณด์ง€ ๋ชปํ•˜๋Š” ๊ณต๊ฐ„์—์„œ ์‹คํ–‰๋˜๋Š” Process
    • Demon(Service): User์™€ ์ƒํ˜ธ์ž‘์šฉํ•˜์ง€ ์•Š๊ณ  ์ •ํ•ด์ง„ ์ผ๋งŒ ์ˆ˜ํ–‰ํ•˜๋Š” Process
      • Unix โžœ Demon
      • Window โžœ Service

๐Ÿ’พ PCB(Process Control Block)

PCB๋ž€ Process๋ฅผ ์‹๋ณ„ํ•˜๊ธฐ ์œ„ํ•ด ๊ผญ ํ•„์š”ํ•œ ์ •๋ณด(Context)๋ฅผ ์ €์žฅํ•˜๋Š” ์ž๋ฃŒ๊ตฌ์กฐ์ด๋ฉฐ Process ์ƒ์„ฑ์‹œ์— Kernel ์˜์—ญ์— ๋งŒ๋“ค์–ด์ง€๊ณ  ์‹คํ–‰์ด ์ข…๋ฃŒ๋˜๋ฉด ํ๊ธฐ๋ฉ๋‹ˆ๋‹ค.

Context์— ํฌํ•จ๋˜๋Š” ์ •๋ณด์—๋Š” OS๋งˆ๋‹ค ์ฐจ์ด๊ฐ€ ์žˆ์ง€๋งŒ ์ผ๋ฐ˜์ ์œผ๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ •๋ณด๋“ค์ด ํฌํ•จ๋ฉ๋‹ˆ๋‹ค.

  • PID(Process ID): ํŠน์ • Process๋ฅผ ์‹๋ณ„ํ•˜๊ธฐ ์œ„ํ•ด ๋ถ€์—ฌํ•˜๋Š” ๊ณ ์œ ํ•œ ๋ฒˆํ˜ธ
  • Register Value: ํ•ด๋‹น Process๊ฐ€ ์‹คํ–‰ํ•˜๋ฉฐ ์‚ฌ์šฉํ–ˆ๋˜ PC(Program Counter)์™€ ๊ฐ™์€ ๋ ˆ์ง€์Šคํ„ฐ ๊ฐ’๋“ค์„ ์ €์žฅ
  • Process State: ํ•ด๋‹น Process์˜ ํ˜„์žฌ ์ƒํƒœ๋ฅผ ์ €์žฅ
  • CPU Scheduling: ํ•ด๋‹น Process๊ฐ€ CPU๋ฅผ ์–ธ์ œ, ์–ด๋–ค ์ˆœ์„œ๋กœ ํ• ๋‹น ๋ฐ›์„์ง€์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ €์žฅ
  • Memory Management: ํ•ด๋‹น Process๊ฐ€ ์ €์žฅ๋œ ๋ฉ”๋ชจ๋ฆฌ์˜ ์œ„์น˜๋ฅผ ์ €์žฅ
  • Used File: ์–ด๋–ค File๋“ค์„ ์—ด์—ˆ๋Š”์ง€์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ €์žฅ
  • Used I/O Device: ์–ด๋–ค I/O Device๊ฐ€ ํ•ด๋‹น Process์— ํ• ๋‹น๋˜์—ˆ๋Š”์ง€์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ €์žฅ

Process๋“ค์€ ์ฐจ๋ก€๋Œ€๋กœ ๋Œ์•„๊ฐ€๋ฉฐ ํ•œ์ •๋œ ์‹œ๊ฐ„๋งŒํผ CPU๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๋ฐ ํŠน์ • Process๊ฐ€ ์ •ํ•ด์ง„ ์‹œ๊ฐ„๋งŒํผ ๋ชจ๋‘ ์‚ฌ์šฉํ–ˆ๊ฑฐ๋‚˜ ์˜ˆ๊ธฐ์น˜ ๋ชปํ•œ ์ƒํ™ฉ์ด ๋ฐœ์ƒํ•˜์—ฌ Interrupt๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๊ฒฝ์šฐ OS๋Š” PCB์— Context๋ฅผ ๋ฐฑ์—…ํ•˜๊ณ  ์ดํ›„ ๋‹ค์‹œ ํ•ด๋‹น Process๊ฐ€ CPU๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ PCB์— ์ €์žฅ๋œ Context๋ฅผ ๋ณต๊ตฌํ•˜์—ฌ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ด์ฒ˜๋Ÿผ PCB์— Context ๋ฐฑ์—…ํ•˜๊ณ  ๋‹ค์‹œ ๋ณต๊ตฌํ•˜์—ฌ ์‹คํ–‰ํ•˜๋Š” ๊ณผ์ •์„ Context Switching์ด๋ผ ํ•ฉ๋‹ˆ๋‹ค.

Context Switching์ด ์ž์ฃผ ์ผ์–ด๋‚˜๋ฉด Process๋“ค์ด ๋™์‹œ์— ์‹คํ–‰๋˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ด์ง€๋งŒ Overhead๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

๐Ÿ’พ Memory

Process๋Š” User Space์—์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‚˜๋‰˜์–ด ๋ฉ”๋ชจ๋ฆฌ์— ์ €์žฅ๋ฉ๋‹ˆ๋‹ค.

๋™์ ์œผ๋กœ ํ• ๋‹น๋˜๋Š” Heap Segment์™€ Stack Segment์˜ ์ฃผ์†Œ๊ฐ€ ๊ฒน์น˜์ง€ ์•Š๋„๋ก Heap Segment๋Š” ๋ฉ”๋ชจ๋ฆฌ์˜ ๋‚ฎ์€ ์ฃผ์†Œ์—์„œ ์‹œ์ž‘ํ•˜๊ณ  Stack Segment๋Š” ๋†’์€ ์ฃผ์†Œ์—์„œ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.

ย Definition์ •์  or ๋™์ 
Code Segment
(Text Segment)
CPU๊ฐ€ ์‹คํ–‰ํ•  ๊ธฐ๊ณ„์–ด๋กœ ์ด๋ฃจ์–ด์ง„ ๋ช…๋ น์–ด๊ฐ€ ์ €์žฅ
(Read-Only)
์ •์  ํ• ๋‹น ์˜์—ญ
Data SegmentProgram์ด ์‹คํ–‰๋˜๋Š” ๋™์•ˆ ์œ ์ง€ํ•  ๋ฐ์ดํ„ฐ๊ฐ€ ์ €์žฅ
(Ex. ์ „์—ญ ๋ณ€์ˆ˜)
์ •์  ํ• ๋‹น ์˜์—ญ
Heap Segmentํ”„๋กœ๊ทธ๋ž˜๋จธ๊ฐ€ ์ง์ ‘ ํ• ๋‹นํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ์–ธ์  ๊ฐ€๋Š” ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•˜๋Š” ๊ณต๊ฐ„
(โš ๏ธ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š์œผ๋ฉด Memory Leak ๋ฌธ์ œ ๋ฐœ์ƒ)
๋™์  ํ• ๋‹น ์˜์—ญ
Stack Segment๋ฐ์ดํ„ฐ๋ฅผ ์ผ์‹œ์ ์œผ๋กœ ์ €์žฅํ•˜๋Š” ๊ณต๊ฐ„
(Ex. ๋งค๊ฐœ๋ณ€์ˆ˜, ์ง€์—ญ ๋ณ€์ˆ˜)
๋™์  ํ• ๋‹น ์˜์—ญ



๐Ÿ’พ Process State

PCB์— ์ €์žฅ๋˜๋Š” Process State๋Š” OS๋งˆ๋‹ค ์ƒ์ดํ•˜์ง€๋งŒ ์ผ๋ฐ˜์ ์œผ๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ƒํƒœ๋ฅผ ๊ฑฐ์น˜๋ฉฐ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.

  • ์ƒ์„ฑ ์ƒํƒœ(New): ๋ฐฉ๊ธˆ ๋ฉ”๋ชจ๋ฆฌ์— ์ ์žฌ๋˜์–ด PCB๋ฅผ ํ• ๋‹น ๋ฐ›์€ ์ƒํƒœ
  • ์ค€๋น„ ์ƒํƒœ(Ready): ๋‹น์žฅ CPU๋ฅผ ํ• ๋‹น ๋ฐ›์•„์„œ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ์ƒํƒœ
  • ์‹คํ–‰ ์ƒํƒœ(Running): CPU๋ฅผ ํ• ๋‹น ๋ฐ›์•„์„œ ์‹คํ–‰์ค‘์ธ ์ƒํƒœ
  • ๋Œ€๊ธฐ ์ƒํƒœ(Blocked): I/O Device์— ์ž‘์—…์„ ์š”์ฒญํ•ด์„œ ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๊ธฐ๋‹ค๋ฆฌ๋Š” ์ƒํƒœ
  • ์ข…๋ฃŒ ์ƒํƒœ(Terminated): Process๊ฐ€ ์ข…๋ฃŒ๋œ ์ƒํƒœ๋กœ PCB์™€ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๋ชจ๋‘ ์ •๋ฆฌ

๐Ÿ’พ Process ๊ณ„์ธต ๊ตฌ์กฐ

๋งŽ์€ OS๋“ค์€ Process๋ฅผ ๊ณ„์ธต์ ์ธ ๊ตฌ์กฐ๋กœ ๊ด€๋ฆฌํ•˜๋ฉฐ Process๋Š” ์‹คํ–‰ ๋„์ค‘ System Call์„ ํ†ตํ•ด ๋‹ค๋ฅธ Process๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด๋•Œ ๊ธฐ์กด์˜ Process๋ฅผ Parent Process, ์ƒˆ๋กœ ์ƒ์„ฑํ•œ Process๋ฅผ Child Process๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

Parent Process์™€ Child Process๋Š” ๋‹ค๋ฅธ Process์ด๊ธฐ ๋•Œ๋ฌธ์— ์„œ๋กœ ๋‹ค๋ฅธ PID๋ฅผ ๊ฐ€์ง€์ง€๋งŒ ์ผ๋ถ€ OS์—์„œ๋Š” Child Process์˜ PCB์— Parent Prcoess์˜ PID๋ฅผ Context์— ํฌํ•จ์‹œํ‚ต๋‹ˆ๋‹ค.

Child Process์˜ ์ƒ์„ฑ๊ณผ์ •์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  1. Parent Process๋Š” fork()๋ฅผ ํ†ตํ•ด ์ž์‹ ์˜ ๋ณต์‚ฌ๋ณธ(Child Process)์„ ์ƒ์„ฑ
  2. Child Process๋Š” exec()๋ฅผ ํ†ตํ•ด Code Segment์™€ Data Segment์˜ ๋‚ด์šฉ์ด ์‹คํ–‰ํ•  ํ”„๋กœ๊ทธ๋žจ์˜ ๋‚ด์šฉ์œผ๋กœ ๋ฐ”๋€Œ๊ณ  Heap Segment์™€ Stack Segment๋Š” ์ดˆ๊ธฐํ™”

๐Ÿ’พ Thread

Thread๋Š” Process๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ์‹คํ–‰์˜ ํ๋ฆ„ ๋‹จ์œ„์ด๋ฉฐ ์—ฌ๋Ÿฌ Thread๋กœ ์—ฌ๋Ÿฌ ๋ช…๋ น์–ด๋ฅผ ๋™์‹œ์— ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ์„ Multi-Thread๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

Process๋“ค๋ผ๋ฆฌ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ์ž์›์„ ๊ณต์œ ํ•˜์ง€ ์•Š์ง€๋งŒ Thread๋ผ๋ฆฌ๋Š” ๊ฐ™์€ Process ๋‚ด์˜ ์ž์›์„ ๊ณต์œ ํ•ฉ๋‹ˆ๋‹ค.

Multi-Thread์˜ ์žฅ์ ๊ณผ ๋‹จ์ ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  • ์žฅ์ 
    • ์—ฌ๋Ÿฌ Process๋“ค์„ ํ•จ๊ป˜ ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๋” ํšจ์œจ์ ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ
    • Thread๋Š” Process ๋‚ด์˜ ์ž์›์„ ๊ณต์œ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์„œ๋กœ ํ˜‘๋ ฅ๊ณผ ํ†ต์‹ ์— ์œ ๋ฆฌ
  • ๋‹จ์ 
    • Multi-Thread๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ํ•˜๋‚˜์˜ Thread์— ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธฐ๋ฉด Process ์ „์ฒด์— ๋ฌธ์ œ๊ฐ€ ์ƒ๊น€
This post is licensed under CC BY 4.0 by the author.

๐Ÿ’พ [Operating System] About Operating System ๐Ÿ’พ

๐Ÿ’พ [Operating System] CPU Scheduling ๐Ÿ’พ