[OS] Operating System(3-1): Process Scheduling/Creation/Termination, IPC
๐ ์ด์์ฒด์ ์ ๊ณต ์์ ์ ๋ฆฌ
Process
๐ง Process = A program in execution, must progress in sequential fashion
- ๋จ์ํ ์ฝ๋๋ง ์๋ ์ ์ ์ธ ํ๋ก๊ทธ๋จ์ด ์๋๋ผ, CPU์์ ์ค์ ์คํ๋๊ณ ์๋ ๋์ ์ธ ์กด์ฌ
- ์ฆ, ํ๋ก์ธ์ค๋ ์๊ฐ์ ๋ฐ๋ผ ์ํ๊ฐ ๋ณํ๋ฉฐ, ์คํ ์์๋ฅผ ๋ฐ๋ผ ์์ฐจ์ ์ผ๋ก ์งํ
โ ํ๋ก์ธ์ค์ ๊ตฌ์ฑ ์์
- Text section
- ํ๋ก๊ทธ๋จ์ ์คํ ์ฝ๋๊ฐ ์ ์ฅ๋ ์์ญ
- Program Counter and Register
Program Counter
: ๋ค์์ ์คํํ ๋ช ๋ น์ด์ ๋ฉ๋ชจ๋ฆฌ ์ฃผ์๋ฅผ ์ ์ฅRegister
: ํ์ฌ ์ํ ์ค์ธ ์์ ์ ์ํ ์ ๋ณด ์ ์ฅ
- Stack
- ํจ์ ํธ์ถ ์ ์์ฑ๋๋ ์์ ๋ฐ์ดํฐ ์ ์ฅ์
- ์ ์ฅ ๋ด์ฉ:
- Funtion parameters: ํจ์๋ฅผ ํธ์ถํ ๋ ์ผ๋ฐ์ ์ธ user program๋ค์ ํธ์์ฑ ๋๋ฌธ์ stack์ ํตํด์ parameterํ์์ผ๋ก ๋๊น
- return addresses: ํจ์์์ returnํ ๊ฐ์ stack์ผ๋ก ์ ์ฅ
- local variables: ํจ์๊ฐ ๊ตฌ๋๋๋ ๋์๋ง ์ฌ์ฉํ ์ ์๋ ๋ณ์,์ฌ์ฉ์ด ๋๋๋ฉด ํ๊ดด๋จ(โ ์ ์ญ๋ณ์)
- Data section: ํ๋ก๊ทธ๋จ ๋ด ์ ์ญ ๋ณ์(Global variables) ์ ์ฅ ๊ณต๊ฐ โ ํ๋ก๊ทธ๋จ ์ ์ฒด์์ ์ ๊ทผ ๊ฐ๋ฅ
- Heap: ๋์ ๋ฉ๋ชจ๋ฆฌ ํ ๋น ๊ณต๊ฐ, ์คํ ์ค
malloc
,new
๊ฐ์ ํจ์๋ฅผ ํตํด ์์ฒญํ ๋ฉ๋ชจ๋ฆฌ๋ ์ด๊ณณ์ ์ ์ฅ๋จ - ํ๋ก์ธ์ค๋
active
์ด์ง๋ง ํ๋ก๊ทธ๋จ์passive
entity์ด๋ค. - ํ๋์ ํ๋ก๊ทธ๋จ์ ์ฌ๋ฌ ๊ฐ์ process๋ฅผ ์์ฑ ๊ฐ๋ฅํจ(์์: ์น๋ธ๋ผ์ฐ์ ์ฌ๋ฌ๊ฐ ๋์ฐ๊ธฐ ๊ฐ๋ฅ)
text๋ ๊ธฐ๊ณ์ด๊ฐ ๋ค์ด๊ฐ ์์
stack
,heap
,data(์ ์ญ ๋ณ์ ๋ฑ)
๋ค์ด ์๋ค
stack
,heap
์ ๋์ ์ผ๋ก ๊ณต๊ฐ์ ํ ๋น,free()
ํ๊ธฐ ๋๋ฌธ์ ์์ชฝ์ผ๋ก ๋์ด๋ฌ๋ค ์ค์๋ค ํจ โ ๊ทธ๋์ ๋ง์ฃผ๋ณด๊ฒ ๋์์ธ
์ฝ๋๋ฅผ ์ปดํ์ผํ๊ฒ ๋๋ฉด binary code๊ฐ ๋๊ณ text์ ์ ์ฅ๋จ
x, y๋ data์ ์์ญ์ ์ ์ฅ
malloc
์ heap,main
ํจ์ ์์*value
๋ฑ์ local variable๋ก stack์ ์ ์ฅ
Process state
process์ ์คํ์ ๋ฐ๋ผ ์ํ๊ฐ ๋ฐ๋๋ค
New
: process๊ฐ ๋ฐฉ๊ธ ์์ฑ๋ ์ํRunning
: process๊ฐ ์คํ ์ค์ธ ์ํ(CPU๋ฅผ ์ก์)Waiting
: ์ด๋ค event๋ฐ์์ ๊ธฐ๋ค๋ฆฌ๋ ์ํ(CPU๋ฅผ ์ก๊ณ ์์ง ์์)Ready
: ๋ฉ์ถฐ์๋ค๊ฐ CPU๋ง ํ ๋นํ๋ฉด ๋ฐ๋ก ์คํํ ์ ์๊ฒ ์ค๋น๋ ์ํTerminated
: process๊ฐ ์ข ๋ฃ๋ ์ํ
- running ์ํ์ ๊ฐ๊ธฐ ์ง์ ์ ๋ฐ๋์ ready์ํ์ ๋์ฌ์์ด์ผํจ
- ๊ทธ ์ด์ ๋ ๋ฌด์ํ ๋ง์ process๊ฐ ready์ํ์ ๋์ฌ์๊ธฐ ๋๋ฌธ์
- ๋ง์ process์ค์ ์คํํ process๋ฅผ ๊ณ ๋ฅด๋ ์์ ์ด
process scheduling ์๊ณ ๋ฆฌ์ฆ
์ด๋ค.
Process Control Block (PCB)
Process Control Block (PCB): process๋ฅผ ๊ด๋ฆฌํ๋ ์ ๋ณด๋ฅผ ๋ด๊ณ ์๋ ์๋ฃ๊ตฌ์กฐ PCB๋ ํ๋ก์ธ์ค๊ฐ ์๊ธฐ๋ฉด ๊ฐ์ด ์์ฑ๋๊ณ , ํ๋ก์ธ์ค๊ฐ ์ญ์ ๋๋ฉด ์ญ์ ๋๋ค.
- Process state:
running
,waiting
๋ฑ์ ์ํ ์ ๋ณด - Program counter: ๋ค์์ ์คํํ CPU instruction์ ์ฃผ์ โ ๋์ค์ ํ๋ก์ธ์ค๊ฐ ์ฌ์์์ ํ๊ธฐ ์ํ ์ ๋ณด๊ฐ ๋ด๊น
- CPU registers: CPU๊ฐ ์ฌ์ฉํ๋ ๋ ์ง์คํฐ ๊ฐ๋ค
- CPU scheduling info: ์ฐ์ ์์, ํ ์์น ๋ฑ ์ค์ผ์ค๋ง ๊ด๋ จ ์ ๋ณด
- Memory management info: process์ ํ ๋น๋ ๋ฉ๋ชจ๋ฆฌ ์ ๋ณด
- Accounting info: ์ฌ์ฉ๋ CPU ์๊ฐ, ์คํ ์๊ฐ, ์ ํ์๊ฐ ๋ฑ
- I/O status info: ์ฐ๊ฒฐ๋ I/O ์ฅ์น๋ ์ด๋ฆฐ ํ์ผ ๋ฆฌ์คํธ
Threads
๐ ์ค๋ ๋๋ ํ๋ก์ธ์ค ๋ด์ ์๋ ์คํ์ ํ ํ๋ฆ์ ์๋ฏธํ๋ค
- ๊ฐ ํ๋ก์ธ์ค๋ ๊ตฌ๋ณ๋๊ณ ๋ฉ๋ชจ๋ฆฌ๋ ๊ตฌ๋ณ๋์ง๋ง, ํ๋์ ํ๋ก์ธ์ค์ ์ํด์๋ ์ค๋ ๋๋ค์ ๋ ๋ฆฝ์ ์ผ๋ก ์์ง์ด๊ณ ๋ฉ๋ชจ๋ฆฌ/์ฝ๋๋ฅผ ๊ณต์ ํ๋ค.
Linux์์๋ ๋๋ถ๋ถ process๋ณด๋ค thread ๋จ์๋ก ๋ํ๋ธ๋ค
Process Scheduling
Process scheduling์ CPU๊ฐ ๋์ง ์๊ฒ ํญ์ ์์ ์ ์ํค๊ณ , ์ฌ๋ฌ ํ๋ก์ธ์ค๋ฅผ ๋น ๋ฅด๊ฒ CPU์ ํ ๋นํด์ ์คํํ๊ธฐ ์ํด ํ์ํ๋ค Process scheduling: ์คํ ๊ฐ๋ฅํ ํ๋ก์ธ์ค๋ค ์ค์์ ๋ค์์ CPU์ ์ฌ๋ฆด ๊ฑธ ์ ํํ๋ ๋ด๋น์
โ Process Queue ์ข ๋ฅ
- Ready Queue: ๋ฉ์ธ ๋ฉ๋ชจ๋ฆฌ์ ์๊ณ ์ฆ์ CPU ํ ๋น๋ฐ์ ์ค๋น๊ฐ ๋ ํ๋ก์ธ์ค๋ค์ ์งํฉ
- Wait Queue: I/O ๋ฑ ์ด๋ค ์ด๋ฒคํธ๋ฅผ ๊ธฐ๋ค๋ฆฌ๋ ์ค์ธ ํ๋ก์ธ์ค๋ค์ ์ค(์: ํค๋ณด๋ ์ ๋ ฅ, ๋์คํฌ ์ฝ๊ธฐ ๋ฑ)
- ํ๋ก์ธ์ค๋ ์ด ํ๋ค ์ฌ์ด๋ฅผ ์์ ๋กญ๊ฒ ์ด๋ํจ
- ์: CPU ์ฐ๋ค๊ฐ โ ์
์ถ๋ ฅ ๊ธฐ๋ค๋ฆฌ๋ฉด
wait queue
๋ก โ ๋๋๋ฉด ๋ค์ready queue
๋ก
- ์: CPU ์ฐ๋ค๊ฐ โ ์
์ถ๋ ฅ ๊ธฐ๋ค๋ฆฌ๋ฉด
queue์ด๊ธฐ ๋๋ฌธ์
tail
ํฌ์ธํฐ๋ ํ์, ์๋ฐฉํฅ๋ ๊ฐ๋ฅ
time slice expired
๋ ๊ฐ ํ๋์ ์๊ฐ์ ํ ๋นํจ
Multicore system์ด๊ธฐ ๋๋ฌธ์ ์ฌ๋ฌ๊ฐ์Queue
๋ฅผ ๊ฐ์ง๊ณ ์คํํ ์ ์์
Context Switch
process P0 ์คํ ์ค โ
interrupt
orsystemcall
๋ฐ์ โ P0 ์ ์ง ํ ํ์ฌ ์ํ๋ฅผ PCB0์ ์ ์ฅ
๋ฉ์ถฐ์๋ process P1์ PCB1์ผ๋ก๋ถํฐ reload โinterrupt
orsystemcall
์ ์ฒ๋ฆฌํ๊ณ ๋ฉ์ถ๋ฉด ํ์ฌ ์ํ๋ฅผ PCB1์ ์ ์ฅ
๋ ๋ค์ P0๋ฅผ ๋ถ๋ฌ์ด (Process๊ฐ ๋๊ฐ๊ฐ ์๋๋ผ ์ฌ๋ฌ๊ฐ์ฌ๋ ๊ฐ์ ๋งค์ปค๋์ฆ)
๐ Context Switch: ์คํ ์ค์ธ old process
์ ์ํ๋ฅผ ์ ์ฅํ๊ณ , ์ ์ฅ๋ new process
๋ฅผ ๋ถ๋ฌ์จ๋ค
- Context Switch๋ ์๋นํ ๋ง์ด ์ผ์ด๋จ
Context๋ PCB์ ์ ์ฅํจ
Context Switch time
์ overhead๊ฐ ์์ โ overhead์ ์์คํ ์์ ์ด ๋ถ์์ ํจ- OS and PCB๊ฐ ๋ณต์กํ ์๋ก context switch time์ ๋์ด๋ ์ ๋ฐ์ ์์
- ๊ทธ ์๊ฐ์ ์ค์ด๊ธฐ ์ํด
hardware support
๊ฐ ํ์- ์ด๋ค CPU๋
single instruction
orstore all registers
๋ฅผ ํ๋ค. - ์ด๋ค CPU๋
multiple sets of register
๋ฅผ ๊ฐ์ง๊ณ ์์ด์ ๋ณต์ฌํ์ง ์๊ณ ํฌ์ธํฐ๋ง ์ฎ๊ฒจ์ ์ ์ฅํ๋ค.
- ์ด๋ค CPU๋
- P0์
timer interrupt
๊ฐ ๊ฑธ๋ฆฌ๋ฉด, ํ์ฌ register์ IP๊ฐ์ ์ ์ฅ(userโkernel mode์ด๊ธฐ ๋๋ฌธ์ IP๋ ์ ์ฅ)- ์ค์ผ์ฅด๋ง์ผ๋ก ์คํ์๊ฐ ๊ณ์ฐ(๋ง์ฝ ์๊ฐ์ด ๋จ์์ผ๋ฉด ๋ฐ๋ก returnํด์ P0์คํ) โ ๋ค ์ผ์ผ๋ฉด P0์ PCB๋ฅผ
Ready Queue
์ ๋ฃ์, ๋ค์์ ์คํํ P1์Ready Queue
์์ ์ ํ- switching์ ํด์ ํ์ฌ register, IP๊ฐ์ P0 PCB์ ์ ์ฅ, P1 PCB์์ ์ ์ฅ๋ register, IP๊ฐ์ ๋ณต์ํ์๋ง์ ์คํ(
Jump
: ์ง์ ๊ฐ์ง ๋ชปํ๊ธฐ๋๋ฌธ์ jump๋ผํจ)
Multitasking in Mobile Systems
์ด๊ธฐ์ iOS ๋ชจ๋ฐ์ผ system์ ํ ๋ฒ์ ํ๋์ ํ๋ก์ธ์ค๋ง ์คํ ๊ฐ๋ฅํ๊ณ ๋๋จธ์ง ํ๋ก์ธ์ค๋ ์ผ์์ ์ง ์ํ
- User Interface(UI) ๊ณต๊ฐ์ด ์ ํ๋๊ธฐ ๋๋ฌธ์ iOS๋ ๋ค์๊ณผ ๊ฐ์
Multitasking
๋ฐฉ์์ ์ ๊ณต- Foreground Process: ํ๋ฉด์ ํ์๋๋ฉฐ ์ฌ์ฉ์์ ์ง์ ์ํธ์์ฉํจ, ์ค์ง ํ๋๋ง ์กด์ฌ ๊ฐ๋ฅ
- Background Processes: ์คํ ์ค์ด์ง๋ง ํ๋ฉด์๋ ๋ณด์ด์ง ์์, ๋ฉ๋ชจ๋ฆฌ์ ์์ฃผํ์ง๋ง ๊ธฐ๋ฅ ์ ํ ์กด์ฌ
- Android ์์คํ
์ ๋ ์ ์ฐํจ
Foreground & Background ํ๋ก์ธ์ค
๋ฅผ ๋์์ ์คํ ๊ฐ๋ฅBackground Processes
๋Service
(๋ฐฑ๊ทธ๋ผ์ด๋์์ ๋์๊ฐ๋ ๋ณ๋์ ๊ตฌ์ฑ ์์)๋ฅผ ์ฌ์ฉ- Service๋ UI ์์, ์์ ๋ฉ๋ชจ๋ฆฌ๋ง ์ฌ์ฉ
Background Processes
๊ฐ ์ค๋จ๋ผ๋ ๊ณ์ ์คํ ๊ฐ๋ฅ
Process Creation
Parent process๋ ์์ ์ ๋ณต์ ํด์ children process๋ค์ ๋ง๋ค์ด ๋ธ๋ค
- ๊ทธ๋์ ๋ถ๋ชจ-์์ ๊ด๊ณ๋ก ์ฐ๊ฒฐ๋ process๋ค์ `tree`ํ์์ ๋ค๋ค.
๋ชจ๋ ํ๋ก์ธ์ค๋ ๊ณ ์ ํ ๋ฒํธ๋ก ์๋ณ๋๋ค โ ๊ทธ ๋ฒํธ๊ฐ PID(process identifier)
- Resource Sharing Options
โ Execution Options
- Concurrently: ๋ถ๋ชจ์ ์์์ด ๋์์ ์คํ(์: ์น๋ธ๋ผ์ฐ์ ์ ๋ค์ด๋ก๋ ์ฐฝ์ด ๋ฐ๋ก ์์ง์)
- parents wait: ์์ ํ๋ก์ธ์ค๊ฐ ์ข ๋ฃ๋ ๋๊น์ง ๋ถ๋ชจ๊ฐ ๋ฉ์ถค
โ Address space
- Address space์ ๊ฒฝ์ฐ ์์ํ๋ก์ธ์ค๋ ๋ถ๋ชจ ํ๋ก์ธ์ค๋ฅผ ์์ ํ ๋ณต์
- ๊ทธ๋ฆฌ๊ณ ์ด ๊ณต๊ฐ์์ ๋ค๋ฅธ ํ๋ก๊ทธ๋จ์ ๋ก๋ํจ
- UNIX ์์:
fork()
: ๋ถ๋ชจ๊ฐ ์์ ํ๋ก์ธ์ค๋ฅผ ์์ฑํจ, ์์์ ๋ถ๋ชจ์ ๋ณต์ฌ๋ณธ์ผ๋ก ์์exec()
: ์์์ด ์์ ๋ง์ ์๋ก์ด ํ๋ก๊ทธ๋จ์ผ๋ก ๋ฐ๊ฟ, ์๊ธฐ ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ์ ์์ ํ ๋ฎ์ด์(๋ถ๋ชจ ๋ด์ฉ์ ์ฌ๋ผ์ง)wait()
: ๋ถ๋ชจ๋wait()
์ ํธ์ถํ์ฌ ์์์ ์ข ๋ฃ๋ฅผ ๊ธฐ๋ค๋ฆผ, ์์์ด ์ข ๋ฃ(exit()
)ํ๋ฉด ๋ถ๋ชจ๋ ๋ค์ ์คํ๋จ
parent process์์
fork()
๋ฅผ ํ๋ฉดpid=0
์ผ ๊ฒฝ์ฐ child,pid>0
์ด๋ฉด parent๋ก ๊ตฌ๋ถํ๋ค.
fork()
ํ์๋ง์ parent์ child๊ฐ ์์ฑ๋จ
fork()
์ข ๋ฃ ํ parent์๋ child์ pid๊ฐ ๋ฆฌํด, child์๊ฒ๋ 0์ด ๋ฆฌํด
pid>0
์ด๋ฉด ๋ ธ๋์๋ถ๋ถ ์คํ,pid==0
์ด๋ฉด ์ด๋ก๋ถ๋ถ ์คํ
1
2
3
4
5
6
7
8
9
10
11
12
13
parent process
|
|-- fork() --> child process ์์ฑ
|
โโ> execl("/bin/ls", "ls") ์คํ โ ํ์ฌ child process๊ฐ 'ls' ๋ช
๋ น์ด๋ก ์์ ํ ๊ต์ฒด๋จ
|
โโ> 'ls' ๋ช
๋ น ์คํ ๊ฒฐ๊ณผ ์ถ๋ ฅ๋จ (์์)
๋ถ๋ชจ๋ wait() ํธ์ถ
|
|-- ์์ ์ข
๋ฃ๋ ๋๊น์ง ๋๊ธฐ
|
|-- "Child Complete" ์ถ๋ ฅ (๋ถ๋ชจ)
Process Termination
- ์ผ๋ฐ์ ์ธ ์์ ํ๋ก์ธ์ค ์ข
๋ฃ(
exit()
)- ํ๋ก์ธ์ค๋ ๋ง์ง๋ง ๋ช
๋ น๋ฌธ์ ์คํํ ๋ค, ์ด์์ฒด์ ์๊ฒ
exit()
์์คํ ํธ์ถ๋ก ์์ ์ ์ข ๋ฃํ๋๋ก ์์ฒญ - ์์ ํ๋ก์ธ์ค๋ status data๋ฅผ
wait()
์์คํ ํธ์ถ์ ํตํด ๋ถ๋ชจ์๊ฒ ์ ๋ฌ - ์์ ํ๋ก์ธ์ค๊ฐ ์ข ๋ฃ๋๋ฉด, ์ด์์ฒด์ ๊ฐ ์์์ด ์ฌ์ฉํ๋ ์์์ ํ์
- ํ๋ก์ธ์ค๋ ๋ง์ง๋ง ๋ช
๋ น๋ฌธ์ ์คํํ ๋ค, ์ด์์ฒด์ ์๊ฒ
- ๋ถ๋ชจ๊ฐ ์์์ ๊ฐ์ ์ข
๋ฃํ๋ ๊ฒฝ์ฐ(
abort()
)- ๋ถ๋ชจ ํ๋ก์ธ์ค๊ฐ
abort()
์์คํ ํธ์ถ์ ์ด์ฉํด ์์ ํ๋ก์ธ์ค๋ฅผ ๊ฐ์ ๋ก ์ข ๋ฃํ ์๋ ์๋ค - ์ด์ 3๊ฐ์ง:
- ์์์ด ํ ๋น๋ ์์์ ์ด๊ณผํ์ ๋ โ Ex: ๋ฉ๋ชจ๋ฆฌ, CPU ์๊ฐ ๋ฑ ์ ํ๋ ์์์ ๋๊ธด ๊ฒฝ์ฐ
- ์์์๊ฒ ๋งก๊ธด ์์ ์ด ๋ ์ด์ ํ์ ์์ ๋ โ Ex: ์ ์ ๊ฐ ์์ ์ ์ทจ์ํ๊ฑฐ๋, ์กฐ๊ฑด์ด ๋ฐ๋ ๊ฒฝ์ฐ
- ๋ถ๋ชจ๊ฐ ๋จผ์ ์ข ๋ฃ๋ ๋ โ OS๋ ๋ถ๋ชจ๊ฐ ์ข ๋ฃ๋๋ฉด ์์๋ ์คํ์ ํ์ฉํ์ง ์๋ ๊ฒฝ์ฐ๊ฐ ์์
- ๋ถ๋ชจ ํ๋ก์ธ์ค๊ฐ
์ผ๋ถ ์ด์์ฒด์ ๋ ๋ถ๋ชจ ํ๋ก์ธ์ค๊ฐ ์ข ๋ฃ๋๋ฉด ์์ ํ๋ก์ธ์ค๋ ์๋์ผ๋ก ์ข ๋ฃ์ํจ๋ค
- ์ด๋ ์ผ์ด๋๋ ํ์: Cascading Termination (๊ณ๋จ์ ์ข
๋ฃ)
- ๋ถ๋ชจ๊ฐ ์ข ๋ฃ๋๋ฉด โ ์์, ์์, ์ฆ์์ ํ๋ก์ธ์ค๋ค๊น์ง ์ ๋ถ ์ด์์ฒด์ ๊ฐ ๊ฐ์ ๋ก ์ข ๋ฃ์ํด
- This is initiated by the OS, not the parent
โ
๋ถ๋ชจ๊ฐ ์์ ์ข
๋ฃ๋ฅผ ๊ธฐ๋ค๋ฆฌ๋ ๊ฒฝ์ฐ(wait()
)
1
pid = wait(&status); // ์์ ์ค ํ๋๊ฐ ์ข
๋ฃ๋ ๋๊น์ง ๊ธฐ๋ค๋ฆผ
wait()
์์คํ ํธ์ถ์ ํตํด ๋ถ๋ชจ๋ ์์ ํ๋ก์ธ์ค์ ์ข ๋ฃ๋ฅผ ๊ธฐ๋ค๋ฆด ์ ์์- ์์์ด ์ข ๋ฃ๋๋ฉด, ํด๋น ์์์ pid์ status๋ฅผ ๋ฐํ๋ฐ์
ํน์ ์์์ ์ข ๋ฃ๋ง ๊ธฐ๋ค๋ฆฌ๊ณ ์ถ์ ๋ โ
waitpid(pid, &status, 0);
- ํน์ํ ์ํ์ ํ๋ก์ธ์ค | ์ํ | ์ค๋ช
| | โโโโโโโโ | โโโโโโโโโโโโโโโโโโโโโโโโโโโโ- | | Zombie ์ข๋น ํ๋ก์ธ์ค | ์์์ ์ข
๋ฃ๋์ง๋ง, ๋ถ๋ชจ๊ฐ
wait()
ํ์ง ์์ ์์ฒด์ฒ๋ผ ํ ์ด๋ธ์ ๋จ์ ์๋ ํ๋ก์ธ์ค | | Orphan ๊ณ ์ ํ๋ก์ธ์ค | ๋ถ๋ชจ๊ฐ ๋จผ์ ์ข ๋ฃ๋ผ์, ์์์ ๊ณ ์ ์ํ๊ฐ ๋จ. ๋ณดํตinit
ํ๋ก์ธ์ค๊ฐ ๋งก์ |
Andriod Process Importance Hierarchy
์๋๋ก์ด๋๋ ๋ฉ๋ชจ๋ฆฌ ํ๋ณด๋ฅผ ์ํด ํ์์ ๋ฐ๋ผ ํ๋ก์ธ์ค๋ฅผ ์ข ๋ฃํด์ผ ํ ๋๊ฐ ์๋ค ์ด๋ ์ด๋ค ํ๋ก์ธ์ค๋ฅผ ๋จผ์ ์ฃฝ์ผ์ง ๊ฒฐ์ ํ๋ ๊ธฐ์ค์ด ๋ฐ๋ก ์ด Process Importance Hierarchy
- Android will begin terminating processes that are least important
โ ์ค์๋ ์์ (์ โ ์๋๋ก ์ค์ํจ)
1
2
3
4
5
6
7
8
9
โ ์ค์ํจ (์ด๋ ค์ผ ํจ)
โ
โ **Foreground** (์ง์ ์ฌ์ฉ ์ค ์ฑ)
โ **Visible** (ํ๋ฉด ์ผ๋ถ ํ์)
โ **Service** (์์
์ฌ์ ๋ฑ ๋ฐฑ๊ทธ๋ผ์ด๋ ์์
)
โ **Background** (์ด์ ์ฑ, ๋ฐฑ๊ทธ๋ผ์ด๋ ๋๊ธฐ)
โ **Empty** (์บ์๋ง ๋จ์ ์ฑ)
โ
โ ๋ ์ค์ํจ (๋จผ์ ์ข
๋ฃ๋จ)
Multiprocess Architecture - Chrome Browser
๐ฅ โํ๋์ ํ๋ก๊ทธ๋จ์ด ์ฌ๋ฌ ๊ฐ์ ํ๋ก์ธ์ค๋ฅผ ์์ฑโ
์์ ์ ์น ๋ธ๋ผ์ฐ์ ๋ ๋๋ถ๋ถ single process
๋ก ์คํ๋์๋ค.
ํ์ง๋ง Chrome
์ ์ฌ๋ฌ ํ๋ก์ธ์ค๋ก ๋๋์ด ์ฒ๋ฆฌ(multiprocess
)
๐งฉ Chrome์ 3๊ฐ์ง ์ฃผ์ ํ๋ก์ธ์ค ์ ํ
- Browser process: User interface, ๋์คํฌ ์ ๊ทผ, ๋คํธ์ํฌ ์ ์ถ๋ ฅ ๊ด๋ฆฌ
- Renderer process: web page rendering(ํ๋ฉด์ ๋์ฐ๋ ์ฒ๋ฆฌ)(
HTML
,CSS
,JavaScript
์ฒ๋ฆฌ)
์น์ฌ์ดํธ๋ง๋ค ์๋กญ๊ฒ ์์ฑ๋จ โ Sandbox ํ๊ฒฝ์์ ์คํ๋์ด ๋ณด์ ์ํ์ ์ค์ - Plug-in process:
plug-in
(์: Flash ๋ฑ)๋ณ๋ก ๋ถ๋ฆฌ๋ ํ๋ก์ธ์ค ์ฌ์ฉ
Interprocess Communication(IPC)
โ process์ ์ข ๋ฅ
- Independent process
- ๋ค๋ฅธ ํ๋ก์ธ์ค์ ์ ํ ์ํธ์์ฉํ์ง ์์
- Cooperating process
- ๋ค๋ฅธ ํ๋ก์ธ์ค์ ๋ฐ์ดํฐ๋ฅผ ์ฃผ๊ณ ๋ฐ์ผ๋ฉฐ ์ํธ์์ฉํจ
- ์ฆ, ๋ค๋ฅธ ํ๋ก์ธ์ค์ ์ํฅ์ ์ฃผ๊ฑฐ๋ ๋ฐ์ ์ ์์
๐Reasons for cooperating
- Information sharing: ์ฌ๋ฌ ํ๋ก์ธ์ค๊ฐ ๋์ผํ ํ์ผ์ด๋ ๋ฉ๋ชจ๋ฆฌ ๋ฐ์ดํฐ๋ฅผ ํจ๊ป ์ฝ๊ณ ์ธ ๋
- Computation speedup: ํฐ ์์ ์ ์ฌ๋ฌ ํ๋ก์ธ์ค๋ก ๋๋ ์ ๋์์ ์ฒ๋ฆฌ (๋ณ๋ ฌ ์ฒ๋ฆฌ)
- Modularity: ๊ธฐ๋ฅ์ ๋๋์ด ๊ฐ๊ธฐ ๋ค๋ฅธ ํ๋ก์ธ์ค๊ฐ ์ฒ๋ฆฌํ๋๋ก ์ค๊ณ
- Convenience: ์ฌ์ฉ์์๊ฒ ํธ๋ฆฌํ ๊ธฐ๋ฅ ์ ๊ณต (์: ๋ฐฑ๊ทธ๋ผ์ด๋ ์์ ์ฌ์)
๐ก IPC (Interprocess Communication)
Cooperating process
๊ฐ์๋ ํต์ ์ด ํ์ํจ โ ์ด๋ฅผ ์ํดIPC
๊ฐ ์ฌ์ฉ๋จ
๐ฃ Shared memory: ์ฌ๋ฌ ํ๋ก์ธ์ค๊ฐ ๊ณต์ ๋ ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ์ ์ฌ์ฉ
๐ต Message passing: ๋ฉ์์ง๋ฅผ ์ฃผ๊ณ ๋ฐ๋ ๋ฐฉ์์ผ๋ก ๋ฐ์ดํฐ ๊ตํ
Shared memory
๋ ์๊ฐ๋ณด๋ค ๋ณต์ก, 2๊ฐ์ process๊ฐ ์ด๋ค ๊ณต๋ํธ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ฌ์ฉMessage passing
์ message queue๊ฐ ์์ด์ ๊ฑฐ๊ธฐ์ message๋ฅผ ์ฃผ๊ณ ๋ฐ์
Producer-Consumer Problem
๋ ๊ฐ ์ด์์ ํ๋ก์ธ์ค๊ฐ ๋ฐ์ดํฐ๋ฅผ ์ฃผ๊ณ ๋ฐ์ ๋ ๋ฐ์ํ๋ ๋ํ์ ์ธ ๋๊ธฐํ ๋ฌธ์
- Producer: data๋ฅผ ์์ฐ์์ buffer์ ๋ฃ๋ ์ญํ
- Consumer: buffer์์ ๋ฐ์ดํฐ๋ฅผ ๊บผ๋ด ์ฌ์ฉํ๋ ์ญํ
โ Buffer์ ์ข ๋ฅ
- unbounded buffer: ๋ฒํผ ํฌ๊ธฐ์ ์ ํ์ด ์์ โ ์ด๋ก ์ ์ผ๋ก ๋ฌดํ ์ ์ฅ ๊ฐ๋ฅ
- bounded buffer: ๋ฒํผ ํฌ๊ธฐ๊ฐ ์ ํด์ ธ ์์ โ ๊ณต๊ฐ์ด ๊ฝ ์ฐจ๋ฉด ๋๊ธฐ ํ์(์ค์ ์์คํ ์๋ ๋๋ถ๋ถ bounded buffer)
IPC - Shared Memory
๐ Shared Memory: ์๋ก ํต์ ํ๊ณ ์ถ์ ํ๋ก์ธ์ค๋ค๋ผ๋ฆฌ ๊ณต์ ํ๋ ๋ฉ๋ชจ๋ฆฌ ์์ญ
- OS๊ฐ ์ง์ ๊ด์ฌํ์ง ์๊ณ , process๋ค์ด ์์ฒด์ ์ผ๋ก ๋ฉ๋ชจ๋ฆฌ ์ ๊ทผ๊ณผ ํต์ ์ ์กฐ์ ํด์ผ ํจ
โ ์ฃผ์ ๋ฌธ์
๐ฅ Synchronization
- ๋ ํ๋ก์ธ์ค๊ฐ ๋์์ ๋ฉ๋ชจ๋ฆฌ์ ์ ๊ทผํ๋ฉด ์ถฉ๋ ๋ฐ์ ๊ฐ๋ฅ
- ์:
Producer
๊ฐ ๋ฐ์ดํฐ๋ฅผ ์ฐ๊ณ ์๋ ์ค์,Consumer
๊ฐ ์ฝ์ผ๋ ค๊ณ ํ๋ฉด ๋ฐ์ดํฐ๊ฐ ์์๋ ์ ์์
- Bounded-Buffer โ Shared-Memory Solution
1
2
3
4
5
6
7
8
9
// Shared data (์ค์ ํจ์ X)
#define BUFFER_SIZE 10
typedef struct {
. . .
} item;
item buffer[BUFFER_SIZE];
int in = 0; // in: ์๋ก์ด ์์ดํ
์ ๋ฃ์ ์์น, out: ์ง๊ธ ์ฌ์ฉํ ์ ์๋ ์์ดํ
์์น
int out = 0; // in == out์ buffer๊ฐ ๋น ์ํ๋ฅผ ์๋ฏธ
if ((in + 1) % BUFFER_SIZE) == out
์ด๋ฉด ๊ฐ๋ ์ฐฌ ์ํ๋ก ๊ฐ์ฃผ โ ๋ฐ๋ผ์ ํ ์นธ์ ๋น์๋๊ณ ์ฌ์ฉํด์ ์ค์ ์ฌ์ฉ ๊ฐ๋ฅํ ๊ณต๊ฐ์BUFFER_SIZE-1
- producer code
1
2
3
4
5
6
7
8
item next_produced;
while (true) {
while (((in + 1) % BUFFER_SIZE) == out) ; // ๋ฒํผ๊ฐ ๊ฐ๋ ์ฐผ๋์ง ํ์ธ (busy wait)
/* do nothing */
buffer[in] = next_produced; // ์์ฐ๋ ์์ดํ
์ ์ฅ
in = (in + 1) % BUFFER_SIZE; // ๋ค์ ์ ์ฅ ์์น๋ก ์ด๋
}
((in + 1) % BUFFER_SIZE) == out
์ ํ์ธ ์ํ๊ณ ๋ฃ๊ฒ ๋๋ฉดin==out
์ด ๋๋ฉด์ buffer๊ฐ ๋น์ด์๋์ง ๊ฐ๋ ์ฐจ์๋์ง ํ์ธ ๋ถ๊ฐ
- consumer code
1
2
3
4
5
6
7
8
item next_consumed;
while (true) {
while (in == out) ; // ๋ฒํผ๊ฐ ๋น์ด ์์ผ๋ฉด ๋๊ธฐ
/* do nothing */
next_consumed = buffer[out]; // ์์ดํ
๊บผ๋ด๊ธฐ
out = (out + 1) % BUFFER_SIZE; // ๋ค์ ์์น๋ก ์ด๋
}