Linux内核实验(四):动态模块设计实验

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/baidu_34045013/article/details/81201420

一、引言

Linux模块是一些可以独立于内核单独编译的内核函数和数据类型集合,是可增删的内核部分。模块在内核启动时装载称为静态装载,在内核已经运行时装载称为动态装载。模块可以扩充内核所期望的任何功能,但通常用于实现设备驱动程序。

二、实验内容

问题A:分析实验以上模块,编写一个测试该模块的用户程序,比较该模块读取的时间和用gettimeofday()读取的时间的精度。

问题B:实现一个模块用它遍历当前进程的父进程和任务队列,并将遍历的结果输出到一个proc文件中(遍历可以从current当前进程开始,父进程遍历到初始化进程,遍历任务队列可以利用for_each_process宏)。

三、实验代码

mod_a.c

/*****************************************
*
* 动态模块设计
*
* Copyright: (C) 2018.5.3 by shaomingshan
*
* Install: insmod mod_a.ko
*
* Remove: rmmod mod_a.ko
*
*****************************************/
#include <linux/kernel.h> 
#include <linux/module.h> 
#include <linux/init.h> 
#include <linux/fs.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/jiffies.h>

#define MODULE_NAME "mod_a" 

//proc结构变量
static struct proc_dir_entry* myfile;

static int
jif_show(struct seq_file *m, void *v)
{
    seq_printf(m,"%ld\n",(unsigned long) jiffies);
    return 0;
}

static int
jif_open(struct inode *inode, struct file *file)
{
    return single_open(file, jif_show, NULL);
}

static const struct file_operations jif_fops = {
    .owner = THIS_MODULE,
    .open  = jif_open,
    .read  = seq_read,
    .llseek= seq_lseek,
    .release= single_release,
};

//装入模块
static int __init
mod_init(void)
{
    //创建/proc/myfile1文件
    myfile = proc_create("myfile1", 0, NULL, &jif_fops);
    return 0;
}

//卸载模块
static void __exit
mod_exit(void)
{
    //移除/proc/myfile1文件
    remove_proc_entry("myfile1", NULL);
    printk("Goodbye.\n"); 
}

module_init(mod_init);
module_exit(mod_exit);

MODULE_LICENSE("GPL"); 
MODULE_DESCRIPTION("Test"); 
MODULE_AUTHOR("xxx"); 

Makefile

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

mod_b.c

/*****************************************
*
* 动态模块设计
*
* Copyright: (C) 2018.5.3 by shaomingshan
*
* Install: insmod mod_b.ko
*
* Remove: rmmod mod_b.ko
*
*****************************************/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/sched.h>
#include <linux/fs.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>

#define MODULE_NAME "mod_b"

//proc结构变量
static struct proc_dir_entry* myfile;

int myshow(struct seq_file *, void *);
int myopen(struct inode *, struct file *);

int myshow(struct seq_file *m, void *v)
{
    struct task_struct *task;
    for_each_process(task){
        seq_printf(m,"%d  %s\n",task->pid,task->comm);
    }
    
    return 0;
}

int myopen(struct inode *inode, struct file *file)
{
    return single_open(file, myshow, NULL);
}

static const struct file_operations my_fops = {
    .owner = THIS_MODULE,
    .open  = myopen,
    .read  = seq_read,
    .llseek= seq_lseek,
    .release= single_release,
};

//装入模块
static int __init
mod_init(void)
{
    //创建/proc/myfile2文件
    myfile = proc_create("myfile2", 0444, NULL, &my_fops);
    return 0;

}

static void __exit
mod_exit(void)
{
    //移除/proc/myfile2文件
    remove_proc_entry("myfile2", NULL);
    printk("Goodbye.\n"); 
}

module_init(mod_init);
module_exit(mod_exit);

MODULE_LICENSE("GPL"); 
MODULE_DESCRIPTION("Test"); 
MODULE_AUTHOR("xxx"); 

Makefile

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

main.c

/*****************************************
*
* 动态模块设计
*
* Copyright: (C) 2018.5.3 by shaomingshan
*
* Compile: gcc -o main main.c
*
* Execute: ./main
*
*****************************************/
#include <stdio.h>
#include <sys/time.h>

#define HZ 100
FILE *thisProcFile;
struct timeval now; 
char line[64];
int main(){
	while (1) {
		thisProcFile = fopen("/proc/myfile1" ,"r");
		gettimeofday(&now, NULL);
		fgets(line , 64, thisProcFile);
		unsigned long jiffies = atol(&line[6]);
		close(thisProcFile);
		printf("jiffies:%ld; timeofday:%ld\r", jiffies / HZ, now.tv_sec);
		fflush(stdout);
		sleep(1);
	}
	return 0;
}

Makefile

src = main.c

all: main

main: $(src)
	gcc $(src) -o main
clean:
	rm main

四、运行结果

装载mod_a模块:

proc文件系统中已创建myfile1文件:

模块读取时间和gettimeofday()获取的时间比较:

卸载模块mod_a:

装载模块mod_b.ko

proc文件系统中已创建myfile2文件:

读取/proc/myfile2文件内容:

卸载mod_b模块:

如有错误请指正

猜你喜欢

转载自blog.csdn.net/baidu_34045013/article/details/81201420