news 2026/9/3 3:40:56

【把Linux“聊”明白】进程的概念与状态

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
【把Linux“聊”明白】进程的概念与状态

一、基本概念与操作

1-1 基本概念

先来看课本与内核对于进程的解释:

课本概念:程序的一个执行实例,正在执行的程序等; 内核观点:担当分配系统资源(CPU时间,内存)的实体。

听起来都太抽象,在这里,我们可以理解为进程 = 内核数据结构对象 + 自己的代码和数据,如下图所示:

在这里插入图片描述

可以看到,每个进程都对应着其内核数据结构对象和自己的代码和数据,我们可以把其内核数据结构用类似链表的结构连接起来,那么对于进程的管理,就变成对链表的增删查改了。

1-2 PCB

内核数据结构我们又称为PCB (Process Control Block),即进程控制块。可以理解为进程属性的集合。简单说,它就是描述进程的结构体。 我们通过PCB,就可以直接或者间接的找到进程的所有属性。 在Linux中,具体的PCB是task_struct,PCB是课本的说法。 task_struct是Linux内核的⼀种数据结构,它会被装载到RAM(内存)里并且包含着进程的信息。

1-3 task_struct

既然task_struct是描述进程的结构体,那么它里面详细有什么呢?简单看一下:

标示符:描述本进程的唯一标示符,用来区别其他进程。 状态:任务状态,退出代码,退出信号等。 优先级:相对于其他进程的优先级。 程序计数器:程序中即将被执行的下⼀条指令的地址。 内存指针:包括程序代码和进程相关数据的指针,还有和其他进程共享的内存块的指针。 上下文数据:进程执行时处理器的寄存器中的数据。 I∕O状态信息:包括显示的I/O请求,分配给进程的I∕O设备和被进程使用的⽂文件列表。 记账信息:可能包括处理器时间总和,使用的时钟数总和,时间限制,记账号等。 其他信息……

1-4 查看进程

在这里,我们要知道,我们历史上执行的所有指令(内建命令除外)、工具、自己的程序,运行起来,都是进程! 很好理解,现在我们来了解一下如何查看进程呢?

  1. 通过/proc系统文件夹查看

如:要获取PID为1的进程信息,你需要查看 /proc/1 这个⽂件夹。

在这里插入图片描述

  1. 使用top和ps这些用户级工具 我们可以先建一个一直循环的程序来进行测试:

代码语言:javascript

AI代码解释

#include <stdio.h> #include <unistd.h> int main() { while (1) { printf("hello world\n"); sleep(1); } return 0; }

在这里插入图片描述

代码语言:javascript

AI代码解释

ps ajx | head -1 && ps ajx | grep myproc | grep -v grep

ps ajx:使用特定的格式选项显示进程信息 其它命令组合,就是帮助我们既能清晰看到表头信息,又能准确找到目标进程,排除干扰项所用。

补充:

在这里插入图片描述

1-5 通过系统调用获取进程标示符

在这里插入图片描述

pidppid

进程id(PID) 父进程id(PPID)

使用一下:

代码语言:javascript

AI代码解释

#include <stdio.h> #include <unistd.h> int main() { printf("pid:%d\n",getpid()); printf("ppid:%d\n",getppid()); return 0; }

输出:

在这里插入图片描述

我们可以对ppid进行搜索

在这里插入图片描述

哦,bash? 说明:bash(命令行解释器)也是一个进程。 我们要知道,OS会为每一个登录的用户,分配一个bash

补充:exe和cwd它们是进程对应的两个属性,我们可以在/proc下查看进程对应的属性:

在这里插入图片描述

执行命令后可以看到:

在这里插入图片描述

解释:

exe :指向启动该进程的可执行文件的完整路径。它告诉你这个进程是由哪个程序文件创建的。 cwd :Current Working Directory 的缩写,代表进程的当前工作目录。像我们在C语言中的fopen函数,以写的形式打开文件…,如果我们没有指定路径的话,就会默认在当前路径,即根据cwd来确定路径。

