|
| 1 | +#include "cpuscheduler.h" |
| 2 | + |
| 3 | +CPUScheduler::CPUScheduler(QObject *parent) : QObject(parent) |
| 4 | +{ |
| 5 | + |
| 6 | +} |
| 7 | + |
| 8 | +void CPUScheduler::newProcess(PCB* newP) |
| 9 | +{ |
| 10 | + if(readyQueue->size() < 6) |
| 11 | + readyQueue->push(*newP); |
| 12 | + else |
| 13 | + jobQueue->push(*newP); |
| 14 | +} |
| 15 | + |
| 16 | +void CPUScheduler::newProcess(string PID,int time,int priority,string father) |
| 17 | +{ |
| 18 | + PCB* newP = new PCB(PID,time,priority,father); |
| 19 | + if(readyQueue->size() < 6) |
| 20 | + readyQueue->push(*newP); |
| 21 | + else |
| 22 | + { |
| 23 | + if(father == "") |
| 24 | + jobQueue->push(*newP); |
| 25 | + else |
| 26 | + { |
| 27 | + queue<PCB>* tempQ = new queue<PCB>; |
| 28 | + tempQ->push(*newP); |
| 29 | + while(!jobQueue->empty()) |
| 30 | + { |
| 31 | + tempQ->push(jobQueue->front()); |
| 32 | + jobQueue->pop(); |
| 33 | + } |
| 34 | + queue<PCB>* a = jobQueue; |
| 35 | + jobQueue = tempQ; |
| 36 | + delete a; |
| 37 | + } |
| 38 | + |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +void CPUScheduler::exec1() |
| 43 | +{ |
| 44 | + if(readyQueue->empty()) |
| 45 | + return; |
| 46 | + PCB tempE = readyQueue->top(); |
| 47 | + if(tempE.suspend) |
| 48 | + return; |
| 49 | + cpu->exec1(tempE); |
| 50 | + readyQueue->pop(); |
| 51 | + |
| 52 | + //其余进程优先度增加 |
| 53 | + int inc = 1; |
| 54 | + int size = (int)readyQueue->size(); |
| 55 | + priority_queue<PCB>* tempQ = new priority_queue<PCB>; |
| 56 | + for(int i = 0;i < size;i++) |
| 57 | + { |
| 58 | + PCB temp2 = readyQueue->top(); |
| 59 | + if(temp2.suspend) |
| 60 | + { |
| 61 | + tempQ->push(temp2); |
| 62 | + readyQueue->pop(); |
| 63 | + continue; |
| 64 | + } |
| 65 | + if(temp2.priority + inc <= 140) |
| 66 | + { |
| 67 | + temp2.priority += inc; |
| 68 | + temp2.priority_pre += inc; |
| 69 | + } |
| 70 | + else |
| 71 | + { |
| 72 | + temp2.priority = 140; |
| 73 | + temp2.priority_pre = 140; |
| 74 | + } |
| 75 | + |
| 76 | + tempQ->push(temp2); |
| 77 | + readyQueue->pop(); |
| 78 | + } |
| 79 | + priority_queue<PCB>* a; |
| 80 | + a = readyQueue; |
| 81 | + readyQueue = tempQ; |
| 82 | + delete a; |
| 83 | + |
| 84 | + //进程未完成push回 已完成push历史队列 |
| 85 | + if(tempE.time > 0) |
| 86 | + { |
| 87 | + readyQueue->push(tempE); |
| 88 | + } |
| 89 | + else |
| 90 | + { |
| 91 | + //运行完毕队列 |
| 92 | + free(tempE.father); |
| 93 | + historyQueue->push_back(tempE); |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +void CPUScheduler::free(string fatherPID) |
| 98 | +{ |
| 99 | + priority_queue<PCB>* tempQ = new priority_queue<PCB>; |
| 100 | + |
| 101 | + while(!readyQueue->empty()) |
| 102 | + { |
| 103 | + PCB tempP = readyQueue->top(); |
| 104 | + if(tempP.PID == fatherPID) |
| 105 | + { |
| 106 | + tempP.running_children--; |
| 107 | + qDebug()<<"father --"; |
| 108 | + } |
| 109 | + tempQ->push(tempP); |
| 110 | + readyQueue->pop(); |
| 111 | + } |
| 112 | + priority_queue<PCB>* a = readyQueue; |
| 113 | + readyQueue = tempQ; |
| 114 | + delete a; |
| 115 | +} |
0 commit comments