Sleep function of C language

Source: Internet
Author: User
Tags sleep function usleep

Sleep function:

Function: Execution hangs for a period of time

Usage: unsigned sleep (unsigned seconds);

Attention:

Used in VC with top file # # <WINDOWS.H> under Linux, the GCC compiler uses a header file that differs depending on the GCC version # include <unistd.h>

In the VC, sleep in the first English characters in uppercase "S", in Linux do not capitalize, in the standard C is sleep, do not capitalize, simply say VC with sleep, all other uses sleep

In the VC, the Unit in Sleep () is in milliseconds, so if you want the function to be stuck for 1 seconds, it should be sleep (1000); Under Linux, the units in sleep () are seconds, not milliseconds.

Example:

#include <stdio.h>#include<windows.h>int  main () {    int a=  ;    Sleep (+);//stall 3s print a    printf ("%d", a);     return 0 ;}

Usleep function:

Function: The Usleep function suspends the process for a period of time, in microseconds us (one out of 10,000 seconds).

Syntax: void usleep (int micro_seconds);

return value: None

Note: This function does not work in the Windows operating system.

Usleep () is similar to sleep () for delaying the suspend process. The process is suspended and placed into the Reday queue. Just in general, use the sleep () function whenever possible with a delay of magnitude of seconds. And this function has been revoked, you can use Nanosleep.

If the delay time is dozens of milliseconds, or smaller, use the usleep () function whenever possible. This will make the best use of CPU time.

Delay function:

Function: Pause the execution of a program for a period of milliseconds MS (1 per thousand seconds)

Usage: void delay (unsigned milliseconds);

Example:

#include <dos.h> int main (void) {sound ();  Delay (+);  Nosound ();   return0; }

Delay () is a cyclic wait, and the process is still running, consuming the processor.

Sleep () is different, it is suspended, and the processor is ceded to other processes.

Sleep function of C language

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.