- #include "apue.h"
-
static void
-
sig_int(int signo)
-
{
-
printf("caught sigint\n");
-
}
-
static void
-
sig_chld(int signo)
-
{
-
printf("caught sigchld\n");
-
}
-
int main(void)
-
{
-
if (signal(SIGINT,sig_int) == SIG_ERR)
-
err_sys("sig_int error");
-
if (signal(SIGCHLD,sig_chld) == SIG_ERR)
-
err_sys("sig_chld error");
-
if (system("/bin/ed") < 0)
-
err_sys("system error");
-
exit(0);
-
}
- #include <sys/wait.h>
-
#include <errno.h>
-
#include <unistd.h>
-
-
int system(const char * cmdstring) /*version without signal handling */
-
{
-
pid_t pid;
-
int status;
-
-
if (cmdstring == NULL)
-
return(1); /* always a command preocessor with unix */
-
if ( (pid = fork()) < 0) {
-
status = -1; /* probably out of processor */
-
} else if (pid == 0) { /* child porcess */
-
execl("/bin/sh","sh","-c",cmdstring,(char *) 0 );
-
_exit(127); /* execl error */
-
} else {
-
while ( waitpid(pid,&status,0) < 0) {
-
if (errno == EINTR) {
-
status = -1; /* error other than sintr from waitpid */
-
break;
-
}
-
}
-
}
-
return(status);
-
}
output of the program :
gcc -Wall -o c10-19 c10-19.c signal
l.o error.c system.c
ubuntu@ubuntu-virtual-machine:~/Desktop/apue$ ./c10-19
a
hello. ,world
.
1,$p
hello,world
w temp.too
12
q
caught sigchld
ubuntu@ubuntu-virtual-machine:~/Desktop/apue$ ./c10-19
a
hello,wol rld
1,4p 4 $p
.
1,$p
hello,world
1,$p
w temp.to foo
17
^Ccaught sigint
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx?
^Ccaught sigint
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx?
q
caught sigchld
system系统调用没有处理SIGCHLD,SIGINT,导致父子进程均受到该信号,而产生上面的输出,唯一和原文不一致的地方在于用xXXX表的行,一个空白行,书上没有,不知道什么原因,估计是shell产生的输入,shell本身是忽略这两个信号的,不知道这个猜测对不对?
进程如图所示:
fork fork fork
sehll--------->c10-20----------->shell -------------> ed
ps -o pid,ppid,commd
PID PPID COMMAND
2610 2437 bash
3267 2610 c10-20
3268 3267 sh
3269 3268 ed
3273 2610 ps
阅读(969) | 评论(0) | 转发(0) |