// zcn.cpp : Defines the entry point for the console application.
//
#include"stdafx.h"
#include<iostream>
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 argc, char* argv[])
{
int option;
pinput();
printf("请选择调度算法(0~4):\n");
printf(" 1.先来先服务\n");
printf(" 2.优先级调度\n");
printf(" 3.短作业优先\n");
printf(" 4.响应比高优先\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;
int i;
int number_schedul;
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_schedul=i;
tasks[number_schedul].order=i+1;
}
return 0;
}
int ps() /*优先级调度*/
{
float temp_time=0;
int i=0,j;
int number_schedul,temp_counter;
int max_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_schedul=i;
tasks[number_schedul].run_begin_time=tasks[number_schedul].come_time;
tasks[number_schedul].run_end_time=tasks[number_schedul].run_begin_time+tasks[number_schedul].run_time;
tasks[number_schedul].run_flag=1;
temp_time=tasks[number_schedul].run_end_time;
tasks[number_schedul].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_schedul=j;
}
} /*查找下一个被调度的进程*/
/*对找到的下一个被调度的进程求相应的参数*/
tasks[number_schedul].run_begin_time=temp_time;
tasks[number_schedul].run_end_time=tasks[number_schedul].run_begin_time+tasks[number_schedul].run_time;
tasks[number_schedul].run_flag=1;
temp_time=tasks[number_schedul].run_end_time;
temp_counter++;
tasks[number_schedul].order=temp_counter;
}
return 0;
}
int sjf() /*短作业优先*/
{
float temp_time=0;
int i=0,j;
int number_schedul,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_schedul=i;
tasks[number_schedul].run_begin_time=tasks[number_schedul].come_time;
tasks[number_schedul].run_end_time=tasks[number_schedul].run_begin_time+tasks[number_schedul].run_time;
tasks[number_schedul].run_flag=1;
temp_time=tasks[number_schedul].run_end_time;
tasks[number_schedul].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_schedul=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_schedul=j;
}
}
/*查找下一个被调度的进程*/
/*对找到的下一个被调度的进程求相应的参数*/
tasks[number_schedul].run_begin_time=temp_time;
tasks[number_schedul].run_end_time=tasks[number_schedul].run_begin_time+tasks[number_schedul].run_time;
tasks[number_schedul].run_flag=1;
temp_time=tasks[number_schedul].run_end_time;
temp_counter++;
tasks[number_schedul].order=temp_counter;
}
return 0;
}
int hrrn() /*响应比高优先*/
{
int j,number_schedul,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_schedul=j;
}
}
} /*找响应比高的进程*/
tasks[number_schedul].run_begin_time=temp_time;
tasks[number_schedul].run_end_time=tasks[number_schedul].run_begin_time+tasks[number_schedul].run_time;
temp_time=tasks[number_schedul].run_end_time;
tasks[number_schedul].run_flag=1;
temp_counter+=1;
tasks[number_schedul].order=temp_counter;
}
return 0;
}
int pinput() /*进程参数输入*/
{
int i;
printf("请输入实际进程个数:\n");
scanf("%d",&counter); //实际进程个数
for(i=0;i<counter;i++)
{
printf("******************************************\n");
printf("请输入第 %d 个 :\n",i+1);
printf("请输入进程名:\n");
scanf("%s",tasks[i].name);
printf("请输入进程号:\n");
scanf("%d",&tasks[i].number);
printf("请输入进程到达时间:\n");
scanf("%f",&tasks[i].come_time);
printf("请输入运行时间:\n");
scanf("%f",&tasks[i].run_time);
printf("请输入进程优先级:\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("进程名 进程号 到达时间 运行时间 开始运行时间 结束运行时间 优先级 运行次序 \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)