pid_t waitpid(pid_t pid, int *stat_loc, int options)
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
pid_t pc, pr;
pc = fork();
if(pc<0)
printf("Error fork.\n");
else if(pc==0){
sleep(5); /* 子进程暂停5s */
exit(0); /* 子进程正常退出 */
}
else { /* 父进程 */
do{ /*循环测试子进程是否退出*/
pr = waitpid(pc, NULL, WNOHANG);
if(pr==0) {
printf("The child process has not exited\n");
sleep(1);/*若子进程未退出则父进程暂停1s*/
}
}while(pr==0);
if(pr==pc)
printf("Get child %d\n", pr);
else
printf("some error occured.\n");
}
return 0;
}
beyes@linux-beyes:~/C/call/wait> ./waitpid.exe
The child process has not exited
The child process has not exited
The child process has not exited
The child process has not exited
The child process has not exited
Get child 13116
欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) | Powered by Discuz! X3.2 |