www.dongchedi.com/article/7595302803858293310
www.dongchedi.com/article/7595302533728551486
www.dongchedi.com/article/7595303526705152574
www.dongchedi.com/article/7595302533728911934
www.dongchedi.com/article/7595302732916277822
www.dongchedi.com/article/7595300345145164313
www.dongchedi.com/article/7595287730565710361
www.dongchedi.com/article/7595287469617529368
www.dongchedi.com/article/7595289256520663576
www.dongchedi.com/article/7595287092394000920
www.dongchedi.com/article/7595286612611957273
www.dongchedi.com/article/7595285133738410521
www.dongchedi.com/article/7595285905729487384
www.dongchedi.com/article/7595287514580435481
www.dongchedi.com/article/7595285072006382105
www.dongchedi.com/article/7595285719334502936
www.dongchedi.com/article/7595285631619007000
www.dongchedi.com/article/7595277509785453081
www.dongchedi.com/article/7595276630432760345
www.dongchedi.com/article/7595275735409967640
www.dongchedi.com/article/7595277089067549246
www.dongchedi.com/article/7595276413155295769
www.dongchedi.com/article/7595276373905195544
www.dongchedi.com/article/7595274144955499033
www.dongchedi.com/article/7595274423667048984
www.dongchedi.com/article/7595275907217195545
www.dongchedi.com/article/7595274913787953689
www.dongchedi.com/article/7595274806057337368
www.dongchedi.com/article/7595274833727406617
www.dongchedi.com/article/7595255011878208062
www.dongchedi.com/article/7595255232834159166
www.dongchedi.com/article/7595244636982379033
www.dongchedi.com/article/7595246619336000062
www.dongchedi.com/article/7595245102663352894
www.dongchedi.com/article/7595246113737982526
www.dongchedi.com/article/7595238963515146814
www.dongchedi.com/article/7595238605032292888
www.dongchedi.com/article/7595237840809198105
www.dongchedi.com/article/7595237829975212569
www.dongchedi.com/article/7595237024668877336
www.dongchedi.com/article/7594914712018600510
www.dongchedi.com/article/7594913283375907352
www.dongchedi.com/article/7594914016129106456
www.dongchedi.com/article/7594914424213766718
www.dongchedi.com/article/7594913083894891033
www.dongchedi.com/article/7594913119710069310
www.dongchedi.com/article/7594912459706696254
www.dongchedi.com/article/7594911633613390360
www.dongchedi.com/article/7594909036307595800
www.dongchedi.com/article/7594910057444786750
www.dongchedi.com/article/7594909893274927641
www.dongchedi.com/article/7594909974816588350
www.dongchedi.com/article/7594909035217404441
www.dongchedi.com/article/7594908551181615678
www.dongchedi.com/article/7594906883010855486
www.dongchedi.com/article/7594907513641058878
www.dongchedi.com/article/7594905549272285720
www.dongchedi.com/article/7594906861996884505
www.dongchedi.com/article/7594906134906421785
www.dongchedi.com/article/7594905869373096472
www.dongchedi.com/article/7594904114086560281
www.dongchedi.com/article/7594903301414650392
www.dongchedi.com/article/7594901235942228542
www.dongchedi.com/article/7594901805579452953
www.dongchedi.com/article/7594900315275657752
www.dongchedi.com/article/7594899458794799641
www.dongchedi.com/article/7594900266315645465
www.dongchedi.com/article/7594900151853236798
www.dongchedi.com/article/7594898846111580697
www.dongchedi.com/article/7594899021756776984

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/9/3 0:23:54

TradingView图表库终极集成指南:5分钟构建专业金融图表应用

TradingView图表库终极集成指南&#xff1a;5分钟构建专业金融图表应用 【免费下载链接】charting-library-examples Examples of Charting Library integrations with other libraries, frameworks and data transports 项目地址: https://gitcode.com/gh_mirrors/ch/charti…

作者头像 李华
网站建设 2026/9/3 1:30:35

小米智能家居C控制终极指南:从零打造个性化智能生活

小米智能家居C#控制终极指南&#xff1a;从零打造个性化智能生活 【免费下载链接】mi-home С# API for Xiaomi Mi Home devices 项目地址: https://gitcode.com/gh_mirrors/mi/mi-home 还在为小米官方APP的功能限制而烦恼吗&#xff1f;想要实现更灵活、更个性化的智能…

作者头像 李华
网站建设 2026/9/3 2:38:33

开源模型也能强逻辑?DeepSeek-R1思维链能力评测与部署

开源模型也能强逻辑&#xff1f;DeepSeek-R1思维链能力评测与部署 1. 背景与技术定位 近年来&#xff0c;大语言模型在自然语言理解、代码生成和逻辑推理等任务中展现出惊人能力。然而&#xff0c;主流高性能模型往往依赖高算力GPU进行推理&#xff0c;限制了其在边缘设备或隐…

作者头像 李华
网站建设 2026/9/3 2:15:24

没GPU如何测试Qwen-Image?云端按需付费方案详解

没GPU如何测试Qwen-Image&#xff1f;云端按需付费方案详解 你是不是也遇到过这种情况&#xff1a;AI课程老师布置了作业&#xff0c;要求体验最新的Qwen-Image图像生成模型&#xff0c;结果发现学校机房没有GPU&#xff0c;自己的笔记本连显卡都没有&#xff0c;或者只有个入…

作者头像 李华
网站建设 2026/9/2 23:25:24

ProGuard Maven插件完整指南:Java字节码优化与混淆的最佳实践

ProGuard Maven插件完整指南&#xff1a;Java字节码优化与混淆的最佳实践 【免费下载链接】proguard-maven-plugin ProGuard Maven plugin that supports modularised ProGuard packages 项目地址: https://gitcode.com/gh_mirrors/pr/proguard-maven-plugin 在当今Java…

作者头像 李华
网站建设 2026/9/3 1:51:38

小鹏汽车:以AI重构万人团队招聘,赋能一线业务管理

在“AI招聘 潮头之上”2025NFuture最佳雇主颁奖盛典深圳站现场&#xff0c;小鹏汽车营销服招聘负责人冯晓莲分享了题为《用AI赋能一线管理者》的深度实践。面对业务快速扩张带来的海量人才需求&#xff0c;她坦言团队曾面临巨大挑战&#xff0c;而AI工具的引入&#xff0c;帮助…

作者头像 李华