Linux的进程

描述

目的:

初步了解进程描述符 task_struct。

目录:

Linux的进程

Linux 的进程描述符

task_struct

内核如何找到 task_struct

task_struct 的分配和初始化

实验:打印 task_struct / thread_info / kernel mode stack

环境:

Linux-4.14 + ARMv7

1. Linux 的进程

进程的术语是 process,是 Linux 最基础的抽象,另一个基础抽象是文件。

最简单的理解,进程就是执行中 (executing, 不等于running) 的程序。

更准确一点的理解,进程包括执行中的程序以及相关的资源(包括cpu状态、打开的文件、挂起的信号、tty、内存地址空间等)。

一种简洁的说法:进程 = n*执行流 + 资源,n>=1。

Linux 进程的特点:

通过系统调用 fork() 创建进程,fork() 会复制现有进程来创建一个全新的进程。

内核里,并不严格区分进程和线程。

从内核的角度看,调度单位是线程 (即执行流)。可以把线程看做是进程里的一条执行流,1个进程里可以有1个或者多个线程。

内核里,常把进程称为 task 或者 thread,这样描述更准确,因为许多进程就只有1条执行流。

内核通过轻量级进程 (lightweight process) 来支持多线程。1个轻量级进程就对应1个线程,轻量级进程之间可以共享打开的文件、地址空间等资源。

2. Linux 的进程描述符

2.1 task_struct

内核里,通过 task_struct 结构体来描述一个进程,称为进程描述符 (process descriptor),它保存着支撑一个进程正常运行的所有信息。

每一个进程,即便是轻量级进程(即线程),都有1个 task_struct。

sched.h (includelinux) struct task_struct {     struct thread_info thread_info;     volatile long state;     void *stack;     [...]     struct mm_struct *mm;     [...]     pid_t pid;     [...]     struct task_struct *parent;     [...]     char comm[TASK_COMM_LEN];     [...]  struct files_struct *files;  [...]  struct signal_struct *signal; }

这是一个庞大的结构体,不仅有许多进程相关的基础字段,还有许多指向其他数据结构的指针。

它包含的字段能完整地描述一个正在执行的程序,包括 cpu 状态、打开的文件、地址空间、挂起的信号、进程状态等。

多线程

点击查看大图

作为初学者,先简单地了解部分字段就好::

struct thread_info thread_info:进程底层信息,平台相关,下面会详细描述。

long state:进程当前的状态,下面是几个比较重要的进程状态以及它们之间的转换流程。

点击查看大图

void *stack:指向进程内核栈,下面会解释。

struct mm_struct *mm:与进程地址空间相关的信息都保存在一个叫内存描述符 (memory descriptor) 的结构体 (mm_struct) 中。

点击查看大图

pid_t pid:进程标识符,本质就是一个数字,是用户空间引用进程的唯一标识。

struct task_struct *parent:父进程的 task_struct。

char comm[TASK_COMM_LEN]:进程的名称。

struct files_struct *files:打开的文件表。

struct signal_struct *signal:信号处理相关。

其他字段,等到有需要的时候再回过头来学习。

2.2 当发生系统调用或者进程切换时,内核如何找到 task_struct ?

对于 ARM 架构,答案是:通过内核栈 (kernel mode stack)。

为什么要有内核栈?

因为内核是可重入的,在内核中会有多条与不同进程相关联的执行路径。因此不同的进程处于内核态时,都需要有自己私有的进程内核栈 (process kernel stack)。

当进程从用户态切换到内核态时,所使用的栈会从用户栈切换到内核栈。

至于是如何切换的,关键词是系统调用,这不是本文关注的重点,先放一边,学习内核要懂得恰当的时候忽略细节。

当发生进程切换时,也会切换到目标进程的内核栈。

同上,关键词是硬件上下文切换 (hardware context switch),忽略具体实现。

无论何时,只要进程处于内核态,就会有内核栈可以使用,否则系统就离崩溃不远了。

ARM 架构的内核栈和 task_struct 的关系如下:

内核栈的长度是 THREAD_SIZE,对于 ARM 架构,一般是 2 个页框的大小,即 8KB。

内核将一个较小的数据结构 thread_info 放在内核栈的底部,它负责将内核栈和 task_struct 串联起来。thread_info 是平台相关的,在 ARM 架构中的定义如下:

// thread_info.h (archarmincludeasm) struct thread_info {  unsigned long flags;  /* low level flags */  int preempt_count; /* 0 => preemptable, <0 => bug */  mm_segment_t addr_limit; /* address limit */  struct task_struct *task;  /* main task structure */     [...]  struct cpu_context_save cpu_context; /* cpu context */  [...] };

thread_info 保存了一个进程能被调度执行的最底层信息(low level task data),例如struct cpu_context_save cpu_context 会在进程切换时用来保存/恢复寄存器上下文。

内核通过内核栈的栈指针可以快速地拿到 thread_info:

// thread_info.h (includelinux) static inline struct thread_info *current_thread_info(void) {     // current_stack_pointer 是当前进程内核栈的栈指针  return (struct thread_info *)   (current_stack_pointer & ~(THREAD_SIZE - 1)); }

然后通过 thread_info 找到 task_struct:

// current.h (includeasm-generic) #define current (current_thread_info()->task)

内核里通过 current 宏可以获得当前进程的 task_struct。

2.3 task_struct 的分配和初始化

当上层应用使用 fork() 创建进程时,内核会新建一个 task_struct。

进程的创建是个复杂的工作,可以延伸出无数的细节。这里我们只是简单地了解一下 task_struct 的分配和部分初始化的流程。

fork() 在内核里的核心流程:

dup_task_struct() 做了什么?

至于设置内核栈里做了什么,涉及到了进程的创建与切换,不在本文的关注范围内,以后再研究了。

3. 实验:打印 task_struct / thread_info / kernel mode stack

实验目的:

梳理 task_struct / thread_info / kernel mode stack 的关系。

实验代码:

#include  #include  #include  static void print_task_info(struct task_struct *task) {     printk(KERN_NOTICE "%10s %5d task_struct (%p) / stack(%p~%p) / thread_info->task (%p)",         task->comm,          task->pid,         task,         task->stack,         ((unsigned long *)task->stack) + THREAD_SIZE,         task_thread_info(task)->task); } static int __init task_init(void) {     struct task_struct *task = current;     printk(KERN_INFO "task module init ");     print_task_info(task);     do {         task = task->parent;         print_task_info(task);     } while (task->pid != 0);     return 0; } module_init(task_init); static void __exit task_exit(void) {     printk(KERN_INFO "task module exit  "); } module_exit(task_exit);

运行效果:

task module init     insmod  3123 task_struct (edb42580) / stack(ed46c000~ed474000) / thread_info->task (edb42580)       bash  2393 task_struct (eda13e80) / stack(c9dda000~c9de2000) / thread_info->task (eda13e80)       sshd  2255 task_struct (ee5c9f40) / stack(c9d2e000~c9d36000) / thread_info->task (ee5c9f40)       sshd   543 task_struct (ef15f080) / stack(ee554000~ee55c000) / thread_info->task (ef15f080)    systemd     1 task_struct (ef058000) / stack(ef04c000~ef054000) / thread_info->task (ef058000)

在程序里,我们通过 task_struct 找到 stack,然后通过 stack 找到 thread_info,最后又通过 thread_info->task 找到 task_struct。

4. 相关参考

Linux 内核设计与实现 / 第 3.1 章节

深入理解 Linux 内核 / 3

Linux 内核深度解析 / 2.5.1

深入Linux 内核架构 / 2.3

责任编辑:lq

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分