内核模块遍历进程

本文介绍了如何在Linux环境下,通过内核模块实现对进程的遍历。首先,创建了sched_class.o模块目标文件,并确定了当前工作路径及Linux内核源代码路径。接着,使用make命令在内核源代码路径下编译模块,最后提供了清理编译产物的步骤。
摘要由CSDN通过智能技术生成

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>  
#include <linux/sched.h>
#include <linux/list.h>  


int __init sched_fun_init()
{
	struct task_struct *p, *ts = &init_task;
	struct list_head *pos;
	int count = 0;

	list_for_each(pos, &ts->tasks) {
		p = list_entry(pos, struct task_struct,tasks);
		count++;
		printk("<1> pid = %ld----%s\n", p->pid, p->comm);
	}

	printk("<1> count = %d\n", count);
}


void __init sched_fun_exit()
{
	printk("<1> sched_fun_exit\n");
}


module_init(sched_fun_init);
module_exit(sched_fun_exit);



obj-m += sched_class.o        # 产生hellomod 模块的目标文件
CURRENT_PATH := $(shell pwd)   #模块所在的当前路径
LINUX_KERNEL := $(shell uname -r)    #Linux内核源代码的当前版本
LINUX_KERNEL_PATH := /usr/src/linux-$(LINUX_KERNEL) #Linux内核源代码的绝对路径
all:
        make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) modules   #编译模块了
clean:
        make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) clean    #清理

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值