2015년 6월 27일 토요일

fork 를 사용해서 자식 프로세스 만든 후, 부모 프로세스가 먼저 죽으면, 자식 프로세스의 PPID(부포 프로세스 ID) 는 1 이 되는 시험.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
 
 
void error_handling( char *msg);
 
 
void read_childproc(int sig)
{
    pid_t pid;
    int status;
 
    pid = waitpid( -1, &status, WUNTRACED | WCONTINUED | WNOHANG );
    //pid = waitpid( -1, &status,  WNOHANG );
 
    if( WIFEXITED( status ) )
    {
        printf("child process : %d exit, ret value : %d\n", pid, WEXITSTATUS( status ) );
    }
    else if( WIFSIGNALED(status) )
    {
        printf("child process : %d killed by signal %d\n", pid, WTERMSIG(status) );
    }
    else if( WIFSTOPPED(status) ) 
    {
        printf("child process : %d stopped by signal %d\n", pid, WSTOPSIG(status) );
    }
    else if( WIFCONTINUED(status) )
    {
        printf("child process : %d continued\n",pid );
    }
    else
    {
        printf("Unexpected Flow!\n");
        printf("Unexpected Flow!\n");
        printf("Unexpected Flow!\n");
    }
}
 
 
 
int main(int argc, char * argv[] )
{
 
    pid_t pid_1;
    pid_t pid_2;
 
    struct sigaction act;
    act.sa_handler = read_childproc;
    sigemptyset( &act.sa_mask );
    act.sa_flags = 0;
 
    sigaction( SIGCHLD,  &act, 0 );
 
 
    printf("parent : proces ID : %3d\n",  getpid() );
 
    pid_1 = fork();
 
    if( pid_1 < 0 )
    {
        error_handling("fork() error");
    }
    else if( pid_1 == 0// child
    {
        int j;
 
        printf("child 1:  proces ID : %3d\n",  getpid() );
 
        for(j=0; j<30; j++)
        {
            sleep(10);
        }
        printf("child 1:  finish    : %3d\n",  getpid() );
        return 10;
    }
    else // parent
    {
        printf("parent : child 1 process ID : %d\n", pid_1 );
 
        pid_2 = fork();
        if( pid_2 < 0 )
        {
            error_handling("fork() error");
        }
        else if( pid_2 == 0 ) // child
        {
            int k;
            printf("child 2: proces ID : %3d\n",  getpid() );
            for(k=0; k<30; k++)
            {
                sleep(10);
            }
            printf("child 2: finish    : %3d\n",  getpid() );
            return 20;
        }
        else //parent
        {
            int i;
            printf("parent : child 2 process ID : %d\n", pid_2 );
 
 
            for(i=0; i< 10; i++ )
            {
                printf("wait...(%d)\n", i + 1);
                sleep(5);
            }
        }
    }
    
    return 0;
}
 
void error_handling( char *msg)
{
    fputs( msg , stderr );
    fputc( '\n', stderr );
 
    fprintf(stderr, "error number : %d\n", errno );
    fprintf(stderr, "error Msg    : %s\n", strerror( errno ) );
 
    exit(1);
}
 
cs


이 코드를 실행하면, 자식 프로세스가 먼저 죽는다.
(for 문의 반복횟수 및 시간 설정에 따라 조절 가능 )

그러면, 자식 프로세스의 PPID 번호가 원래 PPID 와는 달리 1 이 되어 있는 것을 확인할 수 있다.


먼저 프로그램을 실행하고
또 다른 terminal 에서 ps -ef | grep <부모 프로세스 PID>
를 입력하면,
부모, 자식 프로세스를 다 확인할 수 있다.

그리고 부모 프로세스가 죽고 나서
ps -ef 를 이용해
자식 프로세스를 확인하면, PPID 가 1 이 되어 있는 것을 확인할 수 있다.

댓글 없음:

댓글 쓰기

팔로어