Search
Duplicate

ν”„λ‘œμ„ΈμŠ€ 관리

ν”„λ‘œμ„ΈμŠ€λŠ” 싀행쀑인 ν”„λ‘œκ·Έλž¨.
Process is a program in execution
β€’
ν”„λ‘œμ„ΈμŠ€μ˜ λ¬Έλ§₯(context)
β—¦
νŠΉμ • μ‹œμ μ„ 놓고 봀을 λ•Œ 이 ν”„λ‘œμ„ΈμŠ€κ°€ μ–΄λ””κΉŒμ§€ μˆ˜ν–‰μ„ ν–ˆκ³ , 무엇을 ν•΄μ•Όν•˜λ‚˜λ₯Ό μ•Œλ €μ£ΌλŠ” 것
β—¦
PCκ°€ μ–΄λ””λ₯Ό κ°€λ₯΄ν‚€κ³  μžˆλŠ”κ°€ μ½”λ“œμ˜ μ–΄λ””λΆ€λΆ„κΉŒμ§€ μ‹€ν–‰ν–ˆλŠ”κ°€
β—¦
ν”„λ‘œμ„ΈμŠ€ λ©”λͺ¨λ¦¬μ— μ–΄λ–€ λ‚΄μš©μ„ λ‹΄κ³  μžˆλŠ”κ°€ μŠ€νƒμ— μ–΄λ””κΉŒμ§€, 무엇을 μŒ“μ•˜λŠ”κ°€
β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
β—¦
CPU 와 κ΄€λ ¨λœ ν•˜λ“œμ›¨μ–΄ λ¬Έλ§₯
β–ͺ
ν”„λ‘œμ„ΈμŠ€λŠ” CPU λ₯Ό 작고 λ§€μˆœκ°„ instruction 을 μ‹€ν–‰
β–ͺ
λ¬Έλ§₯을 μ•ŒκΈ° μœ„ν•΄μ„œ register 에 μ–΄λ–€ 값이 λ“€μ–΄κ°€μžˆκ³ , PC λŠ” μ–΄λ””λ₯Ό κ°€λ₯΄ν‚€κ³  μžˆλŠ”μ§€ 확인
β—¦
λ©”λͺ¨λ¦¬μ™€ κ΄€λ ¨λœ λ¬Έλ§₯
β–ͺ
code, data, stack 에 μ–΄λ–€ λ‚΄μš©μ΄ λ“€μ–΄κ°€μžˆλŠ”κ°€
β—¦
컀널 자료 ꡬ쑰와 κ΄€λ ¨λœ λ¬Έλ§₯
β–ͺ
PCB(Process Control Block)
β–ͺ
컀널 μŠ€νƒ
β€’
ν”„λ‘œμ„ΈμŠ€ μ»¨ν…μŠ€νŠΈλ₯Ό μ•Œμ•„μ•Ό ν•˜λŠ” 이유
β†’ time sharing(μ‹œλΆ„ν• ), λ©€ν‹° νƒœμŠ€ν‚Ή λ“± μ—¬λŸ¬ ν”„λ‘œμ„ΈμŠ€κ°€ ν•˜λ‚˜μ˜ CPU μ—μ„œ λ™μ‹œμ— μ‹€ν–‰λ˜κΈ° λ•Œλ¬Έ

ν”„λ‘œμ„ΈμŠ€ μƒνƒœ(Process State)

Running

β€’
맀 μˆœκ°„ CPU λ₯Ό 작고 instruction 을 μ‹€ν–‰ν•˜λŠ” ν”„λ‘œμ„ΈμŠ€μ˜ κ°œμˆ˜λŠ” ν•˜λ‚˜μ΄λ‹€.
β€’
μ΄λ•Œ, instruction 을 μ‹€ν–‰ν•˜λŠ” ν”„λ‘œμ„ΈμŠ€μ˜ μƒνƒœλ₯Ό running
β€’
CPU λ₯Ό λ‚΄λ €λ†“λŠ” 상황은 3가지
β—¦
IO 같은 μ˜€λž˜κ±Έλ¦¬λŠ” μž‘μ—…μ΄ ν•„μš”ν•΄μ„œ μžμ§„ν•˜μ—¬ λ‚΄λ €λ†“μŒ β†’ waiting
β—¦
timer interrupt(ν• λ‹Ήλœ μ‹œκ°„μ΄ 만료) β†’ ready

