struct task_struct *p;
char buff[1024];
if(offset>0)
return 0;
//加锁(SMP)
read_lock(&tasklist_lock);
for_each_process(p)
{
sprintf(buff,"%d\t%d\t\t%s\n",p->pid,p->parent->pid,p->comm);
}
//解锁
read_unlock(&tasklist_lock);
for_each_process是在linux/sched.h中的宏定义:
#define for_each_process(p) for (p = &init_task ; (p = next_task(p)) != &init_task ; )
作用是从init_task遍历所有的进程描述符。
当前进程
linux内核是如何获取当前进程的任务结构指针的,以下代码均参照linux内核2.4.0的源码。
在include\asm-i386\ current.h中
#ifndef _I386_CURRENT_H
#define _I386_CURRENT_H
struct task_struct;
static inline struct task_struct * get_current(void)
{
struct task_struct *current;
__asm__("andl %%esp,%0; ":"=r" (current) : "0" (~8191UL));
return current;
}
#define current get_current()
#endif /* !(_I386_CURRENT_H) */