#include
#include
#define True
1
#define False
0
#define Q
1
//定义 PCB 的数据结构
typedef struct pcb{
unsigned char pid; //进程 id 号
unsigned char startTime; //开始时间
unsigned char serviceTime; //服务时间
unsigned char runningTime; //表示在 cpu 上已经执行了多少时间,用于判断时间片到
期
unsigned char endTime; //结束时间
unsigned char needTime; //剩余时间(需要多少时间能完成)
}PCB;
//定义就绪队列的数据结构
typedef struct readyList{
PCB *p;
struct readyList *next;
}RL;
void init(PCB *p, unsigned char n);
void newProcessArrived(unsigned int clock,PCB *p, unsigned char n, RL *rl);
unsigned char isInterrupt(unsigned int clock,RL *cpu, RL *rl);
void insertIntoReadyList(RL *cpu, RL *rl);
RL * schedule(RL *rl);
void destroyProcess(RL *cpu);
void prt(RL *cpu, RL *rl);
void output(PCB *p, unsigned char n);
unsigned int clock = 0;
int main(){
PCB p[5];
int count = 0;
RL *rl = (RL *)malloc(sizeof(RL)); //初始化链表 rl 的头节点
RL *cpu = NULL; //指向当前正在 CPU 执行的进程
rl->p = NULL;
rl->next = NULL;
init(p,5); //初始化进程的到达时间和服务时间以及进程名
while(True){
printf("时刻%d:n",clock);
newProcessArrived(clock,p,5,rl);//判断是否在该时刻有新的进程达到
if(isInterrupt(clock,cpu,rl) == True){ //判断是否在该时刻发生调度中断
if(cpu!=NULL){// 表示发生中断只有两种可能:1. 时间片到期 2. 进程执行完
毕
if(cpu->p->needTime <= 0){//进程执行完毕printf("进程%c 执行完毕,进程终止!n",cpu->p->pid);
cpu->p->endTime = clock;
destroyProcess(cpu);
count++;
}else{ //时间片到期
printf("时间片到期,进程%c 执行未完成,重新进入就绪队列!
n",cpu->p->pid);
insertIntoReadyList(cpu,rl);//插入到就绪队列中
}
}
if(count >= 5){ //表示所有进程都执行完毕
printf("所有进程执行完毕,程序结束n");
break;
}
cpu = schedule(rl); //获取一个新的进程进入 CPU 调度。
printf("进程%c 上处理机运行n",cpu->p->pid);
}
prt(cpu,rl); //输出当前时刻 CPU 上运行的进程和就绪队列中的进程
cpu->p->needTime--; //剩余时间减一
cpu->p->runningTime++; //运行时间加 1
clock++;
}
output(p,5); //输出结果,计算周转时间,带权周转时间
return 0;
}
void init(PCB *p, unsigned char n){
p[0].pid = 'A';
p[1].pid = 'B';
p[2].pid = 'C';
p[3].pid = 'D';
p[4].pid = 'E';
p[0].startTime = 0;
p[1].startTime = 2;
p[2].startTime = 4;
p[3].startTime = 6;
p[4].startTime = 8;
p[0].runningTime = 0;
p[1].runningTime= 2;
p[2].runningTime= 4;
p[3].runningTime= 6;
p[4].runningTime= 8;
p[0].serviceTime = 3;
p[1].serviceTime = 6;
p[2].serviceTime = 4;p[3].serviceTime = 5;
p[4].serviceTime = 2;
p[0].needTime = 3;
p[1].needTime = 6;
p[2].needTime = 4;
p[3].needTime = 5;
p[4].needTime = 2;
}
void newProcessArrived(unsigned int clock,PCB *p, unsigned char n, RL *rl){
int i;
for(i=0;ip = &p[i];
node->next = NULL;
insertIntoReadyList(node, rl); //插入到就绪队列中
}
}
}
unsigned char isInterrupt(unsigned int clock,RL *cpu, RL *rl){
if(cpu == NULL && rl->next != NULL){ //cpu 空闲,第一次进程调度
return True;
}
if(cpu->p->runningTime >= Q) { //时间片到期
return True;
}
if(cpu->p->needTime <=0){ //执行完毕
return True;
}
return False;
}
void insertIntoReadyList(RL *cpu, RL *rl){ //插入到队尾
RL *q = rl;
while(q->next!=NULL){
q = q->next;
}
q->next = cpu;
cpu->next=NULL;
cpu->p->runningTime = 0; //进入就绪队列,运行时间清零,保证下一次调度的时候重
新计时
}
RL * schedule(RL *rl){ //返回就绪队列的第一节点。RL *q = rl->next;
rl->next = q->next;
return q;
}
void destroyProcess(RL *cpu){
free(cpu);
}
void prt(RL *cpu, RL *rl){
RL *q = rl;
if(cpu == NULL){
printf("当前无进程运行n");
}else{
printf("当前运行进程:%cn",cpu->p->pid);
}
printf("就绪队列:");
while(q->next!=NULL){
printf("%c ",q->next->p->pid);
q = q->next;
}
putchar('n');
}
void output(PCB *p, unsigned char n){
int i;
printf("--------------------------------------------n");
printf(" |");
for(i=0;i