session_group
时间:2007-02-14 来源:ffjnfj
By definition, /dev/tty refers to the controlling tty, entirely independent of
redirects of standard input and output.
Every session is tied to a terminal from which processes in the session get
their input and to which they send their output. That terminal may be the
machine's local console, a terminal connected over a serial line, or a pseudo
terminal that maps to an X window or across a network (see Chapter 16 for
information on pseudo terminal devices). The terminal to which a session is
related is called the controlling terminal (or controlling tty) of the
session. A terminal can be the controlling terminal for only one session at a
time.
Another popular feature added to Unix fairly early was job control. Job
control allows users to suspend the current task (known as the foreground
task) while they go and do something else on their terminals. When the
suspended task is a sequence of processes working together, the system needs
to keep track of which processes should be suspended when the user wants to
suspend "the" foreground task. Process groups allow the system to keep track
of which processes are working together and hence should be managed together
via job control.o
When a background process reads from the terminal it gets a SIGTTIN signal.
or if its process group is orphaned (see below), then the read() returns an
EIO error, and no signal is sent. When a background process writes to the
terminal, it may get a SIGTTOU signal. May: namely, when the flag that this
must happen is set (it is off by default). One can set the flag by
>
% stty tostop
Orphaned process groups ~
The process group leader is the first member of the process group. It may
terminate before the others, and then the process group is without leader.
A process group is called orphaned when the parent of every member is either
in the process group or outside the session. In particular, the process group
of the session leader is always orphaned.
If termination of a process causes a process group to become orphaned, and
some member is stopped, then all are sent first SIGHUP and then SIGCONT.
The idea is that perhaps the parent of the process group leader is a job
control shell. 其实如果他不是job control shell, 则还是有其他的进程(job control
shell) continue stopped process,但是是job control shell 的情况也是有的,所以
为了确保能够continue stopped proceess,才有这样的约定。也就是说,真的kill了
job control process该怎么办? (In the same session but a different process
group.) As long as this parent is alive, it can handle the stopping and
starting of members in the process group. When it dies, there may be nobody to
continue stopped processes. Therefore, these stopped processes are sent
SIGHUP, so that they die unless they catch or ignore it, and then SIGCONT to
continue them.
Note that the process group of the session leader is already orphaned, so no
signals are sent when the session leader dies.
Note also that a process group can become orphaned in two ways by termination
of a process: either it was a parent and not itself in the process group, or
it was the last element of the process group with a parent outside but in the
same session. Furthermore, that a process group can become orphaned other than
by termination of a process, namely when some member is moved to a different
process group.
$ cat a.sh
cat
$ ./a.sh &
$ ps -o pid,ppid,sid,stat,comm
PID PPID SID STAT COMMAND
7415 5329 7415 Ss bash
8988 7415 7415 T a.sh
8989 8988 7415 T cat
9070 7415 7415 R+ ps
$ kill -9 8988
$ ps
7415 pts/3 00:00:01 bash
8883 pts/3 00:00:00 ps
也就是说cat进程被kill了,但是并没有显式的kill,在a.sh被kill之前,cat的状态是
STOP(T),那么cat就会收到一个SIGHUP信号,所以就挂了。如果把cat换成sleep,则
sleep就不会死了,因为sleep不read stdio,所以就不会STOP,所以就不会收到HUP信号
Every session may have a controlling tty, that then also is called the
controlling tty of each of its member processes. A file descriptor for the
controlling tty is obtained by opening /dev/tty.
If a process wants to continue as a daemon, it must detach itself from its
controlling tty. Above we saw that setsid() will remove the controlling tty.
Also the ioctl TIOCNOTTY does this. Moreover, in order not to get a
controlling tty again as soon as it opens a tty, the process has to fork once
more, to assure that it is not a session leader. Typical code fragment:
>
if ((fork()) != 0)
exit(0);
setsid();
if ((fork()) != 0)
exit(0);
If a daemon was started from a mounted file system, it would be impossible to
unmount the file system until the daemon was killed. As a result, it is a
sensible daemon programming practice to perform a chdir() to /
1. session主要是跟controlling terminal相关联的。也就是说,一个session可以且最多
有一个控制终端,也可以没有
2. 进程组的主要目的是job control。比如foreground/background进程组
vim:ft=help:tw=78
redirects of standard input and output.
Every session is tied to a terminal from which processes in the session get
their input and to which they send their output. That terminal may be the
machine's local console, a terminal connected over a serial line, or a pseudo
terminal that maps to an X window or across a network (see Chapter 16 for
information on pseudo terminal devices). The terminal to which a session is
related is called the controlling terminal (or controlling tty) of the
session. A terminal can be the controlling terminal for only one session at a
time.
Another popular feature added to Unix fairly early was job control. Job
control allows users to suspend the current task (known as the foreground
task) while they go and do something else on their terminals. When the
suspended task is a sequence of processes working together, the system needs
to keep track of which processes should be suspended when the user wants to
suspend "the" foreground task. Process groups allow the system to keep track
of which processes are working together and hence should be managed together
via job control.o
When a background process reads from the terminal it gets a SIGTTIN signal.
or if its process group is orphaned (see below), then the read() returns an
EIO error, and no signal is sent. When a background process writes to the
terminal, it may get a SIGTTOU signal. May: namely, when the flag that this
must happen is set (it is off by default). One can set the flag by
>
% stty tostop
Orphaned process groups ~
The process group leader is the first member of the process group. It may
terminate before the others, and then the process group is without leader.
A process group is called orphaned when the parent of every member is either
in the process group or outside the session. In particular, the process group
of the session leader is always orphaned.
If termination of a process causes a process group to become orphaned, and
some member is stopped, then all are sent first SIGHUP and then SIGCONT.
The idea is that perhaps the parent of the process group leader is a job
control shell. 其实如果他不是job control shell, 则还是有其他的进程(job control
shell) continue stopped process,但是是job control shell 的情况也是有的,所以
为了确保能够continue stopped proceess,才有这样的约定。也就是说,真的kill了
job control process该怎么办? (In the same session but a different process
group.) As long as this parent is alive, it can handle the stopping and
starting of members in the process group. When it dies, there may be nobody to
continue stopped processes. Therefore, these stopped processes are sent
SIGHUP, so that they die unless they catch or ignore it, and then SIGCONT to
continue them.
Note that the process group of the session leader is already orphaned, so no
signals are sent when the session leader dies.
Note also that a process group can become orphaned in two ways by termination
of a process: either it was a parent and not itself in the process group, or
it was the last element of the process group with a parent outside but in the
same session. Furthermore, that a process group can become orphaned other than
by termination of a process, namely when some member is moved to a different
process group.
$ cat a.sh
cat
$ ./a.sh &
$ ps -o pid,ppid,sid,stat,comm
PID PPID SID STAT COMMAND
7415 5329 7415 Ss bash
8988 7415 7415 T a.sh
8989 8988 7415 T cat
9070 7415 7415 R+ ps
$ kill -9 8988
$ ps
7415 pts/3 00:00:01 bash
8883 pts/3 00:00:00 ps
也就是说cat进程被kill了,但是并没有显式的kill,在a.sh被kill之前,cat的状态是
STOP(T),那么cat就会收到一个SIGHUP信号,所以就挂了。如果把cat换成sleep,则
sleep就不会死了,因为sleep不read stdio,所以就不会STOP,所以就不会收到HUP信号
Every session may have a controlling tty, that then also is called the
controlling tty of each of its member processes. A file descriptor for the
controlling tty is obtained by opening /dev/tty.
If a process wants to continue as a daemon, it must detach itself from its
controlling tty. Above we saw that setsid() will remove the controlling tty.
Also the ioctl TIOCNOTTY does this. Moreover, in order not to get a
controlling tty again as soon as it opens a tty, the process has to fork once
more, to assure that it is not a session leader. Typical code fragment:
>
if ((fork()) != 0)
exit(0);
setsid();
if ((fork()) != 0)
exit(0);
If a daemon was started from a mounted file system, it would be impossible to
unmount the file system until the daemon was killed. As a result, it is a
sensible daemon programming practice to perform a chdir() to /
1. session主要是跟controlling terminal相关联的。也就是说,一个session可以且最多
有一个控制终端,也可以没有
2. 进程组的主要目的是job control。比如foreground/background进程组
vim:ft=help:tw=78
相关阅读 更多 +