按照这个实验,我用C++写了一个程序,就是进程调度的程序。下面是代码:(很多注释,希望你们能够看懂)
#include<iostream>
#include<string>
#include<fstream>
#include<windows.h>
using namespace std;
struct PCB {
string name;//进程名
struct PCB *next;//指针
int time;//运行时间
int grade;//优先级
string state;//状态
};
PCB *head;//进程队列的头
int couter = 1;//计算第几次运行
char choose;//用来判断用户是否申请资源
void init();//初始化
void output();//打印进程调用的情况
void ReSort(PCB *p);//重新对进程队列排序
void Print();//打印
void ReadIn();
void WriteOut();
void Request();
int main() {
init();
output();
return 0;
}
void init() {
ReadIn();
Print();
system("pause");
}
void output() {
PCB *p = head;
system("cls");
cout<<"第"<<couter<<"次运行:"<<endl;
cout<<"当前运行的进程:"<<p->name<<endl;
//Print();
p->grade = p->grade - 1;//运行后优先级-1
p->time = p->time - 1; //运行后运行时间-1
couter++;
if(head->next != NULL && p->time != 0) {//如果不为尾节点,当前的运行时间不为0,把头节点独立出来,重新按优先级排序
// cout<<"重新排序之前:\n\n";
Print();
PCB *work = head;//把头节点独立出来
head = head->next;//将头指针指向第二节点
PCB *p = head;
PCB *temp = head;//用来保存上一节点
//按优先级,插入排序
while(p != NULL) {
if(p->grade >= work->grade && p->next == NULL) {//如果当前节点大于或者等于独立出来的节点,且当前只有一个节点
p->next = work;
work->next = NULL;
break;
} else if(p->grade >= work->grade && p->next->grade <= work->grade) { //如果当前进程的优先级大于或者等于独立出来的节点,且当前节点的下一个节点的优先级小于或者等于独立出来的节点
work->next = p->next;
p->next = work;
break;
} else if(p->grade < work->grade) { //如果当前节点比独立的节点要小
work->next = p;
head = work;
break;
}
temp = p;//保留当前节点
p = p->next;
}
//cout<<"重新排序之后:\n\n";
//Print();
}
else if(head->next == NULL && head->time != 0){//如果当前为最后一个进程,且运行时间不为0
Print();
}
else if(head->time == 0) {//如果当前进程的运行时间为0
if(head->next != NULL) { //如果当前不是最后一个结束的节点
head->state = "结束";
Print();
head = head->next;
} else if(head->next == NULL) { //如果当前为最后一个结束的点
head->state = "结束";
Print();
cout<<"所有进程均已完成!"<<endl;
exit(0);
}
}
if(choose != 'k') {
cout<<"你想申请新的进程吗?(y申请)/(n不申请)/(k不再申请)"<<endl;
cin>>choose;
if(choose == 'y' || choose == 'Y') {
ReSort(p);
}
}
output();//递归调用
}
void ReSort(PCB *p) {
PCB *temp = new PCB;
temp->next = NULL;
cout<<"请输入进程名:";
cin>>temp->name;
cout<<"请输入进程运行时间:";
cin>>temp->time;
cout<<"请输入进程优先级:";
cin>>temp->grade;
temp->state = "就绪";
//重新排序
p = head;
PCB *last = head;//用来保存上一节点
while(p != NULL) {
if(p->grade >= temp->grade && p->next->grade <= temp->grade) {
temp->next = p->next;
p->next = temp;
break;
} else if(p->grade >= temp->grade && p->next == NULL) {
p->next = temp;
temp->next = NULL;
break;
} else if(p->grade < temp->grade) {
temp->next = p;
head = temp;
break;
}
last = p;//保留当前节点
p = p->next;
}
//调试
Print();//打印
WriteOut();
system("pause");
}
void ReadIn() {
ifstream inputfile;
inputfile.open("进程调度.txt");
head = new PCB;
PCB *p = head;
while(inputfile) {
inputfile>>p->name>>p->time>>p->grade>>p->state;
p->next = NULL;
if(inputfile.peek() == EOF) break;//判断文件尾巴字符,到了尾巴,退出循环
p->next = new PCB;
p = p->next;
}
}
void WriteOut() {
PCB *p = head;
ofstream outputfile;
outputfile.open("进程调度.txt");
while(p != NULL) {
// if(p->next != NULL)outputfile<<p->name<<" "<<p->time<<" "<<p->grade<<" "<<p->state<<"\n";
// else outputfile<<p->name<<" "<<p->time<<" "<<p->grade<<" "<<p->state;
outputfile<<p->name<<" "<<p->time<<" "<<p->grade<<" "<<p->state;
if(p->next != NULL)outputfile<<"\n";
p = p->next;
}
}
void Print() {
//system("cls");
cout<<"------------------------------------------------------------"<<endl;
cout<<"当前进程队列\n";
PCB *p = head;
cout<<"进程名字 运行时间 优先级数 进程状态\n" ;
while(p != NULL) {
cout<<p->name<<" ";
cout<<p->time<<" ";
cout<<p->grade<<" ";
cout<<p->state<<endl;
p = p->next;
}
cout<<"------------------------------------------------------------"<<endl<<endl;
cout<<"正在进入运行下一个进程。。。。\n\n";
Sleep(1500);
}