#include<iostream>
#include<string>
#include <stdio.h>
#include<stdlib.h>
using namespace std;
struct PCB //创建进程
{
int ID; //进程优先数ID,其中0为闲逛进程
int priority; //进程优先级Priority
int CPUtime; //进程占用的CPU时间
int ALLtime; //进程总共需要运行的时间
int State; //进程状态,:就绪态;:运行态;。
int arrive_time; //进程到达时间
int pcnumber; //记录进程数
int time; //进程执行时间
PCB* next; //链表指针
};
int pcnumber;
int min_usetime;
int process;
int process1;
int time;
void init(PCB *process) //链表初始化函数
{
int i;
PCB *p;
PCB *temp;
process->next=NULL;
p=process;
int pcnumber;
printf("请输入进程数: ");
scanf("%d",&pcnumber);
// N=pcnumber;
// srand(time(NULL));
for(i=0;i<pcnumber;i++)
{
temp=(PCB *)malloc(sizeof(PCB));
temp->next=NULL;
temp->ID=i+1;
temp->CPUtime=0;
temp->State=0;
temp->pcnumber=pcnumber;
temp->ALLtime=rand()%15+15;
temp->priority=rand()%9+1;
temp->arrive_time=rand()%5+1;
p->next=temp;
p=p->next;
}
}
void output(PCB *process) //输出函数
{
PCB *temp;
PCB *temp1;
temp1=process->next;
temp=process->next;
int i=0;
int j=temp1->pcnumber;
//static int j=0;
while(temp1!=NULL)
{
if(temp1->State==1)
break;
temp1=temp->next;
}
if(temp1!=NULL)
{
printf("此时进程状态为:\n");
}
else
printf("进程初始状态为:\n");
printf(" 优先数 优先级 到达时间 运行时间 进程状态\n");
while(temp!=NULL)
{
if(temp->ID<=9)
printf("%进程%d : %d %d %d %d ",temp->ID-1,temp->ID,temp->priority,temp->arrive_time,temp->ALLtime);
else if(temp->ID==10)
printf("%进程%d : %d %d %d %d ",temp->ID-1,temp->ID,temp->priority,temp->arrive_time,temp->ALLtime);
else
{
printf("%进程%d : %d %d %d %d ",temp->ID-1,temp->ID,temp->priority,temp->arrive_time,temp->ALLtime);
}
if(temp->State==0)
printf("ready\n");
else if(temp->State==1)
printf("runnnig\n");
else
printf("end!\n");
temp=temp->next;i++;
}
}
void FCFS() //先来先服务算法
{
PCB *process;
process=(PCB *)malloc(sizeof(PCB));
init(process);
output(process);
int minarrive_time=0; //定义最短达到时间
PCB *temp,*temp2;
int pcnumber=process->next->pcnumber;
temp=process->next; //初始化
while(pcnumber>0)
{
temp=process->next;
while(temp->State!=0)
temp=temp->next;
temp2=temp; //用于比较到达时间相同情况下比较优先级
minarrive_time=temp->arrive_time;
temp=temp->next;
while(temp!=NULL)
{
if(temp->State==0&&temp->arrive_time<=minarrive_time)
{
if(temp->arrive_time==temp2->arrive_time)
{
if(temp2->priority>temp->priority) //优先级小的先处理
{
minarrive_time=temp->arrive_time;
temp2=temp;
}
}
else
{
minarrive_time=temp->arrive_time;
temp2=temp;
}
}
temp=temp->next;
}
temp2->State=1;
output(process);
temp2->State=2;
pcnumber--;
}
output(process);
}
void SJF()
{
PCB *process1; //重新开辟进程
int remainder=0; //共职等于进程数减一,作用为用于第二个while循环
process1=(PCB*)malloc(sizeof(PCB));
init(process1);
PCB *temp1,*temp2;
int minarrive_time=0;min_usetime=0; //定义最短到达时间和最少使用时间
temp1=process1->next; //初始化
while(temp1->State!=0)
temp1=temp1->next;
minarrive_time=temp1->arrive_time;
printf("minarrive_time=%d\n",minarrive_time);
temp2=temp1;
output(process1);
temp1=temp1->next;
while(temp1!=NULL)
{
if(temp1->State==0&&temp1->arrive_time<=minarrive_time)
{
if(temp1->arrive_time==temp2->arrive_time)
{
if(temp2->priority>temp1->priority) //优先级小的先处理
{
minarrive_time=temp1->arrive_time;temp2=temp1;
}
}
else
{
minarrive_time=temp1->arrive_time;
temp2=temp1;
}
}
temp1=temp1->next;
}
temp2->State=1;
output(process1);
temp2->State=2;
remainder=process1->next->pcnumber;
--remainder;
while(remainder>0)
{
temp1=process1->next;
while(temp1->State!=0)
temp1=temp1->next;
temp2=temp1;
min_usetime=temp1->ALLtime;
while(temp1!=NULL)
{
if(temp1->State==0&&temp1->ALLtime<=min_usetime)
{
if(temp1->ALLtime==temp2->ALLtime)
{
if(temp2->priority>temp1->priority) //优先级小的先处理
{
min_usetime=temp1->ALLtime;temp2=temp1;
}
}
else
{
min_usetime=temp1->ALLtime;temp2=temp1;
}
}
temp1=temp1->next;
}
temp2->State=1;
output(process1);
temp2->State=2;
remainder--;
}
output(process1);
}
void PSA()
{
PCB *process1; //重新开辟进程
int remainder=0; //共职等于进程数减一,作用为用于第二个while循环
process1=(PCB*)malloc(sizeof(PCB));
init(process1); //初始化
int minarrive_time=0,min_priority=0; //定义最短到达时间和最小优先级
PCB *temp1,*temp2;
temp1=process1->next; //初始化
while(temp1->State!=0)
temp1=temp1->next;
minarrive_time=temp1->arrive_time; //初始值为第一个state不为的arrive_time
temp2=temp1;
output(process1);
temp1=temp1->next;
while(temp1!=NULL) //寻找第一个到达的进程
{
if(temp1->State==0&&temp1->arrive_time<=minarrive_time)
{
if(temp1->arrive_time==temp2->arrive_time) //到达时间相同的,优先级小的先执行
{
if(temp2->priority>temp1->priority) //优先级小的先处理
{
minarrive_time=temp1->arrive_time;temp2=temp1;
}
}
else
{
minarrive_time=temp1->arrive_time;
temp2=temp1;
}
}
temp1=temp1->next;
}
temp2->State=1;
output(process1);
temp2->State=2;
remainder=process1->next->pcnumber;
--remainder;
while(remainder>0)
{
temp1=process1->next;
while(temp1->State!=0)
temp1=temp1->next;
temp2=temp1;
min_priority=temp1->priority;
while(temp1!=NULL) //优先级小的先执行
{
if(temp1->State==0&&temp1->priority<=min_priority)
{
if(temp1->priority==temp2->priority) //优先级相同的,先到达的先执行
{
if(temp1->arrive_time<temp2->arrive_time)
{
min_priority=temp1->priority;
temp2=temp1;
}
}
else
{
min_priority=temp1->priority;
temp2=temp1;
}
}
temp1=temp1->next;
}
temp2->State=1;
output(process1);
temp2->State=2;
remainder--;
}
output(process1);
}
int judge(int p) //判断输入的选择正误函数
{
int i;
while(p<0||p>4)
{
printf("您的输入有误(0~4)请重新输入:");
scanf("%d",&i);
p=i;
}
return p;
}
void chooseoutput()
{
int p;
printf("****0表示:退出\n");
printf("****1表示:先到先服务算法\n");
printf("****2表示:最短作业优先调度算法\n");
printf("****3表示:优先级调度算法\n");
printf("*****************************************************************\n");
printf("请输入您的选择:");
scanf("%d",&p);
p=judge(p);
while(1)
{
if(p==1)
{
printf("先来先服务算法\n");
FCFS();
printf("****************************************************************\n");
printf("请输入您的选择:");
scanf("%d",&p);
p=judge(p);
}
else if(p==2)
{
printf("最短作业优先调度算法\n");
SJF();
printf("****************************************************************\n");
printf("请输入您的选择:");
scanf("%d",&p);
p=judge(p);
}
else if(p==3)
{
printf("优先级调度算法\n");
PSA();
printf("****************************************************************\n");
printf("请输入您的选择:");
scanf("%d",&p);
p=judge(p);
}
else
exit(0);
}
}
void main()
{
chooseoutput();
}
pv.rar_PV原语
版权申诉
154 浏览量
2022-09-24
20:16:54
上传
评论
收藏 2KB RAR 举报
我虽横行却不霸道
- 粉丝: 72
- 资源: 1万+
最新资源
- 论文(最终)_20240430235101.pdf
- 基于python编写的Keras深度学习框架开发,利用卷积神经网络CNN,快速识别图片并进行分类
- 最全空间计量实证方法(空间杜宾模型和检验以及结果解释文档).txt
- 5uonly.apk
- 蓝桥杯Python组的历年真题
- 2023-04-06-项目笔记 - 第一百十九阶段 - 4.4.2.117全局变量的作用域-117 -2024.04.30
- 2023-04-06-项目笔记 - 第一百十九阶段 - 4.4.2.117全局变量的作用域-117 -2024.04.30
- 前端开发技术实验报告:内含4四实验&实验报告
- Highlight Plus v20.0.1
- 林周瑜-论文.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