Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
17 views8 pages

Scheduling OS

The document contains C++ implementations for various CPU scheduling algorithms including FCFS, SJF, non-preemptive priority scheduling, and preemptive priority scheduling. Each algorithm calculates process metrics such as turnaround time, waiting time, and displays a Gantt chart. The code allows users to input process details and outputs the scheduling results along with average times.

Uploaded by

singlerverma
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)
17 views8 pages

Scheduling OS

The document contains C++ implementations for various CPU scheduling algorithms including FCFS, SJF, non-preemptive priority scheduling, and preemptive priority scheduling. Each algorithm calculates process metrics such as turnaround time, waiting time, and displays a Gantt chart. The code allows users to input process details and outputs the scheduling results along with average times.

Uploaded by

singlerverma
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
You are on page 1/ 8

FCFS

#include <iostream>
#include <algorithm>
#include <iomanip>
using namespace std;
struct Process {
int id, arrival, burst, completion, turnaround, waiting, start; };
bool compareArrival(Process a, Process b) {
return a.arrival < b.arrival; }
int main() {
int n;
cout << "Enter number of processes: ";
cin >> n;
Process p[n];
for (int i = 0; i < n; i++) {
p[i].id = i + 1;
cout << "Enter arrival time & burst time for Process P" << p[i].id <<
": ";
cin >> p[i].arrival>> p[i].burst; }
sort(p, p + n, compareArrival);
float currentTime = 0, totalTAT = 0, totalWT = 0;
for (int i = 0; i < n; i++) {
if (currentTime < p[i].arrival)
currentTime = p[i].arrival;
p[i].start = currentTime;
p[i].completion = currentTime + p[i].burst;
p[i].turnaround = p[i].completion - p[i].arrival;
p[i].waiting = p[i].turnaround - p[i].burst;
currentTime = p[i].completion;
totalTAT += p[i].turnaround;
totalWT += p[i].waiting; }
cout << "\nPID\tAT\tBT\tCT\tTAT\tWT\n";
for (int i = 0; i < n; i++) {
cout << "P" << p[i].id << "\t"<< p[i].arrival << "\t"<< p[i].burst <<
"\t"<< p[i].completion << "\t"<< p[i].turnaround << "\t"<< p[i].waiting << "\
n"; }
cout << "\nGantt Chart:";
cout << "\n|";
for (int i = 0; i < n; i++) {
cout << " P" << p[i].id << " |"; }
cout << "\n" << p[0].start;
for (int i = 0; i < n; i++) {
cout << setw(7) << p[i].completion; }
cout << "\nAverage Turnaround Time: " << totalTAT / n;
cout << "\nAverage Waiting Time: " << totalWT / n << "\n";
return 0; }

SJF
#include <iostream>
#include <algorithm>
#include <iomanip>
using namespace std;
struct Process {
int id,arrival,burst,completion,turnaround,waiting,start;
bool isCompleted = false; };
int main() {
int n;
cout << "Enter number of processes: ";
cin >> n;
Process p[n];
for (int i = 0; i < n; i++) {
p[i].id = i + 1;
cout << "Enter arrival time & burst time for Process P" << p[i].id <<
": ";
cin >> p[i].arrival >> p[i].burst; }
float completed = 0, currentTime = 0, totalTAT = 0, totalWT = 0;
while (completed < n) {
int idx = -1, minBurst = 1e9;
for (int i = 0; i < n; i++) {
if (!p[i].isCompleted && p[i].arrival <= currentTime) {
if (p[i].burst < minBurst) {
minBurst = p[i].burst, idx = i;
} else if (p[i].burst == minBurst && p[i].arrival <
p[idx].arrival) {
idx = i; }}}
if (idx != -1) {
p[idx].start = currentTime;
p[idx].completion = currentTime + p[idx].burst;
p[idx].turnaround = p[idx].completion - p[idx].arrival;
p[idx].waiting = p[idx].turnaround - p[idx].burst;
p[idx].isCompleted = true;
currentTime = p[idx].completion;
totalTAT += p[idx].turnaround;
totalWT += p[idx].waiting;
completed++;
} else {
currentTime++; } }
cout << "\nPID\tAT\tBT\tCT\tTAT\tWT\n";
for (int i = 0; i < n; i++) {
cout << "P" << p[i].id << "\t"<< p[i].arrival << "\t"<< p[i].burst <<
"\t"<< p[i].completion << "\t"<< p[i].turnaround << "\t"<< p[i].waiting << "\
n";}
cout << "\nGantt Chart:";
cout << "\n|";
sort(p, p + n, [](Process a, Process b) {
return a.start < b.start; });
for (int i = 0; i < n; i++) {
cout << " P" << p[i].id << " |";}
cout << "\n" << p[0].start;
for (int i = 0; i < n; i++) {
cout << setw(7) << p[i].completion;}
cout << "\nAverage Turnaround Time: " << totalTAT / n;
cout << "\nAverage Waiting Time: " << totalWT / n << "\n";
return 0;}
PRIORITY SCHEDULING NON-PREEMPTIVE
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <climits>
using namespace std;
struct Process {
int id, arrival,burst,priority,start,completion,turnaround,waiting;
bool completed = false; };
int main() {
int n;
cout << "Enter number of processes: ";
cin >> n;
Process p[n];
for (int i = 0; i < n; i++) {
p[i].id = i + 1;
cout << "Enter arrival time for Process P" << p[i].id << ": ";
cin >> p[i].arrival;
cout << "Enter burst time for Process P" << p[i].id << ": ";
cin >> p[i].burst;
cout << "Enter priority for Process P" << p[i].id << " (lower number =
higher priority): ";
cin >> p[i].priority;}
float completed =0, currentTime =0, totalTAT =0, totalWT = 0;
while (completed < n) {
int idx = -1;
int bestPrio = INT_MAX;
for (int i = 0; i < n; i++) {
if (!p[i].completed && p[i].arrival <= currentTime) {
if (p[i].priority < bestPrio) {
bestPrio = p[i].priority;
idx = i;
} else if (p[i].priority == bestPrio&& p[i].arrival <
p[idx].arrival){
idx = i;}}}
if (idx == -1) {
currentTime++;
} else {
p[idx].start = currentTime;
p[idx].completion = p[idx].start + p[idx].burst;
p[idx].turnaround = p[idx].completion - p[idx].arrival;
p[idx].waiting = p[idx].turnaround - p[idx].burst;
p[idx].completed = true;
currentTime = p[idx].completion;
totalTAT += p[idx].turnaround;
totalWT += p[idx].waiting;
completed++;}}
cout << "\nPID\tAT\tBT\tPR\tST\tCT\tTAT\tWT\n";
for (int i = 0; i < n; i++) {
cout<<"P"<<p[i].id<<"\t"<<p[i].arrival<<"\t"<<p[i].burst<<"\
t"<<p[i].priority<<"\t"<<p[i].start<< "\t"<<p[i].completion<< "\
t"<<p[i].turnaround<< "\t"<<p[i].waiting<<"\n";}
cout << "Gantt Chart:\n";
cout << " ";
for (int i = 0; i < n; i++)
cout << "-------";
cout << "\n|";
sort(p, p + n, [](const Process &a, const Process &b) {
return a.start < b.start;});
for (int i = 0; i < n; i++) {
cout << " P" << p[i].id << " |";}
cout << "\n ";
for (int i = 0; i < n; i++) cout << "-------";
cout << "\n" << p[0].start;
for (int i = 0; i < n; i++) {
cout << setw(7) << p[i].completion;}
cout << "\nAverage Turnaround Time: " << (totalTAT / n);
cout << "\nAverage Waiting Time: " << (totalWT / n) << "\n";
return 0;}

