Thread: Need something better than sleep()

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    278

    Need something better than sleep()

    I've been using sleep(1) after requesting data via a serial port because of the slight delay time in receiveing the data. However, now those 1 second delays have accumulated to too much wasted time since I really only need to wait a fraction of a second. If I was only doing one data request it wouldn't bee too bad... but I'm requesting over 5000 data chunks... 5000 seconds is a lot of time...

    is there a similar function that takes values less than one second? I know of Sleep() in windows.h, but I'm running the program on a computer with only DOS 6.2. What are my options?

    Thanks in advance

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    nanosleep(); you will probably want to build a function that uses the nanosleep/time structs internally so you can just do something like "mysleep(0,10)" where the first arg is seconds and the second one hundredths thereof, because nanosleep itself is not that simple or user friendly.

    Here's the one I've been using, there are some includes that go with it that will be particular to your system, that is a mystery to me:
    Code:
    void gap (int secs, int hundredths) {
            struct timespec interval;
            interval.tv_sec=secs;
            interval.tv_nsec=hundredths*10000;
            nanosleep(&interval,NULL);
    }
    
    so, eg,
    gap(0,25);
    ...actually it looks like nanosleep is POSIX, sorry.
    Last edited by MK27; 03-30-2009 at 10:50 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    That's similar to what I'm looking for, just I need it for dos...

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Check your dos.h header file, and see if there are any sleep functions that have the resolution you are looking for.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    DOS, or Windows? Most people tend to say DOS when they mean Windows Console apps, but the Native API is quite different.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    No I actually mean DOS. Windows is not installed.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And what compiler are you using?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    Turbo C++ 3.0

  9. #9
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    what compiler and libraries you use?
    Code:
    printf("%c%c%c%c%c%c%c",0x68,0x68^0xd,0x68|0x4,0x68|0x4,0x68|0xf,0x68^0x49,0x68^0x62);

  10. #10
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    there is delay() which causes delay in milliseconds

    search turboc++ help for "delay"
    Code:
    printf("%c%c%c%c%c%c%c",0x68,0x68^0xd,0x68|0x4,0x68|0x4,0x68|0xf,0x68^0x49,0x68^0x62);

  11. #11
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    Just saw that is dos.h... I swear I looked through that before :/

    Thanks

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The answer is to use the delay() function.

    #include <dos.h>


    and then set your delay for what you want, in milliseconds:

    delay(500); //delay will be 1/2 second.

    This also works in WIndows console code, but I don't know why.

    This is from Turbo C/C++ 1.01 btw, so it's 100% DOS, not windows.

  13. #13
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    how about this custom made function

    Code:
    #include <time.h>
    #define MILLISEC 1000
    void timewaste(clock_t lMillisec)
    {
    clock_t finish;
    finish = clock() + (lMillisec / MILLISEC * CLOCKS_PER_SEC);
    while (finish > clock())
    ;
    }
    you could easily extend that to any other time interval
    Code:
    printf("%c%c%c%c%c%c%c",0x68,0x68^0xd,0x68|0x4,0x68|0x4,0x68|0xf,0x68^0x49,0x68^0x62);

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by creeping death View Post
    how about this custom made function

    Code:
    #include <time.h>
    #define MILLISEC 1000
    void timewaste(clock_t lMillisec)
    {
    clock_t finish;
    finish = clock() + (lMillisec / MILLISEC * CLOCKS_PER_SEC);
    while (finish > clock())
    ;
    }
    That is no better than the delay function that is already available in TurboC. Both have the drawback that it uses 100% of the CPU time in the machine, which is not ideal (but there's little else that can be done in DOS).

    In a non-DOS environment, you should be using the OS supplied functions, as that will let the CPU do something else useful or go to sleep if there is nothing useful to do.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    Well, I would have liked to not be using the CPU during the delay, but since I don't need the CPU to really be doing anything else during this time, then it's not that big a deal... out of curiosity, does the sleep function is dos.h use the CPU as well?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [pthread] cancellable sleep in win32?
    By cyberfish in forum C++ Programming
    Replies: 2
    Last Post: 08-11-2007, 02:30 AM
  2. Sleep works with just one thread, but not 2
    By finkus in forum C++ Programming
    Replies: 5
    Last Post: 12-01-2005, 09:17 PM
  3. Problem with Sleep() #$@^#$%^
    By intruder in forum C++ Programming
    Replies: 8
    Last Post: 10-11-2004, 06:46 AM
  4. why do we require sleep?
    By jinx in forum A Brief History of Cprogramming.com
    Replies: 43
    Last Post: 07-14-2004, 08:21 AM
  5. Sleep is overrated...
    By Polymorphic OOP in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 01-24-2003, 12:40 PM