In one of my sample Linux kernel module, I have a variable Device_Open
declared static outside all functions and a static variable counter
declared inside a function device_open
. 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 do cat /dev/chardev
. What i can see is that counter
gets incremented for each invocation of cat /dev/chardev
, but Device_Open
always remains 0. What is the reason for the difference in behavior related to incrementing the value of the variables ?
Below is the code snippet 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;
}
The difference between static global variables and static local variables is that a static global variable can be accessed from anywhere inside the program while a static local variable can be accessed only where its scope exists (i.e block scope).
Scope of static variable/function is within the same file despite you include the file as part of a different source file. Scope of global variable is throughout the files in which it is included. To include the variable in a different source file, we use extern before the variable declaration.
A static variable can be either a global or local variable. Both are created by preceding the variable declaration with the keyword static. A local static variable is a variable that can maintain its value from one function call to another and it will exist until the program ends.
Example. Static variables have a lifetime that lasts until the end of the program. If they are local variables, then their value persists when execution leaves their scope.
A Global Static variable serves no useful purpose as it is essentially the same as a normal Global variable. However, a Local Static variable exists only within the function in which it is declared, but is NOT destroyed when the function ends and can be reused when the function is next entered.
Local variables are not known to functions on their own. This will give the output − Global variables are defined outside of all the functions, usually on top of the program. The global variables will hold their value throughout the lifetime of your program.
A global variable with static keyword has internal linkage, so it only accesses within the translation unit (.c). It is not accessible by another translation unit. The static keyword protects your variable to access from another translation unit. 4. Static variables are initialized as 0 if not initialized explicitly. 5.
If you used a normal local variable, the variable would be created and initialized every time the function was executed. With a const static local variable, you can create and initialize the expensive object once, and then reuse it whenever the function is called. Consider the following code:
I searched for "Device_open" and I found its corresponding device release. Are you sure you don't have this function ? I found it at 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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With