0% found this document useful (0 votes)
1 views23 pages

PDF Os

The document contains multiple implementations of CPU scheduling algorithms including First-Come First-Serve (FIFO), Shortest Job First (SJF), Priority Scheduling, Round Robin, and various page replacement algorithms like FIFO and Optimal. Each algorithm is presented with code snippets in C++ that demonstrate how to calculate waiting time, turnaround time, and page faults. Additionally, the document outlines the logic and structure of each algorithm, providing a comprehensive guide for understanding and implementing these scheduling techniques.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views23 pages

PDF Os

The document contains multiple implementations of CPU scheduling algorithms including First-Come First-Serve (FIFO), Shortest Job First (SJF), Priority Scheduling, Round Robin, and various page replacement algorithms like FIFO and Optimal. Each algorithm is presented with code snippets in C++ that demonstrate how to calculate waiting time, turnaround time, and page faults. Additionally, the document outlines the logic and structure of each algorithm, providing a comprehensive guide for understanding and implementing these scheduling techniques.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

LAB-7:

First-Come First-Serve (FIFO)

Code
#include <iostream>p
using namespace std;

int main() {
int n, bt[20], wt[20], tat[20];
float avg_wt = 0, avg_tat = 0;
cout << "Enter total number of processes: ";
cin >> n;

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


cout << "Enter Burst Time for P" << i + 1 << ": ";
cin >> bt[i];
}

wt[0] = 0;
for (int i = 1; i < n; i++)
wt[i] = bt[i - 1] + wt[i - 1];

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


tat[i] = bt[i] + wt[i];
avg_wt += wt[i];
avg_tat += tat[i];
}

cout << "\nProcess\tBT\tWT\tTAT\n";


for (int i = 0; i < n; i++)
cout << "P" << i + 1 << "\t" << bt[i] << "\t" << wt[i] << "\t" << tat[i] << endl;

cout << "Average Waiting Time: " << avg_wt / n << endl;
cout << "Average Turnaround Time: " << avg_tat / n << endl;

return 0;
}

Non-Preemptive Shortest Job First (SJF)

Code
#include <iostream>
#include <algorithm>
using namespace std;

struct Process {
int id, bt, at;
};

bool compare(Process a, Process b) {


return a.bt < b.bt;
}
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 burst time for P" << p[i].id << ": ";
cin >> p[i].bt;
}

sort(p, p + n, compare);

int wt[n], tat[n];


wt[0] = 0;
for (int i = 1; i < n; i++)
wt[i] = p[i - 1].bt + wt[i - 1];

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


tat[i] = p[i].bt + wt[i];

float avg_wt = 0, avg_tat = 0;


cout << "\nProcess\tBT\tWT\tTAT\n";
for (int i = 0; i < n; i++) {
avg_wt += wt[i];
avg_tat += tat[i];
cout << "P" << p[i].id << "\t" << p[i].bt << "\t" << wt[i] << "\t" << tat[i] << endl;
}
cout << "Average Waiting Time: " << avg_wt / n << endl;
cout << "Average Turnaround Time: " << avg_tat / n << endl;
return 0;
}

Preemptive SJF (Shortest Remaining Time First)

Code
#include <iostream>
#include <vector>
#include <climits>
using namespace std;

struct Process {
int id;
int at, bt, rt;
int wt, tat;
};
int main() {
int n;
cout << "Enter number of processes: ";
cin >> n;

vector<Process> p(n);
for (int i = 0; i < n; i++) {
p[i].id = i + 1;
cout << "Enter Arrival Time and Burst Time for P" << p[i].id << ": ";
cin >> p[i].at >> p[i].bt;
p[i].rt = p[i].bt;
}

int complete = 0, t = 0, shortest = -1;


int minm = INT_MAX;
bool found = false;

while (complete < n) {


shortest = -1;
minm = INT_MAX;
found = false;

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


if (p[i].at <= t && p[i].rt > 0 && p[i].rt < minm) {
minm = p[i].rt;
shortest = i;
found = true;
}
}
if (!found) {
t++;
continue;
}

p[shortest].rt--;

if (p[shortest].rt == 0) {
complete++;
int finish_time = t + 1;
p[shortest].wt = finish_time - p[shortest].bt - p[shortest].at;
if (p[shortest].wt < 0) p[shortest].wt = 0;
}

t++;
}

float avg_wt = 0, avg_tat = 0;


