环境:ubuntu16.04
在linux中为我们提供了内核接口让我们可以编写内核模块,将内核模块加入到内核中运行。但是内核模块是以何种策略来调度的?
编写hello.c:
vi hello.c
在hello.c中输入如下代码:
//必要的头文件
#include
#include
#include
//模块许可证声明(必须)
MODULE_LICENSE("Dual BSD/GPL");
//模块加载函数(必须)
static int hello_init(void)
{
while(1)
{
printk(KERN_INFO "Hello World enter\n");
}
return 0;
}
//模块卸载函数(必须)
static void hello_exit(void)
{
printk(KERN_INFO "exit\n");
}
//模块的注册
module_init(hello_init);
module_exit(hello_exit);
//声明模块的作者(可选)
MODULE_AUTHOR("XXX");
//声明模块的描述(可选)
MODULE_DESCRIPTION("This is a simple example!/n");
//声明模块的别名(可选)
MODULE_ALIAS("A simplest example");
编写Makefile文件
obj-m += hello.o
#generate the path
CURRENT_PATH:=$(shell pwd)
#the current kernel version number
LINUX_KERNEL:=$(shell uname -r)
#the absolute path
LINUX_KERNEL_PATH:=/usr/src/linux-headers-$(LINUX_KERNEL)
#complie object
all:
make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) modules
#clean
clean:
make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) clean
运行命令make
make
将模块加载到内核中
insmod hello.ko
运行ps aux命令找出hello.ko进程的进程pid:
ps aux
输出如下:
root 3022 104 0.0 4616 844 pts/18 R+ 10:05 0:15 insmod hello.k
找到hello.ko的进程pid是3022
运行cat /proc/3022/sched命令来查看3022进程的调度信息
cat /proc/3022/sched
输出结果如下:
insmod (3022, #threads: 1)
-------------------------------------------------------------------
se.exec_start : 794140.425506
se.vruntime : 247397.050655
se.sum_exec_runtime : 246902.119597
se.nr_migrations : 0
nr_switches : 0
nr_voluntary_switches : 0
nr_involuntary_switches : 0
se.load.weight : 1048576
se.runnable_weight : 1048576
se.avg.load_sum : 47048
se.avg.runnable_load_sum : 47048
se.avg.util_sum : 48182444
se.avg.load_avg : 1023
se.avg.runnable_load_avg : 1023
se.avg.util_avg : 1024
se.avg.last_update_time : 794140425216
policy : 0
prio : 120
clock-delta : 45
mm->numa_scan_seq : 0
numa_pages_migrated : 0
numa_preferred_nid : -1
total_numa_faults : 0
current_node=0, numa_group_id=0
numa_faults node=0 task_private=0 task_shared=0 group_private=0 group_shared=0
policy:是调度策略。0是OTHER;1是FIFO;2是RR。
prio:是调度优先级。120对应的nice是0.