- 论坛徽章:
- 0
|
我用下面这个程序来测这个, 但是得到的结果是子进程每一次都在父进程
停止以后才停止
判断子进程是否消亡用的是上面楼主的方法, 不知道管用不管用
#include <unistd.h>;
#include <sys/types.h>;
#include <sys/uio.h>;
#include <stdio.h>;
#include <strings.h>;
#include <errno.h>;
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! |
|