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
  • 等等
相关推荐
AI视觉网奇1 分钟前
麒麟系统 doc转pdf
linux·运维·pdf
1candobetter24 分钟前
软考中级习题与解答——第三章_操作系统(1)
linux·运维·服务器
淮北49439 分钟前
计算机网络学习(六、应用层)
linux·学习·计算机网络
lew-yu1 小时前
【已解决】Linux中程序脚本可以手动执行成功,但加在rc.local中不能开机自启
linux·服务器
ajassi20001 小时前
linux C 语言开发 (四) linux系统常用命令
linux·运维·服务器
小嵌同学1 小时前
Linux:malloc背后的实现细节
大数据·linux·数据库
EveryPossible1 小时前
如何终止画图
linux·编辑器·vim
CYRUS_STUDIO2 小时前
LLVM 不止能编译!自定义 Pass + 定制 clang 实现函数名加密
c语言·c++·llvm
CYRUS_STUDIO2 小时前
OLLVM 移植 LLVM 18 实战,轻松实现 C&C++ 代码混淆
c语言·c++·llvm
wangjialelele2 小时前
Linux匿名管道和命名管道以及共享内存
linux·运维·网络