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来轻松探索内核源代码。
| 归档时间: |
|
| 查看次数: |
6784 次 |
| 最近记录: |