Home ๐Ÿ’พ [Operating System] CPU Scheduling ๐Ÿ’พ
Post
Cancel

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

๐Ÿ’พ CPU Scheduling

CPU Scheduling์ด๋ž€ OS(Operating System)๊ฐ€ Process๋“ค์—๊ฒŒ CPU ์ž์›์„ ๊ณต์ •ํ•˜๊ณ  ํ•ฉ๋ฆฌ์ ์œผ๋กœ ๋ฐฐ๋ถ„ํ•˜๋Š” ๊ฒƒ์„ ๋งํ•ฉ๋‹ˆ๋‹ค.

CPU๊ฐ€ ๋จผ์ € ์ฒ˜๋ฆฌํ•˜๋ฉด ์ข‹์„ ์ˆœ์„œ๋Œ€๋กœ ๋ฒˆํ˜ธ๋ฅผ ๋ถ€์—ฌํ•œ ๊ฒƒ์„ ์šฐ์„ ์ˆœ์œ„(Priority)๋ผ ํ•˜๋ฉฐ OS๋Š” PCB(Process Control Block)์— ์šฐ์„ ์ˆœ์œ„๋ฅผ ๋ช…์‹œํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด I/O Bound Process๋ฅผ ๋จผ์ € ์ฒ˜๋ฆฌํ•œ ํ›„ ๋Œ€๊ธฐ ์ƒํƒœ๋กœ ๋งŒ๋“ค๊ณ  CPU Bound Process์— ์ง‘์ค‘์ ์œผ๋กœ CPU๋ฅผ ํ• ๋‹นํ•˜๋Š” ๊ฒƒ์ด ํšจ์œจ์ ์ด๊ธฐ ๋•Œ๋ฌธ์— CPU Bound Process์˜ ์šฐ์„ ์ˆœ์œ„๋ณด๋‹ค I/O Bound Process์˜ ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋” ๋†’์Šต๋‹ˆ๋‹ค.

  • CPU Burst: CPU๋ฅผ ์ด์šฉํ•˜๋Š” ์ž‘์—…
  • I/O Burst: I/O Device๋ฅผ ๊ธฐ๋‹ค๋ฆฌ๋Š” ์ž‘์—…
  • CPU Bound Process(CPU ์ง‘์ค‘ ํ”„๋กœ์„ธ์Šค): CPU Burst๊ฐ€ ๋งŽ์€ Process
  • I/O Bound Process(I/O ์ง‘์ค‘ ํ”„๋กœ์„ธ์Šค): I/O Burst๊ฐ€ ๋งŽ์€ Process

๐Ÿ’พ Scheduling Queue

Process State Diagram With Scheduling Queue

๋งค๋ฒˆ ๋ชจ๋“  Process๋“ค์„ ์‚ดํŽด๋ณธ ํ›„ ์šฐ์„ ์ˆœ์œ„๋ฅผ ์ •ํ•˜๋Š” ๊ฒƒ์€ ๋ชจ๋“  Process๋“ค์„ ์‚ดํŽด๋ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋งค์šฐ ๋น„ํšจ์œจ์ ์ž…๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋น„ํšจ์œจ์„ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด Scheduling Queue๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

OS๊ฐ€ ๊ด€๋ฆฌํ•˜๋Š” Queue์—๋Š” ๋Œ€ํ‘œ์ ์œผ๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒƒ๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค.

  • Process State๊ฐ€ Ready(์ค€๋น„ ์ƒํƒœ)์— ์žˆ๋Š” Ready Queue
  • Process State๊ฐ€ Blocked(๋Œ€๊ธฐ ์ƒํƒœ)์— ์žˆ๋Š” Waiting Queue

๐Ÿ’พ CPU Scheduling Algorithm

OS๋Š” Ready Queue์— ์žˆ๋Š” Process๋“ค์˜ ์šฐ์„ ์ˆœ์œ„๋ฅผ CPU Scheduling Algorithm์„ ์‚ฌ์šฉํ•ด ์ •ํ•ฉ๋‹ˆ๋‹ค.

CPU Scheduling Algorithm์€ ํฌ๊ฒŒ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‚˜๋ˆ„์–ด์ง‘๋‹ˆ๋‹ค.

