Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1752300
  • 博文数量: 335
  • 博客积分: 4690
  • 博客等级: 上校
  • 技术积分: 4341
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-08 21:38
个人简介

无聊之人--除了技术,还是技术,你懂得

文章分类

全部博文(335)

文章存档

2016年(29)

2015年(18)

2014年(7)

2013年(86)

2012年(90)

2011年(105)

分类: C/C++

2012-02-15 22:32:24


  1. #include "apue.h"
  2. static void
  3. sig_int(int signo)
  4. {
  5.     printf("caught sigint\n");
  6. }
  7. static void
  8. sig_chld(int signo)
  9. {
  10.     printf("caught sigchld\n");
  11. }
  12. int main(void)
  13. {
  14.     if (signal(SIGINT,sig_int) == SIG_ERR)
  15.         err_sys("sig_int error");
  16.     if (signal(SIGCHLD,sig_chld) == SIG_ERR)
  17.         err_sys("sig_chld error");
  18.     if (system("/bin/ed") < 0)
  19.         err_sys("system error");
  20.     exit(0);
  21. }

  1. #include <sys/wait.h>
  2. #include <errno.h>
  3. #include <unistd.h>

  4. int system(const char * cmdstring) /*version without signal handling */
  5. {
  6.     pid_t pid;
  7.     int status;

  8.     if (cmdstring == NULL)
  9.         return(1); /* always a command preocessor with unix */
  10.     if ( (pid = fork()) < 0) {
  11.         status = -1; /* probably out of processor */
  12.     } else if (pid == 0) { /* child porcess */
  13.         execl("/bin/sh","sh","-c",cmdstring,(char *) 0 );
  14.         _exit(127); /* execl error */
  15.     } else {
  16.         while ( waitpid(pid,&status,0) < 0) {
  17.             if (errno == EINTR) {
  18.                 status = -1; /* error other than sintr from waitpid */
  19.                 break;
  20.             }
  21.         }
  22.     }
  23.     return(status);
  24. }
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


















阅读(879) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~