Chinaunix首页 | 论坛 | 博客
  • 博客访问: 899159
  • 博文数量: 119
  • 博客积分: 2493
  • 博客等级: 大尉
  • 技术积分: 2363
  • 用 户 组: 普通用户
  • 注册时间: 2012-06-03 14:00
文章分类

全部博文(119)

文章存档

2013年(19)

2012年(100)

分类: LINUX

2012-06-03 15:46:30

进程链表示意图(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);
阅读(2740) | 评论(1) | 转发(3) |
给主人留下些什么吧!~~

Aquester2012-06-04 13:03:59

可以看看这个(Linux内核list&hlist解读):http://blog.chinaunix.net/uid-20682147-id-3160772.html