ย ์„ ์ ํ˜• ์Šค์ผ€์ค„๋ง(Preemptive Scheduling)๋น„์„ ์ ํ˜• ์Šค์ผ€์ค„๋ง(Non-Preemptive Scheduling)
์ •์˜OS๊ฐ€ CPU๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š” Process๋กœ๋ถ€ํ„ฐ CPU๋ฅผ ๊ฐ•์ œ๋กœ ๋นผ์•—์•„
๋‹ค๋ฅธ Process์—๊ฒŒ ํ• ๋‹นํ•˜๋Š” ์Šค์ผ€์ค„๋ง
CPU๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š” Process๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ ํ•ด๋‹น Process๊ฐ€ Terminated(์ข…๋ฃŒ ์ƒํƒœ)๊ฐ€ ๋˜๊ฑฐ๋‚˜
Blocked(๋Œ€๊ธฐ ์ƒํƒœ)๊ฐ€ ๋˜๊ธฐ ์ „๊นŒ์ง€ ๋‹ค๋ฅธ Process๊ฐ€ CPU๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ๋ชปํ•˜๋Š” ์Šค์ผ€์ค„๋ง
์žฅ์ ์–ด๋Š ํ•œ Process์˜ ์ž์› ๋…์ ์„ ๋ง‰๊ณ 
Process๋“ค์—๊ฒŒ ๊ณจ๊ณ ๋ฃจ ์‹œ์Šคํ…œ ์ž์›์„ ๋ฐฐ๋ถ„ํ•  ์ˆ˜ ์žˆ์Œ
Context Switching ํšŸ์ˆ˜๊ฐ€ ์„ ์ ํ˜• ์Šค์ผ€์ค„๋ง๋ณด๋‹ค ์ ๊ธฐ ๋•Œ๋ฌธ์— Overhead๊ฐ€ ๋œ ๋ฐœ์ƒ
๋‹จ์ Context Switching์—์„œ Overhead๊ฐ€ ๋ฐœ์ƒ๋ชจ๋“  Process๊ฐ€ ๊ณจ๊ณ ๋ฃจ ์‹œ์Šคํ…œ ์ž์›์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Œ
์ถ”๊ฐ€ํ˜„์žฌ ๋Œ€๋ถ€๋ถ„์˜ OS์—์„œ ์‚ฌ์šฉํ•˜๋Š” CPU Scheduling Algorithmย 

CPU Scheduling Algorithm์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒƒ๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค.

CPU Scheduling AlgorithmDefinition
FCFS(First Come First Served Scheduling)Ready Queue์— ์‚ฝ์ž…๋œ ์ˆœ์„œ๋Œ€๋กœ Process๋“ค์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋น„์„ ์ ํ˜• ์Šค์ผ€์ค„๋ง
Rounding Robin Scheduling์ •ํ•ด์ง„ Time Slice๋งŒํผ์˜ ์‹œ๊ฐ„ ๋™์•ˆ ๋Œ์•„๊ฐ€๋ฉฐ CPU๋ฅผ ์ด์šฉํ•˜๋Š” ์„ ์ ํ˜• ์Šค์ผ€์ค„๋ง
Priority SchedulingProcess๋“ค์— ์šฐ์„ ์ˆœ์œ„๋ฅผ ๋ถ€์—ฌํ•˜๊ณ  ๊ฐ€์žฅ ๋†’์€ ์šฐ์„ ์ˆœ์œ„๋ฅผ ๊ฐ€์ง„ Process๋ถ€ํ„ฐ ์‹คํ–‰ํ•˜๋Š” ์Šค์ผ€์ค„๋ง
SJF(Shortest Job First) SchedulingReady Queue์— ์‚ฝ์ž…๋œ Process๋“ค ์ค‘ CPU ์ด์šฉ ์‹œ๊ฐ„์˜ ๊ธธ์ด๊ฐ€ ๊ฐ€์žฅ ์งง์€ Process๋ถ€ํ„ฐ ์‹คํ–‰ํ•˜๋Š” ๋น„์„ ์ ํ˜• ์Šค์ผ€์ค„๋ง
SRT(Shortest Remaining Time) SchedulingReady Queue์— ์‚ฝ์ž…๋œ Process๋“ค ์ค‘ CPU ์ด์šฉ ์‹œ๊ฐ„์˜ ๊ธธ์ด๊ฐ€ ๊ฐ€์žฅ ์งง์€ Process๋“ค์„ ๋จผ์ € ์‹คํ–‰ํ•˜๋˜
Time Slice๋งŒํผ ์‹คํ–‰ํ•˜๋Š” ์„ ์ ํ˜• ์Šค์ผ€์ค„๋ง
Multilevel Queue Scheduling์šฐ์„ ์ˆœ์œ„๊ฐ€ ๊ฐ€์žฅ ๋†’์€ Queue์— ์žˆ๋Š” Process๋“ค์„ ๋จผ์ € ์ฒ˜๋ฆฌํ•˜๊ณ 
์šฐ์„ ์ˆœ์œ„๊ฐ€ ๊ฐ€์žฅ ๋†’์€ Queue๊ฐ€ ๋น„์–ด ์žˆ์œผ๋ฉด ๊ทธ๋‹ค์Œ ์šฐ์„ ์ˆœ์œ„ Queue์— ์žˆ๋Š” Process๋“ค์„ ์ฒ˜๋ฆฌํ•˜๋Š” ์Šค์ผ€์ค„๋ง
Multilevel Feedback Queue Scheduling์–ด๋–ค Process์˜ CPU ์ด์šฉ ์‹œ๊ฐ„์ด ๊ธธ๋ฉด ๋‚ฎ์€ ์šฐ์„ ์ˆœ์œ„ Queue๋กœ ์ด๋™์‹œํ‚ค๊ณ 
์–ด๋–ค Process๊ฐ€ ๋‚ฎ์€ ์šฐ์„ ์ˆœ์œ„ Queue์—์„œ ๋„ˆ๋ฌด ์˜ค๋ž˜ ๊ธฐ๋‹ค๋ฆฐ๋‹ค๋ฉด ๋†’์€ ์šฐ์„ ์ˆœ์œ„ Queue๋กœ ์ด๋™์‹œํ‚ฌ ์ˆ˜ ์žˆ๋Š” ์Šค์ผ€์ค„๋ง

