欢迎批评指正
#include <iostream>
#include <fstream>
#include<time.h>
using namespace std;
bool valid = false;
struct PCB {
int ID, Priority;
double StartBlock, StartTime;
double CPUTime, ArrivalTime, AllTime;
double TravelTime, AverageTTime;
char State;
PCB* Next;
}pcb;
PCB* front = new PCB(); int n;
ofstream out("Input.txt");
PCB* FCFS(int i) {
int min = 999999;
PCB* tmp = front->Next; PCB* tmp2 = new PCB();
valid = false;
while (tmp->Next != 0) {
if ((tmp->ArrivalTime <= (double)i) && tmp->AllTime > 0 && tmp->State == 'R' && tmp->ArrivalTime <min) {
min = tmp->ArrivalTime;
tmp2 = tmp;
valid = true;
}tmp = tmp->Next;
}
return tmp2;
}
PCB* SJF(int i) {
int min = 999999; valid = false;
PCB* tmp = front->Next; PCB* tmp2 = new PCB();
while (tmp->Next != 0) {
if ((tmp->ArrivalTime <= (double)i) && tmp->AllTime > 0 && tmp->State == 'R' && tmp->AllTime < min) {
min = tmp->AllTime;
tmp2 = tmp;
valid = true;
}tmp = tmp->Next;
}
return tmp2;
}
PCB* RR(int i) {
int count = 0;
int t = i % n; valid = false;
PCB* tmp = front->Next;
while (tmp->Next != 0) {
if ((tmp->ArrivalTime <= (double)i) && tmp->AllTime > 0 && tmp->State == 'R') {
count++;
if (count == t) {
valid = true;
break;
}
}
tmp = tmp->Next;
}
return tmp;
}
PCB* Priority(int i) {
PCB* tmp = front->Next; valid = false;
PCB* tmp2=new PCB();
int max = 0;
while (tmp->Next != 0) {
if ((tmp->ArrivalTime <= (double)i) && tmp->AllTime > 0 && tmp->State == 'R' && tmp->Priority > max) {
max = tmp->Priority;
tmp2 = tmp;
valid = true;
}tmp = tmp->Next;
}
return tmp2;
}
void Input() {
int i;
cout << "进程数目: " << endl;
out<< "进程数目: " << endl;
n = (1 + rand() % 10);
out << n << endl;
PCB* tmp = new PCB();
front->Next = tmp;
cout << "依次输入进程ID、优先权、到达时间、需要运行时间" << endl;
out << "依次输入进程ID、优先权、到达时间、需要运行时间" << endl;
for (i = 0; i < n; i++) {
tmp->ID = i + 1;
tmp->Priority = (rand() % 300 + 1);
tmp->ArrivalTime = (1.0 * (rand() % 10) + 1);
tmp->AllTime = (1.0*(rand() % 10) + 1);
tmp->CPUTime = 0; tmp->State = 'R';
out << " tmp->ID: " << tmp->ID << " tmp->Priority: " << tmp->Priority << " tmp->ArrivalTime: "
<< tmp->ArrivalTime << " tmp->AllTime: " << tmp->AllTime << " tmp->CPUTime: " << tmp->CPUTime << endl;
PCB* tmp2 = tmp;
tmp= new PCB();
tmp2->Next = tmp;
}
}
void Output() {
PCB* tmp = front->Next;
while (tmp->Next != 0) {
cout<<"进程ID:" << tmp->ID << ' ' <<"进程状态:"<<tmp->State << ' '<<"进程优先权: " << tmp->Priority << ' ' <<"进程到达时间 "<<
tmp->ArrivalTime << ' ' <<"进程还需执行时间(片): "<< tmp->AllTime << ' ' << "已用CPU时间:" << tmp->CPUTime << ' ' << "距离阻塞时间:" << tmp->StartBlock << ' ' << "阻塞状态到开始时间:" << tmp->StartTime << ' ' << endl;
out<<"进程ID:" << tmp->ID << ' ' << "进程状态:" << tmp->State << ' ' << "进程优先权: " << tmp->Priority << ' ' << "进程到达时间 " <<
tmp->ArrivalTime << ' ' << "进程还需执行时间(片): " << tmp->AllTime << ' ' << "已用CPU时间:" << tmp->CPUTime << ' ' << "距离阻塞时间:" << tmp->StartBlock << ' ' << "阻塞状态到开始时间:" << tmp->StartTime << ' ' << endl;
cout <<"tmp->TravelTime:" << tmp->TravelTime << "tmp->AverageTTime:" << tmp->AverageTTime << endl;
out << "tmp->TravelTime:" << tmp->TravelTime << " tmp->AverageTTime:" << tmp->AverageTTime << endl;
tmp = tmp->Next;
}
}
void Cal() {
PCB* tmp = front->Next;
int count = 0;
double m = 0, n = 0;
while (tmp->Next != 0) {
m += tmp->TravelTime;
n += tmp->AverageTTime;
count++;
tmp = tmp->Next;
}
cout << endl << endl << "周转时间:" << m / count << endl;
out<< endl << endl << "周转时间:" << m / count << endl;
cout << "平均周转时间:" << n / count << endl;
out << endl << endl << "平均周转时间:" << n / count << endl;
}
void adjust(int n) {
int j=1, count = 0;
bool tag;
PCB* tmp = front->Next;
PCB* tmp2=new PCB();
for (int i = 1;; i++) {
cout << "时间片:" <<i<< endl;
out<<"时间片:" << i << endl;
tmp2 = Priority(i);
cout << "正在执行的进程:ID:" << tmp2->ID << endl << "进程状态" << tmp2->State << endl
<< "进程优先权 " << tmp2->Priority << endl << "进程到达时间 " <<
tmp2->ArrivalTime << endl << "进程还需执行时间(片) " << tmp2->AllTime << endl;
out<< "正在执行的进程:ID:" << tmp2->ID << endl << "进程状态" << tmp2->State << endl
<< "进程优先权 " << tmp2->Priority << endl << "进程到达时间 " <<
tmp2->ArrivalTime << endl << "进程还需执行时间(片) " << tmp2->AllTime << endl;
cout << "已用CPU时间:" << tmp2->CPUTime << endl << "距离阻塞时间" << tmp2->StartBlock << endl << "阻塞状态到开始时间" << tmp2->StartTime << endl << endl;
out << "已用CPU时间:" << tmp2->CPUTime << endl << "距离阻塞时间" << tmp2->StartBlock << endl << "阻塞状态到开始时间" << tmp2->StartTime << endl << endl;
if (valid) {
tmp2->Priority -= 3;
if (tmp2->Priority <= 0)
tmp2->Priority = 1;
cout << "进程是否有I/O或其他事件进入?: (有输入1,无输入0)" << endl;
out<< "进程是否有I/O或其他事件进入?: (有输入1,无输入0)" << endl;
tag = rand() % 2;
out << "tag: " << tag << endl;
if (tag) {
cout << "输入距离进入阻塞时间,和阻塞进入就绪时间:" << endl;
tmp2->StartBlock = 1.0 * rand()/RAND_MAX;
tmp2->StartTime = 1.0*(rand() % 5) + 1;
out << "tmp2->StartBlock" << tmp2->StartBlock << "tmp2->StartTime" << tmp2->StartTime << endl;
tmp2->State = 'C';
tmp2->CPUTime += tmp2->StartBlock;
tmp2->StartTime -= (1 - tmp2->StartBlock);
tmp->StartBlock = 0;
}
else {
tmp2->CPUTime += 1.0;
tmp2->AllTime -= 1.0;
tmp2->StartBlock = 0;
tmp2->StartBlock = 0;
}
if (tmp2->AllTime <= 0) {
tmp2->State = 'F';
count++;
tmp2->TravelTime = (double)i - tmp2->ArrivalTime;
tmp2->AverageTTime = tmp2->TravelTime / n;
if (count == n)return;
}
}
PCB* tmp3 = new PCB();
tmp3 = front->Next;
while (tmp3&&tmp3!=tmp2)
{
if (tmp3->State == 'C') {
tmp3->StartTime -= 1;
if (tmp3->StartTime <= 0) {
tmp3->StartTime = 0;
tmp3->State = 'R';
}
}
tmp3 = tmp3->Next;
}
Output();
}
}
int main()
{
srand(time(0));
Input();
adjust(n);
Output();
Cal();
return 0;
}