Ready

β€’
λ©”λͺ¨λ¦¬ λ“± λ‹€λ₯Έ 쑰건을 λͺ¨λ‘ λ§Œμ‘±ν•˜κ³  CPU λ₯Ό 작기 μœ„ν•΄ κΈ°λ‹€λ¦¬λŠ” ν”„λ‘œμ„ΈμŠ€μ˜ μƒνƒœ

Blocked(wait, sleep)

β€’
IO μž‘μ—… λ˜λŠ” λ””μŠ€ν¬μ—μ„œ μ½μ–΄μ™€μ•Όν•˜λŠ”λ° 아직 μ™„λ£Œν•˜μ§€ λͺ»ν•˜λŠ” λ“± CPU λ₯Ό μž‘μ•„λ„ λ‹Ήμž₯ instruction 을 μˆ˜ν–‰ν•  수 μ—†λŠ” μƒνƒœ
β€’
IO, disk IO, 곡유 데이터 μ ‘κ·Ό μž‘μ—… λ“±

Suspended(stopped)

β€’
외뢀적인 이유(medium-term scheduler λ“±)둜 ν”„λ‘œμ„ΈμŠ€μ˜ μˆ˜ν–‰μ΄ μ •μ§€λœ μƒνƒœ
β€’
ν”„λ‘œμ„ΈμŠ€λŠ” ν†΅μ§Έλ‘œ λ””μŠ€ν¬μ— swap out
Blocked: μžμ‹ μ΄ μš”μ²­ν•œ event κ°€ 만쑱되면 ready μƒνƒœ Suspended: μ™ΈλΆ€μ—μ„œ resume ν•΄ μ£Όμ–΄μ–΄μ•Ό active
Plain Text
볡사
νŽΈμ˜μƒ queue μ§€λ§Œ, μ‹€μ œλ‘œλŠ” priority queue 이닀.

PCB(Process Control Block)

β€’
운영체제 컀널이 각 ν”„λ‘œμ„ΈμŠ€λ§ˆλ‹€ ν”„λ‘œμ„ΈμŠ€μ™€ κ΄€λ ¨λœ 정보, ν”„λ‘œμ„ΈμŠ€λ₯Ό κ΄€λ¦¬ν•˜κΈ° μœ„ν•΄ μš΄μ˜μ²΄μ œκ°€ μžμ‹ μ˜ 데이터 μ˜μ—­μ— 두고 μžˆλŠ” 것

OSκ°€ 관리상 μ‚¬μš©ν•˜λŠ” 정보

β€’
Process state, pointer, pid
β€’
μŠ€μΌ€μ₯΄λ§ 정보, μš°μ„ μˆœμœ„ λ“±

CPU μˆ˜ν–‰ κ΄€λ ¨ ν•˜λ“œμ›¨μ–΄ κ°’

β€’
PC, registers
β€’
ν”„λ‘œμ„ΈμŠ€ μ»¨ν…μŠ€νŠΈλ₯Ό μœ μ§€ν•˜κΈ° μœ„ν•œ 정보듀

λ©”λͺ¨λ¦¬ κ΄€λ ¨

β€’
code, data, stack 의 μœ„μΉ˜ 정보

파일 κ΄€λ ¨

β€’
open files list

Context Switching

