自学内容网 自学内容网

Linux gettid()系统调用源码分析

1、gettid()系统调用作用

gettid() 是一个Linux系统调用,用于获取当前进程的线程ID。在使用此系统调用时,你需要包含 <sys/syscall.h> 头文件,并且可以通过直接调用或使用 syscall() 函数来进行系统调用。
注意:ps 中显示的PID列的值和gettid()的值是一样的

以下是一个简单的示例代码,展示如何使用 gettid() 获取当前线程的ID:

#define _GNU_SOURCE
#include <unistd.h>
#include <sys/syscall.h>
#include <stdio.h>
 
int main() {
    pid_t tid;
 
    // 直接调用gettid()
    tid = syscall(SYS_gettid);
    printf("当前线程的ID是: %ld\n", (long)tid);
 
    return 0;
}

2、getpid()系统调用定义

/* Thread ID - the internal kernel "pid" */
SYSCALL_DEFINE0(gettid)
{
return task_pid_vnr(current);
}

从系统调用注释解释可以看出,gettid()系统调用获取的是内核的pid值。

3、gettid()代码流程分析

我们从task_pid_vnr()函数开始分析,这里task_pid_vnr()调用内部函数__task_pid_nr_ns()函数,将当前线程的task_struct以及pid_type=PIDTYPE_PID作为参数传入;

static inline pid_t task_pid_vnr(struct task_struct *tsk)
{
return __task_pid_nr_ns(tsk, PIDTYPE_PID, NULL);
}

pid_t __task_pid_nr_ns(struct task_struct *task, enum pid_type type,
struct pid_namespace *ns)
{
pid_t nr = 0;

rcu_read_lock();
if (!ns) // 由于我们传入到ns指针为NULL,所以需要重新根据当前线程的task_struct获取ns
ns = task_active_pid_ns(current);
// 根据传入pid_type和task_struct指针获取pid指针,再通过pid_nr_ns()从ns中提取到pid值
nr = pid_nr_ns(rcu_dereference(*task_pid_ptr(task, type)), ns);
rcu_read_unlock();

// 返回当前线程的pid
return nr;
}
EXPORT_SYMBOL(__task_pid_nr_ns);

我们下面逐步分析一下这几个关键函数的具体实现:

3.1 task_active_pid_ns()

struct pid_namespace *task_active_pid_ns(struct task_struct *tsk)
{
return ns_of_pid(task_pid(tsk));
}
EXPORT_SYMBOL_GPL(task_active_pid_ns);

static inline struct pid *task_pid(struct task_struct *task)
{
return task->thread_pid;
}

/*
 * ns_of_pid() returns the pid namespace in which the specified pid was
 * allocated.
 *
 * NOTE:
 * ns_of_pid() is expected to be called for a process (task) that has
 * an attached 'struct pid' (see attach_pid(), detach_pid()) i.e @pid
 * is expected to be non-NULL. If @pid is NULL, caller should handle
 * the resulting NULL pid-ns.
 */
static inline struct pid_namespace *ns_of_pid(struct pid *pid)
{
struct pid_namespace *ns = NULL;
if (pid)
ns = pid->numbers[pid->level].ns;
return ns;
}

task_active_pid_ns()根据传入的task_struct对象,获取task->thread_pid,然后再通过pid获取到ns。

3.2 task_pid_ptr()

static struct pid **task_pid_ptr(struct task_struct *task, enum pid_type type)
{
return (type == PIDTYPE_PID) ?
&task->thread_pid :
&task->signal->pids[type];
}

由于我们传入的pid_type=PIDTYPE_PID,所以这里直接返回task->thread_pid指针的地址。

3.3 pid_nr_ns()

