본문 바로가기
Study/운영체제

3. Processes - Operations on processes

by 이미뇨 2023. 4. 11.
  • Process create
  • Process termination
  • Process communication

 

1.Process Creation

Create-process system call :

  • 프로세스를 만들고 pid(process ID)를 할당
  • fork system call
  • kernel이 생성된다.

 

Process tree : 프로세스 간의 Parent-child 관계

Example of Process Tree

 

Displaying Process Information

  • UNIX : ps [–el] // 전체를 다 보고 싶으면 
  • Windows : Task manager (windows system program)

 

프로세스 생성을 위한 몇 가지 옵션

Resource Child OS에서 직접 자신의 리소스를 요청 or 부모 프로세스와 자식 프로세스 간에 부모 프로세스에서 사용하던 자원 중 일부가 공유된다
Execution 동시실행 or Parent 가 child가 종료될때까지 기다림
Address space 프로그램 코드 및 데이터 공유 or 자식 프로세스가 새로운 프로그램을 로드

 

Process Creation in UNIX

  • fork(): 현재 실행 중인 프로세스를 복제하여 새로운 자식 프로세스를 만듬
  • exec() family : 새로운 프로그램을 실행할 때 사용 (execl(), execv(), execlp(), execvp(), execle(), execve())
  • wait() : 자식 프로세스가 종료될 때까지 부모 프로세스를 대기시키는 함수
  • (which ls : 현재 사용자의 PATH 환경 변수에서 "ls" 명령의 위치를 찾아주는 명령어)

 

Example of Process Creation

  • fork() 함수의 return value(반환값)이 0 => Child process
  • fork() 함수의 return value 0이 아니라면 => 이 값은 Child process PID(Process ID)값

int main()
{
pid_t child_pid = fork(); // create a process
// in general, pid_t is defined as int
if(child_pid < 0){ // error occurred
fprintf(stderr, “fork failed\n”);
exit(-1);
} else if(child_pid == 0){ // child process
execlp(“/bin/ls”, “ls”, NULL);
} else { // if pid != 0, parent process
wait(NULL); // waits for child process to complete
printf(“Child Completed\n”);
exit(0);
}
return 0;
}
//Parent process
int main()
{
pid_t child_pid = fork();
if(child_pid < 0){
fprintf(stderr, “fork failed\n”);
exit(-1);
} else if(child_pid == 0){ // Parent에서는 false
execlp(“/bin/ls”, “ls”, NULL);
} else {
wait(NULL); // child를 기다리기 위해, 먼저 끝내지 않기 위해
printf(“Child Completed\n”)
exit(0);
}
}

//Child process
int main()
{
pid_t child_pid = fork();
if(child_pid < 0){ // return value = 0
fprintf(stderr, “fork failed\n”);
exit(-1);
} else if(child_pid == 0){ // child에서는 true
execlp(“/bin/ls”, “ls”, NULL);
} else {
wait(NULL);
printf(“Child Completed\n”)
exit(0);
}
}

 

More About fork()

Resource of child process

  • Data (variables) : parent process의 변수 복사
    • Child process는 자체 주소 공간을 가진다.
    • 유일한 차이점은 fork()에서 반환된 child pid이다.
  • Files
    • Opened before fork() : parent와 file 공유
    • Opened after fork() : 공유되지않음

 

More About exec Family

Functions in exec family (declared in <unistd.h>)

int execl(const char *path, const char *arg0, ..., const char *argn, char * /*NULL*/);
int execv(const char *path, char *const argv[]);
int execlp(const char *file, const char *arg0, ..., const char *argn, char * /*NULL*/);
int execvp(const char *file, char *const argv[]);
execle(), execve()
  • execl(): 새로운 프로그램의 파일 이름과 인자를 직접 지정해서 전달 (list)
  • execv(): 새로운 프로그램의 파일 이름과 인자를 배열로 전달 (vector)
  • execlp(): 새로운 프로그램의 파일 이름을 PATH 환경 변수에서 찾아서 인자를 직접 지정해서 전달 (path)

전달하는 인자의 방식과 파일 이름 찾기 방법이 다르다.

 

More About wait()

pid_t wait(int *stat_loc);

 

  • stat_loc : an integer pointer
    • 자식 프로세스가 정상적으로 종료되었다면 stat_loc에 저장되는 값은 0
    • 만약 stat_locNULL이면, parent process는 child process의 종료 상태 정보를 받지 않는다
    • blocking wait: parent process는 child process가 종료될 때까지 블록(실행일시중지상태)된다
  • Return value of wait
    • 종료된 자식의 pid
    • -1은 child process가 없음을 의미
  • parent process가 child process의 종료 상태를 확인하기 위해 wait() 함수를 호출하는 경우
    • 자식 프로세스는 "좀비 프로세스"
    • wait() 함수를 이용해 child process의 종료 상태를 확인
    • kill() 함수를 사용해 좀비 프로세스를 완전히 종료시켜야 한다.

 

Process Creation in win32

  • CreateProcess()
    • UNIX의 fork()와 비슷하지만 하위 프로세스의 속성을 지정하기 위한 매개 변수가 훨씬 더 많다.
  • WaitForSingleObject()
    • UNIX의 wait()와 유사함
  • void ZeroMemory(PVOID Destination, SIZE_T Length );
    • 메모리 블록을 0으로 채운다.

2.Process Termination

Normal termination

  • exit(int return_code) : child process에 의해 호출됨
    • 정리 작업 : Deallocate memory, Close files ...
  • return_code가 parent process로 전달
    • 일반적으로 0은 성공을 의미
    • parent는 return code 읽을수 있음
      • wait(&status); // 자식이 종료될 때까지 기다림
        ret = WEXITSTATUS(status); // return_code from the child