- 论坛徽章:
- 0
|
1 #include<sys/types.h>
2 #include<sys/wait.h>
3 #include<stdio.h>
4 #include<stdlib.h>
5 #include<signal.h>
6
7 int main()
8 {
9 pid_t pid;
10 int ret;
11 int status;
12 int ppid;
13 if((pid=fork())<0)
14 {
15 perror("进程创建失败!\n");
16 exit(0);
17 }
18 if(pid==0) /* 在子进程中 */
19 {
20 printf("子程序开始结束自身...\n");
21 raise(SIGSTOP); /*给自己发信号 */
22 printf("子程序已经结束自身!\n");
23 exit(0);
24 }
25 else /* 在父进程中 */
26 {
27 printf("PID=%d\n",pid);
28 if((ppid=waitpid(pid,&status,WNOHANG)) == 0)
29 {
30 if((ret=kill(pid,SIGKILL)) == 0)
31 printf("Kill PID=%d Status=%d ppid=%d\n",pid,status,ppid);
32 }
33 else
34 {
35 perror("Kill Error!\n");
36 }
37 }
38 return 0;
39 }
程序的运行结果为:
子程序开始结束自身...
PID=28161
Kill PID=28161 Status=-1078681508 ppid=0
请问程序的第28行waitpid(...)的返回值为什么为0,而不等于pid的值?根据库函数文档说,应返回pid的值呀? |
|