cout << "\nProcess\tAT\tBT\tWT\tTAT\n";
for (int i = 0; i < n; i++) {
p[i].tat = p[i].wt + p[i].bt;
avg_wt += p[i].wt;
avg_tat += p[i].tat;
cout << "P" << p[i].id << "\t" << p[i].at << "\t" << p[i].bt << "\t" << p[i].wt << "\t" <<
p[i].tat << endl;
}

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


cout << "\nAverage Turnaround Time: " << avg_tat / n << endl;
return 0;
}

Non-Preemptive Priority Scheduling

Code
#include <iostream>
#include <algorithm>
using namespace std;

struct Process {
int id, bt, priority, wt, tat;
};

bool compare(Process a, Process b) {


return a.priority < b.priority; // lower number = higher priority
}

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 Burst Time and Priority for P" << p[i].id << ": ";
cin >> p[i].bt >> p[i].priority;
}

sort(p, p + n, compare);

p[0].wt = 0;
for (int i = 1; i < n; i++)
p[i].wt = p[i - 1].wt + p[i - 1].bt;

float avg_wt = 0, avg_tat = 0;


cout << "\nProcess\tBT\tPriority\tWT\tTAT\n";
for (int i = 0; i < n; i++) {
p[i].tat = p[i].bt + p[i].wt;
avg_wt += p[i].wt;
avg_tat += p[i].tat;
cout << "P" << p[i].id << "\t" << p[i].bt << "\t" << p[i].priority << "\t\t" << p[i].wt <<
"\t" << p[i].tat << endl;
}

cout << "Average Waiting Time: " << avg_wt / n << endl;
cout << "Average Turnaround Time: " << avg_tat / n << endl;
return 0;
}
Preemptive Priority Scheduling

Code
#include <iostream>
#include <climits>
using namespace std;

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

int at[n], bt[n], pr[n], rt[n], wt[n] = {0}, tat[n];


for (int i = 0; i < n; i++) {
cout << "Enter Arrival Time, Burst Time and Priority for P" << i + 1 << ": ";
cin >> at[i] >> bt[i] >> pr[i];
rt[i] = bt[i];
}
int complete = 0, t = 0, shortest = -1;
int minPriority = INT_MAX;
bool found = false;

while (complete != n) {
found = false;
minPriority = INT_MAX;

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


if (at[i] <= t && rt[i] > 0 && pr[i] < minPriority) {
minPriority = pr[i];
shortest = i;
found = true;
}
}

if (!found) {
t++;
continue;
}

rt[shortest]--;

if (rt[shortest] == 0) {
complete++;
int finish_time = t + 1;
wt[shortest] = finish_time - bt[shortest] - at[shortest];
if (wt[shortest] < 0) wt[shortest] = 0;
}
t++;
}

float avg_wt = 0, avg_tat = 0;


cout << "\nProcess\tBT\tPriority\tWT\tTAT\n";
for (int i = 0; i < n; i++) {
tat[i] = bt[i] + wt[i];
avg_wt += wt[i];
avg_tat += tat[i];
cout << "P" << i + 1 << "\t" << bt[i] << "\t" << pr[i] << "\t\t" << wt[i] << "\t" <<
tat[i] << endl;
}

cout << "Average Waiting Time: " << avg_wt / n << endl;
cout << "Average Turnaround Time: " << avg_tat / n << endl;
return 0;
}

Round Robin Scheduling

Code
#include <iostream>
#include <queue>
using namespace std;
int main() {
int n, tq;
cout << "Enter number of processes: ";
cin >> n;

int bt[n], rt[n], wt[n] = {0}, tat[n], at[n];


for (int i = 0; i < n; i++) {
cout << "Enter Arrival Time and Burst Time for P" << i + 1 << ": ";
cin >> at[i] >> bt[i];
rt[i] = bt[i];
}

cout << "Enter Time Quantum: ";


cin >> tq;

queue<int> q;
int t = 0, complete = 0;
bool visited[n] = {false};

while (complete < n) {


for (int i = 0; i < n; i++) {
if (at[i] <= t && !visited[i]) {
q.push(i);
visited[i] = true;
}
}

if (!q.empty()) {
int i = q.front(); q.pop();
int timeSpent = min(tq, rt[i]);
t += timeSpent;
rt[i] -= timeSpent;

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


if (at[j] <= t && !visited[j]) {
q.push(j);
visited[j] = true;
}
}

if (rt[i] > 0)
q.push(i);
else {
complete++;
tat[i] = t - at[i];
wt[i] = tat[i] - bt[i];
}
} else {
t++;
}
}

float avg_wt = 0, avg_tat = 0;