PRIORITY SCHEDULING PREEMPTIVE


#include <iostream>
#include <algorithm>
#include <iomanip>
#include <climits>
using namespace std;
struct Process {
int
id,arrival,burst,remaining,priority,completion,turnaround,waiting,start;
bool started = false, completed = false;};
int main() {
int n;
cout << "Enter number of processes: ";
cin >> n;
Process p[n];
for (int i = 0; i < n; i++) {
p[i].id = i + 1;
cout<< "Enter arrival time for Process P"<< p[i].id << ":";
cin >> p[i].arrival;
cout << "Enter burst time for Process P" << p[i].id << ": ";
cin >> p[i].burst;
p[i].remaining = p[i].burst;
cout<<"Enter priority for Process P"<<p[i].id<<"(lower number = higher
priority): ";
cin >> p[i].priority;}
float completedCount =0, currentTime =0 ,totalTAT =0, totalWT = 0;
while (completedCount < n) {
int idx = -1;
int bestPrio = INT_MAX;
for (int i = 0; i < n; i++) {
if (!p[i].completed && p[i].arrival <= currentTime) {
if (p[i].priority < bestPrio) {
bestPrio = p[i].priority;
idx = i;
} else if (p[i].priority == bestPrio && p[i].arrival <
p[idx].arrival){
idx = i;}}}
if (idx != -1) {
if (!p[idx].started) {
p[idx].start = currentTime;
p[idx].started = true;}
p[idx].remaining--;
currentTime++;
if (p[idx].remaining == 0) {
p[idx].completion = currentTime;
p[idx].turnaround = p[idx].completion - p[idx].arrival;
p[idx].waiting = p[idx].turnaround - p[idx].burst;
p[idx].completed = true;
totalTAT += p[idx].turnaround;
totalWT += p[idx].waiting;
completedCount++;}
} else {
currentTime++;}}
cout << "\nPID\tAT\tBT\tPR\tST\tCT\tTAT\tWT\n";
for (int i = 0; i < n; i++) {
cout<<"P"<<p[i].id<<"\t"<<p[i].arrival<<"\t"<<p[i].burst<<"\
t"<<p[i].priority<<"\t"<<p[i].start<<"\t"<<p[i].completion<<"\
t"<<p[i].turnaround<<"\t"<<p[i].waiting<<"\n";}
cout << "Gantt Chart:\n";
cout << " ";
for (int i = 0; i < n; i++) cout << "-------";
cout << "\n|";
sort(p, p + n, [](const Process &a, const Process &b) {
return a.start < b.start;});
for (int i = 0; i < n; i++) {
cout << " P" << p[i].id << " |";}
cout << "\n ";
for (int i = 0; i < n; i++)
cout << "-------";
cout << "\n" << p[0].start;
for (int i = 0; i < n; i++) {
cout << setw(7) << p[i].completion;}
cout << "\nAverage Turnaround Time: " << (totalTAT / n);
cout << "\nAverage Waiting Time: " << (totalWT / n) << "\n";
return 0;}

You might also like