#include
#include
#include
using namespace std;
/*定义进程控制块*/
typedef struct pcb
{
char name; //进程名
struct pcb *next; //链接指针
int arrivetime; //到达时间
int runtime; //估计运行时间
char state; //进程状态(就绪R和完成C)
int rank; //估计运行时间长短排序
}PCB;
/*进程创建函数*/
void initPCB(PCB *P) {
cout << "\nPlease input current PCB's name:";
cin >> P->name;
cout << "\nPlease input this PCB's arrivetime:";
cin >> P->arrivetime;
cout << "\nPlease input this PCB's runtime:";
cin >> P->runtime;
P->state = 'R';
}
/*进程运行函数*/
void runPCB(PCB *P, int& time) {
int tTime; //定义每个进程的周转时间
while (time<P->arrivetime) {
time++;
}
tTime = time - P->arrivetime;
cout << "Time slice is " << time << ",Process " << P->name << " start," << endl;
while (P->runtime > 0) {
P->runtime--;
time++;
}
cout << "Time slice is " << time << ",Process " << P->name << " end." << endl;
cout << "Process " << P->name << "'s turnaround time is " << tTime << endl;
cout << "The remaining processes in the ready queue :" ;
P->state = 'C'; //程序置为完成态
//输出等待队列里面剩余进程的进程名。
while (P->next != NULL) {
cout << P->next->name << " ";
P = P->next;
}
cout << endl;
cout << endl;
}
///*进程打印函数*/
//void printPCB(PCB *P) {
// cout << P->name << "周转时间为:" << endl;
//}
int Time = 0;
int time = 0;
int main() {
PCB *head; //队首指针
int process_num = 0, i, j;
PCB P[5];
PCB *PCB_line[5];
for (i = 0; i < 5; i++)
{
PCB_line[i] = &P[i];
}
cout << "开始进程创建~" << endl;
for (; process_num < 5; process_num++) {
cout << "创建第" << process_num + 1 << "个程序!" << endl;
initPCB(PCB_line[process_num]);
cout << "创建成功!" << endl;
}
cout << "所有进程创建结束!" << endl;
cout << "当进程到达处理机时:" << endl;
//用插入法创建队列,将它们按到达时间(arrivetime)从小到大排列
PCB *temp;
//用3个for循环将创建的5个进程按照到达时间(arrivetime)从小到大进行排序
for (i = 0; i < 4; i++) {
for (j = 0; j < 4 - i; j++) {
/*if (PCB_line[j]->runtime > max->runtime) {
max = PCB_line[j];
}*/
if (PCB_line[j]->arrivetime > PCB_line[j + 1]->arrivetime)eEWWW
{
temp = PCB_line[j];
PCB_line[j] = PCB_line[j + 1]; //交换指针排序
PCB_line[j + 1] = temp;
}
}
}
head = PCB_line[0];
for (i = 0; i < 5-1; i++) {
PCB_line[i]->next = PCB_line[i + 1]; //将所有链表链接在一起
}
PCB_line[4]->next= NULL;
//进程运行
process_num = 0;
while (process_num < 5) {
process_num++;
cout << "第" << process_num << "个程序进入处理机。" << endl;
time++;
runPCB(PCB_line[process_num-1],time);
}
getchar();
getchar();
return 0;
}
进程名 | 进程达到时间 | 进程运行时间 |
---|---|---|
A | 5 | 11 |
B | 4 | 11 |
C | 3 | 11 |
D | 2 | 11 |
E | 1 | 11 |
记2019年双十一,为什么我的女朋友还没来呢!(┬_┬)
不管了~
Happy solo 耶!