cout << "\nProcess\tBT\tWT\tTAT\n";
for (int i = 0; i < n; i++) {
avg_wt += wt[i];
avg_tat += tat[i];
cout << "P" << i + 1 << "\t" << bt[i] << "\t" << wt[i] << "\t" << tat[i] << endl;
}
cout << "Average Waiting Time: " << avg_wt / n << endl;
cout << "Average Turnaround Time: " << avg_tat / n << endl;
return 0;
}
LAB-10:To implement FIFO page replacement algorithm.
CODE:
#include <iostream>
#include <queue>
using namespace std;

int getPageFaults(int pages[], int n, int frames) {


int memory[frames];
for (int i = 0; i < frames; i++) {
memory[i] = -1;
}

queue<int> indexes;
int pageFaults = 0;

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


bool pageFound = false;

for (int j = 0; j < frames; j++) {


if (memory[j] == pages[i]) {
pageFound = true;
break;
}
}

if (!pageFound) {
pageFaults++;

if (indexes.size() == frames) {
int oldestPage = indexes.front();
indexes.pop();

for (int j = 0; j < frames; j++) {


if (memory[j] == oldestPage) {
memory[j] = pages[i];
break;
}
}
} else {
for (int j = 0; j < frames; j++) {
if (memory[j] == -1) {
memory[j] = pages[i];
break;
}
}
}

indexes.push(pages[i]);
}

cout << "Memory: ";


for (int j = 0; j < frames; j++) {
if (memory[j] != -1) {
cout << memory[j] << " ";
} else {
cout << "_ ";
}
}
cout << endl;
}
return pageFaults;
}

int main() {
int n, frames;

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


cin >> n;

int pages[n];

cout << "Enter the page reference string (space-separated): ";


for (int i = 0; i < n; i++) {
cin >> pages[i];
}

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


cin >> frames;

int faults = getPageFaults(pages, n, frames);


cout << "Total Page Faults: " << faults << endl;

return 0;
}
OUTPUT:
AIM: Implementation of Optimal page replacement algorithm.
CODE:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int findOptimalPage(const vector<int>& pages, const vector<int>& frames, int currentIndex)


{
int farthestIndex = currentIndex;
int pageToReplace = -1;

for (int i = 0; i < frames.size(); i++) {


int j;
for (j = currentIndex; j < pages.size(); j++) {
if (frames[i] == pages[j]) {
if (j > farthestIndex) {
farthestIndex = j;
pageToReplace = i;
}
break;
}
}
if (j == pages.size()) {
return i;
}
}
return pageToReplace;
}

int optimalPageReplacement(const vector<int>& pages, int frameCount) {


vector<int> frames;
int pageFaults = 0;

for (int i = 0; i < pages.size(); i++) {


bool pageFound = false;
for (int page : frames) {
if (page == pages[i]) {
pageFound = true;
break;
}
}

if (!pageFound) {
if (frames.size() < frameCount) {
frames.push_back(pages[i]);
} else {
int replaceIndex = findOptimalPage(pages, frames, i + 1);
frames[replaceIndex] = pages[i];
}
pageFaults++;
}

// Display the current page and memory state


cout << "Page: " << pages[i] << " -> Memory: ";
for (int page : frames) {
cout << page << " ";
}
cout << endl;
}
return pageFaults;
}

int main() {
vector<int> pages = {1, 2, 1, 3, 1, 2, 1};
int frameCount = 3;

int result = optimalPageReplacement(pages, frameCount);


cout << "Total Page Faults: " << result << endl;

return 0;
}
OUTPUT:
AIM: To analyse and implement LRU (least recently used) page replacement algorithm.
CODE:
#include <iostream>
#include <list>
#include <algorithm>
using namespace std;

int LRU(int pages[], int n, int num_frames) {


list<int> memory;

int page_faults = 0;
for (int i = 0; i < n; i++) {
int page = pages[i];
auto it = find(memory.begin(), memory.end(), page);
if (it == memory.end()) {
if (memory.size() == num_frames) {
memory.pop_back();
}
memory.push_front(page);
page_faults++;
}
else {
memory.erase(it);
memory.push_front(page);
}
cout << "Page: " << page << " -> Memory: ";
for (auto mem_page : memory)
cout << mem_page << " ";
cout << endl;
}

return page_faults;
}

int main() {
int pages[] = {7, 8, 9, 2, 0};
int num_frames = 3;
int n = sizeof(pages) / sizeof(pages[0]);
int faults = LRU(pages, n, num_frames);
cout << "\nTotal Page Faults: " << faults << endl;

return 0;
}
OUTPUT:

You might also like