免费注册 查看新帖 |

Chinaunix

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

[求助]按下Ctrl+Z调用自己的函数处理 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-03-20 21:17 |只看该作者 |倒序浏览
我想实现 find /|grep a,可以在按下 Ctrl+Z 时正常挂起它们两个,然后回到bash。但我写的程序有点儿问题,按下之后find和grep是都停住了,但回不到正常的bash了,另开一个bash用ps aux看,find和grep的进程状态都是T。请各位帮忙看一下原因,非常感谢。

  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. pid_t newpgid;//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.     printf("main, pid: %d, pgid: %d\n", getpid(), getpgid(0));
  17.     signal(SIGTSTP, ctrl_z);
  18.     if(pipe(pipes[0]) == -1){
  19.         printf("pipe error\n");
  20.     }
  21.     if((pids[1] = fork()) == -1){
  22.         perror("fork error");
  23.     } else if(pids[1] == 0){
  24.         //输入
  25.         close(pipes[0][1]);
  26.         dup2(pipes[0][0], STDIN_FILENO);
  27.         close(pipes[0][0]);

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

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

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

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

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP