读入一个文本文件中描述的进程序列,模拟短作业优先算法
发布网友
发布时间:2023-03-27 13:34
我来回答
共1个回答
热心网友
时间:2023-10-24 00:07
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<string.h>
#define LENsizeof(struct job)
struct job
{
char name[2];
int cometime;
int runtime;
int priority;
int finishtime;
int state;
struct job *next;
};
void readtxt();
void SJF();
void copy(structjob *,struct job *);
void RR(int);
void FPF();
void print1(structjob *);
void print2(structjob *);
int n=0;
struct job*head=NULL;
struct job*end=NULL;
FILE*fp=NULL;
void main()
{
if((fp=fopen("JOB1.txt","rb"))==NULL){
printf("can not find file\n");
exit(0);
}
while(!feof(fp)){
readtxt();
}
fclose(fp);
int x,y;
printf("请选择进程调度算法:\n");
printf("1.短作业优先算法 2.时间片轮转算法 3.优先数算法\n");
printf("选择序号:");
scanf("%d",&x);
if((x!=1)&&(x!=2)&&(x!=3))printf("序号不存在!\n");
else{
switch(x){
case 1: SJF(); break;
case 2: {
printf("输入时间片:");
scanf("%d",&y);
RR(y);
break;
}
case 3: FPF(); break;
}
}
}
void readtxt(){
struct job *p1;
p1=(struct job *)malloc(LEN);
fscanf(fp,"%s %d %d%d",(*p1).name,&((*p1).cometime),&((*p1).runtime),&((*p1).priority));
(*p1).state=0;
(*p1).finishtime=0;
if(n==0){
head=p1;
end=p1;
(*end).next=NULL;
n++;
}
else{
(*end).next=p1;
end=p1;
(*end).next=NULL;
n++;
}
}
void SJF(){
struct job *shead,*send,*p1,*p2,*p;
int i,j,curtime;
p1=head;
for(i=0;i<n;i++){
if((*p1).cometime==0) break;
else p1=(*p1).next;
}
p2=(*p1).next;
for(i=i+1;i<n;i++){
if(((*p2).cometime==0)&&((*p2).runtime<(*p1).runtime)){p1=p2;p2=(*p2).next;}
else p2=(*p2).next;
}
(*p1).state=1;
curtime=(*p1).runtime;
(*p1).finishtime=curtime;
p=(struct job *)malloc(LEN);
copy(p,p1);
shead=p;
send=p;
for(j=0;j<n-1;j++){
p1=head;
for(i=0;i<n;i++){
if(((*p1).cometime<=curtime)&&((*p1).state!=1))break;
else p1=(*p1).next;
}
p2=(*p1).next;
for(i=i+1;i<n;i++){
if(((*p2).cometime<=curtime)&&((*p2).runtime<(*p1).runtime)&&((*p2).state!=1))
{p1=p2;p2=(*p2).next;}
else p2=(*p2).next;
}
(*p1).state=1;
curtime=(*p1).runtime+curtime;
(*p1).finishtime=curtime;
p=(struct job *)malloc(LEN);
copy(p,p1);
(*send).next=p;
send=p;
}
(*send).next=NULL;
printf("%s\n","短作业优先算法执行结果:");
printf("%s\n","进程执行顺序 周转时间");
print1(shead);
}
void RR(intpertime){
structjob *rhead,*rend,*rrhead,*rrend,*p1,*p2,*p;
int i,curtime=0,m=0,temp1=0,temp2;
while(m!=n){
p1=head;
temp2=temp1;
for(i=0;i<n;i++){
if(((*p1).cometime<=curtime)&&((*p1).runtime!=0)&&((*p1).state!=1)){temp1++;break;}
else p1=(*p1).next;
}
if(p1!=NULL){
p2=(*p1).next;
for(i=i+1;i<n;i++){
if((((*p2).cometime<(*p1).cometime)&&((*p2).runtime!=0)&&((*p2).state!=1))||
(((*p2).cometime==(*p1).cometime)&&((*p2).priority<(*p1).priority)&&((*p2).runtime!=0)&&((*p2).state!=1)))
{p1=p2;p2=(*p2).next;}
else p2=(*p2).next;
}
}
if(temp2!=temp1){
(*p1).state=1;
p=(struct job *)malloc(LEN);
copy(p,p1);
if(temp1==1) {rhead=p;rend=p;}
else{
(*rend).next=p;
rend=(*rend).next;
}
}
else{
if((temp1==1)&&(m==0)){
curtime+=pertime;
(*rhead).runtime-=pertime;
if((*rhead).runtime<=0){
curtime+=(*rhead).runtime;
(*rhead).runtime=0;
(*rhead).finishtime=curtime;
m++;
temp1--;
}
p=(struct job *)malloc(LEN);
copy(p,rhead);
rrhead=p;
rrend=p;
}
else{
if(strcmp((*rhead).name,(*rrend).name)==0){
(*rend).next=rhead;
rend=rhead;
rhead=(*rhead).next;
curtime+=pertime;
(*rhead).runtime-=pertime;
if((*rhead).runtime<=0){
curtime+=(*rhead).runtime;
(*rhead).runtime=0;
(*rhead).finishtime=curtime;
m++;
temp1--;
p=(struct job *)malloc(LEN);
copy(p,rhead);
(*rrend).next=p;
rrend=(*rrend).next;
rhead=(*rhead).next;
}
else{
p=(struct job*)malloc(LEN);
copy(p,rhead);
(*rrend).next=p;
rrend=(*rrend).next;
(*rend).next=rhead;
rend=rhead;
rhead=(*rhead).next;
(*rend).next=NULL;
}
}
else{
curtime+=pertime;
(*rhead).runtime-=pertime;
if((*rhead).runtime<=0){
curtime+=(*rhead).runtime;
(*rhead).runtime=0;
(*rhead).finishtime=curtime;
m++;
temp1--;
p=(struct job *)malloc(LEN);
copy(p,rhead);
(*rrend).next=p;
rrend=(*rrend).next;
rhead=(*rhead).next;
}
else{
p=(struct job*)malloc(LEN);
copy(p,rhead);
(*rrend).next=p;
rrend=(*rrend).next;
(*rend).next=rhead;
rend=rhead;
rhead=(*rhead).next;
(*rend).next=NULL;
}
}
}
}
}
(*rrend).next=NULL;
printf("%s%d%s\n","时间片轮转算法执行结果(时间片",pertime,"):");
print2(rrhead);
}
void FPF(){
structjob *fhead,*fend,*p1,*p2,*p;
int i,j,curtime;
p1=head;
for(i=0;i<n;i++){
if((*p1).cometime==0) break;
else p1=(*p1).next;
}
p2=(*p1).next;
for(i=i+1;i<n;i++){
if(((*p2).cometime==0)&&((*p2).priority<(*p1).priority)){p1=p2;p2=(*p2).next;}
else p2=(*p2).next;
}
(*p1).state=1;
curtime=(*p1).runtime;
(*p1).finishtime=curtime;
p=(struct job *)malloc(LEN);
copy(p,p1);
fhead=p;
fend=p;
for(j=0;j<n-1;j++){
p1=head;
for(i=0;i<n;i++){
if(((*p1).cometime<=curtime)&&((*p1).state!=1))break;
else p1=(*p1).next;
}
p2=(*p1).next;
for(i=i+1;i<n;i++){
if(((*p2).cometime<=curtime)&&((*p2).priority<(*p1).priority)&&((*p2).state!=1))
{p1=p2;p2=(*p2).next;}
else p2=(*p2).next;
}
(*p1).state=1;
curtime=(*p1).runtime+curtime;
(*p1).finishtime=curtime;
p=(struct job *)malloc(LEN);
copy(p,p1);
(*fend).next=p;
fend=p;
}
(*fend).next=NULL;
printf("%s\n","最高优先权优先算法执行结果(非抢占方式):");
printf("%s\n","进程执行顺序 周转时间");
print1(fhead);
}
void copy(structjob *p,struct job *p1){
strcpy((*p).name,(*p1).name);
(*p).cometime=(*p1).cometime;
(*p).runtime=(*p1).runtime;
(*p).priority=(*p1).priority;
(*p).finishtime=(*p1).finishtime;
(*p).state=(*p1).state;
}
void print1(structjob *p){
while(p!=NULL){
printf("%-14s%d\n",(*p).name,(*p).finishtime-(*p).cometime);
p=(*p).next;
}
}
void print2(structjob *p){
struct job *head;
head=p;
printf("%s\n","进程执行顺序");
while(head!=NULL){
printf("%3s",(*head).name);
head=(*head).next;
}
printf("\n%s\n","进程周转时间");
head=p;
while(head!=NULL){
if(((*head).finishtime-(*head).cometime)>0)
printf("%-4s%d\n",(*head).name,(*head).finishtime-(*head).cometime);
head=(*head).next;
}
}
网上找的,自己看着办吧