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

例子来自:http://www.ibm.com/developerworks/cn/linux/l-linux-process-management/

#include
#include
#include

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

你可能感兴趣的:(kernel分析,内核驱动)