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
  • 等等
相关推荐
无敌昊哥战神1 小时前
深入理解 C 语言:巧妙利用“0地址”手写 offsetof 宏与内存对齐机制
c语言·数据结构·算法
一叶知秋yyds1 小时前
Ubuntu 虚拟机安装 OpenClaw 完整流程
linux·运维·ubuntu·openclaw
cmpxr_3 小时前
【C】数组名、函数名的特殊
c语言·算法
楠奕3 小时前
CentOS7安装GoldenDB单机搭建及常见报错解决方案
linux·运维·服务器
剑锋所指,所向披靡!4 小时前
Linux常用指令(2)
linux·运维·服务器
不愿透露姓名的大鹏4 小时前
Oracle归档日志爆满急救指南
linux·数据库·oracle·dba
W.W.H.4 小时前
嵌入式常见的面试题1
linux·网络·经验分享·网络协议·tcp/ip
木白CPP4 小时前
DMA-Buffer内核驱动API文档
linux
itman3015 小时前
C语言怎么学?从写程序到玩指针的实操攻略
c语言·指针·结构体·编程学习·资源推荐
HXQ_晴天5 小时前
Linux 系统的交互式进程监控工具htop
linux·服务器·网络