How to Implement precise timing and sleep in Linux within seconds

Source: Internet
Author: User
In Linux, how to implement accurate timing and sleep in seconds or below-Linux general technology-Linux programming and kernel information is as follows. Sleep and alarm are provided in Linux, but they only provide sleep in seconds. In this case, some processes are obviously too long to sleep, so how can we make the process sleep at a lower time resolution?

There are two methods I know. Here we will introduce them separately.

The first method is to use a timer. The timer function provided by Linux is:

Int setitimer (int which, const struct itimerval * value, struct
Itimerval * ovalue );

Which specifies the timer type. Linux provides three types of Timers:
TIMER_REAL: accurate timer, which sends a SIGALRM signal upon timeout;
TIMER_VIRTUAL: a virtual timer that only records the process time. Therefore, the timer changes according to the process execution time and cannot be accurate. The SIGVTALRM signal is sent out upon timeout;
TIMER_PROF: Synopsis timer, which changes according to the process time and system time. It cannot implement accurate timing and times out to send a SIGPROF signal;
In the process, capture the signals sent by the timer, because after the process receives the signals sent by the timer timeout, the default action is to terminate.

Value indicates the timer time. The related structure is as follows:

Struct itimerval {
Struct timeval it_interval;
Struct timeval it_value;
};
Struct timeval {
Long TV _sec;
Long TV _usec;
};

It_interval specifies the interval and it_value specifies the initial time. If only it_value is specified, the timer is implemented once. If it_interval is specified at the same time, the system reinitializes it_value to it_interval after the timeout, to implement repeated timer; if both are cleared, the timer is cleared.

TV _sec provides second-level precision, while TV _usec provides microsecond-level precision. The first value is greater. Note that 1 s = 1000000 ms.

Ovalue is used to save the previous value, which is always NULL.

If you use a timer provided by setitimer to sleep, you only need to block and wait for the timer signal.

The second method is to use select to provide precise timing and sleep:

Int select (int n, fd_set * readfds, fd_set * writefds, fd_set * limit TFDs,
Struct timeval * timeout );

N refers to the range of monitored file descriptors. Generally, it is set to the fd + 1, readfds, writefds, and limit TFDs to be selected as read, write, and exception file descriptor sets, and timeout is the timeout time.

The following macros may be used for file descriptor set operations:
FD_CLR (int fd, fd_set * set); clear fd
FD_ISSET (int fd, fd_set * set); test whether fd is set
FD_SET (int fd, fd_set * set); Sets fd
FD_ZERO (fd_set * set); clears the descriptor set


We cannot use these macros at this time, because we do not care about the state of the file descriptor, And we care about the select timeout. Therefore, we need to set readfds, writefds, and effectfds to NULL and only specify the timeout time. We don't care about n, so you can set it to any non-negative value. The implementation code is as follows:



Int msSleep (long MS ){

Struct timeval TV;



TV. TV _sec = 0;

TV. TV _usec = MS;



Return select (0, NULL, & TV );

}

Haha, how is it, isn't it easy?

Conclusion:
Setitimer and select can both implement Precise sleep of processes. This article briefly introduces them and provides a simple implementation for select. I do not recommend setimer, because one Linux system provides limited timer (each process can have up to three different types of timer). In addition, it is not easy to implement ssetitimer.

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.