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中SSH服务配置的全面指南

《Linux中SSH服务配置的全面指南》作为网络安全工程师,SSH(SecureShell)服务的安全配置是我们日常工作中不可忽视的重要环节,本文将从基础配置到高级安全加固,全面解析SSH服务的各项参... 目录概述基础配置详解端口与监听设置主机密钥配置认证机制强化禁用密码认证禁止root直接登录实现双因素

解析C++11 static_assert及与Boost库的关联从入门到精通

《解析C++11static_assert及与Boost库的关联从入门到精通》static_assert是C++中强大的编译时验证工具,它能够在编译阶段拦截不符合预期的类型或值,增强代码的健壮性,通... 目录一、背景知识:传统断言方法的局限性1.1 assert宏1.2 #error指令1.3 第三方解决

在Linux终端中统计非二进制文件行数的实现方法

《在Linux终端中统计非二进制文件行数的实现方法》在Linux系统中,有时需要统计非二进制文件(如CSV、TXT文件)的行数,而不希望手动打开文件进行查看,例如,在处理大型日志文件、数据文件时,了解... 目录在linux终端中统计非二进制文件的行数技术背景实现步骤1. 使用wc命令2. 使用grep命令

Linux如何快速检查服务器的硬件配置和性能指标

《Linux如何快速检查服务器的硬件配置和性能指标》在运维和开发工作中,我们经常需要快速检查Linux服务器的硬件配置和性能指标,本文将以CentOS为例,介绍如何通过命令行快速获取这些关键信息,... 目录引言一、查询CPU核心数编程(几C?)1. 使用 nproc(最简单)2. 使用 lscpu(详细信

Spring如何使用注解@DependsOn控制Bean加载顺序

《Spring如何使用注解@DependsOn控制Bean加载顺序》:本文主要介绍Spring如何使用注解@DependsOn控制Bean加载顺序,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录1.javascript 前言2. 代码实现总结1. 前言默认情况下,Spring加载Bean的顺

linux重启命令有哪些? 7个实用的Linux系统重启命令汇总

《linux重启命令有哪些?7个实用的Linux系统重启命令汇总》Linux系统提供了多种重启命令,常用的包括shutdown-r、reboot、init6等,不同命令适用于不同场景,本文将详细... 在管理和维护 linux 服务器时,完成系统更新、故障排查或日常维护后,重启系统往往是必不可少的步骤。本文

基于Python开发Windows屏幕控制工具

《基于Python开发Windows屏幕控制工具》在数字化办公时代,屏幕管理已成为提升工作效率和保护眼睛健康的重要环节,本文将分享一个基于Python和PySide6开发的Windows屏幕控制工具,... 目录概述功能亮点界面展示实现步骤详解1. 环境准备2. 亮度控制模块3. 息屏功能实现4. 息屏时间

基于Linux的ffmpeg python的关键帧抽取

《基于Linux的ffmpegpython的关键帧抽取》本文主要介绍了基于Linux的ffmpegpython的关键帧抽取,实现以按帧或时间间隔抽取关键帧,文中通过示例代码介绍的非常详细,对大家的学... 目录1.FFmpeg的环境配置1) 创建一个虚拟环境envjavascript2) ffmpeg-py

Linux脚本(shell)的使用方式

《Linux脚本(shell)的使用方式》:本文主要介绍Linux脚本(shell)的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录概述语法详解数学运算表达式Shell变量变量分类环境变量Shell内部变量自定义变量:定义、赋值自定义变量:引用、修改、删

Windows的CMD窗口如何查看并杀死nginx进程

《Windows的CMD窗口如何查看并杀死nginx进程》:本文主要介绍Windows的CMD窗口如何查看并杀死nginx进程问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录Windows的CMD窗口查看并杀死nginx进程开启nginx查看nginx进程停止nginx服务