0% found this document useful (0 votes)
3 views31 pages

Include

The document contains C++ code that implements the Shortest Remaining Time First (SRTF) scheduling algorithm and the Round Robin (RR) scheduling algorithm. It defines a Process structure to hold process details and includes functions to perform scheduling based on user input. The main function collects process information, sorts them, applies the scheduling algorithms, and displays the results including finish time, turnaround time, and waiting time for each process.

Uploaded by

danielarega2727
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views31 pages

Include

The document contains C++ code that implements the Shortest Remaining Time First (SRTF) scheduling algorithm and the Round Robin (RR) scheduling algorithm. It defines a Process structure to hold process details and includes functions to perform scheduling based on user input. The main function collects process information, sorts them, applies the scheduling algorithms, and displays the results including finish time, turnaround time, and waiting time for each process.

Uploaded by

danielarega2727
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#include <iostream> #include <vector> #include <algorithm> using

namespace std; struct Process { int id; int arrivalTime; int burstTime; int
remainingTime; int finishTime; int turnaroundTime; int waitingTime; }; bool
compareArrivalTime(const Process &a, const Process &b) { return
[Link] < [Link]; } void SRTF(vector<Process> &processes) { int
n = [Link](); int currentTime = 0; int completed = 0; vector<int>
isCompleted(n, 0); while (completed != n) { int shortest = -1; int
minRemainingTime = INT_MAX; // Find the process with the shortest
remaining time for (int i = 0; i < n; i++) { if (processes[i].arrivalTime <=
currentTime && !isCompleted[i] && processes[i].remainingTime <
minRemainingTime) { shortest = i; minRemainingTime =
processes[i].remainingTime; } } if (shortest == -1) { currentTime++;
continue; } // Execute the process for 1 unit of time
processes[shortest].remainingTime--; currentTime++; // If the process is
completed if (processes[shortest].remainingTime == 0)
{ processes[shortest].finishTime = currentTime;
processes[shortest].turnaroundTime = processes[shortest].finishTime -
processes[shortest].arrivalTime; processes[shortest].waitingTime =
processes[shortest].turnaroundTime - processes[shortest].burstTime;
isCompleted[shortest] = 1; completed++; } } } int main() { int n; cout <<
"Enter the number of processes: "; cin >> n; vector<Process> processes(n);
for (int i = 0; i < n; i++) { processes[i].id = i + 1; cout << "Enter arrival time
for process " << processes[i].id << ": "; cin >> processes[i].arrivalTime; cout
<< "Enter burst time for process " << processes[i].id << ": "; cin >>
processes[i].burstTime; processes[i].remainingTime = processes[i].burstTime;
} // Sort processes by arrival time sort([Link](), [Link](),
compareArrivalTime); // Apply SRTF scheduling SRTF(processes); // Display the
results cout << "\nProcess ID\tArrival Time\tBurst Time\tFinish Time\
tTurnaround Time\tWaiting Time\n"; for (const auto &p : processes) { cout <<
[Link] << "\t\t" << [Link] << "\t\t" << [Link] << "\t\t" <<
[Link] << "\t\t" << [Link] << "\t\t" << [Link] <<
endl; } return 0; } EXPLAIN ECH LINE OF CODE AND HOW IT WORKS

Certainly! Below is a detailed explanation of each line of code in the provided


C++ program, which implements the Shortest Remaining Time First
(SRTF) scheduling algorithm.

1. Header Files and Namespace


cpp

Copy
#include <iostream>#include <vector>#include <algorithm>
using namespace std;

#include <iostream>: Includes the standard input-output library for


handling input and output operations (e.g., cin, cout).



#include <vector>: Includes the vector library, which provides a dynamic
array-like container.


#include <algorithm>: Includes the algorithm library, which provides


functions like sort for sorting data structures.


using namespace std;: Allows us to use standard library functions and


objects (e.g., cout, vector) without the std:: prefix.

2. Process Structure
cpp

Copy
struct Process {
int id;
int arrivalTime;
int burstTime;
int remainingTime;
int finishTime;
int turnaroundTime;
int waitingTime;};

Defines a struct named Process to represent a process in the scheduling