β€’
ν”„λ‘œμ„ΈμŠ€λŠ” 짧은 μ‹œκ°„λ™μ•ˆ CPU λ₯Ό μ–»μ—ˆλ‹€κ°€ λ‚΄λ†“λŠ” 과정을 λ°˜λ³΅ν•œλ‹€.
β€’
CPU λ₯Ό ν•œ ν”„λ‘œμ„ΈμŠ€μ—μ„œ λ‹€λ₯Έ ν”„λ‘œμ„ΈμŠ€λ‘œ λ„˜κ²¨μ£ΌλŠ” κ³Όμ •
β—¦
CPU λ₯Ό μ‚¬μš©ν•˜λ˜ ν”„λ‘œμ„ΈμŠ€κ°€ λ‹€λ₯Έ ν”„λ‘œμ„ΈμŠ€μ— λ„˜κ²¨μ€„ λ•Œ λ‹Ήμ‹œμ˜ μƒνƒœλ₯Ό ν”„λ‘œμ„ΈμŠ€μ˜ PCB 에 μ €μž₯(PC, registers, memory map λ“±)
β—¦
CPU λ₯Ό μƒˆλ‘œ μž‘λŠ” ν”„λ‘œμ„ΈμŠ€λŠ” ν”„λ‘œμ„ΈμŠ€μ˜ μƒνƒœλ₯Ό PCB μ—μ„œ μ½μ–΄μ˜΄
β€’
System call: ν”„λ‘œμ„ΈμŠ€κ°€ 본인이 ν•„μš”ν•΄μ„œ OS 에 μš”μ²­
β€’
system call μ΄λ‚˜ interrupt λ°œμƒ μ‹œ λ°˜λ“œμ‹œ context switch κ°€ μΌμ–΄λ‚˜λŠ” 것은 μ•„λ‹ˆλ‹€.
ν”„λ‘œμ„ΈμŠ€κ°€ λ‹€λ₯Έ ν”„λ‘œμ„ΈμŠ€λ‘œ λ„˜μ–΄κ°€μ•Ό context switch

Process Queue

β€’
queue μ—λŠ” 각 ν”„λ‘œμ„ΈμŠ€μ˜ pcb κ°€ λ“€μ–΄κ°„λ‹€.
β€’
Job queue
β—¦
ν˜„μž¬ μ‹œμŠ€ν…œ λ‚΄μ˜ λͺ¨λ“  ν”„λ‘œμ„ΈμŠ€ 집합
β€’
Ready queue
β—¦
ν˜„μž¬ λ©”λͺ¨λ¦¬ 내에 μžˆμœΌλ©΄μ„œ CPU λ₯Ό μž‘μ•„ μ‹€ν–‰λ˜κΈ°λ₯Ό κΈ°λ‹€λ¦¬λŠ” ν”„λ‘œμ„ΈμŠ€μ˜ 집합
β€’
Device queue
β—¦
IO device 처리λ₯Ό κΈ°λ‹€λ¦¬λŠ” ν”„λ‘œμ„ΈμŠ€μ˜ 집합
β€’
ν”„λ‘œμ„ΈμŠ€λ“€μ€ 각 큐듀을 μ˜€κ°€λ©΄μ„œ μˆ˜ν–‰
IO request 와 wait for an interrupt λ°•μŠ€λŠ” κ°œλ…μ΄ μœ μ‚¬

Scheduler

long-term scheduler

β€’
job scheduler
β€’
λ©”λͺ¨λ¦¬λ₯Ό μ–΄λ–€ ν”„λ‘œμ„ΈμŠ€μ—κ²Œ 쀄지 κ²°μ •
β€’
ν”„λ‘œμ„ΈμŠ€κ°€ μ‹œμž‘μ΄ 되고 ready μƒνƒœμ— κ°€κΈ° μœ„ν•΄μ„œλŠ” memory 할당이 ν•„μš”ν•˜λ‹€. μ΄λ•Œ λ©”λͺ¨λ¦¬ 할당을 κ²°μ •ν•˜λŠ” μŠ€μΌ€μ₯΄λ§
β€’
time sharing system μ—λŠ” 보톡 long-term scheduler κ°€ μ—†λ‹€(무쑰건 ready).
β€’
multiprogramming 을 μ œμ–΄
β€’
λ©”λͺ¨λ¦¬μ— ν”„λ‘œκ·Έλž¨ λͺ‡κ°œλ₯Ό μ˜¬λ €λ†“μ„μ§€λ₯Ό μ œμ–΄

short-term scheduler

β€’
CPU scheduler
β€’
ms λ‹¨μœ„
β€’
λ‹€μŒ λ²ˆμ— μ–΄λ–€ ν”„λ‘œμ„ΈμŠ€λ₯Ό running μ‹œν‚¬μ§€ κ²°μ •

medium-term scheduler

