task_struct 存储在哪里?

All*_*lok 5 linux kernel process task

Task_struct 用于通过内核保存有关进程的必要信息。由于这种结构,内核可以暂停一个进程,并在一段时间后继续其实现。但我的问题是:这个 task_struct 存储在内存中的什么地方(我读过关于内核堆栈的内容,是虚拟地址空间的内核空间中的那个吗?)?挂起进程后,内核在哪里保留指向该结构和该结构的指针?

如果您在描述资源的地方提供一些参考资料,我将不胜感激。

附注。我忘了说这个问题是关于 Linux 内核的。

bet*_*ido 6

Linux 内核通过 kmem_cache 工具分配一个 task_struct。例如在 fork.c 中有一段代码负责分配任务结构:

#define alloc_task_struct_node(node) \
             kmem_cache_alloc_node(task_struct_cachep, GFP_KERNEL, node)
static struct kmem_cache *task_struct_cachep;
Run Code Online (Sandbox Code Playgroud)

存储指向当前线程的指针的位置取决于体系结构。例如,这是 x86 (arch/x86/include/asm/current.h) 的工作方式:

static __always_inline struct task_struct *get_current(void)
{
    return percpu_read_stable(current_task);
}
Run Code Online (Sandbox Code Playgroud)

在 PowerPC (arch/powerpc/include/asm/current.h) 中:

static inline struct task_struct *get_current(void)
{
    struct task_struct *task;

    __asm__ __volatile__("ld %0,%1(13)"
        : "=r" (task)
        : "i" (offsetof(struct paca_struct, __current)));

    return task;
}
Run Code Online (Sandbox Code Playgroud)

您可以使用Elixir Cross Reference来轻松探索内核源代码。


Mar*_*mes 0

处理线程和进程上下文的内核结构是依赖于操作系统的。通常,它们将从非分页池中分配,就像用于管理它们的指向它们的指针的集合一样。