《Unix/Linux系统编程》学习笔记8

第五章 定时器及时钟服务

一、知识点归纳

(一)硬件定时器

 定时器是由时钟源和可编 程计数器组成的硬件设备。时钟源 通常是一个晶体振荡器,会产生周期性电信号,以料青确的频率驱动计数器。使用一个倒计时值对计数器进行编程,每个时钟信号减1。当计 改减为0时,计数器向CPU生成一个定时器中断,将计数值重新加载到计数器中,并重复复倒计时。计数器周期称为定时器刻店度,是系统的基本计时单元。

(二)个人计算机定时器

(三)CPU操作

(四)中断处理

(五)时钟服务函数

1.gettimeofday-settimeofday

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

这些是对 Linux 内核的系统调用。第一个参数 tv 指向一个 timeval 结构体。

struct timeval{
    time_t         tv_sec;    /* seconds */
    suseconds_t    tv_usec;   /* microseconds */
};

第二个参数 timezone 已过期,应设置为 NULL。gettimeofday() 函数用于返回当前时间(当前秒的秒和微秒)。settimeofday() 函数用于设置当前时间。在 Unix/Linux 中,时间表示自 1970年1月1日 00:00:00 起经过的秒数。它可以通过库函数 ctime(&time) 转换为日历形式。下面给出了 gettimeofday() 函数和settimeofday() 函数的示例。

(1)gettimeofday 系统调用

(2)settimeofday 系统调用

2.time 系统调用

3.times 系统调用

times 系统调用

clock_t times(struct tms *bus);

可用于获取某进程的具体执行时间。它将进程时间存储在 struct tms buf 中,即:

struct tms{
    clock_t tms_utime;    // user mode time
    clock_t tms_stime;    // system mode time
    clock_t tms_cutime;   // user time of children
    clock_t tms_cstime;   // system time of children
}

以时钟计时单元报告所有时间。这可以为分析某个正在执行的进程提供信息,包括其子进程的时间(如有)。

4.time 和 date 命令

  • date:打印或设置系统日期和时间。
  • time:报告进程在用户模式和系统模式下的执行时间和总时间。
  • hwclock:查询并设置硬件时钟(RTC),也可以通过 BIOS 来完成。

(六)间隔定时器

  • setitimeer 程序代码

(七)REAL 模式间隔定时器

(八)编程项目

1.系统基本代码

2.定时器中断

3.定时器队列

4.临界区

5.高级主题


二、问题与解决思路
三、实践内容与截图

1.gettimeofday 系统调用

#include<stdio.h>
#include<stdlib.h>
#include<sys/time.h>
#include<time.h>

struct timeval t;

int main()
{
 gettimeofday(&t,NULL);
 printf("sec=%ld usec=%ld\n",t.tv_sec,t.tv_usec);
 printf((char *)ctime(&t.tv_sec));
}

2.settimeofday 系统调用

#include<stdio.h>
#include<stdlib.h>
#include<sys/time.h>
#include<time.h>

struct timeval t;

int main()
{
 int r;
 t.tv_sec=123456789;
 t.tv_usec=0;
 r=settimeofday(&t,NULL);
 if(!r){
  printf("settimeofday() failed\n");
  exit(1);
 }
 gettimeofday(&t,NULL);
 printf("sec=%ld usec=%ld\n",t.tv_sec,t.tv_usec);
 printf("%s",ctime(&t.tv_sec));
}

3.time 系统调用

#include<stdio.h>
#include<time.h>

time_t start,end;

int main()
{
 int i;
 start=time(NULL);
 printf("start=%ld\n",start);
 for(i=0;i<123456789;i++);
 end=time(NULL);
 printf("end =%ld time=%ld\n",end,end-start);
}

4.setitimeer 程序代码

#include<signal.h>
#include<stdio.h>
#include<sys/time.h>
#include<time.h>
int count=0;
struct itimerval t;

void timer_handler(int sig)
{
 printf("timer_hander: signal=%d count=%d\n",sig,++count);
 if(count>=8){
  printf("cancel timer\n");
  t.it_value.tv_sec=0;
  t.it_value.tv_usec=0;
  setitimer(ITIMER_VIRTUAL,&t,NULL);
 }
}

int main()
{
 struct itimerval timer;
 signal(SIGVTALRM,timer_handler);
 timer.it_value.tv_sec=0;
 timer.it_value.tv_usec=100000;
 timer.it_interval.tv_sec=1;
 timer.it_interval.tv_usec=0;
 setitimer(ITIMER_VIRTUAL,&timer,NULL);
 printf("looping: enter Control-C to terminate\n");
 while(1);
}

posted @ 2022-10-22 16:37  油菜园12号  阅读(44)  评论(0编辑  收藏  举报