Unix/Linux系统编程(定时器及时钟服务)
一.硬件定时器
定时器:由时钟源和可编程计数器组成的硬件设备。(功能类似CPU的闹钟,一段时间后发出中断信号提醒CPU做下一件事)
时钟源:通常是一个晶体振荡器,会产生周期性电信号,以精确的频率驱动计数器。
硬件定时器工作原理:使用一个倒计时值对计数器进行编程,每个时钟信号减一。当计数器减为0时,计数器向CPU生成一个定时器中断,将计数值重新加载到计数器中,并重复倒计时。计数器周期称为定时器刻度,是系统的基本计时单元。
二.个人计算机定时器
基于Intel x86 的个人计算机有数个定时器:
(1)实时时钟(RTC)
(2)可编程间隔定时器(PIT)
(3)多核CPU中的本地定时器
(4)高分辨定时器
三.CPU操作
  每个CPU都有一个程序计数器(PC),也称为指令指针(IP),以及一个标志或状态寄存器(SR)、一个堆栈指针(SP)和几个通用寄存器,当PC指向内存中要执行的下一条指令时,SR包含CPU的当前状态,如操作模式、中断掩码和条件码,SP指向当前堆栈栈顶。
  CPU操作可通过无限循环进行建模。
1 while (power-on){ 2 3 (1)fetch instruction:load*PC as instruction,increment PC to point to the 4 next instruction in memory; 5 6 (2)decode instruction: interpret the instruction's operation code and 7 generate operandis; 8 9 (3)execute instruction: perform operation on operands,write results to 10 memory if needed; execution may use the stack,implicitly change PC, etC. 11 12 (4)check for pending interrupts; may handle interrupts; 13 }
四.中断处理
  在每条指令执行结束时,如果CPU未处于接受中断的状态,即在CPU的状态寄存器中屏蔽了中断。它将忽略中断请求.使其处于挂起状态,并继续执行下一条指令。
对于每个中断,可以编程中断控制器以生成一个唯一编号,叫作中断向量,标识中断源。
五.时钟服务函数
- 在linux下,常用的获取时间的函数有如下几个:
 asctime, ctime, gmtime, localtime, gettimeofday
 mktime, asctime_r, ctime_r, gmtime_r, localtime_r
time() 函数获取当前时间
 SYNOPSIS
         #include <time.h>
 
        time_t time(time_t *t);
  
  DESCRIPTION
        time() returns the time as the number of seconds since the Epoch, 1970-01-01 00:00:00+0000 
        (UTC).8    //此函数会返回从公元1970年1月1日的UTC时间从0时0分0秒算起到现在所经过的秒数。如果t 并非空指针的话,此函数也会将返回值存到t指针所指的内存。
  RETURN VALUE
        On  success,  the value of time in seconds since the Epoch is returned.  On error, ((time_t) -1) is returned, and errno is
        set appropriately.
 ERRORS
        EFAULT t points outside your accessible address space.
     //成功返回秒数,错误则返回(time_t) -1),错误原因存于errno中
localtime_r() localtime()取得当地目前时间和日期
函数原型如下:
 #include <time.h>
       
     struct tm *localtime(const time_t *timep);
     struct tm *localtime_r(const time_t *timep, struct tm *result);
         
 /*该函数将有time函数获取的值timep转换真实世界所使用的时间日期表示方法,然后将结果由结构tm返回*/
 
 /**需要注意的是localtime函数可以将时间转换本地时间,但是localtime函数不是线程安全的。
多线程应用里面,应该用localtime_r函数替代localtime函数,因为localtime_r是线程安全的**/
asctime() asctime_r()将时间和日期以字符串格式返回‘
函数原型如下:
  #include <time.h>
         
      struct tm *gmtime(const time_t *timep);
      struct tm *gmtime_r(const time_t *timep, struct tm *result);
         
      char *asctime(const struct tm *tm);
      char *asctime_r(const struct tm *tm, char *buf);
         
         
 /**gmtime是把日期和时间转换为格林威治(GMT)时间的函数。将参数time 所指的time_t 结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果由结构tm返回**/
        
 /**asctime 将时间以换为字符串字符串格式返回 **/
ctime(),ctime_r()将时间和日期以字符串格式表示
函数原型如下:
 #include <time.h>
        
       char *ctime(const time_t *timep);
       char *ctime_r(const time_t *timep, char *buf);
        
 /**ctime()将参数timep所指的time_t结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果以字符串形态返回**/
mktime()将时间结构体struct tm的值转化为经过的秒数
函数原型:
 #include <time.h>
        
     time_t mktime(struct tm *tm);
        /**将时间结构体struct tm的值转化为经过的秒数**/
gettimeofday()获取当前时间
函数原型如下:
  #include <sys/time.h>
  
      int gettimeofday(struct timeval *tv, struct timezone *tz);
      
  struct timeval {
                 time_t      tv_sec;     /* seconds (秒)*/
                 suseconds_t tv_usec;    /* microseconds(微秒) */
             };
  struct timezone {
                int tz_minuteswest;     /* minutes west of Greenwich */
                int tz_dsttime;         /* type of DST correction */1          };
 //gettimeofday函数获取当前时间存于tv结构体中,相应的时区信息则存于tz结构体中
 //需要注意的是tz是依赖于系统,不同的系统可能存在获取不到的可能,因此通常设置为NULL  

 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号