您的位置:首页 > 运维架构 > Linux

一个Linux内核利用init_task进行进程管理的简单例子

2014-11-15 12:37 716 查看
例子来自:http://www.ibm.com/developerworks/cn/linux/l-linux-process-management/

#include <linux/kernel.h>

#include <linux/module.h>

#include <linux/sched.h>

int init_module( void )

{

/* Set up the anchor point */

struct task_struct *task = &init_task;

/* Walk through the task list, until we hit the init_task again */

do {

printk( KERN_INFO "*** %s [%d] parent %s\n",

task->comm, task->pid, task->parent->comm );

} while ( (task = next_task(task)) != &init_task );

printk( KERN_INFO, "Current task is %s [%d], current->comm, current->pid );

return 0;

}

void cleanup_module( void )

{

return;

}

上面例子作用类似ps一样。

循环结构还可以改成以下,使用更方便:

for_each_process(task)

{

printk(KERN_INFO "=== %s [%d] parent %s\n",task->comm,task->pid,task->parent->comm);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