5

When we say that a process has a controlling terminal, do we mean that the process itself has a controlling terminal, or is it the session that the process belongs to that has a controlling terminal?

I used to think that it is the session that has a controlling terminal, but then I have read the following (from here) which implies that it is the process that has a controlling terminal:

One of the attributes of a process is its controlling terminal. Child processes created with fork inherit the controlling terminal from their parent process. In this way, all the processes in a session inherit the controlling terminal from the session leader. A session leader that has control of a terminal is called the controlling process of that terminal.

8

It is indeed the session that has a controlling terminal

The Single UNIX Specification describes the relationship in terms of the controlling terminal being "associated with a session". As it goes on to specify, a controlling terminal has a 1:1 relationship with a session. There is "at most one controlling terminal" associated with a session, and "a controlling terminal is associated with exactly one session".

The FreeBSD Design and Implementation book approaches this slightly differently, but reaches the same place. It is not possible for processes that share the same session to have different controlling terminals, nor is it possible for a single terminal to be the controlling terminal of multiple sessions.

Internally in FreeBSD that is how the data structures actually work. The process structure has a pointer to the pgrp structure representing the process group that the process belongs to, which in turn points to the session structure representing the session that the process group belongs to, which in turn points to the tty structure of the controlling terminal for the session.

Internally in Linux, things are slightly more complex. Each task_struct has a set of pointers to pid structures for its process group ID and session ID; and has another pointer to a per-process signal_struct structure that in turn directly points to the tty structure of the controlling terminal.

Further reading

  • George V. Neville-Neil, Marshall Kirk McKusick, and Robert N.M. Watson (2014-09-25). "Process Management". The Design and Implementation of the FreeBSD Operating System. Addison-Wesley Professional. ISBN 9780133761832.
  • Donald Lewine (1991). "Terminal I/O". POSIX Programmers Guide. O'Reilly Media, Inc. ISBN 9780937175736.
  • Daniel P. Bovet and Marco Cesati (2005). "Processes". Understanding the Linux Kernel: From I/O Ports to Process Management. 3rd edition. O'Reilly Media, Inc. ISBN 9780596554910.
  • "Definitions". The Open Group Base Specifications. Issue 7. 2016. IEEE 1003.1:2008.
  • "General Terminal Interface". The Open Group Base Specifications. Issue 7. 2016. IEEE 1003.1:2008.
  • So, does Linux kernel satisfy this relationship? I mean, is that possible in Linux to make processes in the same session use different controlling terminals or let one terminal be multi sessions' controlling terminal? – 炸鱼薯条德里克 Dec 30 '18 at 3:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.