Sleep () and nanosleep () wake up the process after a period of sleep, but the implementation of the two is completely different.
Sleep in user programs:
Sleep ()
Usleep ()
Nanosleep ()
Sleep () and nanosleep () wake up the process after a period of sleep, but the implementation of the two is completely different.
In Linux, the system call sleep () is not provided. sleep () is implemented in the library function. it sets the alarm time by calling alarm () and calls sigsuspend () the process is suspended on the SIGALARM signal, and sleep () can only be accurate to seconds.
Nanosleep () is a system call in Linux. it is implemented using a timer, which sleep the calling process and adds a timer_list timer to the timer queue, the time_list structure includes the wake-up time and the functions executed after the wake-up. the timer execution function added by nanosleep () can only wake up the current process. The system regularly checks these queues through a certain mechanism (for example, after a system call falls into the core, check whether the time slice of the current process is exhausted before returning the user state from the core, if yes, call the schedule () function to re-schedule the task. in this function, the timer queue is checked, and the timer queue is also checked before the slow interrupt is returned.) if the scheduled time has exceeded, then, the function specified by the timer is executed to wake up the calling process. Of course, because the system time slice may be lost, the precision of nanosleep () is not very high.
Alarm () is also implemented through the timer, but its precision is only accurate to seconds. In addition, the timer execution function it sets sends a SIGALRM signal to the current process at a specified time.
Copy codeThe code is as follows:
# Include
# Include
# Include
# Include
# Include
# Derefined COUNT 1000
# Define MILLION limit 00l
Int main (void)
{
Int I;
Struct timespec slptm;
Long tdif;
Struct timeval tend, tstart;
Slptm. TV _sec = 0;
Slptm. TV _nsec = 1000; // 1000 ns = 1 us
// Struct sched_param param;
// Param. sched_priority = 0;
// Sched_setscheduler (getpid (), SCHED_FIFO, & param );
If (gettimeofday (& tstart, NULL) =-1 ){
Fprintf (stderr, "Failed to get start time \ n ");
Return 1;
}
For (I = 0; I <COUNT; I ++ ){
If (nanosleep (& slptm, NULL) =-1 ){
Perror ("Failed to nanosleep ");
Return 1;
}
}
If (gettimeofday (& tend, NULL) =-1 ){
Fprintf (stderr, "Failed to get end time \ n ");
Return 1;
}
Tdif = MILLION * (tend. TV _sec-tstart. TV _sec) + (tend. TV _usec-tstart. TV _usec );
Printf ("nanosleep () time is % ld us \ n", tdif/COUNT );
Return 0;
}
HZ 250 HZ
Clock interruption interval: 4 ms (1000 ms/250)
----------------------------------------
Nanosleep () time is 4019 us (4.019 MS)
It indicates that the sleeping timer of nanosleep depends on clock interruption.
HZ 1000 HZ
Interval of clock interruption: 1 MS
----------------------------------------
Nanosleep () time is 12 us
Note: The minimum sleep time is 1 us.