分类:
2008-11-28 13:56:15
9.6 controlling terminal
1.Controlling terminal是用来进行job control,你可以通过controlling terminal向session 内的process group发信号,可以对其进行管理。
2.如果是采用terminal device login,那么这个terminal device就是该session的controlling terminal。
3.如果是采用pseudo terminal来进行的login,那么这个pseudo terminal就是该session的controlling terminal。
4.一个session仅能最多拥有一个controlling terminal, 一个session可以不拥有controlling terminal,使用setsid刚建立的session就没有controlling terminal。
5.Terminal一般来说由我们的login shell就打开了,所以我们一般没必要手动打开。从login shell fork出来的进程都回继承controlling terminal。
6. 打开controlling terminal的process被称为controlling process,它拥有对该controlling terminal的最多的使用权限。
7.Controlling process所在的process group也拥有使用controlling terminal的脚多的权限。有时我们在controlling terminal上敲击的字符命令就会以信号的方式发送给该process group内部的所有的process。如Ctl+C, DELETE,QUIT字符的信号会发给该process group。当modem的disconnect发生时,会仅仅向controlling process发送 hangup信号。然后controlling process一般来说会退出。而这个 process group一般来说被称为foreground process group,而session内部其他的process group被称为background process group. Back ground process group内部的process拥有较少的对controlling 的使用权限。
8.一般来说,descriptor 0,1,2会映射到controlling terminal上,但是有的程序将0,1.2给冲向了,那么此时如何访问controlling terminal呢? 有办法:调用opend打开/dev/tty,这就是你的controlling terminal。
具体controlling terminal的东西很复杂,如下是一些资料:
A terminal may belong to a process as its controlling-terminal, which is a terminal uniquely associated with one session. Each process of a session with a controlling-terminal has the same controlling-terminal assigned to it. Each session may have at most one controlling-terminal associated with it and vice versa. A terminal may be assigned to at most one session as the controlling-terminal. Certain input sequences from the controlling-terminal cause signals to be sent to all processes in the process-group for the controlling-terminal (see ). The controlling-terminal plays a special role in handling quit and ``interrupt'' signals (see ).
The controlling-terminal for a session is acquired by the session-leader, which is the process that created the session; the session-id of a session equals the process-id of the session-leader. When a session-leader acquires a controlling-terminal for its session, it thereby becomes the controlling-process of that session (see ). Should the terminal later cease to be a controlling-terminal for the session of the session-leader, the session-leader ceases to be a controlling-process.
When a session-leader without a controlling-terminal opens a terminal-device-file and the flag O_NOCTTY is clear on open, that terminal becomes the controlling-terminal assigned to the session-leader if the terminal is not already assigned to some session (see ). When any process other than a session-leader opens a terminal-device-file, or the flag O_NOCTTY is set on open, that terminal does not become the controlling-terminal assigned to the calling-process.
A controlling-terminal distinguishes one of the process-groups in the session assigned to it as the ``foreground'' process-group; all other process-groups in the session are ``background'' process-groups. By default, when the session-leader acquires a controlling-terminal, the process-group of the session-leader becomes the foreground process-group of the controlling-terminal. The foreground process-group plays a special role in handling signal-generating input characters (see above).
A new process inherits the controlling-terminal through the
fork operation (see ).
When a process calls setsid to create a new session, the process
relinquishes its controlling-terminal; other processes remaining in the old
session with that terminal as their controlling-terminal continue to have it
(see ).
When all file-descriptors that denote the controlling-terminal in the system
are closed (whether or not it is in the current session), it is unspecified
whether all processes that had that terminal as their controlling-terminal
cease to have any controlling-terminal. Whether and how a session-leader can
reacquire a controlling-terminal after the controlling-terminal is relinquished
in this fashion is unspecified. A process does not relinquish its
controlling-terminal simply by
closing all of its file-descriptors that denote the controlling-terminal if
other processes continue to have it open.
When a session-leader terminates, the current session relinquishes the controlling-terminal allowing a new session-leader to acquire it. Any further attempts to access the terminal by other processes in the old session may be denied and treated as if modem-disconnect was detected on the terminal.
如下内容来自:
The controlling terminal can receive signals and send signals. If a foreground process group has the stream as a controlling terminal stream, drivers and modules can use M_SIG messages to send signals to processes.
An overview of Job Control is provided here because it interacts with the STREAMS-based terminal subsystem. You can obtain more information on Job Control from the following manual pages: , , , , , , , , , , , , , and .
Job Control breaks a login session into smaller units called jobs. Each job consists of one or more related and cooperating processes. The foreground job, is given complete access to the controlling terminal. The other background jobs are denied read access to the controlling terminal and given conditional write and access to it. The user can stop the executing job and resume the stopped job either in the foreground or in the background.
Under Job Control, background jobs do not receive events generated by the terminal and are not informed with a hangup indication when the controlling process exits. Background jobs that linger after the login session has been dissolved are prevented from further access to the controlling terminal, and do not interfere with the creation of new login sessions.
The following list defines terms associated with Job Control:
Background process group
A process group that is a member of a session that established a connection with a controlling terminal and is not the foreground process group.
Controlling process
A session leader that established a connection to a controlling terminal.
Controlling terminal
A terminal that is associated with a session. Each session can have at most one controlling terminal associated with it, and a controlling terminal can be associated with at most one session. Certain input sequences from the controlling terminal cause signals to be sent to the process groups in the session associated with the controlling terminal.
Foreground process group
Each session that establishes a connection with a controlling terminal distinguishes one process group of the session as a foreground process group. The foreground process group has certain privileges that are denied to background process groups when accessing its controlling terminal.
Orphaned process group
A process group in which the parent of every member in the group is either a member of the group, or is not a member of the process group's session.
Process group
Each process in the system is a member of a process group that is identified by a process group ID. Any process that is not a process group leader can create a new process group and become its leader. Any process that is not a process group leader can join an existing process group that shares the same session as the process. A newly created process joins the process group of its creator.
Process group leader
A process whose process ID is the same as its process group ID.
Process group lifetime
A time period that begins when a process group is created by its process group leader and ends when the last process that is a member in the group leaves the group.
Process ID
A positive integer that uniquely identifies each process in the system. A process ID cannot be reused by the system until the process lifetime, process group lifetime, and session lifetime end for any process ID, process group ID, and session ID sharing that value.
Process lifetime
A period that begins when the process is forked and ends after the process exits, when its termination has been acknowledged by its parent process.
Session
Each process group is a member of a session that is identified by a session ID.
Session ID
A positive integer that uniquely identifies each session in the system. It is the same as the process ID of its session leader (POSIX).
Session leader
A process whose session ID is the same as its process and process group ID.
Session lifetime
A period that begins when the session is created by its session leader and ends when the lifetime of the last process group that is a member of the session ends.
The following signals manage Job Control: (see also ) :
SIGCONT
Sent to a stopped process to continue it.
SIGSTOP
Sent to a process to stop it. This signal cannot be caught or ignored.
SIGTSTP
Sent to a process to stop it. It is typically used when a user requests to stop the foreground process.
SIGTTIN
Sent to a background process to stop it when it attempts to read from the controlling terminal.
SIGTTOU
Sent to a background process to stop it when a user attempts to write to or modify the controlling terminal.
A session can be allocated a controlling terminal. For every allocated controlling terminal, Job Control elevates one process group in the controlling process's session to the status of foreground process group. The remaining process groups in the controlling process's session are background process groups. A controlling terminal gives a user the ability to control execution of jobs within the session. Controlling terminals are critical in Job Control. A user can cause the foreground job to stop by typing a predefined key on the controlling terminal. A user can inhibit access to the controlling terminal by background jobs. Background jobs that attempt to access a terminal that has been so restricted is sent a signal that typically causes the job to stop. (See .)
Job Control requires support from a line-discipline module on the controlling terminal's stream. The TCSETA, TCSETAW, and TCSETAF commands of allow a process to set the following line discipline values relevant to Job Control:
SUSP character
A user-defined character that, when typed, causes the line discipline module to request that the stream head send a SIGTSTP signal to the foreground process, which by default stops the members of that group. If the value of SUSP is zero, the SIGTSTP signal is not sent, and the SUSP character is disabled.
TOSTOP flag
If TOSTOP is set, background processes are inhibited from writing to their controlling terminal. A line discipline module must record the SUSP suspend character and notify the stream head when the user has typed it, and record the state of the TOSTOP bit and notify the stream head when the user has changed it.
A stream is allocated as a controlling terminal for a session if it:
· Is acting as a terminal.
· Is not already allocated as a controlling terminal.
· Is opened by a session leader that does not have a controlling terminal.
Controlling terminals are allocated with . The device must inform the stream head that it is acting as a terminal.
When a stream head receives a hangup message from a device or module, it is marked as hung up. A stream that is marked as hung up is allowed to be reopened by its session leader if it is allocated as a controlling terminal, and by any process if it is not allocated as a controlling terminal. This way, the hangup error can be cleared without forcing all file descriptors to be closed first.
If the reopen is successful, the hangup condition is cleared.
When the SIGHUP signal is generated by a hangup message instead of a signal message, the signal is sent to the controlling process instead of the foreground process group. The allocation and deallocation of controlling terminals to a session is the responsibility of that process group.
If a process attempts to access its controlling terminal after it has been deallocated, access is denied. If the process is not holding or ignoring SIGHUP, it is sent a SIGHUP signal. Otherwise, the access fails with an EIO error.
Members of background process groups have limited access to their controlling terminals:
· If the background process is ignoring or holding the SIGTTIN signal or is a member of an orphaned process group, an attempt to read from the controlling terminal fails with an EIO error. Otherwise, the process is sent a SIGTTIN signal, which by default stops the process.
· If the process is attempting to write to the terminal and if the terminal's TOSTOP flag is clear, the process is allowed access.
· If the terminal's TOSTOP flag is set and a background process is attempting to write to the terminal, the write succeeds if the process is ignoring or holding SIGTTOU. Otherwise, the process stops except when it is a member of an orphaned process group, in which case it is denied access to the terminal and it is returned an EIO error.
If a background process is attempting to perform a destructive (one that modifies terminal parameters), the call succeeds if the process is ignoring or holding SIGTTOU. Otherwise, the process stops except when the process is a member of the orphaned process group. In that case the access to the terminal is denied and an EIO error is returned.
下面是Single Unix specification的general terminal interface:
这个比较多,就不贴在这里了。