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

2024-09-02 19:44

本文主要是介绍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.
2. 交互式程序:
- 假设你打开一个终端,运行 `vim` 来编辑一个文件:
vim example.txt
- 在这里,你输入命令的终端是 `vim` 进程的控制终端。您键入的输入会发送到 `vim`,而 `vim` 的输出会显示在该终端上。
2. 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.
3. 无控制终端(守护进程示例):
- 许多服务器进程或守护进程在启动时没有控制终端。典型的例子是启动时启动的服务或由系统管理器(如 `systemd`)启动的服务。这些进程通常与任何终端分离,以确保它们不会意外接收或需要终端输入。
- 例如,像 Apache 这样的网络服务器是作为后台进程启动的,没有控制终端:
apachectl start
- apache进程在后台运行,与终端分离,这意味着它没有控制终端。
注意,使用“&”启用后台进程时,控制终端是仍然存在的。后台进程不直接接收当前终端的输入。
3. 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.
4. 使用 `O_NOCTTY`:
- 如果编写的程序要打开终端设备(如串行端口),但又不想让终端设备成为进程的控制终端,则可以使用 `O_NOCTTY` 标志。
- 例如
int fd = open(“/dev/ttyS0”, O_RDWR | O_NOCTTY);
- 这将确保打开串行端口 `/dev/ttyS0`,但它不会成为打开该端口的进程的控制终端。
4. 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.

这篇关于GNU/Linux - 进程关联的控制终端的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1130891

相关文章

Linux实现查看某一端口是否开放

《Linux实现查看某一端口是否开放》文章介绍了三种检查端口6379是否开放的方法:通过lsof查看进程占用,用netstat区分TCP/UDP监听状态,以及用telnet测试远程连接可达性... 目录1、使用lsof 命令来查看端口是否开放2、使用netstat 命令来查看端口是否开放3、使用telnet

Linux系统管理与进程任务管理方式

《Linux系统管理与进程任务管理方式》本文系统讲解Linux管理核心技能,涵盖引导流程、服务控制(Systemd与GRUB2)、进程管理(前台/后台运行、工具使用)、计划任务(at/cron)及常用... 目录引言一、linux系统引导过程与服务控制1.1 系统引导的五个关键阶段1.2 GRUB2的进化优

Linux查询服务器 IP 地址的命令详解

《Linux查询服务器IP地址的命令详解》在服务器管理和网络运维中,快速准确地获取服务器的IP地址是一项基本但至关重要的技能,下面我们来看看Linux中查询服务器IP的相关命令使用吧... 目录一、hostname 命令:简单高效的 IP 查询工具命令详解实际应用技巧注意事项二、ip 命令:新一代网络配置全

linux安装、更新、卸载anaconda实践

《linux安装、更新、卸载anaconda实践》Anaconda是基于conda的科学计算环境,集成1400+包及依赖,安装需下载脚本、接受协议、设置路径、配置环境变量,更新与卸载通过conda命令... 目录随意找一个目录下载安装脚本检查许可证协议,ENTER就可以安装完毕之后激活anaconda安装更

Linux查询服务器系统版本号的多种方法

《Linux查询服务器系统版本号的多种方法》在Linux系统管理和维护工作中,了解当前操作系统的版本信息是最基础也是最重要的操作之一,系统版本不仅关系到软件兼容性、安全更新策略,还直接影响到故障排查和... 目录一、引言:系统版本查询的重要性二、基础命令解析:cat /etc/Centos-release详

Linux grep 命令的使用指南

《Linuxgrep命令的使用指南》本文给大家介绍Linuxgrep命令的使用指南,包括基础搜索语法、实践指南,感兴趣的朋友跟随小编一起看看吧... 目录linux grep 命令全面使用指南一、基础搜索语法1. 基本文本搜索2. 多文件搜索二、常用选项详解1. 输出控制选项2. 上下文控制选项三、正则表达

Linux部署中的文件大小写问题的解决方案

《Linux部署中的文件大小写问题的解决方案》在本地开发环境(Windows/macOS)一切正常,但部署到Linux服务器后出现模块加载错误,核心原因是Linux文件系统严格区分大小写,所以本文给大... 目录问题背景解决方案配置要求问题背景在本地开发环境(Windows/MACOS)一切正常,但部署到

更改linux系统的默认Python版本方式

《更改linux系统的默认Python版本方式》通过删除原Python软链接并创建指向python3.6的新链接,可切换系统默认Python版本,需注意版本冲突、环境混乱及维护问题,建议使用pyenv... 目录更改系统的默认python版本软链接软链接的特点创建软链接的命令使用场景注意事项总结更改系统的默

Linux升级或者切换python版本实现方式

《Linux升级或者切换python版本实现方式》本文介绍在Ubuntu/Debian系统升级Python至3.11或更高版本的方法,通过查看版本列表并选择新版本进行全局修改,需注意自动与手动模式的选... 目录升级系统python版本 (适用于全局修改)对于Ubuntu/Debian系统安装后,验证Pyt

Linux从文件中提取特定内容的实用技巧分享

《Linux从文件中提取特定内容的实用技巧分享》在日常数据处理和配置文件管理中,我们经常需要从大型文件中提取特定内容,本文介绍的提取特定行技术正是这些高级操作的基础,以提取含有1的简单需求为例,我们可... 目录引言1、方法一:使用 grep 命令1.1 grep 命令基础1.2 命令详解1.3 高级用法2