Linux 内核的 current

1. 进程结构体

task_struct 是描述 Linux 进程的一个结构体,记录着进程的信息

cpp 复制代码
// include/linux/sched.h
struct task_struct {
	volatile long state;	/* -1 unrunnable, 0 runnable, >0 stopped */
	void *stack;
	atomic_t usage;
	unsigned int flags;	/* per process flags, defined below */
	unsigned int ptrace;
	.....
	.....
#ifdef CONFIG_UPROBES
	struct uprobe_task *utask;
#endif
#if defined(CONFIG_BCACHE) || defined(CONFIG_BCACHE_MODULE)
	unsigned int	sequential_io;
	unsigned int	sequential_io_avg;
#endif
#ifdef CONFIG_DEBUG_ATOMIC_SLEEP
	unsigned long	task_state_change;
#endif
};

2. current

current 宏其实就是静态函数 get_current()。它的返回值是一个 struct task_struct

c 复制代码
static inline struct task_struct *get_current(void)
{
	return current_thread_info()->task;
}

#define current get_current()

通过调用 current,我们可以获取当前的进程的信息:

  • current->comm: 进程的名字
  • current->pid: 进程的 pid
  • 等等
相关推荐
楼兰公子16 小时前
读取rpi摄像头
linux·服务器·算法
李景琰16 小时前
Debian12安装配置Mqtt之EMQX
linux·运维·服务器
测试员周周16 小时前
【AI测试系统】第1篇:LangGraph 实战:用 State Graph 搭建 AI测试流水线(4 步编排 + RAG 增强 + 完整代码)
linux·windows·python·功能测试·microsoft·单元测试·多轮对话
不做无法实现的梦~17 小时前
PX4 机载电脑 Linux 环境安装、串口、网络、ROS 完整配置
linux·运维·网络
嵌入式×边缘AI:打怪升级日志17 小时前
嵌入式Linux开发(了解交叉编译工具链的组成)
java·linux·运维
李小白2020020217 小时前
RK3568 linux6.1 死机
linux·运维·服务器
FreeGo~17 小时前
Linux 系统编程 进程篇 (五)
java·linux·服务器
杨云龙UP17 小时前
Oracle数据库启动失败:ORA-29701、ORA-01565、ORA-17503故障处理记录_20260429
linux·运维·数据库·oracle·centos
handler0117 小时前
Git 核心指令速查
linux·c语言·c++·笔记·git·学习
学会去珍惜17 小时前
学会C语言可以做什么
c语言·网络编程·游戏开发·嵌入式系统·系统编程