实例要求:

编写一个Linux的内核模块,其功能是遍历操作系统所有进程。该内核模块输出系统中:每个进程的名字、进程pid、进程的状态、父进程的名字;以及统计系统中进程个数,包括统计系统中TASK_RUNNING、TASK_INTERRUPTIBLE、TASK_UNINTERRUPTIBLE、TASK_ZOMBIE、TASK_STOPPED等(还有其他状态)状态进程的个数。同时还需要编写一个用户态下执行的程序,显示内核模块输出的内容。

学习资料:
《边干边学—Linux内核指导》
ArchLinux Wiki

解决方案:

#include <linux/module.h> //needed by all modules
#include <linux/kernel.h> //needed for KERN_INFO
#include <linux/sched.h> //defined struct task_struct
#include <linux/proc_fs.h> //create proc file
#include <linux/seq_file.h> //use seq_file#define FILENAME "osexp" //defile proc file nameint myshow(struct seq_file *,void *);
int myopen(struct inode *,struct file *);/* custom the operations to the seq_file */
static const struct file_operations myops = {.owner = THIS_MODULE,.open = myopen,.read = seq_read,.release = single_release
};
/* do when open the seq file */
int myopen(struct inode *inode,struct file *file){single_open(file,myshow,NULL);//bind seq_file with myshow functionreturn 0;
}
/* create proc file */
int init_proc(void){struct proc_dir_entry * myfile;myfile = proc_create(FILENAME,0444,NULL,&myops);//create proc fileif(myfile == NULL) //deal with errorreturn -ENOMEM;return 0;
}
/* remove proc file */
void remove_proc(void){remove_proc_entry(FILENAME,NULL);//remove proc fileprintk(KERN_INFO "[m] proc file:%s removed\n",FILENAME);//print debug message
}/*description: output process's info to log */
int myshow(struct seq_file *file,void *v){int num_running = 0; //the number of process whose status is runningint num_interruptible = 0; //the number of process whose status is interruptibleint num_uninterruptible = 0; //the ... status is uninterruptibleint num_zombie = 0; //the process exited with status zombieint num_stopped = 0; //the ... status is stoppedint num_traced = 0; //the ... status is tracedint num_dead = 0; //the process has deaded;int num_unknown = 0; //the process whose status is unknownint num_total = 0; //the total number of processint t_exit_state; //temp var to store task_struct.exit_stateint t_state; //temp var to store task_struct.statestruct task_struct *p; //pointer to task_struct//printk("[m] All processes' info:\n");//print boot infoseq_printf(file,"[m] All processes' info:\n");for(p=&init_task;(p=next_task(p))!=&init_task;){ //go througn the linklist//printk(KERN_INFO "[m] Name:%s Pid:%d State:%ld ParName:%s\n",p->comm,p->pid,p->state,p->real_parent->comm); //print the process's info to logseq_printf(file,"[m] Name:%s Pid:%d State:%ld ParName:%s\n",p->comm,p->pid,p->state,p->real_parent->comm);num_total++; //total number of process plus onet_state = p->state; //put p->state to variable t_statet_exit_state = p->exit_state;//similar to aboveif(t_exit_state!=0){ //if the process has exitedswitch(t_exit_state){case EXIT_ZOMBIE://if the exit state is zombienum_zombie++;//variable plus onebreak; //break switch case EXIT_DEAD://if the exit state is deadnum_dead++;//variable plus onebreak;//break switchdefault: //other casebreak;//break switch}}else{ // if the proess hasn't exitedswitch(t_state){case TASK_RUNNING://if the state is runningnum_running++;//variable plus onebreak;//break switchcase TASK_INTERRUPTIBLE://state is interruptiblenum_interruptible++;//variable plus onebreak;//break switchcase TASK_UNINTERRUPTIBLE://state is uninterruptiblenum_uninterruptible++;//var + 1break;//break switchcase TASK_STOPPED://state is stoppednum_stopped++;//var +1break;//break switchcase TASK_TRACED://state is tracednum_traced++;//var +1break;//break switchdefault://other casenum_unknown++;break;}}}//below instruction is to print the statistics result in above code// printk(KERN_INFO "[m] total tasks: %10d\n",num_total);// printk(KERN_INFO "[m] TASK_RUNNING: %10d\n",num_running);// printk(KERN_INFO "[m] TASK_INTERRUPTIBLE: %10d\n",num_interruptible);// printk(KERN_INFO "[m] TASK_UNINTERRUPTIBLE: %10d\n",num_uninterruptible);// printk(KERN_INFO "[m] TASK_TRACED: %10d\n",num_stopped);// printk(KERN_INFO "[m] TASK_TRACED: %10d\n",num_stopped);// printk(KERN_INFO "[m] EXIT_ZOMBIE: %10d\n",num_zombie);// printk(KERN_INFO "[m] EXIT_DEAD: %10d\n",num_dead);// printk(KERN_INFO "[m] UNKNOWN: %10d\n",num_unknown);seq_printf(file,"[m] total tasks: %10d\n",num_total);seq_printf(file,"[m] TASK_RUNNING: %10d\n",num_running);seq_printf(file,"[m] TASK_INTERRUPTIBLE: %10d\n",num_interruptible);seq_printf(file,"[m] TASK_UNINTERRUPTIBLE: %10d\n",num_uninterruptible);seq_printf(file,"[m] TASK_TRACED: %10d\n",num_stopped);seq_printf(file,"[m] TASK_TRACED: %10d\n",num_stopped);seq_printf(file,"[m] EXIT_ZOMBIE: %10d\n",num_zombie);seq_printf(file,"[m] EXIT_DEAD: %10d\n",num_dead);seq_printf(file,"[m] UNKNOWN: %10d\n",num_unknown);return 0;
} int init_module(void){// init the kernel moduleprintk(KERN_INFO "[m] exp_process started\n");//print start messagereturn init_proc();//create proc file
}void cleanup_module(void){ //clean up the resources when module finishedremove_proc();//remove proc fileprintk(KERN_INFO "[m] exp_process finished\n");//print finish message
}MODULE_LICENSE("GPL");//show that this code follow GUN General Public License

