Linux System Programming Learning Notes (11) Time

Source: Internet
Author: User
Tags sleep function time and date

1. The kernel provides three different ways to record time:

Wall Time(or real time): actual time and date in the real worldProcess Time: The time, a process spends executing on a processor includes user times and system time themonotonic Time: Use the system ' suptime (time since boot)For this purpose,guarantee, the time source is strictly linearly increasing UNIX represents absolute timing: the number of elapsed seconds s Ince the epoch, which is defined as00:00:00UTC on the morning of 1 January 1970On Linux, the frequency of the system timer is called HZ,The value of HZ is Architecture-specificPOSIX functions that return time in terms of the clock ticks use clocks_per_sec To represent the fixed frequency 2. The original representation of the data structure of the time representation:
long time_t;
That's won ' t last long before Overflowing! Microsecond precision means:
 #include <sys/time.h> Timeval {time_t tv_sec;  /*   seconds   suseconds_t Tv_usec;  /*   microseconds  Span style= "Color:rgb (0, 128, 0); >*/ };  
<time.h>struct  timespec {        time_t  tv_sec;        /*  */        long    tv_nsec;      /* */};

3. Get the current time

#include <time.h>*t);

Time () returns the current time represented as the number of seconds elapsed since the epoch

#include <sys/time.h>int gettimeofday(structstruct timezone *tz);
Gettimeofday () places the current time of the timeval structure pointed at by TV and returns 0 parameter TZ is always null
 struct   Timeval TV;  int   Ret;ret  = gettimeofday (&tv, NULL ); if   (ret) perror (  gettimeofday   "  else   printf (  seconds=%ld useconds=%ld\n  " , ( long ) Tv.sec, (long ) tv.tv_usec); 

#include <sys/times.h>structTMS {clock_t tms_utime; /*User Time consumed*/clock_t Tms_stime; /*system time consumed*/clock_t Tms_cutime; /*user time consumed by children*/clock_t Tms_cstime; /*system time consumed by children*/};clock_t Times (structTMS *buf);
User time is the time spent executing code in user space.  System time is the time spent executing code in kernel space. 4. Set the current time
#include <time.h>int stime (time_t *<sys/time.h>int settimeofday (const  structconststruct timezone *tz);

Parameter TZ is always null

struct 31415926 27182818  }; int  = Settimeofday (&TV, NULL); if (ret)        perror ("settimeofday");
5. Sleep hibernation
/*   puts the invoking process to  sleepfor the number of seconds*/<unistd.h> /c6>intint seconds);

/*  */#define _xopen_source<unistd.h>int  int usec);
#include <sys/Select.h>intSelect (intstruct timeval *timeout);

The Select function sets the parameter n to 0, and the three detection event set is set to null, when the select is equivalent to a sleep function of the exact time, and this usage is most portable

struct 0 757  }; Select (0, NULL, NULL, NULL, &TV);

6. Timer timers provide a mechanism for notifying a process when a given amount of time elapses simple timer:
#include <unistd.h>intint seconds);

Schedules the delivery of a SIGALRM signal to the invoking process after seconds of real time has elapsed

void alarm_handler (int  signum) {        printf ("Five seconds passed!\n " );} void func (void) {        signal (SIGALRM, alarm_handler);        Alarm (5);        Pause ();}

Interval Timer:

#include <sys/time.h>intGetitimer (intWhich,structItimerval *value);intSetitimer (intWhich,Const structItimerval *value,structItimerval *ovalue);structItimerval {structTimeval It_interval;/*Next Value*/        structTimeval It_value;/*Current value*/};structTimeval {LongTv_sec;/*seconds*/        LongTv_usec;/*microseconds*/};
voidAlarm_handler (intSigno) {printf ("Timer hit!\n");}voidFoo (void){        structitimerval delay; intret;        Signal (SIGALRM, Alarm_handler); Delay.it_value.tv_sec=5; Delay.it_value.tv_usec=0; Delay.it_interval.tv_sec=1; Delay.it_interval.tv_usec=0; RET= Setitimer (Itimer_real, &delay, NULL); if(ret) {perror ("Setitimer"); return; } pause ();}

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: 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.