LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-27-2010, 11:50 PM   #1
bizoo
LQ Newbie
 
Registered: Jul 2009
Posts: 17

Rep: Reputation: 0
Question 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
 
Old 06-28-2010, 03:01 AM   #2
zirias
Member
 
Registered: Jun 2010
Posts: 361

Rep: Reputation: 59
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).
 
Old 06-28-2010, 04:27 AM   #3
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by bizoo View Post
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.
 
Old 06-28-2010, 04:54 AM   #4
zirias
Member
 
Registered: Jun 2010
Posts: 361

Rep: Reputation: 59
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
global variables Swapna173 Linux - Newbie 2 03-19-2009 01:01 AM
Defining Global Variables in the kernel crunchyblack Programming 4 10-20-2008 10:52 AM
exporting global variables neo77777 Programming 6 09-18-2002 01:46 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration