#Process State
프로세스의 상태는 크게 3가지, 세분화 되었을 때는 많으면 5가지에서 15가지 까지 나뉠 수 있다. 프로세스 상태의 개수는 OS 아키텍쳐 마다 다르게 디자인 되어있기 때문에 OS 마다 다를 수 있다. 우리는 크게 3가지 process state를 알아보자.
- Running
- 프로세스가 하나의 명령어를 할당 받아 CPU에서 실행 시키고 있는 상태
- Ready
- 프로세스가 운영체제의 승인을 받고 CPU에서 실행이 준비된 상태 (CPU 지정을 기다리는 상태/No CPU is ready at this state, CPU 제외 다른 자원들은 프로세스의 메모리 로드 상태)
- Blocked (waiting)
- 프로세스가 어떠한 event (보통 I/O 요청에 의한 syscall , 또는 동기신호[synchronize signal]) 때문에 CPU에서 실행 준비가 안된 상태. CPU가 준비됐어도 사용 불가 상태 (CPU 반납한 상태, 보통 I/O 작업이 끝나면 다시 ready상태로 돌아감).
여기서 중요한 점은 커널이 멀티 쓰레딩을 지원하는 시스템일 경우, 각 상태는 하나의 쓰레드 마다 별도로 유지된다.
#Process State Transition (처리 상태 전이)
이제는 프로세스 상태의 이동 과정을 알아봅시다.
- RUNNING -> BLOCKED (Block)
- CPU를 할당 받은 프로세스가 I/O등 다른 자원을 기다리는 상태.
- 여기서 다른 자원이란
- waiting for input (keystroke, file from disk, network message, data from Unix pipe 등등)
- waiting for exclusive access to a resource (acquire lock)
- waiting for a signal from another thread/process
- waiting for time to pass (e.g, sleep(2) syscall)
- waiting for a child process to terminate
- CPU를 할당 받은 프로세스가 I/O등 다른 자원을 기다리는 상태.
- BLOCKED -> READY (Wake-up)
- 대기 중인 프로세스가 I/O작업 또는 다른 event 처리가 끝나 준비된상태.
- 여기서 os가 프로세스를 ready queue 자료구조에 추가함
- 대기 중인 프로세스가 I/O작업 또는 다른 event 처리가 끝나 준비된상태.
- READY -> RUNNING (Dispatch)
- Ready Queue에있는 프로세스를 schedular가 선택 후 CPU를 할당
- 여기서 오직 CPU당 하나의 프로세스만 선택가능.
- 초과할당의 경우 scheduling policy 요구
- Ready Queue에있는 프로세스를 schedular가 선택 후 CPU를 할당
- RUNNING -> READY (TimerRunout)
- 할당 받은 CPU의 사용 기간을 다 사용한 경우, 프로세스가 descheduled됨
- os가 프로세스를 먼저 획득하고 다른 ready 프로세스에게 턴을줌
- 드물게는 프로세스가 자발적으로 CPU yield함
- 할당 받은 CPU의 사용 기간을 다 사용한 경우, 프로세스가 descheduled됨
- RUNNING->Terminated (Exit)
- 프로세스의 정상/비정상 종료
#Linux에서의 Process State
앞에서도 말했듯이 보통의 os들은 5개에서 15개의 prcess state을 가지고있다. Linux의 경우 9개의 process state를 가지고있다
- D (blocked): uninterruptible sleep (대게로 I/O)
- I: Idle kernel thread
- R (running, ready): running or runnable, ready (on run queue)
- S (blocked): interruptible sleep (event가 완료되기를 기다리는 상태)
- T: stopped by a job control signal
- t: stopped by debugger during the tracing
- W: paging (커널 2.6.xx 부터 지원안됨)
- X: dead (should never be seen)
- Z: defunct ("zombie") process (terminated but not reaped by its parent)
#Process States and Job control
몇몇개의 시스템들은 프로세스를 잠깐동안 멈추고 (suspend) 나중에 다시 온전한 상태로 실행할수있는 능력이있다. 이것이 바로 job control이다. Job control의 대표적인 예로는 linux의 Ctrl-Z 가있다. 이 기능은 state transition과 별개로 event process에의해 실행되며 중지된 프로세스에도 event가 arrive하는것이 가능함.
#Programmer's View (Process State Transition)
process state transition 은 프로그래머의 관할구역 밖에서 결정된다. (유저 action, 유저 input, I/O 이벤트, interprocess communication, synchronization 또는 OS (scheduling decisions) 에 의해 결정됨). 또한 process state transition은 짧은 시간동안 자주 발생할수있다. 예를 들어 linux에서 preemption은 Running process의 40ms 마다 발생하고, process가 shared resources (locks 또는 pipes) 와 interact할때마다 block/unblock이 자주 발생할수있다.
또한 실용적인 목적때문에 이 transition과 resulting execution order는 예측할수 없다. 그리고 resulting concurrecy는 프로그래머들이 프로세스의 실행 순서를 추측하면 안되도록 요구하고, 프로그래머들은 signaling과 synchronization facilities 를 사용하여 process interaction을 coordinate해야한다.
#연습문제
#연습문제 보기
# 답
1. What happens if an n CPU system has exactly n READY processes?Answer: Each CPU runs exactly 1 process.
2. What happens if an n CPU system has 0 READY processes?
Answer: The system is idle and goes into a low-power mode.
3. What happens if an n CPU system has k < n READY processes?
Answer: n-k CPUs are idle, and k CPUs run exactly 1 process.
4. What happens if an n CPU system has 2n READY processes?
Answer: Every process takes about twice as long as it normally would.
5. What happens if an n CPU system has m ≫ n READY processes?
Answer: The system becomes very laggy, and processes take much longer than normal.
6. What is a typical number of BLOCKED/READY/RUNNING processes in a system (e.g. your phone or laptop?)
Answer: 150-500 BLOCKED, and 0-2 RUNNING.
7. How does the code you write influence the proportion of time your program spends in the READY/RUNNING state?
Answer: Performing computation without performing I/O means the process is READY at all times and will be RUNNING if scheduled.
8. How can the number of processes in the READY/RUNNING state be used to measure CPU demand?
Answer: The load average is a weighted moving average of the size of the ready queue (including RUNNING processes); it says how many CPUs could be kept busy.
9. Assuming the same functionality is achieved, is it better to write code that causes a process to spend most of its time BLOCKED, or READY?
Answer: Prefer BLOCKED to READY because it does not consume CPU; use OS facilities to wait for events rather than poll in a loop.
'Computer Science > Computer Systems' 카테고리의 다른 글
[Lecture 3] Unix Signals (0) | 2022.09.15 |
---|---|
[Lecture 2] Unix File Descriptors and Pipes (0) | 2022.09.10 |
[Lecture 1-3] Processes Part III (0) | 2022.09.04 |
[Mini-Lecture] Character Sets and Unicode (0) | 2022.09.04 |
[Lecture 1-1] Processes Part I (0) | 2022.08.29 |