archlinux 下的makefile

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

[Linux] 内核模块proc使用 实例:统计所有进程的信息相关推荐

  1. linux 内核模块 proc,Linux 内核模块 proc文件系统.pdf

    Linux内核模块 proc文件系统 Linux内核模块 §  内核模块是内核的扩展,它供了在内核运 行过程中动态加载的特性. §  模块被加载进系统后,就在内核态下运行了, 成了内核的一部分,可以读 ...

  2. 嵌入式 linux下proc目录下的文件详解

    http://blog.csdn.net/skdkjzz/article/details/19566717 目录 ----------------- 0 序言 0.1 简介 0.2 责任 1 收集系统 ...

  3. Linux中/proc/pid/status信息含义

    原文:https://blog.csdn.net/bugouyonggan/article/details/24349883 其实在认真阅读了这篇名为"计算内存使用"的文章之后,还 ...

  4. linux下查看运行进程详细信息

    通过ps及top命令查看进程信息时,只能查到相对路径,查不到的进程的详细信息,如绝对路径等.这时,我们需要通过以下的方法来查看进程的详细信息: Linux在启动一个进程时,系统会在/proc下创建一个 ...

  5. linux关闭io统计,linux 统计每个进程所占用的io数

    linux 统计每个进程所占用的io数 (2012-06-29 19:44:49) 标签: it 在新版本的内核,可以用iotop来实时的看到io的情况.但是在老版本的内核没有此工具,怎么办呢从网上搜 ...

  6. linux统计所有进程总共占用多少内存?

    原文地址:http://linuxperf.com/?p=143 很多人通过累加 "ps  aux" 命令显示的 RSS 列来统计全部进程总共占用的物理内存大小,这是不对的.RSS ...

  7. 利用Linux的/proc/stat获取指定进程的cpu占有率及内存

    利用Linux的 /proc/stat计算进程的cpu占有率和内存 计算的是进程多个cpu的平均占有率,并不是每个cpu的,所以和top命令中的不一样 getinfo.cpp 1 #include&l ...

  8. Linux内核编译(通过内核模块显示进程控制块信息)

    Linux内核编译(通过内核模块显示进程控制块信息) 实验说明 在内核中,所有进程控制块都被一个双向链表连接起来,该链表中的第一个进程控制块为init_task.编写一个内核模块,模块接收用户传递的一 ...

  9. 【Linux 内核 内存管理】内存管理架构 ⑤ ( sbrk 内存分配系统调用代码示例 | 在 /proc/pid/maps 中查看进程堆内存详情 )

    文章目录 一.sbrk 内存分配系统调用代码示例 二.在 /proc/pid/maps 中查看进程堆内存详情 本篇博客调用 sbrk 系统调用函数 , 申请并修改 堆内存 , 并在 /proc/pid ...

最新文章

  1. 展望2021年:智能机器人可监督工业机器人干活,效率提升30%
  2. 对超线程几个不同角度的解释
  3. 进阶必备:素数筛法(欧拉,埃氏筛法)
  4. python和c哪个适合入门-关于python和c语言学哪个好
  5. python如何导入txt文件-数据从txt文本导入python
  6. matlab中大figure怎样修改,操作Matlab的Figure窗口(一)
  7. Java算法--冒泡排序
  8. ADO连接各种数据库
  9. 【CSS3】CSS3背景相关属性大全
  10. SQL查询单表数据之排序(二)
  11. 云栖科技评论第57期:技术拓展科学边界 科学激发技术创新
  12. String.raw()方法
  13. JSON(1)--- 语法
  14. 一次解决DB2接口文件到Oracle无法导入问题的经历
  15. 发那科机器人寄存器Ar_浅谈发那科机器人与TP参数之间的关系
  16. windows 下安装 sns 学习研究
  17. ae 导出html5,AE脚本-导出json格式的Web动画工具 Bodymovin v5.7.0 + 使用教程
  18. 【MATLAB】机器学习:线性判别分析LDA
  19. 回撤率 python 平台_python夏普率、最大回撤计算
  20. 算法 树7 二叉搜索树的操作集

热门文章

  1. SAP License:SAP S/4HANA Cloud [ERP 云]
  2. SAP License:SAP学习笔记-集成与核算
  3. 超市百货电商app移动端原型+通用模块全局规则说明+超市电商后台管理web端原型+超市电商产品原型及需求文档+业务后台(商品管理+广告管理+活动管理)
  4. 注册表的监控 -- WIN9X
  5. 课时4:改进我们的小游戏
  6. Storm Trident示例shuffleparallelismHint
  7. python encode和decode函数说明
  8. BZOJ 2820: YY的GCD
  9. ORACLE登录错误的解决方法
  10. [HDU] 1533 Going Home