实验报告一——中断处理姓名:XXXX 学号:XXXX 班级:XXXXX一、 实习内容-运用某种高级语言(如C或C++)模拟时钟中断的产生及设计一个对时钟中断事件 进行处理的模拟程序二、 实习目的-现代计算机系统的硬件部分都设有中断机构,中断机构能发现中断事件,且当发现 中断事件后迫使正在处理器上执行的进程暂时停止执行,而让操作系统的中断处理 程序占有处理器去处理出现的中断事件本实习模拟“时钟中断事件”的处理,对其它中断事件的模拟处理,可根据各中断 事件的性质确定处理原则,制定算法,自行设计希望学生通过本实习了解中断及中断处理程序的作用1. 计算机系统工作过程中,若出现中断事件,硬件就把它记录在中断寄存器中中断寄存 器的每一位可与一个中断事件对应,当出现某中断事件后,对应的中断寄存器的某一位就被 置成“1-处理器每执行一条指令后,必须查中断寄存器,当中断寄存器内容不为“0”时,说 明有中断事件发生硬件把中断寄存器内容以及现行程序的断点存在主存的固定单 元操作系统分析保存在主存固定单元中的中断寄存器内容就可知道出现的中断事 件的性质,从而作出相应的处理本实习中,用从键盘读入信息来模拟中断寄存器的作用,用计数器加1来模拟处理 器执行了一条指令。
每模拟一条指令执行后,从键盘读入信息且分析,当读入信息 =0时,表示无中断事件发生,继续执行指令;当读入信息=1时,表示发生了时钟 中断事件,转时钟中断处理程序2. 假定计算机系统有一时钟,它按电源频率(50Hz)产生中断请求信号,即每隔20毫秒产 生一次中断请求信号,称时钟中断信号,时钟中断的间隔时间(20毫秒)称时钟单位学生W按自己确定的频率在键盘上键入“0”或“1”来模拟按电源频率产生的时钟 中断信号3. 中断处理程序应首先保护被中断的现行进程的现场(通用寄存器内容、断点等),现场信 息可保存在进程控制块中;然后处理出现的中断事件,根据处理结果修改被中断进程的状态; 最后转向处理器调度,由处理器调度选择可运行的进程,恢复现场使其运行本实习主要模拟中断事件的处理,为简单起见可省去保护现场和处理器调度的工作4. 为模拟时钟中断的处理,先分析一下时钟中断的作用利用时钟中断可计算日历时钟,也可作定时闹钟等计算日历时钟一一把开机时的时间存放在指定的称为“日历时钟”的工作单元中, 用一计时器累计时钟中断次数C根据时钟中断的次数和时钟单位(20毫秒)以及开 机时的日历时钟可计算出当前的精确的日历时钟。
定时闹钟一一对需要定时的场合,可把轮到运行的进程的时间片值送到称为“定时 闹钟”的工作单元中,每产生一次时钟中断就把定时闹钟值减1,当该值为“0”时, 表示确定的时间己到,起到定时的作用5. 本实习的模拟程序可由两部分组成,一部分是模拟硬件产生时钟中断,另一部分模拟操 作系统的时钟中断处理程序模拟程序的算法如图1-1其中,保护现场和处理器调度的工 作在编程序时可省去约定处理器调度总是选择被中断进程继续执行6. 按模拟算法设计程序,要求显示或打印开机时间、定时闹钟初值、定时闹钟为“0”时的 日历时钟确定三个不同的定时闹钟初值,运行设计的程序,观察得到的结果四、主要数据结构及符号说明star = localtime(& t);st=asctime(star);//获取开机时间作为起始时间定义每个时间单元为2说明:因为要输入数字模拟有无中断发生,输入频率无法保证和电脑同步,所以用一个数字 来记录时间,输出的结束时间不是结束时电脑的时间,而是设每个时间但愿为2s计算的时 间时钟中断处理模拟算法五、实现代码为:#include#include#includeint main()(〃初始化,显示开机时间timc.t t;double p;int a,m,r,n,e,o,u;int count=0,clock_alann,w,Timer=0;ini i;char *st,s[27];struct tm * star;t = tirne(NULL);star = localtime(& t);st=asctime(star);for(i=0;iv27;i++)s[i]=*st;st++;)printf(n*****a simulation program ofclock to deal with disruptions*****\n");printf(Hstar time:");for( i=4;i<25;i++){prinir(”%c”,sE);}//时间存储在数组中,从第5位开始为月份、 时间、年份printf(Hset clock alarm ;scanf("%d”,&clock-alarm);〃 设置定时闹钟))else(s[14]=s[l4]-48;sll5]=sll5J-48;o=s[l4J*IO+sll5];o=o+(int)p/60;if(o<60){s[14]=(o/10)+48;s[ 15]=(o% 10)+48;s[!7]=((int)p%60)/I0+48;s[ 18]=((inl)p%60)% 10+48;for( i=4;iv27;i++)(printf(”%c“,s[i]);}}else(slll]=s[ll]-48;sfl21=s[12]-48;u=s[U]*10+s[12];u=u+o/60;if(u<24)(s[ I l]=u/IO+48;sll2]=u%IO+48;s[14]=(o%60)/10+48;s[15]=(o%60)% 10+48;s[l7]=((int)p%6O)/IO+48;s[l8]=((int)p%60)%10+48;for( i=4;i<25;i++)(prinlf(”%c”,s[i]);))})printf(Hthe clock alarm is %d,timer is O\n",clock_alarm);while(clock_alarm !=0)( 〃模拟时钟中断do{prinlf(A piece of order have been finishedn");count++7/a拟一条指令被执行,计数器 加1printf(Hinput a numbcr(1 means an interrupt,while 0 means there is no interrupt!\nH);scanf("%d”,&w);〃 读键盘信息)while(w==0);〃模拟时钟中断处理if(w==l)printf(Hthe spot have been saved !\n,r)y/tt 护现场,计时器加I,定时闹钟减I,处理器调度Timer++;clock顼larni-・;printf(nprocessor finished assignmentll");}〃计算当前日历时间p=Timcr*2;//dcflnc the time unit is 2 a=(int)Timcr*2;if((p-a)>=0.5)p=a+1;elsep=a;m=(int)p;printf(Htimc have passed :%d\n”,m);r=s[17]-48;//计算运行时间n=s[!8]-48;e=r* 10+n;p=p+e;if(p<60)(s[17]=((int)p/10)+48;s[18]=((int)p% 10)+48;for( i=4;iv27;i++)(printfC%c",s[i]);六、在虚拟机上的具体操作及结果feifei@ubuntu:*$ cd /home/feifei/oslfeifei@ubuntu:*/os1$ gcc -o osl osl.cfeifei@ubuntu:*/osl$ ./osl♦♦♦♦♦a simulation program of clock to deal with disruptions*****star time:Nov 30 05:54:61 2012set clock alarm :8the clock alarm is 8,timer is 0A piece of order have been finishedinput a numberd means an interruptrwhile 01the spot have been saved!processor finished assignmentA piece of order have been finishedinput a numberd means an interrupt,while O1the spot have been saved!processor finished assignmentA piece of order have been finishedinput a numberd means an interrupt,while Q1the spot have been saved!processor finished assignmentA piece of order have been finishedinput a numberd means an interruptrwhile 06A piece of order have been finishedinput a numberd means an interruptrwhile 0 0meansmeansmeansmeansmeansthere is no interrupt!there is no interrupt!there is no interrupt!there is no interrupt!there is no interrupt!A piece of order have been finishedeA piece of order have been finished input a number(l means an interruptrwhile 1 the spot have been saved! processor finished assignmentA piece of order have been finished input a number(l means an interruptrwhile 1 the spot have been saved! processor finished assignmentA piece of order have been finished input a number(1 means an interrupt,while 1 the s。