β€’
μž₯κΈ° μŠ€μΌ€μ₯΄λŸ¬λŠ” ν”„λ‘œκ·Έλž¨μ΄ μ‹œμž‘ν•  λ•Œ λ©”λͺ¨λ¦¬λ₯Ό 할당할지 말지 κ²°μ •ν•˜λŠ”λ°, 쀑기 μŠ€μΌ€μ₯΄λŸ¬λŠ” λ©”λͺ¨λ¦¬μ—μ„œ 일뢀 ν”„λ‘œμ„ΈμŠ€λ₯Ό ν†΅μ§Έλ‘œ λ””μŠ€ν¬λ‘œ μ«“μ•„λ‚Έλ‹€.
β€’
multiprogramming 을 μ œμ–΄

μ“°λ ˆλ“œ

ν”„λ‘œμ„ΈμŠ€ λ‚΄λΆ€ CPU μˆ˜ν–‰ λ‹¨μœ„
pointer, process state, process number λŠ” OS κ΄€λ¦¬μš© 정보
memory limits, list of open files λŠ” μžμ› κ΄€λ ¨ 정보
Program Counter: λ©”λͺ¨λ¦¬μ˜ μ–΄λŠ 뢀뢄을 μ‹€ν–‰ν•˜κ³  μžˆλŠ”μ§€ κ°€λ₯΄ν‚€λŠ” 것
같은 일을 ν•˜λŠ” ν”„λ‘œμ„ΈμŠ€λ₯Ό μ—¬λŸ¬κ°œ μ‹€ν–‰ν•˜λ €κ³  ν–ˆμ„ λ•Œ λ©”λͺ¨λ¦¬ λ‚­λΉ„κ°€ μžˆκΈ°λ•Œλ¬Έμ— ν”„λ‘œμ„ΈμŠ€λ₯Ό ν•˜λ‚˜λ§Œ μ‹€ν–‰ν•˜κ³  λ‚΄λΆ€μ—μ„œ PC, λ˜λŠ” CPU μˆ˜ν–‰ λ‹¨μœ„λ§Œ μ—¬λŸ¬ 개 λ†“λŠ” 것
ν”„λ‘œμ„ΈμŠ€ λ‚΄λΆ€μ—μ„œ κ³΅μœ ν•  수 μžˆλŠ” 뢀뢄은 μ΅œλŒ€ν•œ κ³΅μœ ν•˜κ³  CPU μˆ˜ν–‰κ³Ό κ΄€λ ¨λœ 정보(PC, registers, stack) 만 λ…λ¦½μ μœΌλ‘œ κ°–λŠ”λ‹€.
A thread(or lightweight process) is a basic unit of CPU utilization.

ꡬ성

β€’
Program Counter
β€’
register set
β€’
stack space

κ³΅μœ ν•˜λŠ” λΆ€λΆ„(task)

β€’
code section
β€’
data section
β€’
OS resources

μž₯점

β€’
ν•˜λ‚˜μ˜ μ“°λ ˆλ“œκ°€ blocked(waiting) μƒνƒœ λ™μ•ˆμ—λ„ λ‹€λ₯Έ μ“°λ ˆλ“œκ°€ running μƒνƒœκ°€ λ˜μ–΄ λΉ λ₯΄κ²Œ 처리(web page μ½μ–΄μ˜€λŠ” μ˜ˆμ‹œ)
β€’
λ™μΌν•œ 일을 μˆ˜ν–‰ν•˜λŠ” 닀쀑 μ“°λ ˆλ“œκ°€ ν˜‘λ ₯ν•˜μ—¬ 높은 처리율(throughput)κ³Ό μ„±λŠ₯ ν–₯상
β€’
병렬성 λ†’μž„
β€’
Responsiveness
β—¦
ν•˜λ‚˜μ˜ μ“°λ ˆλ“œκ°€ block μƒνƒœλΌλ„ λ‹€λ₯Έ μ“°λ ˆλ“œλŠ” running μƒνƒœ
β€’
Resource Sharing
β—¦
code, data, ν”„λ‘œμ„ΈμŠ€ μžμ› 등을 곡유
β€’
κ²½μ œμ„±
β—¦
n 개의 ν”„λ‘œμ„ΈμŠ€λ₯Ό λ§Œλ“œλŠ” 것보닀 n 개의 μ“°λ ˆλ“œλ₯Ό λ§Œλ“œλŠ” 것이 creating, CPU switching μ—μ„œ κ²½μ œμ μ΄λ‹€.
β€’
λ©€ν‹° ν”„λ‘œμ„Έμ„œ κ΅¬μ‘°μ—μ„œ 병렬성

