Chinaunix

标题: APUE第九章示例代码的问题 [打印本页]

作者: 耗资喜欢猫    时间: 2012-12-22 17:40
标题: APUE第九章示例代码的问题
在APUE第九章的示例程序的解释中说到,“最后,要注意的是父进程终止时,子进程被置入后台进程组中,因为父进程是由shell作为前台作业执行的”。但我用debian7 run这个示例程序的时候,却并不像书上所说的那样,Ok,有图有真相
在上图中,我们看到,父进程结束时,子进程并没有被置入后台进程组中,而仍然是保留在前台进程组中,这是为什么?

为了节省大家的时间,我把书中源代码贴上来
  1. #include "apue.h"
  2. #include <errno.h>

  3. static void
  4. sig_hup(int signo)
  5. {
  6.         printf("SIGHUP received, pid = %d\n", getpid());
  7. }

  8. static void
  9. pr_ids(char *name)
  10. {
  11.         printf("%s: pid = %d, ppid = %d, pgrp = %d, tpgrp = %d\n",
  12.             name, getpid(), getppid(), getpgrp(), tcgetpgrp(STDIN_FILENO));
  13.         fflush(stdout);
  14. }

  15. int
  16. main(void)
  17. {
  18.         char        c;
  19.         pid_t        pid;

  20.         pr_ids("parent");
  21.         if ((pid = fork()) < 0) {
  22.                 err_sys("fork error");
  23.         } else if (pid > 0) {        /* parent */
  24.                 sleep(5);                /* sleep to let child stop itself */
  25.                 exit(0);                /* then parent exits */
  26.         } else {                        /* child */
  27.                 pr_ids("child");
  28.                 signal(SIGHUP, sig_hup);        /* establish signal handler */
  29.                 kill(getpid(), SIGTSTP);        /* stop ourself */
  30.                 pr_ids("child");        /* prints only if we're continued */
  31.                 if (read(STDIN_FILENO, &c, 1) != 1)
  32.                         printf("read error from controlling TTY, errno = %d\n",
  33.                             errno);
  34.                 exit(0);
  35.         }
  36. }
复制代码

作者: linux_c_py_php    时间: 2012-12-23 15:03
意思是说shell这个程序是那么做的, 不是说系统会把任何父进程和子进程这样处理.
作者: 耗资喜欢猫    时间: 2012-12-23 15:18
linux_c_py_php 发表于 2012-12-23 15:03
意思是说shell这个程序是那么做的, 不是说系统会把任何父进程和子进程这样处理.


这种再象,只能在debian上观察到,我在ubuntu上,观察不到这种现象,而在debian上才能观察到这种现象,又作何解释?
作者: 耗资喜欢猫    时间: 2012-12-24 20:40
难道用debian的坛友没遇到这个问题,我分别在debian6跟debian7上run过这个示例程序,结果跟1楼的图一样,但是用ubuntu则不会出现这种情况,难道这是debian的特色?




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2