1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
| #include<queue> #include<iostream> #include<iomanip> using namespace std; typedef int ID; typedef int Priority; typedef int Time;
enum State { sready, sblocked, sruning, sstop }; struct PCB{ ID id; Priority priority; Time cpu_time; Time all_time; Time start_block; Time block_time; State state; public: PCB( ID id0, Priority priority0, Time cpu_time0, Time all_time0, Time start_block0, Time block_time0, State state0 ): id(id0), priority(priority0), cpu_time(cpu_time0), all_time(all_time0), start_block(start_block0), block_time(block_time0), state(state0) { } };
struct cmp { bool operator() (PCB* a, PCB* b) { return a->priority < b->priority; } }; typedef priority_queue<PCB*, vector<PCB*>, cmp> mqueue; const int width = 10; void show_PCB(PCB* pcb) { string state = ""; switch(pcb->state) { case sready: state = "ready"; break; case sblocked : state = "blocked";break; case sruning : state = "runing";break; case sstop: state = "stop";break; defalut:state="unknown";break; } cout << left << setw(width) << setfill(' ') << pcb->id << "|" << left << setw(width) << setfill(' ') << pcb->priority << "|" << left << setw(width) << setfill(' ') << pcb->cpu_time << "|" << left << setw(width) << setfill(' ') << pcb->all_time << "|" << left << setw(width) << setfill(' ') << pcb->start_block << "|" << left << setw(width) << setfill(' ') << pcb->block_time << "|" << left << setw(width) << setfill(' ') << state << "|" << endl; } void show_queue(queue<PCB*> q){ cout << left << setw(7*width+7) << setfill('-') << "" << "\n" << left << setw(width) << setfill(' ') << "id" << "|" << left << setw(width) << setfill(' ') << "priority" << "|" << left << setw(width) << setfill(' ') << "cpuTime" << "|" << left << setw(width) << setfill(' ') << "allTime" << "|" << left << setw(width) << setfill(' ') << "startBlock" << "|" << left << setw(width) << setfill(' ') << "blockTime" << "|" << left << setw(width) << setfill(' ') << "state" << "|" << endl; while(!q.empty()) { PCB* t = q.front(); q.pop(); show_PCB(t); } cout << left << setw(7*width+7) << setfill('-') << "" << "\n"; } void show_queue(mqueue q){ cout << left << setw(7*width+7) << setfill('-') << "" << "\n" << left << setw(width) << setfill(' ') << "id" << "|" << left << setw(width) << setfill(' ') << "priority" << "|" << left << setw(width) << setfill(' ') << "cpuTime" << "|" << left << setw(width) << setfill(' ') << "allTime" << "|" << left << setw(width) << setfill(' ') << "startBlock" << "|" << left << setw(width) << setfill(' ') << "blockTime" << "|" << left << setw(width) << setfill(' ') << "state" << "|" << endl; while(!q.empty()) { PCB* t = q.top(); q.pop(); show_PCB(t); } cout << left << setw(7*width+7) << setfill('-') << "" << "\n"; } queue<PCB*> finished; void run_a_time(mqueue& ready,mqueue& blocked,PCB* runing) { mqueue t_ready, t_blocked; runing = ready.top(); ready.pop(); runing->priority -= 3; runing->cpu_time += 1; runing->all_time -= 1; runing->start_block -= 1; if(runing->start_block == 0) { t_blocked.push(runing); runing->state = sruning; } else { if(runing->all_time > 0) { t_ready.push(runing); } else { finished.push(runing); runing->state = sstop; } } mqueue t_queue = blocked; while(!t_queue.empty()) { PCB* t = t_queue.top(); t_queue.pop(); t->block_time -= 1; if(t->block_time == 0) { t_ready.push(t); t->block_time = 0; t->start_block = -1; t->state = sready; } else { t_blocked.push(t); t->state = sblocked; } } t_queue = ready; while(!t_queue.empty()) { PCB* t = t_queue.top(); t_queue.pop(); t->priority += 1; t->start_block -= 1; if(t->start_block == 0) { t_blocked.push(t); t->state = sblocked; } else { t_ready.push(t); t->state = sready; } } ready = t_ready; blocked = t_blocked; } int main() { mqueue ready, blocked; PCB* runing = nullptr; PCB* pcbs[] = { new PCB(0,9,0,3,2,3, sready), new PCB(1,38,0,3,-1,0, sready), new PCB(2,30,0,6,-1,0, sready), new PCB(3,29,0,3,-1,0, sready), new PCB(4,0,0,4,-1,0, sready) }; int pcb_num = sizeof(pcbs)/sizeof(PCB*); for(int i = 0; i < pcb_num; i++) { ready.push(pcbs[i]); } show_queue(ready); int clock_ = 1; while(!ready.empty() || !blocked.empty()) { cout << "clock = " << clock_ << endl; run_a_time(ready, blocked, runing); cout << "ready queue:" << endl; show_queue(ready); cout << "blocked queue:" << endl; show_queue(blocked); clock_++; } show_queue(finished); for(int i = 0; i < pcb_num; i++) { delete pcbs[i]; } }
|