algorithm.


Members:

id: Unique identifier for the process.


o
o

arrivalTime: Time at which the process arrives in the ready queue.

o
o

burstTime: Total CPU time required by the process.

o
o

remainingTime: Remaining CPU time needed to complete the


process.

o
o

finishTime: Time at which the process completes execution.

o
o

turnaroundTime: Total time taken from arrival to completion


(finishTime - arrivalTime).

o
o

waitingTime: Total time the process spends waiting in the ready


queue (turnaroundTime - burstTime).

3. Comparison Function for Sorting


cpp

Copy
bool compareArrivalTime(const Process &a, const Process &b) {
return [Link] < [Link];}

A helper function to sort processes based on their arrival time.



Used by the sort function to arrange processes in ascending order of
arrival time.

4. SRTF Scheduling Function


cpp

Copy
void SRTF(vector<Process> &processes) {
int n = [Link]();
int currentTime = 0;
int completed = 0;
vector<int> isCompleted(n, 0);

int n = [Link]();: Stores the total number of processes.


int currentTime = 0;: Tracks the current time in the scheduling simulation.


int completed = 0;: Counts the number of processes that have completed
execution.


vector<int> isCompleted(n, 0);: A vector to track whether a process has


completed (1 for completed, 0 for not completed).

cpp

Copy
while (completed != n) {
int shortest = -1;
int minRemainingTime = INT_MAX;

while (completed != n): Continues the scheduling loop until all processes
are completed.


int shortest = -1;: Stores the index of the process with the shortest
remaining time.


int minRemainingTime = INT_MAX;: Stores the minimum remaining time


among the available processes.

cpp

Copy
for (int i = 0; i < n; i++) {
if (processes[i].arrivalTime <= currentTime && !
isCompleted[i] && processes[i].remainingTime <
minRemainingTime) {
shortest = i;
minRemainingTime = processes[i].remainingTime;
}
}

Iterates through all processes to find the one with the shortest
remaining time that has arrived and is not yet completed.


processes[i].arrivalTime <= currentTime: Ensures the process has arrived.


!isCompleted[i]: Ensures the process is not already completed.



processes[i].remainingTime < minRemainingTime: Updates the shortest
process if a better candidate is found.

cpp

Copy
if (shortest == -1) {
currentTime++;
continue;
}

If no process is available to execute at the current time, increment


the currentTime and continue to the next iteration.

cpp

Copy
processes[shortest].remainingTime--;
currentTime++;

Executes the shortest process for 1 unit of time:

Decrements its remainingTime.

o
o

Increments the currentTime.

cpp
Copy
if (processes[shortest].remainingTime == 0) {
processes[shortest].finishTime = currentTime;
processes[shortest].turnaroundTime =
processes[shortest].finishTime - processes[shortest].arrivalTime;
processes[shortest].waitingTime =
processes[shortest].turnaroundTime -
processes[shortest].burstTime;
isCompleted[shortest] = 1;
completed++;
}

If the process completes (remainingTime == 0):

Sets its finishTime to the current time.

o
o

Calculates turnaroundTime and waitingTime.

o
o

Marks the process as completed and increments


the completed counter.

5. Main Function
cpp

Copy
int main() {
int n;
cout << "Enter the number of processes: ";
cin >> n;
vector<Process> processes(n);

Prompts the user to enter the number of processes.


Creates a vector of Process objects to store the process details.

cpp

Copy
for (int i = 0; i < n; i++) {
processes[i].id = i + 1;
cout << "Enter arrival time for process " << processes[i].id <<
": ";
cin >> processes[i].arrivalTime;
cout << "Enter burst time for process " << processes[i].id <<
": ";
cin >> processes[i].burstTime;
processes[i].remainingTime = processes[i].burstTime;
}

Loops through each process to collect its arrivalTime and burstTime from
the user.


Initializes remainingTime to burstTime.

cpp

Copy
sort([Link](), [Link](), compareArrivalTime);

Sorts the processes based on their arrival time using


the compareArrivalTime function.

cpp

Copy
SRTF(processes);

Calls the SRTF function to perform the scheduling.

cpp

Copy
cout << "\nProcess ID\tArrival Time\tBurst Time\tFinish Time\
tTurnaround Time\tWaiting Time\n";
for (const auto &p : processes) {
cout << [Link] << "\t\t" << [Link] << "\t\t" <<
[Link] << "\t\t" << [Link] << "\t\t" <<
[Link] << "\t\t" << [Link] << endl;
}

Displays the results in a tabular format, showing


the id, arrivalTime, burstTime, finishTime, turnaroundTime, and waitingTime for
each process.

cpp

Copy
return 0;}

Ends the program.


How It Works
1.

The program collects process details (arrival time and burst time) from
the user.

2.
3.

It sorts the processes by arrival time.

4.
5.

The SRTF algorithm schedules processes by always selecting the one


with the shortest remaining time.

6.
7.

After scheduling, it calculates and displays the finish time, turnaround


time, and waiting time for each process.

8.

This implementation ensures that the process with the least remaining time is
always executed first, minimizing average waiting time and turnaround time.

#include <iostream> #include <vector> #include <queue> using


namespace std; struct Process { int id; int arrivalTime; int burstTime; int
remainingTime; int finishTime; int turnaroundTime; int waitingTime; }; void
RoundRobin(vector<Process> &processes, int timeQuantum) { int n =
[Link](); int currentTime = 0; queue<int> readyQueue; // Queue to
store indices of processes in the ready queue // Initialize remaining time for all
processes for (int i = 0; i < n; i++) { processes[i].remainingTime =
processes[i].burstTime; } int completed = 0; int idx = 0; // Add processes that
arrive at time 0 to the ready queue while (idx < n &&
processes[idx].arrivalTime <= currentTime) { [Link](idx); idx+
+; } // Process the ready queue while (completed != n) { if (!
[Link]()) { int currentProcess = [Link]();
[Link](); // Execute the process for the time quantum or its
remaining time, whichever is smaller int executionTime = min(timeQuantum,
processes[currentProcess].remainingTime);
processes[currentProcess].remainingTime -= executionTime; currentTime +=
executionTime; // Add newly arrived processes to the ready queue while (idx
< n && processes[idx].arrivalTime <= currentTime) { [Link](idx);
idx++; } // If the process is not completed, add it back to the ready queue if
(processes[currentProcess].remainingTime > 0)
{ [Link](currentProcess); } else { // Process is completed
processes[currentProcess].finishTime = currentTime;
processes[currentProcess].turnaroundTime =
processes[currentProcess].finishTime - processes[currentProcess].arrivalTime;
processes[currentProcess].waitingTime =
processes[currentProcess].turnaroundTime -
processes[currentProcess].burstTime; completed++; } } else { // If no process
is in the ready queue, increment time currentTime++; // Check if any new
process arrives at this time while (idx < n && processes[idx].arrivalTime <=
currentTime) { [Link](idx); idx++; } } } } int main() { int n,
timeQuantum; cout << "Enter the number of processes: "; cin >> n; cout <<
"Enter the time quantum: "; cin >> timeQuantum; vector<Process>
processes(n); // Input process details for (int i = 0; i < n; i++) { processes[i].id
= i + 1; cout << "Enter arrival time for process " << processes[i].id << ": ";
cin >> processes[i].arrivalTime; cout << "Enter burst time for process " <<
processes[i].id << ": "; cin >> processes[i].burstTime; } // Apply Round Robin
scheduling RoundRobin(processes, timeQuantum); // Display results cout <<
"\nProcess ID\tArrival Time\tBurst Time\tFinish Time\tTurnaround Time\
tWaiting Time\n"; for (const auto &p : processes) { cout << [Link] << "\t\t" <<
[Link] << "\t\t" << [Link] << "\t\t" << [Link] << "\t\t" <<
[Link] << "\t\t" << [Link] << endl; } return 0; } DO THE
SAME FOR THIS

