Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Q3) What is Context Switching? (Interview Question in OS) #11

Open
hidaehyunlee opened this issue Dec 7, 2020 · 7 comments
Open

Q3) What is Context Switching? (Interview Question in OS) #11

hidaehyunlee opened this issue Dec 7, 2020 · 7 comments
Labels

Comments

@hidaehyunlee
Copy link
Member

๐Ÿ˜Š Context Switching(๋ฌธ๋งฅ๊ตํ™˜)์— ๋Œ€ํ•ด ์•„๋Š”๋งŒํผ ๋ชจ๋ฅด๋Š”๋งŒํผ ์•Œ๊ณ ์‹ถ์€๋งŒํผ ์•Œ๋ ค์ฃผ๊ณ ์‹ถ์€๋งŒํผ ์„ค๋ช…ํ•ด์ฃผ์„ธ์š”. ๐Ÿ˜Š

@hidaehyunlee hidaehyunlee added the OS label Dec 7, 2020
@hidaehyunlee
Copy link
Member Author

๋‘๋‘” ... ๋‚ผ ์ฆ‰์„ ๊ตฌ์ˆ ๋ฉด์ ‘์œผ๋กœ ์ „ํ™˜ํ•ฉ๋‹ˆ๋‹ค.. ๐Ÿ˜ญ

@365kim
Copy link
Member

365kim commented Dec 13, 2020

์•Œ๋ ค์ฃผ๊ณ ์‹ถ์€๋งŒํผใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹

@365kim
Copy link
Member

365kim commented Dec 13, 2020

์ „๋ถ€ ์•Œ๋ ค์ฃผ๊ณ ์‹ถ์ง€๋งŒ... ์ค„ ์ˆ˜ ์žˆ๋Š”๊ฒŒ~

@365kim
Copy link
Member

365kim commented Dec 13, 2020

์ฆ‰์„
์ปจํ…์ŠคํŠธ ์Šค์œ„์นญ์€ ํ˜„์žฌ CPU๋ฅผ ํ• ๋‹น๋ฐ›์€ ํ”„๋กœ์„ธ์Šค๋กœ๋ถ€ํ„ฐ CPU๋ฅผ ๋นผ์•—์•„ ๋‹ค๋ฅธ ํ”„๋กœ์„ธ์Šค์—๊ฒŒ CPU๋ฅผ ํ• ๋‹นํ•˜๋ ค๊ณ ํ•  ๋•Œ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ํ˜„๋Œ€ ์ปดํ“จํ„ฐ ํ™˜๊ฒฝ์—์„œ๋Š” CPU๋ฅผ ์•„์ฃผ ์งง์€ ์‹œ๊ฐ„๋‹จ์œ„๋กœ ๊ฐ ํ”„๋กœ์„ธ์Šค์—๊ฒŒ ๋Œ์•„๊ฐ€๋ฉด์„œ ๋ฐฐ์ •ํ•˜๋Š” ์‹œ๋ถ„ํ•  ํ™˜๊ฒฝ์ด๊ธฐ ๋•Œ๋ฌธ์— ์ปจํ…์ŠคํŠธ ์Šค์œ„์นญ์€ ์ž์ฃผ ๋ฐœ์ƒํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์›๋ž˜ CPU๋ฅผ ํ• ๋‹น๋ฐ›์•„ ์‹คํ–‰๋˜๊ณ  ์žˆ๋˜ ํ”„๋กœ์„ธ์Šค๋Š” ์–ด๋””๊นŒ์ง€ ์‹คํ–‰๋˜์—ˆ๋Š”์ง€๋ฅผ ๊ทธ ํ”„๋กœ์„ธ์Šค์˜ PCB์— ์ €์žฅํ•˜๊ณ , ์ƒˆ๋กœ CPU๋ฅผ ํ• ๋‹น๋ฐ›์•„ ์‹คํ–‰๋  ํ”„๋กœ์„ธ์Šค๋Š” ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰์— ์–ด๋””๊นŒ์ง€ ์‹คํ–‰๋˜์—ˆ๋Š”์ง€๋ฅผ ๊ทธ ํ”„๋กœ์„ธ์Šค์˜ PCB๋กœ๋ถ€ํ„ฐ ๋ถˆ๋Ÿฌ์™€ ์‹คํ–‰์„ ์ด์–ด๊ฐ€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.


