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

相关文章

Linux脚本(shell)的使用方式

《Linux脚本(shell)的使用方式》:本文主要介绍Linux脚本(shell)的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录概述语法详解数学运算表达式Shell变量变量分类环境变量Shell内部变量自定义变量:定义、赋值自定义变量:引用、修改、删

MyBatis Plus 中 update_time 字段自动填充失效的原因分析及解决方案(最新整理)

《MyBatisPlus中update_time字段自动填充失效的原因分析及解决方案(最新整理)》在使用MyBatisPlus时,通常我们会在数据库表中设置create_time和update... 目录前言一、问题现象二、原因分析三、总结:常见原因与解决方法对照表四、推荐写法前言在使用 MyBATis

Python主动抛出异常的各种用法和场景分析

《Python主动抛出异常的各种用法和场景分析》在Python中,我们不仅可以捕获和处理异常,还可以主动抛出异常,也就是以类的方式自定义错误的类型和提示信息,这在编程中非常有用,下面我将详细解释主动抛... 目录一、为什么要主动抛出异常?二、基本语法:raise关键字基本示例三、raise的多种用法1. 抛

Linux链表操作方式

《Linux链表操作方式》:本文主要介绍Linux链表操作方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、链表基础概念与内核链表优势二、内核链表结构与宏解析三、内核链表的优点四、用户态链表示例五、双向循环链表在内核中的实现优势六、典型应用场景七、调试技巧与

基于Python实现一个简单的题库与在线考试系统

《基于Python实现一个简单的题库与在线考试系统》在当今信息化教育时代,在线学习与考试系统已成为教育技术领域的重要组成部分,本文就来介绍一下如何使用Python和PyQt5框架开发一个名为白泽题库系... 目录概述功能特点界面展示系统架构设计类结构图Excel题库填写格式模板题库题目填写格式表核心数据结构

详解Linux中常见环境变量的特点与设置

《详解Linux中常见环境变量的特点与设置》环境变量是操作系统和用户设置的一些动态键值对,为运行的程序提供配置信息,理解环境变量对于系统管理、软件开发都很重要,下面小编就为大家详细介绍一下吧... 目录前言一、环境变量的概念二、常见的环境变量三、环境变量特点及其相关指令3.1 环境变量的全局性3.2、环境变

github打不开的问题分析及解决

《github打不开的问题分析及解决》:本文主要介绍github打不开的问题分析及解决,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、找到github.com域名解析的ip地址二、找到github.global.ssl.fastly.net网址解析的ip地址三

Linux系统中的firewall-offline-cmd详解(收藏版)

《Linux系统中的firewall-offline-cmd详解(收藏版)》firewall-offline-cmd是firewalld的一个命令行工具,专门设计用于在没有运行firewalld服务的... 目录主要用途基本语法选项1. 状态管理2. 区域管理3. 服务管理4. 端口管理5. ICMP 阻断

Linux实现线程同步的多种方式汇总

《Linux实现线程同步的多种方式汇总》本文详细介绍了Linux下线程同步的多种方法,包括互斥锁、自旋锁、信号量以及它们的使用示例,通过这些同步机制,可以解决线程安全问题,防止资源竞争导致的错误,示例... 目录什么是线程同步?一、互斥锁(单人洗手间规则)适用场景:特点:二、条件变量(咖啡厅取餐系统)工作流

Linux中修改Apache HTTP Server(httpd)默认端口的完整指南

《Linux中修改ApacheHTTPServer(httpd)默认端口的完整指南》ApacheHTTPServer(简称httpd)是Linux系统中最常用的Web服务器之一,本文将详细介绍如何... 目录一、修改 httpd 默认端口的步骤1. 查找 httpd 配置文件路径2. 编辑配置文件3. 保存