Unix 简明教程

Unix / Linux - Processes Management

在本章中,我们将详细讨论 Unix 中的进程管理。当你在 Unix 系统中执行程序时,系统会为该程序创建一个特殊环境。此环境包含系统运行该程序所需的一切,就好像系统中没有其他程序在运行一样。

In this chapter, we will discuss in detail about process management in Unix. When you execute a program on your Unix system, the system creates a special environment for that program. This environment contains everything needed for the system to run the program as if no other program were running on the system.

在 Unix 中发出命令时,它会创建一个新的进程或者启动一个新的进程。当你尝试 ls 命令以列出目录内容时,你就已经启动了一个进程。进程简单来说是正在运行的程序的一个实例。

Whenever you issue a command in Unix, it creates, or starts, a new process. When you tried out the ls command to list the directory contents, you started a process. A process, in simple terms, is an instance of a running program.

操作系统通过一个五位数的 ID 号来跟踪进程,该 ID 号被称为 pidprocess ID 。系统中的每个进程都有一个唯一的 pid

The operating system tracks processes through a five-digit ID number known as the pid or the process ID. Each process in the system has a unique pid.

由于用尽了所有可能的数字,下一个 pid 将滚动或重新开始,所以 pid 最终会重复。在任何时间点,系统中都无法存在两个 pid 相同的进程,因为 pid 是 Unix 用来跟踪每个进程的。

Pids eventually repeat because all the possible numbers are used up and the next pid rolls or starts over. At any point of time, no two processes with the same pid exist in the system because it is the pid that Unix uses to track each process.

Starting a Process

启动进程(运行命令)时,有两种方法可以运行它 −

When you start a process (run a command), there are two ways you can run it −

  1. Foreground Processes

  2. Background Processes

Foreground Processes

默认情况下,你启动的每个进程都在前台运行。它从键盘获取输入,并将输出发送到屏幕上。

By default, every process that you start runs in the foreground. It gets its input from the keyboard and sends its output to the screen.

你可以看到这是通过 ls 命令实现的。如果你希望列出你当前目录中的所有文件,可以使用以下命令 −

You can see this happen with the ls command. If you wish to list all the files in your current directory, you can use the following command −

$ls ch*.doc

这将显示所有名称以 ch 开头并以 .doc 结尾的文件 −

This would display all the files, the names of which start with ch and end with .doc

ch01-1.doc   ch010.doc  ch02.doc    ch03-2.doc
ch04-1.doc   ch040.doc  ch05.doc    ch06-2.doc
ch01-2.doc   ch02-1.doc

进程在前台运行时,输出将定向到我的屏幕上,如果 ls 命令需要任何输入(实际上并不需要),它会从键盘上等待输入。

The process runs in the foreground, the output is directed to my screen, and if the ls command wants any input (which it does not), it waits for it from the keyboard.

当程序在前台运行且耗时时,不能运行任何其他命令(启动任何其他进程),因为提示符在程序完成处理并退出之前都是不可用的。

While a program is running in the foreground and is time-consuming, no other commands can be run (start any other processes) because the prompt would not be available until the program finishes processing and comes out.

Background Processes

后台进程运行时无需连接到你的键盘。如果后台进程需要任何键盘输入,它会等待。

A background process runs without being connected to your keyboard. If the background process requires any keyboard input, it waits.

在后台运行进程的优势在于,你可以运行其他命令;你无需等待它完成即可开始另一个命令!

The advantage of running a process in the background is that you can run other commands; you do not have to wait until it completes to start another!

启动后台进程最简单的方法是在命令末尾添加一个和号 ( & )。

The simplest way to start a background process is to add an ampersand (&) at the end of the command.

$ls ch*.doc &

这会显示所有名称以 ch 开头并以 .doc 结尾的文件 −

This displays all those files the names of which start with ch and end with .doc

