当前位置:首页 » 编程语言 » 先来先服务算法c语言

先来先服务算法c语言

发布时间: 2022-10-01 11:29:31

⑴ 先来先服务 调度算法

太笼统了,不知道用什么代码写

⑵ 求编程模拟进程调度算法,c语言版,要求用先来先服务。

先来先服务:用队列来实现,至于是循环队列,链队还是顺序队列,那要看具体问题决定
进程调度:可以将要处理的任务集成为函数,并定义函数指针,最后将指针入队就行了

说白了,就是对队列的各种操作~~~

⑶ 用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语言模拟先来先服务的方法的进程调度,请大神帮忙看看我这个代码有什么问题

假设你的系统是win7,而你参照的代码是在xp上面写的【就是调用xp的底层的接口】,有些会出现这种问题。。。你就行你安装一个不符合这个系统版本的软件,不是也是会提示类似的错误?

⑸ 操作系统的一个小问题~~~~~

先来先服务的调度算法(C语言)主要数据结构:
typedef struct Lnode
{
char name[10];
struct Lnode *point;
int arrivetime;
int runtime;
char situation;
int wtime; //用来计算周转时间
}Lnode,*Linklist;

流程图:

代码:
#include<stdio.h>
#include<malloc.h>

//使用链表
typedef struct Lnode
{
char name[10];
struct Lnode *point;
int arrivetime;
int runtime;
char situation;
int wtime; //用来计算周转时间
}Lnode,*Linklist; //定义结点即定义pcb块

void creatlist(Linklist &L,int n) //给每个结点初始化
{
int i;
Linklist p;
L=(Linklist)malloc(sizeof(Lnode));
L->point=NULL;
for(i=n;i>0;i--)
{
p=(Linklist)malloc(sizeof(Lnode));
printf("Please input the name of the course\n");
getchar();
gets(p->name);
printf("then input the arrivetime and the runtime!\n");
scanf("%d %d",&p->arrivetime,&p->runtime);
p->situation='R';p->wtime=0;
p->point=L->point;L->point=p;
}
printf("\n");

}

int Run(Linklist p,int &n) //n初值为1必须为引用
{
Linklist q=p->point;
printf("Now the course %s is running!\n",p->name); //其实q即为head

printf("The time of this course has already run is %d\n",n++);

printf("The lefttime of this course is %d\n",--p->runtime);
if(p->runtime==0) p->situation='C';

printf("The leftcourse of the queue are:");
while(q!=NULL&&q->situation=='R') //situation为R即为在队列中就绪的
{ //直接打印输出即可
printf("%s ",q->name);
q=q->point;
}
printf("\n");
return 0;
}

Linklist adjust(Linklist &L) //结点排序!!
{
Linklist bef,now,p,q; //删掉now结点,先得找到他前面的结点bef
Linklist M; //p是灵活结点随时调用!!
M=(Linklist)malloc(sizeof(Lnode)); //新建一个链表选择需要的然后重新插入
M->point=NULL;
q=M;

while(L->point!=NULL)
{
int min=1000;
for(p=L->point;p!=NULL;p=p->point)
if(min>p->arrivetime)
{
min=p->arrivetime;
now=p;
}
for(bef=L;;bef=bef->point) //for(bef=L->point;;bef=bef->point)
if(bef->point==now) break; //前一个有可能是头结点
bef->point=now->point; //删除结点操作

{now->point=NULL;q->point=now; //删完立即插入另一个链表
q=now;}

}
return M;
}

void main()
{
int m,n; //n是进程的个数
int alltime=0; //即前一个进程运行完的时间
int waittime=0;
Linklist L,p,M; //L是表头,P可以灵活的随便指
printf("pleast input the number of the course!\n");
scanf("%d",&n);
creatlist(L,n); //创建一个头结点为L,个数为N的单向链表
M=adjust(L);
alltime=M->point->arrivetime;
for(p=M->point;p!=NULL;p=p->point)
{
if(p->arrivetime<alltime)
p->wtime=alltime-p->arrivetime;
p->wtime+=p->runtime;
alltime+=p->runtime; //这4行计算周转时间

m=1; //这里的m表示的是RUN函数执行的次数
while(p->runtime!=0)
{
Run(p,m); //执行并打印输出所需的结果
printf("The situation of this course is %c\n\n",p->situation);
}

}
for(p=M->point;p!=NULL;p=p->point)
{
printf("The course %s's waitingtime is %d\n",p->name,p->wtime);
waittime+=p->wtime;
}
printf("The average waitingtime of those courses is %d\n",waittime/n);
}
//等待时间+运行时间=周转时间.. /进程个数=平均周转

实验1.2 设计一个按优先级调度的算法,实现处理机调度
主要数据结构:
typedef struct Lnode
{
char name[10];
struct Lnode *point;
int priority;
int runtime;
char situation;
}Lnode,*Linklist;

流程图:

