本文主要是介绍操作系统原理与系统——实验十三多道批处理作业调度(作业可移动),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
关键代码
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct data{int hour;//当前小时int min;//当前分钟
}time;
struct node{char name[20];//进程名time arrive;//到达就绪队列时间int zx;//执行时间(预期时间)int size;int tai;int flag;time zhuangru; time start;//开始执行时间(进程调度)time finish;//执行完成时间int zz;//周转时间=完成时间-到达就绪队列时间float zzxs;//带权周转时间系数=周转时间/执行时间struct node* next;
};
//内存结构体typedef struct memory_node{int size; //内存大小int address; //内存始址
} memoryNode;
memoryNode memory;int resource;
//函数名:in参数:无
node* in(){
//函数功能:输入访问序列信息int n;node *q,*head;head = NULL; printf("请输入内存大小:");scanf("%d",&memory.size);printf("请输入起始地址大小为:");scanf("%d",&memory.address);printf("请输入磁带机资源:");scanf("%d",&resource); printf("请输入进程数量:");scanf("%d",&n);printf("请输入进程的参数:\n"); for(int i=0;i<n;i++){node *p = (node *)malloc(sizeof(node));p->flag = 0;p->next = NULL;scanf("%s %d:%d %d分钟 %dK %d台",&p->name,&p->arrive.hour,&p->arrive.min,&p->zx,&p->size,&p->tai);if(head == NULL){head = p;q = p;} else{q->next = p;q = p;}}return head;
}void output(node *p) {//函数功能:按进程执行顺序分别输出每个进程全部结果信息和系统平均时间int sum=0,count=0; float sum1=0; printf("\n模拟进程FCFS调度过程输出结果:\n作业名 到达时间 执行时间(分钟) 装入时间 开始时间 完成时间 周转时间(分钟) 带权周转系数\n");while(p!=NULL){printf("%5s ",p->name);printf("%5d:%02d ",p->arrive.hour,p->arrive.min);printf("%8d(分钟) ",p->zx);printf("%14d:%02d ",p->zhuangru.hour,p->zhuangru.min);printf("%6d:%02d ",p->start.hour,p->start.min);printf("%7d:%02d ",p->finish.hour,p->finish.min);printf("%7d ",p->zz);printf("%20.2f\n",p->zzxs);sum=sum+p->zz;sum1=sum1+p->zzxs;count++;p=p->next;}printf("系统平均周转时间为:%.2f\n",sum*1.0/count);printf("系统平均带权周转系数为:%.2f\n\n",sum1/count);
}
//函数名:FCFS参数:无
void FCFS(){
//函数功能:调用先来先服务算法printf("\n***********FCFS多道批处理作业调度***********\n");node *head = in();node *h,*q;h = NULL;q = head;while(q!=NULL){node *p = (node *)malloc(sizeof(node));*p = *q;p->next = NULL;if(h == NULL){h = p;}else{node *a = h,*b;while(a!=NULL){if(p->arrive.hour*60+p->arrive.min<a->arrive.hour*60+a->arrive.min)break;b = a;a = a->next;}if(a == h){p->next = h;h = p;}else{p->next = a;b->next = p;}}q = q->next; }time now;now.hour = 0;now.min = 0;int qaq = 0,available = resource,space = memory.size;q = h;node *duilie = NULL,*flow,*flagh;node *k,*flowk,*KK=NULL; while(h!=NULL){flagh = h;//每一次大循环都找在自己执行完的时间内并且符合条件的 摘下来 //大循环每次末尾都回收第一个 q = h;if(duilie==NULL){h = q->next;q->next = NULL;duilie = q;flow = duilie;if(duilie->arrive.hour*60+duilie->arrive.min>now.hour*60+now.min){now.hour = duilie->arrive.hour;now.min = duilie->arrive.min;}duilie->start.hour = now.hour;duilie->start.min = now.min;duilie->zhuangru.hour = now.hour;duilie->zhuangru.min = now.min;available -=q->tai;space = memory.size - duilie->size;}//如果不是第一个,则遍历去找符合条件的 node *hh = h,*hhpre;while(hh!=NULL){if(hh->arrive.hour*60+hh->arrive.min<=duilie->start.hour*60+duilie->start.min+duilie->zx&&space - hh->size>=0&&available-hh->tai>=0){//满足条件hh->start.hour = duilie->start.hour + (duilie->start.min + duilie->zx )/60;hh->start.min = (duilie->start.min + duilie->zx )%60;hh->zhuangru.hour = now.hour;hh->zhuangru.min = now.min;available -=hh->tai;space -=hh->size;if(hh==h){h=hh->next;hh->next = NULL;if(duilie == NULL){duilie = hh;flow = hh;}else{flow->next = hh;flow = hh;}hh = h;}else{hhpre->next = hh->next;hh->next = NULL;if(duilie == NULL){duilie = hh;flow = hh;}else{flow->next = hh;flow = hh;}hh = hhpre->next;}}else{//不满足条件——————>空间不满足 or 磁带机不满足hhpre = hh;hh=hh->next;}}//回收资源k=duilie;k->finish.hour = k->start.hour+(k->start.min+k->zx)/60;k->finish.min = (k->start.min + k->zx)%60;k->zz = k->finish.hour*60 + k->finish.min - (k->arrive.hour*60 + k->arrive.min);k->zzxs = k->zz*1.0/k->zx; now.hour = k->finish.hour;now.min = k->finish.min;available +=k->tai;space +=k->size;duilie = duilie->next;if(KK==NULL){KK=k;flowk = k;k->next = NULL;}else{flowk->next = k;flowk = k;k->next = NULL;} }while(duilie!=NULL)//h==NULL但是duilie不等于NULL {k=duilie;k->start.hour = now.hour;k->start.min = now.min;k->finish.hour = k->start.hour+(k->start.min+k->zx)/60;k->finish.min = (k->start.min + k->zx)%60;k->zz = k->finish.hour*60 + k->finish.min - (k->arrive.hour*60 + k->arrive.min);k->zzxs = k->zz*1.0/k->zx; now.hour = k->finish.hour;now.min = k->finish.min;available +=k->tai;memory.size +=k->size;duilie = duilie->next;if(KK==NULL){KK=k;flowk = k;k->next = NULL;}else{flowk->next = k;flowk = k;k->next = NULL;}}output(KK);
}
//函数名:SSTF参数:无
void SPF(){
//函数功能:调用短进程优先调度算法printf("\n***********SPF多道批处理作业调度***********\n");node *head = in();node *h,*q,*H;h = NULL;q = head;H = q;q = q->next;while(q!=NULL){node *p = (node *)malloc(sizeof(node));*p = *q;p->next = NULL;if(h == NULL){h = p;}else{node *a = h,*b;while(a!=NULL){if(p->zx<a->zx)break;b = a;a = a->next;}if(a == h){p->next = h;h = p;}else{p->next = a;b->next = p;}}q = q->next; }H->next = h;h = H;time now;now.hour = 0;now.min = 0;int qaq = 0,available = resource,space = memory.size;q = h;node *duilie = NULL,*flow,*flagh;node *k,*flowk,*KK=NULL; while(h!=NULL){flagh = h;//每一次大循环都找在自己执行完的时间内并且符合条件的 摘下来 //大循环每次末尾都回收第一个q = h;if(duilie==NULL){h = q->next;q->next = NULL;duilie = q;flow = duilie;if(duilie->arrive.hour*60+duilie->arrive.min>now.hour*60+now.min){now.hour = duilie->arrive.hour;now.min = duilie->arrive.min;}duilie->start.hour = now.hour;duilie->start.min = now.min;duilie->zhuangru.hour = now.hour;duilie->zhuangru.min = now.min;space -= q->size;available -=q->tai;}//如果不是第一个,则遍历去找符合条件的 node *hh = h,*hhpre;while(hh!=NULL){if(hh->arrive.hour*60+hh->arrive.min<=duilie->start.hour*60+duilie->start.min+duilie->zx&&space-hh->size>=0&&available-hh->tai>=0){//满足条件hh->start.hour = duilie->start.hour + (duilie->start.min + duilie->zx )/60;hh->start.min = (duilie->start.min + duilie->zx )%60;hh->zhuangru.hour = now.hour;hh->zhuangru.min = now.min;available -=hh->tai;space -= hh->size;if(hh==h){h=hh->next;hh->next = NULL;if(duilie == NULL){duilie = hh;flow = hh;}else{flow->next = hh;flow = hh;}hh = h;}else{hhpre->next = hh->next;hh->next = NULL;if(duilie == NULL){duilie = hh;flow = hh;}else{flow->next = hh;flow = hh;}hh = hhpre->next;}}else{//不满足条件——————> 空间不满足 or 磁带机不满足hhpre = hh;hh=hh->next;} }//回收资源k=duilie;space += k->size; k->finish.hour = k->start.hour+(k->start.min+k->zx)/60;k->finish.min = (k->start.min + k->zx)%60;k->zz = k->finish.hour*60 + k->finish.min - (k->arrive.hour*60 + k->arrive.min);k->zzxs = k->zz*1.0/k->zx; now.hour = k->finish.hour;now.min = k->finish.min;available +=k->tai;duilie = duilie->next;if(KK==NULL){KK=k;flowk = k;k->next = NULL;}else{flowk->next = k;flowk = k;k->next = NULL;} }while(duilie!=NULL)//h==NULL但是duilie不等于NULL {k=duilie;k->start.hour = now.hour;k->start.min = now.min;k->finish.hour = k->start.hour+(k->start.min+k->zx)/60;k->finish.min = (k->start.min + k->zx)%60;k->zz = k->finish.hour*60 + k->finish.min - (k->arrive.hour*60 + k->arrive.min);k->zzxs = k->zz*1.0/k->zx; now.hour = k->finish.hour;now.min = k->finish.min;available +=k->tai;memory.size +=k->size;duilie = duilie->next;if(KK==NULL){KK=k;flowk = k;k->next = NULL;}else{flowk->next = k;flowk = k;k->next = NULL;}}output(KK);
}
//函数名:Elevator参数:无
void HRRF(){
//函数功能:调用电梯调度算法printf("\n***********HRRF多道批处理作业调度***********\n");node *head = in();node *h,*q;h = NULL;q = head;while(q!=NULL){node *p = (node *)malloc(sizeof(node));*p = *q;p->next = NULL;if(h == NULL){h = p;}else{node *a = h,*b;while(a!=NULL){if(p->arrive.hour*60+p->arrive.min<a->arrive.hour*60+a->arrive.min)break;b = a;a = a->next;}if(a == h){p->next = h;h = p;}else{p->next = a;b->next = p;}}q = q->next; }time now;now.hour = 0;now.min = 0;int qaq = 0,available = resource,flag=0,space = memory.size;q = h;node *duilie = NULL,*flow,*flagh;node *k,*flowk,*KK=NULL; while(h!=NULL){flagh = h;//每一次大循环都找在自己执行完的时间内并且符合条件的 摘下来 //大循环每次末尾都回收第一个 q = h;if(flag==0){flag=1;//如果duilie为NULL去找先到达的node *first = h,*b = h;while(b!=NULL){if(b->arrive.hour*60+b->arrive.min<first->arrive.hour*60+first->arrive.min)first = b;b = b->next; } b = h;while(b!=NULL){if(b->next == first)break;b = b->next;}if(first == h){h = first->next;first->next = NULL;duilie = first;}else{b->next = first->next;first->next = NULL;duilie = first;}flow = duilie;now.hour = duilie->arrive.hour;now.min = duilie->arrive.min;duilie->start.hour = now.hour;duilie->start.min = now.min;duilie->zhuangru.hour = now.hour;duilie->zhuangru.min = now.min;now.hour = duilie->start.hour + (duilie->start.min + duilie->zx)/60;now.min = (duilie->start.min + duilie->zx)%60;duilie->finish.hour = now.hour;duilie->finish.min = now.min;duilie->zz = duilie->finish.hour*60 + duilie->finish.min - (duilie->arrive.hour*60 + duilie->arrive.min);duilie->zzxs = duilie->zz*1.0/duilie->zx; duilie = duilie->next;KK = flow;flowk = KK;}q = h;node *xyb=NULL;while(q!=NULL){node *p = (node *)malloc(sizeof(node));*p = *q;p->next = NULL;if(xyb == NULL){xyb = p;}else{node *a = xyb,*b;while(a!=NULL){if((now.hour*60+now.min-q->arrive.hour*60-q->arrive.min+q->zx)*1.0/q->zx>(now.hour*60+now.min-a->arrive.hour*60-a->arrive.min+a->zx)*1.0/a->zx)break;b = a;a = a->next;}if(a == xyb){p->next = xyb;xyb = p;}else{p->next = a;b->next = p;}} q = q->next; } //如果不是第一个,则遍历去找符合条件的 node *hh = xyb,*hhpre;while(hh!=NULL){if(memory.size - hh->size>=0&&available-hh->tai>=0){//满足条件available -=hh->tai;memory.size -=hh->size;node *xybh = h,*xybhpre;while(xybh!=NULL){if(strcmp(xybh->name,hh->name) == 0){xybh->zhuangru.hour = now.hour;xybh->zhuangru.min = now.min;break;}xybhpre = xybh;xybh = xybh->next;}if(xybh == h){h = h->next;xybh->next = NULL;if(duilie == NULL){ duilie = xybh;flow = xybh; } else{flow->next = xybh;flow = xybh;}}else{xybhpre->next = xybh->next;xybh->next = NULL;if(duilie == NULL){ duilie = xybh;flow = xybh; } else{flow->next = xybh;flow = xybh;} }}//否则不满足条件——————> 空间不满足 or 磁带机不满足hh = hh->next; }//回收资源k=duilie;k->start.hour = now.hour;k->start.min = now.min;k->finish.hour = k->start.hour+(k->start.min+k->zx)/60;k->finish.min = (k->start.min + k->zx)%60;k->zz = k->finish.hour*60 + k->finish.min - (k->arrive.hour*60 + k->arrive.min);k->zzxs = k->zz*1.0/k->zx; now.hour = k->finish.hour;now.min = k->finish.min;available +=k->tai;memory.size +=k->size;duilie = duilie->next;if(KK==NULL){KK=k;flowk = k;k->next = NULL;}else{flowk->next = k;flowk = k;k->next = NULL;}}while(duilie!=NULL)//h==NULL但是duilie不等于NULL {k=duilie;k->start.hour = now.hour;k->start.min = now.min;k->finish.hour = k->start.hour+(k->start.min+k->zx)/60;k->finish.min = (k->start.min + k->zx)%60;k->zz = k->finish.hour*60 + k->finish.min - (k->arrive.hour*60 + k->arrive.min);k->zzxs = k->zz*1.0/k->zx; now.hour = k->finish.hour;now.min = k->finish.min;available +=k->tai;memory.size +=k->size;duilie = duilie->next;if(KK==NULL){KK=k;flowk = k;k->next = NULL;}else{flowk->next = k;flowk = k;k->next = NULL;}}output(KK);
}//函数名:Out参数:无
void Out(){
//函数功能:输出选项菜单printf("***************多道批处理作业调度***************\n");printf(" * 1.先来先服务调度算法(FCFS) *\n");printf(" * 2.最短作业优先算法(SPF) *\n");printf(" * 3.响应比最高者优先算法(HRRF) *\n");printf(" * 0.退出 *\n");printf(" 请输入选项[ ]\b\b");
}
int main()
{while (1){Out();//给出选项int c;scanf("%d", &c);switch (c){//选择算法case 1:FCFS();break;case 2:SPF();break;case 3:HRRF();break;default:printf("\n调度结束!");exit(1);}}return 0;
}
实验结果
实验总结
如果学会了作业不可移动的话,对于可移动来说就是不用实际分配内存给作业。
tips:本题思路
这篇关于操作系统原理与系统——实验十三多道批处理作业调度(作业可移动)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!