Linux time-related struct and function sorting [reprinted]

Source: Internet
Author: User
Tags usleep


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.

 

    1. Struct timeb
    2. {
    3. Time_tTime;
    4. Unsigned short millitm;
    5. Short timezone;
    6. Short dstflag;
    7. };

 

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.

 

    1. Struct timeval
    2. {
    3. Long TV _sec; /*Seconds*/
    4. Long TV _usec; /*Microseconds*/
    5. };
Obtained by INT gettimeofday (struct timeval * TV, struct timezone * tz. The struct timezone structure is defined:
    1. Struct timezone
    2. {
    3. IntTz_minuteswest; /*How many minutes is the difference from Greewich?*/
    4. IntTz_dsttime; /*Daylight Saving Time status*/
    5. };

 

(4) struct timespec has two members, one being seconds and the other being nanoseconds. Therefore, the highest accuracy is nanoseconds.
    1. Struct timespec
    2. {
    3. Time_t TV _sec; /*Seconds*/
    4. Long TV _nsec; /*Nanoseconds*/
    5. };
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:

 

  1. Struct TM
  2. {
  3. IntTm_sec; /*Seconds*/
  4. IntTm_min; /*Minutes*/
  5. IntTm_hour; /*Hours*/
  6. IntTm_mday; /* DayOfMonth */
  7. IntTm_mon; /* Month */
  8. IntTm_year; /* Year */
  9. IntTm_wday; /* DayOf the week*/
  10. IntTm_yday; /* Day InTheYear */
  11. IntTm_isdst; /*Daylight SavingTime */
  12. };
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 ().
  1. UnsignedIntSleep(UnsignedIntSeconds);
  2. Void usleep(Unsigned long USEC);
  3. IntNanosleep(ConstStruct timespec*REQ,Struct timespec*Rem);
  4. Int Select(IntN,Fd_set*Readfds,Fd_set*Writefds,Fd_set*Exceptfds,Struct timeval*Timeout);
  5. 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.


Related Article

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.