代码:
#include<stdio.h>
#include<malloc.h>
typedef struct Lnode
{
char name[10];
struct Lnode *point;
int priority;
int runtime;
char situation;
}Lnode,*Linklist; //定义结点即定义pcb块

void creatlist(Linklist &L,int n) //给每个结点初始化
{
int i;
Linklist p,l;
L=(Linklist)malloc(sizeof(Lnode));
L->point=NULL;
l=L;
for(i=n;i>0;i--)
{
p=(Linklist)malloc(sizeof(Lnode));
printf("Please input the name of the course\n");
getchar();
gets(p->name);
printf("then input the priority and the runtime!\n");
scanf("%d %d",&p->priority,&p->runtime);
p->situation='R';
p->point=NULL;l->point=p;l=p; //依次构造结点并非倒着构造
}
printf("\n");

}

int Run(Linklist &L,Linklist p) //L为链头指针
{
Linklist h=L->point;
printf("Now the course %s is running!\n",p->name); //其实h即为head

printf("Now The priority of the course is %d\n",p->priority); //只打印输出即可减操作已在
//主函数执行!!

printf("And The lefttime of this course is %d\n",--p->runtime);
if(p->runtime==0) p->situation='C';

if(p->situation=='R')
printf("This course's situation is R now!\n");
if(p->situation=='C') printf("This course's situation is C now!\n");

printf("The leftcourse of the queue are:");
while(h!=NULL) //situation为R即为在队列中就绪的
{ //直接打印输出即可
if(h->situation=='R')
printf("%s ",h->name);
h=h->point;
}
printf("\n");
printf("\n");
return 0;
}

//返回链表中优先级最大的!!!
Linklist choose (Linklist L)
{
Linklist p,m;
int max=-10000;
for(p=L->point;p!=NULL;p=p->point)
{
if(max<p->priority)
{
max=p->priority;
m=p;
}
}
return m;
}
//返回链表中runtime最大的值并且得是最后的一个!!!
Linklist latest (Linklist L)
{
Linklist p,m;
int max=-1;
for(p=L->point;p!=NULL;p=p->point)
{
if(max<=p->runtime)
{
max=p->runtime;
m=p;
}
}
return m;
}

void main()
{
int n; //n是进程的个数
Linklist L,m,nn,late; //L是表头,late指向runtime最长的,m为被选中的优先级最高的
printf("pleast input the number of the course!\n");
scanf("%d",&n);
creatlist(L,n); //创建一个头结点为L,个数为N的单向链表(按顺序链接)
late=latest(L); //找到所需时间最长的
nn=late; //给nn赋初值,没有报错!
while(late->situation!='C') //循环条件
{
m=choose(L); //选出优先级最高的
if(m->priority==nn->priority&&nn->runtime!=0) //若m经一次处理后优先级仍是最高
m=nn; //则继续执行他!!
//if(m->priority>0) //优先级可以为负的!!考虑当优先级都为0的情况
m->priority--; //priority放在Run函数外执行!!
if(m->runtime>0)
Run(L,m); //俩参数,头结点,和m.
nn=m;
late=latest(L);
}
}
3.设计一个按时间片轮转法实现处理机调度的程序。
(1)数据结构:
PCB格式:
进程名
连接指针
到达时间
要求运行时间
进程状态

流程图:

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>

//使用链表?主要结构:
typedef struct Lnode
{
char name[10];
struct Lnode *point;
int arrivetime;
int runtime;
char situation;
}Lnode,*Linklist; //定义结点即定义pcb块

void creatlist(Linklist &L,int n) //给每个结点初始化
{
int i;
Linklist p;
L=(Linklist)malloc(sizeof(Lnode));
L->point=NULL;
for(i=n;i>0;i--)
{
p=(Linklist)malloc(sizeof(Lnode));
printf("Please input the name of the course\n");
getchar();
gets(p->name);
printf("then input the arrivetime and the runtime!\n");
scanf("%d %d",&p->arrivetime,&p->runtime);
p->situation='R';
p->point=L->point;L->point=p;
}
printf("\n");

}

int Run(Linklist &L,Linklist p)
{
Linklist h=L->point;

printf("Now the course %s is running!\n",p->name); //其实q即为head

printf("The lefttime of this course is %d\n",--p->runtime);
if(p->runtime==0) p->situation='C';

printf("The situation of this course is %c\n",p->situation);
printf("The leftcourse of the queue are:");
while(h!=NULL) //situation为R即为在队列中就绪的
{ //直接打印输出即可
if(h->situation=='R')
printf("%s ",h->name);
h=h->point;
}
printf("\n\n");
return 0;
}

