I make a program to simulate the Shortest Job First algorithm in CPU scheduling ..
this function will calculate the start , end, turn,and wait time for each proccess according to Non-preemptive way .. the 1st condition gonna check for the 1st arrival time or the smallest burst time .. I go some trouble here with handling the condition .. i just need some help to show me the right way to handle the condition .. thanks
vector<timeSlote> SJF_Np(vector<process> v)
{
vector<timeSlote> t;
timeSlote ts;
int temp,temp1;
int turn,wait;
int totalwait = 0;
int totalturn = 0;
int currentTime = 0;
for (unsigned int i = 0; i<v.size(); i++)
{
for (unsigned int j = 1; j <= v.size(); j++)
{
if (v.at(i).arrival <= currentTime || v.at(j - 1).burst >v.at(j).burst)
{
ts.start = currentTime;
temp = v.at(j - 1).burst;
temp1 = v.at(i).arrival;
v.at(j - 1).burst = v.at(j).burst;
v.at(i).arrival = currentTime;
v.at(j).burst = temp;
currentTime = temp1;
ts.start = currentTime;
ts.p = v.at(i);
ts.end = v.at(i).burst + ts.start;
currentTime = ts.end;
turn = ts.end - v.at(i).arrival;
wait = turn - v.at(i).burst;
totalturn += turn;
totalwait+= wait;
t.push_back(ts);
}
else
{
process p = { "idle" };
ts.start = currentTime;
ts.p = p;
ts.end = v.at(i).arrival;
t.push_back(ts);
timeSlote ts2;
ts2.start = ts.end;
ts2.p = v.at(i);
ts2.end = v.at(i).burst + ts2.start;
currentTime = ts2.end;
turn= ts2.end - v.at(i).arrival;
wait = turn - v.at(i).burst;
totalturn += turn;
totalwait+= wait;
t.push_back(ts2);
}
}
}