Certainly! Below is a detailed explanation of each line of code in the provided


C++ program, which implements the Round Robin (RR) scheduling
algorithm.

1. Header Files and Namespace


cpp

Copy
#include <iostream>#include <vector>#include <queue>
using namespace std;

#include <iostream>: Includes the standard input-output library for


handling input and output operations (e.g., cin, cout).


#include <vector>: Includes the vector library, which provides a dynamic


array-like container.


#include <queue>: Includes the queue library, which provides a FIFO


(First-In-First-Out) data structure.


using namespace std;: Allows us to use standard library functions and


objects (e.g., cout, vector, queue) without the std:: prefix.

2. Process Structure
cpp

Copy
struct Process {
int id;
int arrivalTime;
int burstTime;
int remainingTime;
int finishTime;
int turnaroundTime;
int waitingTime;};

Defines a struct named Process to represent a process in the scheduling


algorithm.


Members:

id: Unique identifier for the process.

o
o

arrivalTime: Time at which the process arrives in the ready queue.

o
o
burstTime: Total CPU time required by the process.

o
o

remainingTime: Remaining CPU time needed to complete the


process.

o
o

finishTime: Time at which the process completes execution.

o
o

turnaroundTime: Total time taken from arrival to completion


(finishTime - arrivalTime).