๐Ÿ’พ FCFS

FCFS๋Š” ๋‹จ์ˆœํžˆ ์‚ฝ์ž…๋œ ์ˆœ์„œ๋Œ€๋กœ ์ฒ˜๋ฆฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— Convoy Effect(ํ˜ธ์œ„ ํšจ๊ณผ)๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Convoy Effect: CPU๋ฅผ ์ ๊ฒŒ ์‚ฌ์šฉํ•˜๋Š” Process๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋จผ์ € ์‹คํ–‰๋˜์–ด์•ผ ํ•˜๋Š” Process๋“ค์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ธด ์‹œ๊ฐ„์„ ๊ธฐ๋‹ค๋ ค์•ผ ํ•˜๋Š” ํ˜„์ƒ

๐Ÿ’พ Round Robin Scheduling

Round Robin Scheduling์€ FCFS์— Time Slice๋ผ๋Š” ๊ฐœ๋…์ด ๋”ํ•ด์ง„ ์Šค์ผ€์ค„๋ง ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด Ready Queue์— ์‚ฝ์ž…๋œ Process๋“ค์€ ์‚ฝ์ž…๋œ ์ˆœ์„œ๋Œ€๋กœ CPU๋ฅผ ์ •ํ•ด์ง„ ์‹œ๊ฐ„๋งŒํผ ์ด์šฉํ•˜๋˜ ์•„์ง ์™„๋ฃŒ๋˜์ง€ ์•Š์•˜๋‹ค๋ฉด ๋‹ค์‹œ Ready Queue ๋งจ ๋’ค์— ๋‹ค์‹œ ์‚ฝ์ž…๋ฉ๋‹ˆ๋‹ค.

Round Robin Scheduling์—์„œ๋Š” Time Slice์˜ ํฌ๊ธฐ๊ฐ€ ๋งค์šฐ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค.

  • Time Slice์˜ ํฌ๊ธฐ๊ฐ€ ํฐ ๊ฒฝ์šฐ: Convoy Effect๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Œ
  • Time Slice์˜ ํฌ๊ธฐ๊ฐ€ ์ž‘์€ ๊ฒฝ์šฐ: Context Switching์— ๋ฐœ์ƒํ•˜๋Š” ๋น„์šฉ์ด ์ปค์ง€๋Š” ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Œ

Time Slice: ๊ฐ Process๊ฐ€ ์–ผ๋งˆ๋‚˜ CPU๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ์ •ํ•ด์ง„ ์‹œ๊ฐ„

๐Ÿ’พ Priority Scheduling

Priority Scheduling์—์„œ ๋งŒ์•ฝ ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๊ฐ™๋‹ค๋ฉด FCFS๋กœ ์Šค์ผ€์ค„๋ง๋ฉ๋‹ˆ๋‹ค.

