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
  • 等等
相关推荐
十年编程老舅10 分钟前
Linux DMA 技术深度拆解
linux·网络·linux内核·dma·c/c++·内存访问
weiabc16 分钟前
今日C/C++学习笔记20260223
c语言·c++·学习
jianqiang.xue19 分钟前
ESP32-S3 运行 Linux 全指南:从 RISC-V 模拟器移植到 8 秒快速启动
linux·stm32·单片机·mongodb·risc-v·esp32s3
bing_feilong23 分钟前
Ubuntu Tips
linux·运维
~光~~26 分钟前
【嵌入式linux学习】0_3位运算整理
linux·学习
悲伤小伞31 分钟前
9-MySQL_索引
linux·数据库·c++·mysql·centos
SeanDe31 分钟前
【Linux `top` 命令详解(结合截图逐行拆解)】
linux·运维·服务器
mi200632 分钟前
wiki.js知识库系统搭建和配置总结
linux·运维·知识库
busideyang44 分钟前
STC8H单片机delay_ms函数闪烁不准?原因是参数溢出!
c语言·单片机·嵌入式硬件·嵌入式
向依阳1 小时前
Linux应用-----进程间通信
linux