ch01-1.doc   ch010.doc  ch02.doc    ch03-2.doc
ch04-1.doc   ch040.doc  ch05.doc    ch06-2.doc
ch01-2.doc   ch02-1.doc

在此,如果 ls 命令需要任何输入(实际上并不需要),它会进入停止状态,直到我们将其移至前台并从键盘提供数据为止。

Here, if the ls command wants any input (which it does not), it goes into a stop state until we move it into the foreground and give it the data from the keyboard.

第一行包含有关后台进程的信息 - 作业号和进程 ID。你需要了解作业号才能在后台和前台之间对其进行操作。

That first line contains information about the background process - the job number and the process ID. You need to know the job number to manipulate it between the background and the foreground.

按 Enter 键,你将看到以下内容 −

Press the Enter key and you will see the following −

[1]   +   Done                 ls ch*.doc &
$

第一行告诉我们, ls 命令的后台进程已成功完成。第二行提示输入另一个命令。

The first line tells you that the ls command background process finishes successfully. The second is a prompt for another command.

Listing Running Processes

运行 ps (进程状态)命令很容易看到您自己的进程,具体如下 −

It is easy to see your own processes by running the ps (process status) command as follows −

$ps
PID       TTY      TIME        CMD
18358     ttyp3    00:00:00    sh
18361     ttyp3    00:01:31    abiword
18789     ttyp3    00:00:00    ps

One of the most commonly used flags for ps is the -f ( f for full) option, which provides more information as shown in the following example −

One of the most commonly used flags for ps is the -f ( f for full) option, which provides more information as shown in the following example −

$ps -f
UID      PID  PPID C STIME    TTY   TIME CMD
amrood   6738 3662 0 10:23:03 pts/6 0:00 first_one
amrood   6739 3662 0 10:22:54 pts/6 0:00 second_one
amrood   3662 3657 0 08:10:53 pts/6 0:00 -ksh
amrood   6892 3662 4 10:51:50 pts/6 0:00 ps -f

Here is the description of all the fields displayed by ps -f command −

Here is the description of all the fields displayed by ps -f command −

Sr.No.

Column & Description

1

UID User ID that this process belongs to (the person running it)

2

PID Process ID

3

PPID Parent process ID (the ID of the process that started it)

4

C CPU utilization of process

5

STIME Process start time

6

TTY Terminal type associated with the process

7

TIME CPU time taken by the process

8

CMD The command that started this process

ps 命令一起使用的其他选项 −

There are other options which can be used along with ps command −

Sr.No.

Option & Description

1

-a Shows information about all users

2

-x Shows information about processes without terminals

3

-u Shows additional information like -f option

4

-e Displays extended information

Stopping Processes

以多种不同的方式结束进程。通常,从基于控制台的命令发送 CTRL + C 键击(默认中断字符)将退出该命令。当进程在前景模式下运行时,此方法有效。

Ending a process can be done in several different ways. Often, from a console-based command, sending a CTRL + C keystroke (the default interrupt character) will exit the command. This works when the process is running in the foreground mode.

如果一个进程在后台运行,您应当使用 ps 命令获取其作业 ID。然后,您可以使用 kill 命令按如下方式中止该进程:

If a process is running in the background, you should get its Job ID using the ps command. After that, you can use the kill command to kill the process as follows −

$ps -f
UID      PID  PPID C STIME    TTY   TIME CMD
amrood   6738 3662 0 10:23:03 pts/6 0:00 first_one
amrood   6739 3662 0 10:22:54 pts/6 0:00 second_one
amrood   3662 3657 0 08:10:53 pts/6 0:00 -ksh
amrood   6892 3662 4 10:51:50 pts/6 0:00 ps -f
$kill 6738
Terminated

此处, kill 命令中止了 first_one 进程。如果一个进程忽略常规中止命令,则您可以按如下方式使用 kill -9 再跟上进程 ID:

Here, the kill command terminates the first_one process. If a process ignores a regular kill command, you can use kill -9 followed by the process ID as follows −

