Experiment No.
– 6
Aim: Write a program to implement a Priority CPU scheduling
algorithm
Code:-
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Process {
int pid, bt, priority;
};
// Function to calculate waiting time
void findWaitingTime(vector<Process>& proc, vector<int>& wt) {
wt[0] = 0;
for (size_t i = 1; i < proc.size(); i++)
wt[i] = proc[i - 1].bt + wt[i - 1];
}
// Function to calculate turnaround time
void findTurnAroundTime(vector<Process>& proc, vector<int>& wt,
vector<int>& tat) {
for (size_t i = 0; i < proc.size(); i++)
tat[i] = proc[i].bt + wt[i];
}
// Function to calculate and display average times
void findAvgTime(vector<Process>& proc) {
int n = proc.size();
vector<int> wt(n), tat(n);
int total_wt = 0, total_tat = 0;
findWaitingTime(proc, wt);
findTurnAroundTime(proc, wt, tat);
cout << "\nProcesses Burst Time Waiting Time Turnaround Time\
n";
for (int i = 0; i < n; i++) {
total_wt += wt[i];
total_tat += tat[i];
cout << " " << proc[i].pid << "\t\t" << proc[i].bt
<< "\t\t " << wt[i] << "\t\t " << tat[i] << endl;
}
cout << "\nAverage Waiting Time = " << (float)total_wt / n << endl;
cout << "Average Turnaround Time = " << (float)total_tat / n <<
endl;
}
// Function to implement Priority Scheduling
void priorityScheduling(vector<Process>& proc) {
sort(proc.begin(), proc.end(), [](Process& a, Process& b) {
return a.priority > b.priority; // Higher priority executes first
});
cout << "Execution Order: ";
for (Process& p : proc) cout << p.pid << " ";
cout << endl;
findAvgTime(proc);
}
int main() {
int n;
cout << "Enter number of processes: ";
cin >> n;
vector<Process> proc(n);
for (int i = 0; i < n; i++) {
cout << "Enter PID, Burst Time & Priority for process " << (i + 1)
<< ": ";
cin >> proc[i].pid >> proc[i].bt >> proc[i].priority;
}
priorityScheduling(proc);
return 0;
}
Experiment No. - 7
Aim: Write a program to implement the Round Robin Scheduling
Algorithm.
Code:-
public class Main {
static void findWaitingTime(int processes[], int n, int bt[], int wt[],
int quantum) {
int rem_bt[] = new int[n];
for (int i = 0; i < n; i++)
rem_bt[i] = bt[i];
int t = 0;
while (true) {
boolean done = true;
for (int i = 0; i < n; i++) {
if (rem_bt[i] > 0) {
done = false;
if (rem_bt[i] > quantum) {
t += quantum;
rem_bt[i] -= quantum;
} else {
t += rem_bt[i];
wt[i] = t - bt[i];
rem_bt[i] = 0;
}
}
}
if (done)
break;
}
}
static void findTurnAroundTime(int processes[], int n, int bt[], int
wt[], int tat[]) {
for (int i = 0; i < n; i++)
tat[i] = bt[i] + wt[i];
}
static void findavgTime(int processes[], int n, int bt[], int quantum)
{
int wt[] = new int[n], tat[] = new int[n], ct[] = new int[n];
int total_wt = 0, total_tat = 0;
findWaitingTime(processes, n, bt, wt, quantum);
findTurnAroundTime(processes, n, bt, wt, tat);
// Calculate Completion Time: Completion Time = Turnaround
Time
for (int i = 0; i < n; i++) {
ct[i] = tat[i];
}
System.out.println("PN\tBT\tCT\tTAT\tWT");
for (int i = 0; i < n; i++) {
total_wt += wt[i];
total_tat += tat[i];
System.out.println((i + 1) + "\t" + bt[i] + "\t" + ct[i] + "\t" +
tat[i] + "\t" + wt[i]);
}
System.out.println("Average waiting time = " + (float) total_wt /
n);
System.out.println("Average turn around time = " + (float)
total_tat / n);
}
public static void main(String[] args) {
int processes[] = {1, 2, 3};
int n = processes.length;
int burst_time[] = {10, 5, 8};
int quantum = 2;
findavgTime(processes, n, burst_time, quantum);
}
}
Experiment No. - 8
Aim: Write a program to implement the Banker’s algorithm.
Code:-
#include <iostream>
#include <vector>
using namespace std;
class BankersAlgorithm {
private:
int n, m; // Number of processes & resources
vector<vector<int>> max, allocation, need;
vector<int> available;
public:
BankersAlgorithm(int n, int m) : n(n), m(m) {
max.assign(n, vector<int>(m));
allocation.assign(n, vector<int>(m));
need.assign(n, vector<int>(m));
available.assign(m, 0);
}
// Function to take input
void inputData() {
cout << "Enter allocation matrix:\n";
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
cin >> allocation[i][j];
cout << "Enter max matrix:\n";
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
cin >> max[i][j];
cout << "Enter available resources:\n";
for (int i = 0; i < m; i++)
cin >> available[i];
// Calculate need matrix
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
need[i][j] = max[i][j] - allocation[i][j];
}
// Function to check system safety
bool isSafe() {
vector<bool> finish(n, false);
vector<int> work = available;
vector<int> safeSequence(n);
int count = 0;
while (count < n) {
bool found = false;
for (int i = 0; i < n; i++) {
if (!finish[i]) {
bool possible = true;
for (int j = 0; j < m; j++) {
if (need[i][j] > work[j]) {
possible = false;
break;
}
}
if (possible) {
for (int j = 0; j < m; j++)
work[j] += allocation[i][j];
safeSequence[count++] = i;
finish[i] = true;
found = true;
}
}
}
if (!found) {
cout << "System is not in a safe state.\n";
return false;
}
}
cout << "System is in a safe state. Safe sequence is: ";
for (int i = 0; i < n; i++)
cout << "P" << safeSequence[i] << " ";
cout << endl;
return true;
}
};
int main() {
int n, m;
cout << "Enter the number of processes: ";
cin >> n;
cout << "Enter the number of resources: ";
cin >> m;
BankersAlgorithm bankers(n, m);
bankers.inputData();
bankers.isSafe();
return 0;
}
Experiment No. - 9
Aim: Write a program to implement Producer Consumer Algorithm.
Code:-
#include <iostream>
#include <queue>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <chrono>
using namespace std;
class Buffer {
private:
const int capacity = 5; // Maximum buffer size
queue<int> buffer;
mutex mtx;
condition_variable cv;
public:
void produce(int value) {
unique_lock<mutex> lock(mtx);
cv.wait(lock, [this]() { return buffer.size() < capacity; });
buffer.push(value);
cout << "Produced: " << value << endl;
cv.notify_all(); // Notify consumers
}
void consume() {
unique_lock<mutex> lock(mtx);
cv.wait(lock, [this]() { return !buffer.empty(); });
int value = buffer.front();
buffer.pop();
cout << "Consumed: " << value << endl;
cv.notify_all(); // Notify producers
}
};
void producer(Buffer &buffer) {
int value = 0;
for (int i = 0; i < 5; i++) { // Limited production to 5 items
buffer.produce(value++);
this_thread::sleep_for(chrono::seconds(1));
}
}
void consumer(Buffer &buffer) {
for (int i = 0; i < 5; i++) { // Limited consumption to 5 items
buffer.consume();
this_thread::sleep_for(chrono::milliseconds(1500));
}
}
int main() {
Buffer buffer;
thread producerThread(producer, ref(buffer));
thread consumerThread(consumer, ref(buffer));
producerThread.join();
consumerThread.join();
return 0;
}
Experiment No. - 10
Aim: Write a program to implement the First - In - first - Out Page
Replacement Algorithm.
Code:-
#include <iostream>
#include <queue>
#include <unordered_set>
using namespace std;
class FIFOPageReplacement {
private:
int capacity;
queue<int> pages;
unordered_set<int> set;
public:
FIFOPageReplacement(int capacity) {
this->capacity = capacity;
}
void insertPage(int page) {
if (set.find(page) == set.end()) { // Page not in memory
if (pages.size() == capacity) {
int removedPage = pages.front();
pages.pop();
set.erase(removedPage);
cout << "Page " << removedPage << " removed." << endl;
}
pages.push(page);
set.insert(page);
cout << "Page " << page << " added." << endl;
} else {
cout << "Page " << page << " already in memory." << endl;
}
}
void displayPages() {
queue<int> temp = pages; // Copy queue to preserve order
cout << "Current pages in memory: ";
while (!temp.empty()) {
cout << temp.front() << " ";
temp.pop();
}
cout << endl;
}
};
int main() {
int capacity, n;
cout << "Enter the capacity of the page frame: ";
cin >> capacity;
FIFOPageReplacement fifo(capacity);
cout << "Enter the number of page requests: ";
cin >> n;
cout << "Enter the page requests:" << endl;
for (int i = 0; i < n; i++) {
int page;
cin >> page;
fifo.insertPage(page);
fifo.displayPages();
}
return 0;
}
Experiment No. – 11
Aim: Write a program to implement the Least Recently Used(LRU)
page Replacement Algorithm.
Code:-
#include <iostream>
#include <unordered_set>
#include <unordered_map>
#include <vector>
using namespace std;
int pageFaults(vector<int>& pages, int n, int capacity) {
unordered_set<int> s;
unordered_map<int, int> indexes;
int page_faults = 0;
for (int i = 0; i < n; i++) {
if (s.size() < capacity) {
if (s.find(pages[i]) == s.end()) {
s.insert(pages[i]);
page_faults++;
}
indexes[pages[i]] = i;
} else {
if (s.find(pages[i]) == s.end()) {
int lru = INT_MAX, val = -1;
for (auto it : s) {
if (indexes[it] < lru) {
lru = indexes[it];
val = it;
}
}
s.erase(val);
indexes.erase(val);
s.insert(pages[i]);
page_faults++;
}
indexes[pages[i]] = i;
}
}
return page_faults;
}
int main() {
vector<int> pages = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 9, 7};
int capacity = 3;
cout << "Least page use: " << pageFaults(pages, pages.size(),
capacity) << endl;
return 0;
}