이 포스트는 서울대학교의 '운영체제의 기초' 강의를 개인 학습용으로 작성한 포스트입니다.
Process Concepts
Process : Program in execution
OS위에서 program을 수행시키는 기본 주체
Program : 저장매체에 저장된 수동적인 code sequence
Process : Memory 등 더 많은 자원을 점유하며 능동적인 존재
Process state와 Execution stream으로 이루어짐
Process state (Context) : Program이 수행되는데 영향을 주는 모든 정보들
Execution stream (Thread of Control) : Sequence of instructions
Process state (Context)
1. Memory context : Code segment, Data segment ...
2. Hardware context : CPU registers, I/O registers ...
3. System context : Process table, Page table ...
* Multiprogramming : Memory에 여러 process가 있음
* Multiprocessing : 한 시점에 여러 process가 있음
System design
Task : design-time entity
Process : run-time entity
Implementation : design-time entities -> run-time entities mapping
State Transition
: Ready, Running, Waiting
New, Terminated

* Process life cycle : 프로세스가 생성부터 종료까지 발생하는 일련의 상태 변화
* Ready queue : 일반적으로 PCB들을 Linked list로 연결하여 구현
* Waiting queue : Waiting 이유에 따라 별도의 queue 사용
Process Scheduling
: 2-level architecture : Mechanism + Policy
Dispatcher (Mechanism)
loop forever{
run the process for a while
stop it and save its state
load state of another process
}
Non-preemptive scheduling : 프로세스가 자발적으로 CPU 양보 (Trap, System call)
Preemptive scheduling : OS가 강제로 프로세스로부터 CPU를 빼앗음 (H/W interrupt)
* Kernel은 Passive entity에 가까움, library 같은 느낌
* Kernel mode execution이 일어날 때 수행의 주체가 되는 프로세스는? : System call을 호출한 프로세스
Context Switching
: 현재 수행중인 Process state (context)를 저장하고 다음 수행할 프로세스의 state를 불러오는 작업
Memory context : 필요한 경우 대피
Hardware context : 무조건 대피 (Program counter, Processor status word, General purpose word...)
Kernel context : 대피 필요 X
* 하드웨어 지원 : Save PC, Save PSR
Implementation

* Process 생성할 때 Fake stack 만듦 : Context switch를 위해
Process Creation and Termination
Process Creation From Scratch
1. Scratch로부터 Load code & data into memory
2. Create empty call stack
3. Create and initialize PCB
4. Put process on ready queue
Process Creation Cloning : fork()
1. Stop current process & save its state
2. Copy of code, data, stack, PCB
3. Add new PCB to ready queue
Process life cycle in UNIX

exec() : code와 data를 overide하여 새로운 프로레스로 출발, parent와 child가 다른 프로그램 실행 가능
exit() : data structure, resource 등을 OS가 걷어감
* Zombie state : exit()를 마치고 Parent process가 exit status를 읽어가기 기다리는 Process 상태
* COW (Copy on Write) : fork()의 비효율을 극복하기 위해, Process Context를 fork() 시점에 복사하지 않고 Data segment에 새 값이 쓰여질 때 복사하는 기법
* PID : parenet는 child의 PID저장 / child는 PID = 0 저장
wait() : Child가 exit()하는 것을 기다림
abort() : 강제로 Child를 종료시킴
Multithreading
: Concurrency는 높이면서 Execution unit을 생성하거나 수행시키는데 드는 부담을 줄임
Server Architecture
Iterative server : Queue에 있는지 주기적으로 확인하고 직접 처리
Concurrent server : 서버가 직접 처리하지 않고 처리해 줄 worker를 fork함
-> 프로세스 생성에 큰 부담이 들므로 Thread를 생성함

Thread 구현 : Stack, Thread ID, TCB (Thread Control Block)
User Address Space 공유함
효과
1. Effective concurrent programming
2. Resource sharing (No need for IPC)
3. Cheap to implement
4. Good for reactive system
Threads Implementation
1. User-Level Thread
Thread library : 라이브러리에 scheduling 기능 등 포함
I/O가 거의 없는 프로그램에 유용함
문제점
: Blocking system call을 호출할 경우 해당 process의 모든 thread가 block됨
OS가 thread에게 interrupt를 전달할 수 없어 preemptive scheduling 불가
2. Kernel-Level Thread
Kernel이 API형태로 필요한 기능 제공
문제점
: Kernel의 Overhead 증가
3. Combined UL/KL Thread
N:M mapping
Thread creation, Thread synchronization은 user space에서 함
POSIX
다양한 UNIX 계열 운영체제들의 API를 표준화하기 위해 IEEE가 정의한 인터페이스
pthread_create()
pthread_exit()
pthread_join() : 자식 thread가 exit()할 때까지 기다림
pthread_yield() : CPU 양도, OS가 관리함
'Basic Learning > Operating System Concepts' 카테고리의 다른 글
Deadlock (0) | 2020.06.15 |
---|---|
Process Synchronization (0) | 2020.06.14 |
CPU Scheduling (0) | 2020.06.14 |
Review of Computer Hardware (0) | 2020.06.13 |
Introduction to OS (0) | 2020.06.13 |