apue遇到麻烦 对SIGLD语义不太明白 请解答 谢谢
发表于 : 2007-04-26 17:02
小弟正在看10.7节,十分的郁闷,对“SIGCLD的配置设置为捕捉,内核立即检测是否有子进程准备好被等待”一直无法理解
书中的程序是这样的:
#include "apue.h"
#include <sys/wait.h>
static void sig_cld(int);
int
main()
{
pid_t pid;
if (signal(SIGCLD, sig_cld) == SIG_ERR)
perror("signal error");
if ((pid = fork()) < 0) {
perror("fork error");
} else if (pid == 0) { /* child */
sleep(2);
_exit(0);
}
pause(); /* parent */
exit(0);
}
static void
sig_cld(int signo) /* interrupts pause() */
{
pid_t pid;
int status;
printf("SIGCLD received\n");
if (signal(SIGCLD, sig_cld) == SIG_ERR) /* reestablish handler */
perror("signal error");
if ((pid = wait(&status)) < 0) /* fetch child status */
perror("wait error");
printf("pid = %d\n", pid);
}
作者说在SVR2下编译运行此程序则其输出是一行行地不断重复SIGCLD received .最后进程用完其栈并异常终止。为什么会不断重复SIGCLD received呢?我在我的机子上运行输出的却只有SIGCLD received pid = 6840这一行并没有重复,为什么会出现差别呢?
谢谢!1
书中的程序是这样的:
#include "apue.h"
#include <sys/wait.h>
static void sig_cld(int);
int
main()
{
pid_t pid;
if (signal(SIGCLD, sig_cld) == SIG_ERR)
perror("signal error");
if ((pid = fork()) < 0) {
perror("fork error");
} else if (pid == 0) { /* child */
sleep(2);
_exit(0);
}
pause(); /* parent */
exit(0);
}
static void
sig_cld(int signo) /* interrupts pause() */
{
pid_t pid;
int status;
printf("SIGCLD received\n");
if (signal(SIGCLD, sig_cld) == SIG_ERR) /* reestablish handler */
perror("signal error");
if ((pid = wait(&status)) < 0) /* fetch child status */
perror("wait error");
printf("pid = %d\n", pid);
}
作者说在SVR2下编译运行此程序则其输出是一行行地不断重复SIGCLD received .最后进程用完其栈并异常终止。为什么会不断重复SIGCLD received呢?我在我的机子上运行输出的却只有SIGCLD received pid = 6840这一行并没有重复,为什么会出现差别呢?
谢谢!1