Processses and Threads
Process Creation and Termination
Process Creation(1)
Creating new processes :
- Build one from scratch ( Ex : UNIX Process 0 )
- Clone an exsting one ( Ex : UNIX fork() syscall )
From scratch
- Load code and data into memory
- Create (empty) call stack
- Create and initialize a process control block
- Put process on ready list
Process Creation (2)
Cloning : UNIX fork() system call
- Stop current process and save its state.
- Make a copy of cde, data, stack and PCB => PID는 다르게 저장됨.
- Add new PCB to ready list
- Not quite right. What’s missing?
Process creation in UNIX with fork() and exec().
Process Creation (3)
- Process life cycle in UNIX
- wait()에서 parent process는
- child process가 끝날때까지 대기
- 올바르게 종료 state 값을 반환.
- Zombie state
- Child process가 exit()을 마치고,
parent process가 자신의 Exit status를 읽어가기를 기다리는 Process 상태.
Process Creation (4)
- In UNIX
UNIX에서 fork()로 프로세스를 생성하는 이유?
Process Creation (5)
- Shell example
1. for(;;){
2. cmd = readcmd();
3. pid = fork(); //
4. if(pid < 0){
5. perror("fork failed");
6. exit(-1);
7. } else if (pid == 0){ //child가 수행되는 부분
8. //child process는 fork()다음 부터 실행되므로 return value 0으로 child process에게 넘깁니다.
9. //Child - setup enviroment
10. if(exec(cmd) < 0) perror("exec failed");
11. exit(-1); //Exit on exec failure
12. } else {
13. //Parent - Wait for command to finish.
14. wait(pid);
15. }
16. }
- Shell ( Command Line Interpreter )
- 사용자가 입력한 명령어를, 입력으로 받아들여 새로운 프로세스를 수행시키는 프로그램. system call에서 return value가 양수이면 정상작동, 음수이면 error를 의미합니다.
Process Termination
- Process executes last statement and asks the OS to decide it ( exit() )
- Output data from child to parent ( via wait() )
- Process’ resources are deallocated by OS
- Parent may terminate execution of children processes ( abort() )
- Child has exceeded allocated resources
- Task assigned to child is no longer required
- Parent is exiting
- OS does not allow child to continue if its parent terminates
- Cascading termination
'코딩이야기 > 운영체제' 카테고리의 다른 글
파일 시스템(File system) - 2. big picture (0) | 2016.04.11 |
---|---|
파일시스템(File system) - 1. files and directories (0) | 2016.04.11 |
04-3.Processes and Threads - Context Switching (0) | 2016.02.19 |
04-2.Processes and Threads - Process Scheduling (0) | 2016.02.16 |
04-1.Proceses and Threads - Process concepts (0) | 2016.02.15 |