免费注册 查看新帖 |

Chinaunix

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

关于进程组的一个问题(源自APUE) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-02-24 13:06 |只看该作者 |倒序浏览

  1. #include <sys/types.h>
  2. #include <errno.h>
  3. #include <fcntl.h>
  4. #include <signal.h>
  5. #include <stdio.h>
  6. #include <unistd.h>
  7. //////////////////////////////////////
  8. static void sig_hup(int);
  9. static void pr_ids(char *);
  10. //////////////////////////////////////
  11. int main(void)
  12. {
  13.         char c;
  14.         pid_t pid;
  15.         ////////////////////////////////
  16.         pr_ids("parent");
  17.         ////////////////////////////////
  18.         if ( (pid = fork()) < 0 )
  19.                 perror("fork error");

  20.         else if ( pid > 0 ) {
  21.                 sleep(5);
  22.                 exit(0);

  23.         } else {
  24.                 pr_ids("child");
  25.                 signal(SIGHUP, sig_hup);
  26.                 kill(getpid(), SIGTSTP);
  27.                 pr_ids("child");
  28.                 if ( read(0, &c, 1) != 1 )
  29.                         printf("read error from control terminal, errno = %d\n", errno);
  30.                 exit(0);
  31.         }
  32. }
  33. ////////////////////////////////////////////////////////////////////////
  34. static void sig_hup(int signo)
  35. {
  36.         printf("SIGHUP recevid, pid = %d\n", getpid());
  37.         return;
  38. }
  39. ////////////////////////////////////////////////////////////////////////
  40. static void pr_ids(char *name)
  41. {
  42.         printf("%s: pid = %d, ppid = %d, pgrp = %d\n", name, getpid(), getppid(), getpgrp());
  43.         fflush(stdout);
  44. }
复制代码

上述程序是APUE第9章中的一个例子,该例子创建了一个孤儿进程组。编译后的输出为:

  1. [k3rn3l@localhost test]$ ./a.out
  2. parent: pid = 3235, ppid = 3069, pgrp = 3235
  3. child: pid = 3236, ppid = 3235, pgrp = 3235
  4. [k3rn3l@localhost test]$ SIGHUP recevid, pid = 3236
  5. child: pid = 3236, ppid = 1, pgrp = 3235
  6. read error from control terminal, errno = 5
复制代码

对于此,在APUE中的描述是:“注意,因为两个进程,登陆shell和子进程都写向终端,所以shell提示符和子进程的输出一起出现。”这里我不太明白,哪位DX能解释一下,小弟先谢过了。

另外,不知道下面这个if语句在这个程序中起到一个什么样的作用???

  1. if ( read(0, &c, 1) != 1 )
  2.                         printf("read error from control terminal, errno = %d\n", errno);
复制代码

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
2 [报告]
发表于 2006-02-24 13:29 |只看该作者
[k3rn3l@localhost test]$ SIGHUP recevid, pid = 3236
这句不就是 登陆的shell 和你的子进程都向终端写了吗? 解释你的红字.

read(0, &c, 1) != 1 这个是要向标准输出写一个字符.如果成功的话 read会返回1的 ,否则就是出错.
出错了以后执行这句printf("read error from control terminal, errno = %d\n", errno);

论坛徽章:
0
3 [报告]
发表于 2006-03-19 19:43 |只看该作者
if ( read(0, &c, 1) != 1 )
                        printf("read error from control terminal, errno = %d\n", errno);
是为了证明孤儿进程组失去了控制终端

论坛徽章:
0
4 [报告]
发表于 2006-03-20 09:31 |只看该作者
学到不少东西

论坛徽章:
0
5 [报告]
发表于 2006-03-20 13:06 |只看该作者
read(0, &c, 1) != 1 这个是要向标准输出写一个字符.如果成功的话 read会返回1的 ,否则就是出错.


read ?写?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP