Answer to Question #293148 in C for Yash

Question #293148

Create a program in C/C++ for Shortest Job First (SJF) Scheduling for the following processes that are in ready queue at 0 timestamp



Process CPU Burst Time


P1 1


P2 4


P3 2


P4 10


P5 7



1
Expert's answer
2022-02-02T06:11:34-0500
#include <iostream>

const int M = 11;

template <class T>
class Queue
{
private:
    int top;
    int bottom;
    static int c;
    T st[M];
    
public:
    class Full {};
    class Empty {};
    Queue();
    void push(T value);
    T pop ();
};

template <class T>
int Queue<T>::c = 0;

template <class T>
Queue<T>::Queue():top(10), bottom(10){}


template <class T>
void Queue<T>::push(T value)
{
    if(c > M-1){
        throw Full();
    }
    st[top--] = value;
    ++c;
    if (top==-1) {
        bottom = 2;
    }
}

template <class T>
T Queue<T>::pop()
{
    if(c == 0)
    {
        throw Empty();
    }
    if(bottom==0)
    {
        top = 10;
        bottom = 10;
        c--;
        return st[0];
    }
    else c--;
    return st[bottom--];
}

struct Process
{
    int i;
    Process(){}
    Process(int time)
    {
        i = time;
    }
};

int main()
{
    
    Queue<Process> q;
    Process P1(1);
    Process P2(4);
    Process P3(2);
    Process P4(10);
    Process P5(7);
    q.push(P1);
    q.push(P2);
    q.push(P3);
    q.push(P4);
    q.push(P5);
    
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS