real-time programming

Discussion in 'C Programming' started by marcpirat@yahoo.com, Mar 2, 2005.

  1. Guest

    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
     
    , Mar 2, 2005
    #1
    1. Advertising

  2. wrote:

    > 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.
     
    E. Robert Tisdale, Mar 2, 2005
    #2
    1. Advertising

  3. Guest

    ok but that will generate a lot of operation on the stack
     
    , Mar 2, 2005
    #3
  4. "E. Robert Tisdale" <> writes:
    > wrote:
    >
    >> 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.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Mar 2, 2005
    #4
  5. Default User Guest

    wrote:
    > 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
     
    Default User, Mar 2, 2005
    #5
  6. Re: [OT] real-time programming

    In article <d056vj$a4$>,
    E. Robert Tisdale <> wrote:
    : wrote:

    :> 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.
    --
    csh is bad drugs.
     
    Walter Roberson, Mar 2, 2005
    #6
  7. Re: [OT] real-time programming

    Walter Roberson wrote:

    > 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.
     
    E. Robert Tisdale, Mar 2, 2005
    #7
  8. writes:
    > 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

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Mar 2, 2005
    #8
  9. wrote:

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


    What's wrong with that?
     
    E. Robert Tisdale, Mar 2, 2005
    #9
  10. Guest

    , Mar 3, 2005
    #10
  11. Guest

    we have about 150 variables

    i could post the code (about 1800 lines) if somebody want to give me
    some tips
     
    , Mar 3, 2005
    #11
  12. infobahn Guest

    Re: [OT] real-time programming

    wrote:
    >
    > we use a MPC855T processor
    >

    <snip>
    >
    > it's only 50mhz
    >
    > the variable are always used... every 3 seconds, value changes


    I don't see how it's possible, on such a machine, for the value
    to change more frequently than once every 20 seconds.
     
    infobahn, Mar 3, 2005
    #12
  13. Guest

    Re: [OT] real-time programming

    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
     
    , Mar 3, 2005
    #13
  14. CBFalconer Guest

    wrote:
    >
    > 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.

    --
    "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
     
    CBFalconer, Mar 3, 2005
    #14
  15. Default User Guest

    wrote:
    > 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
     
    Default User, Mar 3, 2005
    #15
  16. wrote:

    > 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.)
     
    E. Robert Tisdale, Mar 3, 2005
    #16
  17. E. Robert Tisdale, Mar 3, 2005
    #17
  18. Guest

    CBFalconer wrote:
    > wrote:
    > >
    > > 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.
    >
    > --
    > "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
     
    , Mar 3, 2005
    #18
  19. infobahn Guest

    wrote:
    >
    > > "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


    Nie rozumiem po francuski; prosze pisze po angielsku. Stokrotne dzieki!
     
    infobahn, Mar 3, 2005
    #19
  20. Guest

    infobahn wrote:
    > wrote:
    > >
    > > > "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

    >
    > Nie rozumiem po francuski; prosze pisze po angielsku. Stokrotne

    dzieki!

    un altro fucker della madre
     
    , Mar 3, 2005
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mike Wahler
    Replies:
    19
    Views:
    4,783
    shuvro_sabbir
    Jun 1, 2010
  2. PX
    Replies:
    2
    Views:
    709
  3. Madhusudan Singh
    Replies:
    3
    Views:
    614
    Lou Pecora
    Aug 8, 2005
  4. flamesrock
    Replies:
    8
    Views:
    551
    Hendrik van Rooyen
    Nov 24, 2006
  5. Brian

    Real time converted to Unix time

    Brian, Aug 29, 2003, in forum: Perl Misc
    Replies:
    1
    Views:
    408
    James Willmore
    Aug 29, 2003
Loading...

Share This Page