Linklist adjust(Linklist &L) //结点排序!!
{
Linklist bef,now,p,q; //删掉now结点,先得找到他前面的结点bef
Linklist M; //p是灵活结点随时调用!!
M=(Linklist)malloc(sizeof(Lnode)); //新建一个链表选择需要的然后重新插入
M->point=NULL;
q=M;

while(L->point!=NULL)
{
int min=1000;
for(p=L->point;p!=NULL;p=p->point)
if(min>p->arrivetime)
{
min=p->arrivetime;
now=p;
}
for(bef=L;;bef=bef->point) //for(bef=L->point;;bef=bef->point)
if(bef->point==now) break; //前一个有可能是头结点
bef->point=now->point; //删除结点操作

{now->point=NULL;q->point=now; //删完立即插入另一个链表
q=now;}

}
return M;
}

void Del(Linklist &L,Linklist &now)
{
Linklist bef,temp;
temp=now;//!要删掉now 先把它赋值给另外一个Linklist型temp然后释放掉temp!
for(bef=L;;bef=bef->point)
if(bef->point==now) break;//先找到now前的一个节点bef

bef->point=now->point; //bef指向now的point
now=bef;//!不加这句话不行!没法执行指针加1操作
free(temp);
}

void main()
{
int n; //n是进程的个数
Linklist L,p; //L是表头,P可以灵活的随便指
printf("pleast input the number of the course!\n");
scanf("%d",&n);
creatlist(L,n); //创建一个头结点为L,个数为N的单向链表
Linklist M=adjust(L); //M即为已排好序的链表头结点
while(M->point!=NULL)
{
for(p=M->point;p!=NULL;p=p->point)
{
if(p->runtime!=0)
Run(M,p); //执行并打印输出所需的结果
else
Del(M,p);
}
}
}

⑹ 求进程调度先来先服务算法,短进程优先算法完整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;
}

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

你试一下

#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;
}

⑻ 先来先服务算法(C语言版)

#include<stdio.h>
#include<stdlib.h>

typedef struct process_FCFS{
float arrivetime;//到达时间
float servetime;//服务时间
float finishtime;//完成时间
float roundtime;//周转时间
float daiquantime;//带权周转时间
struct process_FCFS *link;//结构体指针
}FCFS;
FCFS *p,*q,*head=NULL;
struct process_FCFS a[100];
//按到达时间进行冒泡排序

struct process_FCFS *sortarrivetime(struct process_FCFS a[],int n)
{
int i,j;
struct process_FCFS t;
int flag;
for(i=1;i<n;i++)
{
flag=0;
for(j=0;j<n-i;j++)
{
if(a[j].arrivetime>a[j+1].arrivetime)
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
flag=1;//交换
}
}
if(flag==0)//如果一趟排序中没发生任何交换,则排序结束
break;
}
return a;
}

//先来先服务算法

void print(struct process_FCFS a[],int n)
{
int i;
for(i=0;i<n;i++)
{
printf("到达时间:%f",a[i].arrivetime);
printf("服务时间:%f",a[i].servetime);

printf("完成时间:%f",a[i].finishtime);
printf("周转时间:%f",a[i].roundtime);
printf("带权周转时间:%f",a[i].daiquantime);
printf("\n");
}
}

void Fcfs(struct process_FCFS a[],int n)
{
int i;
a[0].finishtime=a[0].arrivetime+a[0].servetime;
a[0].roundtime=a[0].finishtime+a[0].arrivetime;
a[0].daiquantime=a[0].roundtime/a[0].servetime;
for(i=0;i<n;i++)
{
if(a[i].arrivetime<a[i-1].finishtime)
{
a[i].finishtime=a[i-1].finishtime+a[i].servetime;
a[i].roundtime=a[i].finishtime-a[i].arrivetime;
a[i].daiquantime=a[i].roundtime/a[i].servetime;
}
else
{
a[i].finishtime=a[i].arrivetime+a[i].servetime;
a[i].roundtime=a[i].finishtime-a[i].arrivetime;
a[i].daiquantime=a[i].roundtime/a[i].servetime;
}
}
printf("先来先服务\n");
print(a,n);
}
//主函数
void main()
{
int n,i;
printf("请输入有几个进程\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("arrivetime");
scanf("%f",&a[i].arrivetime);
printf("servetime");
scanf("%f",&a[i].servetime);
}
Fcfs(a,n);
}

⑼ 用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();

}

这是操作系统的作业吧

⑽ 急求 程序代码 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;

}

热点内容
pythonfuture 发布:2024-12-25 01:46:47 浏览:586
如何提升交换机配置能力 发布:2024-12-25 01:41:53 浏览:669
安卓系统怎么删除主屏 发布:2024-12-25 01:41:45 浏览:493
微信小程序客户端是如何访问服务器的 发布:2024-12-25 01:39:26 浏览:508
python逗号split 发布:2024-12-25 01:24:06 浏览:155
sqlwithas效率 发布:2024-12-25 01:21:25 浏览:484
pcielinux 发布:2024-12-25 01:12:02 浏览:644
展示迷宫算法 发布:2024-12-25 00:58:25 浏览:438
手机酷我音乐上传歌词 发布:2024-12-25 00:58:14 浏览:797
路由器哪里改密码 发布:2024-12-25 00:53:18 浏览:659