Like Tree8Likes

Delay for DOS

This is a discussion on Delay for DOS within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by Elysia I think what everyone here is trying to tell you is: It simply isn't possible to ...

  1. #16
    Registered User
    Join Date
    Jul 2011
    Posts
    14
    Quote Originally Posted by Elysia View Post
    I think what everyone here is trying to tell you is:
    It simply isn't possible to make a "sleep" function that works in both Windows and DOS.
    Well, we have the sleep function from Turbo C++ which works quite well, also when the DOS program is run in Windows. (The only drawback is the fact that it only waits seconds, not milliseconds).

    Quote Originally Posted by Elysia View Post
    The reason why "halt" isn't working is that Windows probably won't allow it (ie emulates it in a wrong way than you expect).
    Or maybe because the suggestion to use hlt in a loop was incorrect in the first place. (I mean, why shouldn't it work in Windows? The whole thing works in the native Windows Sleep function, so why should DOS emulation just skip it instead of emulating its behavior?) After all, can anybody confirm to me that this:
    Code:
    while ((clock() - start) * 1000 / CLOCKS_PER_SEC < milliseconds)
    {
        asm{"hlt"};
    }
    indeed does work under DOS and wasn't just a quick first idea? But even if it would, it's obviously still not the way Turbo C++'s sleep is implemented because that one does work in Windows.

  2. #17
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,920
    Windows Sleep function does not use a "halt" function. It does not need to.
    It simply tells the scheduler not to schedule the program for execution for a while. Windows is natively multi-threaded as opposed to DOS.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #18
    Registered User
    Join Date
    Jul 2011
    Posts
    14
    But the DOS function sleep still works from within Windows.

  4. #19
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,767
    The DOS sleep() call probably maps down to a DOS interrupt call, which can be trapped by the VM and thus "do the right thing".

    You might be able to make your own around this DOS interrupt, if the VM emulates it correctly
    Int 21/AH=89h

    Only a tiny fraction of the DOS interrupt API is mapped to C function calls, and those that are are also constrained by the historic API requirements (like sleeping for integer seconds). The underlying DOS API might be better.

    FWIW, some degree of familiarity with the "Ralph Brown Interrupt List" is a must if you want this level of control.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #20
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    A busy wait is not necessarily bad for non multitasking operations. For a long wait it does make sense to make the CPU go into a low power mode, but for a small time period there isn't much benefit. I suspect the windows scheduler will do a busy wait, and not try to halt the cpu.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #21
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,920
    I find it quite unlikely the scheduler will do any busy wait of sorts, seeing as Windows has lots of work to do in the background and as all operating systems, works with timer interrupts.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #22
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    Quote Originally Posted by Elysia View Post
    I find it quite unlikely the scheduler will do any busy wait of sorts, seeing as Windows has lots of work to do in the background and as all operating systems, works with timer interrupts.
    A timer interrupt can interrupt a low priority busy wait. But yeah, it may just use halt.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  8. #23
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,920
    Sure, but my point was that Windows has lots of things to do (and have better things to do than busy wait!).
    There is also the question of how a busy wait would be implemented. Would it be spun off as a DPC/APC?
    And if it was, would it interfere with other work, especially that of drivers? It seems just more certain that it would be easier and better if it simply scheduled it for running some timer interrupt later.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #24
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    Necessarily for windows to run smoothly, most of the time it must be doing nothing. DPCs and APCs are things that are differed when there is high load, but at some point you must run out of those too, or else the queue of things to do will grown faster than it is consumed.

    Researching this a little it does seem like a halt command is likely. However with a modern CPU, the command cannot just power down the CPU. A CPU has it's own instruction queue of active and pending instructions that it must empty.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  10. #25
    Registered User
    Join Date
    Jul 2011
    Posts
    14
    Quote Originally Posted by Salem View Post
    The DOS sleep() call probably maps down to a DOS interrupt call
    Alright, that might help me. Unfortunately, I know almost nothing about Assembler, therefore, what do I have to do to understand how to implement a timer without studying a huge reference of overall commands?

  11. #26
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,767
    There's a generic API called int86 which allows you to easily call and DOS/BIOS interrupt without messing about with assembler.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #27
    Registered User
    Join Date
    Jul 2011
    Posts
    14
    OK, thanks. But how do I know what commands to use? As I said, I don't really know how to use that whole low level commands. Where can I get to know how to use the stuff relevant for the delay?

    By the way, I downloaded Open Watcom and in this compiler, both functions, delay and sleep, rise the processor load to 100%, while with Turbo C++ and also Borland C++ 5.02, only the delay makes problems.

  13. #28
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,767
    > But how do I know what commands to use?
    Post #19 and lots of reading on your part.
    The RBIL is a very comprehensive list of essential information for low level real-mode programmers.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  14. #29
    Registered User
    Join Date
    Jul 2011
    Posts
    14
    Quote Originally Posted by Salem View Post
    Post #19 and lots of reading on your part.
    The RBIL is a very comprehensive list of essential information for low level real-mode programmers.
    Yeah, and that's exactly the problem: I don't want to become an advanced low level programmer, I only have one tiny bit in my program that needs such stuff. That's why I need tutorials that show me exactly and to the point how to do it, not whole lists and references about low level programming in general. If you want to clean your drain and need to know how to remove the pipe from the washbowl, you look for something that tells you how to do exactly that. You wouldn't study a whole handbook about plumbing in the hope that one day, you'll be able to achieve the task with your newfound craftsman knowledge.

  15. #30
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    612
    Quote Originally Posted by Erde View Post
    That's why I need tutorials that show me exactly and to the point how to do it
    Things don't always come in neat little packages. Suck it up or go find something else to do.

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help.....!!!!!how much delay i will get by using delay(1)
    By gunjansethi in forum C Programming
    Replies: 7
    Last Post: 03-23-2010, 04:08 AM
  2. delay() in Bc++ 5.5
    By Chiki Chiki Chalem in forum C++ Programming
    Replies: 1
    Last Post: 07-07-2002, 10:58 AM
  3. Delay
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 06-05-2002, 03:30 PM
  4. delay
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 04-20-2002, 09:30 AM
  5. Delay
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 03-30-2002, 12:53 PM

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