我是一名计算机专业的学生,很荣幸在这里结识各位编程高手.今天第一次写东西,希望大家多多支持,多多留言哦.以下是一个多级反馈队列调度算法,请各位帮忙看看对不对.如果谁能帮写一个更好的那就更感谢了.THANK YOU VERY MUCH!
各位有什么好的建议可发邮件到ty144167@sina.com.
#include "stdio.h"
#include
#include
#define getpch(type) (type*)malloc(sizeof(type))
#define NULL 0
struct pcb { /* 定义进程控制块PCB */
char name[10];
char state;
int super;//优先级别
int ntime;//需运行时间
int rtime; //已经运行时间
struct pcb* link;
}*ready=NULL,*p;
typedef struct pcb PCB;
void sort() /* 建立对进程进行fcfs算法排列函数*/
{
PCB *first, *second;
int insert=0;
if(ready==NULL) /*优先来者,插入队首*/
{
p->link=ready;
ready=p;
}
else/*否则插入队尾*/
{
first=ready;
second=first->link;
while(second!=NULL)
{
first=first->link;
second=second->link;
}
if(insert==0) first->link=p;/* 插入到队尾*/
}
}
void input() /* 建立进程控制块函数*/
{
int i,num;
/*clrscr(); 清屏*/
system("cls");
printf("\n 进程调度轮转法.cpp \n");
printf("\n 请输入num再按回车,即进程个数为\n");
scanf("%d",&num);
for(i=0;i
{
printf("\n 进程号No.%d:\n",i);
p=getpch(PCB);
printf("\n 输入进程名:");
scanf("%s",p->name);
printf("\n 输入进程优先数:");
scanf("%d",&p->super);
printf("\n 输入进程运行时间:");
scanf("%d",&p->ntime);
printf("\n");
p->rtime=0;p->state='w';p->link=NULL;
sort(); /* 调用sort函数*/
{}
int space()
{
int l=0; PCB* pr=ready;
while(pr!=NULL)
{
l++;
pr=pr->link;
}
return(l);
}
void disp(PCB * pr) /*建立进程显示函数,用于显示当前进程*/
{
printf("\n qname \t state \t super \t ndtime \t runtime \n");
printf("|%s\t",pr->name);
printf("|%c\t",pr->state);
printf("|%d\t",pr->super);
printf("|%d\t",pr->ntime);
printf("|%d\t",pr->rtime);
printf("\n");
}
void check() /* 建立进程查看函数 */
{
PCB* pr;
printf("\n **** 当前正在运行的进程是:%s",p->name); /*显示当前运行进程*/
disp(p);
pr=ready;
printf("\n ****当前就绪队列状态为:\n"); /*显示就绪队列状态*/while(pr!=NULL)
{
disp(pr);
pr=pr->link;
}
}
void destroy() /*建立进程撤消函数(进程运行结束,撤消进程)*/
{
printf("\n 进程 [%s] 已完成.\n",p->name);
free(p);
}
void running() /* 建立进程就绪函数(进程运行时间到,置就绪状态*/
{
(p->rtime)++;
if(p->rtime==p->ntime)
destroy(); /* 调用destroy函数*/
else
{
(p->super)--;
p->state='w';
sort(); /*调用sort函数*/
}
}
void main() /*主函数*/
{
int len,h=0;
char ch;
input();
len=space();
while((len!=0)&&(ready!=NULL))
{
ch=getchar();
h++;//标志执行的步骤
printf("\n The execute number:%d \n",h);
p=ready;
ready=p->link;
p->link=NULL;
p->state='R';
check();//显示运行及等待队列的状况running();printf("\n 按任一键继续......");
ch=getchar();}printf("\n\n 进程已经完成.\n");ch=getchar();}