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.