In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
What is the difference between nanosleep () and sleep () in linux? In view of this problem, this article introduces the corresponding analysis and answers in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.
What is Linux system Linux is a free-to-use and free-spread UNIX-like operating system, is a POSIX-based multi-user, multi-task, multi-threaded and multi-CPU operating system, using Linux can run major Unix tools, applications and network protocols.
Sleep in the user program:
Sleep ()
Usleep ()
Nanosleep ()
Both sleep () and nanosleep () wake up the process after a period of sleep, but their implementations are completely different.
Linux does not provide the system call sleep (), sleep () is implemented in the library function, it is through the call alarm () to set the alarm time, call sigsuspend () to suspend the process on the signal SIGALARM, sleep () can only be accurate to seconds.
Nanosleep () is a system call in Linux, which is realized by a timer, which makes the calling process sleep and adds a timer_ list timer to the timer queue. The time_list structure includes the wake-up time and the function executed after waking up. The execution function of the timer added by nanosleep () only completes the function of waking up the current process. The system checks these queues regularly through certain mechanisms (for example, after falling into the core through the system call, and before returning to the user state from the core, check whether the time slice of the current process has been exhausted. If so, call the schedule () function to reschedule, the timer queue will be checked in this function, and this check will also be done before the slow interrupt returns), if the timing time has exceeded 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 a timer, but its precision is only up to seconds. In addition, the timer execution function it sets is to send a SIGALRM signal to the current process at a specified time.
The code is as follows:
# include
# include
# include
# include
# include
# define COUNT 1000
# define MILLION 1000000L
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; iTunes +) {
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 250HZ
Interval between clock interrupts: 4 ms (1000ms/250)
-
Nanosleep () time is 4019 us (4.019 ms)
It shows that the sleep timer of nanosleep depends on clock interrupts.
HZ 1000HZ
Interval between clock interrupts: 1 ms
-
Nanosleep () time is 12 us
Note: the minimum sleep time is 1 us.
The answer to the question about the difference between nanosleep () and sleep () in linux is shared here. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un