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
  • 等等
相关推荐
Dlrb12117 分钟前
Linux系统编程-线程与多线程模块的封装
linux·线程·互斥锁·线程同步·线程互斥
拾贰_C8 分钟前
【Ubuntu | VSCode | SSH | 远程连接 | Linux】VSCode 怎么实现ssh远程连接
linux·vscode·ubuntu
一叶知秋dong25 分钟前
llama.cpp 启动脚本
linux·服务器·llama
桌面运维家30 分钟前
校园机房vDisk IDV云桌面建设方案价格参考
linux·服务器·数据库
你疯了抱抱我34 分钟前
【STM32】配置vscode+C工具链+Cortex-Debug开发环境,IC:STM32F411CEU6
c语言·vscode·stm32
Cx330❀38 分钟前
【MySQL基础】库与表的全面操纵指南
linux·服务器·网络·数据库·c++·mysql
凡人叶枫40 分钟前
Effective C++ 条款03:尽可能使用 const
linux·开发语言·c++·嵌入式开发
程序员佳佳42 分钟前
我在 Windows 和低配 Linux 上做 RAG:Milvus、FAISS、向量 API 中转的中立实测
linux·人工智能·windows·gpt·aigc·milvus·faiss
加成BUFF1 小时前
第六天 ROS 《Action 通信实验》
linux·机器人·ros
FFZero11 小时前
[mpv脚本系统] (五) C层系统调用的实现: mpv client通信机制
c语言·音视频