20211314王艺达学习笔记8

发布时间 2023-11-05 14:21:31作者: 20211314王艺达

Unix/Linux系统编程第五章 定时器及时钟服务

5.1硬件定时器

定时器由时钟源和可编程计数器组成。时钟源会产生周期性电信号。计数器减为0时,计数器向CPU生成一个定时器中断,计数器周期称为定时器刻度,是系统的基本计时单元。

5.2个人计时定时器

实时时钟(RTC)

即使在个人计算机关机时,它也能连续运行。它用于实时提供时间和日期信息。

可编程间隔定时器(PIT)

PIT与CPU分离,提供以毫秒为单位的定时器刻度,在所有I/O设备中,PIT可以最高优先级IRO0中断,PIT定时器中断由Linux内核的定时器中断处理程序来处理。

多核CPU中的本地计时器

每个核都是一个独立的处理器,有自己的本地计时器。

高分辨率计时器

时间戳定时器(TSC)不适合作为实时设备,可提供纳秒级的定时器分辨率。

5.3/4CPU操作和中断处理

由于无效地址、非法指令、越权等问题,可能会出现异常或陷阱。CPU会异常处理程序,当指令执行结束时,CPU会检查挂起的中断。如果有中断请求,但CPU未处于接受中断的状态,CPU会忽略中断,继续执行指令。
中断处理和异常处理在操作系统内核中进行。对于每个中断,可以编程中断控制器来生成唯一的中断向量,标识中断源。CPU会用它作为中断向量表,其中包含指向中断处理程序入口地址的指针,终端结束后,CPU恢复指令正常执行。

5.5时钟服务函数

时钟服务可以通过系统调用、库函数和用户级命令调用
gettimeofday-settimeofday函数,gettimeofday()函数用于返回当前时间(当前秒的秒和微秒)。settimeofday()函数用于设置当前时间。在Unix/Linux中,时间表示自1970年1月1日00:00:00起经过的秒数。它可以通过库函数ctime(&time)转换为日历形式。
gettimeofday-settimeofday函数头文件如下:

#include <sys/time.h>
int gettimeofday(struct timeval *tv, struct timezone *tz);
int settimeofday(const struct timeval *tv, const struct timezone *tz);

CPU操作

每个CPU都有一个程序计数器(PC),也称为指令指针(IP),以及一个标志或状态寄存器(SR)、一个堆栈指针(SP)和几个通用寄存器,当PC指向内存中要执行的下一条指令时,SR包含CPU的当前状态,如操作模式、中断掩码和条件码,SP指向当前堆栈栈顶。堆栈是CPU用于特殊操作(如 push、pop调用和返回等)的一个内存区域。CPU操作可通过无限循环进行建模。

while (power-on){

(1).  fetch instruction:load*PC as instruction,increment PC to point to the

next instruction in memory;

(2).  decode instruction: interpret the instruction's operation code and

generate operandis;

(3).  execute instruction: perform operation on operands,write results to

memory if needed; execution may use the stack,implicitly change PC, etC.

(4) .  check for pending interrupts; may handle interrupts;

}

中断处理

外部设备(如定时器)的中断被馈送到中断控制器的预定义输入行,按优先级对中断输入排序,并将具有最高优先级的中断作为中断请求(IRQ)路由到 CPU。对于每个中断,可以编程中断控制器以生成一个唯一编号,叫作中断向量,标识中断源。在获取中断向量号后,CPU用它作为内存中中断向量表(AMD64 20I1)中的条目索引,条目包含一个指向中断处理程序入口地址的指针来实际处理中断。当中断处理结束时,CPU恢复指令的正常执行。

时钟服务函数

在linux下,常用的获取时间的函数有如下几个:
   asctime, ctime, gmtime, localtime, gettimeofday ,
  mktime, asctime_r, ctime_r, gmtime_r, localtime_r
1)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中

(2)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是线程安全的**/

(3)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 将时间以换为字符串字符串格式返回 **/

(4) 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结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果以字符串形态返回**/

(5)mktime() 将时间结构体struct tm的值转化为经过的秒数
函数原型:

 #include <time.h>
        
     time_t mktime(struct tm *tm);
        /**将时间结构体struct tm的值转化为经过的秒数**/

(6)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   

学习自测