๋ณด์ถฉ
์˜ค์šฐ ์Šคํ„ฐ๋”” ๋•Œ ์ œ๊ฐ€ ๋‹ด๋‹นํ–ˆ๋˜ ํŒŒํŠธ๋„ค์š”...! ํ•˜์ง€๋งŒ ์•„์ฃผ ์ƒˆ๋กญ๋„ค์š”...

1. Context Switching ๋ชฉ์ 

  • ํ•˜๋‚˜์˜ ํ”„๋กœ์„ธ์Šค๊ฐ€ CPU๋ฅผ ์‚ฌ์šฉ์ค‘์ธ ์ƒํƒœ์—์„œ ๋‹ค๋ฅธ ํ”„๋กœ์„ธ์Šค์—๊ฒŒ CPU ์ œ์–ด๊ถŒ์„ ์ด์–‘ํ•˜๊ธฐ ์œ„ํ•œ ๊ณผ์ •
    • โ€ป ๋”ฐ๋ผ์„œ CPU๋ฅผ ์ ์œ ํ•˜๋Š” ํ”„๋กœ์„ธ์Šค๊ฐ€ ๋‹ค๋ฅธ ์‚ฌ์šฉ์ž ํ”„๋กœ์„ธ์Šค๋กœ ๋ณ€๊ฒฝ๋˜๋Š” ๊ณผ์ •์ด ์•„๋‹ˆ๋ผ, ํ•˜๋‚˜์˜ ํ”„๋กœ์„ธ์Šค์˜ ์‹คํ–‰๋ชจ๋“œ๋งŒ์ด ์ž ์‹œ ์‚ฌ์šฉ์ž๋ชจ๋“œ์—์„œ ์ปค๋„๋ชจ๋“œ๋กœ ๋ฐ”๋€Œ๋Š” ๊ฒƒ์ผ ๋ฟ์ธ ๋ชจ๋“œ๋ณ€๊ฒฝ์€ ๋ฌธ๋งฅ๊ตํ™˜์ด๋ผ๊ณ  ํ•˜์ง€ ์•Š์Œ

2. Context Switching ๊ณผ์ •

  • ์ด์ „์˜ ํ”„๋กœ์„ธ์Šค์˜ Context๋ฅผ ์ €์žฅ(๋ณด๊ด€)ํ•˜๊ณ , ์ƒˆ๋กœ์šด ํ”„๋กœ์„ธ์Šค์˜ Context๋ฅผ ๋ณต์›(์ ์žฌ)ํ•˜๋Š” ์ž‘์—…

