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].process<<"\t\t"<<start_time<<" - "<<end_time<<endl;
    }
}

♣ Sample Input ::
4
P1 6
P2 8
P3 7
P4 3

♣ Sample Output ::
Grantt chart of SJF:

Process         Time (milliseconds)
-------         -------------------
P4              0 - 3
P1              3 - 9
P3              9 - 16
P2              16 - 24

♣ Source Codes ::

আবূ হুরাইরাহ (রাঃ) হতে বর্ণিত আছে, তিনি বলেন, নাবী (সাল্লাল্লাহু আলাইহি ওয়াসাল্লাম) বলেছেনঃ কবিরা যা বলেছে তার মধ্যে কবি লাবীদ যা বলেছে, তা ধ্রুব সত্যঃ "জেনে রেখো, আল্লাহ ছাড়া সব কিছুই মিথ্যা।" [বুখারীঃ ৩৮৪১, মুসলিমঃ ২২৫৬]

Popular posts from this blog

C++ :: Topological Sort Algorithm (using DFS)

How to Hack Facebook Account

C++ :: Strongly Connected Components Algorithm (SCC)