o
o

waitingTime: Total time the process spends waiting in the ready


queue (turnaroundTime - burstTime).

3. Round Robin Scheduling Function


cpp

Copy
void RoundRobin(vector<Process> &processes, int timeQuantum) {
int n = [Link]();
int currentTime = 0;
queue<int> readyQueue; // Queue to store indices of processes
in the ready queue

int n = [Link]();: Stores the total number of processes.


int currentTime = 0;: Tracks the current time in the scheduling simulation.



queue<int> readyQueue;: A queue to store the indices of processes that
are in the ready queue.

cpp

Copy
// Initialize remaining time for all processes
for (int i = 0; i < n; i++) {
processes[i].remainingTime = processes[i].burstTime;
}

Initializes the remainingTime of each process to its burstTime.

cpp

Copy
int completed = 0;
int idx = 0;

int completed = 0;: Counts the number of processes that have completed
execution.


int idx = 0;: Tracks the index of the next process to be added to the
ready queue.

cpp

Copy
// Add processes that arrive at time 0 to the ready queue
while (idx < n && processes[idx].arrivalTime <= currentTime) {
[Link](idx);
idx++;
}

Adds processes that arrive at time 0 to the ready queue.

cpp

Copy
// Process the ready queue
while (completed != n) {
if (![Link]()) {
int currentProcess = [Link]();
[Link]();

while (completed != n): Continues the scheduling loop until all processes
are completed.


if (![Link]()): Checks if there are processes in the ready


queue.


int currentProcess = [Link]();: Gets the index of the process at


the front of the queue.


[Link]();: Removes the process from the queue.

cpp

Copy
// Execute the process for the time quantum or its remaining
time, whichever is smaller
int executionTime = min(timeQuantum,
processes[currentProcess].remainingTime);
processes[currentProcess].remainingTime -= executionTime;
currentTime += executionTime;

int executionTime = min(timeQuantum,


processes[currentProcess].remainingTime);: Determines the execution time
as the minimum of the time quantum and the remaining time of the
process.


processes[currentProcess].remainingTime -= executionTime;: Reduces the


remaining time of the process by the execution time.


currentTime += executionTime;: Advances the current time by the


execution time.

cpp

Copy
// Add newly arrived processes to the ready queue
while (idx < n && processes[idx].arrivalTime <=
currentTime) {
[Link](idx);
idx++;
}

Adds any new processes that have arrived during the execution time to
the ready queue.


cpp

Copy
// If the process is not completed, add it back to the ready
queue
if (processes[currentProcess].remainingTime > 0) {
[Link](currentProcess);
} else {
// Process is completed
processes[currentProcess].finishTime = currentTime;
processes[currentProcess].turnaroundTime =
processes[currentProcess].finishTime -
processes[currentProcess].arrivalTime;
processes[currentProcess].waitingTime =
processes[currentProcess].turnaroundTime -
processes[currentProcess].burstTime;
completed++;
}

if (processes[currentProcess].remainingTime > 0): If the process is not


completed, it is added back to the ready queue.


else: If the process is completed:

Sets its finishTime to the current time.

o
o

Calculates turnaroundTime and waitingTime.

o
o

Increments the completed counter.

o
cpp

Copy
} else {
// If no process is in the ready queue, increment time
currentTime++;
// Check if any new process arrives at this time
while (idx < n && processes[idx].arrivalTime <=
currentTime) {
[Link](idx);
idx++;
}
}
}}

If no process is in the ready queue, the current time is incremented,


and any new processes that arrive at this time are added to the ready
queue.

4. Main Function
cpp

Copy
int main() {
int n, timeQuantum;
cout << "Enter the number of processes: ";
cin >> n;
cout << "Enter the time quantum: ";
cin >> timeQuantum;

Prompts the user to enter the number of processes and the time
quantum.

cpp

Copy
vector<Process> processes(n);

// Input process details


for (int i = 0; i < n; i++) {
processes[i].id = i + 1;
cout << "Enter arrival time for process " << processes[i].id <<
": ";
cin >> processes[i].arrivalTime;
cout << "Enter burst time for process " << processes[i].id <<
": ";
cin >> processes[i].burstTime;
}

Collects the arrival time and burst time for each process from the user.

cpp

Copy
// Apply Round Robin scheduling
RoundRobin(processes, timeQuantum);

Calls the RoundRobin function to perform the scheduling.

cpp

Copy
// Display results
cout << "\nProcess ID\tArrival Time\tBurst Time\tFinish Time\
tTurnaround Time\tWaiting Time\n";
for (const auto &p : processes) {
cout << [Link] << "\t\t" << [Link] << "\t\t" <<
[Link] << "\t\t" << [Link] << "\t\t" <<
[Link] << "\t\t" << [Link] << endl;
}

Displays the results in a tabular format, showing


the id, arrivalTime, burstTime, finishTime, turnaroundTime, and waitingTime for
each process.

cpp

Copy
return 0;}

Ends the program.

How It Works
1.

The program collects process details (arrival time and burst time) and
the time quantum from the user.

2.
3.

It initializes the remaining time for each process.

4.
5.

The Round Robin algorithm schedules processes by executing each


process for the time quantum or until it completes, whichever is
shorter.
6.
7.

If a process does not complete within the time quantum, it is added


back to the ready queue.

8.
9.

After scheduling, it calculates and displays the finish time, turnaround


time, and waiting time for each process.

10.

This implementation ensures fair CPU time allocation among processes,


making it suitable for time-sharing systems.

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