3. Context Switching ์ด ํ•„์š”ํ•œ ์ด์œ 

  • ํ•˜๋‚˜์˜ ํ”„๋กœ์„ธ์Šค๋ฅผ ์ญ‰ ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ๊ณผ ๋น„๊ตํ–ˆ์„ ๋•Œ Context Switching์„ ํ•˜๋ฉด ๋งŽ์€ ์˜ค๋ฒ„ํ—ค๋“œ๊ฐ€ ๋’ค๋”ฐ๋ฆ„ (Context Switching ๋™์•ˆ ์—๋Š” ํ•ด๋‹น CPU๋Š” ์•„๋ฌด๊ฒƒ๋„ ๋ชปํ•จ)
    • โ€ป ์˜ค๋ฒ„ํ—ค๋“œ: ์–ด๋–ค ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๊ธฐ ์œ„ํ•ด ๋“ค์–ด๊ฐ€๋Š” ๊ฐ„์ ‘์ ์ธ ์ฒ˜๋ฆฌ ์‹œ๊ฐ„ ยท ๋ฉ”๋ชจ๋ฆฌ ๋“ฑ
  • ํ•˜์ง€๋งŒ ๋น ๋ฅธ ์†๋„๋กœ Task๋ฅผ ๋ฐ”๊ฟ”๊ฐ€๋ฉฐ ์‹คํ–‰ํ•˜๋ฉด ์‚ฌ์šฉ์ž ์ž…์žฅ์—์„œ๋Š” ์—ฌ๋Ÿฌ ํ”„๋กœ์„ธ์Šค๊ฐ€ ๋™์‹œ์— ์ฒ˜๋ฆฌ๋˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋Š๋ผ๊ฒŒ ๋˜๋Š” ์žฅ์ ์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ํ•„์š”ํ•จ.
  • ์ด์ฒ˜๋Ÿผ ์—ฌ๋Ÿฌ ํ”„๋กœ์„ธ์Šค๊ฐ€ ํ•จ๊ป˜ ์ˆ˜ํ–‰๋˜๋Š” ์‹œ๋ถ„ํ•  ์‹œ์Šคํ…œ ํ™˜๊ฒฝ์—์„œ๋Š” ํƒ€์ด๋จธ ์ธํ„ฐ๋ŸฝํŠธ์— ์˜ํ•ด ์งง์€ ์‹œ๊ฐ„๋™์•ˆ CPU๋ฅผ ์‚ฌ์šฉํ•œ ํ›„ ๋นผ์•—๊ฒผ๋‹ค๊ฐ€ ์ถ”ํ›„์— ๋‹ค์‹œ CPU๋ฅผ ํš๋“ํ•˜๋Š” ์‹์œผ๋กœ CPU ๊ด€๋ฆฌ๊ฐ€ ์ด๋ฃจ์–ด์ง€๋ฏ€๋กœ, CPU๋ฅผ ๋บ๊ธฐ๊ธฐ ์ง์ „ ์–ด๋Š ๋ถ€๋ถ„๊นŒ์ง€ ๋ช…๋ น์„ ์ˆ˜ํ–‰ํ–ˆ๋Š”์ง€ ์ •ํ™•ํ•œ ์ƒํƒœ๋ฅผ ์žฌํ˜„ํ•  ํ•„์š”๊ฐ€ ์žˆ์Œ

4. Context Switching ์ด ๋ฐœ์ƒํ•˜๋Š” ๊ฒฝ์šฐ

  • ์˜ˆ์‹œ1) timer interrupt (CPU ์‚ฌ์šฉ์‹œ๊ฐ„์ด ๋งŒ๋ฃŒ ๋˜์—ˆ์„ ๋•Œ)
  • ์˜ˆ์‹œ2) I/O request (์ž…์ถœ๋ ฅ ์š”์ฒญํ•  ๋•Œ)
  • ์˜ˆ์‹œ3) fork a child (์ž์‹ ํ”„๋กœ์„ธ์Šค๋ฅผ ๋งŒ๋“ค ๋•Œ)
  • ์˜ˆ์‹œ4) wait for an interrupt (์ธํ„ฐ๋ŸฝํŠธ ์ฒ˜๋ฆฌ๋ฅผ ๊ธฐ๋‹ค๋ฆด ๋•Œ)

@hidaehyunlee
Copy link
Member Author

Context Switching(๋ฌธ๋งฅ๊ตํ™˜)์ด๋ž€...

ํ•˜๋‚˜์˜ ์‚ฌ์šฉ์ž ํ”„๋กœ์„ธ์Šค๋กœ๋ถ€ํ„ฐ ๋‹ค๋ฅธ ์‚ฌ์šฉ์ž ํ”„๋กœ์„ธ์Šค๋กœ CPU์˜ ์ œ์–ด๊ถŒ์ด ์ด์–‘๋˜๋Š” ๊ณผ์ •์„ ๋งํ•ฉ๋‹ˆ๋‹ค.

