时间片轮转算法

来源:百度知道 编辑:UC知道 时间:2024/09/18 04:00:48
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream.h>

typedef struct node
{
char name[10];
int prio;
int round;
int cputime;
int needtime;
int count;
char state;
struct node *next;
}PCB;
PCB *finish,*ready,*tail,*run; //队列指针
int N; //进程数

void firstin()
{
run=ready; //就绪队列头指针赋值给运行头指针
run->state='R'; //进程状态变为运行态]
ready=ready->next; //就绪队列头指针后移到下一进程
}
//输出标题函数
void prt1(char a)
{
if(toupper(a)=='P') //优先级法
cout<<" "<<endl;
cout<<"进程名 占用CPU时间 到完成还要的时间 轮转时间片 状态"<<endl;
}

//进程PCB输出
void prt2(char a,PCB *q)
{
if(toupper(a)=='P') //优先级法的输出
cout<<q->name<<" "<<q->cputime<<&

建议看下《Operating System》高教版(貌似是第六版)

代码写得不错。http://course.bigc.edu.cn/czxt/chapter2/section4/2.4.1.htm

create() 函数是初始化进程数,状态等信息。
timeslicecycle()函数是用于模拟时间片轮转算法。

///////////////////////
//执行算法初始化输入
//////////////////////
输入进程的个数:5
输入进程名及其需要运行的时间:
a 10
输入进程名及其需要运行的时间:
b 20
输入进程名及其需要运行的时间:
c 15
输入进程名及其需要运行的时间:
d 9
输入进程名及其需要运行的时间:
e 7

///////////////////////////////////
//开始模拟的样子 W-Work, F-Finish, R-Ready
///////////////////////////////////
进程名 占用CPU时间 到完成还要的时间 轮转时间片 状态
a 0 10 0 W
b 0 20 0 W
c 0 15 0 W
d 0 9 0 W
e 0 7 0 W

进程名 占用CPU时间 到完成还要的时间 轮转时间片 状态
b 0 20 0 R
c 0 15 0 W
d 0 9 0 W
e 0 7 0 W
a 8 2 8 W

进程名 占用CPU时间 到完成还要的时间 轮转时间片 状态
c 0 15 0 R
d 0 9 0 W
e 0 7 0 W
a 8