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

10 篇文章 1 订阅

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。

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

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux系统调用机制是指用户程序通过特定的系统调用接口来请求操作系统提供服务。在Linux中,系统调用是用户程序与操作系统之间的桥梁,它允许用户程序访问操作系统的功能和资源。 当用户程序需要执行某些特权操作或访问受限资源时,它需要通过系统调用接口向操作系统发起请求。系统调用接口提供了一组函数,用户程序可以通过这些函数来调用相应的系统调用。通常,用户程序使用高级语言(如C)来编写,而系统调用接口则是由操作系统提供的低级接口。 在Linux中,系统调用的实现是通过软中断来完成的。当用户程序调用系统调用接口时,CPU会触发一个软中断,将控制权转移到内核态。在内核态中,操作系统会根据用户程序传递的参数执行相应的操作,并将结果返回给用户程序。完成后,CPU再次切换回用户态,用户程序继续执行。 Linux提供了丰富的系统调用接口,包括文件操作、进程管理、网络通信、内存管理等。每个系统调用都有一个唯一的编(称为系统调用),用户程序通过指定系统调用来请求相应的服务。 总结起来,Linux系统调用机制是通过软中断实现的,用户程序通过系统调用接口向操作系统发起请求并获取相应的服务。这种机制保证了用户程序与操作系统之间的安全隔离,并提供了高效的系统资源访问方式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值