Design and implementation of information security system: Chapter 5 learning notes

Design and implementation of information security system: Chapter 5 learning notes

20191331 lyx

Summary of learning contents of teaching materials

Chapter V timer and clock service

Learning objectives

Learn this chapter to understand timer and timing service. Understand the principle of hardware timer and the hardware timer in PC based on Intel x86; Understand CPU operation and interrupt handling. Learn system calls, library functions and timer service commands related to timers in Linux. Understand the process interval timer through examples. The real-time mode interval timer of Linux process is designed to generate SIGALRM signal regularly as the timer interrupt of virtual CPU. Virtual CPU uses SIGALRM signal catcher as the interrupt handler of timer. Timers also prevent race conditions between tasks and interrupt handlers.

hardware timer

Timer is a hardware device composed of clock source and programmable counter. The clock source is usually a crystal oscillator, which will generate periodic electrical signals to drive the counter at a precise frequency.

The hardware timer can periodically and regularly send interrupt signals to the CPU according to a certain frequency. The frequency (cycle) of sending interrupt can be set by software programming. The interrupt signal generated by the hardware timer can be called clock interrupt.

Personal computer timer

  • Real time clock (RTC): the RTC is powered by a small backup battery. In all Unix like systems, the time variable is a long integer containing the number of seconds since January 1, 1970.

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

  • Programmable interval timer (PIT)(Wang2015):PIT is a hardware timer separated from CPU. It can be programmed to provide a timer scale in milliseconds.

  • Local timer in multi-core CPU (Intel 1997; Wang 2015): in multi-core CPU, each core is an independent processor, which has its own local timer driven by CPU clock.

  • High resolution timer: most computers have a timestamp timer (TSC) driven by the system clock. Its contents can be read through the 64 bit TSC register.

CPU operation

Each CPU has a program counter (PC), also known as an instruction pointer (IP), a flag or status register (SR), and a stack pointer (SP) And several general registers. When PC points to the next instruction to be executed in memory, Sr contains the current state of CPU, such as operation mode, interrupt mask and condition code, and SP points to the top of the current stack. CPU operation can be modeled by infinite loop.

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;

Interrupt and exception handling are carried out in the operating system kernel. In most cases, user level programs cannot access them.

Interrupt operation

The book is rather obscure

Interrupt means that during the normal operation of the CPU, the CPU temporarily stops the running program due to internal and external events or events pre arranged by the program, turns to the program serving the internal or external events or pre arranged events, and returns to continue running the temporarily interrupted program after the service is completed.

Broadly speaking, interrupts can be divided into four categories: interrupt, fault, trap and termination.

Clock service function

  • In almost all operating systems, the operating system kernel provides various clock related services.

Clock related system calls in linux system:



#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
struct timeval t;

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

Compile run:

An error occurred

The code is missing the necessary header file time.h for ctime function calls

And you don't need to cast the type to char *, just output it as a string.


#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);
                printf("settimeofday() faild\n");
        printf("sec=%ld usec=%ld\n",t.tv_sec,t.tv_usec);

Compile run:

Guess failure reason: the system protects the current time from being tampered with

time system call

time_t start,end;

int main(){
int i;
printf("end =%ld time=%ld\n",end,end-start);

Compile run:

Interval timer

#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");

Compile run:

reference material

Linux driver development - timer

Linux kernel interrupt system processing mechanism - detailed analysis

Posted on Sun, 07 Nov 2021 18:14:16 -0500 by tinyashcities