在osx系统中有一个奇怪的现象,就是在子进程退出的时候waitpid会被中断一次,errno返回EINTR号,如果给SIGCHLD改成自己的实现就不会出现这种情况,不知道osx的darwin内核对SIGCHLD的默认处理做了什么。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <termios.h>
#include <errno.h>
int chld = 0;
void sig_chld(int signo) {
chld = 1;
}
int main(){
pid_t pid ,errnopid = 0;
int status = 0;
//signal(SIGCHLD, sig_chld);
if( 0 > ( pid = fork())){
printf("fork error\n");
}else if( 0 == pid) {
printf("child\n");
sleep(10);
exit(123);
}
while ((errnopid = waitpid(pid, &status, 0)) != pid) {
if( EINTR == errno)
printf("EINTR = %d , status = %d , chld %d\n",errnopid,status,chld);
}
printf("pid = %d , status %d ,errnopid %d \n",pid,status,errnopid);
return 0;
}
child
EINTR = -1 , status = 0 , chld 0
pid = 5854 , status 31488 ,errnopid 5854
child
EINTR = -1 , status = 0 , chld 0
pid = 5854 , status 31488 ,errnopid 5854
Output:childEINTR = -1 , status = 0 , chld 0pid = 5854 , status 31488 ,errnopid 5854
这里使用while的话可以处理这种情况的。