控制进程、controlling terminal

来源:互联网 发布:openVPN软件下载 编辑:程序博客网 时间:2024/06/05 20:19

1、Controlling terminal【WP:CC】

A process can have at most one controlling terminal, which is the primary means for interacting with the operator in text mode.
Note: For graphical programs, the controlling terminal is typically not the primary means of interaction, but it still can be used for controlling the process, e.g. pausing or terminating it.

A controlling terminal is assigned to a whole session, which consists of one or more process groups. Only one group can read and write from/to the terminal at one time; such group is called the foreground group; others are said to be at the background. The shell allows the user to switch between the groups.

When a process starts, it inherits the controlling terminal from its parent, as well as it inherits a process group (and therefore a session).


Terminal and the shell:When a process is started from the shell, it is typically in the foreground group (but the user can explictly request otherwise), and its standard input, standard output and standard error output are attached to the controlling terminal (but the user can redirect these streams as needed).Examining, detaching and attaching to a terminal

A process can query its controlling terminal via the /dev/tty file.

A process itself can close and redirect its standard input and outputs, but it stays bound to its controlling terminal. If it needs to disconnect from the controlling terminal, it has to leave its session, which is accomplished with the setsid() call.

If a process has no controlling terminal (i.e. it is in a session that has no controlling terminal), it can acquire one by open()-ing a terminal file without the O_NOCTTY flag.

2、【WP:CC】

 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 termio(7)). The controlling-terminal plays a special role in handling quit and ``interrupt'' signals (see ``Special characters'').

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 setsid(2)). 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 open(2)). 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 ``Special characters'' above).

A new process inherits the controlling-terminal through the fork operation (see fork(2)). 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 setsid(2)). 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.

3、【WP:CC】

A terminal may belong to a process as its controlling terminal. Each process of a session that has a controlling terminal hasthe same controlling terminal. A terminal may be the controlling terminal for at most one session. The controlling terminal for asession is allocated by the session leader in an implementation-defined manner. If a session leader has no controlling terminal,and opens a terminal device file that is not already associated with a session without using the O_NOCTTY option (seeopen()), it is implementation-defined whether the terminal becomes the controlling terminal ofthe session leader. If a process which is not a session leader opens a terminal file, or the O_NOCTTY option is used onopen(), then that terminal shall not become the controlling terminal of the calling process.When a controlling terminal becomes associated with a session, its foreground process group shall be set to the process group ofthe session leader.

The controlling terminal is inherited by a child process during a fork() functioncall. A process relinquishes its controlling terminal when it creates a new session with thesetsid() function; other processes remaining in the old session that had this terminal astheir controlling terminal continue to have it. Upon the close of the last file descriptor in the system (whether or not it is inthe current session) associated with the controlling terminal, it is unspecified whether all processes that had that terminal astheir controlling terminal cease to have any controlling terminal. Whether and how a session leader can reacquire a controllingterminal after the controlling terminal has been relinquished in this fashion is unspecified. A process does not relinquish itscontrolling terminal simply by closing all of its file descriptors associated with the controlling terminal if other processescontinue to have it open.

When a controlling process terminates, the controlling terminal is dissociated from the current session, allowing it to beacquired by a new session leader. Subsequent access to the terminal by other processes in the earlier session may be denied, withattempts to access the terminal treated as if a modem disconnect had been sensed.


原创粉丝点击