免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1921 | 回复: 0
打印 上一主题 下一主题

kill函数的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-03-16 22:32 |只看该作者 |倒序浏览
我用管道实现了 find /|grep a 命令,想按下Ctrl+Z时将命令结束,所以把find和grep加到了一个进程组里。但是调用函数ctrl_z后,这两个命令是停止了但主程序不结束,这是为什么呢?请各位指点一下,谢谢。

  1. /*
  2. *         实现 find /|grep a,可以在按下 Ctrl+Z 时正常结束
  3. */
  4. #include <stdio.h>
  5. #include <unistd.h>
  6. #include <sys/wait.h>
  7. #include <signal.h>

  8. int fgpgid;//find 和 grep 要加入的进程组号
  9. void ctrl_z(int);

  10. int main()
  11. {
  12.         pid_t pids[10];
  13.         int pipes[10][2];
  14.         char *find[] = {"find", "/", NULL};
  15.         char *grep[] = {"grep", "a", NULL};

  16.         signal(SIGTSTP, ctrl_z);

  17.         if(pipe(pipes[0]) == -1){
  18.                 printf("pipe error\n");
  19.         }
  20.         if((pids[1] = fork()) == -1){
  21.                 perror("fork error");
  22.         } else if(pids[1] == 0){
  23.                 //输入
  24.                 close(pipes[0][1]);
  25.                 dup2(pipes[0][0], STDIN_FILENO);
  26.                 close(pipes[0][0]);

  27.                 //设置新的进程组,以脱离main所在的进程组
  28.                 if(setpgid(0, 0) == -1){
  29.                         printf("in child, %s setpgid error\n", grep[0]);
  30.                 }
  31.                 fgpgid = getpgid(0);
  32.                 if(execvp(grep[0], grep) == -1){
  33.                         printf("exec %s failed\n", grep[0]);
  34.                 }
  35.         } else if(pids[1] > 0){
  36.                 //设置新的进程组,以脱离main所在的进程组
  37.                 fgpgid = pids[1];
  38.                 if(setpgid(pids[1],pids[1]) == -1){
  39.                         printf("in parent, %s setpgid error\n", grep[0]);
  40.                 }
  41.                 //printf("comm: %s, pid: %d, pgid: %d\n", grep[0], pids[1], getpgid(pids[1]));
  42.         }

  43.         if((pids[0] = fork()) == -1){
  44.                 perror("fork error");
  45.         } else if(pids[0] == 0){
  46.                 //输出
  47.                 close(pipes[0][0]);
  48.                 dup2(pipes[0][1], STDOUT_FILENO);
  49.                 close(pipes[0][1]);

  50.                 //执行
  51.                 if(setpgid(0, fgpgid) == -1){
  52.                         printf("in child, %s setpgid error\n", find[0]);
  53.                 }
  54.                 if(execvp(find[0], find) == -1){
  55.                         printf("exec %s failed\n", find[0]);
  56.                 }
  57.         } else if(pids[0] > 0){
  58.                 if(setpgid(pids[0], fgpgid) == -1){
  59.                         printf("in parent, %s setpgid error\n", find[0]);
  60.                 }
  61.                 //printf("comm: %s, pid: %d, pgid: %d\n", find[0], pids[0], getpgid(pids[0]));
  62.                 waitpid(pids[0], NULL, 0);
  63.         }
  64.         close(pipes[0][0]);
  65.         close(pipes[0][1]);
  66.         waitpid(pids[1], NULL, 0);
  67.         return 0;
  68. }

  69. void ctrl_z(int signo)
  70. {
  71.         kill(-fgpgid, SIGSTOP);
  72.         printf("kill confirmed\n");
  73. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP