The process refers to the executing program and is an instance of the program when it is running. It consists of program instructions and data read from files, other programs, or input from system users.
In the following guide, we will gradually understand the basic concepts of processes and briefly introduce how to use specific commands to manage processes in Linux systems.
Process Type
In the Linux system, there are two main types of processes: foreground processes and background processes.
The foreground process refers to the process that interacts directly with the user. Typically, commands executed by the user on the terminal run as a foreground process. These processes occupy a terminal and send their output directly to the user.
A background process is a process that runs in the background and does not occupy the terminal and has no direct interaction with the user. Background processes are typically used to perform long-running tasks or services that allow users to perform other operations on the terminal without being affected by it.
By using specific commands, we can manage and control these processes, including viewing running processes, starting new processes, pausing, resuming, or terminating processes. These commands can help us effectively manage and monitor the processes in the system.
前台进程(也称为交互式进程) - 这些进程由终端会话初始化和控制。换句话说,需要有一个连接到系统中的用户来启动这样的进程;它们不是作为系统功能/服务的一部分自动启动。 后台进程(也称为非交互式/自动进程) - 这些进程没有连接到终端;它们不需要任何用户输入。
What is daemon
This is a special type of background process that starts when the system starts and runs as a service; they do not die. They start spontaneously as system tasks (run as services). However, they can be controlled by users through the init process (after RHEL&CentOs7, systemd replaced the init process. For details, please refer to the relevant content on how to learn Linux).
Creating a process in Linux
There are three ways to create a process in Linux:
fork() method
Use the fork() function to copy a process based on the parent process, and its PID number is different from the parent process PID number. In the Linux environment, fork() is implemented by copy-write. The environment of the new child process is the same as that of the parent process. Only the memory is different from the parent process. The rest is shared with the parent process. It can only be modified after the parent process or the child process. , before regenerating one copy.
system() method
The system() function will call /bin/sh –c command to execute a specific command and block the execution of the current process until the command command is executed. The new child process will have a new PID.
exec() method
The exec() method has several different functions. Different from the previous fork() and system() functions, the exec() method will replace the original process with a new process, and the system will run from the new process. The PID value of the process will be the same as the PID value of the original process.
How does Linux identify processes?
Since Linux is a multi-user system, meaning that different users can run a variety of programs on the system, the kernel must uniquely identify each instance of the program running.
A program is identified by its process ID (PID) and the process ID (PPID) of its parent process, so processes can be classified as:
父进程 - 这些是在运行时创建其它进程的进程。 子进程 - 这些是在运行时由其它进程创建的进程。
init process
The init process is the parent process of all processes in the system. It is the first program to run after starting the Linux system; it manages all other processes on the system. It is started by the kernel itself, so theoretically it has no parent process.
The process ID of the init process is always 1. It is the parent process of all child processes. (Similar to the relationship between the root directory and subdirectories, everything starts from the heel, and everything starts from the init process).
Find process ID
You can use the pidof command to find the process ID of a process:
# pidof systemd # pidof top # pidof httpd

要查找当前 shell 的进程 ID 以及它父进程的进程 ID,可以运行:
$ echo $$ $ echo $PPID

在 Linux 中启动进程
每次你运行一个命令或程序(例如 cloudcmd – CloudCommander),它就会在系统中启动一个进程。你可以按照下面的方式启动一个前台(交互式)进程,它会被连接到终端,用户可以发送输入给它:
# cloudcmd

Linux 后台任务
要在后台(非交互式)启动一个进程,使用 & 符号,此时,该进程不会从用户中读取输入,直到它被移到前台。
# cloudcmd & # jobs

你也可以使用 Ctrl + Z 暂停执行一个程序并把它发送到后台,它会给进程发送 SIGSTOP 信号,从而暂停它的执行;它就会变为空闲:
# tar -cf backup.tar /backups/* ### 按下 Ctrl+Z # jobs
要在后台继续运行上面被暂停的命令,使用 bg 命令:
# bg
要把后台进程发送到前台,使用 fg 命令以及任务的 ID,类似:
# jobs # fg %1

Linux 中进程的状态
在执行过程中,取决于它的环境一个进程会从一个状态转变到另一个状态。在 Linux 中,一个进程有下面的可能状态:
Running - 此时它正在运行(它是系统中的当前进程)或准备运行(它正在等待分配 CPU 单元)。 Waiting - 在这个状态,进程正在等待某个事件的发生或者系统资源。另外,内核也会区分两种不同类型的等待进程;可中断等待进程interruptible waiting processes - 可以被信号中断,以及不可中断等待进程uninterruptible waiting processes- 正在等待硬件条件,不能被任何事件/信号中断。 Stopped - 在这个状态,进程已经被停止了,通常是由于收到了一个信号。例如,正在被调试的进程。 Zombie - 该进程已经死亡,它已经停止了但是进程表process table中仍然有它的条目。
如何在 Linux 中查看活跃进程
有很多 Linux 工具可以用于查看/列出系统中正在运行的进程,两个传统众所周知的是 ps 和 top 命令:
1. ps 命令
它显示被选中的系统中活跃进程的信息,如下图所示:
# ps # ps -e | head

