2012. 7. 13. 16:17

wait()의 경우 자식 프로세스중 하나라도 종료되면 부모 프로세스는 깨어 나지만

waitpid()의 경우 특정 프로세스가 종료되기를 기다리도록 지정할 수 있다.

즉 wait()는 자식 프로세스가 종료될 때까지는 부모 프로세스는 아무 일도 못하고 블록화 되자만

waitpid()는 자식 프로세스가 종료되자 않더라도 부모 프로세스가 블록화 되지 않고 다른 일을 할 수 있도록

지정할 수 있다.

wait에 관한 예제 프로그램

#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

main()
{
    int pid, child_pid, status;

    /* 첫 번째 자식 프로세스 생성 */
    if ((pid=fork()) == -1)
       perror("fork failed");
    /* 부모 프로세스 */
    else if (pid != 0) {
       /* 두 번째 자식 프로세스 생성 */
       if ((pid=fork()) == -1)
          perror("fork failed");
       /* 부모 프로세스 */
       else if (pid != 0) {
          /* 아무 자식 프로세스나 종료되기를 기다림.
            여러 자식 프로세스가 있다면 아무 프로세스나 종료되기를 기다림 */
          child_pid = wait(&status);
          printf("child[pid:%d] terminated with code %x\n", child_pid, status);
       /* 두 번째 자식 프로세스 */
       } else {
          printf("run child2[pid:%u]\n", getpid());
          exit(3);
       }
    /* 첫 번째 자식 프로세스 */
    } else {
       printf("run child1[pid:%u]\n", getpid());
       exit(2);
    }
}

--------------------------------------------------------------------------------------------------

waitpid()에 관한 프로그램

#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

main()
{
    int pid1, pid2, child_pid, status;

    /* 첫 번째 자식 프로세스 생성 */
    if ((pid1=fork()) == -1)
       perror("fork failed");
    /* 부모 프로세스 */
    else if (pid1 != 0) {
       /* 두 번째 자식 프로세스 생성 */
       if ((pid2=fork()) == -1)
          perror("fork failed");
       /* 부모 프로세스 */
       else if (pid2 != 0) {
          /* pid1 프로세스가 종료되기를 기다림 */
          child_pid = waitpid(pid1, &status, 0);
          printf("child[pid:%d] terminated with code %x\n", child_pid, status);
       /* 두 번째 자식 프로세스 */
       } else {
          printf("run child2[pid:%u]\n", getpid());
          exit(3);
       }
    /* 첫 번째 자식 프로세스 */
    } else {
       printf("run child1[pid:%u]\n", getpid());
       exit(2);
    }
}

---------------------------------------------------------------------------------------------------
waitpid()의 세번째 인수에 0을 설정하여 호출함으로써 자식 프로세스가 종료될 때 까지 부모 프로세스는

다른 일을 실행하지 못하고 블록화되는데 wait도 이와 같다.

#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

main()
{
    int pid, status;

    if ((pid=fork()) == -1)
       perror("fork failed");
    /* 부모 프로세스 */
    else if (pid != 0) {
       /* waitpid의 세 번째 인수를 0으로 설정하면 부모 프로세스는 자식 프로세스가
         종료될 때까지 블록화. 자식 프로세스가 종료되면 자식 프로세스의 프로세스
         ID가 반환되므로 while 문을 벗어남 */
       while (waitpid(pid, &status, 0)==0) {
          printf("still waiting\n");
          sleep(1);  /* 1초 동안 실행을 중단 */
       }
    /* 자식 프로세스 */
    } else {
       printf("run child\n");
       sleep(5);
       exit(0);
    }
}

[localhost@localhost]#a.out
run child

--------------------------------------------------------------------------------------------------
waitpid()의 세번째 인수에 WHOHANG을 설정하여 호출함으로써 부모프로세스는 블록화 되지 않고 다른일을 수행

#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

main()
{
    int pid, status;

    if ((pid=fork()) == -1)
       perror("fork failed");
    /* 부모 프로세스 */
    else if (pid != 0) {
       /* 세 번째 인수를 WNOHANG으로 설정하여 호출하면 부모 프로세스는
         자식 프로세스가 종료되지 않더라도 블록화 되지 않고 다른 일을 실행.
         자식 프로세스가 종료되지 않은 상태에서 waitpid가 반환하는 값은 0 */
      while (waitpid(pid, &status, WNOHANG)==0) {
          printf("still waiting\n");
          sleep(1);
       }
    /* 자식 프로세스 */
    } else {
       printf("run child\n");
       sleep(5);
       exit(0);
    }
}

[localhost@localhost]#a.out

stall waiting
run child
stall waiting
stall waiting
stall waiting

Posted by 몰라욧