本篇文章是c++实现时间片轮转法,在这个算法中,任务数是随机的,但是因为用到排序算法,所以限制了任务数的大小,同时因为只是一个实现,所以也对到达时间进行了限制,不至于无限长时间之后才有任务到达,如果要模拟实际,可以把取模的大小更改,然后预留了优先级的位置,但在本算法中没有考虑优先级,因为后期会对优先级+时间轮转法进行编写,本算法只实现基本轮转
/////pcb.h///
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <string>
using namespace std;
#define N 10
struct pcb
{
string pcbname;
int cometime;
int runtime;
char rate;
bool fl;
pcb *address;
pcb *next;
};
struct pcblist
{
pcb* head;
pcb* running;
pcb* next;
pcb* ending;
pcb* comming;
pcb* rear;
};
////
int rands();
///
void createlist(pcblist *t,int num);
void setcomelist(pcblist *t, int num, int &mask);
void QuickSort(pcb *E, int s, int t);
void operation(pcblist *t, int num,int mask);
///
void show(pcblist *t);
////pcb.cpp///
#include "pcb.h"
int rands()//产生随机数
{
int num;
num = rand() % N+1;
if (num == 0)num = 1;
return num;
}
void createlist(pcblist *t,int num)//创建进程链表
{
int n = 0;
pcb *i,*u=t->next;
while (n < num)
{
//进程的名字
char c = '0' + n;
string s = "process";
s += c;
i = new pcb();
int n1 = rands();
int n2 = rands();
i->fl = true;
i->pcbname = s;
//进程的到达时间
i->cometime = n1;
//进程的运行时间
i->runtime = n2;
//进程的运行状态
i->rate = 'W';
//进程的地址
i->address = i;
i->next = NULL;
//cout<<
if (u == NULL){ t->next = i; u = t->next; }
else { u->next = i; u = i; }
n++;
}
}
void setcomelist(pcblist *t, int num,int &mask)
{
int oo = 0;
pcb data[N];
pcb *j = t->next,*j2=t->comming=NULL,*j3;
while (j)
{
data[oo] = *j;
oo++;
j = j->next;
}
QuickSort(data, 0, num);
cout << "pcbname" << "\t\t" << "cometime "<< "\t" << "runtime" << "\t" << "address" <<"\t\t"<<"rate"<< "\n";
for (int a1 = 1; a1 <= num; a1++)
{
j3 = new pcb;
j3->pcbname = data[a1].pcbname;
j3->cometime = data[a1].cometime;
j3->runtime = data[a1].runtime;
j3->address = data[a1].address;
mask += j3->runtime;
j3->rate = data[a1].rate;
j3->next = NULL;
if (j2 == NULL){ t->comming = j3; j2 = t->comming; }
else { j2->next = j3; j2 = j3; }
cout <<data[a1].pcbname<<"\t"<< data[a1].cometime << "\t\t"<<data[a1].runtime<<"\t"<<data[a1].address<<"\t"<<data[a1].rate<<"\n";
}
}
void operation(pcblist *t, int num,int mask)
{
cout << "运行时刻\tpc_name pc_addr runtime\tnext_pc next_pcaddr";
int clock=0,cnum=0,g=0;
t->head->next = t->rear;
t->running = NULL;
pcb *h1, *h2=t->ending,*x;
while (cnum < num)
{
clock++; cout <<"\n* "<< clock << " *\t\t";
if (t->comming)
{
while (t->comming->cometime == clock)
{
h1 = t->comming;//q = coming_qu.front();
t->comming = t->comming->next;//coming_qu.pop();
//t->rear = h1; //running_qu.push(q);
x = t->head;while (x->next != t->rear)x = x->next;
x->next = h1; h1->next = t->rear;
if (t->comming == NULL) break;//if (coming_qu.empty()) break;
}
}
if (t->running)
{
if (t->running->runtime > 0)//if (p.running_time > 0)
{
//running_qu.push(p);
x = t->head; while (x->next != t->rear)x = x->next;
x->next = t->running; t->running->next = t->rear;
}
else
{
t->running->rate = 'C';
h2->next = t->running;
h2 = h2->next;
cnum++;
h2->next = NULL;
}
t->running = NULL;
}
if (t->head->next != t->rear)//if (!running_qu.empty())
{
t->running = t->head->next;//p = running_qu.front();
//x = t->running->next;
//
cout << t->running->pcbname << " " << t->running->address<<" "<<t->running->runtime;
if (t->running->next == t->rear)cout << "\t\t no pc";
else cout << "\t\t" << t->running->next->pcbname << " " << t->running->next->address;
//
t->head->next = t->head->next->next;//running_qu.pop();
t->running->runtime--;//p.running_time--;
g++;
}
//cout << g << endl;
if (g == mask)break;
}
}
void QuickSort(pcb *E, int s, int t)
{
int i = s, j = t;
pcb r;
if (s<t)
{
r = E[s];//把这区间的第一元素给 r ,让 r 成为基准
while (i != j)
{
while (j>i&&E[j].cometime >= r.cometime) j--;//从后面往前面遍历,直到找到比头 标准r 小的
E[i] = E[j];
while (i<j&&E[i].cometime<r.cometime) i++; //从前面往前面遍历,直到找到比头 标准r 大的
E[j] = E[i];
}
E[i] = r;
QuickSort(E, s, i - 1);//对左区间递归排序
QuickSort(E, i + 1, t); //对右区间递归排序
}
}
void show(pcblist *t)
{
cout << endl;
pcb* i = t->next; pcb *s;
while (i)
{
//cout << "1111";
s = i;
//cout << i->pcbname << " " <<i->cometime<< " "<<i->address<<endl;
i = i->next;
delete s;
}
i = t->comming;
while (i)
{
//cout << "0000";
s = i;
//cout << i->pcbname << " " << i->cometime << " " << i->address << endl;
i = i->next;
delete s;
}
delete t;
}
///main///
#include "pcb.h"
int main()
{
pcblist *t;
t = new pcblist;
t->next = NULL;
t->head = new pcb;
t->head->pcbname = "root";
t->head->next = NULL;
t->ending = new pcb;
t->ending->pcbname = "finash";
t->ending->next = NULL;
t->rear = new pcb;
t->rear->pcbname = "end";
t->rear->next = NULL;
pcb data[N];
srand((unsigned)time(0));
int mask=0;
int snum = rands();
createlist(t, snum);
setcomelist(t, snum,mask);
operation(t, snum,mask);
show(t);
system("pause");
}