<algorithm> using namespace std; struct Process { int id; int arrivalTime; int
burstTime; int priority; // Priority of the process (lower value means higher
priority) int remainingTime; int finishTime; int turnaroundTime; int
waitingTime; }; // Comparator for priority queue (higher priority first) struct
ComparePriority { bool operator()(const Process &a, const Process &b)
{ return [Link] > [Link]; // Lower value means higher priority } }; //
Function to perform Priority Scheduling (for high-priority queue) void
PriorityScheduling(vector<Process> &processes) { int n = [Link]();
int currentTime = 0; priority_queue<Process, vector<Process>,
ComparePriority> readyQueue; // Sort processes by arrival time
sort([Link](), [Link](), [](const Process &a, const Process
&b) { return [Link] < [Link]; }); int idx = 0; while (idx < n || !
[Link]()) { // Add processes that have arrived by the current time
while (idx < n && processes[idx].arrivalTime <= currentTime)
{ [Link](processes[idx]); idx++; } if (![Link]())
{ Process currentProcess = [Link](); [Link](); // Execute
the process currentTime += [Link];
[Link] = currentTime; [Link] =
[Link] - [Link];
[Link] = [Link] -
[Link]; // Update the process in the original vector for
(auto &p : processes) { if ([Link] == [Link]) { p = currentProcess;
break; } } } else { // If no process is available, increment time currentTime+
+; } } } // Function to perform Round Robin Scheduling (for low-priority
queue) void RoundRobin(vector<Process> &processes, int timeQuantum)
{ int n = [Link](); int currentTime = 0; queue<int> readyQueue; //
Initialize remaining time for all processes for (int i = 0; i < n; i++)
{ processes[i].remainingTime = processes[i].burstTime; } int completed = 0;
int idx = 0; // Add processes that arrive at time 0 to the ready queue while
(idx < n && processes[idx].arrivalTime <= currentTime)
{ [Link](idx); idx++; } // Process the ready queue while
(completed != n) { if (![Link]()) { int currentProcess =
[Link](); [Link](); // Execute the process for the time
quantum or its remaining time, whichever is smaller int executionTime =
min(timeQuantum, processes[currentProcess].remainingTime);
processes[currentProcess].remainingTime -= executionTime; currentTime +=
executionTime; // Add newly arrived processes to the ready queue while (idx
< n && processes[idx].arrivalTime <= currentTime) { [Link](idx);
idx++; } // If the process is not completed, add it back to the ready queue if
(processes[currentProcess].remainingTime > 0)
{ [Link](currentProcess); } else { // Process is completed
processes[currentProcess].finishTime = currentTime;
processes[currentProcess].turnaroundTime =
processes[currentProcess].finishTime - processes[currentProcess].arrivalTime;
processes[currentProcess].waitingTime =
processes[currentProcess].turnaroundTime -
processes[currentProcess].burstTime; completed++; } } else { // If no process
is in the ready queue, increment time currentTime++; // Check if any new
process arrives at this time while (idx < n && processes[idx].arrivalTime <=
currentTime) { [Link](idx); idx++; } } } } int main() { int n,
timeQuantum; cout << "Enter the number of processes: "; cin >> n; cout <<
"Enter the time quantum for Round Robin (low-priority queue): "; cin >>
timeQuantum; vector<Process> processes(n); // Input process details for (int i
= 0; i < n; i++) { processes[i].id = i + 1; cout << "Enter arrival time for
process " << processes[i].id << ": "; cin >> processes[i].arrivalTime; cout <<
"Enter burst time for process " << processes[i].id << ": "; cin >>
processes[i].burstTime; cout << "Enter priority for process " <<
processes[i].id << " (1 for high-priority, 2 for low-priority): "; cin >>
processes[i].priority; } // Separate processes into high-priority and low-priority
queues vector<Process> highPriorityQueue; vector<Process>
lowPriorityQueue; for (const auto &p : processes) { if ([Link] == 1)
{ highPriorityQueue.push_back(p); } else
{ lowPriorityQueue.push_back(p); } } // Schedule high-priority queue using
Priority Scheduling PriorityScheduling(highPriorityQueue); // Schedule low-
priority queue using Round Robin Scheduling RoundRobin(lowPriorityQueue,
timeQuantum); // Combine the results vector<Process> finalProcesses;
[Link]([Link](), [Link](),
[Link]()); [Link]([Link](),
[Link](), [Link]()); // Display results cout <<
"\nProcess ID\tArrival Time\tBurst Time\tPriority\tFinish Time\tTurnaround
Time\tWaiting Time\n"; for (const auto &p : finalProcesses) { cout << [Link] <<
"\t\t" << [Link] << "\t\t" << [Link] << "\t\t" << [Link] << "\
t\t" << [Link] << "\t\t" << [Link] << "\t\t" <<
[Link] << endl; } return 0; }DO THE SAME FOR THIS ALSO

Certainly! Below is a detailed explanation of each line of code in the provided


C++ program, which implements a Multi-Level Queue
Scheduling algorithm. This program uses Priority Scheduling for high-
priority processes and Round Robin Scheduling for low-priority processes.

1. Header Files and Namespace


cpp
Copy
#include <iostream>#include <vector>#include <queue>#include
<algorithm>
using namespace std;

#include <iostream>: Includes the standard input-output library for
handling input and output operations (e.g., cin, cout).


#include <vector>: Includes the vector library, which provides a dynamic
array-like container.


#include <queue>: Includes the queue library, which provides a FIFO
(First-In-First-Out) data structure and a priority queue.


#include <algorithm>: Includes the algorithm library, which provides
functions like sort for sorting data structures.


using namespace std;: Allows us to use standard library functions and
objects (e.g., cout, vector, queue) without the std:: prefix.

