Reprint lyj: http://blog.chinaunix.net/space.php? Uid = 14617649 & Do = Blog & id = 3058661
1. Time type.There are four common time types in Linux: time_t, struct timeb, struct timeval, struct timespec, clock_t, and struct TM.
(1) time_t is a long integer, which is generally used to indicate the number of seconds since January 1, 1970.
This type is defined in <sys/time. h>.
It is generally obtained through time_t time = Time (null.
(2) struct timeb structure: There are two main members: one is the second, the other is the millisecond, the accuracy is the millisecond.
- Struct timeb
- {
- Time_tTime;
- Unsigned short millitm;
- Short timezone;
- Short dstflag;
- };
Timeb is obtained by the int ftime (struct timeb * TP) function.
0 is returned for success,-1 is returned for failure.
(3) struct timeval has two members, one being seconds and the other being subtle.
- Struct timeval
- {
- Long TV _sec; /*Seconds*/
- Long TV _usec; /*Microseconds*/
- };
Obtained by INT gettimeofday (struct timeval * TV, struct timezone * tz. The struct timezone structure is defined:
- Struct timezone
- {
- IntTz_minuteswest; /*How many minutes is the difference from Greewich?*/
- IntTz_dsttime; /*Daylight Saving Time status*/
- };
(4) struct timespec has two members, one being seconds and the other being nanoseconds. Therefore, the highest accuracy is nanoseconds.
- Struct timespec
- {
- Time_t TV _sec; /*Seconds*/
- Long TV _nsec; /*Nanoseconds*/
- };
It is generally obtained by the long clock_gettime (clockid_t which_clock, struct timespec * TP) function. Gets the time of a specific clock. The time is transmitted back through the TP structure. Currently, six clock types are defined:
Clock_realtime: current time, counted from 1970, January 1, 1.1
Clock_monotonic system startup time, cannot be set
Clock_process_cputime_id process running time
Clock_thread_cputime_id thread running time
High-Precision clock_realtime_hr clock_realtime version
Clock_monotonic_hr clock_monotonic high-precision version
Get the time precision of a specific clock:
Long clock_ Getres (clockid_t );
Set the time of a specific clock:
Long clock_settime (clockid_t, struct timespec *);
The time specified in the sleep time. If a signal is interrupted and returned in advance, left_time returns the remaining time:
Long clock_nanosleep (clockid_t, int flag, timespec * time, timespec * left_time );
(5) clock_t type, which is obtained by clock_t clock.
Indicates the CPU time occupied by the process, accurate to microseconds.
(6) struct TM is a Time Representation Method in an intuitive sense:
-
- Struct TM
-
- {
-
- IntTm_sec; /*Seconds*/
-
- IntTm_min; /*Minutes*/
- IntTm_hour; /*Hours*/
-
- IntTm_mday; /* DayOfMonth */
-
- IntTm_mon; /* Month */
-
- IntTm_year; /* Year */
- IntTm_wday; /* DayOf the week*/
-
- IntTm_yday; /* Day InTheYear */
-
- IntTm_isdst; /*Daylight SavingTime */
-
- };
Struct TM * gmtime (const time_t * timep ); Struct TM * localtime (const time_t * timep ); Time_t mktime (struct TM * TM ); The gmtime and localtime parameters and return values are of the same type. The difference is that the Greenwich Mean Time returned by the former and the latter is the local time. Note: The three functions are thread-safe. To use a thread-safe version, use the version with _ r -- gmtime_r, localtime_r, and mktime_r.
Ii. latency Functions Latency functions include: Sleep (), usleep (), nanosleep (), select (), and pselect ().
-
- UnsignedIntSleep(UnsignedIntSeconds);
-
- Void usleep(Unsigned long USEC);
-
- IntNanosleep(ConstStruct timespec*REQ,Struct timespec*Rem);
- Int Select(IntN,Fd_set*Readfds,Fd_set*Writefds,Fd_set*Exceptfds,Struct timeval*Timeout);
-
- IntPselect(IntN,Fd_set*Readfds,Fd_set*Writefds,Fd_set*Exceptfds, ConstStruct timespec*Timeout, ConstSigset_t*Sigmask);
The alarm function is a signal delay, which is not intuitive.
Only by using the time parameter type in the function prototype, we can guess that sleep can be accurate to seconds, usleep/select can be accurate to the subtle level, and nanosleep and pselect can be accurate to the nanosecond level.
In actual implementation, the nanosleep and alarm on Linux are both implemented based on the kernel clock mechanism. Due to the implementation of the Linux kernel clock, the precision cannot reach the nanosecond level, man nanosleep can also see this description, the accuracy given in Man is: Linux/i386 is 10 MS, Linux/Alpha is 1 ms.