1.preface:
协议中的定时器功能比较重要也比较常见如RIP中的update timer, timeout timer, garbage timer, holddown timer.先仅仅对定时器做出提供参考实现:
compiler: TC 2.0:
2.service:code
/**
compile: TC 2.0
**/
#include <stdio.h>
#include <conio.h>
#include <dos.h>
#include <bios.h>
#include <stdlib.h>
#define VK_ESC 0x11b
#define TIMER 0x1c
int TimerCounter=0;
/* void interrupt(* oldhandler() ); */ /*invalid express*/
void interrupt (*oldhandler) ();
/*
this function as produre to IntProc
note!!! this function after function SetTimer
*/
void interrupt newhandler()
{
TimerCounter++;
oldhandler(); /* called the old routine and original */
}
void SetTimer(void interrupt(*IntProc)() )
{
oldhandler=getvect(TIMER);
disable(); /*set new clock process and forbit other interrupt */
setvect(TIMER, IntProc ) ; /*set current clock interrupt vector and pass interrupt procedure */
enable(); /* enable this interrupt */
}
void KillTimer()
{
disable (); /*disable other interrupt*/
/*note that oldhander is gloabal variable*/
/*SetVect(TIMER,oldhandler); */
setvect(TIMER,oldhandler);
enable();
}
main()
{
int key,time=0;
SetTimer(newhandler); /*update clock interrupt,pass a interrupt instance to IntoProc */
for(;;)
{
if (bioskey(1))
{
key=bioskey(0);
if (key==VK_ESC)
{
printf("User cancel!\n");
break;
}
}
if(TimerCounter>18)
{
TimerCounter=0;
time++;
printf("%d\n",time);
if(time==10) /*10 s ends program */
{
printf("Program terminal normally!\n");
break;
}
}
} /*end loop*/
KillTimer(); /*backup clock interrupt */
/*
getch();
*/
}
结果如下:
3.reference:
[1]卢军.Linux_0_01内核分析与操作系统设计.清华大学出版社.2004