Posts

Showing posts from March, 2012

Shortest-Job-First Scheduling (Preemptive)

♣ Try it Out :: #include<iostream> #include<cstdlib> #include<cstdio> #define FOR(i,N) for(int i=1;i<=N;i++) #define MAX 51 using namespace std; class CacheMemory { public: string process; int burst_time, arrival_time; } P[MAX]; void sortByBurst_time(int nop) { FOR(i,nop) for(int j=i+1; j<=nop; j++) if(P[i].burst_time > P[j].burst_time) swap(P[i], P[j]); } int a_time[MAX], INF = 99999999; void sortArrival_time(int n) { FOR(i,n) for(int j=i+1; j<=n; j++) if(a_time[i] > a_time[j]) swap(a_time[i], a_time[j]); } int main() { //freopen("sjfs_p.in", "r", stdin); int n; cin>>n; FOR(i,MAX) a_time[i] = INF; FOR(i,n) { cin>>P[i].process>>P[i].arrival_time>>P[i].burst_time; a_time[i] = P[i].arrival_time; } sortByBurst_time(n); sortArrival_time(n); FOR(i,n) if(P[i]

Shortest-Job-First Scheduling (Non-Preemptive)

♣ Try it Out :: #include<iostream> #include<cstdlib> #include<cstdio> #define FOR(i,N) for(int i=1;i<=N;i++) #define MAX 51 using namespace std; class CacheMemory { public: string process; int burst_time; } P[MAX]; void sortByBurst_time(int nop) { FOR(i,nop) for(int j=i+1; j<=nop; j++) if(P[i].burst_time > P[j].burst_time) swap(P[i], P[j]); } int main() { //freopen("sjfs.in", "r", stdin); int n; cin>>n; FOR(i,n) { cin>>P[i].process>>P[i].burst_time; } sortByBurst_time(n); cout<<"\nGrantt chart of SJF:"<<endl<<endl; cout<<"Process\t\tTime (milliseconds)"<<endl; cout<<"-------\t\t-------------------"<<endl; int start_time=0, end_time=0; FOR(i,n) { start_time = end_time; end_time += P[i].burst_time; cout<<P[i].proces