Can you fix the code of the completion time based on the gantt chart and the output of the completion time must be the same in the image below? Thank you!

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Can you fix the code of the completion time based on the gantt chart and the output of the completion time must be the same in the image below? Thank you!

Code:

#include <iostream>
#include <queue>
#include <string>
#include <vector>

struct Process {
    int processId;
    int burstTime;
    int priority;
};

void print_gantt_chart(const std::vector<std::pair<int, int>>& gantt_chart) {
    std::cout << "Gantt Chart:" << std::endl;
    std::cout << "----------------------------------------------------------------------------" << std::endl;
    std::cout << "| ";

    for (const auto& process : gantt_chart) {
        std::cout << "P" << process.first << " | ";
    }

    std::cout << std::endl;
    std::cout << "----------------------------------------------------------------------------" << std::endl;
    std::cout << "0  ";

    int currentTime = 0;
    for (const auto& process : gantt_chart) {
        currentTime += process.second;
        if (std::to_string(currentTime).length() == 1)
            std::cout << "  " << currentTime << "  ";
        else
            std::cout << "  " << currentTime << " ";
    }

    std::cout << std::endl;
}

void multiLevelQueueScheduling(const std::vector<Process>& processes, int quantumTime) {
    std::queue<Process> fcfs;
    std::queue<Process> rr;
    std::queue<Process> priority;

    for (const auto& process : processes) {
        if (process.priority == 1)
            fcfs.push(process);
        else if (process.priority == 2)
            rr.push(process);
        else if (process.priority == 3 || process.priority == 4)
            priority.push(process);
    }

    std::vector<std::pair<int, int>> gantt_chart;

    while (!fcfs.empty() || !rr.empty() || !priority.empty()) {
        if (!fcfs.empty()) {
            Process process = fcfs.front();
            fcfs.pop();

            int executionTime = std::min(quantumTime, process.burstTime);
            process.burstTime -= executionTime;

            gantt_chart.emplace_back(process.processId, executionTime);

            if (process.burstTime > 0)
                fcfs.push(process);
        }

        if (!rr.empty()) {
            Process process = rr.front();
            rr.pop();

            int executionTime = std::min(quantumTime, process.burstTime);
            process.burstTime -= executionTime;

            gantt_chart.emplace_back(process.processId, executionTime);

            if (process.burstTime > 0)
                rr.push(process);
        }

        if (!priority.empty()) {
            Process process = priority.front();
            priority.pop();

            int executionTime = std::min(quantumTime, process.burstTime);
            process.burstTime -= executionTime;

            gantt_chart.emplace_back(process.processId, executionTime);

            if (process.burstTime > 0)
                priority.push(process);
        }
    }

    print_gantt_chart(gantt_chart);

    std::vector<int> completionTime(processes.size());
    std::vector<int> turnaroundTime(processes.size());
    std::vector<int> waitingTime(processes.size());

    int currentEndTime = 0;
    for (const auto& process : processes) {
        int processIndex = process.processId - 1;
        currentEndTime += process.burstTime;
        completionTime[processIndex] = currentEndTime;
        turnaroundTime[processIndex] = completionTime[processIndex];
        waitingTime[processIndex] = turnaroundTime[processIndex] - process.burstTime;
    }

    std::cout << "\nProcess\t\tBurst Time\t\tPriority\t\tCompletion Time\t\tTurnaround Time\t\tWaiting Time" << std::endl;
    for (const auto& process : processes) {
        int processIndex = process.processId - 1;
        std::cout << "P" << process.processId << "\t\t" << process.burstTime << "\t\t\t" << process.priority << "\t\t\t"
            << completionTime[processIndex] << "\t\t\t" << turnaroundTime[processIndex] << "\t\t\t" << waitingTime[processIndex] << std::endl;
    }

    float avgWaitingTime = 0;
    float avgTurnaroundTime = 0;
    for (const auto& process : processes) {
        avgWaitingTime += waitingTime[process.processId - 1];
        avgTurnaroundTime += turnaroundTime[process.processId - 1];
    }

    avgWaitingTime /= processes.size();
    avgTurnaroundTime /= processes.size();

    std::cout << std::endl;
    std::cout << "Average Waiting Time: " << avgWaitingTime << std::endl;
    std::cout << "Average Turnaround Time: " << avgTurnaroundTime << std::endl;
}

