real-time programming

M

marcpirat

hi

we create a real-time program on a embedded linux system.
the program will run a few time by second

i would like to know if it's better to create global variable or create
the variable in the main and pass it to the function?

thanks
 
E

E. Robert Tisdale

We create a real-time program on a embedded linux system.
The program will run a few times each second.

I would like to know if it's better to create global a variable
or create the variable in the main and pass it to the function?

It's always better
to create a local variable in the calling function [main]
and pass it the function.
 
K

Keith Thompson

E. Robert Tisdale said:
We create a real-time program on a embedded linux system.
The program will run a few times each second.
I would like to know if it's better to create global a variable
or create the variable in the main and pass it to the function?

It's always better
to create a local variable in the calling function [main]
and pass it the function.

Always?

Should stdin, stdout, and stderr be local variables?

To the OP: what do you mean by "better"? If you're referring to
performance, the C language says nothing about whether using local or
global variables results in faster code; the difference, if any, is
not likely to be significant. Using locals often results in cleaner
and more maintainable code. On the other hand, if you have a lot of
data that really is global to your entire program, artificially making
it local and passing it around as parameters will probably make your
code less maintainable.

If the fact that it's a real-time program is really relevant, you
might get better information in comp.realtime or comp.os.linux.embedded.
 
D

Default User

ok but that will generate a lot of operation on the stack

What will? Without a representative quote, it's difficult to tell who
or what you are responding to.

To post correctly (that is with proper attributions and quotes) from
the Google interface, click "show options" and use the Reply shown in
the expanded headers. You will get more and better help if you are a
good net citizen.



Brian
 
W

Walter Roberson

:> We create a real-time program on a embedded linux system.
:> The program will run a few times each second.

:> I would like to know if it's better to create global a variable
:> or create the variable in the main and pass it to the function?

:It's always better
:to create a local variable in the calling function [main]
:and pass it the function.

This is not always better for performance. If you create a
global variable, especially one with external linkage (instead of
file scope) then the linker may be able to completely resolve the
address of the variable at link time.

Speed of access to the variable then depends on architecture details,
but would usually be either a load of a constant to an address register
followed by an indirection, or a direct reference to constant address
within the instruction [depending on the instruction set and upon local
code optimizations.] If the variable were used often, the address would
be likely to sit in primary or secondary cache.

The alternative would load the variable once and then pass it in
a number of procedure calls. That could result in a stack push and
a stack pop per call, or could result in the address being stored
in an address register... which might require saving the previous value
of the register. Saving of registers may require a series of stack
operations, or may have an internal write-consequative optimization that
does the work in less time, or may work within a "register stack" that
is internal to the processor with the possibility that one
may need to dump the register stack anyhow if one gets nested too deeply
in calls. If your system can use an internal register stack [e.g., SPARC]
then passing the address around may require only internal processor
operations and thus be quite fast... until the program gets deeply
nested or the routine gets complex enough that the processor needs to
dump the register in order to use the register for other optimizations.

Which is faster? "It depends". The answer requires a fair detail of
the knowledge of the architecture and the ABI.
 
E

E. Robert Tisdale

Walter said:
E. Robert Tisdale wrote:

:It's always better
:to create a local variable in the calling function [main]
:and pass it the function.

This is not always better for performance.
If you create a global variable,
especially one with external linkage (instead of file scope)
then the linker may be able to completely resolve
the address of the variable at link time.

Speed of access to the variable then depends on architecture details,
but would usually be either a load of a constant to an address register
followed by an indirection,
or a direct reference to constant address within the instruction
[depending on the instruction set and upon local code optimizations.]
If the variable were used often,
the address would be likely to sit in primary or secondary cache.

The alternative would load the variable once and then pass it in
a number of procedure calls. That could result in a stack push and
a stack pop per call, or could result in the address being stored
in an address register... which might require saving the previous value
of the register. Saving of registers may require a series of stack
operations, or may have an internal write-consequative optimization that
does the work in less time, or may work within a "register stack" that
is internal to the processor with the possibility that one
may need to dump the register stack anyhow if one gets nested too deeply
in calls. If your system can use an internal register stack [e.g., SPARC]
then passing the address around may require only internal processor
operations and thus be quite fast... until the program gets deeply
nested or the routine gets complex enough that the processor needs to
dump the register in order to use the register for other optimizations.

Which is faster? "It depends". The answer requires a fair detail of
the knowledge of the architecture and the ABI.

No.

First of all, the question was about real-time programming
and *not* high performance programming.
They don't really have anything to do with each other.

Second, you can't even measure the difference, if any,
in the time required to access a global variable
versus a function argument.

Finally, using global variables will simply make your program
less modular and more difficult to read, understand and maintain.
This will quickly overwhelm any advantage in performance
that you might possibly achieve by using global variables
instead of passing arguments to functions
whether you are writing program for real-time
or time-sharing environments.
 
K

Keith Thompson

ok but that will generate a lot of operation on the stack

groups.google.com is still buggy, making it too easy to post followups
with no context.

To quite CBFalconer's sig quote:

"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
 
M

marcpirat

we have about 150 variables

i could post the code (about 1800 lines) if somebody want to give me
some tips
 
M

marcpirat

i can change the program to read value every second or less , we can do
a lot of thing with a 50Mhz processor....

we run a webserver, ftp, telnet, ssh and 3 home made program with
another machine who use the same cpu
 
C

CBFalconer

we have about 150 variables

i could post the code (about 1800 lines) if somebody want to
give me some tips

Whatever it is we certainly don't want that. Start by learning how
to quote and post correctly (see my sig below if you MUST use
google). Meanwhile work on reducing your trouble making program to
about one to two hundred lines that still exhibits the problem. By
the time you have done that you will (hopefully) have learned to
post and/or gotten a proper news reader (try Mozilla or Thunderbird
from Mozilla.com), and quite likely will have solved your own
problem.

Oh yes, while you are at it pay no attention to Trollsdale, his
only purpose here is to confuse, annoy, and misinform. He picks on
newbies with glee.
 
D

Default User

we use a MPC855T processor


Is there some reason you refuse to post with proper quotes and
attributions? I already explained to you how to do so from Google.

Not doing so is disrespectful to the rest of the newsgroup.




Brian
 
E

E. Robert Tisdale

we have about 150 variables

I could post the code (about 1800 lines)
if somebody want to give me some tips.

Don't post it.
I will be happy to read it
if you send me a copy.
(Please don't send me any other email.
I don't normally respond to email from newsgroup subscribers.)
 
M

marcpirat

CBFalconer said:
Whatever it is we certainly don't want that. Start by learning how
to quote and post correctly (see my sig below if you MUST use
google). Meanwhile work on reducing your trouble making program to
about one to two hundred lines that still exhibits the problem. By
the time you have done that you will (hopefully) have learned to
post and/or gotten a proper news reader (try Mozilla or Thunderbird
from Mozilla.com), and quite likely will have solved your own
problem.

Oh yes, while you are at it pay no attention to Trollsdale, his
only purpose here is to confuse, annoy, and misinform. He picks on
newbies with glee.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson

encore un trou cul qui se la pète, continue champion pis reste dans
ton newgroups à marde crisse d'américain
 
I

infobahn

encore un trou cul qui se la pète, continue champion pis reste dans
ton newgroups à marde crisse d'américain

Nie rozumiem po francuski; prosze pisze po angielsku. Stokrotne dzieki!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top