Home >Operation and Maintenance >Linux Operation and Maintenance >What is the difference between usleep and sleep in linux?

What is the difference between usleep and sleep in linux?

WBOY
WBOYOriginal
2022-07-19 15:00:564177browse

The difference between usleep and sleep in Linux: The sleep function is used to execute the suspension for the specified number of seconds, while the usleep function is used to execute the suspension for the specified number of microseconds. sleep is a non-system call, implemented in a library function, and uses alarm() to set the alarm time. usleep is similar to sleep in use except that the time unit is microseconds.

What is the difference between usleep and sleep in linux?

#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.

What is the difference between usleep and sleep in Linux

(1) sleep()-------in seconds

Head File:

 #include <windows.h> // 在VC中使用带上头文件
        #include <unistd.h>  // 在gcc编译器中,使用的头文件因gcc版本的不同而不同

Function: Execute suspension for the specified number of seconds

Syntax:

unsigned int sleep(unsigned int seconds);

sleep() is not a system call, sleep() is in the library function It is implemented by setting the alarm time through alarm() and suspending the process on the signal SIGALARM using sigsuspend().

sleep() can only be accurate to the second level. sleep() will suspend the current process until the time specified by the seconds parameter is reached or it is interrupted by a signal.

#includef55648144b4a1c5cce7ad2f6519be0f3

return: If the process is suspended to the time specified by the seconds parameter, 0 is returned if successful. If there is a signal interruption, the remaining seconds are returned.

Example:

 #include <windows.h>
 #include<stdio.h>
 int main()
 {
  int a;
  a=1;
  printf("hello");
  sleep(a);        /* VC 使用Sleep*/
  printf("world");
  return 0;
 }

(2) usleep()----in microseconds

Header file:

#include <unistd.h>

Function: usleep function suspends the process for a period of time, the unit is microseconds (millionths of a second);

Syntax:

void usleep(int micro_seconds);

Except the time unit is Other than microseconds, it is similar to sleep() in use. In addition, the implementation is also different. Because sleep is implemented using alarm, the time unit is s, while the time unit of usleep is us

, which is definitely not implemented by alarm, so their implementations are different. , but they are all used in Linux, but cannot be used in Windows, because both sleep and usleep are defined under unistd.h.

#includef55648144b4a1c5cce7ad2f6519be0f3

return: If the process is suspended to the time specified by the seconds parameter, 0 will be returned if successful. If there is a signal interruption, the remaining microseconds will be returned.

Return value: None

Content description: This function can temporarily stop the execution of the program. The parameter micro_seconds is the number of microseconds (us) to be paused.

Note:

This function cannot work in Windows operating system. Used in Linux test environment.

See: usleep() is similar to sleep(), used to delay suspended processes. The process is suspended and placed on the reday queue.

Under normal circumstances, when the delay time is on the order of seconds, use the sleep() function as much as possible.

If the delay time is tens of milliseconds (1ms = 1000us), or smaller, use the usleep() function if possible. Only in this way can you make the best use of CPU time

Recommended learning: Linux video tutorial

The above is the detailed content of What is the difference between usleep and sleep in linux?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn