Linux time & timer Introduction

Source: Internet
Author: User

1. Time Representation

In the program, we often need to output the current time of the system, for example, the output result using the date command. At this time, we can use the following two functions:

# Include

Time_t time (time_t * tloc );

Char * ctime (const time_t * Clock );

The time function returns the number of seconds since, January 1, January 1, 1970. stored in the time_t structure. however, the return value of this function has no practical significance for us. in this case, we use the second function to convert seconds to strings. the return type of this function is fixed: a possible value is. thu dec7 14:58:59 2000 the string length is fixed to 26.

2. Time Measurement

Sometimes we need to calculate the execution time of the program. For example, we need to analyze the algorithm time. At this time, we can use the following function. # include

Int gettimeofday (struct timeval * TV, struct timezone * tz );

Strut timeval {

Long TV _sec;/* seconds */

Long TV _usec;/* Number of microseconds */

};

Gettimeofday stores the time in the Structure TV. tz Is generally replaced by null.

3. Timer usage

3.1. Alarm
-------------------------------------------
Alarm () and signal () are enough if they are not required to be accurate.
Unsigned int alarm (unsigned int seconds)
Function Description: Alarm () is used to set the signal sigalrm to be transmitted to the current process after the number of seconds specified by the seconds parameter. If the seconds parameter is 0, the previously set alarm is canceled and the remaining time is returned.
Return Value: returns the remaining seconds of the previous alarm. If no alarm is set, 0 is returned.
After alarm () is executed, the process continues to be executed. In the later stage (after alarm), the process will receive the signal sigalrm and execute its processing function in seconds.
# Include <stdio. h>
# Include <unistd. h>
# Include <signal. h>
Void sigalrm_fn (INT sig)
{
Printf ("alarm! /N ");
Alarm (2 );
Return;
}
Int main (void)
{
Signal (sigalrm, sigalrm_fn );
Alarm (1 );
While (1) pause ();
}

3.2. setitimer ()
-------------------------------------------
Int setitimer (INT which, const struct itimerval * value, struct itimerval * ovalue ));
Setitimer () is more powerful than alarm and supports three types of Timers:
Itimer_real: calculated based on the real time of the system. It sends the sigalrm signal.
Itimer_virtual:-calculate the time spent by the process in the user State, and it sends the sigvtalrm signal.
Itimer_prof: calculates the time spent by the process in the user and kernel modes, and sends the sigprof signal.
Setitimer () the first parameter which specifies the timer type (one of the above three); the second parameter is an instance of the structure itimerval; the third parameter can not be processed.
If setitimer () is called successfully, 0 is returned; otherwise,-1 is returned.

The following is a simple example of setitimer calling. In this example, a sigalrm is sent every second and a sigvtalrm signal is sent every 0.5 seconds:
# Include <stdio. h>
# Include <stdlib. h>
# Include <unistd. h>
# Include <signal. h>
# Include <time. h>
# Include <sys/time. h>
Int sec;
Void sigroutine (INT signo ){
Switch (signo ){
Case sigalrm:
Printf ("catch a signal -- sigalrm/N ");
Signal (sigalrm, sigroutine );
Break;
Case sigvtalrm:
Printf ("catch a signal -- sigvtalrm/N ");
Signal (sigvtalrm, sigroutine );
Break;
}
Return;
}
Int main ()
{
Struct itimerval value, ovalue, value2; // (1)
SEC = 5;
Printf ("process ID is % d/N", getpid ());
Signal (sigalrm, sigroutine );
Signal (sigvtalrm, sigroutine );
Value. it_value. TV _sec = 1;
Value. it_value. TV _usec = 0;
Value. it_interval. TV _sec = 1;
Value. it_interval. TV _usec = 0;
Setitimer (itimer_real, & Value, & ovalue); // (2)
Value2.it _ value. TV _sec = 0;
Value2.it _ value. TV _usec = 500000;
Value2.it _ interval. TV _sec = 0;
Value2.it _ interval. TV _usec = 500000;
Setitimer (itimer_virtual, & value2, & ovalue );
For (;;)
;
}
(1) struct itimerval
Struct itimerval {
Struct timeval it_interval;/* timer interval */
Struct timeval it_value;/* Current Value */
};
Itimerval: I --> Interval
Val --> Value
It_value in the itimerval structure is the time for reduction. When this value is 0, a corresponding signal is sent. Then, it_value is set to it_interval.
(2) setitimer ()
Setitimer () sets a timer for the process in which it is located, if itimerval. if the value of it_interval is not 0 (neither of the two it_interval fields is 0), the timer will remain valid (a signal will be sent every time)
Note: The Linux signal mechanism is basically inherited from UNIX systems. In early Unix systems, the signal mechanism was relatively simple and original, and some problems were exposed in practice. Therefore, the signals built on the early mechanism were called "unreliable signals ", signals smaller than sigrtmin (sigrtmin = 32, sigrtmax = 63) are unreliable signals. This is the source of "unreliable signal. The main problem is that each time a process processes a signal, it sets the response to the signal as the default action. In some cases, it may lead to incorrect signal processing. Therefore, if you do not want such an operation, you need to call signal () again at the end of the signal processing function (), reinstall the signal.

***********************************

 

How to Implement precise timing and sleep in Linux within seconds

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.

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/zhuky/archive/2009/12/17/5018511.aspx

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.