Differences between clock (), sleep (), and sleep () in Windows and Linux

Source: Internet
Author: User

Recently, for Linux development on arm, C Programs Written in windows need to be transplanted to Linux. The underlying SPI driver implementation and socket communication on the upper layer need to be rewritten, the application also needs to be changed. It does not take much effort to make the program run smoothly throughout the process. Thanks to the powerful eclipse + arm-Linux-GCC combination, however, many interesting problems are found during debugging. One of them is about the differences between sleep () functions in windows and sleep () functions in Linux.

The sleep () function in windows must contain windows. h header file, while the header file to be included in Linux is unistd. the H header file indicates that the sleep () function is not a standard C language library, and sleep () in Windows is sleeping in milliseconds, while sleep () function in Linux is in seconds, if you need to implement a more accurate time, you can use the usleep () function in Linux. It is subtle. In Windows, it does not seem to be more accurate. It can only be at the millisecond level (personal opinion, not confirmed yet ). Speaking of this, the difference between Windows and Linux is actually very small. After the combination of clock () and clock () functions, we find the difference between them, in which clock () A function is a function provided by the Standard C language library. The function is as follows:

Clock returns the processor time used by program since the beginning of the execution, or-1 if unavailable. Clock ()/clocks_per_sec is a time in seconds.

Here we mention that the program running process returned by the clock () function consumes the process time, that is, the CPU time. In Windows and Linux, we test the following code respectively:

Windows:

#include <stdio.h>#include <time.h>#include <windows.h>int main(){printf("The start clock is: %ld\n", clock());Sleep(2000);printf("The end clock is: %ld\n", clock());return 0;}

Linux:

#include <stdio.h>#include <time.h>#include <unistd.h>int main(){        printf("The start clock is: %ld\n", clock());        sleep(2);        printf("The end clock is: %ld\n", clock());        return 0;}

Running result:

Windows:

The start clock is: 1The end clock is: 2001

Linux:

The start clock is: 0The end clock is: 0

This indicates that in Windows sleep (), processor time is used. In Linux, sleep () does not use processor time, which may be different from the underlying sleep () implementation mechanism.

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.