๊ทธ ๊ณผ์ •์—์„œ ์‹คํ–‰ ์ค‘์ธ ํ”„๋กœ์„ธ์Šค ์ƒํƒœ ๋ฐ ๋ ˆ์ง€์Šคํ„ฐ ๊ฐ’(Context)์„ ํ•ด๋‹น ํ”„๋กœ์„ธ์Šค์˜ PCB์— ์ €์žฅ(๋ณด๊ด€)ํ•˜๊ณ , ๋‹ค์Œ์œผ๋กœ ์ˆ˜ํ–‰ํ•ด์•ผํ•  ํ”„๋กœ์„ธ์Šค์˜ PCB Context๋ฅผ CPU ๋ ˆ์ง€์Šคํ„ฐ์— ๋ณต์›(์ ์žฌ)ํ•˜๋Š” ์ž‘์—…์ด ์ด๋ค„์ง‘๋‹ˆ๋‹ค.

๋‹จ๊ณ„ ์ ˆ์ฐจ ์„ค๋ช…
1 ์ธํ„ฐ๋ŸฝํŠธ/์‹œ์Šคํ…œ ํ˜ธ์ถœ โ€“ ์šด์˜์ฒด์ œ์—์„œ ํ”„๋กœ์„ธ์Šค ์Šค์ผ€์ฅด๋Ÿฌ์— ์˜ํ•ด ์ธํ„ฐ๋ŸฝํŠธ ๋ฐœ์ƒ
2 ์ปค๋„ ๋ชจ๋“œ ์ „ํ™˜ โ€“ ํ”„๋กœ์„ธ์Šค๊ฐ€ ์‹คํ–‰๋˜๋Š” ์‚ฌ์šฉ์ž๋ชจ๋“œ์—์„œ ์ปค๋„ ๋ชจ๋“œ๋กœ ์ „ํ™˜
3 ํ˜„์žฌ ํ”„๋กœ์„ธ์Šค ์ƒํƒœ PCB ์ €์žฅ โ€“ ๊ธฐ์กด ์‹คํ–‰๋˜๋Š” ํ”„๋กœ์„ธ์Šค ์ •๋ณด๋ฅผ PCB์— ์ €์žฅ
4 ๋‹ค์Œ ์‹คํ–‰ ํ”„๋กœ์„ธ์Šค ๋กœ๋“œ โ€“ PCB์— ์žˆ๋Š” ๋‹ค์Œ ์‹คํ–‰ ํ”„๋กœ์„ธ์Šค ์ƒํƒœ ์ •๋ณด ๋ณต๊ตฌ
5 ์‚ฌ์šฉ์ž ๋ชจ๋“œ ์ „ํ™˜ โ€“ ์ปค๋„ ๋ชจ๋“œ์—์„œ ์‚ฌ์šฉ์ž ๋ชจ๋“œ๋กœ ์ „ํ™˜ํ•˜์—ฌ ํ”„๋กœ์„ธ์Šค ์‹คํ–‰

์ด๋Œ€๋ฆฌ ๋ฉด์ ‘๊ด€์˜ ์ถ”๊ฐ€ ์งˆ๋ฌธ ๐Ÿง

์งˆ๋ฌธ 1. ์‹œ์Šคํ…œ์ฝœ์ด๋‚˜ ์ธํ„ฐ๋ŸฝํŠธ๋กœ ์ธํ•ด CPU์ œ์–ด๊ถŒ์ด ์šด์˜์ฒด์ œ๋กœ ๋„˜์–ด๊ฐ€๋Š” ๊ฒฝ์šฐ๋ฅผ Context Switching ์ด๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ์„๊นŒ์š”?
์งˆ๋ฌธ 2. Context Switching ์ค‘ CPU๋Š” ์–ด๋–ค ์ผ์„ ํ• ๊นŒ์š”?
์งˆ๋ฌธ 3. ๋ฌธ๋งฅ๊ตํ™˜์˜ ์˜ค๋ฒ„ํ—ค๋“œ ํ•ด๊ฒฐ ๋ฐฉ์•ˆ์„ ์„ค๋ช…ํ•ด์ฃผ์„ธ์š”.

@gaeun0204
Copy link
Contributor

์ •์˜: Context Switching์ด๋ž€ ํ•˜๋‚˜์˜ ์‚ฌ์šฉ์ž ํ”„๋กœ์„ธ์Šค๋กœ๋ถ€ํ„ฐ ๋‹ค๋ฅธ ์‚ฌ์šฉ์ž ํ”„๋กœ์„ธ์Šค๋กœ CPU์˜ ์ œ์–ด๊ถŒ์ด ์ด์–‘๋˜๋Š” ๊ณผ์ •์„ ๋งํ•œ๋‹ค.
๋ฐœ์ƒ ์กฐ๊ฑด: Context Switching๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ์กฐ๊ฑด์€ ํ•˜๋“œ์›จ์–ด๋ฅผ ํ†ตํ•œ I/O์š”์ฒญ์ด ๋“ค์–ด์™”์„ ๋•Œ, ํƒ€์ž„ํ€€ํ…€์ด ๊ฒฝ๊ณผ๋˜์—ˆ์„ ๋•Œ ๋“ฑ์ด๋‹ค.
๋ฐœ์ƒ ๊ณผ์ •: Context Switching์ด ๋ฐœ์ƒํ•˜๋ฉด ์›๋ž˜ CPU๋ฅผ ๋ณด์œ ํ•˜๊ณ  ์žˆ๋˜ ํ”„๋กœ์„ธ์Šค๋Š” ์ž์‹ ์˜ ๋ฌธ๋งฅ(context)์„ PCB์— ์ €์žฅํ•˜๊ณ , ์ƒˆ๋กœ CPU๋ฅผ ํ• ๋‹น๋ฐ›๋Š” ํ”„๋กœ์„ธ์Šค๋Š” ์˜ˆ์ „์— ์ €์žฅํ–ˆ๋˜ ์ž์‹ ์˜ ๋ฌธ๋งฅ(context)์„ PCB๋กœ๋ถ€ํ„ฐ ๊ฐ€์ ธ์™€์„œ ์‹ค์ œ ํ•˜๋“œ์›จ์–ด๋กœ ๋ณต์›์‹œํ‚ค๋Š” ๊ณผ์ •์„ ๊ฑฐ์นœ๋‹ค.

@SeongsangCHO
Copy link
Contributor

CPU๊ฐ€ ์–ด๋–ค ํ”„๋กœ์„ธ์Šค๋ฅผ ์‹คํ–‰ํ•˜๊ณ  ์žˆ๋Š” ๋„์ค‘์— ์ธํ„ฐ๋ŸฝํŠธ๋ฐœ์ƒ์‹œ ์‹คํ–‰์ค‘์ธ ํ”„๋กœ์„ธ์Šค๋ฅผ ์ค‘๋‹จ๋˜๊ณ , CPU ์ œ์–ด๊ถŒ์ด ๋‹ค๋ฅธ ํ”„๋กœ์„ธ์Šค๋กœ ์ด์–‘๋˜์–ด ๊ธฐ์กด ํ”„๋กœ์„ธ์Šค ์ •๋ณด๋“ค์„ PCB์— ์ €์žฅ, ์‹คํ–‰๋  ํ”„๋กœ์„ธ์Šค์˜ ์ •๋ณด๋ฅผ PCB๋กœ๋ถ€ํ„ฐ ๊ฐ€์ ธ์˜ค๋Š” ์ž‘์—…์„ ์ปจํ…์ŠคํŠธ ์Šค์œ„์นญ์ด๋ผํ•œ๋‹ค.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants