Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1390565
  • 博文数量: 120
  • 博客积分: 182
  • 博客等级: 入伍新兵
  • 技术积分: 2278
  • 用 户 组: 普通用户
  • 注册时间: 2012-07-19 16:31
文章分类

全部博文(120)

文章存档

2015年(12)

2014年(13)

2013年(40)

2012年(55)

分类:

2012-10-22 20:11:23

原文地址:进程PCB的双向循环链表 作者:zhe_wang

进程链表示意图(linux-2.6.38内核)

  内核中对应结构体。

  1. struct task_struct {
  2.    ...;
  3.     struct list_head tasks;
  4.    ...;
  5. };

  6. struct list_head {
  7.     struct list_head *next,*prev;
  8. };

----------------------------------------------------------------------------------------
编写内核模块遍历双向循环链表。(逆序的方式,pid从大到小)

  1. #include<linux/module.h>
  2. #include<linux/init.h>
  3. #include<linux/sched.h>

  4. MODULE_LICENSE("GPL");

  5. #ifndef offsetof
  6. #define offsetof(type, field) ((long) &((type *)0)->field)
  7. #endif /* offsetof */
  8.  
  9. #ifndef container_of
  10. #define container_of(ptr, type, member) ({ \
  11.         const typeof( ((type *)0)->member ) *__mptr = (ptr); \
  12.         (type *)( (char *)__mptr - offsetof(type,member) );})
  13. #endif

  14. static __init int process_init(void)
  15. {
  16.     struct task_struct *task = NULL,*task_ptr = NULL;
  17.     struct list_head *p = NULL;
  18.     printk("<0> the pid = %d\n",init_task.pid);//init_task(0号进程的PCB 常驻内存(内核数据区))
  19.     task = &init_task;
  20.     p = &(task->tasks);
  21.     for (p = &(task->tasks);(p=(p->prev)) != &(task->tasks);) {
  22.      task_ptr = container_of(p,struct task_struct,tasks);
  23.      printk("%d------------------>%s\n",task_ptr->pid,task_ptr->comm);
  24.     }

  25.     return 0;
  26. }


  27. static __exit void process_exit(void)
  28. {
  29.     printk("<0> bye! \n");
  30. }

  31. module_init(process_init);
  32. module_exit(process_exit);
阅读(1386) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~