Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1759062
  • 博文数量: 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-17 20:35:01

  1. #include <sys/wait.h>
  2. #include <errno.h>
  3. #include <signal.h>
  4. #include <unistd.h>
  5. int
  6. system(char * cmdstring)
  7. {
  8.     pid_t pid;
  9.     int status;
  10.     struct sigaction ignore,saveintr,savequit;
  11.     sigset_t chldmask,savemask;

  12.     if (cmdstring == NULL )
  13.         return(1); /* always a command processor with unix */
  14.     ignore.sa_handler = SIG_IGN ; /* ignore sigint and sigquit */
  15.     sigemptyset(&ignore.sa_mask);
  16.     ignore.sa_flags = 0;
  17.     if (sigaction(SIGINT,&ignore,&saveintr) < 0)
  18.         return(-1);
  19.     if (sigaction(SIGQUIT,&ignore,&savequit) <0)
  20.         return(-1);
  21.     sigemptyset(&chldmask); /* now block sigchld */
  22.     sigaddset(&chldmask,SIGCHLD);
  23.     if (sigprocmask(SIG_BLOCK,&chldmask,&savemask) < 0)
  24.             status = -1;
  25.     if ((pid = fork()) < 0) {
  26.     status = -1 ; /* probably out of process */
  27.     } else if ( pid == 0) {
  28.     /* restore previous signal actions & reset ssignal mask */
  29.     sigaction(SIGINT,&saveintr,NULL);
  30.     sigaction(SIGQUIT,&savequit,NULL);
  31.     sigprocmask(SIG_SETMASK,&savemask,NULL);
  32.     execl("/bin/sh","sh","-c",cmdstring,(char *) 0);
  33.     _exit(-1); /* exec error*/
  34.     } else { /* parent */
  35.         while(waitpid(pid,&status,0) < 0) {
  36.             if (errno != EINTR)
  37.                 status = -1; /* error other that EINTR form waitpid() */
  38.         break;
  39.         }
  40.     }
  41.     /* restore previous signal action &reset signal mask */
  42.     if (sigaction(SIGINT,&saveintr,NULL) < 0)
  43.         return(-1);
  44.     if (sigaction(SIGQUIT,&savequit,NULL) < 0)
  45.         return(-1);
  46.     if (sigprocmask(SIG_SETMASK,&savemask,NULL) < 0)
  47.         return(-1);
  48.     return(status);
  49. }

  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. }
system
  1. ./c10-19-2

  2. .

  3. ?

  4. a

  5. hello,world

  6. .

  7. 1,$p

  8. hello,world

  9. w temp.too

  10. 12

  11. ^C

  12. ?

  13. ^\caught sigchld

  1. ubuntu@ubuntu-virtual-machine:~/Desktop/apue$ ./c10-19

  2. a

  3. hello,world

  4. .

  5. 1,$p

  6. hello,world

  7. w tremp.foo

  8. 12

  9. ^Ccaught sigint



  10. ?

  11. ^Ccaught sigint



  12. ?

  13. ^Ccaught sigint



  14. ?

  15. ^Ccaught sigint



  16. ?

  17. q

  18. caught sigchld

  19. ubuntu@ubuntu-virtual-machine:~/Desktop/apue$ ps

  20. PID TTY TIME CMD

  21. 2235 pts/0 00:00:00 bash

  22. 2293 pts/0 00:00:00 ps

  23. c10-19-2

  24. a
  25. hello,world

  26. .

  27. 1,$p

  28. hello,world

  29. w temp.foo

  30. 12

  31. ^C

  32. ?

  33. ^C

  34. ?

  35. ^C

  36. ?

  37. q

  38. caught sigchld
















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