• 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:

    enter image description here

    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

  • 相关阅读:
    jquery对象中 “冒号” 详解
    vscode自定以vue代码模板
    Vue项目开发环境proxyTable反向代理,生产环境下服务接口地址找不到的解决
    JS函数的节流和防抖
    BFC-块状格式化上下文
    c# 鼠标钩子
    C# 以管理员身份运行WinForm程序
    样式更换
    Revert to this revision 和 Revert changes from this revision 区别
    人工智能几行代码实现换脸
  • 原文地址:https://www.cnblogs.com/aspirs/p/12503117.html
Copyright © 2020-2023  润新知