当前位置:首页 » 编程语言 » 短作业优先算法c语言

短作业优先算法c语言

发布时间: 2022-10-04 02:49:30

‘壹’ 求进程调度先来先服务算法,短进程优先算法完整c语言代码

/*(一)进程调度

进程调度算法有FIFO,优先数调度算法,时间片轮转调度算法,分级调度算法,

输入:进程流文件,其中存储的是一系列要执行的进程,
每个作业包括三个数据项:
进程名 所需时间 优先数(0级最高)
输出:
进程执行流 等待时间 平均等待时间

本程序包括:FIFO,优先数调度算法,时间片轮转调度算法

进程流文件process_stream.txt
测试数据:
p0 16 2
p1 5 1
p2 4 3
p3 8 0
p4 9 4
p5 7 6

VC++调试通过
*/
#include <stdio.h>
#include <string.h>
#include <iostream.h>
#include <stdlib.h>

const int Quatum=2;//定义时间片的长度为2秒
const int MAXPCB=100;//定义最大进程数

//定义进程结构体
typedef struct node
{
char name[20];//进程名
int time; //进程运行时间
int privilege;//进程优先级(静态)
int finished;//进程完成标志,0-未完成,1-已完成
int wait_time;//进程等待时间
}pcb;

pcb pcbs[MAXPCB];
int quantiry;//进程流文件中的进程总数

void initial()
{
int i;
for (i=0;i<MAXPCB;i++)
{
strcpy(pcbs[i].name,"");
pcbs[i].time=0;
pcbs[i].privilege=0;
pcbs[i].finished=0;
pcbs[i].wait_time=0;
}
quantiry=0;
}

int readData()
{
FILE *fp;
char fname[20];
int i;
cout<<"请输入进程流文件名:"<<endl;
cin>>fname;
if ((fp=fopen(fname,"r"))==NULL)
{
cout<<"错误,文件打不开,请检查文件名"<<endl;
}
else
{
while (!feof(fp))
{
fscanf(fp,"%s %d %d %d",pcbs[quantiry].name,
&pcbs[quantiry].time,&pcbs[quantiry].privilege);
quantiry++;
}
//输出所读入得数据
cout<<"输出所读入的数据"<<endl;
cout<<"进程流文件中的进程总数="<<quantiry<<endl;
cout<<"进程名 所需时间 优先数"<<endl;

for (i=0;i<quantiry;i++)
{
cout<<" "<<pcbs[i].name<<" "<<pcbs[i].time<<" "<<pcbs[i].privilege<<endl;
}

return 1;
}

return 0;
}

//重置数据,以供另一个算法使用
void init()
{
int i;
for (i=0;i<MAXPCB;i++)
{
pcbs[i].finished=0;
pcbs[i].wait_time=0;
}
}

void FIFO()
{
int i,j;

int total;
//输出FIFO算法执行流
cout<<endl<<"---------------------------------------------------------------"<<endl;
cout<<"FIFO算法执行流:"<<endl;
cout<<"进程名 等待时间"<<endl;

for (i=0;i<quantiry;i++)
{
cout<<" "<<pcbs[i].name<<" "<<pcbs[i].wait_time<<endl;
for (j=i+1;j<quantiry;j++)
{
pcbs[j].wait_time+=pcbs[i].time;
}
}

total=0;
for (i=0;i<quantiry;i++)
{
total+=pcbs[i].wait_time;
}
cout<<"总等待时间:"<<total<<" "<<"平均等待时间:"<<total/quantiry<<endl;
}

//优先度调度算法
void privilege()
{
int i,j,p;
int passed_time=0;
int total;

int queue[MAXPCB];
int current_privielege=1000;

for (i=0;i<quantiry;i++)
{
current_privielege=1000;
for (j=0;j<quantiry;j++)
{
if ((pcbs[j].finished==0)&&(pcbs[j].privilege<current_privielege))
{
p=j;
current_privielege=pcbs[j].privilege;
}
}
queue[i]=p;
pcbs[p].finished=1;
pcbs[p].wait_time+=passed_time;
passed_time+=pcbs[p].time;

}
//输出优先数调度执行流
cout<<endl<<"-----------------------------------------"<<endl;
cout<<"优先数调度执行流:"<<endl;
cout<<"进程名 等待时间"<<endl;

for (i=0;i<quantiry;i++)
{
cout<<" "<<pcbs[queue[i]].name<<" "<<pcbs[queue[i]].wait_time<<"--"<<queue[i]<<endl;
}

total=0;
for (i=0;i<quantiry;i++)
{
total+=pcbs[i].wait_time;
}

cout<<"总等待时间:"<<total<<" 平均等待时间:"<<total/quantiry<<endl;
}

//时间片轮转调度算法
void timer()
{
int i,j,sum,flag=1;
int passed_time=0;
int max_time=0;
int round=0;

int queue[1000];
int total=0;

while(flag==1)
{
flag=0;
for (i=0;i<quantiry;i++)
{
if (pcbs[i].finished==0)
{
flag=1;
queue[total]=i;
total++;
if (pcbs[i].time<=Quatum*(round+1))
pcbs[i].finished=1;
}
}
round++;
}

cout<<endl<<"---------------------------------------------------------------"<<endl;
cout<<"时间片轮转调度执行流:";
for(i=0;i<total;i++)
{
cout<<pcbs[queue[i]].name<<" ";
}
cout<<endl;
cout<<"进程名 结束时间 运行时间 等待时间"<<endl;

sum=0;

for (i=0;i<quantiry;i++)
{
for(j=total-1;j>=0;j--)//从轮转调度执行流序列由后往前比较,找到同名进程即可计算其完成时间
{
if (strcmp(pcbs[queue[j]].name,pcbs[i].name)==0)
{
cout<<" "<<pcbs[i].name<<" "<<(j+1)*Quatum<<" ";
cout<<pcbs[i].time<<" "<<(j+1)*Quatum-pcbs[i].time<<endl;
sum+=(j+1)*Quatum-pcbs[i].time;
break;
}
}
}

cout<<"总等待时间:"<<sum<<" "<<"平均等待时间:"<<sum/quantiry<<endl;
}

//显示版权信息函数
void version()
{
cout<<endl<<endl;

cout<<" ┏━━━━━━━━━━━━━━━━━━━━━━━┓"<<endl;
cout<<" ┃ 进程调度模拟系统 ┃"<<endl;
cout<<" ┠───────────────────────┨"<<endl;
cout<<" ┃ version 2011 ┃"<<endl;
cout<<" ┗━━━━━━━━━━━━━━━━━━━━━━━┛"<<endl;
cout<<endl<<endl;
}
//主函数

int main()
{
int flag;
version();
initial();
flag=readData();
if(flag==1){
FIFO();
init();
privilege();
init();
timer();
}
cout<<endl;
system("pause");
return 0;
}

‘贰’ 短作业优先算法用c语言如何写

这样写应该可以:
#include<iostream.h>

#include<stdio.h>
struct pcb{
char pno;
int come_time; //到达时间
int run_time; //服务时间
};
float fcfs(pcb pro[],int n)
{
struct pcb temp;
int i,j,k; //time为当前时间
float weight_time=0,time=0; //记录周转时间的和
//temp=(pcb)malloc(sizeof(pcb));
cout<<"进程调度情况如下:"<<endl;
cout<<"进程号 到达时间 服务时间 周转时间:"<<endl;
//选择排序过程,按到达时间升序排列
for(i=0;i<n-1;i++)
{
k=i;
for(j=i+1;j<n;j++)
if(pro[k].come_time>pro[j].come_time)
k=j;
if(k!=i)
{
temp=pro[i];
pro[i]=pro[k];
pro[k]=temp;
}
}
for(i=0;i<n;i++)
{ time+=pro[i].run_time;
weight_time+=(time-pro[i].come_time)/pro[i].run_time; //(time-pro[i].come_time)/pro[i].run_time为排序后第i个进程的周转时间
cout<<pro[i].pno<<" "<<pro[i].come_time<<" "<<pro[i].run_time<<" "<<(time-pro[i].come_time)/pro[i].run_time<<endl;
}
return weight_time/=n; //返回平均带权周转时间
}
void insert(pcb pro[],pcb pro1,int start,int end)//将一pcb类型的元素插入到有序数组中,最后还保持有序
{
int i=end;
while((i--)>start)
if(pro[i].run_time>pro1.run_time)pro[i+1]=pro[i];
pro[i]=pro1;

}
float sjp(pcb pro[],int n)
{
int i,first=0,count,flag[20],k,min;
float time=0,weight_time=0;
//调度第一个到达内存的进程
for(i=1;i<n;i++)
{
if(pro[first].come_time>pro[i].come_time) first=i;
flag[i]=0;
}
flag[first]=1;
time=(float)pro[first].run_time;
weight_time=1;
cout<<pro[first].pno<<" "<<pro[first].come_time<<" "<<pro[first].run_time<<" "<<weight_time<<endl;
//pro_temp[0]=pro[first];
count=n-1;
while(count)
{
k=0;
min=32767; //设置一个较大的阈值,
for(i=0;i<n;i++) //找到一个未被访问的,作业较短的且已经到达内存的作业调度
if((i!=first)&&(flag[i]==0)&&(time>=pro[i].come_time)&&(min>pro[i].run_time))
{
k=i;
min=pro[i].run_time;

}
flag[k]=1; //访问后置标记为访问
time+=pro[k].run_time;
weight_time+=(time-pro[k].come_time)/pro[k].run_time;
cout<<pro[k].pno<<" "<<pro[k].come_time<<" "<<pro[k].run_time<<" "<<(time-pro[k].come_time)/pro[k].run_time<<endl;
count--; //每调度一个作业,count减1
}
return weight_time/=n;
}

void main()
{
pcb pro[5]={{'C',2,5},{'A',0,4},{'B',1,3},{'D',3,2},{'E',4,4}};
cout<<fcfs(pro,5)<<endl;
cout<<sjp(pro,5)<<endl;
}

‘叁’ 用c语言实现先到先处理和最短路径优先的cpu调度算法

#include <stdio.h>
#define n 20
struct fcfs
{
int id;
int atime;
int runtime;
int ftime;
}f[n];
zcx(){
int xz;
int amount;
printf("**************分割线*********************\n");
printf("1.先来先服务\n");
printf("2.优先级\n");
printf("请输入你的选择:");
scanf("%d",&xz);
printf("\n\n");
if(xz==1)
{
printf("你的选择是先来先服务\n");
printf("请输入进程数:");
scanf("%d",&amount);
yihao(amount);

}
else
{
printf("你的选择是优先级\n");
printf("请输入进程数:");
scanf("%d",&amount);
erhao(amount);

}
}
yihao(int amount)
{
int i,j,l,k;
struct fcfs f[n];

printf("\n\n");
for(i=0;i<amount;i++)
{
printf("请输入第 %d 个程序的信息\n",i+1);
printf("进程名 :");
scanf("%d",&f[i].id);
printf("到达时间:");
scanf("%d",&f[i].atime);
printf("运行时间:");
scanf("%d",&f[i].runtime);

}
for(i=0;i<amount;i++)
{
for(j=0;j<amount-i-1;j++)
{
if(f[j].atime>f[j+1].atime)
{
l=f[j].atime;
f[j].atime=f[j+1].atime;
f[j+1].atime=l;

k=f[j].id;
f[j].id=f[j+1].id;
f[j+1].id=k;
}
}
}

printf("进程名 开始时间 运行时间 结束时间 \n");
for(i=0;i<amount;i++)
{
f[i].ftime=f[i].atime+f[i].runtime;

printf("%d %d %d %d\n",f[i].id,f[i].atime,f[i].runtime,f[i].ftime);
f[i+1].atime=f[i].ftime;
}

zcx();

}
erhao(int amount)
{
int i,j,l,k;
struct fcfs f[n];

printf("\n\n");
for(i=0;i<amount;i++)
{
printf("请输入第 %d 个程序的信息\n",i+1);
printf("进程名 :");
scanf("%d",&f[i].id);
printf("优先级 :");
scanf("%d",&f[i].atime);
printf("运行时间:");
scanf("%d",&f[i].runtime);

}
for(i=0;i<amount;i++)
{
for(j=0;j<amount-i-1;j++)
{
if(f[j].atime>f[j+1].atime)
{
l=f[j].atime;
f[j].atime=f[j+1].atime;
f[j+1].atime=l;

k=f[j].id;
f[j].id=f[j+1].id;
f[j+1].id=k;
}
}
}
printf("进程名 优先级 工作时间 \n");
for(i=0;i<amount;i++)
{
f[i].ftime=f[i].atime+f[i].runtime;

printf("%d %d %d \n",f[i].id,f[i].atime,f[i].runtime);
f[i+1].ftime=f[i].ftime+f[i+1].atime;
}

zcx();

}

void main()
{

zcx();

}

这是操作系统的作业吧

‘肆’ 短作业优先调度算法sjf 写一下具体的运算过程,谢谢

短作业优先(SJF, Shortest Job First)又称为“短进程优先”SPN(Shortest Process Next);这是对FCFS算法的改进,其目标是减少平均周转时间。
定义
对预计执行时间短的作业(进程)优先分派处理机。通常后来的短作业不抢先正在执行的作业。

‘伍’ 用C语言编写一段简单的程序,作业调度和低级调度算法

真不容易啊,怕是没人弄了!
优先级调度算法程序:
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
typedef struct node
{
char name[10]; /*进程标识符*/
int prio; /*进程优先数*/
int round; /*进程时间轮转时间片*/
int cputime; /*进程占用CPU时间*/
int needtime; /*进程到完成还要的时间*/
int count; /*计数器*/
char state; /*进程的状态*/
struct node *next; /*链指针*/
}PCB;
PCB *finish,*ready,*tail,*run; /*队列指针*/
int N; /*进程数*/
/*将就绪队列中的第一个进程投入运行*/
firstin()
{
run=ready; /*就绪队列头指针赋值给运行头指针*/
run->state='R'; /*进程状态变为运行态*/
ready=ready->next; /*就绪对列头指针后移到下一进程*/
}
/*标题输出函数*/
void prt1(char a)
{
if(toupper(a)=='P') /*优先数法*/
printf(" name cputime needtime priority state\n");
else
printf(" name cputime needtime count round state\n");
}
/*进程PCB输出*/
void prt2(char a,PCB *q)
{
if(toupper(a)=='P') /*优先数法的输出*/
printf(" %-10s%-10d%-10d%-10d %c\n",q->name,
q->cputime,q->needtime,q->prio,q->state);
else/*轮转法的输出*/
printf(" %-10s%-10d%-10d%-10d%-10d %-c\n",q->name,
q->cputime,q->needtime,q->count,q->round,q->state);
}
/*输出函数*/
void prt(char algo)
{
PCB *p;
prt1(algo); /*输出标题*/
if(run!=NULL) /*如果运行指针不空*/
prt2(algo,run); /*输出当前正在运行的PCB*/
p=ready; /*输出就绪队列PCB*/
while(p!=NULL)
{
prt2(algo,p);
p=p->next;
}
p=finish; /*输出完成队列的PCB*/
while(p!=NULL)
{
prt2(algo,p);
p=p->next;
}
getch(); /*压任意键继续*/
}
/*优先数的插入算法*/
insert1(PCB *q)
{
PCB *p1,*s,*r;
int b;
s=q; /*待插入的PCB指针*/
p1=ready; /*就绪队列头指针*/
r=p1; /*r做p1的前驱指针*/
b=1;
while((p1!=NULL)&&b) /*根据优先数确定插入位置*/
if(p1->prio>=s->prio)
{
r=p1;
p1=p1->next;
}
else
b=0;
if(r!=p1) /*如果条件成立说明插入在r与p1之间*/
{
r->next=s;
s->next=p1;
}
else
{
s->next=p1; /*否则插入在就绪队列的头*/
ready=s;
}
}
/*轮转法插入函数*/
insert2(PCB *p2)
{
tail->next=p2; /*将新的PCB插入在当前就绪队列的尾*/
tail=p2;
p2->next=NULL;
}
/*优先数创建初始PCB信息*/
void create1(char alg)
{
PCB *p;
int i,time;
char na[10];
ready=NULL; /*就绪队列头指针*/
finish=NULL; /*完成队列头指针*/
run=NULL; /*运行队列指针*/
printf("Enter name and time of process\n"); /*输入进程标识和所需时间创建PCB*/
for(i=1;i<=N;i++)
{
p=malloc(sizeof(PCB));
scanf("%s",na);
scanf("%d",&time);
strcpy(p->name,na);
p->cputime=0;
p->needtime=time;
p->state='w';
p->prio=50-time;
if(ready!=NULL) /*就绪队列不空调用插入函数插入*/
insert1(p);
else
{
p->next=ready; /*创建就绪队列的第一个PCB*/
ready=p;
}
}
clrscr();
printf(" output of priority:\n");
printf("************************************************\n");
prt(alg); /*输出进程PCB信息*/
run=ready; /*将就绪队列的第一个进程投入运行*/
ready=ready->next;
run->state='R';
}
/*轮转法创建进程PCB*/
void create2(char alg)
{
PCB *p;
int i,time;
char na[10];
ready=NULL;
finish=NULL;
run=NULL;
printf("Enter name and time of round process\n");
for(i=1;i<=N;i++)
{
p=malloc(sizeof(PCB));
scanf("%s",na);
scanf("%d",&time);
strcpy(p->name,na);
p->cputime=0;
p->needtime=time;
p->count=0; /*计数器*/
p->state='w';
p->round=2; /*时间片*/
if(ready!=NULL)
insert2(p);
else
{
p->next=ready;
ready=p;
tail=p;
}
}
clrscr();
printf(" output of round\n");
printf("************************************************\n");
prt(alg); /*输出进程PCB信息*/
run=ready; /*将就绪队列的第一个进程投入运行*/
ready=ready->next;
run->state='R';
}
/*优先数调度算法*/
priority(char alg)
{
while(run!=NULL) /*当运行队列不空时,有进程正在运行*/
{
run->cputime=run->cputime+1;
run->needtime=run->needtime-1;
run->prio=run->prio-3; /*每运行一次优先数降低3个单位*/
if(run->needtime==0) /*如所需时间为0将其插入完成队列*/
{
run->next=finish;
finish=run;
run->state='F'; /*置状态为完成态*/
run=NULL; /*运行队列头指针为空*/
if(ready!=NULL) /*如就绪队列不空*/
firstin(); /*将就绪对列的第一个进程投入运行*/
}
else /*没有运行完同时优先数不是最大,则将其变为就绪态插入到就绪队列*/
if((ready!=NULL)&&(run->prio<ready->prio))
{
run->state='W';
insert1(run);
firstin(); /*将就绪队列的第一个进程投入运行*/
}
prt(alg); /*输出进程PCB信息*/
}
}
/*时间片轮转法*/
roundrun(char alg)
{
while(run!=NULL)
{
run->cputime=run->cputime+1;
run->needtime=run->needtime-1;
run->count=run->count+1;
if(run->needtime==0)/*运行完将其变为完成态,插入完成队列*/
{
run->next=finish;
finish=run;
run->state='F';
run=NULL;
if(ready!=NULL)
firstin(); /*就绪对列不空,将第一个进程投入运行*/
}
else
if(run->count==run->round) /*如果时间片到*/
{
run->count=0; /*计数器置0*/
if(ready!=NULL) /*如就绪队列不空*/
{
run->state='W'; /*将进程插入到就绪队列中等待轮转*/
insert2(run);
firstin(); /*将就绪对列的第一个进程投入运行*/
}
}
prt(alg); /*输出进程信息*/
}
}
/*主函数*/
main()
{
char algo; /*算法标记*/
clrscr();
printf("type the algorithm:P/R(priority/roundrobin)\n");
scanf("%c",&algo); /*输入字符确定算法*/
printf("Enter process number\n");
scanf("%d",&N); /*输入进程数*/
if(algo=='P'||algo=='p')
{
create1(algo); /*优先数法*/
priority(algo);
}
else
if(algo=='R'||algo=='r')
{
create2(algo); /*轮转法*/
roundrun(algo);
}
}

‘陆’ 急求 程序代码 c/c++ 操作系统中的 处理机调度算法

#include <iostream>

#include <stdio.h>

#include <string>

//#include <windows.h>

using namespace std;

//hyugtyftydrtdtrdrrtrdrt

struct Node

{

string name;//进程(作业)名称

int arriveTime;//到达时间

int ServerTime;//服务时间

int leftTime;//the left time

Node *link;//指向下一个节点的指针

};

class CProcess

{

public:

CProcess();//构造函数

~CProcess();//析构函数

const CProcess &operator =(const CProcess& p);//重载赋值操作符

void insertNode(string &na,int& at,int& st);//插入新元素(at由小到大)到链表合适的位置

void sort();//按照服务时间由大到小排序

bool isEmpty();//判断是否为空

void destroy();//销毁

int length();//求出链表长度

void print();//打印出元素

void FCFS();//先到先服务

void SJF();//短进程(作业)优先

void RR(int& q);//时间片轮转

void priority();//优先权调度

protected:

Node *first;

Node *last;

};

const CProcess& CProcess::operator=(const CProcess& p)

{

Node *newNode;

Node *Current;

if(this!=&p)//避免自己给自己赋值

{

if(first!=NULL)//如果链表不为空

destroy();

if(p.first==NULL)

{//如果要拷贝的对象为空

this->first = NULL;

this->last = NULL;

}

else

{

Current = p.first;

first= new Node;

first->name=Current->name;//

first->arriveTime=Current->arriveTime;

first->ServerTime=Current->ServerTime;

first->link =NULL;

last =first;

Current = Current->link;

while(Current!=NULL)

{

newNode = new Node;

newNode->name=Current->name;

newNode->arriveTime=Current->arriveTime;

newNode->ServerTime=Current->ServerTime;

newNode->link=NULL;

last->link=newNode;

last=newNode;

Current = Current->link;

}

}

}

return *this;

}

CProcess::CProcess()

{//构造函数

first=NULL;

last=NULL;

}

CProcess::~CProcess()

{

Node *temp;

while(first!=NULL)

{

temp=first;

first=first->link;

delete temp;

}

last=NULL;

}

void CProcess::insertNode(string &na,int& at,int& st)

{//按照到达时间升序排序

Node *Current;

Node *trailCurrent;//指向Current的前一个节点

Node *newNode;

bool found;

newNode = new Node;//建立一个新节点

newNode->name=na;

newNode->arriveTime=at;

newNode->ServerTime=st;

newNode->link=NULL;//

if(first==NULL)//如果第一个节点为空(如果是第一次插入元素)

first=newNode;//将新节点赋给第一个节点

else

{//如果不是第一次

Current =first;

found = false;

while(Current!=NULL && !found)

{

if(Current->arriveTime >= at)

found = true;

else

{

trailCurrent = Current;

Current = Current->link;

}

}

if(Current==first)

{

newNode->link = first;

first = newNode;

}

else

{

trailCurrent->link = newNode;

newNode->link = Current;

}

}

}

int CProcess::length()

{

int count =0;//声明变量,并初始化为0(用来记录长度)

Node *Current;

Current = first;

while(Current!=NULL)//当前节点不为空,记录值自加,一直向后遍历,

{

count++;

Current = Current->link;

}

return count;//返回长度

}

void CProcess::sort()//按照服务时间,升序排列

{//冒泡排序

string sname;

int at;

int st;

Node *Current;//指向当前节点

Node *trailCurrent;//指向当前节点的前一个节点

for(trailCurrent=first->link;trailCurrent!=NULL;trailCurrent=trailCurrent->link)//控制条件有问题

{

for(Current=trailCurrent->link;Current!=NULL;Current=Current->link)//控制条件有问题

{

if(trailCurrent->ServerTime > Current->ServerTime)

{

sname=trailCurrent->name;

at=trailCurrent->arriveTime;

st=trailCurrent->ServerTime;

trailCurrent->name=Current->name;

trailCurrent->arriveTime=Current->arriveTime;

trailCurrent->ServerTime=Current->ServerTime;

Current->name=sname;

Current->arriveTime=at;

Current->ServerTime=st;

}

}

}

}

bool CProcess::isEmpty()//判断是否为空

{

return (first==NULL);//如果第一个节点为空,返回值

}

void CProcess::print()

{

Node *Current;

Current = first->link;//头节点赋给当前节点

while(Current!=NULL)//当前节点不为空,一直向后遍历打印

{

cout<<Current->name<<" ";

cout<<Current->arriveTime<<" ";

cout<<Current->ServerTime<<"\n";

Current = Current->link;

}

}

void CProcess::destroy()

{

Node *temp;//定义一个临时指针变量

while(first!=NULL)

{

temp=first;

first=first->link;

delete temp;

}

last=NULL;

}

void CProcess::FCFS()//先到先服务

{

Node *Current;

int T0=0;//完成时间

int T1=0;//周转时间

Current = first->link;//头节点赋给当前节点

while(Current!=NULL)

{

if(T0 < Current->arriveTime)

{

T0=Current->arriveTime+Current->ServerTime;

T1=T0-Current->arriveTime;

cout<<Current->name<<"\t";//打印出进程名

cout<<T0<<"\t";//打印出完成时间

cout<<T1<<"\n";//打印出周转时间

Current = Current->link;

}

else

{

T0=Current->ServerTime+T0;

T1=T0-Current->arriveTime;//周转时间等于,完成时间 - 到达时间

cout<<Current->name<<"\t";//打印出进程名

cout<<T0<<"\t";//打印出完成时间

cout<<T1<<"\n";//打印出周转时间

Current = Current->link;

}

}

}

void CProcess::SJF()//短进程(作业)优先

