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

来源:互联网 发布:怎么找淘宝客服工作 编辑:程序博客网 时间:2024/05/16 13:44

#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);
    }

阅读全文
0 0
原创粉丝点击