Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2102775
  • 博文数量: 374
  • 博客积分: 7276
  • 博客等级: 少将
  • 技术积分: 5668
  • 用 户 组: 普通用户
  • 注册时间: 2011-10-06 16:35
文章分类

全部博文(374)

文章存档

2013年(23)

2012年(153)

2011年(198)

分类: LINUX

2012-04-19 09:56:15

#include
 

#include
 #include
 #include
 int main()
 {
     pid_t pid = 0;
 
     if((pid = fork()) < 0)
     {
         printf("frok erro and exit\n");
         exit(0);
     }
 
     if(0==pid)
     {
         #ifdef SETGROUP
         setpgrp();
         #endif
         while(1)
         {
             printf("In child, pid is %d, pgid is %d\n", getpid(), getpgrp());
             sleep(2);
         }
     }else
     {
         while(1)
         {
             printf("In parent, pid is %d, pgid is %d\n", getpid(), getpgrp());
             sleep(2);
         }
     }
     return 0;
 }

在没有定义SETGROUP时,输出如下:

./a.out
In child, pid is 24010, pgid is 24009
In parent, pid is 24009, pgid is 24009
In parent, pid is 24009, pgid is 24009
In child, pid is 24010, pgid is 24009
In parent, pid is 24009, pgid is 24009
In child, pid is 24010, pgid is 24009
In parent, pid is 24009, pgid is 24009
In child, pid is 24010, pgid is 24009

Ctrl+c之后父子进程都退出

在定义了SETGROUP时,输出如下:

./a.out
In child, pid is 24562, pgid is 24562
In parent, pid is 24561, pgid is 24561
In parent, pid is 24561, pgid is 24561
In child, pid is 24562, pgid is 24562
In parent, pid is 24561, pgid is 24561
In child, pid is 24562, pgid is 24562
In parent, pid is 24561, pgid is 24561
In child, pid is 24562, pgid is 24562

Ctrl+c之后父进程退出,子进程仍然运行

 

关于setpgrp(),该函数的调用者会把自己加入一个新的process group,并且process group leader就是自己,即process group ID就是自己的进程号。

 

这个例子可以确定的是:调用了setpgrp()后就不会收到Ctrl+c信号了,但是究竟是调用了setpgrp()后脱离了控制终端 还是 该进程成为了后台进程 导致的,我还不是很清楚。

网上有人说是:脱离了控制终端,但是两种情况下执行lsof可以看到如下输出:

lsof -p 24710
lsof: WARNING: can't stat() ext3 file system /dev/.static/dev
      Output information may be incomplete.
COMMAND   PID USER   FD   TYPE DEVICE    SIZE     NODE NAME
a.out   24710 jsun  cwd    DIR    8,9    4096 10403851 /home/jsun
a.out   24710 jsun  rtd    DIR    8,1    4096        2 /
a.out   24710 jsun  txt    REG    8,9    7811 10404039 /home/jsun/a.out
a.out   24710 jsun  mem    REG    8,1 1241392  7242668 /lib/tls/i686/cmov/libc-2.3.6.so
a.out   24710 jsun  mem    REG    0,0                0 [vdso] (stat: No such file or directory)
a.out   24710 jsun  mem    REG    8,1   88164  7225346 /lib/ld-2.3.6.so
a.out   24710 jsun    0u   CHR  136,2                4 /dev/pts/2
a.out   24710 jsun    1u   CHR  136,2                4 /dev/pts/2
a.out   24710 jsun    2u   CHR  136,2                4 /dev/pts/2



lsof -p 24711
lsof: WARNING: can't stat() ext3 file system /dev/.static/dev
      Output information may be incomplete.
