dimanche 1 mars 2015

SJF Non-preemptive scheduling algorithm


I'm fresh on these scheduling algorithms. I've become comfortable with SJF non-preemptive and I understand it from a pen and paper Gantt chart perspective but not quite so from a programming perspective. My code is below and though it runs successfully my math is incorrect. If anyone could help me better understand the ins and outs of what I'm doing wrong so I can properly fix it I'd really appreciate it! As of now its a work in progress so any help or ideas are immensely welcomed :)


Much thanks in advance, seriously, because I know firstly it's tough reading other peoples code let alone getting into it and debugging the logic errors.


So really, thanks again and God bless! ~J



/* SJF_NP algorithm */

#include <iostream>
using namespace std;

void SJF_NP(int n, int burst[], int arrival[], int throughput)
{
cout << "Output for SJF_Non_Preemptive scheduling algorithm" << endl;

int i, j, temp, temp2;
double tot, avgwait, avgturnaround, avgresponse, tp;

//array instantiations
int start[n], end[n], wait[n];

//calculations
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if (i>=2 && burst[i-1]>burst[j-1])
{
temp = burst[i-1];
temp2 = arrival[i-1];
burst[i-1]=burst[j-1];
arrival[i-1]=arrival[j-1];
burst[j-1]=temp;
arrival[j-1]=temp2;
}
}
if(i==1)
{
start[0]=0;
end[0]=burst[0];
wait[0]=0;
}
else
{
start[i-1]=end[i-2];
end[i-1]=start[i-1]+burst[i-1];
wait[i-1]=start[i-1]+arrival[i-1];
}
//throughput
if (start[i+1] <= throughput)
tp = i+1;
}

//output
cout << "\n\nPROCESS \t BURST TIME\tARRIVAL TIME\tWAIT TIME\tSTART TIME\tEND TIME\n";
for (i=0;i<n;i++){
cout << "\nP[" << i + 1 << "]" << "\t\t" << burst[i] << "\t\t" << arrival[i] << "\t\t" << wait[i] << "\t\t" << start[i] << "\t\t" << end[i];
}
//avg wait time
for(i=1,tot=0;i<n;i++){
tot+=wait[i-1];
avgwait=tot/n;
}
//avg turnaround time
for(i=1,tot=0;i<n;i++){
tot+=end[i-1];
avgturnaround=tot/n;
}
//avg response time
for(i=1,tot=0;i<n;i++){
tot+=start[i-1];
avgresponse=tot/n;
}
cout << "\n\nAverage Wait Time: " << avgwait;
cout << "\nAverage Response Time: " << avgturnaround;
cout << "\nAverage Turnaround Time: " << avgresponse;
cout << "\nThroughput for (" << throughput << "): " << tp << endl;
}



Aucun commentaire:

Enregistrer un commentaire