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
|
#include <cstdlib>
#include <iostream>
#include <deque>
#include <queue>
using namespace std;
/*
*
*/
int main(int argc, char** argv)
{
int ps[9][19] = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 4, 15, 5, 31, 6, 26, 7, 24, 6, 41, 4, 51, 5, 16, 4, 0, 0, 0, 0 }, // p1
{ 9, 28, 11, 22, 15, 21, 12, 28, 8, 34, 11, 34, 9, 29, 10, 31, 7, 0, 0 }, // p2
{ 24, 28, 12, 21, 6, 27, 17, 21, 11, 54, 22, 31, 18, 0, 0, 0, 0, 0, 0 }, // p3
{ 15, 35, 14, 41, 16, 45, 18, 51, 14, 61, 13, 54, 16, 61, 15, 0, 0, 0, 0 }, // p4
{ 6, 22, 5, 21, 15, 31, 4, 26, 7, 31, 4, 18, 6, 21, 10, 33, 3, 0, 0 }, // p5
{ 22, 38, 27, 41, 25, 29, 11, 26, 19, 32, 18, 22, 6, 26, 6, 0, 0, 0, 0 }, // p6
{ 4, 36, 7, 31, 6, 32, 5, 41, 4, 42, 7, 39, 6, 33, 5, 34, 6, 21, 9 }, // p7
{ 5, 14, 4, 33, 6, 31, 4, 31, 6, 27, 5, 21, 4, 19, 6, 11, 6, 0, 0 } }; // p8
deque<int> burst[9], iotime[9], remainio[9];
// init burst and iotime
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 19; j = j + 2)
{
if (ps[i][j] != 0)
{
burst[i].push_back(ps[i][j]);
iotime[i].push_back(ps[i][j + 1]);
remainio[i].push_back(ps[i][j + 1]);
}
}
}
// process in ready queue and io queue
deque<int> ready, io;
for (int i = 1; i < 9; i++)
{
ready.push_back(i);
}
int currTime = 0;
int currBurst = 0;
int passTime = 0;
int running; // running process
int runningBurst = 0; // burst of running process
int runningIO; // iotime of runnign process
int count = 0;
while (ready.size() != 0 || io.size() != 0)
{
cout << "Current Time: " << currTime << endl;
// Get a running process from ready queue
running = ready.front(); ready.pop_front();
// Get burstime
runningBurst = burst[running].front(); burst[running].pop_front();
// Get iotime
runningIO = iotime[running].front(); iotime[running].pop_front();
// Shown running process
cout << "\nNow running: P" << running << endl;
cout << "..................................................\n" << endl;
// Show the other queue in ready with burst time
cout << "Ready Queue: Process Burst" << endl;
for (int i = 0; i < ready.size(); i++)
{
int rp = ready.at(i); //cout << "Debug:" << rp << endl;
int burstTime = burst[rp].front();
cout << " P" << rp << " " << burstTime << endl;
}
// Show IO queue
cout << "Now in I/O: Process Remaining I/O time" << endl;
if (io.size() == 0)
cout << " [empty]" << endl;
else
{
for (int i = 0; i < io.size(); i++)
{
int iop = io.at(i);
int remainTime = remainio[iop].front(); //
cout << " P" << iop << " " << remainTime << endl;
}
}
// update ready queue, io queue
// for each process in io queue, update remain time
// if remain time is < 0, push new process in ready
int size = io.size();
for (int i = 0; i < size; i++)
{
int iop = io.front(); io.pop_front();
int remainTime = remainio[iop].front(); remainio[iop].pop_front();//
remainTime -= runningBurst;
if (remainTime > 0) // still in io queue
{
io.push_back(iop);
remainio[iop].push_front(remainTime);
}
else // push new process in ready
{
ready.push_back(iop);
}
}
cout << "\n.................................................." << endl;
cout << "..................................................\n" << endl;
// push running process to I/O queue
io.push_back(running);
// update current time
currTime += runningBurst;
}
cout << "Total Time: " << currTime << endl;
cout << "CPU Utiliation: " << endl;
cout << "Waiting Times: " << endl;
cout << "Turnaround Times: " << endl;
cout << "Average Turnaround: " << endl;
cout << "Response Times: " << endl;
cout << "Average Response: " << endl;
return 0;
}
|