2. Process Structure
cpp
Copy
struct Process {
int id;
int arrivalTime;
int burstTime;
int priority; // Priority of the process (lower value means higher
priority)
int remainingTime;
int finishTime;
int turnaroundTime;
int waitingTime;};

Defines a struct named Process to represent a process in the scheduling
algorithm.


Members:

o
id: Unique identifier for the process.
o
o
arrivalTime: Time at which the process arrives in the ready queue.
o
o
burstTime: Total CPU time required by the process.
o
o
priority: Priority of the process (1 for high-priority, 2 for low-
priority).
o
o
remainingTime: Remaining CPU time needed to complete the
process.
o
o
finishTime: Time at which the process completes execution.
o
o
turnaroundTime: Total time taken from arrival to completion
(finishTime - arrivalTime).
o
o
waitingTime: Total time the process spends waiting in the ready
queue (turnaroundTime - burstTime).
o

3. Comparator for Priority Queue


cpp
Copy
struct ComparePriority {
bool operator()(const Process &a, const Process &b) {
return [Link] > [Link]; // Lower value means higher
priority
}};

A custom comparator for the priority queue to ensure that processes
with lower priority values (higher priority) are executed first.

4. Priority Scheduling Function (High-Priority Queue)


cpp
Copy
void PriorityScheduling(vector<Process> &processes) {
int n = [Link]();
int currentTime = 0;
priority_queue<Process, vector<Process>, ComparePriority>
readyQueue;

int n = [Link]();: Stores the total number of processes.


int currentTime = 0;: Tracks the current time in the scheduling simulation.


priority_queue<Process, vector<Process>, ComparePriority> readyQueue; : A
priority queue to store processes based on their priority.

cpp
Copy
// Sort processes by arrival time
sort([Link](), [Link](), [](const Process &a,
const Process &b) {
return [Link] < [Link];
});

Sorts the processes by their arrival time to ensure they are processed
in the correct order.

cpp
Copy
int idx = 0;
while (idx < n || ![Link]()) {
// Add processes that have arrived by the current time
while (idx < n && processes[idx].arrivalTime <= currentTime)
{
[Link](processes[idx]);
idx++;
}

Adds processes that have arrived by the current time to the priority
queue.

cpp
Copy
if (![Link]()) {
Process currentProcess = [Link]();
[Link]();

// Execute the process


currentTime += [Link];
[Link] = currentTime;
[Link] = [Link]
- [Link];
[Link] =
[Link] - [Link];

If the priority queue is not empty, the process with the highest priority
is executed.


The process's finishTime, turnaroundTime, and waitingTime are calculated.

cpp
Copy
// Update the process in the original vector
for (auto &p : processes) {
if ([Link] == [Link]) {
p = currentProcess;
break;
}
}
} else {
// If no process is available, increment time
currentTime++;
}
}}

Updates the process details in the original vector.


If no process is available, the current time is incremented.

5. Round Robin Scheduling Function (Low-Priority


Queue)
cpp
Copy
void RoundRobin(vector<Process> &processes, int timeQuantum) {
int n = [Link]();
int currentTime = 0;
queue<int> readyQueue;

int n = [Link]();: Stores the total number of processes.


int currentTime = 0;: Tracks the current time in the scheduling simulation.


queue<int> readyQueue;: A queue to store the indices of processes that
are in the ready queue.

cpp
Copy
// Initialize remaining time for all processes
for (int i = 0; i < n; i++) {
processes[i].remainingTime = processes[i].burstTime;
}

Initializes the remainingTime of each process to its burstTime.

cpp
Copy
int completed = 0;
int idx = 0;

// Add processes that arrive at time 0 to the ready queue


while (idx < n && processes[idx].arrivalTime <= currentTime) {
[Link](idx);
idx++;
}

Adds processes that arrive at time 0 to the ready queue.

cpp
Copy
// Process the ready queue
while (completed != n) {
if (![Link]()) {
int currentProcess = [Link]();
[Link]();

// Execute the process for the time quantum or its remaining


time, whichever is smaller
int executionTime = min(timeQuantum,
processes[currentProcess].remainingTime);
processes[currentProcess].remainingTime -= executionTime;
currentTime += executionTime;

Executes the process for the time quantum or its remaining time,
whichever is smaller.

cpp
Copy
// Add newly arrived processes to the ready queue
while (idx < n && processes[idx].arrivalTime <=
currentTime) {
[Link](idx);
idx++;
}

// If the process is not completed, add it back to the ready


queue
if (processes[currentProcess].remainingTime > 0) {
[Link](currentProcess);
} else {
// Process is completed
processes[currentProcess].finishTime = currentTime;
processes[currentProcess].turnaroundTime =
processes[currentProcess].finishTime -
processes[currentProcess].arrivalTime;
processes[currentProcess].waitingTime =
processes[currentProcess].turnaroundTime -
processes[currentProcess].burstTime;
completed++;
}
} else {
// If no process is in the ready queue, increment time
currentTime++;
// Check if any new process arrives at this time
while (idx < n && processes[idx].arrivalTime <=
currentTime) {
[Link](idx);
idx++;
}
}
}}

