0% found this document useful (0 votes)
4 views

OS PROGRAMS

The document contains C++ implementations of various CPU scheduling algorithms including Shortest Job First (SJF), Longest Job First (LJF), Shortest Remaining Time First (SRTF), Longest Remaining Time First (LRTF), and Round Robin. Each algorithm is implemented in a separate function, with classes defined to manage process attributes such as PID, burst time, waiting time, and turnaround time. The main function for each algorithm collects user input for process details and displays the scheduling results including average waiting and turnaround times.

Uploaded by

Shan Ali5656
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

OS PROGRAMS

The document contains C++ implementations of various CPU scheduling algorithms including Shortest Job First (SJF), Longest Job First (LJF), Shortest Remaining Time First (SRTF), Longest Remaining Time First (LRTF), and Round Robin. Each algorithm is implemented in a separate function, with classes defined to manage process attributes such as PID, burst time, waiting time, and turnaround time. The main function for each algorithm collects user input for process details and displays the scheduling results including average waiting and turnaround times.

Uploaded by

Shan Ali5656
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

NAME : SYED SHAN-E-ALI

ID : f2022266476
ASSIGNMENT NO 2
TASK 1 :
SJF C++ PROGRAM:
#include <iostream>

#include <algorithm>

using namespace std;

class Process {

public:

int pid;

int burstTime;

int waitingTime;

int turnaroundTime;

Process() : pid(0), burstTime(0), waitingTime(0), turnaroundTime(0) {}

static void sortByBurstTime(Process processes[], int n) {

sort(processes, processes + n, [](Process a, Process b) {

return a.burstTime < b.burstTime;

});

static void calculateWaitingTime(Process processes[], int n) {

processes[0].waitingTime = 0;

for (int i = 1; i < n; ++i) {

processes[i].waitingTime = processes[i - 1].waitingTime + processes[i - 1].burstTime;

static void calculateTurnaroundTime(Process processes[], int n) {


for (int i = 0; i < n; ++i) {

processes[i].turnaroundTime = processes[i].waitingTime + processes[i].burstTime;

};

void SJF(Process processes[], int n) {

Process::sortByBurstTime(processes, n);

Process::calculateWaitingTime(processes, n);

Process::calculateTurnaroundTime(processes, n);

cout << "\nProcess\tBurst Time\tWaiting Time\tTurnaround Time\n";

for (int i = 0; i < n; ++i) {

cout << "P" << processes[i].pid << "\t\t"

<< processes[i].burstTime << "\t\t"

<< processes[i].waitingTime << "\t\t"

<< processes[i].turnaroundTime << endl;

double totalWaitingTime = 0, totalTurnaroundTime = 0;

for (int i = 0; i < n; ++i) {

totalWaitingTime += processes[i].waitingTime;

totalTurnaroundTime += processes[i].turnaroundTime;

cout << "\nAverage Waiting Time: " << totalWaitingTime / n;

cout << "\nAverage Turnaround Time: " << totalTurnaroundTime / n << endl;

int main() {

int n;

cout << "Enter the number of processes: ";

cin >> n;

Process processes[n];
for (int i = 0; i < n; ++i) {

processes[i].pid = i + 1;

cout << "Enter burst time for Process " << processes[i].pid << ": ";

cin >> processes[i].burstTime;

SJF(processes, n);

return 0;

QNO 2(LONGEST JOB FIRST):


#include <iostream>

#include <algorithm>

using namespace std;

class Process {

public:

int pid;

int burstTime;

int waitingTime;

int turnaroundTime;

Process() : pid(0), burstTime(0), waitingTime(0), turnaroundTime(0) {}

static void sortByBurstTimeDesc(Process processes[], int n) {

sort(processes, processes + n, [](Process a, Process b) {

return a.burstTime > b.burstTime;

});

static void calculateWaitingTime(Process processes[], int n) {

processes[0].waitingTime = 0;

for (int i = 1; i < n; ++i) {

processes[i].waitingTime = processes[i - 1].waitingTime + processes[i - 1].burstTime;


}

static void calculateTurnaroundTime(Process processes[], int n) {

for (int i = 0; i < n; ++i) {

processes[i].turnaroundTime = processes[i].waitingTime + processes[i].burstTime;

};

void LJF(Process processes[], int n) {

Process::sortByBurstTimeDesc(processes, n);

Process::calculateWaitingTime(processes, n);

Process::calculateTurnaroundTime(processes, n);

cout << "\nProcess\tBurst Time\tWaiting Time\tTurnaround Time\n";

for (int i = 0; i < n; ++i) {

cout << "P" << processes[i].pid << "\t\t"

<< processes[i].burstTime << "\t\t"

<< processes[i].waitingTime << "\t\t"

<< processes[i].turnaroundTime << endl;

double totalWaitingTime = 0, totalTurnaroundTime = 0;

for (int i = 0; i < n; ++i) {

totalWaitingTime += processes[i].waitingTime;

totalTurnaroundTime += processes[i].turnaroundTime;

cout << "\nAverage Waiting Time: " << totalWaitingTime / n;

cout << "\nAverage Turnaround Time: " << totalTurnaroundTime / n << endl;

int main() {

int n;
cout << "Enter the number of processes: ";

cin >> n;

Process processes[n];

for (int i = 0; i < n; ++i) {

processes[i].pid = i + 1;

cout << "Enter burst time for Process " << processes[i].pid << ": ";

cin >> processes[i].burstTime;

LJF(processes, n);

return 0;

Qno 3(SRTF):
#include <iostream>

#include <climits>

using namespace std;

class Process {

public:

int pid;

int arrivalTime;

int burstTime;

int remainingTime;

int completionTime;

int waitingTime;

int turnaroundTime;

Process() : pid(0), arrivalTime(0), burstTime(0), remainingTime(0),

completionTime(0), waitingTime(0), turnaroundTime(0) {}


};

void SRTF(Process processes[], int n) {

int currentTime = 0;

int completed = 0;

int minIndex = -1;

int minRemainingTime = INT_MAX;

for (int i = 0; i < n; ++i) {

processes[i].remainingTime = processes[i].burstTime;

while (completed < n) {

minIndex = -1;

minRemainingTime = INT_MAX;

for (int i = 0; i < n; ++i) {

if (processes[i].arrivalTime <= currentTime && processes[i].remainingTime > 0 &&

processes[i].remainingTime < minRemainingTime) {

minIndex = i;

minRemainingTime = processes[i].remainingTime;

if (minIndex == -1) {

currentTime++;

continue;

processes[minIndex].remainingTime--;

currentTime++;

if (processes[minIndex].remainingTime == 0) {

completed++;

processes[minIndex].completionTime = currentTime;
processes[minIndex].turnaroundTime = processes[minIndex].completionTime -
processes[minIndex].arrivalTime;

processes[minIndex].waitingTime = processes[minIndex].turnaroundTime -
processes[minIndex].burstTime;

cout << "\nProcess\tArrival Time\tBurst Time\tCompletion Time\tTurnaround Time\tWaiting Time\n";

for (int i = 0; i < n; ++i) {

cout << "P" << processes[i].pid << "\t\t" << processes[i].arrivalTime << "\t\t"

<< processes[i].burstTime << "\t\t" << processes[i].completionTime << "\t\t"

<< processes[i].turnaroundTime << "\t\t" << processes[i].waitingTime << endl;

double totalWaitingTime = 0, totalTurnaroundTime = 0;

for (int i = 0; i < n; ++i) {

totalWaitingTime += processes[i].waitingTime;

totalTurnaroundTime += processes[i].turnaroundTime;

cout << "\nAverage Waiting Time: " << totalWaitingTime / n;

cout << "\nAverage Turnaround Time: " << totalTurnaroundTime / n << endl;

int main() {

int n;

cout << "Enter the number of processes: ";

cin >> n;

Process processes[n];

for (int i = 0; i < n; ++i) {

processes[i].pid = i + 1;

cout << "Enter arrival time for Process " << processes[i].pid << ": ";

cin >> processes[i].arrivalTime;


cout << "Enter burst time for Process " << processes[i].pid << ": ";

cin >> processes[i].burstTime;

SRTF(processes, n);

return 0;

QNO4(LRTF):
#include <iostream>

#include <climits>

using namespace std;

class Process {

public:

int pid;

int arrivalTime;

int burstTime;

int remainingTime;

int completionTime;

int waitingTime;

int turnaroundTime;

Process() : pid(0), arrivalTime(0), burstTime(0), remainingTime(0),

completionTime(0), waitingTime(0), turnaroundTime(0) {}

};

void LRTF(Process processes[], int n) {

int currentTime = 0;

int completed = 0;

int maxIndex = -1;

int maxRemainingTime = INT_MIN;


for (int i = 0; i < n; ++i) {

processes[i].remainingTime = processes[i].burstTime;

while (completed < n) {

maxIndex = -1;

maxRemainingTime = INT_MIN;

for (int i = 0; i < n; ++i) {

if (processes[i].arrivalTime <= currentTime && processes[i].remainingTime > 0 &&

processes[i].remainingTime > maxRemainingTime) {

maxIndex = i;

maxRemainingTime = processes[i].remainingTime;

if (maxIndex == -1) {

currentTime++;

continue;

processes[maxIndex].remainingTime--;

currentTime++;

if (processes[maxIndex].remainingTime == 0) {

completed++;

processes[maxIndex].completionTime = currentTime;

processes[maxIndex].turnaroundTime = processes[maxIndex].completionTime -
processes[maxIndex].arrivalTime;

processes[maxIndex].waitingTime = processes[maxIndex].turnaroundTime -
processes[maxIndex].burstTime;

cout << "\nProcess\tArrival Time\tBurst Time\tCompletion Time\tTurnaround Time\tWaiting Time\n";


for (int i = 0; i < n; ++i) {

cout << "P" << processes[i].pid << "\t\t" << processes[i].arrivalTime << "\t\t"

<< processes[i].burstTime << "\t\t" << processes[i].completionTime << "\t\t"

<< processes[i].turnaroundTime << "\t\t" << processes[i].waitingTime << endl;

double totalWaitingTime = 0, totalTurnaroundTime = 0;

for (int i = 0; i < n; ++i) {

totalWaitingTime += processes[i].waitingTime;

totalTurnaroundTime += processes[i].turnaroundTime;

cout << "\nAverage Waiting Time: " << totalWaitingTime / n;

cout << "\nAverage Turnaround Time: " << totalTurnaroundTime / n << endl;

int main() {

int n;

cout << "Enter the number of processes: ";

cin >> n;

Process processes[n];

for (int i = 0; i < n; ++i) {

processes[i].pid = i + 1;

cout << "Enter arrival time for Process " << processes[i].pid << ": ";

cin >> processes[i].arrivalTime;

cout << "Enter burst time for Process " << processes[i].pid << ": ";

cin >> processes[i].burstTime;

LRTF(processes, n);

return 0;

}
QNO 5(ROUND ROBIN):
#include <iostream>

#include <queue>

using namespace std;

class Process {

public:

int pid;

int arrivalTime;

int burstTime;

int remainingTime;

int waitingTime;

int turnaroundTime;

int completionTime;

Process() : pid(0), arrivalTime(0), burstTime(0), remainingTime(0),

waitingTime(0), turnaroundTime(0), completionTime(0) {}

};

void RoundRobin(Process processes[], int n, int timeQuantum) {

queue<int> readyQueue;

int currentTime = 0, completed = 0;

bool inQueue[n] = {false};

for (int i = 0; i < n; ++i) {

processes[i].remainingTime = processes[i].burstTime;

while (completed < n) {

for (int i = 0; i < n; ++i) {

if (processes[i].arrivalTime <= currentTime && !inQueue[i] && processes[i].remainingTime > 0) {

readyQueue.push(i);

inQueue[i] = true;
}

if (readyQueue.empty()) {

currentTime++;

continue;

int currentIndex = readyQueue.front();

readyQueue.pop();

inQueue[currentIndex] = false;

int executionTime = min(processes[currentIndex].remainingTime, timeQuantum);

processes[currentIndex].remainingTime -= executionTime;

currentTime += executionTime;

for (int i = 0; i < n; ++i) {

if (i != currentIndex && processes[i].arrivalTime <= currentTime && processes[i].remainingTime >


0 && !inQueue[i]) {

readyQueue.push(i);

inQueue[i] = true;

if (processes[currentIndex].remainingTime > 0) {

readyQueue.push(currentIndex);

inQueue[currentIndex] = true;

} else {

completed++;

processes[currentIndex].completionTime = currentTime;

processes[currentIndex].turnaroundTime = processes[currentIndex].completionTime -
processes[currentIndex].arrivalTime;
processes[currentIndex].waitingTime = processes[currentIndex].turnaroundTime -
processes[currentIndex].burstTime;

cout << "\nProcess\tArrival Time\tBurst Time\tCompletion Time\tTurnaround Time\tWaiting Time\n";

for (int i = 0; i < n; ++i) {

cout << "P" << processes[i].pid << "\t\t" << processes[i].arrivalTime << "\t\t"

<< processes[i].burstTime << "\t\t" << processes[i].completionTime << "\t\t"

<< processes[i].turnaroundTime << "\t\t" << processes[i].waitingTime << endl;

double totalWaitingTime = 0, totalTurnaroundTime = 0;

for (int i = 0; i < n; ++i) {

totalWaitingTime += processes[i].waitingTime;

totalTurnaroundTime += processes[i].turnaroundTime;

cout << "\nAverage Waiting Time: " << totalWaitingTime / n;

cout << "\nAverage Turnaround Time: " << totalTurnaroundTime / n << endl;

int main() {

int n, timeQuantum;

cout << "Enter the number of processes: ";

cin >> n;

Process processes[n];

for (int i = 0; i < n; ++i) {

processes[i].pid = i + 1;

cout << "Enter arrival time for Process " << processes[i].pid << ": ";

cin >> processes[i].arrivalTime;

cout << "Enter burst time for Process " << processes[i].pid << ": ";
cin >> processes[i].burstTime;

cout << "Enter time quantum: ";

cin >> timeQuantum;

RoundRobin(processes, n, timeQuantum);

return 0;

You might also like