{

//首先执行第一个到达的作业

Node *Current;

int T0=0;//完成时间

int T1=0;//周转时间

T0=first->link->ServerTime+T0;

T1=T0-first->link->arriveTime;

cout<<first->link->name<<"\t";

cout<<T0<<"\t";//打印出完成时间

cout<<T1<<"\n";//打印出周转时间

first->link=first->link->link;//删除

//执行剩下的

sort();//对剩下的排序

Current = first->link;//头节点赋给当前节点

while(Current!=NULL)

{

if(T0 < Current->arriveTime)

{

T0=Current->arriveTime+Current->ServerTime;

T1=T0-Current->arriveTime;

cout<<Current->name<<"\t";//打印出进程名

cout<<T0<<"\t";//打印出完成时间

cout<<T1<<"\n";//打印出周转时间

Current = Current->link;

}

else

{

T0=Current->ServerTime+T0;

T1=T0-Current->arriveTime;//周转时间等于,完成时间 - 到达时间

cout<<Current->name<<"\t";//打印出进程名

cout<<T0<<"\t";//打印出完成时间

cout<<T1<<"\n";//打印出周转时间

Current = Current->link;

}

}

}

void CProcess::RR(int& q)//时间片轮转

{

cout<<"时间片轮转操作完成!\n";

}

void CProcess::priority()//优先权调度

{

cout<<"优先权操作完成!\n";

}

void main()

{

CProcess p0,p1,p2,p3,p4;

int at,st;

string na;

int judge=1;//控制退出程序

int choice;//控制选择操作

while(judge)

{

cout<<"********************************************************\n";

cout<<"****** 说明:本程序适用于单道进程(作业) ******\n";

cout<<"******** 请选择您的操作 ***************\n";

cout<<"*********输入相应的数字,按下(Enter)键!**************\n";

cout<<"************* 5.录入信息 ************\n";

cout<<"************* 1.先到先服务 ************\n";

cout<<"************* 2.短进程(作业)优先 ************\n";

cout<<"************* 3.时间片轮转 ************\n";

cout<<"************* 4.优先权(静态)调度 ************\n";

cout<<"************* 0.退出程序 ************\n";

cout<<"********************************************************\n";

cin>>choice;

switch(choice)

{

case 0:

judge=0;

break;

case 5:

cout<<"请输入信息以“end”结束输入!\n";

cout<<"进程名 到达时间 服务时间"<<endl;

while(na.compare("end"))//如果相等则会返回0

{

p0.insertNode(na,at,st);

cin>>na>>at>>st;

}

cout<<"录入成功,目前的信息为:\n";

cout<<"进程名 到达时间 服务时间"<<endl;

p0.print();

break;

case 1://先到先服务

p1=p0;//拷贝一份

if(p1.isEmpty())

{

cout<<"请先录入信息\n";

break;

}

else

{

cout<<"先到先服务\n";

cout<<"进程名 完成时间 周转时间\n";

p1.FCFS();

break;

}

case 2://短作业优先

p2=p0;//拷贝一份

//p2.sort();

//p2.print();

if(p2.isEmpty())

{

cout<<"请先录入信息\n";

break;

}

else

{

cout<<"短作业优先\n";

cout<<"进程名 完成时间 周转时间\n";

p2.SJF();

break;

}

case 3://时间片轮转

p3=p0;//拷贝一份

int q;

if(p3.isEmpty())

{

cout<<"请先录入信息\n";

break;

}

else

{

cout<<"请输入时间片大小";

cin>>q;

cout<<"时间片轮转\n";

cout<<"进程名 完成时间 周转时间\n";

p3.RR(q);

break;

}

case 4://优先权

p4=p0;//拷贝一份

if(p4.isEmpty())

{

cout<<"请先录入信息\n";

break;

}

else

{

cout<<"时间片轮转\n";

cout<<"进程名 完成时间 周转时间\n";

p4.priority();

break;

}

default:

cout<<"请选择目录中的选项!\n";

break;

}

}

return;

}

‘柒’ 作业调度算法的短作业优先法

短作业优先(SJF, Shortest Job First)又称为“短进程优先”SPN(Shortest Process Next);这是对FCFS算法的改进,其目标是减少平均周转时间。 (1) 优点:
比FCFS改善平均周转时间和平均带权周转时间,缩短作业的等待时间;
提高系统的吞吐量;
(2) 缺点:
对长作业非常不利,可能长时间得不到执行;
未能依据作业的紧迫程度来划分执行的优先级;
难以准确估计作业(进程)的执行时间,从而影响调度性能。 “最短剩余时间优先”SRT(Shortest Remaining Time)(允许比当前进程剩余时间更短的进程来抢占)
“最高响应比优先”HRRN(Highest Response Ratio Next)(响应比R = (等待时间 + 要求执行时间) / 要求执行时间,是FCFS和SJF的折衷)

‘捌’ 用C语言实现短作业优先的算法

t=t+b[k];
这句运行出错
应该我不懂你要达到什么效果
所以没办法修改
请谅解
还有
你这个程序里面
int
的和float的在相加
你得注意
两个最好转换成相同的然后在相加
怎么转换
转换成那个你自己拿主意

‘玖’ 先来先服务调度算法。 优先级调度算法。 短作业优先调度算法 轮转调度算法 响应比高优先调度算法

你试一下

#include<stdio.h>
//using namespace std;
#define MAX 10
struct task_struct
{
char name[10]; /*进程名称*/
int number; /*进程编号*/
float come_time; /*到达时间*/
float run_begin_time; /*开始运行时间*/
float run_time; /*运行时间*/
float run_end_time; /*运行结束时间*/
int priority; /*优先级*/
int order; /*运行次序*/
int run_flag; /*调度标志*/
}tasks[MAX];
int counter; /*实际进程个数*/
int fcfs(); /*先来先服务*/
int ps(); /*优先级调度*/
int sjf(); /*短作业优先*/
int hrrn(); /*响应比高优先*/
int pinput(); /*进程参数输入*/
int poutput(); /*调度结果输出*/

