c語言中時間陷阱的完成
發表時間:2024-01-02 來源:明輝站整理相關軟件相關文章人氣:
[摘要]時間陷阱.它能是主程序的運行和對事件的處理異步進行.通過設置時間陷阱.可以在規定的時間間隔到達后自動執行某個子程序.子程序運行結束后又回到主程序的斷點處繼續執行.basic語言提供了有關時間陷阱的語句.可以直接調用.但C語言沒有這樣的功能,為了能實現上述功能,這里給出了幾個子函數以幫助實現. #i...
時間陷阱.它能是主程序的運行和對事件的處理異步進行.通過設置時間陷阱.可以在規定的時間間隔到達后自動執行某個子程序.子程序運行結束后又回到主程序的斷點處繼續執行.basic語言提供了有關時間陷阱的語句.可以直接調用.但C語言沒有這樣的功能,為了能實現上述功能,這里給出了幾個子函數以幫助實現.
#include<dos.h>
#define Sizeprogram 375
void interrupt (*oldtimer)();
void interrupt newtimer();
static struct SREGS seg;
unsigned intsp,intss;
unsigned myss,stack;
unsigned vseg;
int running=0;
int m1,m2,m3=0;
int (*sub_ptr)();
void on_timer();
void timer_on();
void timer_off();
void on_timer(t,pp);
int t;
int (*pp)();
{
sub_ptr=pp;
m1=t;
segread(&seg);
stack=(Sizeprogram-(seg.ds-seg.cs))*16-300;
myss=_SS;
oldtimer=getvect(0x1c);
}
void timer_on()
{
m2=0;
m3=1;
setvect(0x1c,newtimer);
}
void timer_off()
{
m3=0;
setvect(0x1c,oldtimer);
}
void interrupt newtimer()
{
(*oldtimer)();
if(running==0)
running=1;
disable();
intsp=_SP;
intss=_SS;
_SP=stack;
_SS=myss;
enable();
if(m3==1){
if(m2==m1*18){
m2=0;
(*sb_ptr)();
}
m2+=1;
}
disable();
_SP=intsp;
_SS=intss;
enable();
running=0;
}
}
#include<dos.h>
#define TRUE 1
void ptr();
main()
{
char ch;
char *p:
p=(char *)ptr;
on_timer(t,p);
while(TRUE){
ch=getch();
switch(ch){
case ''0'': timer_off();break;
case ''1'': timer_on();break;
case ''2'': timer_off();brea;
default: printf("%c",ch);
}
}
}
void ptr()
{
union REGS in,out;
in.h.al=''A'';
in.h.ah=14;
int86(0x10,&in,&out);
}