1.先来先服务算法
#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);
}
int 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);
}
2.短进程优先算法
#include<iostream>
#include<stdio.h>
using namespace std;
struct pcb
{
char pno;
int come_time;
int run_time;
};
float fcfs(pcb pro[],int n)
{
struct pcb temp;
int i,j,k;
float weight_time=0,time=0;
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;
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)
{
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;
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--;
}
return weight_time/=n;
}
int 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;
}