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
  • 等等
相关推荐
持力行8 小时前
从C struct到C++中的class
c语言·c++
不能跑的代码不是好代码9 小时前
Linux系统常用命令中文速查表
linux·运维·服务器
石一峰69910 小时前
深入理解 Linux 中断三层机制与 1-Wire 时序锁原理
linux·运维·服务器
无限码农11 小时前
Linux上通过cmake编译uchardet
linux·运维·服务器
运维大师12 小时前
【Linux运维极简教程】06-网络配置与管理
linux·运维
2501_9142459313 小时前
C语言设计模式详解:从理论到实践的完整指南
c语言·开发语言·设计模式
探索云原生14 小时前
终于搞懂 Kueue:5 个核心对象一次讲透
linux·docker·ai·云原生·kubernetes
An_s15 小时前
机器学习python之识别图中物品信息
java·linux·开发语言
2501_9142459315 小时前
C语言与硬件交互:从GPIO到中断的嵌入式编程实践
c语言·单片机·交互
Championship.23.2416 小时前
Linux 3.0 LVDS驱动开发详解
linux·运维·驱动开发·lvds