waitpid

#include
#include
#include

#include
#include
#include

int main(int argc,char *argv[])
{
pid_t pid = 0;

pid = fork();
if(pid < 0)
{
	printf("fork failed\n");
	return 1;
}
if(pid == 0)
{
	sleep(5);
	printf("child  :%d",getpid());
	printf(" \t %d",getppid());
}
else
{    //pid_t waitpid(pid_t pid,int * status,int options);
     //子进程的结束状态值会由参数 status 返回,而子进程的进程识别码也会一起返回
	while(pid != waitpid(pid,NULL,WNOHANG))//会暂时停止目前进程的执行,直到有信号来到或子进程结束。
	//WNOHANG 若pid指定的子进程没有结束,则waitpid()函数返回0,不予以等待。若结束,则返回该子进程的ID
	{
		sleep(1);
	}
	printf("%d process had exited\n",pid);
}
return 0;

}

你可能感兴趣的:(waitpid)