What is the purpose of the controlling terminal?

What is the purpose of the controlling terminal?

Say that I have opened some program from the terminal, and I have redirected its stdin and stdout and stderr to three files.

Even though this program no longer send its output to the terminal and no longer receive its input from the terminal, the program still has a "link" to the terminal. The terminal that the program has this "link" with is called the controlling terminal, the following image illustrates this:

What is the purpose of the controlling terminal?

 

 

My question is: what is the purpose of the controlling terminal, my guess is that the controlling terminal can be used to send signals to the program (for example: Ctrl+C).

Are there other purposes for the controlling terminal?

 

answer:

With controlling terminal, process is able to tell kernel which process group is foreground process group (in same session). If there is a foreground process group for a terminal, we can control the foreground process group via the terminal, e.g., Ctrl-C/Ctrl-\ to terminate the foreground process group. (A terminal may have only one foreground process group, or may not have any. To put it precisely, a terminal can only be associated with one process session.)

With controlling terminal, even if you already redirect stdin to other places/files, you still be able to read/write from/to controlling terminal, the /dev/tty. This special file is a synonym within the kernel for the controlling terminal of current process. If your process has no controlling terminal associated, then open this file will fail. What can you do regarding this file? E.g., some programs need user to input password before doing something, such as programs for login or encryption. These programs may prohibit user from inputting password from stdin, which means even if you redirect their stdin to a random file, they'll still wait for your type. The reason is they all open /dev/tty to read.

To sum up, with controlling terminal, kernel is aware of where to deliver terminal-generated signal and terminal input if they are expected by someone. That's all.

So it is unnecessary for a process to associate with a controlling terminal, if it doesn't want to be controlled by any terminal, and doesn't want to read/write from/to "/dev/tty" (like most daemon programs). However, a common process launched from within a shell always have controlling terminal associated, because it is member of shell session, which has already established a controlling terminal when shell starts up. (Actually, a random process cannot attach a terminal as controlling terminal, only session leader process can.)

 

from: https://unix.stackexchange.com/questions/404555/what-is-the-purpose-of-the-controlling-terminal

 

上一篇:vscode护眼绿+python环境搭建


下一篇:vue2 visible.sync 属性用法