The difference between Nanosleep () & Sleep () under Linux

Source: Internet
Author: User
Tags sleep time interval

Sleep () and Nanosleep () are awakened after a process sleeps for a period of time, but the two implementations are completely different

 

Sleep in user programs:

Sleep ()
Usleep ()
Nanosleep ()

Sleep () and Nanosleep () are awakened after a process sleeps for a period of time, but the two implementations are completely different.
The system call sleep () is not provided in Linux, which is implemented in the library function by calling alarm () to set the alarm time, calling Sigsuspend () to suspend the process on the signal sigalarm, and sleep () can only be accurate to the second level.

Nanosleep () is a system call in Linux, which is implemented using a timer, which causes the calling process to sleep and adds a timer_list timer to the timer queue, which includes the wake time and the functions that are executed after the wake up, time_list structure. The execution function of the timer added by Nanosleep () only completes the function of waking the current process. System through a certain mechanism to check these queues periodically (for example, through system calls into the core, before returning user state from the core, to check whether the current process of the time slice has been exhausted, if it is called schedule () function rescheduling, the function will check the timer queue, This check is also done before the slow interrupt is returned, and the function that is specified by the timer executes the wake-up call process if the timed time is exceeded. Of course, because the system time slice may be lost, so the nanosleep () precision is not very high.

Alarm () is also implemented by a timer, but its precision is only accurate to the second level, in addition, it sets the timer execution function is to send the SIGALRM signal to the current process at a specified time.

Copy Code code as follows:


#include <stdio.h>


#include <stdlib.h>


#include <unistd.h>


#include <sys/time.h>


#include <sched.h>


#define COUNT 1000


#define million 1000000l</p> <p>int main (void)


{


int i;


struct Timespec slptm;


long tdif;


struct timeval tend, tstart;</p> <p> slptm.tv_sec = 0;


slptm.tv_nsec = 1000; 1000 ns = 1 us</p> <p>//struct sched_param param;


//param.sched_priority = 0;


//sched_setscheduler (Getpid (), Sched_fifo, &param);</p> <p> if (Gettimeofday (&tstart, NULL) = =-1) {


fprintf (stderr, "Failed to get Start timen");


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 Timen");


return 1;


}


TDIF = million * (tend.tv_sec-tstart.tv_sec) + (TEND.TV_USEC-TSTART.TV_USEC);


printf ("Nanosleep () time is%ld USN", Tdif/count);


return 0;


}

HZ 250HZ
Clock Interrupt Time interval: 4 ms (1000MS/250)
----------------------------------------
Nanosleep () is 4019 us (4.019 ms)
Indicates that the Nanosleep sleep timer relies on clock interrupts

hz                                  1000HZ
time interval for clock interrupts:                    1 Ms
----------------------------------------
Nanosleep () is a US
Note: Minimum sleep time of 1 US

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.