insmod期间内核级别的同步

时间:2017-01-07 17:02:33

标签: linux-kernel synchronization driver

我是一个内核新手,我最近一直在研究信号量,这有助于在内核线程之间进行同步,我知道它不是一种很好的同步方式,但请耐心等待我如果我在这里遗漏了一些东西。 我有两个模块,其中我尝试通过将符号从第一个模块导出到内核符号表来访问公共函数,这将在init_module()调用期间由第二个模块访问。

下面给出的第一个模块有导出的符号"来电者",

#define DEBUG                                        
#include<linux/init.h>                               
#include<linux/module.h>                             

struct semaphore sem =__SEMAPHORE_INITIALIZER(sem,1);               
EXPORT_SYMBOL(sem);


void static caller(int a, int b,int count){
        complete(&sig);
        while(1){

                int temp=20000,i;
                printk(KERN_INFO"called from module %s\n",THIS_MODULE->name);
//              down_interruptible(&sem);                                    
                while(--count>0){
                        for(i=0;i<temp;i++);
                        printk(KERN_INFO"This is a Exported Symbol into the table %d %d",a+b,count);   
                }
//              up(&sem);                                                              
                break;
        }
}
static int __init hello_kernel(void){
        int count=1000000;                                                                      
        init_completion(&sig);
        printk(KERN_ALERT"Entered In the First Module");
//      printk(KERN_INFO" Parameter passed are %d %s",arg,chrarg);
        caller(1,1,count);                                                                      
        return 0;                                                                               

}

static void __exit  hello_exit(void){
        dump_stack();
        printk(KERN_EMERG"GOHEAVEN");

}

module_init(hello_kernel);
module_exit(hello_exit);
MODULE_LICENSE("GPL");                   
EXPORT_SYMBOL(caller);
module_init(hello_kernel);
module_exit(hello_exit);

第二个模块调用导出的符号&#34;调用者&#34;

#define DEBUG                                        
#include<linux/init.h>                               
#include<linux/module.h>                             
void caller(int , int, int);                         


static int __init hello_kernel(void){
        int count=50;                                                           
        printk(KERN_INFO"Entered In Second Module");            
        caller(2,2,count);
        return 0;

}


static void __exit  hello_exit(void){
        dump_stack();
        printk(KERN_EMERG"GOHEAVEN");

}

module_init(hello_kernel);
module_exit(hello_exit);
MODULE_LICENSE("GPL");                   
EXPORT_SYMBOL(caller);
module_init(hello_kernel);
module_exit(hello_exit);

首先是模块日志,然后调用第二个模块init。

.........
.........
[36056.684970] This is a Exported Symbol into the table 2 36
[36056.685136] This is a Exported Symbol into the table 2 35
[36056.685301] This is a Exported Symbol into the table 2 34
[36056.685467] This is a Exported Symbol into the table 2 33
[36056.685632] This is a Exported Symbol into the table 2 32
[36056.685798] This is a Exported Symbol into the table 2 31
[36056.685963] This is a Exported Symbol into the table 2 30
[36056.686129] This is a Exported Symbol into the table 2 29
[36056.686314] This is a Exported Symbol into the table 2 28
[36056.686488] This is a Exported Symbol into the table 2 27
[36056.686664] This is a Exported Symbol into the table 2 26
[36056.686851] This is a Exported Symbol into the table 2 25
[36056.687032] This is a Exported Symbol into the table 2 24
[36056.687228] This is a Exported Symbol into the table 2 23
[36056.687393] This is a Exported Symbol into the table 2 22
[36056.687559] This is a Exported Symbol into the table 2 21
[36056.687724] This is a Exported Symbol into the table 2 20
[36056.687890] This is a Exported Symbol into the table 2 19
[36056.688055] This is a Exported Symbol into the table 2 18
[36056.688221] This is a Exported Symbol into the table 2 17
[36056.688387] This is a Exported Symbol into the table 2 16
[36056.688552] This is a Exported Symbol into the table 2 15
[36056.688718] This is a Exported Symbol into the table 2 14
[36056.688883] This is a Exported Symbol into the table 2 13
[36056.689049] This is a Exported Symbol into the table 2 12
[36056.689215] This is a Exported Symbol into the table 2 11
[36056.689380] This is a Exported Symbol into the table 2 10
[36056.689546] This is a Exported Symbol into the table 2 9
[36056.689711] This is a Exported Symbol into the table 2 8
[36056.689877] This is a Exported Symbol into the table 2 7
[36056.690044] This is a Exported Symbol into the table 2 6
[36056.690238] This is a Exported Symbol into the table 2 5
[36056.690432] This is a Exported Symbol into the table 2 4
[36056.690604] This is a Exported Symbol into the table 2 3
[36056.690786] This is a Exported Symbol into the table 2 2
[36056.690964] This is a Exported Symbol into the table 2 1

...........
...........
[36058.216861] This is a Exported Symbol into the table 4 19
[36058.216931] This is a Exported Symbol into the table 4 18
[36058.217001] This is a Exported Symbol into the table 4 17
[36058.217070] This is a Exported Symbol into the table 4 16
[36058.217140] This is a Exported Symbol into the table 4 15
[36058.217210] This is a Exported Symbol into the table 4 14
[36058.217279] This is a Exported Symbol into the table 4 13
[36058.217349] This is a Exported Symbol into the table 4 12
[36058.217419] This is a Exported Symbol into the table 4 11
[36058.217488] This is a Exported Symbol into the table 4 10
[36058.217558] This is a Exported Symbol into the table 4 9
[36058.217628] This is a Exported Symbol into the table 4 8
[36058.217698] This is a Exported Symbol into the table 4 7
[36058.217767] This is a Exported Symbol into the table 4 6
[36058.217837] This is a Exported Symbol into the table 4 5
[36058.217907] This is a Exported Symbol into the table 4 4
[36058.217976] This is a Exported Symbol into the table 4 3
[36058.218046] This is a Exported Symbol into the table 4 2

在没有使用信号量的情况下,我期望dmesg内核日志将在两个模块中混合,因为它们不一致地访问代码,但看起来像insmod进程 只能一个接一个地加载模块。

我在这里缺少什么?

感谢。

0 个答案:

没有答案