父进程等待子进程终止 wait, WIFEXITED, WEXITSTATUS

wait()的函数原型是:
#include <sys/types.h>  
#include <sys/wait.h> 
pid_t wait(int *status)

进程一旦调用了wait,就立即阻塞自己,由wait自动分析是否当前进程的某个子进程已经退出。如果让它找到了这样一个已经变成僵尸的子进程,wait就会收集这个子进程的信息,并把它彻底销毁后返回;如果没有找到这样一个子进程,wait就会一直阻塞在这里,直到有一个出现为止。

参数status用来保存被收集进程退出时的一些状态,它是一个指向int类型的指针。但如果我们对这个子进程是如何死掉的毫不在意,只想把这个僵尸进程消灭掉,(事实上绝大多数情况下,我们都会这样想),我们就可以设定这个参数为NULL,就象下面这样:
pid = wait(NULL); 

如果成功,wait会返回被收集的子进程的进程ID,如果调用进程没有子进程,调用就会失败,此时wait返回-1,同时errno被置为ECHILD。

WIFEXITED(status) 这个宏用来指出子进程是否为正常退出的,如果是,它会返回一个非零值。

WEXITSTATUS(status) 当WIFEXITED返回非零值时,我们可以用这个宏来提取子进程的返回值,如果子进程调用exit(5)退出,WEXITSTATUS(status)就会返回5;如果子进程调用exit(7),WEXITSTATUS(status)就会返回7。请注意,如果进程不是正常退出的,也就是说,WIFEXITED返回0,这个值就毫无意义。

#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <sys/wait.h>
void f(){
printf("THIS MESSAGE WAS SENT BY PARENT PROCESS..\n");
}

main(){
int i,childid,status=1,c=1;
signal(SIGUSR1,f); //setup the signal value
i=fork(); //better if it was: while((i=fork)==-1);
if (i){
printf("Parent: This is the PARENT ID == %d\n",getpid());
sleep(3);
printf("Parent: Sending signal..\n");
kill(i,SIGUSR1); //send the signal

//status is the return code of the child process
wait(&status);
printf("Parent is over..status == %d\n",status);

//WIFEXITED return non-zero if child exited normally 
printf("Parent: WIFEXITED(status) == %d\n",WIFEXITED(status));

//WEXITSTATUS get the return code
printf("Parent: The return code WEXITSTATUS(status) == %d\n",WEXITSTATUS(status));
} else {
printf("Child: This is the CHILD ID == %d\n",getpid());
while(c<5){
sleep(1);
printf("CHLID TIMER: %d\n",c);
c++;
}
printf("Child is over..\n");
exit(2);
}
}

输出:
Child: This is the CHILD ID == 8158
Parent: This is the PARENT ID == 8157
CHLID TIMER: 1
CHLID TIMER: 2
Parent: Sending signal..
THIS MESSAGE WAS SENT BY PARENT PROCESS..
CHLID TIMER: 3
CHLID TIMER: 4
Child is over..
Parent is over..status == 512
Parent: WIFEXITED(status) == 1 //正常退出
Parent: The return code WEXITSTATUS(status) == 2 //拿到子进程返回值
### Fork 创建的父进程子进程的关系 Fork 调用用于创建新进程,该操作由现有进程发起并复制其当前状态来形成新的子进程。在 fork 执行之后,两个几乎完全相同的进程中有一个是原进程(父进程),另一个是由前者派生出来的副本(子进程)。父子两者的区别在于返回值以及它们各自的 PID 和 PPID (父进程 ID)[^1]。 对于父进程而言,在成功调用 `fork()` 后会获得一个正值作为子进程的 PID;而子进程则会在相同位置收到 0 的返回结果,并且拥有自己独立的新 PID 。这种机制允许程序区分哪个部分正在父进程中执行,哪些是在新生的子进程中运行[^3]。 #### 关键属性对比 | 属性 | 父进程 | 子进程 | | --- | ------ | -------| | 返回值 | 新建子进程的 PID | 总为 0 | | 自身 PID | 不变 | 新分配 | | 父进程 PID | 不变 | 继承自父进程 | ### 行为差异 - **资源共享**:尽管两者看起来相似,但实际上除了某些特定的数据结构外,比如文件描述符表项等会被二者共同持有之外,其他大部分内存映像都会被操作系统标记为写时拷贝(Copy-On-Write),这意味着只有当任意一方尝试修改数据时才会真正分离出单独的一份副本给改方使用[^2]。 - **终止通知**:一旦子进程结束生命周期,不会立即释放其所占用的所有系统资源直到对应的僵尸(zombie)条目被清理掉为止——这通常发生在父进程通过 waitwaitpid 类型的功能收集到有关已故子女的信息之时。如果子进程异常退出,则可以通过宏 WIFSIGNALED 来检测相应的信号编号[^4]。 ```c #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/wait.h> int main() { pid_t pid; printf("Before forking.\n"); pid = fork(); if (pid < 0){ fprintf(stderr,"Fork failed\n"); exit(1); }else if(pid == 0){ // Child process printf("This is child process, my PID=%d and parent's PID=%d\n",getpid(), getppid()); sleep(5); // Simulate work _exit(EXIT_SUCCESS); // Exit without calling cleanup handlers. } else{ // Parent process int status; wait(&status); // Wait until the child has finished. if(WIFEXITED(status)){ printf("Child exited normally with code %d\n", WEXITSTATUS(status)); } if(WIFSIGNALED(status)){ printf("Child terminated by signal %d\n", WTERMSIG(status)); } printf("Parent got message from child. My PID=%d\n", getpid()); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值