ν”„λ‘œμ„ΈμŠ€ 생성

β€’
λΆ€λͺ¨ ν”„λ‘œμ„ΈμŠ€κ°€ μžμ‹ ν”„λ‘œμ„ΈμŠ€ 생성
β€’
ν”„λ‘œμ„ΈμŠ€ 트리(계측 ꡬ쑰) ν˜•μ„±
β€’
ν”„λ‘œμ„ΈμŠ€κ°€ μ‹€ν–‰λ˜λ €λ©΄ μžμ›μ΄ ν•„μš”
β—¦
OS λ‘œλΆ€ν„° λ°›μŒ
β€’
μ£Όμ†Œ 곡간
β—¦
μžμ‹ ν”„λ‘œμ„ΈμŠ€λŠ” λΆ€λͺ¨ ν”„λ‘œμ„ΈμŠ€μ˜ 곡간을 λ³΅μ‚¬ν•˜κ³  κ·Έ 곡간에 μƒˆλ‘œμš΄ ν”„λ‘œκ·Έλž¨μ„ μ˜¬λ¦°λ‹€.
β€’
μœ λ‹‰μŠ€
β—¦
fork() μ‹œμŠ€ν…œ 콜이 μƒˆλ‘œμš΄ ν”„λ‘œμ„ΈμŠ€ 생성
A process is created by the fork() system call β†’ creates a new address space that is a duplicate of the caller.
β—¦
copy μ‹œ λͺ¨λ“  정보(code, data, stack)을 λ©”λͺ¨λ¦¬μ— copy ν•˜λŠ” 것이 μ•„λ‹ˆλΌ κ³΅μœ ν•  수 μžˆλŠ”κ±΄ μ΅œλŒ€ν•œ κ³΅μœ ν•œ λ’€ copy
β—¦
exec() μ‹œμŠ€ν…œ 콜둜 μƒˆλ‘œμš΄ ν”„λ‘œκ·Έλž¨μ„ λ©”λͺ¨λ¦¬μ— 올림
β€’
copy on write
int main() { int pid; pid = fork(); if (pid == 0) printf("I am child"); else if (pid > 0) printf("I am parent"); }
C
볡사
β†’ λΆ€λͺ¨ ν”„λ‘œμ„ΈμŠ€μ—μ„œ fork() λ₯Ό ν•˜λ©΄ μžμ‹ ν”„λ‘œμ„ΈμŠ€κ°€ λ§Œλ“€μ–΄μ§€λŠ”λ°, μžμ‹ ν”„λ‘œμ„ΈμŠ€λŠ” main ν•¨μˆ˜ μ²˜μŒλΆ€ν„° λ‹€μ‹œ μ‹œμž‘ν•˜λŠ” 것이 μ•„λ‹ˆκ³ , fork() 이후뢀터 μ‹€ν–‰λœλ‹€. κ·Έλž˜μ„œ λ¬΄ν•œνžˆ 반볡 X
μ΄μœ λŠ” fork() λΆ€λͺ¨ ν”„λ‘œμ„ΈμŠ€μ˜ μ»¨ν…μŠ€νŠΈ(PC)λ₯Ό λ³΅μ œν•˜κΈ° λ•Œλ¬Έμ΄λ‹€.
β†’ fork() 의 return value κ°’μœΌλ‘œ μžμ‹ ν”„λ‘œμ„ΈμŠ€λŠ” 0 을 μ–»κ³  λΆ€λͺ¨ ν”„λ‘œμ„ΈμŠ€λŠ” μ–‘μˆ˜ 값을 μ–»λŠ”λ‹€. 이 return value 둜 λΆ€λͺ¨ ν”„λ‘œμ„ΈμŠ€μ™€ μžμ‹ ν”„λ‘œμ„ΈμŠ€λ₯Ό ꡬ뢄할 수 μžˆλ‹€.
β—¦
exec()
A process can execute a different program by the exec() system call. β†’ replaces the memory image of the caller with a new program.
β—¦
λ‹€λ₯Έ ν”„λ‘œκ·Έλž¨μ„ μ‹€ν–‰μ‹œν‚¨λ‹€.
int main() { printf("hello"); /* execlp("/bin/date", "/bin/date", (char *) 0); */ execlp("echo", "echo", "HI", (char *) 0); printf("bye"); }
C
볡사
β†’ printf(”bye”); λŠ” μ‹€ν–‰λ˜μ§€ μ•ŠλŠ”λ‹€.
β—¦
wait()
β—¦
컀널은 μžμ‹ ν”„λ‘œμ„ΈμŠ€κ°€ μ’…λ£Œλ  λ•ŒκΉŒμ§€ λΆ€λͺ¨ ν”„λ‘œμ„ΈμŠ€λ₯Ό sleep μ‹œν‚¨λ‹€(block μƒνƒœ)
β—¦
μžμ‹ ν”„λ‘œμ„ΈμŠ€κ°€ μ’…λ£Œλ˜λ©΄ 컀널은 λΆ€λͺ¨ ν”„λ‘œμ„ΈμŠ€λ₯Ό κΉ¨μš΄λ‹€(ready μƒνƒœ)
main { int childPID; childPID = fork(); if (childPID == 0) printf("HI"); else { wait(); } printf("wake up"); }
C
볡사
β—¦
exit()
β—¦
λͺ…μ‹œμ μœΌλ‘œ ν”„λ‘œκ·Έλž¨ μ’…λ£Œ
β€’
정리
β—¦
fork(): create a child(copy)
β—¦
exec(): overlay new image
β—¦
wait(): sleep until child is done
β—¦
exit(): frees all the resources, notify parent