$kill -9 6738
Terminated

Parent and Child Processes

每个 Unix 进程都有两个分配给它的 ID 号:进程 ID (pid) 和父进程 ID (ppid)。系统中的每个用户进程都有一个父进程。

Each unix process has two ID numbers assigned to it: The Process ID (pid) and the Parent process ID (ppid). Each user process in the system has a parent process.

你运行的大多数命令都以 shell 作为它们的父进程。检查 ps -f 示例,其中此命令列出了进程 ID 和父进程 ID。

Most of the commands that you run have the shell as their parent. Check the ps -f example where this command listed both the process ID and the parent process ID.

Zombie and Orphan Processes

通常,当子进程被杀死时,父进程将通过 SIGCHLD 信号进行更新。然后,父进程可以根据需要执行一些其他任务或重新启动一个新子进程。但是,有时父进程在子进程被杀死之前被杀死。在这种情况下,“所有进程的父进程”( init 进程)变为新的 PPID(父进程 ID)。在某些情况下,这些进程称为孤儿进程。

Normally, when a child process is killed, the parent process is updated via a SIGCHLD signal. Then the parent can do some other task or restart a new child as needed. However, sometimes the parent process is killed before its child is killed. In this case, the "parent of all processes," the init process, becomes the new PPID (parent process ID). In some cases, these processes are called orphan processes.

当进程被杀死时, ps 列表可能仍会显示处于 Z 状态的进程。这是一个僵尸进程或死进程。该进程已死亡且不再使用。这些进程与孤儿进程不同。它们已完成执行,但仍在进程表中找到条目。

When a process is killed, a ps listing may still show the process with a Z state. This is a zombie or defunct process. The process is dead and not being used. These processes are different from the orphan processes. They have completed execution but still find an entry in the process table.

Daemon Processes

守护程序是相关的背景进程,通常使用 root 权限以其他进程的服务请求来运行。

Daemons are system-related background processes that often run with the permissions of root and services requests from other processes.

守护程序不控制本机终端。它无法打开 /dev/tty 。如果您执行 "ps -ef" ,并查看 tty 字段,则所有守护程序的 tty 都为 ?

A daemon has no controlling terminal. It cannot open /dev/tty. If you do a "ps -ef" and look at the tty field, all daemons will have a ? for the tty.

准确地说,守护程序是在后台运行,通常等待能够处理的事情发生。例如,打印机守护程序将等待打印命令。

To be precise, a daemon is a process that runs in the background, usually waiting for something to happen that it is capable of working with. For example, a printer daemon waiting for print commands.

如果您有需要大量处理时间的程序,那么将它设为守护程序并在后台运行非常值得。

If you have a program that calls for lengthy processing, then it’s worth to make it a daemon and run it in the background.

The top Command

top 命令是一个非常有用的工具,可以快速按各种条件对进程进行排序。

The top command is a very useful tool for quickly showing processes sorted by various criteria.

它是频繁更新并显示物理和虚拟内存、CPU 使用率、平均负载以及繁忙进程的信息的交互式诊断工具。

It is an interactive diagnostic tool that updates frequently and shows information about physical and virtual memory, CPU usage, load averages, and your busy processes.

以下是运行 top 命令并查看不同进程的 CPU 使用情况统计信息的简单语法 −

Here is the simple syntax to run top command and to see the statistics of CPU utilization by different processes −

$top

Job ID Versus Process ID

后台和暂停进程通常通过 job number (job ID) 进行控制。此号码不同于进程 ID,并且由于更短而使用。

Background and suspended processes are usually manipulated via job number (job ID). This number is different from the process ID and is used because it is shorter.

此外,作业可能包含多个进程,这些进程可能会按顺序或同时并行运行。使用作业 ID 比跟踪各个进程更容易。

In addition, a job can consist of multiple processes running in a series or at the same time, in parallel. Using the job ID is easier than tracking individual processes.