方法四:
#include <signal.h>
#include <sys/wait.h>
#include "../common/common.h"
void sig_chld(int signo);
int main(void)
{
/**捕获此信号, 此刻系统会立刻检测是否有次信号产生**/
if (signal(SIGCHLD, sig_chld) == SIG_ERR) {
handler_err("signal error to SIGCHLD");
}
pid_t pid;
int i;
for (i=0; i<10; i++) {
if ((pid = fork()) < 0) {
handler_err("fork error");
} else if (0 == pid) {
printf("child pid: %dn", getpid());
_exit(0);
}
sleep(1);
continue;
}
for (; ;) {
pause();
}
return EXIT_SUCCESS;
}
/**捕获到信号后会立刻执行此段代码***/
void sig_chld(int signo)
{
printf("receive child signaln");
if (waitpid(-1, NULL, 0) < 0) {
perror("waitpid error");
}
if (signal(SIGCHLD, sig_chld) == SIG_ERR) {
perror("signal error to SIGCHLD");
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易采站长站。








