Linux系统下通过内核模块显示进程控制块信息
前言
本机为微软Surface pro4
,为64
位,所用操作系统为Windos 10
。本机虚拟机版本为Oracle VM VirtualBox 6.1.8
,所用操作系统是使用Ubuntu18.04
,。Ubuntu的虚拟硬盘设置为200G
,显存为128MB
,内存为4G
,CPU
2个,所用镜像源为清华大学软件镜像源。所使用linux
内核为linux-5.11.8
。
注意事项
(1)使用本博客中的指令时一定要采取对应自己电脑安装的Ubuntu配置。
(2)内核模块在内核空间运行,编写时不能使用C库函数,不能使用浮点数运算。
(3)做实验时直接上手操作即可,如果报错了再去找问题。
(4)本实验编译时如果编译器使用的Geany
时,尽量使用终端指令进行编译,因为Geany
进行编译会因为很多原因报错。
方案一:访问/proc目录(用户空间编程)
1、基本思路
2、编程思路
3、在Ubuntu
中使用Geany
书写实现程序。(注:实现程序如下)
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<sys/types.h>
#include<netdb.h>
#include<pthread.h>
#include<unistd.h>
#include<dirent.h>
char default_path[1024]="/proc/"; //定义大小为1024的字符数组
int s=0; //定义全局变量
//定义结构体
typedef struct file_info
{
int pid; // 进程号
int ppid; // 父进程号
char name[1024]; // 进程名称
int flag; //进程标志
int rec; //打印进程树时用来标志是几级进程的
}info;
int my_getpid(char *str) // 获得进程号
{
int len=strlen(str); //获得进程号的有效长度
char num[10]; //
int i,j,ret;
if(strncmp(str,"Pid",3)==0) //判断传过来的字符串是否为Pid
{
//确定进程第一个数字位置
for(i=0;i<len;i++)
{
if(str[i]>='0'&&str[i]<='9') //判断进程号是否为有效数字
break;
}
//将进程号赋值给num[]数组
for(j=0;j<len-i;j++)
{
num[j]=str[i+j];
}
ret=atoi(num); //将字符串转换成整数
}
else ret=0;
return ret;
}
int my_getppid(char *str) // 获得父进程号(与获得进程号函数思路一致)
{
int len=strlen(str);
char num[10];
int i,j,ret;
if(strncmp(str,"PPid",4)==0)
{
for(i=0;i<len;i++)
{
if(str[i]>='0'&&str[i]<='9')
break;
}
for(j=0;j<len-i;j++)
{
num[j]=str[i+j];
}
ret=atoi(num);
}
else ret=0;
return ret;
}
int child_exist(info *file,int count,int ppid) //判断是否存在子进程,count代表子进程的数量
{
int i;
//判断该子进程是否存在
for(i=0;i<count;i++)
{
if(file[i].flag==0&&file[i].ppid==ppid)
return 1;
}
return 0;
}
void print_pstree(info *file,int count,int ppid,int rec) // 打印进程树,用递归方法,中序遍历
{
int i,j,k;
for(i=0;i<count;i++)
{
if(file[i].flag==0&&file[i].ppid==ppid)
{
file[i].rec=rec+1;
file[i].flag=1;
for(k=0;k<rec;k++)
printf(" "); //输出空格即可表示进程的级数
printf("%s\n",file[i].name);
print_pstree(file,count,file[i].pid,file[i].rec);
}
}
}
int main()
{
int i,j,k,total,s1,s2,count,t;
char str[1024],dir[1024];
struct dirent **namelist;
strcpy(dir,default_path); //将/proc目录名复制给dir数组
total=scandir(dir,&namelist,0,alphasort); //将dir目录下的文件复制到namelist数组中并且排列目录按名称字母
//成功则返回复制到namelist数组中的数据结构数目,有错误发生则返回-1
printf("path=%s,total=%d\n",dir,total);
for(i=0;i<total;i++)
{
strcpy(str,namelist[i]->d_name); //将目录内文件名字复制给str数组
if(str[0]>='0'&&str[0]<='9') //如果是进程号
count++;
}
//打印进程总数
printf("进程数:%d\n",count);
info file[1024];
i=0;
t=0;
while(i<total)
{
FILE *fp;
char path[1024],name[1024];
int pid,ppid;
strcpy(str,namelist[i]->d_name);
strcpy(path,default_path);
if(str[0]>='0'&&str[0]<='9')
{
strcat(path,str); //连接字符串
strcat(path,"/status"); //连接字符串创建路径
fp=fopen(path,"r"); //按照只读打开路径所示文件
while(!feof(fp))
{
fgets(str,1024,fp);
//pid
if((s1=my_getpid(str))!=0)
pid=s1;
//ppid
if((s2=my_getppid(str))!=0)
ppid=s2;
//name
if(strncmp(str,"Name",4)==0) //判断字符串str的前4位是不是与Name相符
{
for(j=4;j<strlen(str);j++) //判断是不是英文字符
{
if(str[j]>='a'&&str[j]<='z')
break;
}
for(k=j;k<strlen(str);k++)
{
name[k-j]=str[k];
}
name[k-j-1]='\0';
}
//设置file结构体数组中的信息
file[t].pid=pid;
file[t].ppid=ppid;
strcpy(file[t].name,name);
}
fclose(fp);
t++;
}
i++;
}
memset(&file->flag,0,count); //设置flag为0
memset(&file->rec,0,count); //设置rec为0
print_pstree(file,count,0,0); //打印进程树
}
4、在Ubuntu
中保存Geany
中书写的程序。
(步骤一:使用指令mkdir pstree
在主目录下创建文件夹 pstree
)
(步骤二:将程序保存到pstree
文件夹中)
5、按照如图所示执行指令即可实现打印进程树。
方案二:访问PCB结构方案(内核空间编程)
1、基本思路
2、编程思路
3、在Ubuntu
中使用Geany
书写实现程序。(注:实现程序如下)
#include <linux/input.h>
#include <linux/sched.h>
#include <linux/unistd.h>
#include <linux/list.h>
#include <linux/init.h>
#include <linux/module.h>//每一个模块程序的编写都需要包含linux/module.h这个头文件
#include <linux/kernel.h>
MODULE_LICENSE("Dual BSD/GPL"); //编写内核模块时的声明
//打印进程树
void pstreea(struct task_struct* p,int b){
struct list_head* l; //链表的头节点
int i;
for(i=1;i<=b;i++)
printk(" ");
printk("|--%s\n",p->comm);
for (l = p->children.next; l!= &(p->children); l = l->next ){
//循环链表中的每一个元素
struct task_struct*t=list_entry(l,struct task_struct,sibling);//将children链上的某一节点作为sibling赋给task_struct即
pstreea(t,b+1); //实现了向children节点的移动
}
}
//进程树初始化
static int pstree_init(void){
struct task_struct* p;
int b=0;
for ( p = current; p != &init_task; p = p->parent ) ;//回溯到初始父进程
pstreea(p,b);
return 0;
}
//模块卸载函数
static void pstree_exit(void){
printk("Hello, kernel!/n");
}
module_init(pstree_init);
module_exit(pstree_exit);//函数init ()和函数exit ( )是模块编程中最基本的也是必须的两个函数。
4、在Ubuntu
中保存Geany
中书写的程序。
(步骤一:使用指令mkdir pstree1
在主目录下创建文件夹 pstree1
)
(步骤二:将书写程序的文件保存到pstree1
文件夹中)
5、创建Makefile
文件。
(步骤一:按如图所示使用指令创建 Makefile
文件)
(步骤二:将如下程序保存到Makefile
文件中)
obj-m:=pstree1.o
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
clean:
rm -rf *.o.*.cmd *.ko *.mod.c
6、在pstree1
文件夹按如下指令在终端执行即可。
编译 make
安装 sudo insmod pstree1
查看 dmesg
卸载 sudo rmmod pstree1