COMMAND   PID USER   FD   TYPE DEVICE    SIZE     NODE NAME
a.out   24711 jsun  cwd    DIR    8,9    4096 10403851 /home/jsun
a.out   24711 jsun  rtd    DIR    8,1    4096        2 /
a.out   24711 jsun  txt    REG    8,9    7811 10404039 /home/jsun/a.out
a.out   24711 jsun  mem    REG    8,1 1241392  7242668 /lib/tls/i686/cmov/libc-2.3.6.so
a.out   24711 jsun  mem    REG    0,0                0 [vdso] (stat: No such file or directory)
a.out   24711 jsun  mem    REG    8,1   88164  7225346 /lib/ld-2.3.6.so
a.out   24711 jsun    0u   CHR  136,2                4 /dev/pts/2
a.out   24711 jsun    1u   CHR  136,2                4 /dev/pts/2
a.out   24711 jsun    2u   CHR  136,2                4 /dev/pts/2
lsof -p 24746
lsof: WARNING: can't stat() ext3 file system /dev/.static/dev
      Output information may be incomplete.
COMMAND   PID USER   FD   TYPE DEVICE    SIZE     NODE NAME
a.out   24746 jsun  cwd    DIR    8,9    4096 10403851 /home/jsun
a.out   24746 jsun  rtd    DIR    8,1    4096        2 /
a.out   24746 jsun  txt    REG    8,9    7894 10404039 /home/jsun/a.out
a.out   24746 jsun  mem    REG    8,1 1241392  7242668 /lib/tls/i686/cmov/libc-2.3.6.so
a.out   24746 jsun  mem    REG    0,0                0 [vdso] (stat: No such file or directory)
a.out   24746 jsun  mem    REG    8,1   88164  7225346 /lib/ld-2.3.6.so
a.out   24746 jsun    0u   CHR  136,2                4 /dev/pts/2
a.out   24746 jsun    1u   CHR  136,2                4 /dev/pts/2
a.out   24746 jsun    2u   CHR  136,2                4 /dev/pts/2
lsof -p 24747
lsof: WARNING: can't stat() ext3 file system /dev/.static/dev
      Output information may be incomplete.
COMMAND   PID USER   FD   TYPE DEVICE    SIZE     NODE NAME
a.out   24747 jsun  cwd    DIR    8,9    4096 10403851 /home/jsun
a.out   24747 jsun  rtd    DIR    8,1    4096        2 /
a.out   24747 jsun  txt    REG    8,9    7894 10404039 /home/jsun/a.out
a.out   24747 jsun  mem    REG    8,1 1241392  7242668 /lib/tls/i686/cmov/libc-2.3.6.so
a.out   24747 jsun  mem    REG    0,0                0 [vdso] (stat: No such file or directory)
a.out   24747 jsun  mem    REG    8,1   88164  7225346 /lib/ld-2.3.6.so
a.out   24747 jsun    0u   CHR  136,2                4 /dev/pts/2
a.out   24747 jsun    1u   CHR  136,2                4 /dev/pts/2
a.out   24747 jsun    2u   CHR  136,2                4 /dev/pts/2

可以看到,都打开了/dev/pts/2,感觉没有脱离控制终端,不知道我这想法对不?后者说打开了终端文件,但是该终端不一定是控制终端?那是否变成了后台进程呢?下面是一段英文来自man setpgrp

Each process group is a member of a session and each process is a member of the session of which its process group is a member.

Process  groups  are  used for distribution of signals, and by terminals to arbitrate requests for their input: Processes that have the same process group as the terminal are foreground and may read, while others will block with  a  signal  if they  attempt to read.  These calls are thus used by programs such as csh(1) to create process groups in implementing job control.  The TIOCGPGRP and TIOCSPGRP calls described in termios(3) are used to get/set the process group of the  control terminal.

If a session has a controlling terminal, CLOCAL is not set and a hangup occurs, then the session leader is sent a SIGHUP.
If the session leader exits, the SIGHUP signal will be sent to each process in the foreground process group of  the  controlling terminal.

If  the  exit  of  the process causes a process group to become orphaned, and if any member of the newly-orphaned process group is stopped, then a SIGHUP signal followed by a SIGCONT signal will be sent to each process  in  the  newly-orphaned process group.


这里父进程所在的组才是前端进程组,所以能够接收终端信号,子进程此时调用setpgrp()脱离了前端进程组,而不是脱离控制台

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