1. Linux time
1.1 Linux Time System
1.2. Time-related data structure in Linux
2. Obtain the current time
3. latency
4. Timer
4.1. Alarm
4.2. setitimer
1Linux time
1.1 Linux Time System
UNIX and Linux time systems start from epoch (the beginning of the computer age), in seconds, and epoch is specified January 1, 1970 At 00:00:00 AM, Greenwich Mean Time (GMT.
Currently, most Unix systems use 32 bits to record the time. Positive values are expressed as 1970 and negative values are expressed as before January 1, 1970. We can easily calculate the time range:
2 ^ 31/86400 (S) = 24855.13481 (days )~ 68.0958 (year)
1970 + 68.0958 = 2038.0958
1970-68.0958 = 1901.9042
The time range is [1901.9042, 2038.0958].
The exact time is January 18, 2038 Monday evening 10:14:07. At that moment, the time will be converted to a negative number December 13, 1901 Black Friday 03:45:52 P.M., this is the so-called UNIX 2038 bug, or you can also call it Jason hatchet bug. In most Unix systems, there is no so-called Y2K problem, but there are still problems in 2038.
1.2. Time-related data structure in Linux
Struct timeval {
Int TV _sec;
Int TV _usec;
};
TV _sec is the number of seconds from epoch, while TV _usec is in microseconds (10e-6 second ).
Struct timezone {
Int TV _minuteswest;
Int TV _dsttime;
};
TV _minuteswest is the time difference from Greenwich Mean Time to the west, while TV _dsttime is the time correction method.
Struct timespec
{
Long int TV _sec;
Long int TV _nsec;
};
TV _nsec is nano second (10e-9 second ).
Struct TM
{
Int tm_sec;
Int tm_min;
Int tm_hour;
Int tm_mday;
Int tm_mon;
Int tm_year;
Int tm_wday;
Int tm_yday;
Int tm_isdst;
};
The number of seconds in the tm_sec table. In the range of [0, 61], two seconds are used to handle the second hop problem.
The number of "points" in the tm_min table, which is between [0, 59.
The "Hour" number in the tm_hour table, which is between [0, 23.
The tm_mday table "day of the month" is in the range.
The month of the current year in the tm_mon table, between [0, 11.
Tm_year adds 1900 to indicate that year.
The date of the tm_wday table, which is in the range of [0, 6.
The tm_yday table "the day of the current year". In the range of [0,365], the leap year has 366 days.
Whether the tm_isdst table is daylight saving time 」.
Struct itimerval {
Struct timeval it_interval;
Struct timeval it_value;
};
The it_interval Member indicates the initial value of the interval counter, while the it_value Member indicates the current value of the interval counter.
2, Get the current time
In all UNIX systems, there is a time () function.
Time_t time (time_t * t );
This function returns the number of seconds since Epoch. If t is non-null, it will enter the time value in T.
For some requirements that require high accuracy, Linux provides gettimeofday ().
Int gettimeofday (struct timeval * TV, struct timezone * tz );
Int settimeofday (const struct timeval * TV, const struct timezone * tz );
Struct TM format Time Function
Struct TM * gmtime (const time_t * t );
Converts it to Greenwich Mean Time. It is sometimes called GMT or UTC.
Struct TM * localtime (const time_t * t );
Time of conversion cost. It can be used to modify the TZ environment variables on a single machine. Different users indicate different times.
Time_t mktime (struct TM * TP );
Convert TM to time_t format and use local time.
Tme_t timegm (strut TM * TP );
Convert TM to time_t format and use UTC time.
Double difftime (time_t T2, time_t T1 );
Calculate the second difference.
Text time format functions
Char * asctime (struct TM * TP );
Char * ctime (struct TM * TP );
Both functions convert the time format to the standard UNIX time format.
Mon May 3 08:23:35 1999
Ctime uses the local time, while asctime uses the timezone information in the TM structure.
Size_t strftime (char * STR, size_t Max, char * FMT, struct TM * TP );
Strftime is a bit like sprintf, and its format is specified by FMT.
% A: the name of the day.
% A: The full name of the day.
% B: name of the month.
% B: The full name of the month.
% C: The format is the same as that of ctime/asctime.
% D: name of the day of the month, counted from zero.
% H: the hour of the day, in the 24-hour format, counted from zero.
% I: the hour of the day, in 12-hour format, counted from zero.
% J: the day of the current year, counted from zero.
% M: The month of the current year, counted from zero.
% M: the fraction of the hour, counted from zero.
% P: am or PM.
% S: the second of the minute, counted from zero.
% U: the number of the current year, which is calculated from the first day.
% W: the number of the current year, which is calculated from the first one.
% W: the number of days counted from zero.
% X: Local date.
% X: local time.
% Y: two-digit year.
% Y: the four-digit year.
% Z: abbreviation of the time zone name.
%: % Symbol.
Char * strptime (char * s, char * FMT, struct TM * TP );
Like scanf, the interpretation string is in the TM format.
% H: Same as % B and % B.
% C: Read % x and % x format.
% C: Read % C format.
% E: Same as % d.
% D: Read % m/% d/% Y format.
% K: Same as % H.
% L: Same as % I.
% R: Read the format "% I: % m: % S % P.
% R: Read the format "% H: % m.
% T: Read "% H: % m: % s" format.
% Y: read two-digit year.
% Y: Read the four-digit year.
The following example shows how to obtain the current system time:
Time_t now;
Struct TM * timenow;
Char strtemp [255];
Time (& now );
Timenow = localtime (& now );
Printf ("recent time is: % s/n", asctime (timenow ));
3Latency
The following functions can be used for latency:
Unsigned int sleep (unsigned int seconds );
Sleep () will cause the current program to fall into "hibernation" seconds, unless it receives a "uncertain" signal.
If sleep () is not full, it returns the remaining sleep time; otherwise, it returns zero.
Void usleep (unsigned long USEC );
Usleep is similar to sleep (), and the unit of seconds is 10e-6 seconds.
Int select (0, null, struct timeval * TV );
You can use the select function to implement sleep () without waiting for any event.
Int nanosleep (struct timespec * req, struct timespec * rem );
Nanosleep will sleep at the time specified by Req. If REM is non-null and not full, it will put the time to sleep on rem.
4, Timer
4.1. Alarm
Alarm () and signal () are enough if they are not required to be accurate.
Unsigned int alarm (unsigned int seconds)
Specifically set for the sigalrm signal. After the specified time is seconds, the sigalrm signal will be sent to the process itself, also known as the alarm time. After a process calls alarm, any previous alarm () call will be invalid. If the seconds parameter is zero, no alert time is included in the process. If the alarm time has been set in the process before calling alarm (), the remaining time of the previous alarm time is returned; otherwise, 0 is returned.
Example:
# Include <stdio. h>
# Include <unistd. h>
# Include <signal. h>
Void sigalrm_fn (INT sig)
{
/* Do something */
Printf ("alarm! /N ");
Alarm (2 );
Return;
}
Int main (void)
{
Signal (sigalrm, sigalrm_fn );
Alarm (2 );
/* Do someting */
While (1) pause ();
}
4.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 based on the actual execution time of the trip, and it sends the sigvtalrm signal.
Itimer_prof: calculates the actual schedule and the time spent in the core. It sends a 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;
SEC = 5;
Printf ("process ID is % d", 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 );
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 (;;)
;
}
The Screen Copy in this example is as follows:
Localhost :~ $./Timer_test
Process ID is 579
Catch a signal-sigvtalrm
Catch a signal-sigalrm
Catch a signal-sigvtalrm
Catch a signal-sigvtalrm
Catch a signal-sigalrm
Catch a signal-gvtalrm
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 (in Red Hat 7.2, 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.