struct thread_info 定位struct task_struct 需要什么?

Sen*_*Sen 8 linux process c kernel multithreading

在阅读 Linux 设备驱动程序时,我可以理解进程描述符(类型struct task_struct)包含有关特定任务的所有信息。进程描述符由slab分配器动态分配。

我想知道的是需要引入一个称为thread_info存储在堆栈底部的新结构(假设为 x86)。为什么这样做?

为什么不能将当前正在执行的任务地址 ( struct task_struct)的地址放入内核堆栈?

Pau*_*esC 5

我们需要thread_info的原因是因为我们使用 Slab Allocator为task_struct分配内存。现在你可能会问这之间有什么关系?

要了解这一点,您需要了解 Slab Allocator 的工作原理。

如果没有 Slab Allocator ,内核开发人员可以为特定进程的内核堆栈中的task_struct分配内存,以便可以轻松访问它。现在随着 Slab Allocator 的出现,内存被分配给由 Slab Allocator 确定的task_struct 。因此,使用 Slab 分配器,您可以将task_struct存储在其他位置,而不是存储在特定进程的内核堆栈中。现在内核开发人员引入了thread_info,并在其中放置了一个指向task_struct所在位置的指针。这就是为什么我们必须忍受thread_info

您可以在 Robert Love 的《Linux 内核开发》一书中阅读有关 Slab Allocator 的内容。


小智 -4

轻量级进程没有task_struct; 一个堆栈和少量信息就足够了。多个 LWP 共享相同的task_struct,其中包含所有资源描述。

  • 在linux内核中,`taks_struct`和`thread_info`是1-1映射。 (2认同)