Answer to Question #273217 in C for Ashwini

Question #273217

Create a program of short job first (SJF)-non preemptive CPU Scheduling that will display gantt chart, total turnaround time, average turnaround time, total waiting time and average waiting time.


1
Expert's answer
2021-11-29T18:42:45-0500
#include <iostream>
using namespace std;
int calc[10][6];
 
void swap(int* x, int* y)
{
    int temp = *x;
    *x = *y;
    *y = temp;
}
 
void AArrive(int N, int calc[][6])
{
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N - i - 1; j++) {
            if (calc[j][1] > calc[j + 1][1]) {
                for (int k = 0; k < 5; k++) {
                    swap(calc[j][k], calc[j + 1][k]);
                }
            }
        }
    }
}
 
void compTime(int N, int calc[][6])
{
    int temp, val;
    calc[0][3] = calc[0][1] + calc[0][2];
    calc[0][5] = calc[0][3] - calc[0][1];
    calc[0][4] = calc[0][5] - calc[0][2];
 
    for (int i = 1; i < N; i++) {
        temp = calc[i - 1][3];
        int low = calc[i][2];
        for (int j = i; j < N; j++) {
            if (temp >= calc[j][1] && low >= calc[j][2]) {
                low = calc[j][2];
                val = j;
            }
        }
        calc[val][3] = temp + calc[val][2];
        calc[val][5] = calc[val][3] - calc[val][1];
        calc[val][4] = calc[val][5] - calc[val][2];
        for (int k = 0; k < 6; k++) {
            swap(calc[val][k], calc[i][k]);
        }
    }
}
 
int main()
{
    int N, temp;
 
    cout << "Enter number of Process: ";
    cin >> N;
 
    cout << "...Enter the process ID...\n";
    for (int i = 0; i < N; i++) {
        cout << "...Process " << i + 1 << "...\n";
        cout << "Enter Process Id: ";
        cin >> calc[i][0];
        cout << "Enter Arrival Time: ";
        cin >> calc[i][1];
        cout << "Enter Burst Time: ";
        cin >> calc[i][2];
    }
 
    cout << "Before Arrange...\n";
    cout << "Process ID\tArrival Time\tBurst Time\n";
    for (int i = 0; i < N; i++) {
        cout << calc[i][0] << "\t\t" << calc[i][1] << "\t\t"
             << calc[i][2] << "\n";
    }
 
    AArrive(N, calc);
    compTime(N, calc);
    cout << "Final Result...\n";
    cout << "Process ID\tArrival Time\tBurst Time\tWaiting "
            "Time\tTurnaround Time\n";
    for (int i = 0; i < N; i++) {
        cout << calc[i][0] << "\t\t" << calc[i][1] << "\t\t"
             << calc[i][2] << "\t\t" << calc[i][4] << "\t\t"
             << calc[i][5] << "\n";
    }
}

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