实验二 (2)优先数调度
#include<stdio.h>
#define Time int
#define M 100
typedef struct process{
char name[M];//进程名
int priority;//优先数
int reachtime;//到达时间
int needtime;//需要时间
int usedtime;//已用时间
char state;//进程状态
int Ptime;//时间片大小
}PCB; //进程控制块
int n;
PCB pcb[M];
int pTime;
void print(int n){
int i;
printf("#####################################进程调度###################################\n");
printf("--------------------------------------------------------------------------------\n");
printf(" 进程名\t优先数\t\t需要时间\t到达时间\n");
for(i=0;i<n;i++){
printf(" %s\t\t%d\t\t%d\t\t%d\n",pcb[i].name,pcb[i].priority,pcb[i].needtime,pcb[i].reachtime);
}
printf("--------------------------------------------------------------------------------\n");
}
void sort(int n)
{
int i;
PCB temp;
int j;
for(i=0;i<n;i++){//按优先数的高低排序
for(j=i;j<n;j++){
if(pcb[j].priority>pcb[i].priority)
{
temp=pcb[j];
pcb[j]=pcb[i];
pcb[i]=temp;
}
if(pcb[j].priority==pcb[i].priority)//优先数相等则按到达时间排序
{
if(pcb[j].reachtime<pcb[i].reachtime){
temp=pcb[j];
pcb[j]=pcb[i];
pcb[i]=temp;
}
}
}
}
}
void main(){
int i;
int j;
PCB temp;
printf("请输入进程数:");
scanf("%d",&n);
for(i=0;i<n;i++){
printf("\n请输入进程名:");
scanf("%s",&pcb[i].name);
printf("请输入优先数:");
scanf("%d",&pcb[i].priority);
printf("请输入需要的时间:");
scanf("%d",&pcb[i].needtime);
printf("请输入到达的时间:");
scanf("%d",&pcb[i].reachtime);
}
sort(n);
print(n);
for(i=0;i<n;i++){
while(pcb[i].needtime!=0){
printf("\n请按任意键继续......\n");
printf("\n");
fflush(stdin);
getchar();
printf(" 当前运行的程序是:\n");
printf("--------------------------------------------------------------------------------\n");
printf(" 进程名\t优先数\t\t需要时间\t到达时间\n");
pcb[i].needtime=pcb[i].needtime-1;
pcb[i].priority=pcb[i].priority-1;
printf(" %s\t\t%d\t\t%d\t\t%d\n",pcb[i].name,pcb[i].priority,pcb[i].needtime,pcb[i].reachtime);
printf("--------------------------------------------------------------------------------\n");
printf("\n");
printf("\n");
print(n);
sort(n);
}
}
printf(" 提示:时间片轮转调度结束!\n");
}