linux内核模块遍历获取进程列表,使用list_entry()

使用方法:

编写Makefile如下:

obj-m +=pscmd.o
all:
        make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
        make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

 

然后执行如下命令

insmod pscmd

cat /proc/pscmd

然后就会显示出进程列表

 

 struct task_struct {
    
//xxxxxxx
    struct hlist_head preempt_notifiers;
    
struct list_head rcu_node_entry;
    
struct list_head tasks;
    
struct list_head children;    /* list of my children */
    
struct list_head sibling;    /* linkage in my parent's children list */
    
struct list_head ptraced;
    
struct list_head ptrace_entry;
    
struct list_head thread_group;
    
//还有好多list,不抄了……
}

linux的task_struct(进程PCB)组织如上(只列出list_head部分),可以看到struct list_head tasks; 正是用这个结构将所有进程的PCB链接成了一个链表,结构定义如下:

struct list_head {
    
struct list_head *next, *prev;
};

图示如下:

所以遍历进程列表需要先找到一个进程的PCB首指针p,然后由p->tasks来遍历链表,遍历的时候使用宏list_entry来获取PCB首指针

list_entry(ptr,type,member)宏,

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值