- 论坛徽章:
- 0
|
有一个地方不解,程序如下(APUE 10.18),问题是,在(1)处已经阻塞的子进程结束信号的递送,
在(2)中,父进程还可以等待到子进程的结束信号吗?
谢谢!!
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <errno.h>
- #include <signal.h>
- #include <unistd.h>
- int system(const char *cmdstring)
- {
- pid_t pid;
- int status;
- struct sigaction ignore, saveintr, savequit;
- sigset_t chldmask, savemask;
-
- if (cmdstring == NULL)
- return (1); //always a command processor with unix
-
- ignore.sa_handler = SIG_IGN; //ignore SIGINT and SIGQUIT
- sigempty(&ignore.sa_mask);
- ignore.sa_flags = 0;
- if (sigaction(SIGINT, &ignore, &saveintr) < 0)
- return (-1);
- if (sigaction(SIGQUIT,&ignore, &savequit) < 0)
- return (-1);
-
- sigemptyset(&chldmask); //now block SIGCHLD
- sigaddset(&chldmask, SIGCHLD);
- //---------- (1) -----------------
- if (sigprocmask(SIG_BLOCK, &chldmask, &savemask) < 0)
- return (-1);
-
- if ( (pid = fork()) < 0)
- {
- status = -1; //probably out of processes
- }
- else if (pid == 0)
- {
- //restore previous signal actions & reset signal mask
- sigaction(SIGINT, &saveintr, NULL);
- sigaction(SIGQUIT, &savequit, NULL);
- sigprocmask(SIG_SETMASK, &savemask, NULL);
-
- execl("/bin/sh", "sh", "-c", cmdstring, (char*)0);
- _exit(127); //exec error
- }
- else
- {
- //---------- (2) -----------------
- while (waitpid(pid, &status, 0) < 0)
- if (errno != EINTR)
- {
- status = -1; //error other than EINTR form waitpid()
- break;
- }
- }
-
- if (sigaction(SIGINT, &saveintr, NULL) < 0)
- return (-1);
- if (sigaction(SIGQUIT, &savequit, NULL) < 0)
- return (-1);
- if (sigprocmask(SIG_SETMASK, &savemask, NULL) < 0)
- return (-1);
-
- return (status);
- }
-
复制代码
[ 本帖最后由 rwen2012 于 2006-5-27 11:50 编辑 ] |
|