Task_struct 用于内核保存进程的必要信息。由于该结构,内核可以暂停一个进程,并在一段时间后继续执行它。但我的问题是:这个 task_struct 存储在内存中的什么地方(我读过内核堆栈,是在虚拟地址空间的内核空间中的那个吗?)?挂起进程后,内核在哪里保存指向该结构和该结构的指针?
如果您在描述的地方提供一些资源引用,我将不胜感激。
附言。我忘了说这个问题是关于 Linux 内核的。
最佳答案
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;
当前线程指针的存储位置是依赖于体系结构的。例如,这是它在 x86 上的工作方式 (arch/x86/include/asm/current.h):
static __always_inline struct task_struct *get_current(void)
{
return percpu_read_stable(current_task);
}
在 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;
}
您可以使用 Elixir Cross Reference以便轻松探索内核源代码。
关于linux - task_struct 存储在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10604632/