驱动函数中的静态全局变量和静态局部变量

nit*_*ian 8 c linux variables static module

在我的一个示例Linux内核模块中,我有一个Device_Open在所有函数外部声明为static的变量,以及counter在函数内声明的静态变量device_open.在里面device_open,我增加两个Device_Opencounter.模块插入没有任何错误进入内核,我为我的模块/ dev/chardev创建了一个设备文件.

我知道cat /dev/chardev.我可以看到counter,每次调用都会增加cat /dev/chardev,但Device_Open始终保持为0.与递增变量值相关的行为差异的原因是什么?

以下是用于理解的代码段

static int Device_Open = 0;

static int device_open(struct inode *inode, struct file *file)
{
    static int counter = 0;

    printk(KERN_INFO "Device_Open = %d", Device_Open);
    printk(KERN_INFO "counter = %d", counter);

    if (Device_Open)
        return -EBUSY;

    Device_Open++;
        counter++;

    try_module_get(THIS_MODULE);

    return SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)

cni*_*tar 7

我搜索了"Device_open"并找到了相应的设备版本.你确定你没有这个功能吗?我在TLDP找到了它.

static int device_release(struct inode *inode, struct file *file)
{
#ifdef DEBUG
    printk(KERN_INFO "device_release(%p,%p)\n", inode, file);
#endif

    /* 
     * We're now ready for our next caller 
     */
    Device_Open--;

    module_put(THIS_MODULE);
    return SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)