为了理解正在运行的进程的含义,我们需要了解进程的不同状态。进程在linux内核中也被称为任务。进程的状态由task_struct中的一个整型变量表示。以下是kernel源代码中定义的进程状态:
/*
* The task state array is a strange "bitmap" of
* reasons to sleep. Thus "running" is zero, and
* you can test for combinations of others with
* simple bit tests.
*/
static const char * const task_state_array[] = {
"R (running)", /* 0 */
"S (sleeping)", /* 1 */
"D (disk sleep)", /* 2 */
"T (stopped)", /* 4 */
"t (tracing stop)", /* 8 */
"X (dead)", /* 16 */
"Z (zombie)", /* 32 */
};为什么需要进程状态?在日常生活中,如果你感冒了,你可能会告诉室友今天状态不好不去上课了。这里的状态决定了你后续的行动——不去上课。在Linux中,操作系统需要根据进程的状态来决定对这些进程的后续操作。
进程的状态可以通俗地分为五种:创建状态、就绪状态、阻塞状态、执行状态和终止状态。最基本的状态包括:运行状态、就绪状态和阻塞状态。
就绪状态:进程已经具备运行条件,但由于没有空闲的CPU而暂时不能运行。
运行状态:进程正在运行,占用CPU资源。
阻塞状态:进程因为等待某一事件而暂时不能运行,如执行sleep或等待输入。
创建状态:进程正在被创建,操作系统为其分配资源、初始化PCB。
终止状态:进程从系统中被撤销,操作系统回收进程拥有的资源。
前面提到的状态与我们之前描述的具体状态有所不同,具体状态是通俗状态的具体实例。让我们再看看代码:
/*
* The task state array is a strange "bitmap" of
* reasons to sleep. Thus "running" is zero, and
* you can test for combinations of others with
* simple bit tests.
*/
static const char * const task_state_array[] = {
"R (running)", /* 0 */
"S (sleeping)", /* 1 */
"D (disk sleep)", /* 2 */
"T (stopped)", /* 4 */
"t (tracing stop)", /* 8 */
"X (dead)", /* 16 */
"Z (zombie)", /* 32 */
};我们可以通过以下命令查看进程状态:
ps aux / ps axj 命令
让我们编写一个程序并观察其状态:
#include <stdio.h>
#include <unistd.h>
int main(){
while(1){
printf("I am a process!\n");
sleep(1); //休眠一秒
}
return 0;
}Makefile配置如下:
mybin:test1.c
gcc -o mybin test1.c
.PHONY:clean
clean:
rm -f mybin使用两个xshell窗口,一个运行程序,另一个观察进程状态。我们还可以编写一个循环命令来查看进程状态:
while :;do ps ajx|head -1 && ps ajx|grep mybin|grep -v grep;sleep 1; done
每秒打印一次mybin进程的状态。运行程序后,我们可以看到进程的STAT状态为S,这是因为程序中的sleep函数会让进程休眠一秒。如果去掉sleep,STAT仍然会是S,因为printf也会导致短暂的休眠。要显示R状态,可以去掉printf,让程序执行死循环。
Z(zombie) - 僵尸进程:当进程退出且父进程未读取其返回代码时,进程进入僵尸状态。僵尸进程会一直保持在进程表中,等待父进程读取其退出状态。僵尸进程会导致内存泄漏。
我们可以创建一个维持30秒的僵尸进程:
#include <stdio.h>
#include <stdlib.h>
int main(){
pid_t id = fork();
if(id > 0){
printf("parent[%d] is sleeping...\n",getpid());
sleep(30);
}else{
printf("child[%d] is begin Z...\n",getpid());
sleep(5);
exit(-1);
}
return 0;
}孤儿进程:如果父进程提前退出,子进程后退出并进入Z状态,子进程会成为孤儿进程,由1号进程收养。
我们可以编写一个程序来观察孤儿进程:
#include <stdio.h>
#include <stdlib.h>
int main(){
pid_t id = fork();
if(id > 0){
printf("parent[%d] is sleeping...\n",getpid());
sleep(3);
exit(-1);
}else{
printf("child[%d] is begin Z...\n",getpid());
sleep(10);
}
return 0;
}通过这些示例,我们可以看到子进程被1号进程接管。






以上就是Linux——进程状态的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号