中断处理——程序描述
模拟时钟中断的产生及设计一个对时钟中断事件进行处理的模拟程序。
/*******************************************************/
/* 程序题目: 中断处理
/* 程序员: Cassie
/* 编程时间: 2017-2-12
/* 程序功能: 模拟时钟中断的产生
/*******************************************************/
#include<iostream>
#include<ctime>
using namespace std;
int main()
{
int cnum=0;//计数器
int ctime=0;//计时器
int clock=0;//定时闹钟
int input;//输入的命令(0,1)
int year,month,day,hour,minute,second;//时钟日历的定义
int hour1,minute1,second1;//计算时间差值所需要的变量
time_t now;
struct tm *tme;
time(&now);
tme = localtime(&now);//根据电脑系统时间初始化时钟日历
year=tme->tm_year+1900;
month=tme->tm_mon+1;
day=tme->tm_mday;
hour=tme->tm_hour;
minute=tme->tm_min;
second=tme->tm_sec; //初始化
cout<<"系统时间为:"<<year<<"-"<<month<<"-"<<day<<" "<<hour<<":"<<minute<<":"<<second<<endl;
for(int i=1;i<=3;++i)
{
cout<<"请输入第"<<i<<"个时钟闹钟的初值:"<<endl;
cin>>clock;
cnum++;
while(clock!=0)
{
cout<<"请输入信号1或0(1表示中断发生,0则表示没有中断产生)"<<endl;
cin>>input;
if(input!=0)
{
cout<<"正在处理中断!"<<endl;
ctime++;
clock--;
cout<<"处理完成!"<<endl;
}
else
cnum++;
}
//根据计时器计算时间(在一天中完成)
hour1=ctime/3600;
minute1=(ctime-hour1*3600)/60;
second1=ctime-hour1*3600-minute1*60;
hour=hour1+hour;
minute=minute+minute1;
second=second+second1;
cout<<"当前时间为:"<<year<<"-"<<month<<"-"<<day<<" "<<hour<<":"<<minute<<":"<<second;
cout<<endl;
cnum=0;
ctime=0;//每一次完成后清零
}
return 0;
}
|
开机时间:2017-2-12 12:53:34 |
|
|
定时闹钟(单位/s) |
闹钟结束时间 |
|
3 |
2017-2-12 12:53:37 |
|
0 |
2017-2-12 12:53:37 |
|
4 |
2017-2-12 12:53:41 |
/***********************************
/* 程序题目: 用时间片轮转的调度策略进行实验
/* 程序员: Cassie
/* 时间: 2017-2-12
/* 程序功能: 用时间片轮转的调度策略进行实验
/*******************************************************/
#include <iostream>
#include<stdlib.h>
#include<conio.h>
#include <ctime>
using namespace std;
typedef struct PCB
{
char name[10];//进程的名字
int timep;//时间片
int time;//运行所需时间
struct PCB *next;//指向的指针
}PCB,*PCBnode;//定义结构体
void creat(PCBnode L)//创建进程
{
PCBnode p,q;
cout<<"请输入进程名及运行时间(默认时间片为1):"<<endl;
p=(PCBnode)malloc(sizeof(PCB));
cin>>p->name>>p->time;
p->next=NULL;
p->timep=1;//将时间片定位1
q=L;
while(q->next!=NULL)
{
q=q->next;
}
q->next=p;
}
void out(PCBnode L)//输出信息
{
PCBnode p;
p=L->next;
if(p!=NULL)
{
cout<<"经运行后的结果:"<<endl;
cout<<"进程名 运行时间 时间片"<<endl;
while(p!=NULL)
{
cout<<" "<<p->name<<" "<<p->time<<" "<<p->timep<<endl;
p=p->next;
}
}
}
void resout(PCBnode L,int Time,int clock[])
{
PCBnode p,q;
int clock1[6]={0};//初始化 ,由0到5分别是年,月,日,时,分,秒
if(L->next==NULL)
{
cout<<"程序运行完成"<<endl;
}
else
{
q=L;
p=L->next;
p->time=p->time-1;
p->timep=p->timep-1;
if(p->time==0)
{
cout<<p->name<<"程序已运行结束"<<endl;
L->next=p->next;//移除
//定义每个时间单位为1s来计算时间(假定在一天内完成)
clock1[3]=Time/3600;
clock1[4]=(Time-clock1[3]*3600)/60;
clock1[5]=Time-clock1[3]*3600-clock1[4]*60;
clock1[0]=clock[0];
clock1[1]=clock[1];
clock1[2]=clock1[2];
clock1[3]=clock1[3]+clock[3];
clock1[4]=clock1[4]+clock[4];
clock1[5]=clock1[5]+clock[5];
cout<<"结束时间为:"<<clock[0]<<"-"<<clock[1]<<"-"<<clock[2]
<<" "<<clock[3]<<":"<<clock[4]<<":"<<clock[5]<<endl<<endl;
}
if(p->timep==0&&p->time!=0)
{
L->next=p->next;
p->next=NULL;
while(q->next!=NULL)
{
q=q->next;
}
q->next=p;
p->timep=1;
}
out(L);
cout<<endl;
}
}
int main()
{
time_t now;
struct tm *fmt;
int clock[6]={0};//分别代表年,月,日,小时,分钟,秒
int count=0;//计数
int a,i=0;
PCBnode L;//头结点
L=(PCBnode)malloc(sizeof(PCB));
L->next=NULL;
time(&now);
fmt = localtime(&now);//根据电脑的时间,自动算出现在的时间
clock[0]=fmt->tm_year+1900;
clock[1]=fmt->tm_mon+1;
clock[2]=fmt->tm_mday;
clock[3]=fmt->tm_hour;
clock[4]=fmt->tm_min;
clock[5]=fmt->tm_sec;
cout<<"电脑的系统时间为:"<<clock[0]<<"-"<<clock[1]<<"-"<<clock[2]
<<" "<<clock[3]<<":"<<clock[4]<<":"<<clock[5]<<endl;
cout<<endl;
do//创建进程
{
creat(L);
cout<<"输入完毕请输0,否则输1"<<endl;
cin>>a;
}while(a!=0);
out(L);//调用输出信息函数
while(i==0)
{
cout<<"请输入0或1(1为中断,0为无中断)"<<endl;
cin>>a;
if(a==1)
{
cout<<"产生中断"<<endl;
count++;
resout(L,count,clock);
cout<<endl;
if(L->next==NULL)
{
cout<<"程序已运行完!"<<endl;
i=1;
}
}
else if(a==0)
{
cout<<"未产生中断!"<<endl;
cout<<endl;
}
}
}
浙公网安备 33010602011771号