时间片轮转调度算法
实验二 时间片轮转调度算法
实验内容:
模拟实现时间片轮转调度算法,具体如下:
设置进程体:进程名,进程的到达时间,服务时间,,进程状态(W ——等待,R ——运行,F
——完成),进程间的链接指针
进程初始化:由用户输入进程名、服务时间进行初始化,同时,初始化进程的状态为W 。 显示函数:在进程调度前、调度中和调度后进行显示。
排序函数:对就绪状态的进程按照进入就绪队列的时间排序,新到达的进行应优先于刚刚执
行过的进程进入就绪队列的队尾。注意考虑到达时间
调度函数:每次从就绪队列队首调度优一个进程执行,状态变化。并在执行一个时间片后化,
服务时间变化,状态变化。当服务时间为0时,状态变为F 。
删除函数:撤销状态为F 的进行。
实验要求:
1、测试数据可以随即输入或从文件中读入。
2、必须要考虑到进程的到达时间
3、最终能够计算每一个进程的周转时间的带权周转时间。
实验代码:
#include
#include
#include
using namespace std;
int n;
class PCB
{
public:
int pri;
int runtime;
int pieceOftime;
string procname;
string state;
int needOftime;
int Counter;
PCB * next;
};
PCB * run = NULL;
PCB * ready = NULL;
PCB * finish = NULL;
PCB * tial = ready;
void Dtime(int t);
void Prinft(int a)
{
if(a==1)
{
cout
}
void Prinft(int b,PCB * p)
{
if(b==1)
{
coutprocname
"needOftimeruntimepieceOftimestate
}
void display(int c)
{
PCB *p;
if(run!=NULL)
Prinft(c,run);
p=ready;
while(p!=NULL)
{
Prinft(c,p);
p=p->next;
}
p=finish;
while(p!=NULL)
{
Prinft(c,p);
p=p->next;
}
}
void insert(PCB *p)
{
PCB *S1,*S2;
if(ready==NULL)
{
p->next = NULL;
ready = p;
}
else
{
S1 = ready;
S2 = S1;
while(S1!=NULL)
{
if(S2->pri >= p->pri)
{
S2->next = p;
p->next = S1;
}
else
{
p->next = ready;
ready = p;
}
}
}
}
bool CTProcessOfPri()
{
PCB * Node;
cout
cin >>n;
for(int j = 0;j
{
Node = new PCB;
if(Node==NULL)
return false;
else
{
cout >Node->procname>>Node->needOftime; Node->runtime = 0;
Node->state ="就绪";
Node->pri =Node->needOftime;
cout procname
insert(Node);
}
return true;
}
void priority(int i)
{
run = ready;
ready = ready->next;
run->state = "运行";
Prinft(i);
while(run!=NULL)
{
run->runtime=run->runtime+1;
run->needOftime=run->needOftime-1;
run->pri=run->pri-1;
if(run->needOftime==0)
{
run->state = "完成";
run->next = finish;
finish = run;
run=NULL;
if(ready!=NULL)
{
run = ready;
run->state = "运行";
ready = ready->next;
}
}
else if((ready!=NULL)&&(run->pripri))
{
run->state="就绪";
insert(run);
run = ready;
run->state = "运行";
ready = ready->next;
}
display(i);
}
}
void queue(PCB *p)
{
if(ready==NULL)
{
p->next = NULL;
ready = p;
tial = p;
}
else
{
tial->next = p;
tial = p;
p->next = NULL;
}
}
bool CTProcessOfRuntime()
{
PCB * Node;
int m;
cout
cin >>n;
cout
cin >>m;
for(int j = 0;j
{
Node = new PCB;
if(Node==NULL)
return false;
else
{
cout >Node->procname>>Node->needOftime; Node->runtime = 0;
Node->state ="就绪";
Node->Counter = 0;
Node->pieceOftime = m;
cout procname
queue(Node);
}
return true;
}
void Runtime(int c)
{
run = ready;
ready = ready->next;
run->state = "运行";
Prinft(c);
while(run!=NULL)
{
run->runtime=run->runtime+1;
run->needOftime=run->needOftime-1;
run->Counter = run->Counter + 1;
if(run->needOftime==0)
{
run->state = "完成";
run->next = finish;
finish = run;
run = NULL;
if(ready!=NULL)
{
run = ready;
ready = ready->next;
}
}
else if(run->Counter == run->pieceOftime)
{
run->Counter = 0;
run->state = "就绪";
queue(run);
run=NULL;
if(ready!=NULL)
{
run = ready;
run->state = "运行";
ready = ready->next;
}
}
display(c);
}
}
int main()
{
int i;
cout
cout >i;
switch(i)
{
case 1:
CTProcessOfRuntime();
Runtime(i);
break;
case 2:
break;
}
return 0;
}
void Dtime(int t)
{
time_t current_time;
time_t start_time;
time(&start_time);
do
{
time(& current_time);
}while((current_time-start_time)
实验结果: