fork함수 이용해서 C코드로
부모 자식 손자 프로세스 생성 하려고 하거든요.

유닉스에서 실행 시키고 ps -f 명령어로
작동 중인 프로세스 보면

부모랑 손자는 나오는데 자식 프로세스가 안나와요.
왜 이런건지 좀 알려주세요.

그런데 희안한건 손자 프로세스를 kill명령어로 죽이고 나면
자식 프로세스가 ps 명령어에 뜨는데.. 참... 아직도
갈피를 못 잡고 있네요.

 

#inlcude <sys/types.h>

#include <unistd.h>

#include <sys/wait.h>

int main(void)

{

        int     pid, status, pid2;

        if ((pid = fork()) < 0)

                printf("Fork error\n");

        else if (pid > 0) {

                /* parent process */

                printf("PARENT: Child pid = %d\n", pid);

                if (waitpid(pid, &status, 0) < 0)

                        printf("waitpid error\n");

                printf("PARENT: Child process exited (parent is still running)\n");

                do {} while (1);

        } else if (pid == 0) {

                if((pid2 = fork()) < 0)

                        printf("Fork error\n");

                else if (pid2 > 0) {

                        /* child process */

                        printf("CHILD: Grand Child pid = %d\n", pid2);

                        if (waitpid(pid2, &status, 0) < 0)

                                printf("waitpid error\n");

                        printf("CHILD: Grand Child process exited (child is still running)\n");

                        do {} while (1);

                } else if (pid2 == 0) {

                        /* grand child */

                        printf("GRAND CHILD: Grand Child process is running.\n");

                        do {} while (1);

                }

        }

}