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(M)). 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(S)). 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(S)). 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
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
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.