守护进程之Sessions

Sessions:A session is a collection of one or more process groups.

Sessions:是单个或多个进程组的一个集合。

process establishes a new session by calling the setsid function.
进程靠调用setsid()创建一个会话。
#include <unistd.h>
pid_t setsid(void);

If the calling process is not a process group leader, this function creates a new session. Three things happen
如果调用进程不是组长进程,函数setsid()会创建一个新的会话。起到三个作用:
  1.The process becomes the session leader of this new session. (A session leader is the process that creates a session.) 
     调用进程成为新会话的the session leader。A session leader 是创建会话的进程。
     The process is the only process in this new session.
     在新的会话中,调用进程是唯一的进程。
  2.The process becomes the process group leader of a new process group. The new process group ID is the process ID of the calling process.
     调用进程成为新进程组的组长进程。新进程组ID就是调用进程的进程ID。
  3.The process has no controlling terminal. (We'll discuss controlling terminals in the next section.)   
     调用进程没有控制终端。
     If the process had a controlling terminal before calling setsid, that association is broken.
     在调用setsid()之前,如果调用进程有控制终端, 那么调用进程会脱离这个控制终端。

 

This function returns an error if the caller is already a process group leader. 

如果调用进程已经是一个组长进程,则函数返回出错。
To ensure this is not the case, the usual practice is to call fork and have the parent terminate and the child continue.
为了避免这种状况发生,通常的做法是调用fork()使父进程终止,子进程继续运行。
We are guaranteed that the child is not a process group leader, because the process group ID of the parent is inherited by the child,
我们保证了子进程不是组长进程,因为父进程的进程组ID没有被子进程继承,
but the child gets a new process ID. Hence, it is impossible for the child's process ID to equal its inherited process group ID.
而子进程得到一个新进程ID。因此,子进程ID等于它继承的进程组ID是不可能的。

posted @ 2012-02-20 22:57  PoleStar  阅读(303)  评论(0编辑  收藏  举报