study/linux/linux_c/progess/wait.c
2020-02-23 22:23:40 +08:00

41 lines
977 B
C

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
/*
* === FUNCTION ======================================================================
* Name: main
* Description:
* =====================================================================================
*/
int main (void)
{
int pid,status,exit_status;
if ((pid = fork()) < 0){
perror("fork failed!");
exit(1);
}
if (!pid){
sleep(4);
exit(6);
}
/* 父进程 */
if ((wait(&status)) < 0){
perror("wait failed");
exit(1);
}
/* 用来测试status的低八位是否为0 */
if (status & 0xFF){
printf("低八位不为0");
}else{
printf("status:%d\n",status);
/* 将status的高八位放置到exit_status当中 */
exit_status = status >> 8;
exit_status &= 0xFF;
/* 用来输出子进程的结束的状态 */
printf("exit status from %d is %d\n",pid,exit_status);
}
return EXIT_SUCCESS;
} /* ---------- end of function main ---------- */