news 2026/5/12 8:24:30

【把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/5/9 3:21:41

打造个性化艺术展:AI印象派工坊批量处理部署实战

打造个性化艺术展&#xff1a;AI印象派工坊批量处理部署实战 1. 业务场景与技术选型背景 在数字内容创作日益普及的今天&#xff0c;用户对个性化视觉表达的需求不断增长。无论是社交媒体配图、个人作品集美化&#xff0c;还是轻量级艺术展览策划&#xff0c;将普通照片转化为…

作者头像 李华
网站建设 2026/5/1 11:36:27

MoeKoeMusic终极体验指南:打造你的专属二次元音乐天地

MoeKoeMusic终极体验指南&#xff1a;打造你的专属二次元音乐天地 【免费下载链接】MoeKoeMusic 一款开源简洁高颜值的酷狗第三方客户端 An open-source, concise, and aesthetically pleasing third-party client for KuGou that supports Windows / macOS / Linux :electron:…

作者头像 李华
网站建设 2026/5/12 8:23:41

day128—二分查找—搜索二维矩阵(LeetCode-74)

题目描述给你一个满足下述两条属性的 m x n 整数矩阵&#xff1a;每行中的整数从左到右按非严格递增顺序排列。每行的第一个整数大于前一行的最后一个整数。给你一个整数 target &#xff0c;如果 target 在矩阵中&#xff0c;返回 true &#xff1b;否则&#xff0c;返回 fals…

作者头像 李华
网站建设 2026/5/11 6:46:01

单麦语音降噪实践|基于FRCRN语音降噪-16k镜像快速实现

单麦语音降噪实践&#xff5c;基于FRCRN语音降噪-16k镜像快速实现 1. 引言&#xff1a;单通道语音降噪的现实挑战与技术选择 在真实场景中&#xff0c;语音信号常常受到环境噪声、设备干扰和混响等因素影响&#xff0c;导致语音可懂度下降。尤其在仅具备单麦克风输入的设备上…

作者头像 李华
网站建设 2026/5/2 10:01:51

MoeKoe Music二次元音乐播放器使用指南:从新手到高手的完整教程

MoeKoe Music二次元音乐播放器使用指南&#xff1a;从新手到高手的完整教程 【免费下载链接】MoeKoeMusic 一款开源简洁高颜值的酷狗第三方客户端 An open-source, concise, and aesthetically pleasing third-party client for KuGou that supports Windows / macOS / Linux :…

作者头像 李华
网站建设 2026/5/6 20:00:12

如何完整备份QQ空间历史记录:GetQzonehistory终极指南

如何完整备份QQ空间历史记录&#xff1a;GetQzonehistory终极指南 【免费下载链接】GetQzonehistory 获取QQ空间发布的历史说说 项目地址: https://gitcode.com/GitHub_Trending/ge/GetQzonehistory 在数字化记忆时代&#xff0c;QQ空间承载了无数人的青春印记。那些珍贵…

作者头像 李华