GNU/Linux - 进程关联的控制终端

Controlling terminal
在类 Unix 操作系统中,控制终端是与进程组相关联的终端设备,它控制着进程与终端输入和输出的交互。控制终端通常是启动进程的终端。它负责处理进程的输入(如键盘输入)和输出(如显示文本)。
A controlling terminal is the terminal device associated with a process group in Unix-like operating systems, which controls the process's interaction with the terminal input and output. The controlling terminal is typically the terminal from which a process was started. It is responsible for handling input (like keyboard input) and output (like displaying text) for the process.
Controlling Terminal in Practice

  1. 在终端中启动进程:
  • 在 Linux 系统上打开一个终端(如 `bash` 或 `sh`)并运行一个命令(如 `vim`、`cat` 或任何脚本)时,该终端就会成为 shell 进程和从该 shell 启动的任何进程的控制终端。
  1. Starting a Process in a Terminal:
  • When you open a terminal (like `bash` or `sh`) on a Linux system and run a command (e.g., `vim`, `cat`, or any script), the terminal becomes the controlling terminal for the shell process and any processes started from that shell.
  1. 交互式程序:
  • 假设你打开一个终端,运行 `vim` 来编辑一个文件:
    vim example.txt
  • 在这里,你输入命令的终端是 `vim` 进程的控制终端。您键入的输入会发送到 `vim`,而 `vim` 的输出会显示在该终端上。
  1. Interactive Programs:
  • Suppose you open a terminal and run `vim` to edit a file:
    vim example.txt
  • Here, the terminal where you typed the command is the controlling terminal for the `vim` process. Input you type is sent to `vim`, and output from `vim` is displayed on that terminal.
  1. 无控制终端(守护进程示例):
  • 许多服务器进程或守护进程在启动时没有控制终端。典型的例子是启动时启动的服务或由系统管理器(如 `systemd`)启动的服务。这些进程通常与任何终端分离,以确保它们不会意外接收或需要终端输入。
  • 例如,像 Apache 这样的网络服务器是作为后台进程启动的,没有控制终端:
    apachectl start
  • apache进程在后台运行,与终端分离,这意味着它没有控制终端。
    注意,使用"&"启用后台进程时,控制终端是仍然存在的。后台进程不直接接收当前终端的输入。
  1. No Controlling Terminal (Daemon Example):
  • Many server processes or daemons are started without a controlling terminal. This is typical for services started at boot time or by a system manager like `systemd`. These processes are usually detached from any terminal to ensure they don't accidentally receive or require terminal input.
  • For example, a web server like Apache is started as a background process with no controlling terminal:
    apachectl start
  • The `apache` process runs in the background, detached from the terminal, meaning it has no controlling terminal.
  1. 使用 `O_NOCTTY`:
  • 如果编写的程序要打开终端设备(如串行端口),但又不想让终端设备成为进程的控制终端,则可以使用 `O_NOCTTY` 标志。
  • 例如
    int fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY);
  • 这将确保打开串行端口 `/dev/ttyS0`,但它不会成为打开该端口的进程的控制终端。
  1. Using `O_NOCTTY`:
  • If you write a program that opens a terminal device (like a serial port) but doesn't want the terminal device to become the controlling terminal for the process, you use the `O_NOCTTY` flag.
  • For instance:
    int fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY);
  • This ensures that the serial port `/dev/ttyS0` is opened, but it does not become the controlling terminal for the process that opened it.
    Why Is This Important?
    控制终端的概念对于信号处理、作业控制以及与终端的交互都很重要。例如,"SIGINT"(通过按下 "Ctrl+C "产生)等信号会发送到控制终端的进程组。如果进程没有控制终端,它就不会从终端接收此类信号,而这些信号对于进程在后台或作为守护进程时的行为至关重要。
    The concept of a controlling terminal is important for signal handling, job control, and interaction with the terminal. For example, signals like `SIGINT` (generated by pressing `Ctrl+C`) are sent to the process group of the controlling terminal. If a process doesn't have a controlling terminal, it won't receive such signals from the terminal, which can be crucial for how a process behaves in the background or as a daemon.
相关推荐
dadaobusi13 小时前
Linux内核完成大量内存/调度/时间子系统初始化的关键阶段
java·linux·前端
唐墨12313 小时前
关于linux kernel错误码为负数编码这件事情,我个人的一些看法
linux·运维·服务器
Full Stack Developme14 小时前
Linux Shell 教程概览
linux·前端·chrome
网络系统管理14 小时前
第八届江苏技能状元大赛选拔赛信息通信网络运行管理项目模块D网络服务与系统运维-Linux样题
linux·运维
凡人叶枫14 小时前
Effective C++ 条款24:若所有参数皆须要类型转换,请为此采用 non-member 函数
linux·前端·c++·算法·嵌入式开发
零陵上将军_xdr14 小时前
Shell流程控制:if/case/for/while让脚本活起来
linux·运维·服务器
坤昱14 小时前
cfs调度类深入解刨——pelt细节篇
linux·linux内核·cfs调度·eevdf·cfs调度类深入解刨·pelt·pelt细节篇
故渊at15 小时前
第十二板块:Android 系统启动与初始化 | 第二十九篇:Init 进程、RC 脚本与属性服务(Property Service)
android·linux·内存映射·权限控制·init进程·rc脚本·属性服务
某林21215 小时前
ROS2 并行编译死锁与 Linux 后台声卡/提权踩坑实录:大型轮足机器人架构复盘
linux·架构·机器人·iassc
无足鸟ICT15 小时前
【RHCA+】末行模式
linux