본문 바로가기
코딩이야기/운영체제

04-4.Processes and Threads - Process creation and Termination

by GiraffeB 2016. 2. 19.
Processses and Threads

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

    1. Load code and data into memory
    2. Create (empty) call stack
    3. Create and initialize a process control block
    4. Put process on ready list

Process Creation (2)
  • Cloning : UNIX fork() system call

    1. Stop current process and save its state.
    2. Make a copy of cde, data, stack and PCB => PID는 다르게 저장됨.
    3. 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
    Alt text
wait()에서 parent process는
child process가 끝날때까지 대기
올바르게 종료 state 값을 반환.
Zombie state
Child process가 exit()을 마치고,
parent process가 자신의 Exit status를 읽어가기를 기다리는 Process 상태.

Process Creation (4)
  • In UNIX
    Alt text

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