PDF Os
PDF Os
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;
wt[0] = 0;
for (int i = 1; i < n; i++)
wt[i] = bt[i - 1] + wt[i - 1];
cout << "Average Waiting Time: " << avg_wt / n << endl;
cout << "Average Turnaround Time: " << avg_tat / n << endl;
return 0;
}
Code
#include <iostream>
#include <algorithm>
using namespace std;
struct Process {
int id, bt, at;
};
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);
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;
}
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++;
}
Code
#include <iostream>
#include <algorithm>
using namespace std;
struct Process {
int id, bt, priority, wt, tat;
};
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;
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;
while (complete != n) {
found = false;
minPriority = INT_MAX;
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++;
}
cout << "Average Waiting Time: " << avg_wt / n << endl;
cout << "Average Turnaround Time: " << avg_tat / n << endl;
return 0;
}
Code
#include <iostream>
#include <queue>
using namespace std;
int main() {
int n, tq;
cout << "Enter number of processes: ";
cin >> n;
queue<int> q;
int t = 0, complete = 0;
bool visited[n] = {false};
if (!q.empty()) {
int i = q.front(); q.pop();
int timeSpent = min(tq, rt[i]);
t += timeSpent;
rt[i] -= timeSpent;
if (rt[i] > 0)
q.push(i);
else {
complete++;
tat[i] = t - at[i];
wt[i] = tat[i] - bt[i];
}
} else {
t++;
}
}
queue<int> indexes;
int pageFaults = 0;
if (!pageFound) {
pageFaults++;
if (indexes.size() == frames) {
int oldestPage = indexes.front();
indexes.pop();
indexes.push(pages[i]);
}
int main() {
int n, frames;
int pages[n];
return 0;
}
OUTPUT:
AIM: Implementation of Optimal page replacement algorithm.
CODE:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
if (!pageFound) {
if (frames.size() < frameCount) {
frames.push_back(pages[i]);
} else {
int replaceIndex = findOptimalPage(pages, frames, i + 1);
frames[replaceIndex] = pages[i];
}
pageFaults++;
}
int main() {
vector<int> pages = {1, 2, 1, 3, 1, 2, 1};
int frameCount = 3;
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 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: