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.