Static global variable and static local variable in driver function

In one of my sample Linux kernel modules, I have a Device_Open variable declared static by all functions, and a counter static variable declared inside Device_Open function. Inside Device_Open , I increment both Device_Open and counter . The module is inserted without any errors into the kernel, and I created a device file for my module / dev / chardev.

I am doing cat /dev/chardev . I see that counter is incremented for every call to cat /dev/chardev , but Device_Open always remains 0. What is the reason for the difference in behavior associated with increasing the value of variables?

Below is a snippet of code for understanding

 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; } 
+8
c variables linux module static
source share
1 answer

I searched for "Device_open" and I found its corresponding device. Are you sure you do not have this feature? I found it on 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; } 
+7
source share

All Articles