pid_t pid_nr_ns(struct pid *pid, struct pid_namespace *ns)
{
struct upid *upid;
pid_t nr = 0;

// 如果pid存在,且ns->level小于等于pid->level
if (pid && ns->level <= pid->level) {
upid = &pid->numbers[ns->level]; // 以level为下标从pid->numbers获取upid
if (upid->ns == ns) // 如果upid->ns == ns,则返回upid->nr值,否则返回0
nr = upid->nr;
}
return nr;
}
EXPORT_SYMBOL_GPL(pid_nr_ns);

到这里可以发现,gettid()涉及到好多结构中的数据获取,最终得到upid->nr中保存的pid值。

4、0号线程的pid探究

上面我们知道了gettid()的工作流程,我们拿0号idle内核线程来带入,探究一下idle线程的pid为什么是0。

struct task_struct init_task
#ifdef CONFIG_ARCH_TASK_STRUCT_ON_STACK
__init_task_data
#endif
__aligned(L1_CACHE_BYTES)
= {
#ifdef CONFIG_THREAD_INFO_IN_TASK
.thread_info= INIT_THREAD_INFO(init_task),
.stack_refcount= REFCOUNT_INIT(1),
#endif
...
.thread_pid= &init_struct_pid,
...
};
EXPORT_SYMBOL(init_task);

我们都知道0号内核线程的管理结构是init_task,现在我们只关注thread_pid,这个thread_pid也是一开始初始化好的,指向init_struct_pid;

struct pid init_struct_pid = {
.count= REFCOUNT_INIT(1),
.tasks= {
{ .first = NULL },
{ .first = NULL },
{ .first = NULL },
},
.level= 0,
.numbers= { {
.nr= 0,
.ns= &init_pid_ns,
}, }
};

这里init_struct_pid.numbers.ns是init_pid_ns;

/*
 * PID-map pages start out as NULL, they get allocated upon
 * first use and are never deallocated. This way a low pid_max
 * value does not cause lots of bitmaps to be allocated, but
 * the scheme scales to up to 4 million PIDs, runtime.
 */
struct pid_namespace init_pid_ns = {
.kref = KREF_INIT(2),
.idr = IDR_INIT(init_pid_ns.idr),
.pid_allocated = PIDNS_ADDING,
.level = 0,
.child_reaper = &init_task,
.user_ns = &init_user_ns,
.ns.inum = PROC_PID_INIT_INO,
#ifdef CONFIG_PID_NS
.ns.ops = &pidns_operations,
#endif
};
EXPORT_SYMBOL_GPL(init_pid_ns);

OK,到这里我们用gettid()的逻辑推算0号线程的pid应该是为何值?

static inline pid_t task_pid_vnr(struct task_struct *tsk)
{
return __task_pid_nr_ns(tsk, PIDTYPE_PID, NULL);
}

pid_t __task_pid_nr_ns(struct task_struct *task, enum pid_type type,
struct pid_namespace *ns)
{
pid_t nr = 0;

rcu_read_lock();
if (!ns)
// 这里返回的是init_pid_ns
ns = task_active_pid_ns(current);
// task_pid_ptr()返回的是init_struct_pid
nr = pid_nr_ns(rcu_dereference(*task_pid_ptr(task, type)), ns);
rcu_read_unlock();
return nr;
}
EXPORT_SYMBOL(__task_pid_nr_ns);

pid_t pid_nr_ns(struct pid *pid, struct pid_namespace *ns)
{
struct upid *upid;
pid_t nr = 0;

// pid = init_struct_pid, ns->level = 0, pid->level = 0
if (pid && ns->level <= pid->level) {
// upid = { .nr= 0, .ns = &init_pid_ns, }
upid = &pid->numbers[ns->level];
if (upid->ns == ns) // upid->ns == ns
// nr = 0
nr = upid->nr;
}
return nr;
}
EXPORT_SYMBOL_GPL(pid_nr_ns);

所以0号内核线程的pid为0。

本篇博文到此结束,多谢各位读者浏览!!!


原文地址:https://blog.csdn.net/assiduous_me/article/details/138014521

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!