操作系统原理与系统——实验十三多道批处理作业调度(作业可移动)

关键代码

#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:本题思路

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/10655.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Polygon市值机器人

随着区块链技术的蓬勃发展和数字货币市场的日益繁荣&#xff0c;投资者们对于如何精准把握市场动态、实现资产稳健增长的需求愈发迫切。在这个背景下&#xff08;市值管理飞//机//aishutuyu&#xff09;&#xff0c;Polygon市值机器人应运而生&#xff0c;作为一款基于Polygon公…

LeetCode 第397场周赛个人题解

目录 100296. 两个字符串的排列差 原题链接 思路分析 AC代码 100274. 从魔法师身上吸取的最大能量 原题链接 思路分析 AC代码 100281. 矩阵中的最大得分 原题链接 思路分析 AC代码 100312. 找出分数最低的排列 原题链接 思路分析 AC代码 100296. 两个字符串的排…

timerfd加epoll封装定时器

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 1、用timerfd加epoll封装定时器的优点2、代码实现 1、用timerfd加epoll封装定时器的优点 定时器为什么需要timerfd 在设计定时器时&#xff0c;我们首先想到的就是…

【SpringBoot】Redis Lua脚本实战指南:简单高效的构建分布式多命令原子操作、分布式锁

文章目录 一.Lua脚本1.Lua特性2.Lua优势 二.Lua语法1.注释2.变量3.数据类型&#xff1a;3.1.基本类型3.2.对象类型&#xff1a;表&#xff08;table&#xff09; 4.控制结构&#xff1a;4.1.条件语句: 使用if、else和elseif来实现条件分支。4.2.循环结构&#xff1a;Lua支持for…

Shell参数扩展形式学习笔记

