先来先服务进程调度算法模拟
//先来先服务调度算法
//运行环境:
//Dev c++
//算法思想
//本实验是模拟进程调度中的先来先服务算法,每次CPU都是按照进入就绪队列的先后次序依次选中一个进程装入CPU运行,等结束时再选取下一个。
//核心数据结果
#include<cstdio>
#include<iostream>
#include<malloc.h>
#include<cstring>
using namespace std;
struct Time {
int hour, min;
};
struct node {
int id;//编号你
char name[20];//进程名
Time arrive;//到达就绪队列时间
int zx;//执行时间
Time start;//开始时间
Time finish;//完成时间
int zz;//周转时间=完成时间-到达时间
float zzxs;//带权周转系数=周转间/执行时间,下同
struct node* next;
};
//程序框架
//函数名:check,函数参数:Time x(当前进程的到达时间),Time y(待插入进程的到达时间)
bool check(Time x, Time y) {
//函数功能:比较到达时间的早晚
if(x.hour>y.hour){
return true;
}
else if(x.hour==y.hour&&x.min>y.min){
return true;
}
return false;
}
struct Q{
node *front, *end;
};
//函数名:init,函数参数:Q &x(含有就绪队列的队头和队尾节点地址)
void init(Q &x) {
//函数功能:初始化,为之后操作做准备
x.front=(node*)malloc(sizeof(node));
x.front->arrive.hour=0;
x.front->arrive.min=0;
x.front->next=NULL;
x.end=x.front->next;
x.front->finish.hour=0;
x.front->finish.min=0;
}
//函数名:insert,函数参数:Q *x(指向含有就绪队列的队头和队尾节点的指针),node tmp()
void insert(Q *x, node* p) {
//函数功能:构建并按到达时间先后插入进程节点放入就绪队列中
node* q=x->front;
while(q!=NULL){
if(q->next==NULL){
p->next=q->next;
q->next=p;
break;
}
else if(q->next->arrive.hour>p->arrive.hour||(q->next->arrive.hour==p->arrive.hour&&q->next->arrive.min>p->arrive.min)){
p->next=q->next;
q->next=p;
break;
}
q=q->next;
}
}
//函数名:dele,函数参数:Q &x(含有就绪队列的队头和队尾节点地址)
void dele(Q &x) {
//函数功能:初始化,为下一个进程序列做准备
node* p=x.front;
if(x.end!=x.front){
p=p->next;
}
}
//函数名:input,函数参数:Q &x(含有就绪队列的队头和队尾节点地址)
void input(Q &x) {
//函数功能:输入节点信息,构建并按到达时间先后依次插入进程节点放入就绪队列中
printf("请输入操作(1:开始进程调度;0:结束进程:") ;
int a;
scanf("%d",&a);
if(a==0){
printf("操作结束");
exit(0);
}
else{
printf("请输入进程数量:");
int a;
scanf("%d",&a);
printf("请输入进程参数:\n");
printf("id号 名字 到达时间 执行时间(分钟)\n");
int id,zx;
char name[20];
char arrive[5];
for(int i=0;i<a;i++){
scanf("%d",&id);
scanf("%s",&name);
scanf("%s",&arrive);
scanf("%d",&zx);
node* p=(node*)malloc(sizeof(node));
p->id=id;
p->zx=zx;
for(int i=0;i<20;i++){
p->name[i]=name[i];
}
if(arrive[0]=='0'){
p->arrive.hour=(arrive[1]-48);
p->arrive.min=(arrive[3]-48)*10+arrive[4]-48;
}
else if(arrive[1]==':'){
p->arrive.hour=arrive[0]-48;
p->arrive.min=(arrive[2]-48)*10+arrive[3]-48;
}
else{
p->arrive.hour=(arrive[0]-48)*10+(arrive[1]-48);
p->arrive.min=(arrive[3]-48)*10+arrive[4]-48;
}
insert(&x,p);
}
}
}
//函数名:cal,函数参数:Time x(当前进程的开始时间), int y(当前进程的执行时间)
Time cal(Time x, int y) {
//函数功能:计算当前进程的结束时间(开始时间+执行时间)
Time z;
z.hour=x.hour;
z.min=x.min;
z.hour+=y/60;
z.min=y%60;
return z;
}
//函数名:deline,函数参数:Time x(上一进程的结束时间),Time y(当前进程的到达时间)
int deline(Time x, Time y) {
//函数功能:计算进程的等待时间(即上一进程的结束时间与当前进程的到达时间的时间差)
return (y.hour-x.hour)*60+(y.min-x.min);
}
//函数名:fcfs,函数参数:Q *x(指向含有就绪队列的队头和队尾节点的指针)
void fcfs(Q *x) {
//函数功能:计算进程相关时间信息,分别确定开始时间,结束时间(开始时间+执行时间),周转时间(结束时间-到达时间),周转系数(周转时间/执行时间)
node* p=x->front;
node* q=p;
p=p->next;
while(p!=NULL){
if(q->finish.hour>p->arrive.hour||(q->finish.hour==p->arrive.hour&&(q->finish.min>p->arrive.min))){
p->start.hour=q->finish.hour;
p->start.min=q->finish.min;
}
else{
p->start.hour=p->arrive.hour;
p->start.min=p->arrive.min;
}
p->finish.hour=(p->start.hour+(p->zx+p->start.min)/60);
p->finish.min=(p->zx+p->start.min)%60;
p->zz=60*(p->finish.hour-p->arrive.hour)+p->finish.min-p->arrive.min;
p->zzxs=p->zz*1.0/p->zx;
q=p;
p=p->next;
}
}
//函数名:output,函数参数:Q &x(含有就绪队列的队头和队尾节点的地址)
void output(Q &x) {
//函数功能:按进程执行顺序分别输出每个进程全部结果信息和系统平均时间
printf("id号 名字 到达时间 执行时间(分钟) 开始时间 完成时间 周转时间(分钟) 带权周转系数\n");
node* p=x.front;
p=p->next;
while(p!=NULL){
printf("%d ",p->id);
printf("%3s ",p->name);
printf(" %02d:%02d ",p->arrive.hour,p->arrive.min);
printf(" %2d (分钟) ",p->zx);
printf(" %02d:%02d ",p->start.hour,p->start.min);
printf(" %02d:%02d ",p->finish.hour,p->finish.min);
printf(" %2d (分钟) ",p->zz);
printf(" %.2lf \n",p->zzxs);
p=p->next;
}
}
int main() {
Q x;
while(1) {
init(x);
input(x);
fcfs(&x);
output(x);
while(x.front != NULL) dele(x);
}
return 0;
}
//测试用例:
//5个进程:
//5001 p1 9:40 20
//5004 p4 10:10 10
//5005 p5 10:05 30
//5002 p2 9:55 15
//5003 p3 9:45 25
//
//10个进程:
//5001 p1 19:40 20
//5002 p4 10:10 10
//5003 p5 10:05 30
//5004 p2 9:55 15
//5005 p3 9:45 25
//5006 p6 11:40 20
//5007 p8 12:10 10
//5008 p9 13:05 30
//5009 p10 19:55 15
//5010 p7 7:15 15
短进程优先调度算法模拟
#include <iostream>
#include<cstdio>
#include <malloc.h>
using namespace std;
typedef struct data{
int hour;
int min;
}time;
typedef struct node{
int id;//编号
char name[20];//名字
time arrive;//到达时间
int zx;//执行时间;
time start;//开始时间
time finish;//完成时间
int zz;//周转时间=执行完成时间-到达就绪队列时间
double zzxs;//带权周转时间=周转时间/执行时间
struct node* next;
}linknode;
typedef linknode* linklist;
typedef struct{
linklist front;
linklist rear;
}queue; //队列
//函数名:init 参数:无
queue* init(queue* p){
//函数功能:初始化队列,返回队列指针
p = (queue*)malloc(sizeof(queue));
p->front = NULL;
p->rear = NULL;
return p;
}
//函数名:insert 参数:Queue *cc, node *x
void insert(queue* cc,linklist s){
//函数功能:尾插入队
if(cc->front == NULL)
cc->front = cc->rear = s;
else{
cc->rear->next = s;
cc->rear = s;
}
}
//函数名:dele 参数:Queue *cc
void dele(queue* cc){
//功能:队首结点出队
if(cc->front = NULL){
printf("该队列为空");
return;
}
cc->front = cc->front->next;
}
//函数名:input 参数:Queue *cc
void input(queue* cc){
//功能:实现作业队列的输入
int count;
printf("请输入进程数:");
scanf("%d", &count);
printf("id号 名字 到达时间 执行时间\n");
for(int i = 0; i < count; i++){
linklist tmp = (linklist)malloc(sizeof(linknode));
scanf("%d %s %d:%d %d", &tmp->id, &tmp->name, &tmp->arrive.hour, &tmp->arrive.min, &tmp->zx);
tmp->start.hour = tmp->start.min = 0;
tmp->finish.hour = tmp->finish.min = 0;
tmp->zz = tmp->zzxs = 0;
tmp->next = NULL;
insert(cc, tmp);
}
printf("\n");
}
//函数名:sort参数:Queue *cc
void sort(queue* cc){
//函数功能:对到达时间进行排序
linklist p, pre = NULL, q = NULL;
int flag = 1;
while(flag){
flag = 0;
for(p = cc->front; p != NULL && p->next != NULL; p = p->next){
q = p->next;
if(p->arrive.hour * 60 + p->arrive.min >= q->arrive.hour * 60 + q->arrive.min){
if(q == cc->rear)
cc->rear = p;
p->next = q->next;
q->next = p;
if(pre == NULL)
cc->front = q;
else
pre->next = q;
pre = q;
p = q;
flag = 1;
}
else
pre = p;
}
pre = NULL;
}
}
time ComputeTime(time tt, linklist q){
//函数功能:更新当前时间和进程的各项时间
if(q->arrive.hour * 60 + q->arrive.min > tt.hour * 60 + tt.min){
tt = q->arrive;
}
time now;
if (q->zx < 60) {
now.hour = 0;
now.min = q->zx;
}
else {
now.hour = q->zx / 60;
now.min = q->zx % 60;
}
q->start = tt;
q->finish.hour = q->start.hour + now.hour;
q->finish.min = q->start.min + now.min;
if(q->finish.min >= 60){
q->finish.hour += 1;
q->finish.min -= 60;
}
q->zz = (q->finish.hour - q->arrive.hour) * 60 + (q->finish.min - q->arrive.min);
q->zzxs = (float) q->zz / (float) q->zx;
tt = q->finish;
return tt;
}
//函数名:solve_SJF参数:Queue *cc
void solve_SJF(queue* cc){
//函数功能:解决短进程优先调度算法
sort(cc);
linklist last, p, pre = NULL, near = NULL, pren = NULL, far = NULL, pref = NULL;
time now;
now.hour = cc->front->arrive.hour;
now.min = cc->front->arrive.min;
for(last = cc->front; last != NULL && last->next != NULL; last = last->next){
now = ComputeTime(now, last);
pre = last;
pren = pref = last;
for(p = last->next; p != NULL; p = p->next){
if(now.hour * 60 + now.min >= p->arrive.hour * 60 + p->arrive.min){
if(near == NULL){
near = p;
}
else if(near != NULL && near->zx > p->zx){
pren = pre;
near = p;
}
}
else{
if(near){
break;
}
else if(far == NULL){
far = p;
}
else if(far != NULL && far->arrive.hour * 60 + far->arrive.min > p->arrive.hour * 60 + p->arrive.min){
pref = pre;
far = p;
}
}
pre = pre->next;
}
if(near != NULL){
if(near == cc->rear){
cc->rear = pren;
}
if(last->next != near){
pren->next = near->next;
near->next = last->next;
last->next = near;
}
}
else if(near == NULL && far != NULL){
if(far == cc->rear){
cc->rear = pref;
}
pref->next = far->next;
far->next = last->next;
last->next = far;
}
near = far = NULL;
}
now = ComputeTime(now, last);
float azz=0, azzxs=0;
int count = 0;
printf("模拟短进程调度过程输出结果:\n");
printf("id号 名字 到达时间 执行时间(分钟) 开始时间 完成时间 周转时间(分钟) 带权周转系数\n");
node* tmp = cc->front;
while(tmp != NULL){
azz += (float)tmp->zz;
azzxs += (float)tmp->zzxs;
count++;
printf("%d %s %02d:%02d %d(分钟) %02d:%02d %02d:%02d %d(分钟) %.2f\n",
tmp->id, tmp->name, tmp->arrive.hour, tmp->arrive.min, tmp->zx, tmp->start.hour, tmp->start.min, tmp->finish.hour, tmp->finish.min, tmp->zz, tmp->zzxs);
tmp = tmp->next;
}
printf("\n系统平均周转时间:\t\t\t\t\t %.2f\n", azz / count);
printf("系统平均带权周转系数:\t\t\t\t\t\t\t %.2f\n\n", azzxs / count);
}
int main()
{
while(true)
{
int op;
printf("请输入操作(1:开始进程调度;0:结束进程)");
scanf("%d",&op);
if(op==0) break;
queue *cc;
cc = init(cc);
input(cc);
solve_SJF(cc);
}
return 0;
}
/*
5001 p1 9:40 20
5004 p4 10:10 10
5005 p5 10:05 30
5002 p2 9:55 15
5003 p3 9:45 25
*/
/*
5001 p1 19:40 20
5002 p4 10:10 10
5003 p5 10:05 30
5004 p2 9:55 15
5005 p3 9:45 25
5006 p6 11:40 20
5007 p8 12:10 10
5008 p9 13:05 30
5009 p10 19:55 15
5010 p7 7:15 15
*/
优先级进程调度算法模拟
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <stdio.h>
#include <malloc.h>
#include <string.h>
using namespace std;
typedef struct data {
int hour;
int minute;
}times;
typedef struct node {
int id;//进程编号
char name[20];//进程名
int good;//优先级
times arrive;//到达就绪队列的时间
int zx;//执行时间
times start;//开始执行时间
times finish;//执行完成时间
int zz;//周转时间=执行完成时间-到达就绪队列时间
float zzxs;//带权周转时间=周转时间/执行时间
struct node* next;
}Node;
typedef struct Queue {
Node* front = NULL;
Node* tail = NULL;
}Queue;
Queue* init() {
Queue* p = (Queue*)malloc(sizeof(Queue));
p->front = NULL;
p->tail = NULL;
return p;
}
//函数名:timecompare() 参数:tt 当前时间, p 进程到达时间
bool timecompare(times tt, times p) {//tt<p(时间没到) false tt >= p true
//函数功能:比较进程到达时间和当前时间,若小于则返回false,否则返回true
if (tt.hour < p.hour || (tt.hour == p.hour && tt.minute < p.minute)) {
return false;
}
else {
true;
}
}
//函数名:timecompare2() 参数:tt 当前时间, p 进程到达时间
bool timecompare2(times tt, times p) {//tt<=p(时间没到) false tt > p true
//函数功能:比较进程到达时间和当前时间,若小于等于则返回false,否则返回true
if (tt.hour < p.hour || (tt.hour == p.hour && tt.hour < p.hour) || ((tt.hour == p.hour && tt.hour < p.hour))) {
return false;
}
else {
true;
}
}
//函数名:Levelcompare() 参数:p,q 进程
bool Levelcompare(Node* p, Node* q) {
//函数功能:比较p,q的优先级,p的优先级高则返回true,低则返回false,否则比较到达时间,p先或同时到达则返回true,反之则false
if (p->good > q->good) {
return true;
}
else if (p->good < q->good) {
return false;
}
else {
return timecompare2(q->arrive, p->arrive);
}
}
//函数名:LevelSorted() 参数:que 进程队列指针
void LevelSorted(Queue* que) {
//函数功能:对进程队列按优先级排序
return ;
//Queue* pue = (Queue*)malloc(sizeof(Queue));
//pue->front = NULL;
//pue->tail = NULL;
//pue->front = que->front;
//node* p = pue->front;
//node* q = que->front;
//pue->tail = que->tail;
//p->next = NULL;
//que = (Queue*)malloc(sizeof(Queue));
//que->front = NULL;
//que->tail = NULL;
//p = pue->front;
//que->front = p;
//p = p->next;
//node* qlast=(node*)malloc(sizeof(node));
//while (p != NULL) {
// q = que->front;
// while (q != NULL) {
// if (q->good > p->good) {
// if (q->next = NULL) {
// q->next = p;
// p->next = NULL;
// }
// qlast = q;
// q = q->next;
// }
// else {
// if (q == que->front) {
// que->front = p;
// p->next = q;
// break;
// }
// else {
// qlast->next = p;
// p->next = q;
// break;
// }
// }
// }
// p = p->next;
//}
}
//函数名:ComputeTime() 参数:tt 当前时间的指针,q 当前进程的指针
times ComputeTime(times* tt, Node* q) {
//函数功能:更新当前时间和进程的各项时间
if (q->arrive.hour * 60 + q->arrive.minute > tt->hour * 60 + tt->minute) {
tt->hour = (q->arrive.hour + (q->arrive.minute + q->zx) / 60);
tt->minute = (q->arrive.minute + q->zx) % 60;
}
else {
tt->hour = (tt->hour + (tt->minute + q->zx) / 60);
tt->minute = (tt->minute + q->zx) % 60;
}
return *tt;
}
//函数名:priority() 参数:que进程队列指针,tt当前时间 n 进程数
Queue* priority(Queue* que, times tt, int n) {
//函数功能:进行优先级进程调度,并同时更新当前时间。
bool used[100] = { false};
int a=0,aa=0;
float b=0;
node* q = que->front;
node* p = q;
printf("模拟进程优先进程调度过程输出结果\n");
printf("id号 名字 优先级 到达时间 执行时间(分钟) 开始时间 完成时间 周转时间(分钟) 带权周转系数\n");
for (int i = 0; i < n; i++) {
if (p->arrive.hour == tt.hour && p->arrive.minute == tt.minute && used[i] == false) {
used[i] = true;
printf("%d ", p->id);
printf("%s ", p->name);
printf("%d ", p->good);
printf("%02d:%02d ", p->arrive.hour, p->arrive.minute);
printf("%d ", p->zx);
p->start.hour = p->arrive.hour;
p->start.minute = p->arrive.minute;
if (p->arrive.hour * 60 + p->arrive.minute < tt.hour * 60 + tt.minute) {
p->start.hour = tt.hour;
p->start.minute = tt.minute;
}
printf("%02d:%02d ", p->start.hour, p->start.minute);
p->finish.hour = p->start.hour + (p->start.minute + p->zx) / 60;
p->finish.minute = (p->start.minute + p->zx)%60;
printf("%02d:%02d ", p->finish.hour,p->finish.minute);
p->zz = (p->finish.hour * 60 + p->finish.minute) - (p->arrive.hour * 60 + p->arrive.minute);
printf("%d ", p->zz);
p->zzxs = 1.0*p->zz / p->zx;
printf("%.2lf ", p->zzxs);
printf("\n");
aa++;
a += p->zz;
b += p->zzxs;
tt = ComputeTime(&tt, p);
break;
}
p = p->next;
}
p = q;
for (int i = 0; i < n - 1; i++) {
node* pp = que->front;
times bb;
bb.hour = 24;
bb.minute = 60;
int flag;
for (int i = 0; i < n; i++) {
if (pp->arrive.hour * 60 + pp->arrive.minute < tt.hour * 60 + tt.minute && used[i] == false) {
used[i] = true;
flag = i;
break;
}
else if (pp->arrive.hour * 60 + pp->arrive.minute < bb.hour * 60 + bb.minute&&used[i]==false) {
bb.hour = pp->arrive.hour;
bb.minute = pp->arrive.minute;
flag = i;
}
pp = pp->next;
}
used[flag] = true;
pp = que->front;
for (int i = 0; i < flag; i++) {
pp = pp->next;
}
/*printp*/
p = pp;
printf("%d ", p->id);
printf("%s ", p->name);
printf("%d ", p->good);
printf("%02d:%02d ", p->arrive.hour, p->arrive.minute);
printf("%d ", p->zx);
p->start.hour = p->arrive.hour;
p->start.minute = p->arrive.minute;
if (p->arrive.hour * 60 + p->arrive.minute < tt.hour * 60 + tt.minute) {
p->start.hour = tt.hour;
p->start.minute = tt.minute;
}
printf("%02d:%02d ", p->start.hour, p->start.minute);
p->finish.hour = p->start.hour + (p->start.minute + p->zx) / 60;
p->finish.minute = (p->start.minute + p->zx) % 60;
printf("%02d:%02d ", p->finish.hour, p->finish.minute);
p->zz = (p->finish.hour * 60 + p->finish.minute) - (p->arrive.hour * 60 + p->arrive.minute);
printf("%d ", p->zz);
p->zzxs = 1.0*p->zz / p->zx;
printf("%.2lf ", p->zzxs);
printf("\n");
aa++;
a += p->zz;
b += p->zzxs;
tt = ComputeTime(&tt, pp);
}
q->next = NULL;
printf("系统平均周转时间为%.2lf\n",1.0*a/aa);
printf("系统平均带权周转系数为%.2lf\n",1.0*b/aa);
return que;
}
//函数名:Print() 参数:que进程队列指针, n 进程数
void Print(Queue* que, int n) {
//函数功能:打印输出进程优先进程调度结果
//printf("模拟进程优先进程调度过程输出结果\n");
//printf("id号 名字 优先级 到达时间 执行时间(分钟) 开始时间 完成时间周 转时间(分钟) 带权周转系数\n");
//node* p = que->front;
//while (p != NULL) {
// printf("%d", p->id);
// printf("%s", p->name);
// printf("%d", p->good);
// printf("%d:%d", p->arrive.hour, p->arrive.minute);
// printf("%d", p->zx);
// printf("%d:%d", p->start.hour, p->start.minute);
// printf("%d", p->finish);
// printf("%d", p->zz);
// printf("%d", p->zzxs);
// printf("\n");
// p = p->next;
//}
}
//函数名:ScanIn() 参数:wait进程队列指针, n 进程数
times ScanIn(Queue* wait, int n) {
//函数功能:输入进程信息,返回最早的进程到达时间
times early;
early.hour = 24;
early.minute = 59;
printf("请输入进程参数:\n");
printf("id号 名字 优先级 到达时间 执行时间(分钟)\n");
int id;//进程编号
char name[20];//进程名
int good;//优先级
times arrive;//到达就绪队列的时间
int zx;//执行时间
node* p = (node*)malloc(sizeof(node));
p->next = NULL;
scanf("%d", &id);
scanf("%s", &name);
scanf("%d", &good);
scanf("%d:%d", &arrive.hour, &arrive.minute);
early.hour = arrive.hour;
early.minute = arrive.minute;
scanf("%d", &zx);
p->id = id;
for (int i = 0; i < 20; i++) {
p->name[i] = name[i];
}
p->good = good;
p->arrive.hour = arrive.hour;
p->arrive.minute = arrive.minute;
p->zx = zx;
wait->front = p;
wait->tail = p;
for (int i = 0; i < n - 1; i++) {
node* p = (node*)malloc(sizeof(node));
p->next = NULL;
scanf("%d", &id);
scanf("%s", &name);
scanf("%d", &good);
scanf("%d:%d", &arrive.hour, &arrive.minute);
if (arrive.hour < early.hour || (arrive.hour == early.hour && arrive.minute < early.minute)) {
early.hour = arrive.hour;
early.minute = arrive.minute;
}
scanf("%d", &zx);
p->id = id;
for (int i = 0; i < 20; i++) {
p->name[i] = name[i];
}
p->good = good;
p->arrive.hour = arrive.hour;
p->arrive.minute = arrive.minute;
p->zx = zx;
node* q = wait->front;
node* qq = q;
while (q != NULL) {
if (q->good < p->good) {
if (q == wait->front) {
wait->front = p;
p->next = q;
break;
}
else {
qq->next = p;
p->next = q;
break;
}
}
else if(q->next==NULL) {
q->next = p;
p->next = NULL;
wait->tail->next = p->next;
break;
}
qq = q;
q = q->next;
}
//wait->tail->next = p;
//wait->tail = p;
}
return early;
}
int main() {
Queue* wait;
wait = init();
int flag, n;
times earlytime;
while (1) {
printf("请输入操作:(1:开始进程;0:结束进程):");
scanf("%d", &flag);
if (flag == 0) {
printf("\n操作结束!\n");
break;
}
else {
printf("请输入进程数量:");
scanf("%d", &n);
earlytime = ScanIn(wait, n);
LevelSorted(wait);
wait = priority(wait, earlytime, n);
Print(wait, n);
wait = init();
}
}
return 0;
}
/*
5001 p1 1 14:40 20
5002 p4 2 10:10 10
5003 p5 3 10:05 30
5004 p2 4 9:55 15
5005 p3 5 9:45 25
5006 p6 6 10:40 20
5007 p8 7 11:10 10
5008 p9 8 12:05 30
5009 p10 9 13:55 15
5010 p7 10 7:15 15
*/
/*
5001 p1 1 9:40 20
5004 p4 4 10:10 10
5005 p5 3 10:05 30
5002 p2 3 9:55 15
5003 p3 2 9:45 25
*/
模拟实现单道批处理作业调度
#include<bits/stdc++.h>
using namespace std;
/* 下面有数据 */
typedef struct data{
int hour;
int min;
}Time;
typedef struct link_node{
char name[20];//作业名称
Time arrive;//作业入井时间
int zx;//作业执行时间
Time JobTime;//作业调度时间
int JobWait;//作业调度等待时间
Time ProcessTime;//进程调度时间
int ProcessWait;//进程调度等待时间
Time finish;//作业完成时间
int zz;//作业周转时间=作业完成时间-作业入井时间
float zzxs;//作业带权周转时间=作业周转时间/作业执行时间
struct link_node * next;//结点指针
int finished;//标记作业完成
float response;//作业响应化
}node;
typedef struct{
node *front; //队首指针
node *rear; //队尾指针
}Queue; //队列结构体
//函数名:output 参数:Queue *cc
void output(Queue *cc){
//功能:实现调度结果的输出
node* p = cc->front;
float azz = 0, azzxs = 0;
int count = 0;
cout << "名字\t\t入井时间\t运行时间(分钟)\t作业调度时间\t作业调度等待时间\t进程调度时间\t进程调度等待时间\t完成时间\t周转时间\t带权周转系数\n";
while(p){
azz += p->zz;
azzxs += p->zzxs;
count ++;
printf("%s\t\t%2d:%.2d\t\t%d(分钟)\t%2d:%.2d\t\t%-3d(分钟)\t\t%2d:%.2d\t\t%-3d(分钟)\t\t%2d:%.2d\t\t%-3d(分钟)\t%.4f(系数)\n", p->name, p->arrive.hour, p->arrive.min, p->zx,
p->JobTime.hour, p->JobTime.min, p->JobWait, p->ProcessTime.hour, p->ProcessTime.min, p->ProcessWait, p->finish.hour, p->finish.min, p->zz, p->zzxs);
p = p->next;
}
printf("系统平均周转时间为:\t\t\t\t\t\t\t\t\t\t\t\t\t\t%.2f\n", azz / count);
printf("系统带权平均周转时间为:\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t%.4f\n", azzxs / count);
}
//函数名:menu 参数:无
void menu(){
//功能:展示菜单,选择相应算法
cout << "********************作业管理*****************" << endl;
cout << " * 1.FCFS *" << endl;
cout << " * 2.SJF *" << endl;
cout << " * 3.HRRF *" << endl;
cout << " * 0.退出 *" << endl;
cout << "请输入要进行的操作号:";
}
//函数名:init 参数:无
Queue *init(){
//功能:初始化队列,返回队列指针
Queue* q = new Queue;
q->front = q->rear = NULL;
return q;
}
//函数名:insert 参数:Queue *cc, node *x
Queue *insert(Queue *cc, node *x){
//功能:将结点x按照到达时间先后按序插入队列cc
if(cc->front == NULL){
cc->front = x;
cc->rear = x;
return cc;
}
node* p = cc->front, *pre = cc->front;
while(p){
if(p->arrive.hour * 60 + p->arrive.min < x->arrive.hour * 60 + x->arrive.min){
pre = p;
p = p->next;
}
else
break;
}
if(pre == cc->front){
cc->front = x;
x->next = p;
return cc;
}
if(pre == cc->rear){
cc->rear->next = x;
cc->rear = x;
return cc;
}
pre->next = x;
x->next = p;
return cc;
}
//函数名:dele 参数:Queue *cc
Queue *dele(Queue *cc){
//功能:队首结点出队
cc->front = cc->front->next;
return cc;
}
//函数名:input 参数:Queue *cc
Queue *input(Queue *cc){
//功能:实现作业队列的输入
int sum;
cout << "请输入你需要创建的作业数:";
cin >> sum;
cout << "请输入" << sum << "个作业的数据:" << endl;
cout << "作业名\t入井时间\t运行时间(分钟):" << endl;
for(int i = 0; i < sum; i++){
node* tmp = new node;
scanf("%s %d:%d %d", &tmp->name, &tmp->arrive.hour, &tmp->arrive.min, &tmp->zx);
tmp->JobTime.hour = tmp->ProcessTime.hour = tmp->finish.hour = 0;
tmp->JobTime.min = tmp->ProcessTime.min = tmp->finish.min = 0;
tmp->JobWait = tmp->ProcessWait = tmp->zz = 0;
tmp->zzxs = tmp->response = 0;
tmp->finished = 0;
tmp->next = NULL;
cc = insert(cc, tmp);
}
return cc;
}
//函数名:FCFS 参数:Queue *cc
void FCFS(Queue *cc){
//功能:实现先来先服务算法
node *p = cc->front;
int curTime = p->arrive.hour * 60 + p->arrive.min;
while (p) {
if(curTime < p->arrive.hour * 60 + p->arrive.min){
p->JobTime.hour = p->arrive.hour;
p->JobTime.min = p->arrive.min;
}
else{
p->JobTime.hour = curTime / 60;
p->JobTime.min = curTime % 60;
}
p->JobWait = p->JobTime.hour * 60 + p->JobTime.min - (p->arrive.hour * 60 + p->arrive.min);
p->ProcessTime.hour = p->JobTime.hour;
p->ProcessTime.min = p->JobTime.min;
p->ProcessWait = p->ProcessTime.hour * 60 + p->ProcessTime.min - (p->arrive.hour * 60 + p->arrive.min);
curTime = p->ProcessTime.hour * 60 + p->ProcessTime.min + p->zx;
p->finish.hour = curTime / 60;
p->finish.min = curTime % 60;
p->zz = curTime - (p->arrive.hour * 60 + p->arrive.min);
p->zzxs = static_cast<float>(p->zz) / p->zx;
p->finished = 1;
p = p->next;
}
}
//函数名:SJF 参数:Queue *cc
void SJF(Queue *cc){
//功能:实现短作业优先调度算法
Queue* sj;
sj = init();
sj = insert(sj, cc->front);
cc = dele(cc);
node *p = cc->front;
while (p) {
node *minNode = p;
node *q = p->next;
while (q) {
if (q->zx < minNode->zx)
minNode = q;
q = q->next;
}
if (p != minNode) {
swap(p->name, minNode->name);
swap(p->arrive, minNode->arrive);
swap(p->zx, minNode->zx);
}
p = p->next;
}
sj->front->next = cc->front;
sj->rear = cc->rear;
FCFS(sj);
output(sj);
}
//函数名:HRRF 参数:Queue *cc
void HRRF(Queue *cc){
//功能:实现最高响应比优先算法
node *p = cc->front;
int curTime = p->arrive.hour * 60 + p->arrive.min;
while (p) {
float maxResponse = 0;
node* maxNode = NULL;
node* q = p;
while (q) {
int waitTime = curTime - (q->arrive.hour * 60 + q->arrive.min);
float response = static_cast<float>(waitTime + q->zx) / q->zx;
if (response > maxResponse) {
maxResponse = response;
maxNode = q;
}
q = q->next;
}
if(q != maxNode){
swap(p->name, maxNode->name);
swap(p->arrive, maxNode->arrive);
swap(p->zx, maxNode->zx);
}
maxNode = p;
if(curTime < maxNode->arrive.hour * 60 + maxNode->arrive.min){
maxNode->JobTime.hour = maxNode->arrive.hour;
maxNode->JobTime.min = maxNode->arrive.min;
}
else{
maxNode->JobTime.hour = curTime / 60;
maxNode->JobTime.min = curTime % 60;
}
maxNode->JobWait = maxNode->JobTime.hour * 60 + maxNode->JobTime.min - (maxNode->arrive.hour * 60 + maxNode->arrive.min);
maxNode->ProcessTime.hour = maxNode->JobTime.hour;
maxNode->ProcessTime.min = maxNode->JobTime.min;
maxNode->ProcessWait = maxNode->ProcessTime.hour * 60 + maxNode->ProcessTime.min - (maxNode->arrive.hour * 60 + maxNode->arrive.min);
curTime = maxNode->ProcessTime.hour * 60 + maxNode->ProcessTime.min + maxNode->zx;
maxNode->finish.hour = curTime / 60;
maxNode->finish.min = curTime % 60;
maxNode->zz = curTime - (maxNode->arrive.hour * 60 + maxNode->arrive.min);
maxNode->zzxs = static_cast<float>(maxNode->zz) / maxNode->zx;
maxNode->finished = 1;
p = p->next;
}
output(cc);
}
int main(){
Queue *me;
int x,y;
printf("请输入操作(1:开始进程调度;0:结束进程):");
scanf("%d",&x);
while(x){
me=init();
me=input(me);
menu();
scanf("%d",&y);
switch(y){
case 1: FCFS(me);output(me);break;
case 2: SJF(me);break;
case 3: HRRF(me);break;
case 0: printf("操作结束!");exit(1);
default: printf("输入操作号错误!\n");break;
}
printf("\n请输入操作(1:开始进程调度;0:结束进程):");
scanf("%d",&x);
}
printf("操作结束!");
}
/*
1
4
JOB2 8:50 50
JOB1 8:00 120
JOB4 9:50 20
JOB3 9:00 10
1
1
4
JOB2 8:50 50
JOB1 8:00 120
JOB4 9:50 20
JOB3 9:00 10
2
1
4
JOB2 8:50 50
JOB1 8:00 120
JOB4 9:50 20
JOB3 9:00 10
3
*/