Adds newly arrived processes to the ready queue.


If the process is not completed, it is added back to the ready queue.


If the process is completed, its finishTime, turnaroundTime,
and waitingTime are calculated.

6. Main Function
cpp
Copy
int main() {
int n, timeQuantum;
cout << "Enter the number of processes: ";
cin >> n;
cout << "Enter the time quantum for Round Robin (low-priority
queue): ";
cin >> timeQuantum;

vector<Process> processes(n);

Prompts the user to enter the number of processes and the time
quantum for the Round Robin scheduling.

cpp
Copy
// Input process details
for (int i = 0; i < n; i++) {
processes[i].id = i + 1;
cout << "Enter arrival time for process " << processes[i].id <<
": ";
cin >> processes[i].arrivalTime;
cout << "Enter burst time for process " << processes[i].id <<
": ";
cin >> processes[i].burstTime;
cout << "Enter priority for process " << processes[i].id << " (1
for high-priority, 2 for low-priority): ";
cin >> processes[i].priority;
}

Collects the arrival time, burst time, and priority for each process from
the user.

cpp
Copy
// Separate processes into high-priority and low-priority queues
vector<Process> highPriorityQueue;
vector<Process> lowPriorityQueue;

for (const auto &p : processes) {


if ([Link] == 1) {
highPriorityQueue.push_back(p);
} else {
lowPriorityQueue.push_back(p);
}
}

Separates the processes into high-priority and low-priority queues
based on their priority.

cpp
Copy
// Schedule high-priority queue using Priority Scheduling
PriorityScheduling(highPriorityQueue);

// Schedule low-priority queue using Round Robin Scheduling


RoundRobin(lowPriorityQueue, timeQuantum);

Schedules the high-priority queue using Priority Scheduling.


Schedules the low-priority queue using Round Robin Scheduling.

cpp
Copy
// Combine the results
vector<Process> finalProcesses;
[Link]([Link](),
[Link](), [Link]());
[Link]([Link](),
[Link](), [Link]());

Combines the results from both queues into a single vector.

cpp
Copy
// Display results
cout << "\nProcess ID\tArrival Time\tBurst Time\tPriority\tFinish
Time\tTurnaround Time\tWaiting Time\n";
for (const auto &p : finalProcesses) {
cout << [Link] << "\t\t" << [Link] << "\t\t" <<
[Link] << "\t\t" << [Link] << "\t\t" << [Link] << "\t\
t" << [Link] << "\t\t" << [Link] << endl;
}

Displays the results in a tabular format, showing
the id, arrivalTime, burstTime, priority, finishTime, turnaroundTime,
and waitingTime for each process.

cpp
Copy
return 0;}

Ends the program.

How It Works
1.
The program collects process details (arrival time, burst time, and
priority) and the time quantum for Round Robin scheduling from the
user.
2.
3.
It separates the processes into high-priority and low-priority queues.
4.
5.
High-priority processes are scheduled using Priority Scheduling.
6.
7.
Low-priority processes are scheduled using Round Robin Scheduling.
8.
9.
The results from both queues are combined and displayed.
10.

This implementation ensures that high-priority processes are executed first,


while low-priority processes are executed fairly using Round Robin scheduling.

New chat

You might also like