int main() {
    std::vector<Process> processes = {
        {1, 8, 4},
        {2, 6, 1},
        {3, 1, 2},
        {4, 9, 2},
        {5, 3, 3}
    };

    int quantumTime = 2;

    multiLevelQueueScheduling(processes, quantumTime);

    return 0;
}

Gantt Chart
O
P2 P3 PI P2
2 (3 5 7
Completion Time
27 t
13
P1 =
P2 =
3 +
25²
P3 =
P4 =
P5:201
рч
q
P5 | P2
11
p2 | P4
13
15
PI
17
ру
19
P5
20
P4 | PI
22
24
РЧ ТР
25
27
* Last execution of
each process
Transcribed Image Text:Gantt Chart O P2 P3 PI P2 2 (3 5 7 Completion Time 27 t 13 P1 = P2 = 3 + 25² P3 = P4 = P5:201 рч q P5 | P2 11 p2 | P4 13 15 PI 17 ру 19 P5 20 P4 | PI 22 24 РЧ ТР 25 27 * Last execution of each process
Expert Solution
steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Follow-up Questions
Read through expert solutions to related follow-up questions below.
Follow-up Question

Can you fix the output of the Gantt chart in the code and the output of the gantt chart should be the same in the image below? The instruction also on how to solve the Gantt chart is in the image below. Badly need to fix it :( Thank you!

Code:

#include <iostream>
#include <queue>
#include <string>
#include <vector>

struct Process {
    int processId;
    int burstTime;
    int priority;
};

void print_gantt_chart(const std::vector<std::pair<int, int>>& gantt_chart) {
    std::cout << "Gantt Chart:" << std::endl;
    std::cout << "----------------------------------------------------------------------------" << std::endl;
    std::cout << "| ";

    for (const auto& process : gantt_chart) {
        std::cout << "P" << process.first << " | ";
    }

    std::cout << std::endl;
    std::cout << "----------------------------------------------------------------------------" << std::endl;
    std::cout << "0  ";

    int currentTime = 0;
    for (const auto& process : gantt_chart) {
        currentTime += process.second;
        if (std::to_string(currentTime).length() == 1)
            std::cout << "  " << currentTime << "  ";
        else
            std::cout << "  " << currentTime << " ";
    }

    std::cout << std::endl;
}

void multiLevelQueueScheduling(const std::vector<Process>& processes, int quantumTime) {
    std::queue<Process> fcfs;
    std::queue<Process> rr;
    std::queue<Process> priority;

    // Splitting the processes into different queues based on priority
    for (const auto& process : processes) {
        if (process.priority == 1)
            fcfs.push(process);
        else if (process.priority == 2)
            rr.push(process);
        else if (process.priority == 3 || process.priority == 4)
            priority.push(process);
    }

    std::vector<std::pair<int, int>> gantt_chart;

    while (!fcfs.empty() || !rr.empty() || !priority.empty()) {
        
        // FCFS Scheduling
        if (!fcfs.empty()) {
            Process process = fcfs.front();
            fcfs.pop();

            int executionTime = std::min(quantumTime, process.burstTime);
            process.burstTime -= executionTime;

            gantt_chart.emplace_back(process.processId, executionTime);

            if (process.burstTime > 0)
                fcfs.push(process);
        }

        // RR Scheduling
        if (!rr.empty()) {
            Process process = rr.front();
            rr.pop();

            int executionTime = std::min(quantumTime, process.burstTime);
            process.burstTime -= executionTime;

            gantt_chart.emplace_back(process.processId, executionTime);

            if (process.burstTime > 0)
                rr.push(process);
        }

        // Priority Scheduling
        if (!priority.empty()) {
            Process process = priority.front();
            priority.pop();

            int executionTime = std::min(quantumTime, process.burstTime);
            process.burstTime -= executionTime;

            gantt_chart.emplace_back(process.processId, executionTime);

            if (process.burstTime > 0)
                priority.push(process);
        }
    }

    print_gantt_chart(gantt_chart);

    std::vector<int> completionTime(processes.size());
    std::vector<int> turnaroundTime(processes.size());
    std::vector<int> waitingTime(processes.size());

    int currentEndTime = 0;
    for (const auto& process : gantt_chart) {
        int processIndex = process.first - 1;
        currentEndTime += process.second;
        completionTime[processIndex] = currentEndTime;
        turnaroundTime[processIndex] = completionTime[processIndex];
        waitingTime[processIndex] = turnaroundTime[processIndex] - processes[processIndex].burstTime;
    }

    std::cout << "\nProcess\t\tBurst Time\t\tPriority\t\tCompletion Time\t\tTurnaround Time\t\tWaiting Time" << std::endl;
    for (const auto& process : processes) {
        int processIndex = process.processId - 1;
        std::cout << "P" << process.processId << "\t\t" << process.burstTime << "\t\t\t" << process.priority << "\t\t\t"
            << completionTime[processIndex] << "\t\t\t" << turnaroundTime[processIndex] << "\t\t\t" << waitingTime[processIndex] << std::endl;
    }

    float avgWaitingTime = 0;
    float avgTurnaroundTime = 0;
    for (const auto& process : processes) {
        avgWaitingTime += waitingTime[process.processId - 1];
        avgTurnaroundTime += turnaroundTime[process.processId - 1];
    }

    avgWaitingTime /= processes.size();
    avgTurnaroundTime /= processes.size();

    std::cout << std::endl;
    std::cout << "Average Waiting Time: " << avgWaitingTime << std::endl;
    std::cout << "Average Turnaround Time: " << avgTurnaroundTime << std::endl;
}

int main() {
    std::vector<Process> processes = {
        {1, 8, 4},
        {2, 6, 1},
        {3, 1, 2},
        {4, 9, 2},
        {5, 3, 3}
    };

    int quantumTime = 2;

    multiLevelQueueScheduling(processes, quantumTime);

    return 0;
}

Gantt Chart
hd hd
hd | hd
P2 P3 P4
0 6 7 9
11 13
13
P4 | p4
P5 PI
15 16 19 27
Transcribed Image Text:Gantt Chart hd hd hd | hd P2 P3 P4 0 6 7 9 11 13 13 P4 | p4 P5 PI 15 16 19 27
INDERJE
P4
P4
рч ру T
7 q
18 15
a. Gantt chart
P2
0 6
0
Explanation:
FCFS - First Priority (High priority
RR - second priority / medium Priority
Priority - Third Priority / Low priority
P2
b
P3
a. P2= Highest priority (FCFS) with Burst Time of 6
P2
O
6
P2
b. P3 and Py = Second priority (RR with Time Quantum = 2)
P3
6
7
P4 P4
рз | ри
។
priority 3
q ||
P3 = 1 ×
P4 = 9-2=7 - 2 = 5-2= 3-2 = 1 ×
C. P5 and PI = Low Priority (priority)
priority 4:
P4
P4
16
13
P4 P4
P5
P4 | P4
15
11 13 15
16
PI
19 27
P4 P4 P5 PI
16
q
P5 with Burst Time of 3
P5 =
16 + 3 = (19
PI with Burst Time of 8
Pl. = 19. + 8. (27.
Priority 1 P2 (FCFS)
priority 2: P3 and P4 (RR with Time Quantum = 2)
priority 3: P5 (priority)
priority 4: Pl (Priority)
19 27
Transcribed Image Text:INDERJE P4 P4 рч ру T 7 q 18 15 a. Gantt chart P2 0 6 0 Explanation: FCFS - First Priority (High priority RR - second priority / medium Priority Priority - Third Priority / Low priority P2 b P3 a. P2= Highest priority (FCFS) with Burst Time of 6 P2 O 6 P2 b. P3 and Py = Second priority (RR with Time Quantum = 2) P3 6 7 P4 P4 рз | ри ។ priority 3 q || P3 = 1 × P4 = 9-2=7 - 2 = 5-2= 3-2 = 1 × C. P5 and PI = Low Priority (priority) priority 4: P4 P4 16 13 P4 P4 P5 P4 | P4 15 11 13 15 16 PI 19 27 P4 P4 P5 PI 16 q P5 with Burst Time of 3 P5 = 16 + 3 = (19 PI with Burst Time of 8 Pl. = 19. + 8. (27. Priority 1 P2 (FCFS) priority 2: P3 and P4 (RR with Time Quantum = 2) priority 3: P5 (priority) priority 4: Pl (Priority) 19 27
Solution
Bartleby Expert
SEE SOLUTION
Follow-up Question

Can you fix the code and the gantt chart? The output should be same as the image below?

Code:

#include <iostream>
#include <queue>
#include <string>
#include <vector>

struct Process {
    int processId;
    int burstTime;
    int priority;
};

void print_gantt_chart(const std::vector<std::pair<int, int>>& gantt_chart) {
    std::cout << "Gantt Chart:" << std::endl;
    std::cout << "----------------------------------------------------------------------------" << std::endl;
    std::cout << "| ";

    for (const auto& process : gantt_chart) {
        std::cout << "P" << process.first << " | ";
    }

    std::cout << std::endl;
    std::cout << "----------------------------------------------------------------------------" << std::endl;
    std::cout << "0  ";

    int currentTime = 0;
    for (const auto& process : gantt_chart) {
        currentTime += process.second;
        if (std::to_string(currentTime).length() == 1)
            std::cout << "  " << currentTime << "  ";
        else
            std::cout << "  " << currentTime << " ";
    }

    std::cout << std::endl;
}

void multiLevelQueueScheduling(const std::vector<Process>& processes, int quantumTime) {
    std::queue<Process> fcfs;
    std::queue<Process> rr;
    std::queue<Process> priority;

    // Splitting the processes into different queues based on priority
    for (const auto& process : processes) {
        if (process.priority == 1)
            fcfs.push(process);
        else if (process.priority == 2)
            rr.push(process);
        else if (process.priority == 3 || process.priority == 4)
            priority.push(process);
    }

    std::vector<std::pair<int, int>> gantt_chart;

    while (!fcfs.empty() || !rr.empty() || !priority.empty()) {
        
        // FCFS Scheduling
        if (!fcfs.empty()) {
            Process process = fcfs.front();
            fcfs.pop();

            int executionTime = std::min(quantumTime, process.burstTime);
            process.burstTime -= executionTime;

            gantt_chart.emplace_back(process.processId, executionTime);

            if (process.burstTime > 0)
                fcfs.push(process);
        }

        // RR Scheduling
        if (!rr.empty()) {
            Process process = rr.front();
            rr.pop();

            int executionTime = std::min(quantumTime, process.burstTime);
            process.burstTime -= executionTime;

            gantt_chart.emplace_back(process.processId, executionTime);

            if (process.burstTime > 0)
                rr.push(process);
        }

        // Priority Scheduling
        if (!priority.empty()) {
            Process process = priority.front();
            priority.pop();

            int executionTime = std::min(quantumTime, process.burstTime);
            process.burstTime -= executionTime;

            gantt_chart.emplace_back(process.processId, executionTime);

            if (process.burstTime > 0)
                priority.push(process);
        }
    }

    print_gantt_chart(gantt_chart);

    std::vector<int> completionTime(processes.size());
    std::vector<int> turnaroundTime(processes.size());
    std::vector<int> waitingTime(processes.size());

    int currentEndTime = 0;
    for (const auto& process : gantt_chart) {
        int processIndex = process.first - 1;
        currentEndTime += process.second;
        completionTime[processIndex] = currentEndTime;
        turnaroundTime[processIndex] = completionTime[processIndex];
        waitingTime[processIndex] = turnaroundTime[processIndex] - processes[processIndex].burstTime;
    }

    std::cout << "\nProcess\t\tBurst Time\t\tPriority\t\tCompletion Time\t\tTurnaround Time\t\tWaiting Time" << std::endl;
    for (const auto& process : processes) {
        int processIndex = process.processId - 1;
        std::cout << "P" << process.processId << "\t\t" << process.burstTime << "\t\t\t" << process.priority << "\t\t\t"
            << completionTime[processIndex] << "\t\t\t" << turnaroundTime[processIndex] << "\t\t\t" << waitingTime[processIndex] << std::endl;
    }

    float avgWaitingTime = 0;
    float avgTurnaroundTime = 0;
    for (const auto& process : processes) {
        avgWaitingTime += waitingTime[process.processId - 1];
        avgTurnaroundTime += turnaroundTime[process.processId - 1];
    }

    avgWaitingTime /= processes.size();
    avgTurnaroundTime /= processes.size();

    std::cout << std::endl;
    std::cout << "Average Waiting Time: " << avgWaitingTime << std::endl;
    std::cout << "Average Turnaround Time: " << avgTurnaroundTime << std::endl;
}

int main() {
    std::vector<Process> processes = {
        {1, 8, 4},
        {2, 6, 1},
        {3, 1, 2},
        {4, 9, 2},
        {5, 3, 3}
    };

    int quantumTime = 2;

    multiLevelQueueScheduling(processes, quantumTime);

    return 0;
}

Gantt Chart
Gantt Chart
P2 P3
067
Process
P1
P2
P3
P4
P5
рч P4 P4 P4 p4 P5 PI
9 11 13 15 16 19 27
Burst Time
(ms)
8
6
1
9
@co
3
Priority
4
1
2
2
3
Completion Turnaround
Time
Time
27
27
6
6
7
16
19
7
16
19
Waiting
Time
19
0
6
7
16
Transcribed Image Text:Gantt Chart Gantt Chart P2 P3 067 Process P1 P2 P3 P4 P5 рч P4 P4 P4 p4 P5 PI 9 11 13 15 16 19 27 Burst Time (ms) 8 6 1 9 @co 3 Priority 4 1 2 2 3 Completion Turnaround Time Time 27 27 6 6 7 16 19 7 16 19 Waiting Time 19 0 6 7 16
Solution
Bartleby Expert
SEE SOLUTION
Follow-up Question

Can you provide a step-by-step procedure on how to solve this image below for better understanding of multilevel queue scheduling? thank you!

Solve this table using the Multi-level queue scheduling (FCFS, RR, Priority)
Requirements:
a. Gantt Chart
b. Average Waiting Time
c. Average Turnaround Time
Process
P1
P2
P3
P4
P5
Burst Time (ms)
8
6
1
9
3
Priority
4
1
2
2
Priority 1 is FCFS Scheduling
• Priority 2 is Round Robin Scheduling (Time Quantum = 2)
Priority 3 and 4 is Priority Scheduling
Assume that the process with Priority 1 has highest priority and 4 as low priority
Show how to solve the values completely of the three scheduling of all process in
one Gantt chart.
The last execution of process must end in 27
Transcribed Image Text:Solve this table using the Multi-level queue scheduling (FCFS, RR, Priority) Requirements: a. Gantt Chart b. Average Waiting Time c. Average Turnaround Time Process P1 P2 P3 P4 P5 Burst Time (ms) 8 6 1 9 3 Priority 4 1 2 2 Priority 1 is FCFS Scheduling • Priority 2 is Round Robin Scheduling (Time Quantum = 2) Priority 3 and 4 is Priority Scheduling Assume that the process with Priority 1 has highest priority and 4 as low priority Show how to solve the values completely of the three scheduling of all process in one Gantt chart. The last execution of process must end in 27
Solution
Bartleby Expert
SEE SOLUTION
Follow-up Question

Can you fix and change the code of Splitting the processes into different queues based on priority, FCFS Scheduling, RR Scheduling, and Priority Scheduling into new code? The new code is provided in the image below and please show the output. Thank you!

Code:

#include <iostream>
#include <queue>
#include <string>
#include <vector>

struct Process {
    int processId;
    int burstTime;
    int priority;
};

void print_gantt_chart(const std::vector<std::pair<int, int>>& gantt_chart) {
    std::cout << "Gantt Chart:" << std::endl;
    std::cout << "----------------------------------------------------------------------------" << std::endl;
    std::cout << "| ";

    for (const auto& process : gantt_chart) {
        std::cout << "P" << process.first << " | ";
    }

    std::cout << std::endl;
    std::cout << "----------------------------------------------------------------------------" << std::endl;
    std::cout << "0  ";

    int currentTime = 0;
    for (const auto& process : gantt_chart) {
        currentTime += process.second;
        if (std::to_string(currentTime).length() == 1)
            std::cout << "  " << currentTime << "  ";
        else
            std::cout << "  " << currentTime << " ";
    }

    std::cout << std::endl;
}

void multiLevelQueueScheduling(const std::vector<Process>& processes, int quantumTime) {
    std::queue<Process> fcfs;
    std::queue<Process> rr;
    std::queue<Process> priority;

    // Splitting the processes into different queues based on priority
    for (const auto& process : processes) {
        if (process.priority == 1)
            fcfs.push(process);
        else if (process.priority == 2)
            rr.push(process);
        else if (process.priority == 3 || process.priority == 4)
            priority.push(process);
    }

    std::vector<std::pair<int, int>> gantt_chart;

    while (!fcfs.empty() || !rr.empty() || !priority.empty()) {
        
        // FCFS Scheduling
        if (!fcfs.empty()) {
            Process process = fcfs.front();
            fcfs.pop();

            int executionTime = std::min(quantumTime, process.burstTime);
            process.burstTime -= executionTime;

            gantt_chart.emplace_back(process.processId, executionTime);

            if (process.burstTime > 0)
                fcfs.push(process);
        }

        // RR Scheduling
        if (!rr.empty()) {
            Process process = rr.front();
            rr.pop();

            int executionTime = std::min(quantumTime, process.burstTime);
            process.burstTime -= executionTime;

            gantt_chart.emplace_back(process.processId, executionTime);

            if (process.burstTime > 0)
                rr.push(process);
        }

        // Priority Scheduling
        if (!priority.empty()) {
            Process process = priority.front();
            priority.pop();

            int executionTime = std::min(quantumTime, process.burstTime);
            process.burstTime -= executionTime;

            gantt_chart.emplace_back(process.processId, executionTime);

            if (process.burstTime > 0)
                priority.push(process);
        }
    }

    print_gantt_chart(gantt_chart);

    std::vector<int> completionTime(processes.size());
    std::vector<int> turnaroundTime(processes.size());
    std::vector<int> waitingTime(processes.size());

    int currentEndTime = 0;
    for (const auto& process : gantt_chart) {
        int processIndex = process.first - 1;
        currentEndTime += process.second;
        completionTime[processIndex] = currentEndTime;
        turnaroundTime[processIndex] = completionTime[processIndex];
        waitingTime[processIndex] = turnaroundTime[processIndex] - processes[processIndex].burstTime;
    }

    std::cout << "\nProcess\t\tBurst Time\t\tPriority\t\tCompletion Time\t\tTurnaround Time\t\tWaiting Time" << std::endl;
    for (const auto& process : processes) {
        int processIndex = process.processId - 1;
        std::cout << "P" << process.processId << "\t\t" << process.burstTime << "\t\t\t" << process.priority << "\t\t\t"
            << completionTime[processIndex] << "\t\t\t" << turnaroundTime[processIndex] << "\t\t\t" << waitingTime[processIndex] << std::endl;
    }

    float avgWaitingTime = 0;
    float avgTurnaroundTime = 0;
    for (const auto& process : processes) {
        avgWaitingTime += waitingTime[process.processId - 1];
        avgTurnaroundTime += turnaroundTime[process.processId - 1];
    }

    avgWaitingTime /= processes.size();
    avgTurnaroundTime /= processes.size();

    std::cout << std::endl;
    std::cout << "Average Waiting Time: " << avgWaitingTime << std::endl;
    std::cout << "Average Turnaround Time: " << avgTurnaroundTime << std::endl;
}

int main() {
    std::vector<Process> processes = {
        {1, 8, 4},
        {2, 6, 1},
        {3, 1, 2},
        {4, 9, 2},
        {5, 3, 3}
    };

    int quantumTime = 2;

    multiLevelQueueScheduling(processes, quantumTime);

    return 0;
}

New Code:
// Splitting the processes into different queues based on priority
for (int i = 0; i < totalProcesses; i++) {
if (processes [i].priority == 1)
fcfsQueue.push(processes [i]);
else if (processes[i].priority == 2)
rrQueue.push(processes [i]);
else if (processes[i].priority == 3)
priorityQueue.push(processes[i]);
}
// FCFS Scheduling
int time = 0;
while (!fcfsQueue.empty()) {
Process currentProcess = fcfsQueue.front();
fcfsQueue.pop();
waiting Times [current Process.processld - 1] = time;
time += currentProcess.burst Time;
turnaroundTimes[currentProcess.processld - 1] = time;
}
// RR Scheduling
while (!rrQueue.empty()) {
Process current Process = rrQueue.front();
rrQueue.pop();
}
waiting Times[current Process.processid - 1] = time;
if (currentProcess.burstTime > 2) {
currentProcess.burstTime -= 2;
time += 2;
rrQueue.push(currentProcess);
} else {
time += currentProcess.burst Time;
turnaroundTimes[currentProcess.processld - 1] = time;
}
}
// Priority Scheduling
while (!priorityQueue.empty()) {
Process currentProcess = priorityQueue.top();
priorityQueue.pop();
waiting Times [current Process.processld - 1] = time;
time += currentProcess.burst Time;
turnaroundTimes[currentProcess.processld-
1] = time;
Transcribed Image Text:New Code: // Splitting the processes into different queues based on priority for (int i = 0; i < totalProcesses; i++) { if (processes [i].priority == 1) fcfsQueue.push(processes [i]); else if (processes[i].priority == 2) rrQueue.push(processes [i]); else if (processes[i].priority == 3) priorityQueue.push(processes[i]); } // FCFS Scheduling int time = 0; while (!fcfsQueue.empty()) { Process currentProcess = fcfsQueue.front(); fcfsQueue.pop(); waiting Times [current Process.processld - 1] = time; time += currentProcess.burst Time; turnaroundTimes[currentProcess.processld - 1] = time; } // RR Scheduling while (!rrQueue.empty()) { Process current Process = rrQueue.front(); rrQueue.pop(); } waiting Times[current Process.processid - 1] = time; if (currentProcess.burstTime > 2) { currentProcess.burstTime -= 2; time += 2; rrQueue.push(currentProcess); } else { time += currentProcess.burst Time; turnaroundTimes[currentProcess.processld - 1] = time; } } // Priority Scheduling while (!priorityQueue.empty()) { Process currentProcess = priorityQueue.top(); priorityQueue.pop(); waiting Times [current Process.processld - 1] = time; time += currentProcess.burst Time; turnaroundTimes[currentProcess.processld- 1] = time;
Solution
Bartleby Expert
SEE SOLUTION
Follow-up Question

Can you fix the code by adding if all of the processes in the Arrival Time is 0 and the output must be the same in the image below? Thank you!

Code:

#include <iostream>
#include <queue>
#include <string>
#include <vector>

struct Process {
    int processId;
    int burstTime;
    int priority;
};

void print_gantt_chart(const std::vector<std::pair<int, int>>& gantt_chart) {
    std::cout << "Gantt Chart:" << std::endl;
    std::cout << "----------------------------------------------------------------------------" << std::endl;
    std::cout << "| ";

    for (const auto& process : gantt_chart) {
        std::cout << "P" << process.first << " | ";
    }

    std::cout << std::endl;
    std::cout << "----------------------------------------------------------------------------" << std::endl;
    std::cout << "0  ";

    int currentTime = 0;
    for (const auto& process : gantt_chart) {
        currentTime += process.second;
        if (std::to_string(currentTime).length() == 1)
            std::cout << "  " << currentTime << "  ";
        else
            std::cout << "  " << currentTime << " ";
    }

    std::cout << std::endl;
}

void multiLevelQueueScheduling(const std::vector<Process>& processes, int quantumTime) {
    std::queue<Process> fcfs;
    std::queue<Process> rr;
    std::queue<Process> priority;

    for (const auto& process : processes) {
        if (process.priority == 1)
            fcfs.push(process);
        else if (process.priority == 2)
            rr.push(process);
        else if (process.priority == 3 || process.priority == 4)
            priority.push(process);
    }

    std::vector<std::pair<int, int>> gantt_chart;

    while (!fcfs.empty() || !rr.empty() || !priority.empty()) {
        if (!fcfs.empty()) {
            Process process = fcfs.front();
            fcfs.pop();

            int executionTime = std::min(quantumTime, process.burstTime);
            process.burstTime -= executionTime;

            gantt_chart.emplace_back(process.processId, executionTime);

            if (process.burstTime > 0)
                fcfs.push(process);
        }

        if (!rr.empty()) {
            Process process = rr.front();
            rr.pop();

            int executionTime = std::min(quantumTime, process.burstTime);
            process.burstTime -= executionTime;

            gantt_chart.emplace_back(process.processId, executionTime);

            if (process.burstTime > 0)
                rr.push(process);
        }

        if (!priority.empty()) {
            Process process = priority.front();
            priority.pop();

            int executionTime = std::min(quantumTime, process.burstTime);
            process.burstTime -= executionTime;

            gantt_chart.emplace_back(process.processId, executionTime);

            if (process.burstTime > 0)
                priority.push(process);
        }
    }

    print_gantt_chart(gantt_chart);

    std::vector<int> completionTime(processes.size());
    std::vector<int> turnaroundTime(processes.size());
    std::vector<int> waitingTime(processes.size());

    int currentEndTime = 0;
    for (const auto& process : gantt_chart) {
        int processIndex = process.first - 1;
        currentEndTime += process.second;
        completionTime[processIndex] = currentEndTime;
        turnaroundTime[processIndex] = completionTime[processIndex];
        waitingTime[processIndex] = turnaroundTime[processIndex] - processes[processIndex].burstTime;
    }

    std::cout << "\nProcess\t\tBurst Time\t\tPriority\t\tCompletion Time\t\tTurnaround Time\t\tWaiting Time" << std::endl;
    for (const auto& process : processes) {
        int processIndex = process.processId - 1;
        std::cout << "P" << process.processId << "\t\t" << process.burstTime << "\t\t\t" << process.priority << "\t\t\t"
            << completionTime[processIndex] << "\t\t\t" << turnaroundTime[processIndex] << "\t\t\t" << waitingTime[processIndex] << std::endl;
    }

    float avgWaitingTime = 0;
    float avgTurnaroundTime = 0;
    for (const auto& process : processes) {
        avgWaitingTime += waitingTime[process.processId - 1];
        avgTurnaroundTime += turnaroundTime[process.processId - 1];
    }

    avgWaitingTime /= processes.size();
    avgTurnaroundTime /= processes.size();

    std::cout << std::endl;
    std::cout << "Average Waiting Time: " << avgWaitingTime << std::endl;
    std::cout << "Average Turnaround Time: " << avgTurnaroundTime << std::endl;
}

int main() {
    std::vector<Process> processes = {
        {1, 8, 4},
        {2, 6, 1},
        {3, 1, 2},
        {4, 9, 2},
        {5, 3, 3}
    };

    int quantumTime = 2;

    multiLevelQueueScheduling(processes, quantumTime);

    return 0;
}

Process Arrival
Time
P1
P2
P3
P4
P5
(ms)
0
0
0
0
0
Burst Priority Completion Turnaround
Time
Time
Time
(ms)
8
6
1
9
3
4
1
2
2
3
27
13
3
25
20
27
13
3
25
20
Waiting
Time
19
7
2
16
17
Transcribed Image Text:Process Arrival Time P1 P2 P3 P4 P5 (ms) 0 0 0 0 0 Burst Priority Completion Turnaround Time Time Time (ms) 8 6 1 9 3 4 1 2 2 3 27 13 3 25 20 27 13 3 25 20 Waiting Time 19 7 2 16 17
Solution
Bartleby Expert
SEE SOLUTION
Knowledge Booster
System Management Strategies
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education