进程链表示意图(linux-2.6.38内核)
内核中对应结构体。
- struct task_struct {
- ...;
- struct list_head tasks;
- ...;
- };
- struct list_head {
- struct list_head *next,*prev;
- };
----------------------------------------------------------------------------------------
编写内核模块遍历双向循环链表。(逆序的方式,pid从大到小)
- #include<linux/module.h>
- #include<linux/init.h>
- #include<linux/sched.h>
- MODULE_LICENSE("GPL");
- #ifndef offsetof
- #define offsetof(type, field) ((long) &((type *)0)->field)
- #endif /* offsetof */
-
- #ifndef container_of
- #define container_of(ptr, type, member) ({ \
- const typeof( ((type *)0)->member ) *__mptr = (ptr); \
- (type *)( (char *)__mptr - offsetof(type,member) );})
- #endif
- static __init int process_init(void)
- {
- struct task_struct *task = NULL,*task_ptr = NULL;
- struct list_head *p = NULL;
- printk("<0> the pid = %d\n",init_task.pid);//init_task(0号进程的PCB 常驻内存(内核数据区))
- task = &init_task;
- p = &(task->tasks);
- for (p = &(task->tasks);(p=(p->prev)) != &(task->tasks);) {
- task_ptr = container_of(p,struct task_struct,tasks);
- printk("%d------------------>%s\n",task_ptr->pid,task_ptr->comm);
- }
- return 0;
- }
- static __exit void process_exit(void)
- {
- printk("<0> bye! \n");
- }
- module_init(process_init);
- module_exit(process_exit);
阅读(1446) | 评论(0) | 转发(0) |