一、方法
/* sched.h */
Struct task_struct {
…
list_head children;
list_head sibling;
…
};
1)
/* traver every process */
struct task_struct *task;
for_each_process( task )
{
/* handle the process info */
}
2)
/* traver the children of the init process */
struct task struct *task;
struct list head *list;
list for each(list,&init_task.children) {
task = list entry(list, struct task struct, sibling);
/* task points to the next child in the list */
}
二、实现
1) 简单遍历所有进程,直接使用for_each_process()即可;
do_ps( void )
{
struct task_struct *task;
printk( KERN_INFO "%-5s %-9s %s\n", "PID","STATE", "CMD" );
for_each_process( task )
{
printk( KERN_INFO "%-5d %-9c %s\n", task->pid,get_status_text( task->state ), task->comm );
}