20211325 2023-2024-1 《信息安全系统设计与实现(上)》第八周学习笔记

20211325 2023-2024-1 《信息安全系统设计与实现(上)》第八周学习笔记

一、任务要求

自学教材第5章,提交学习笔记(10分),评分标准如下:

1. 知识点归纳以及自己最有收获的内容,选择至少2个知识点利用chatgpt等工具进行苏格拉底挑战,并提交过程截图,提示过程参考下面内容 (4分) “我在学***X知识点,请你以苏格拉底的方式对我进行提问,一次一个问题” 核心是要求GPT:“请你以苏格拉底的方式对我进行提问” 然后GPT就会给你提问,如果不知道问题的答案,可以反问AI:“你的理解(回答)是什么?” 如果你觉得差不多了,可以先问问GPT:“针对我XXX知识点,我理解了吗?” GPT会给出它的判断,如果你也觉得自己想清楚了,可以最后问GPT:“我的回答结束了,请对我的回答进行评价总结”,让它帮你总结一下。

2. 问题与解决思路,遇到问题最先使用chatgpt等AI工具解决,并提供过程截图(3分)

3. 实践过程截图,代码链接(2分)

4. 其他(知识的结构化,知识的完整性等,提交markdown文档,使用openeuler系统等)(1分)

二、教材内容总结

2.1硬件定时器

定时器是由时钟源和可编程计数器组成的硬件设备。时钟源通常是一个晶体振荡器,会产生周期性电信号,以精确的频率驱动计数器。

硬件定时器能够按照一定的频率周期性的有规律的给CPU发送中断信号,发送中断的频率(周期)可以通过软件编程来设置,硬件定时器产生的中断信号可以称之为时钟中断。

2.2个人计算机定时器

  • 实时时钟(RTC):RTC由一个小型备用电池供电。在所有类Unix系统中,时间变量是一个长整数,包含从1970年1月1日起经过的秒数。

time() returns the time as the number of seconds since the Epoch,1970-01-01 00:00:00 +0000 (UTC).

 

  • 可编程间隔定时器(PIT)(Wang2015):PIT是与CPU分离的一个硬件定时器。可对它进行编程,以提供以毫秒为单位的定时器刻度。

  • 多核CPU中的本地定时器(Intel1997;Wang2015):在多核CPU中,每个核都是一个独立的处理器,它有自已的本地定时器,由CPU时钟驱动。

  • 高分辨率定时器:大多数电脑都有一个时间戳定时器(TSC),由系统时钟驱动。它的内容可通过64位TSC寄存器读取。

2.3CPU操作

每个CPU都有一个程序计数器(PC),也称为指令指针(IP),以及一个标志或状态寄存器(SR)、一个堆栈指针(SP)和几个通用寄存器,当 PC指向内存中要执行的下一条指令时,SR包含 CPU 的当前状态,如操作模式、中断掩码和条件码,SP指向当前堆栈栈顶。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;
}

中断和异常处理都在操作系统内核中进行。大多数情况下,用户级程序无法访问他们。

2.4中断操作

书上讲的比较晦涩

中断是指在CPU正常运行期间,由于内外部事件或由程序预先安排的事件引起的CPU暂时停止正在运行的程序,转而为该内部或外部事件或预先安排的事件服务的程序中去,服务完毕后再返回去继续运行被暂时中断的程序。

从广义上讲,中断又可分为四类:中断、故障、陷阱、终止。

2.5时钟服务函数

  • 在几乎所有的操作系统中,操作系统内核都会提供与时钟相关的各种服务。

linux系统中与时钟有关的系统调用:

2.6gettimeofday&settimeofday

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=%d\n", t.tv_sec, t.tv_usec);
  printf("%s\n",ctime(&t.tv_sec));
}

编译运行:

 (其中代码中对于ctime函数调用缺少必要的头文件time.h

并且不需要强制类型转换为char* 。)

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() faild\n");
                exit(1);
        }
        gettimeofday(&t,NULL);
        printf("sec=%ld usec=%ld\n",t.tv_sec,t.tv_usec);
        printf("%s",ctime(&t.tv_sec));
}

编译运行:

2.7time系统调用

#include<stdio.h>
#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);
}

编译运行:

间隔定时器

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

int count = 0;
struct itimerval t;
time_t start,end ;

void timer_handler(int sig){
      end =time(NULL);
      printf("timer_handler :  signal %d   count=%d  , diff: %ld \n",sig, ++count,end -start);
      start =  end;
      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;
      //every 1s afterward
      timer.it_interval.tv_sec = 1;
      timer.it_interval.tv_usec = 0;
      // start a virtual itimer
      start = time(NULL);
      setitimer( ITIMER_VIRTUAL , &timer ,NULL );
      printf("press Ctrl + C  to terminate \n");
      while(1);
}

编译运行:

三、课堂代码实现

 四、苏格拉底挑战

 (1)时钟服务函数

 (2)间隔定时器

 

posted on 2023-11-04 19:22  STERNSTUN  阅读(8)  评论(0编辑  收藏  举报

导航