ν”„λ‘œμ„ΈμŠ€κ°„ ν˜‘λ ₯

독립적 ν”„λ‘œμ„ΈμŠ€(independent process)

β€’
ν”„λ‘œμ„ΈμŠ€λŠ” 각자의 μ£Όμ†Œ 곡간을 가지고 μˆ˜ν–‰
β€’
μ›μΉ™μ μœΌλ‘œ ν•˜λ‚˜μ˜ ν”„λ‘œμ„ΈμŠ€λŠ” λ‹€λ₯Έ ν”„λ‘œμ„ΈμŠ€μ˜ μˆ˜ν–‰μ— 영ν–₯ X

ν˜‘λ ₯ ν”„λ‘œμ„ΈμŠ€(cooperating process)

β€’
ν˜‘λ ₯ λ©”μ»€λ‹ˆμ¦˜μ„ 톡해 ν•˜λ‚˜μ˜ ν”„λ‘œμ„ΈμŠ€κ°€ λ‹€λ₯Έ ν”„λ‘œμ„ΈμŠ€μ˜ μˆ˜ν–‰μ— 영ν–₯을 λ―ΈμΉ¨

IPC(inter-process communication)

β€’
λ©”μ‹œμ§€ 전달 방식
β—¦
message passing: 컀널을 톡해 λ©”μ‹œμ§€ 전달(자체적으둜 전달은 λͺ»ν•˜κ³  컀널을 ν†΅ν•΄μ„œ 전달해야 함)
β–ͺ
ν”„λ‘œμ„ΈμŠ€ 사이에 곡유 λ³€μˆ˜(shared variable)λ₯Ό μ‚¬μš©ν•˜μ§€ μ•Šκ³  톡신
β–ͺ
direct communication: ν†΅μ‹ ν•˜λ €λŠ” ν”„λ‘œμ„ΈμŠ€μ˜ 이름을 λͺ…μ‹œμ μœΌλ‘œ ν‘œμ‹œ
β–ͺ
indirect communication: mailbox or port λ₯Ό 톡해 λ©”μ‹œμ§€λ₯Ό κ°„μ ‘μ μœΌλ‘œ 전달
β€’
μ£Όμ†Œ 곡간 곡유 방식
β—¦
shared memory: μ„œλ‘œ λ‹€λ₯Έ ν”„λ‘œμ„ΈμŠ€κ°„μ— 일뢀 μ£Όμ†Œ 곡간을 곡유, μ»€λ„μ—κ²Œ shared memory ν•œλ‹€λŠ” μ‹œμŠ€ν…œ μ½œμ„ 해야함