void main()
{ int option;
pinput();
printf("请选择调度算法(0~4):\n");
printf("1.先来先服务\n");
printf("2.优先级调度\n");
printf(" 3.短作业优先\n");
printf(" 4.响应比高优先\n");
printf(" 0.退出\n");
scanf("%d",&option);
switch (option)
{case 0:
printf("运行结束。\n");
break;
case 1:
printf("对进程按先来先服务调度。\n\n");
fcfs();
poutput();
break;
case 2:
printf("对进程按优先级调度。\n\n");
ps();
poutput();
break;
case 3:
printf("对进程按短作业优先调度。\n\n");
sjf();
poutput();
break;
case 4:
printf("对进程按响应比高优先调度。\n\n");
hrrn();
poutput();
break;
}
}
int fcfs() /*先来先服务*/
{
float time_temp=0;
inti;
intnumber_schel;
time_temp=tasks[0].come_time;
for(i=0;i<counter;i++)
{
tasks[i].run_begin_time=time_temp;
tasks[i].run_end_time=tasks[i].run_begin_time+tasks[i].run_time;
tasks[i].run_flag=1;
time_temp=tasks[i].run_end_time;
number_schel=i;
tasks[number_schel].order=i+1;
}
return 0;
}

int ps() /*优先级调度*/
{
float temp_time=0;
inti=0,j;
intnumber_schel,temp_counter;
intmax_priority;
max_priority=tasks[i].priority;
j=1;
while((j<counter)&&(tasks[i].come_time==tasks[j].come_time))
{
if (tasks[j].priority>tasks[i].priority)
{
max_priority=tasks[j].priority;
i=j;
}
j++;
} /*查找第一个被调度的进程*/
/*对第一个被调度的进程求相应的参数*/
number_schel=i;
tasks[number_schel].run_begin_time=tasks[number_schel].come_time;
tasks[number_schel].run_end_time=tasks[number_schel].run_begin_time+tasks[number_schel].run_time;
tasks[number_schel].run_flag=1;
temp_time=tasks[number_schel].run_end_time;
tasks[number_schel].order=1;
temp_counter=1;
while (temp_counter<counter)
{
max_priority=0;
for(j=0;j<counter;j++)
{if((tasks[j].come_time<=temp_time)&&(!tasks[j].run_flag))
if (tasks[j].priority>max_priority)
{
max_priority=tasks[j].priority;
number_schel=j;
}
} /*查找下一个被调度的进程*/
/*对找到的下一个被调度的进程求相应的参数*/
tasks[number_schel].run_begin_time=temp_time;
tasks[number_schel].run_end_time=tasks[number_schel].run_begin_time+tasks[number_schel].run_time;
tasks[number_schel].run_flag=1;
temp_time=tasks[number_schel].run_end_time;
temp_counter++;
tasks[number_schel].order=temp_counter;

}return 0;
}

int sjf() /*短作业优先*/
{
float temp_time=0;
inti=0,j;
intnumber_schel,temp_counter;
float run_time;
run_time=tasks[i].run_time;
j=1;
while((j<counter)&&(tasks[i].come_time==tasks[j].come_time))
{
if (tasks[j].run_time<tasks[i].run_time)
{
run_time=tasks[j].run_time;
i=j;
}
j++;
} /*查找第一个被调度的进程*/
/*对第一个被调度的进程求相应的参数*/
number_schel=i;
tasks[number_schel].run_begin_time=tasks[number_schel].come_time;
tasks[number_schel].run_end_time=tasks[number_schel].run_begin_time+tasks[number_schel].run_time;
tasks[number_schel].run_flag=1;
temp_time=tasks[number_schel].run_end_time;
tasks[number_schel].order=1;
temp_counter=1;
while (temp_counter<counter)
{
for(j=0;j<counter;j++)
{
if((tasks[j].come_time<=temp_time)&&(!tasks[j].run_flag))
{run_time=tasks[j].run_time;number_schel=j;break;}
}

for(j=0;j<counter;j++)
{if((tasks[j].come_time<=temp_time)&&(!tasks[j].run_flag))
if(tasks[j].run_time<run_time)
{run_time=tasks[j].run_time;
number_schel=j;
}
}
/*查找下一个被调度的进程*/
/*对找到的下一个被调度的进程求相应的参数*/
tasks[number_schel].run_begin_time=temp_time;
tasks[number_schel].run_end_time=tasks[number_schel].run_begin_time+tasks[number_schel].run_time;
tasks[number_schel].run_flag=1;
temp_time=tasks[number_schel].run_end_time;
temp_counter++;
tasks[number_schel].order=temp_counter;
}return 0;
}

