Sleep function in C Language

Source: Internet
Author: User
Tags sleep function usleep

Sleep function in C Language

Sleep function:

Function: the execution is suspended for a period of time.

Usage: unsigned sleep (unsigned seconds );

Note:

Use the header file # include <windows. h> in VC. in Linux, the header file used by the gcc compiler varies with the gcc version # include <unistd. h>

In VC, the first English character in Sleep is "S" in uppercase. in linux, do not use uppercase letters. In Standard C, do not use sleep. In short, VC uses Sleep, use sleep for other purposes

In VC, Sleep () is measured in milliseconds. Therefore, if you want to keep the function for 1 second, it should be Sleep (1000). in Linux, sleep () the Unit is second, not millisecond.

Example:
#include <stdio.h>
#include <windows.h>
int main ()
{
    int a = 100;
    Sleep (3000); // Stagnation 3s print a
    printf ("% d", a);
    return 0;
}
usleep function:

Function: usleep function suspends the process for a period of time, the unit is microsecond us (one millionth of a second).

Syntax: void usleep (int micro_seconds);

Return value: None

Note: This function cannot work in Windows operating system.

usleep () is similar to sleep () and is used to delay the suspend process. The process is suspended and placed in the day queue. Only under normal circumstances, when the delay time is in the order of seconds, use the sleep () function as much as possible. And this function has been abolished, you can use nanosleep.

If the delay time is tens of milliseconds or less, use the usleep () function whenever possible. Only in this way can the CPU time be used optimally.

delay function:

Function: pause the execution of the program for a period of time, the unit is millisecond ms (one thousandth of a second)

Usage: void delay (unsigned milliseconds);

Example:

#include <dos.h>

int main (void)

{

sound (440);

delay (500);

nosound ();

return 0;

}
delay () is a loop waiting, the process is still running, occupying the processor.

Unlike sleep (), it will be suspended, giving the processor to other processes.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.