linux - task_struct 存储在哪里?

标签 linux process kernel task

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/

相关文章:

c++ - 分析程序执行的一部分

linux - 在 Linux 上实现 x.224 OSI COTS 协议(protocol)的最佳方式是什么

android - 如何从具有ROOT权限的Android应用程序启动进程

c - 返回状态/值的适当范围

c - 让父进程等待其子进程的正确方法是什么?

linux - 如何限制向 root 用户接收通用 netlink 多播?

c - linux 内核中 container_of() 宏中的 (char *) 转换

c++ - windows下连接信号槽,linux下不连接

android - 在哪里可以找到 buildinfo.sh 文件?

c++ - 为armhf编译Crypto++进行交叉编译