main ()
{
pid_t pid;
int fd[2];
int rpid;
char buf[10];
int i;
if(pipe(fd) == -1)
perror("Failed to create pipe!\n"
pid=fork();
if (pid < 0)
printf("error in fork!"
else if (pid == 0) {
rpid = getpid();
printf("i am the child process, my process id is %d\n", rpid);
sprintf(buf, "%d", rpid);
write(fd[1], buf, sizeof(buf));
}
else {
printf("i am the parent process, my process id is %d\n",getpid());
read(fd[0], buf, sizeof(buf));
rpid=atoi(buf);
printf("I accept the process id of my child, which is %d\n", rpid);
for(i=0; i<10000; i++);
if(kill(rpid, 0) == -1 && errno == ESRCH)
printf("Child process already dead!\n"
else
printf("Child process still alive!\n"
}
}
顺便附一下程序结果
>; cc -o fp forkpipe.c
>; ./fp
i am the parent process, my process id is 88179
i am the child process, my process id is 88180
I accept the process id of my child, which is 88180
Child process still alive!作者: 依赛特小子 时间: 2005-10-09 11:51 标题: 问一下子进程是否一定要等到父进程停止以后才停止? 子进程想停止就停止,就像人可以自杀,但不可以自己生自己,子进程的创建要有父进程才可以!!作者: xiaonanln 时间: 2005-10-09 12:01 标题: 问一下子进程是否一定要等到父进程停止以后才停止? 子进程Exit后,父进程还没有wait过,所以应该是Zombie,而不是not exists作者: gooderfeng 时间: 2005-10-09 12:20 标题: 问一下子进程是否一定要等到父进程停止以后才停止? 僵死进程还占用原来的pid,你kill僵死进程的时候,还是存在的,在父进程里面waitpid之后才能把进程的所有资源全部回收作者: openX 时间: 2005-10-09 12:43 标题: 问一下子进程是否一定要等到父进程停止以后才停止? [quote]原帖由 "gooderfeng"]僵死进程还占用原来的pid,你kill僵死进程的时候,还是存在的,在父进程里面waitpid之后才能把进程的所有资源全部回收[/quote 发表: