下面一段代码算是一个完整的例子了, 纪念一下.
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
pid_t pid;
/* Remove the defunc pid in process table */
signal(SIGCLD, SIG_IGN);
while(true)
{
switch(pid=fork())
{
case -1:
printf("Fork failured!\n");
break;
case 0:
printf("This is child thread!\n");
_exit(0);
break;
default:
printf("Child process %d created!\n", pid);
sleep(10);
break;
}
}
}
以前总是没有写signal一行.