Delay Time?? Need As Soon As Possible

This is a discussion on Delay Time?? Need As Soon As Possible within the C Programming forums, part of the General Programming Boards category; I need a way to delay a program for only a matter of nano seconds in C. I need to ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    19

    Exclamation Delay Time?? Need As Soon As Possible

    I need a way to delay a program for only a matter of nano seconds in C. I need to take up to 2000 samples per second and currently can not do this using clock() since it does not give me accurate enough digits in decimal places. The lowest value that clock() returns is every 10 milliseconds.

    Thanks

  2. #2
    Registered User
    Join Date
    Jul 2003
    Posts
    19
    My OS keeps running the program because it needs to run this application for a given period of time, below is a portion of the code that I am using with some modification to the data it prints out so I don't have to type all of it but the important stuff is there

    Thanks for any help



    clock_t time;
    while((clock()/CLOCKS_PER_SEC)<runningTime)
    {
    fprint(outOpen,"%ld%",clock()/1000);
    for(loc2=0; selection >loc2; loc2++)
    {
    printf(outOpen,"Printing Stuff out here");
    }
    delayT = clock();
    while(clock() < ((1000000/rate)+delayT))
    {
    }
    }

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Answer this:
    Posted by Salem
    Which operating system and compiler?
    [edit]
    Maybe this will help (if you have it):
    http://www.rt.com/man/nanosleep.2.html
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    19
    OS Linux
    Compiler gcc

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    So, do you have nanosleep (linked in my previous post) ? Does it work for you?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Hmm, you're talking about realtime programming under a system that doesn't support true realtime programs.

    Questions for you:
    1) Do you need hard realtime, or soft realtime? E.g. say you miss one sample, can you ignore it and get the next on time?

    2) What is the application, some kind of ADC or DSP type application?

    3) How much margin of error do you have (how close to 2 kHz do you actually need to sample), and how important is it that samples be uniformly spaced in time?

    4) Is cumulative error a worry? E.g. say you actually sample, on average, at 1999 Hz and not 2000 Hz, is this error of one part in 2000 going to have a greater effect as time goes on? An example would be a watch -- if my watch had 1 in 2000 error, it would gain or lose over 4 hours per year, which would certainly be undesirable.

    Hate to say it, but the standard means of doing this is with custom hardware; your software will never be very accurate. Sleep ensures that the software will wait AT LEAST as long as you ask; the OS is allowed to make you wait longer, just as any preemptive multitasking OS is.
    Last edited by Cat; 07-09-2003 at 03:28 PM.

  7. #7
    Registered User
    Join Date
    Jul 2003
    Posts
    19
    sleeps don't seem to work because they seem to stop the clock() while they sleep and thus through the timing of the program out and it runs for a lot longer then I have patients to wait for.

    I need to do fprintf withing the loop and I know without the delay loop I can get 2000 samples per second, so the fprintf is not an issue. The delay or sleep, if it can be figured out, is the issue, thanks though.

    Thanks for any input you can offer

  8. #8
    Registered User
    Join Date
    Jul 2003
    Posts
    19
    This software is for a single board computer, it will only be running for at max 90 seconds, which is why I need the system to keep track of how long it is running.

    Thanks for all input

  9. #9
    Registered User
    Join Date
    Jul 2003
    Posts
    19
    yes the board is running Linux Kernel 2.4.18

  10. #10
    Registered User
    Join Date
    Jul 2003
    Posts
    19
    Little rusty on the time() function + I left my refernce bbok elsewhere. Ideas how to use it please!

    Thanks

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  12. #12
    Registered User
    Join Date
    Jul 2003
    Posts
    19
    That info above I think is good, Thanks

    Now that leave the point that I need to display the difference of time in milleseconds while I go through the loop, any suggestions

    Thanks for all assistance

  13. #13
    Registered User
    Join Date
    Jul 2003
    Posts
    19
    I tried the nanosleep() to make only a quick break in the system unfortunetely it is to long. I need to get between 2000 and 2 samples per second depending on the users requirements. To do this I have a loop and a delay in the loop, currently using nanosleep(). With the nanosleep() I max out at about 50 samples per second. If I take the sleep out so the loop has no delays I average over 2000 samples per second.

    Any suggestions on a better way to delay would be extremely useful.

    Thanks

  14. #14
    Registered User
    Join Date
    Jul 2003
    Posts
    19

    Exclamation

    Ok, well since I got an delay working properly, and I changed the print out to the file from an int to a float I have run into the point that the change from the int to the float darastically slowed my program speed down, so now I am getting insufficient speed for the program. Is there any way to store the values that I return into an array so I can print it out after the logging is done, if it would speed things up? Note: I need to max out at about 2000 samples per sec. and am running this program for around 90 seconds.

    So if you have any suggestion how to do it this way, or have a suggestion on how to do it another way I would appreciate it

    Thanks

  15. #15
    Registered User
    Join Date
    Jul 2003
    Posts
    19
    Ok I tried that so I had a variable

    float sample[180000]

    and all I got back was a segmentation fault. I reduced the number to 8, just to see what would happen, and it worked, any suggestion, because I need that higher number

    Thanks

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Execution Time - Rijandael encryption
    By gamer4life687 in forum C++ Programming
    Replies: 5
    Last Post: 09-20-2008, 10:25 PM
  2. Killing someones grandparents
    By nickname_changed in forum A Brief History of Cprogramming.com
    Replies: 37
    Last Post: 09-07-2003, 08:56 AM
  3. The Timing is incorret
    By Drew in forum C++ Programming
    Replies: 5
    Last Post: 08-28-2003, 05:57 PM
  4. Time delay function
    By sundeeptuteja in forum C++ Programming
    Replies: 4
    Last Post: 02-10-2003, 05:17 AM
  5. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 10:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21