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

2024-04-22 22:12

本文主要是介绍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获取nsns = 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();// 返回当前线程的pidreturn 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->levelif (pid && ns->level <= pid->level) {upid = &pid->numbers[ns->level]; // 以level为下标从pid->numbers获取upidif (upid->ns == ns) // 如果upid->ns == ns,则返回upid->nr值,否则返回0nr = 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_nsns = task_active_pid_ns(current);// task_pid_ptr()返回的是init_struct_pidnr = 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 = 0if (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 = 0nr = upid->nr;}return nr;
}
EXPORT_SYMBOL_GPL(pid_nr_ns);

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

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

这篇关于Linux gettid()系统调用源码分析的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/926996

相关文章

Nginx配置系统服务&设置环境变量方式

《Nginx配置系统服务&设置环境变量方式》本文介绍了如何将Nginx配置为系统服务并设置环境变量,以便更方便地对Nginx进行操作,通过配置系统服务,可以使用系统命令来启动、停止或重新加载Nginx... 目录1.Nginx操作问题2.配置系统服android务3.设置环境变量总结1.Nginx操作问题

jdk21下载、安装详细教程(Windows、Linux、macOS)

《jdk21下载、安装详细教程(Windows、Linux、macOS)》本文介绍了OpenJDK21的下载地址和安装步骤,包括Windows、Linux和macOS平台,下载后解压并设置环境变量,最... 目录1、官网2、下载openjdk3、安装4、验证1、官网官网地址:OpenJDK下载地址:Ar

linux本机进程间通信之UDS详解

《linux本机进程间通信之UDS详解》文章介绍了Unix域套接字(UDS)的使用方法,这是一种在同一台主机上不同进程间通信的方式,UDS支持三种套接字类型:SOCK_STREAM、SOCK_DGRA... 目录基础概念本机进程间通信socket实现AF_INET数据收发示意图AF_Unix数据收发流程图A

linux环境openssl、openssh升级流程

《linux环境openssl、openssh升级流程》该文章详细介绍了在Ubuntu22.04系统上升级OpenSSL和OpenSSH的方法,首先,升级OpenSSL的步骤包括下载最新版本、安装编译... 目录一.升级openssl1.官网下载最新版openssl2.安装编译环境3.下载后解压安装4.备份

C#集成DeepSeek模型实现AI私有化的流程步骤(本地部署与API调用教程)

《C#集成DeepSeek模型实现AI私有化的流程步骤(本地部署与API调用教程)》本文主要介绍了C#集成DeepSeek模型实现AI私有化的方法,包括搭建基础环境,如安装Ollama和下载DeepS... 目录前言搭建基础环境1、安装 Ollama2、下载 DeepSeek R1 模型客户端 ChatBo

Go使用pprof进行CPU,内存和阻塞情况分析

《Go使用pprof进行CPU,内存和阻塞情况分析》Go语言提供了强大的pprof工具,用于分析CPU、内存、Goroutine阻塞等性能问题,帮助开发者优化程序,提高运行效率,下面我们就来深入了解下... 目录1. pprof 介绍2. 快速上手:启用 pprof3. CPU Profiling:分析 C

MySQL表锁、页面锁和行锁的作用及其优缺点对比分析

《MySQL表锁、页面锁和行锁的作用及其优缺点对比分析》MySQL中的表锁、页面锁和行锁各有特点,适用于不同的场景,表锁锁定整个表,适用于批量操作和MyISAM存储引擎,页面锁锁定数据页,适用于旧版本... 目录1. 表锁(Table Lock)2. 页面锁(Page Lock)3. 行锁(Row Lock

linux打包解压命令方式

《linux打包解压命令方式》文章介绍了Linux系统中常用的打包和解压命令,包括tar和zip,使用tar命令可以创建和解压tar格式的归档文件,使用zip命令可以创建和解压zip格式的压缩文件,每... 目录Lijavascriptnux 打包和解压命令打包命令解压命令总结linux 打包和解压命令打

linux如何复制文件夹并重命名

《linux如何复制文件夹并重命名》在Linux系统中,复制文件夹并重命名可以通过使用“cp”和“mv”命令来实现,使用“cp-r”命令可以递归复制整个文件夹及其子文件夹和文件,而使用“mv”命令可以... 目录linux复制文件夹并重命名我们需要使用“cp”命令来复制文件夹我们还可以结合使用“mv”命令总

CSS3 最强二维布局系统之Grid 网格布局

《CSS3最强二维布局系统之Grid网格布局》CS3的Grid网格布局是目前最强的二维布局系统,可以同时对列和行进行处理,将网页划分成一个个网格,可以任意组合不同的网格,做出各种各样的布局,本文介... 深入学习 css3 目前最强大的布局系统 Grid 网格布局Grid 网格布局的基本认识Grid 网