int hrrn() /*响应比高优先*/
{ int j,number_schel,temp_counter;
float temp_time,respond_rate,max_respond_rate;
/*第一个进程被调度*/
tasks[0].run_begin_time=tasks[0].come_time;
tasks[0].run_end_time=tasks[0].run_begin_time+tasks[0].run_time;
temp_time=tasks[0].run_end_time;
tasks[0].run_flag=1;
tasks[0].order=1;
temp_counter=1;
/*调度其他进程*/
while(temp_counter<counter)
{
max_respond_rate=0;
for(j=1;j<counter;j++)
{
if((tasks[j].come_time<=temp_time)&&(!tasks[j].run_flag))
{respond_rate=(temp_time-tasks[j].come_time)/tasks[j].run_time;
if (respond_rate>max_respond_rate)
{
max_respond_rate=respond_rate;
number_schel=j;
}
}
} /*找响应比高的进程*/
tasks[number_schel].run_begin_time=temp_time;
tasks[number_schel].run_end_time=tasks[number_schel].run_begin_time+tasks[number_schel].run_time;
temp_time=tasks[number_schel].run_end_time;
tasks[number_schel].run_flag=1;
temp_counter+=1;
tasks[number_schel].order=temp_counter;
}
return 0;
}
int pinput() /*进程参数输入*/
{ int i;
printf("please input the processcounter:\n");
scanf("%d",&counter);

for(i=0;i<counter;i++)
{printf("******************************************\n");
printf("please input the process of %d th :\n",i+1);
printf("please input the name:\n");
scanf("%s",tasks[i].name);
printf("please input the number:\n");
scanf("%d",&tasks[i].number);
printf("please input the come_time:\n");
scanf("%f",&tasks[i].come_time);
printf("please input the run_time:\n");
scanf("%f",&tasks[i].run_time);
printf("please input the priority:\n");
scanf("%d",&tasks[i].priority);
tasks[i].run_begin_time=0;
tasks[i].run_end_time=0;
tasks[i].order=0;
tasks[i].run_flag=0;
}
return 0;
}
int poutput() /*调度结果输出*/
{
int i;
float turn_round_time=0,f1,w=0;
printf("name number come_time run_timerun_begin_time run_end_time priority order turn_round_time\n");
for(i=0;i<counter;i++)
{
f1=tasks[i].run_end_time-tasks[i].come_time;
turn_round_time+=f1;
w+=(f1/tasks[i].run_time);
printf(" %s, %d, %5.3f, %5.3f, %5.3f, %5.3f, %d, %d,%5.3f\n",tasks[i].name,tasks[i].number,tasks[i].come_time,tasks[i].run_time,tasks[i].run_begin_time,tasks[i].run_end_time,tasks[i].priority,tasks[i].order,f1);
}
printf("average_turn_round_timer=%5.2f\n",turn_round_time/counter);
printf("weight_average_turn_round_timer=%5.2f\n",w/counter);
return 0;
}

‘拾’ 作业调度的短作业优先

短作业优先(SJF, Shortest Job First)又称为“短进程优先”SPN(Shortest Process Next);这是对FCFS算法的改进,其目标是减少平均周转时间。 (1) 优点:
比FCFS改善平均周转时间和平均带权周转时间,缩短作业的等待时间;
提高系统的吞吐量;
(2) 缺点:
对长作业非常不利,可能长时间得不到执行;
未能依据作业的紧迫程度来划分执行的优先级;
难以准确估计作业(进程)的执行时间,从而影响调度性能。 “最短剩余时间优先”SRT(Shortest Remaining Time)(允许比当前进程剩余时间更短的进程来抢占)
“最高响应比优先”HRRN(Highest Response Ratio Next)(响应比R = (等待时间 + 要求执行时间) / 要求执行时间,是FCFS和SJF的折衷)
最高响应比优先法(HRN,Highest Response_ratio Next)是对FCFS方式和SJF方式的一种综合平衡。FCFS方式只考虑每个作业的等待时间而未考虑执行时间的长短,而SJF方式只考虑执行时间而未考虑等待时间的长短。因此,这两种调度算法在某些极端情况下会带来某些不便。HRN调度策略同时考虑每个作业的等待时间长短和估计需要的执行时间长短,从中选出响应比最高的作业投入执行。
响应比R定义如下: R =(W+T)/T = 1+W/T
其中T为该作业估计需要的执行时间,W为作业在后备状态队列中的等待时间。每当要进行作业调度时,系统计算每个作业的响应比,选择其中R最大者投入执行。这样,即使是长作业,随着它等待时间的增加,W / T也就随着增加,也就有机会获得调度执行。这种算法是介于FCFS和SJF之间的一种折中算法。由于长作业也有机会投入运行,在同一时间内处理的作业数显然要少于SJF法,从而采用HRN方式时其吞吐量将小于采用SJF 法时的吞吐量。另外,由于每次调度前要计算响应比,系统开销也要相应增加。

热点内容
数据库主文件 发布:2024-05-07 00:14:41 浏览:607
木头创意解压 发布:2024-05-07 00:11:55 浏览:215
密码都能设置什么 发布:2024-05-07 00:02:30 浏览:741
兰州大学网络服务器ip地址 发布:2024-05-06 23:44:09 浏览:429
安卓手机为什么这么香 发布:2024-05-06 23:33:19 浏览:623
安卓微信深色模式是什么意思 发布:2024-05-06 23:14:41 浏览:686
脚本样本 发布:2024-05-06 23:03:41 浏览:630
sqlserver导出 发布:2024-05-06 23:02:16 浏览:808
phpredis操作 发布:2024-05-06 22:56:24 浏览:584
查看qq加密相册软件 发布:2024-05-06 22:45:36 浏览:619