๊ทธ๋Ÿฌ๋‚˜ Priority Scheduling์€ Starvation ํ˜„์ƒ์ด ์ผ์–ด๋‚  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด Aging ๊ธฐ๋ฒ•์„ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

  • Starvation(๊ธฐ์•„) ํ˜„์ƒ: ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋‚ฎ์€ Process๋“ค์ด ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋†’์€ Process๋“ค ๋•Œ๋ฌธ์— ์‹คํ–‰์ด ๊ณ„์† ์—ฐ๊ธฐ๋˜๋Š” ํ˜„์ƒ
  • Aging: ์˜ค๋žซ๋™์•ˆ ๋Œ€๊ธฐํ•œ Process์˜ ์šฐ์„ ์ˆœ์œ„๋ฅผ ์ ์ฐจ ๋†’์ด๋Š” ๋ฐฉ์‹

๐Ÿ’พ SJF(Shortest Job First) Scheduling

SJF Scheduling์€ Priority Scheduling์˜ ํ•œ ์ข…๋ฅ˜๋กœ FCFS์˜ ๋‹จ์ ์ด์—ˆ๋˜ Convoy Effect๋ฅผ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๐Ÿ’พ SRT(Shortest Remaining Time) Scheduling

SRT Scheduling์€ Priority Scheduling์˜ ํ•œ ์ข…๋ฅ˜๋กœ SJF Scheduling๊ณผ Round Robin Scheduling์„ ํ•ฉ์นœ ์Šค์ผ€์ค„๋ง ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค.

์ฆ‰ Ready Queue์— ์‚ฝ์ž…๋œ Process๋“ค ์ค‘ CPU ์ด์šฉ ์‹œ๊ฐ„์˜ ๊ธธ์ด๊ฐ€ ๊ฐ€์žฅ ์งง์€ Process๋“ค์„ ๋จผ์ € ์‹คํ–‰ํ•˜๋˜ Time Slice๋งŒํผ ์‹คํ–‰ํ•˜๋Š” ์Šค์ผ€์ค„๋ง ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค.

๐Ÿ’พ Mulilevel Queue Scheduling

Multilevel Queue Scheduling์€ Priority Scheduling์˜ ๋ฐœ์ „๋œ ํ˜•ํƒœ๋กœ์„œ ์šฐ์„ ์ˆœ์œ„์— ๋”ฐ๋ฅธ Ready Queue๋ฅผ ์—ฌ๋Ÿฌ ๊ฐœ ์‚ฌ์šฉํ•˜๋Š” ์Šค์ผ€์ค„๋ง์ž…๋‹ˆ๋‹ค.

์žฅ์ ๋‹จ์ 
Queue๋ณ„๋กœ Time Slice๋ฅผ ์—ฌ๋Ÿฌ ๊ฐœ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์ŒStarvation ํ˜„์ƒ์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Œ
Queue๋งˆ๋‹ค ๋‹ค๋ฅธ Scheduling Algorithm์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œย 

๐Ÿ’พ Mulilevel Feedback Queue Scheduling

Multilevel Feedback Queue Scheduling์€ Multilevel Queue Scheduling์ด ๋ฐœ์ „๋œ ํ˜•ํƒœ๋กœ์„œ Process๋“ค์ด Ready Queue ์‚ฌ์ด๋ฅผ ์ด๋™ํ•  ์ˆ˜ ์žˆ๋Š” ์Šค์ผ€์ค„๋ง์ž…๋‹ˆ๋‹ค.

Multilevel Feedback Queue Scheduling์˜ ์ˆœ์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  1. ์ƒˆ๋กญ๊ฒŒ ๋“ค์–ด์˜ค๋Š” Process๋Š” ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๊ฐ€์žฅ ๋†’์€ Ready Queue์— ์‚ฝ์ž…
  2. Time Slice๋™์•ˆ CPU๋ฅผ ์‚ฌ์šฉํ–ˆ๋Š”๋ฐ ์‹คํ–‰์ด ๋๋‚˜์ง€ ์•Š์•˜๋‹ค๋ฉด ๋‹ค์Œ ์šฐ์„ ์ˆœ์œ„์ธ Ready Queue์— ์‚ฝ์ž…๋˜์–ด ์‹คํ–‰(์šฐ์„ ์ˆœ์œ„๊ฐ€ ํ•œ ๋‹จ๊ณ„ ๋ฐ€๋ ค๋‚จ)
  3. $2$๋ฒˆ ๋ฐ˜๋ณต

Multilevel Feedback Queue Scheduling์€ ๊ตฌํ˜„์ด ๋ณต์žกํ•˜์ง€๋งŒ ๊ฐ€์žฅ ์ผ๋ฐ˜์ ์ธ CPU Scheduling Algorithm์ž…๋‹ˆ๋‹ค.

This post is licensed under CC BY 4.0 by the author.

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

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