Shell参数扩展形式学习笔记 文章目录 Shell参数扩展形式学习笔记空值判断处理 ${parameter:-word} ${parameter:word} ${parameter:?word} ${parameter:word}变量位置截取 ${parameter:offset} ${parameter:offset:length}变量匹配组合 ${!prefix*} ${!prefix} ${!name[]} ${!…

感知机和神经网络

引入 什么是神经网络&#xff1f; 我们今天学习的神经网络&#xff0c;不是人或动物的神经网络&#xff0c;但是又是模仿人和动物的神经网络而定制的神经系统&#xff0c;特别是大脑和神经中枢&#xff0c;定制的系统是一种数学模型或计算机模型&#xff0c;神经网络由大量的人…

图像处理:图像噪声添加

文章目录 前言一、高斯噪声二、椒盐噪声三、泊松噪声四、斑点噪声五、指数噪声六、均匀噪声总结 前言 本文主要介绍几种添加图像噪声的方法&#xff0c;用于数据增强等操作。 以下图为例。 一、高斯噪声 高斯噪声就是给图片添加一个服从高斯分布的噪声&#xff0c;可以通过调…

vLLM初探

vLLM是伯克利大学LMSYS组织开源的大语言模型高速推理框架&#xff0c;旨在极大地提升实时场景下的语言模型服务的吞吐与内存使用效率。vLLM是一个快速且易于使用的库&#xff0c;用于 LLM 推理和服务&#xff0c;可以和HuggingFace 无缝集成。vLLM利用了全新的注意力算法「Page…

Python+PySpark数据计算

1、map算子 对RDD内的元素进行逐个处理&#xff0c;并返回一个新的RDD&#xff0c;可以使用lambda以及链式编程&#xff0c;简化代码。 注意&#xff1a;再python中的lambda只能有行&#xff0c;如果有多行&#xff0c;要写成外部函数&#xff1b;&#xff08;T&#xff09;-&…

train_gpt2_fp32.cu - cudaCheck

源码 // CUDA error checking void cudaCheck(cudaError_t error, const char *file, int line) {if (error ! cudaSuccess) {printf("[CUDA ERROR] at file %s:%d:\n%s\n", file, line,cudaGetErrorString(error));exit(EXIT_FAILURE);} }; 解释 该函数用于检查CU…

无人机路径规划:基于鲸鱼优化算法WOA的复杂城市地形下无人机避障三维航迹规划,可以修改障碍物及起始点(Matlab代码)

一、部分代码 close all clear clc rng(default); %% 载入数据 data.S[50,950,12]; %起点位置 横坐标与纵坐标需为50的倍数 data.E[950,50,1]; %终点点位置 横坐标与纵坐标需为50的倍数 data.Obstaclexlsread(data1.xls); data.numObstacleslength(data.Obstacle(:,1)); …

连接和断开与服务器的连接

要连接到服务器&#xff0c;通常需要在调用mysql时提供一个MySQL用户名&#xff0c;很可能还需要一个密码。如果服务器在除了登录的计算机之外的机器上运行&#xff0c;您还必须指定主机名。联系您的管理员以找出应该使用哪些连接参数来连接&#xff08;即使用哪个主机、用户名…

TypeError: can only concatenate str (not “int“) to str

TypeError: can only concatenate str (not "int") to str a 窗前明月光&#xff0c;疑是地上霜。举头望明月&#xff0c;低头思故乡。 print(str_len len(str_text) : len(a)) 试图打印出字符串 a 的长度&#xff0c;但是在 Python 中拼接字符串和整数需要使用字符…

【微服务】spring aop实现接口参数变更前后对比和日志记录

目录 一、前言 二、spring aop概述 2.1 什么是spring aop 2.2 spring aop特点 2.3 spring aop应用场景 三、spring aop处理通用日志场景 3.1 系统日志类型 3.2 微服务场景下通用日志记录解决方案 3.2.1 手动记录 3.2.2 异步队列es 3.2.3 使用过滤器或拦截器 3.2.4 使…

triton编译学习

一 流程 Triton-MLIR: 从DSL到PTX - 知乎 (zhihu.com)https://zhuanlan.zhihu.com/p/671434808Superjomns blog | OpenAI/Triton MLIR 迁移工作简介https://superjom

基于STM32单片机的环境监测系统设计与实现

基于STM32单片机的环境监测系统设计与实现 摘要 随着环境污染和室内空气质量问题的日益严重&#xff0c;环境监测系统的应用变得尤为重要。本文设计并实现了一种基于STM32单片机的环境监测系统&#xff0c;该系统能够实时监测并显示室内环境的温湿度、甲醛浓度以及二氧化碳浓…

C语言题目:A+B for Input-Output Practice

题目描述 Your task is to calculate the sum of some integers 输入格式 Input contains an integer N in the first line, and then N lines follow. Each line starts with a integer M, and then M integers follow in the same line 输出格式 For each group of inpu…

Sass详解

Sass&#xff08;Syntactically Awesome Stylesheets&#xff09;是一种CSS预处理器&#xff0c;它允许你使用变量、嵌套规则、混入&#xff08;Mixin&#xff09;、继承等功能来编写CSS&#xff0c;从而使CSS代码更加简洁、易于维护和扩展。下面是Sass的详细解释&#xff1a; …

【docker】容器优化:一行命令换源

原理&#xff1a; 根据清华源提供的Ubuntu 软件仓库进行sources.list替换 ubuntu | 镜像站使用帮助 | 清华大学开源软件镜像站 | Tsinghua Open Source Mirror 1、换源 echo "">/etc/apt/sources.list \&& echo "# 默认注释了源码镜像以提高 apt …

新iPadPro是怎样成为苹果史上最薄产品的|Meta发布AI广告工具全家桶| “碾碎一切”,苹果新广告片引争议|生成式AI,苹果倾巢出动

Remini走红背后&#xff1a;AI生图会是第一个超级应用吗&#xff1f;新iPadPro是怎样成为苹果史上最薄产品的生成式AI&#xff0c;苹果倾巢出动Meta发布AI广告工具全家桶&#xff0c;图像文本一键生成解放打工人苹果新iPadPro出货量或达500万台&#xff0c;成中尺寸OLED发展关键…