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

相关文章

Spring Security+JWT如何实现前后端分离权限控制

《SpringSecurity+JWT如何实现前后端分离权限控制》本篇将手把手教你用SpringSecurity+JWT搭建一套完整的登录认证与权限控制体系,具有很好的参考价值,希望对大家... 目录Spring Security+JWT实现前后端分离权限控制实战一、为什么要用 JWT?二、JWT 基本结构

Android实现两台手机屏幕共享和远程控制功能

《Android实现两台手机屏幕共享和远程控制功能》在远程协助、在线教学、技术支持等多种场景下,实时获得另一部移动设备的屏幕画面,并对其进行操作,具有极高的应用价值,本项目旨在实现两台Android手... 目录一、项目概述二、相关知识2.1 MediaProjection API2.2 Socket 网络

Linux命令之firewalld的用法

《Linux命令之firewalld的用法》:本文主要介绍Linux命令之firewalld的用法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux命令之firewalld1、程序包2、启动firewalld3、配置文件4、firewalld规则定义的九大

Java程序进程起来了但是不打印日志的原因分析

《Java程序进程起来了但是不打印日志的原因分析》:本文主要介绍Java程序进程起来了但是不打印日志的原因分析,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java程序进程起来了但是不打印日志的原因1、日志配置问题2、日志文件权限问题3、日志文件路径问题4、程序

Linux之计划任务和调度命令at/cron详解

《Linux之计划任务和调度命令at/cron详解》:本文主要介绍Linux之计划任务和调度命令at/cron的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux计划任务和调度命令at/cron一、计划任务二、命令{at}介绍三、命令语法及功能 :at

Linux下如何使用C++获取硬件信息

《Linux下如何使用C++获取硬件信息》这篇文章主要为大家详细介绍了如何使用C++实现获取CPU,主板,磁盘,BIOS信息等硬件信息,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录方法获取CPU信息:读取"/proc/cpuinfo"文件获取磁盘信息:读取"/proc/diskstats"文

Linux内核参数配置与验证详细指南

《Linux内核参数配置与验证详细指南》在Linux系统运维和性能优化中,内核参数(sysctl)的配置至关重要,本文主要来聊聊如何配置与验证这些Linux内核参数,希望对大家有一定的帮助... 目录1. 引言2. 内核参数的作用3. 如何设置内核参数3.1 临时设置(重启失效)3.2 永久设置(重启仍生效

SQL表间关联查询实例详解

《SQL表间关联查询实例详解》本文主要讲解SQL语句中常用的表间关联查询方式,包括:左连接(leftjoin)、右连接(rightjoin)、全连接(fulljoin)、内连接(innerjoin)、... 目录简介样例准备左外连接右外连接全外连接内连接交叉连接自然连接简介本文主要讲解SQL语句中常用的表

kali linux 无法登录root的问题及解决方法

《kalilinux无法登录root的问题及解决方法》:本文主要介绍kalilinux无法登录root的问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,... 目录kali linux 无法登录root1、问题描述1.1、本地登录root1.2、ssh远程登录root2、

Linux ls命令操作详解

《Linuxls命令操作详解》通过ls命令,我们可以查看指定目录下的文件和子目录,并结合不同的选项获取详细的文件信息,如权限、大小、修改时间等,:本文主要介绍Linuxls命令详解,需要的朋友可... 目录1. 命令简介2. 命令的基本语法和用法2.1 语法格式2.2 使用示例2.2.1 列出当前目录下的文