2. top – 系统监控工具
top 是一个强大的工具,它能给你提供 运行系统的动态实时视图,如下面截图所示:
# top

3. glances – 系统监控工具
glances 是一个相对比较新的系统监控工具,它有一些比较高级的功能:
# glances

如何在 Linux 中控制进程
Linux 也有一些命令用于控制进程,例如 kill、pkill、pgrep 和 killall,下面是一些如何使用它们的基本事例:
$ pgrep -u tecmint top $ kill 2308 $ pgrep -u tecmint top $ pgrep -u tecmint glances $ pkill glances $ pgrep -u tecmint glances

给进程发送信号
Linux 中控制进程的基本方法是给它们发送信号。你可以发送很多信号给一个进程,运行下面的命令可以查看所有信号:
$ kill -l

要给一个进程发送信号,可以使用我们之前提到的 kill、pkill 或 pgrep 命令。但只有被编程为能识别这些信号时程序才能响应这些信号。
大部分信号都是系统内部使用,或者给程序员编写代码时使用。下面是一些对系统用户非常有用的信号:
SIGHUP 1 - 当控制它的终端被被关闭时给进程发送该信号。 SIGINT 2 - 当用户使用 Ctrl+C 中断进程时控制它的终端给进程发送这个信号。 SIGQUIT 3 - 当用户发送退出信号 Ctrl+D 时给进程发送该信号。 SIGKILL 9 - 这个信号会马上中断(杀死)进程,进程不会进行清理操作。 SIGTERM 15 - 这是一个程序终止信号(kill 默认发送这个信号)。 SIGTSTP 20 - 它的控制终端发送这个信号给进程要求它停止(终端停止);通过用户按 Ctrl+Z 触发。
下面是当 Firefox 应用程序僵死时通过它的 PID 杀死它的 kill 命令事例:
$ pidof firefox $ kill 9 2687 或 $ kill -KILL 2687 或 $ kill -SIGKILL 2687
使用它的名称杀死应用,可以像下面这样使用 pkill 或 killall:
$ pkill firefox $ killall firefox
更改 Linux 进程优先级
在 Linux 系统中,所有活跃进程都有一个优先级以及 nice 值。有比点优先级进程有更高优先级的进程一般会获得更多的 CPU 时间。
但是,有 root 权限的系统用户可以使用 nice 和 renice 命令影响(更改)优先级。
在 top 命令的输出中, NI 显示了进程的 nice 值:
$ top

使用 nice 命令为一个进程设置 nice 值。记住一个普通用户可以给他拥有的进程设置 0 到 20 的 nice 值。
只有 root 用户可以使用负的 nice 值。
要重新设置一个进程的优先级,像下面这样使用 renice 命令:
$ renice +8 2687 $ renice +8 2103
The above is the detailed content of What you need to know about Linux processes. For more information, please follow other related articles on the PHP Chinese website!

linux设备节点是应用程序和设备驱动程序沟通的一个桥梁;设备节点被创建在“/dev”,是连接内核与用户层的枢纽,相当于硬盘的inode一样的东西,记录了硬件设备的位置和信息。设备节点使用户可以与内核进行硬件的沟通,读写设备以及其他的操作。

区别:1、open是UNIX系统调用函数,而fopen是ANSIC标准中的C语言库函数;2、open的移植性没fopen好;3、fopen只能操纵普通正规文件,而open可以操作普通文件、网络套接字等;4、open无缓冲,fopen有缓冲。

端口映射又称端口转发,是指将外部主机的IP地址的端口映射到Intranet中的一台计算机,当用户访问外网IP的这个端口时,服务器自动将请求映射到对应局域网内部的机器上;可以通过使用动态或固定的公共网络IP路由ADSL宽带路由器来实现。

在linux中,交叉编译是指在一个平台上生成另一个平台上的可执行代码,即编译源代码的平台和执行源代码编译后程序的平台是两个不同的平台。使用交叉编译的原因:1、目标系统没有能力在其上进行本地编译;2、有能力进行源代码编译的平台与目标平台不同。

在linux中,eof是自定义终止符,是“END Of File”的缩写;因为是自定义的终止符,所以eof就不是固定的,可以随意的设置别名,linux中按“ctrl+d”就代表eof,eof一般会配合cat命令用于多行文本输出,指文件末尾。

在linux中,可以利用“rpm -qa pcre”命令判断pcre是否安装;rpm命令专门用于管理各项套件,使用该命令后,若结果中出现pcre的版本信息,则表示pcre已经安装,若没有出现版本信息,则表示没有安装pcre。

linux查询mac地址的方法:1、打开系统,在桌面中点击鼠标右键,选择“打开终端”;2、在终端中,执行“ifconfig”命令,查看输出结果,在输出信息第四行中紧跟“ether”单词后的字符串就是mac地址。

在linux中,rpc是远程过程调用的意思,是Reomote Procedure Call的缩写,特指一种隐藏了过程调用时实际通信细节的IPC方法;linux中通过RPC可以充分利用非共享内存的多处理器环境,提高系统资源的利用率。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download
The most popular open source editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6
Visual web development tools
