LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Avoiding global variables in kernel modules (https://www.linuxquestions.org/questions/programming-9/avoiding-global-variables-in-kernel-modules-816723/)

bizoo 06-27-2010 11:50 PM

Avoiding global variables in kernel modules
 
HI

I have two modules A and B in the kernel which i am trying to hack by introducing a variable in A which is of type 'extern' (and using EXPORT_SYMBOL) and then calling it in B.

Now the problem is how to introduce that variable in module A. Currently i have made a global variable and it works fine but global variable in kernel is a bad bad thing.

I tried having an 'extern' function in A which have that variable and calling that function in module B, but then again how would module A use my variable unless the variable is available outside the scope of that function and that again means a global variable.

Any idea on this avoiding of global variables in Kernel programming.

thx
cheers

zirias 06-28-2010 03:01 AM

Depends on what exactly you're trying to achieve. I don't know the semantics of your variable, but I assume it's somehow "bound" to one of the modules, so let's say it's semantically bound to module A.

Then, the best way would be to make it local to module A and introduce a public interface for other modules to use that provides "valid" operations on the variable. B would call these functions instead of accessing the variable directly. This way, it's also clear which module depends on the other (B on A).

Sergei Steshenko 06-28-2010 04:27 AM

Quote:

Originally Posted by bizoo (Post 4016833)
HI

... global variable in kernel is a bad bad thing. ...

Why do you think so ? Hint: think about (nested) scopes and data exchange between them.

zirias 06-28-2010 04:54 AM

Indeed, if the variable's purpose is data exchange, there's nothing wrong with a global variable -- but, that's a design decision that can't be generally answered without some semantic knowledge.


All times are GMT -5. The time now is 01:36 PM.