KEMBAR78
Ishuos | PDF | Algorithms | Computer Data
0% found this document useful (0 votes)
5 views18 pages

Ishuos

operating system file os ishu chamar

Uploaded by

krisshh
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)
5 views18 pages

Ishuos

operating system file os ishu chamar

Uploaded by

krisshh
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/ 18

Experiment 6

Aim: Write a program to implement a Priority CPU scheduling algorithm

Code:
#include <iostream>

#include <vector>

#include <algorithm>

using namespace std;

class Process {

public:

int pid;

int bt; // Burst time

int priority;

Process(int pid, int bt, int priority) {

this->pid = pid;

this->bt = bt;

this->priority = priority;

int prior() {

return priority;

};

class Scheduler {

public:

void findWaitingTime(vector<Process>& proc, int n, vector<int>& wt) {

wt[0] = 0;

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

wt[i] = proc[i - 1].bt + wt[i - 1];

void findTurnAroundTime(vector<Process>& proc, int n, vector<int>& wt, vector<int>& tat) {


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

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

void findAvgTime(vector<Process>& proc, int n) {

vector<int> wt(n), tat(n);

int total_wt = 0, total_tat = 0;

// Calculate waiting time of all processes

findWaitingTime(proc, n, wt);

// Calculate turnaround time

findTurnAroundTime(proc, n, 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 " << wt[i] << "\t\t " << tat[i] << endl;

cout << "\nAverage waiting time = " << (float)total_wt / n;

cout << "\nAverage turnaround time = " << (float)total_tat / n << endl;

void priorityScheduling(vector<Process>& proc, int n) {

// Sort processes based on priority (higher priority first)

sort(proc.begin(), proc.end(), [](Process a, Process b) {

return a.prior() > b.prior();

});

cout << "Order in which processes get executed: \n";

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

cout << proc[i].pid << " ";

findAvgTime(proc, n);

}
};

int main() {

Scheduler ob;

int n = 4;

vector<Process> proc = {

Process(1, 6, 3),

Process(2, 2, 1),

Process(3, 8, 4),

Process(4, 3, 2)

};

ob.priorityScheduling(proc, n);

return 0;

Output:
Experiment 7
Aim: Write a program to implement the Round Robin Scheduling Algorithm.

Code:
#include <iostream>

#include <vector>

using namespace std;

void findWaitingTime(vector<int>& processes, int n, vector<int>& bt, vector<int>& wt, int quantum) {

vector<int> rem_bt(bt); // Copy burst times

int t = 0;

while (true) {

bool 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;

void findTurnAroundTime(vector<int>& processes, int n, vector<int>& bt, vector<int>& wt, vector<int>&


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

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

void findAvgTime(vector<int>& processes, int n, vector<int>& bt, int quantum) {

vector<int> wt(n), tat(n), ct(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];

cout << "PN\tBT\tCT\tTAT\tWT\n";

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

total_wt += wt[i];

total_tat += tat[i];

cout << processes[i] << "\t" << bt[i] << "\t" << ct[i] << "\t" << tat[i] << "\t" << wt[i] << endl;

cout << "Average waiting time = " << (float) total_wt / n << endl;

cout << "Average turn around time = " << (float) total_tat / n << endl;

int main() {

vector<int> processes = {1, 2, 3, 4};

int n = processes.size();

vector<int> burst_time = {7, 4, 9, 5}; // Updated burst times

int quantum = 3; // Updated time quantum

findAvgTime(processes, n, burst_time, quantum);

return 0;

Output:
Experiment 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 and resources

vector<vector<int>> max, allocation, need;

vector<int> available;

public:

BankersAlgorithm(int n, int m) {

this->n = n;

this->m = m;

max.resize(n, vector<int>(m));

allocation.resize(n, vector<int>(m));

need.resize(n, vector<int>(m));

available.resize(m);

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 the 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];

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;

Output:
Experiment 9
Aim: Write a program to implement Producer Consumer Algorithm.

Code:
import java.util.LinkedList;

class Buffer {

private final int capacity = 5; // Maximum buffer size

private LinkedList<Integer> list = new LinkedList<>();

public synchronized void produce(int value) throws InterruptedException {

while (list.size() == capacity) {

wait();

list.add(value);

System.out.println("Produced: " + value);

notify();

public synchronized void consume() throws InterruptedException {

while (list.isEmpty()) {

wait();

int value = list.removeFirst();

System.out.println("Consumed: " + value);

notify();

class Producer implements Runnable {

private Buffer buffer;

Producer(Buffer buffer) {

this.buffer = buffer;

public void run() {

int value = 0;
try {

for (int i = 0; i < 5; i++) { // Limited production to 5 items

buffer.produce(value++);

Thread.sleep(1000);

} catch (InterruptedException e) {

e.printStackTrace();

class Consumer implements Runnable {

private Buffer buffer;

Consumer(Buffer buffer) {

this.buffer = buffer;

public void run() {

try {

for (int i = 0; i < 5; i++) { // Limited consumption to 5 items

buffer.consume();

Thread.sleep(1500);

} catch (InterruptedException e) {

e.printStackTrace();

public class Main {

public static void main(String[] args) {

Buffer buffer = new Buffer();

Thread producerThread = new Thread(new Producer(buffer));

Thread consumerThread = new Thread(new Consumer(buffer));


producerThread.start();

consumerThread.start();

Output:

Experiment 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 PageManager {

private:

int maxCapacity;

queue<int> pageQueue;

unordered_set<int> pageTracker;

public:

PageManager(int capacity) : maxCapacity(capacity) {}

void addPage(int pageNumber) {

if (pageTracker.find(pageNumber) == pageTracker.end()) {

if (pageQueue.size() == maxCapacity) {

int oldPage = pageQueue.front();

pageQueue.pop();

pageTracker.erase(oldPage);

cout << "Page " << oldPage << " evicted from memory.\n";

pageQueue.push(pageNumber);

pageTracker.insert(pageNumber);

cout << "Page " << pageNumber << " loaded into memory.\n";

} else {

cout << "Page " << pageNumber << " is already present in memory.\n";

void showMemoryState() {

queue<int> tempQueue = pageQueue;

cout << "Current memory contents: ";

while (!tempQueue.empty()) {
cout << tempQueue.front() << " ";

tempQueue.pop();

cout << endl;

};

int main() {

int memorySize, totalRequests;

cout << "Enter the maximum memory capacity: ";

cin >> memorySize;

PageManager manager(memorySize);

cout << "Enter the number of page requests: ";

cin >> totalRequests;

cout << "Enter the sequence of page requests:\n";

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

int requestedPage;

cin >> requestedPage;

manager.addPage(requestedPage);

manager.showMemoryState();

return 0;

Output:
Experiment 11
Aim: Write a program to implement the Least Recently Used(LRU) page Replacement
Algorithm.

Code:
#include <iostream>

#include <unordered_map>

#include <unordered_set>

#include <vector>

#include<algorithm>

#include<climits>

using namespace std;

class LRUPageReplacement {

public:

static int countPageFaults(const vector<int>& pageRequests, int memoryCapacity) {

unordered_set<int> activePages;

unordered_map<int, int> pageIndices;

int pageFaultCount = 0;

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

int currentPage = pageRequests[i];

if (activePages.size() < memoryCapacity) {

if (activePages.find(currentPage) == activePages.end()) {

activePages.insert(currentPage);

pageFaultCount++;

} else {

if (activePages.find(currentPage) == activePages.end()) {

int leastUsedPage = -1, leastIndex = INT_MAX;

for (const auto& page : activePages) {

if (pageIndices[page] < leastIndex) {

leastIndex = pageIndices[page];

leastUsedPage = page;

}
}

activePages.erase(leastUsedPage);

activePages.insert(currentPage);

pageFaultCount++;

pageIndices[currentPage] = i;

return pageFaultCount;

};

int main() {

vector<int> pageSequence = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 9, 7};

int memorySize = 3;

cout << "Total page faults: " << LRUPageReplacement::countPageFaults(pageSequence, memorySize) <<
endl;

return 0;

Output:

You might also like