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- 等等