Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

How to use sleep() function?

leekeng81leekeng81 Posts: 27Member
I have found some source codes that they use sleep(), but I have tried myself but couldn't get it right, can someone tell me what's the error of this coding? I know I have missed something, is it header file? (Asking stupid question again)

[code]
#include

void main()
{
cout << "Hello!
";
sleep(5);
cout << "How are you?
";
}
[/code]
Error Message: error C2065: 'sleep' : undeclared identifier

Comments

  • kern68kern68 Posts: 85Member
    [code]
    #include

    void main()
    {
    cout << "Hello!
    ";
    [red]S[/red]leep(5); [red]// Sleep shoud be capitalized[/red]
    cout << "How are you?
    ";
    }
    [/code]
  • pseudocoderpseudocoder Posts: 687Member ✭✭✭
    : I have found some source codes that they use sleep(), but I have tried myself but couldn't get it right, can someone tell me what's the error of this coding? I know I have missed something, is it header file? (Asking stupid question again)
    :
    : [code]
    : #include
    :
    : void main()
    : {
    : cout << "Hello!
    ";
    : sleep(5);
    : cout << "How are you?
    ";
    : }
    : [/code]
    : Error Message: error C2065: 'sleep' : undeclared identifier
    :

    [blue]sleep() is one of the compiler specific functions, so it may not be available to you. For dos based compilers try #include <dos.h> As long as you use an ANSI compliant compiler, you can make your own timer function by adding time.h or ctime depending on C or C++[/blue]
    [code]
    #include
    #include

    void mySleep(int delay)
    {
    float t1 = clock(), t2;

    fflush(stdout);

    do
    {
    t2 = clock();

    }while(((t2 - t1) / CLK_TCK) < delay);
    }

    int main(void)
    {
    printf("Hello");

    mySleep(5); /* wait ~5 seconds */

    printf(" World.");

    return (0);
    }[/code]
  • Ed HallEd Hall Posts: 395Member
    Would the code you provided make a similar use of the CPU to that of the sleep() function? Doesn't the sleep() function release the CPU and any thing else make extensive use of the CPU in iterations? Is this different in win32? I also don't seem to have a sleep() at all in win16. Any comments to add to my learning? Thanks.

    Take Care,
    Ed

  • stoberstober Posts: 9,765Member ✭✭✭
    : Would the code you provided make a similar use of the CPU to that of the sleep() function? Doesn't the sleep() function release the CPU and any thing else make extensive use of the CPU in iterations? Is this different in win32? I also don't seem to have a sleep() at all in win16. Any comments to add to my learning? Thanks.
    :
    : Take Care,
    : Ed
    :
    :
    [blue][b]sleep()[/b] (lower-case 's') is a unix/linux function and has an argument in seconds, and is defined in unistd.h (I think). [b]Sleep()[/b] (upper-case 'S') is a Micro$oft Window$ function, and has an argument in milliseconds, and is defined in windows.h. Both versions of sleep allow other processes to gain CPU time while the calling process is idle. In 16-bit MS-DOS this isn't necessary because the calling function is the ONLY processing running on the computer, hence no implementation of sleep. Turbo C has delay(), which is similar to sleep, but that is a complier-specific function and probably not implemented by any other 16-bit MS-DOS compiler.[/blue]
  • Ed HallEd Hall Posts: 395Member
    : [blue][b]sleep()[/b] (lower-case 's') is a unix/linux function and has an argument in seconds, and is defined in unistd.h (I think). [b]Sleep()[/b] (upper-case 'S') is a Micro$oft Window$ function, and has an argument in milliseconds, and is defined in windows.h. Both versions of sleep allow other processes to gain CPU time while the calling process is idle. In 16-bit MS-DOS this isn't necessary because the calling function is the ONLY processing running on the computer, hence no implementation of sleep. Turbo C has delay(), which is similar to sleep, but that is a complier-specific function and probably not implemented by any other 16-bit MS-DOS compiler.[/blue]
    :
    Thanks, Stober,

    I guess I need to rethink my actual situation. Due to the WIN32 restriction for reading of LPT1 (and my limited knowledge in use of dlls), I wrote a DOS program that runs in a window on my WIN98 system. The program runs fine, but I have some delay loops to control some issues (since there is no timer()), and the win32 programs are a bit sluggish. I was thinking that sleep() (which is implemented in my package) would release the CPU somewhat and allow the WIN32 items to work better, but I can't wait an entire second. Perhaps that isn't the answer anyway. Does the WIN32 platform share a certain percentage of CPU time with a DOS window, or does the DOS window take what it feels necessary? The Borland package I have will create all kinds of executables and some (Easywin, for one) take total control of the machine.

    Take Care,
    Ed Hall

  • stoberstober Posts: 9,765Member ✭✭✭
    :
    : I guess I need to rethink my actual situation. Due to the WIN32 restriction for reading of LPT1 (and my limited knowledge in use of dlls), I wrote a DOS program that runs in a window on my WIN98 system. The program runs fine, but I have some delay loops to control some issues (since there is no timer()), and the win32 programs are a bit sluggish. I was thinking that sleep() (which is implemented in my package) would release the CPU somewhat and allow the WIN32 items to work better, but I can't wait an entire second. Perhaps that isn't the answer anyway. Does the WIN32 platform share a certain percentage of CPU time with a DOS window, or does the DOS window take what it feels necessary? The Borland package I have will create all kinds of executables and some (Easywin, for one) take total control of the machine.
    :
    : Take Care,
    : Ed Hall
    :


    The "DOS window" is not really a MS-DOS window at all -- its just another Win32 Window very similar to all other Win32 windows and shares the OS time exactly like all other windows -- regardless of what compiler you use. if you include windows.h in your program you can use Sleep(int milliseconds). Timing loops are of little use because (1) it depends on specific computer processor speed and (2) consums too much CPU time which is not very nice for other processes running on the same computer.
  • Ed HallEd Hall Posts: 395Member
    : The "DOS window" is not really a MS-DOS window at all -- its just another Win32 Window very similar to all other Win32 windows and shares the OS time exactly like all other windows -- regardless of what compiler you use. if you include windows.h in your program you can use Sleep(int milliseconds). Timing loops are of little use because (1) it depends on specific computer processor speed and (2) consums too much CPU time which is not very nice for other processes running on the same computer.
    :
    Thanks again,

    The Sleep() didn't work with my Borland compiler. It wasn't referenced in my windows.h as far as I can tell. I tried compiling my code with the Bloodshed C++ but it doesn't have inportb() and outport(). And I can't seem to find anything in the Help files for the Bloodshed package.

    Well, on to some further research. Thanks for all the help.

    Take Care,
    Ed

  • stoberstober Posts: 9,765Member ✭✭✭
    :
    : The Sleep() didn't work with my Borland compiler. It wasn't referenced in my windows.h as far as I can tell. I tried compiling my code with the Bloodshed C++ but it doesn't have inportb() and outport(). And I can't seem to find anything in the Help files for the Bloodshed package.
    :


    all those 16-bit functions are not supported by any 32-bit compiler. Its a whole new way of programming. If you are attempting to port a 16-bit program to windows console, you will probably wind up rewring all the screen i/o stuff and direct hardware reads/writes. You can't use inp() and outp() functions because they are not available. you have to go through the win32 device drivers.

    look in Winbase.h for the declaration of Sleep(). that header is included in windows.h.
Sign In or Register to comment.