linux下自己編寫的內核模塊的調度策略

環境:ubuntu16.04

一.介紹

在linux中爲我們提供了內核接口讓我們可以編寫內核模塊,將內核模塊加入到內核中運行。但是內核模塊是以何種策略來調度的?

二.編寫內核模塊

編寫hello.c:

vi hello.c

在hello.c中輸入如下代碼:

//必要的頭文件
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
//模塊許可證聲明(必須)
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.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章