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; }
c variables linux module static
nitin_cherian
source share