What you need to know about Linux processes
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!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The key differences between CentOS and Ubuntu are: origin (CentOS originates from Red Hat, for enterprises; Ubuntu originates from Debian, for individuals), package management (CentOS uses yum, focusing on stability; Ubuntu uses apt, for high update frequency), support cycle (CentOS provides 10 years of support, Ubuntu provides 5 years of LTS support), community support (CentOS focuses on stability, Ubuntu provides a wide range of tutorials and documents), uses (CentOS is biased towards servers, Ubuntu is suitable for servers and desktops), other differences include installation simplicity (CentOS is thin)

How to use Docker Desktop? Docker Desktop is a tool for running Docker containers on local machines. The steps to use include: 1. Install Docker Desktop; 2. Start Docker Desktop; 3. Create Docker image (using Dockerfile); 4. Build Docker image (using docker build); 5. Run Docker container (using docker run).

CentOS has been discontinued, alternatives include: 1. Rocky Linux (best compatibility); 2. AlmaLinux (compatible with CentOS); 3. Ubuntu Server (configuration required); 4. Red Hat Enterprise Linux (commercial version, paid license); 5. Oracle Linux (compatible with CentOS and RHEL). When migrating, considerations are: compatibility, availability, support, cost, and community support.

CentOS installation steps: Download the ISO image and burn bootable media; boot and select the installation source; select the language and keyboard layout; configure the network; partition the hard disk; set the system clock; create the root user; select the software package; start the installation; restart and boot from the hard disk after the installation is completed.

Docker process viewing method: 1. Docker CLI command: docker ps; 2. Systemd CLI command: systemctl status docker; 3. Docker Compose CLI command: docker-compose ps; 4. Process Explorer (Windows); 5. /proc directory (Linux).

Docker uses Linux kernel features to provide an efficient and isolated application running environment. Its working principle is as follows: 1. The mirror is used as a read-only template, which contains everything you need to run the application; 2. The Union File System (UnionFS) stacks multiple file systems, only storing the differences, saving space and speeding up; 3. The daemon manages the mirrors and containers, and the client uses them for interaction; 4. Namespaces and cgroups implement container isolation and resource limitations; 5. Multiple network modes support container interconnection. Only by understanding these core concepts can you better utilize Docker.

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

Troubleshooting steps for failed Docker image build: Check Dockerfile syntax and dependency version. Check if the build context contains the required source code and dependencies. View the build log for error details. Use the --target option to build a hierarchical phase to identify failure points. Make sure to use the latest version of Docker engine. Build the image with --t [image-name]:debug mode to debug the problem. Check disk space and make sure it is sufficient. Disable SELinux to prevent interference with the build process. Ask community platforms for help, provide Dockerfiles and build log descriptions for more specific suggestions.
