|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include <iostream>
#include<ctime>
using namespace std;
int Num; //定义总进程数
int Time = 0; //定义系统时间
int slice = 1; //定义时间片长度
#define MAXSIZE 10 //定义就绪队列最大长度
#define false 0
#define true 1
typedef struct PCB
{
char name;
int arrivetime;
int runtime;
char state;//
}PCB;
typedef struct
{
PCB* base;
int front;
int rear;
}LinkQueue;
void initqueue(LinkQueue L)
{
if (!L.base)
exit(1);
L.front = L.rear = 0;
}
void init(PCB p[])
{
for (int i = 0; i < Num; i++)
{
p[i].name = 'A' + i;
p[i].arrivetime = rand() % 8 + 1;
p[i].runtime = rand() % 5 + 1;
p[i].state = 'R';
}
}
void print(PCB p[])
{
cout << "进程名\t" << "到达时间\t" << "估计运行时间\t" << "进程状态" << endl;
for (int i = 0; i < Num; i++)
{
cout << p[i].name << "\t" << p[i].arrivetime << "\t\t" << p[i].runtime << "\t\t" << p[i].state << endl;
}
}
void sort(PCB p[])//插入排序
{
for (int i = 1; i < Num; i++)
{
PCB key = p[i];
int j = i - 1;
while ((j >= 0) && (key.arrivetime < p[j].arrivetime))
{
p[j + 1] = p[j];
j--;
}
p[j + 1] = key;
}
}
void show(LinkQueue& L)
{
while (1)
{
if (L.front == L.rear)
{
cout << "队列为空" << endl;
break;
}
else
{
cout << "进程名" << L.base[L.front].name << "\t" << "到达时间" << L.base[L.front].arrivetime << "\t"
<< "运行时间" << L.base[L.front].runtime << "\t" << "进程状态" << L.base[L.front].state << endl;
L.front = (L.front + 1) % MAXSIZE;
}
}
}
void time_slice(LinkQueue& L, PCB p[])
{
int count = 0;
int* record = new int[Num];
for (int i = 0; i < Num; i++)
{
record[i] = false;
}
while (count != Num)
{
for (int i = 0; i < Num; i++)
{
if (Time >= p[i].arrivetime && record[i] == false)
{
L.base[L.rear] = p[i];
L.rear = (L.rear + 1) % MAXSIZE;
record[i] = true;
}
}
if (L.front == L.rear)//队列为空
{
cout << "*************************系统时间为" << Time << "************************" << endl;
cout << "就绪队列为空" << endl;
Time+=slice;
}
else
{
if (L.base[L.front].runtime == slice)
{
cout << "*************************系统时间为" << Time << "************************" << endl;
cout << "当前运行进程是" << L.base[L.front].name << ",即将运行完成,估计运行时间由"
<< L.base[L.front].runtime <<"变为"<<L.base[L.front].runtime-1<< endl;
L.base[L.front].state = 'F';
Time += slice;
L.base[L.front].runtime--;
show(L);
L.front = (L.front + 1) % MAXSIZE;//出队
count++;
}
else if (L.base[L.front].runtime != slice)
{
cout << "*************************系统时间为" << Time << "************************" << endl;
cout << "当前运行进程是" << L.base[L.front].name <<",估计运行时间由"
<< L.base[L.front].runtime << "变为" << L.base[L.front].runtime - 1 << endl;
L.base[L.front].state = 'r';
Time += slice;
L.base[L.front].runtime--;
show(L);
L.base[L.front].state = 'R';
L.base[L.rear] = L.base[L.front];//队头元素放进队尾
L.front = (L.front + 1) % MAXSIZE;//队头加一
L.rear = (L.rear + 1) % MAXSIZE;//队尾加一
}
}
}
}
int main()
{
LinkQueue L;
srand(unsigned int(time(NULL)));
Num = rand() % 3 + 5;
PCB* p = new PCB[Num];
L.base = new PCB[MAXSIZE];
initqueue(L);
init(p);
print(p);
sort(p);
print(p);
time_slice(L, p);
system("pause");
return 0;
}
L.base[L.rear] = p[i];这一行出现问题,写入访问权限冲突 |
|