( Lab Assignment)
Advanced Operating System
(MCA1004)
Fall Semester: 2024-2025
Slot:D21-D22
Name: DEBMALYA KARMAKAR
Register Number: 24MCA10167
1
INDEX
TABLE OF CONTENTS
S.No Topic Page No.
1 Write a program to implement FCFS CPU scheduling 3
algorithm.
2 Write a program to implement SJF CPU scheduling algorithm. 5
3 Write a program to implement Priority CPU scheduling
8
algorithm.
4 Write a program to implement Round Robin CPU scheduling
11
algorithm.
Write a program to implement Classical Inter Process
5 13
Communication Problem (Producer Consumer).
Write a program to implement Classical Inter Process
6 15
Communication Problem (Reader Writers).
Write a program to implement Optimal Page Replacement
7 18
algorithm.
Write a program to implement FIFO Page Replacement
8 21
algorithm.
9 Write a program to implement LRU Page Replacement
24
algorithm.
10 Write a program to implement Banker’s algorithms. 26
2
1.Write a program to implement FCFS
CPU scheduling algorithm.
PROGRAM :
#include <stdio.h>
struct Process {
int id; // Process ID
int arrival; // Arrival Time
int burst; // Burst Time
int waiting; // Waiting Time
int turnaround; // Turnaround Time
int completion; // Completion Time
};
// Function to sort the processes by arrival time
void sortByArrival(struct Process proc[], int n) {
struct Process temp;
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (proc[i].arrival > proc[j].arrival) {
temp = proc[i];
proc[i] = proc[j];
proc[j] = temp;
}
}
}
}
int main() {
int n;
printf("Enter the number of processes: ");
scanf("%d", &n);
struct Process proc[n];
// Input Arrival Time and Burst Time for each process
printf("Enter Arrival Time and Burst Time for each process:\n");
for (int i = 0; i < n; i++) {
proc[i].id = i + 1;
printf("Process %d - Arrival Time: ", i + 1);
scanf("%d", &proc[i].arrival);
printf("Process %d - Burst Time: ", i + 1);
scanf("%d", &proc[i].burst);
}
// Sort processes by arrival time
sortByArrival(proc, n);
// Calculate Completion Time, Turnaround Time, and Waiting Time
int currentTime = 0;
for (int i = 0; i < n; i++) {
if (currentTime < proc[i].arrival) {
3
currentTime = proc[i].arrival; // CPU is idle until process arrives
}
proc[i].completion = currentTime + proc[i].burst;
currentTime = proc[i].completion;
proc[i].turnaround = proc[i].completion - proc[i].arrival;
proc[i].waiting = proc[i].turnaround - proc[i].burst;
}
// Calculate total waiting time and total turnaround time
float totalWT = 0, totalTAT = 0;
for (int i = 0; i < n; i++) {
totalWT += proc[i].waiting;
totalTAT += proc[i].turnaround;
}
// Display the results
printf("\nProcess\tArrival\tBurst\tCompletion\tWaiting\tTurnaround\n");
for (int i = 0; i < n; i++) {
printf("P%d\t%d\t%d\t%d\t\t%d\t%d\n", proc[i].id, proc[i].arrival, proc[i].burst, proc[i].completion,
proc[i].waiting, proc[i].turnaround);
}
// Calculate and display average waiting time and turnaround time
printf("\nAverage Waiting Time: %.2f", totalWT / n);
printf("\nAverage Turnaround Time: %.2f\n", totalTAT / n);
return 0;
}
OUTPUT :
4
Write a program to implement
SJF CPU scheduling algorithm
PROGRAM :
#include <stdio.h>
#include <stdbool.h>
// Structure to represent a process
struct Process {
int id; // Process ID
int arrival_time; // Arrival Time
int burst_time; // Burst Time
int wait_time; // Waiting Time
int turn_time; // Turnaround Time
int completion_time; // Completion Time
bool completed; // Process completion status
};
// Function to sort processes by arrival time
void sortByArrivalTime(struct Process proc[], int n) {
struct Process temp;
for (int i = 0; i < n-1; i++) {
for (int j = i+1; j < n; j++) {
if (proc[i].arrival_time > proc[j].arrival_time) {
temp = proc[i];
proc[i] = proc[j];
proc[j] = temp;
}
}
}
}
// Function to find the next process with the shortest burst time at a given time
int findNextProcess(struct Process proc[], int n, int current_time) {
int idx = -1;
int min_burst_time = 1e9; // A large number to represent infinity
for (int i = 0; i < n; i++) {
if (!proc[i].completed && proc[i].arrival_time <= current_time && proc[i].burst_time <
min_burst_time) {
min_burst_time = proc[i].burst_time;
idx = i;
}
}
return idx;
}
int main() {
int n;
// Input number of processes
printf("Enter number of processes: ");
scanf("%d", &n);
5
struct Process proc[n];
// Input arrival time and burst time for each process
for (int i = 0; i < n; i++)
proc[i].id = i + 1;
printf("Enter Arrival Time for Process P%d: ", proc[i].id);
scanf("%d", &proc[i].arrival_time);
printf("Enter Burst Time for Process P%d: ", proc[i].id);
scanf("%d", &proc[i].burst_time);
proc[i].completed = false; // Initially, all processes are incomplete
}
// Sort processes by arrival time
sortByArrivalTime(proc, n);
int current_time = 0; // Keeps track of current time
int completed_count = 0;
float total_wait_time = 0, total_turn_time = 0;
// SJF Scheduling
while (completed_count < n) {
// Find the next process with the shortest burst time
int idx = findNextProcess(proc, n, current_time);
if (idx != -1) {
// Process is selected
current_time += proc[idx].burst_time;
proc[idx].completion_time = current_time;
proc[idx].turn_time = proc[idx].completion_time - proc[idx].arrival_time;
proc[idx].wait_time = proc[idx].turn_time - proc[idx].burst_time;
total_wait_time += proc[idx].wait_time;
total_turn_time += proc[idx].turn_time;
proc[idx].completed = true; // Mark the process as completed
completed_count++;
} else {
// If no process has arrived yet, advance time
current_time++;
}
}
// Display results
printf("\nProcess\tArrival Time\tBurst Time\tWaiting Time\tTurnaround Time\tCompletion Time\n");
for (int i = 0; i < n; i++) {
printf("P%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\n", proc[i].id, proc[i].arrival_time, proc[i].burst_time,
proc[i].wait_time, proc[i].turn_time, proc[i].completion_time);
}
printf("\nAverage Waiting Time: %.2f", total_wait_time / n);
printf("\nAverage Turnaround Time: %.2f", total_turn_time / n);
return 0;
}
6
OUTPUT :
7
Write a program to implement
Priority CPU scheduling algorithm.
PROGRAM :
#include<bits/stdc++.h>
using namespace std;
struct Process {
int id;
int burst_time;
int priority;
int waiting_time;
int turnaround_time;
};
bool comparePriority(Process p1, Process p2) {
return p1.priority > p2.priority;
}
void findWaitingTime(Process processes[], int n) {
processes[0].waiting_time = 0;
for (int i = 1; i < n; i++) {
processes[i].waiting_time = processes[i - 1].waiting_time + processes[i - 1].burst_time;
}
}
void findTurnAroundTime(Process processes[], int n) {
for (int i = 0; i < n; i++) {
processes[i].turnaround_time = processes[i].waiting_time + processes[i].burst_time;
}
}
void findAverageTime(Process processes[], int n) {
int total_waiting_time = 0, total_turnaround_time = 0;
findWaitingTime(processes, n);
findTurnAroundTime(processes, n);
8
cout << "\nProcesses Burst Time Priority Waiting Time Turnaround Time\n";
for (int i = 0; i < n; i++) {
total_waiting_time += processes[i].waiting_time;
total_turnaround_time += processes[i].turnaround_time;
cout << " " << processes[i].id << "\t\t" << processes[i].burst_time
<< "\t " << processes[i].priority << "\t "
<< processes[i].waiting_time << "\t\t " << processes[i].turnaround_time << endl;
}
cout << "\nAverage Waiting Time: " << (float)total_waiting_time / (float)n;
cout << "\nAverage Turnaround Time: " << (float)total_turnaround_time / (float)n;
}
int main() {
int n;
cout << "Enter the number of processes: ";
cin >> n;
Process processes[n];
cout << "Enter burst times and priorities for the processes: \n";
for (int i = 0; i < n; i++) {
processes[i].id = i + 1;
cout << "Process " << i + 1 << ": ";
cin >> processes[i].burst_time >> processes[i].priority;
}
sort(processes, processes + n, comparePriority);
findAverageTime(processes, n);
return 0;
}
9
OUTPUT :
10
Write a program to implement
Round Robin CPU scheduling algorithm.
PROGRAM :
#include <stdio.h>
#include <stdbool.h>
// Function to find the waiting time for all processes
void findWaitingTime(int processes[], int n, int bt[], int at[], int wt[], int quantum) {
int rem_bt[n]; // Remaining burst times
int t = 0; // Current time
bool done; // Check if all processes are done
// Copy the burst time into rem_bt[]
for (int i = 0; i < n; i++)
rem_bt[i] = bt[i];
// Loop until all processes are done
while (1) {
done = true;
// Traverse all processes one by one
for (int i = 0; i < n; i++) {
// Process only if it has arrived and has remaining burst time
if (rem_bt[i] > 0 && at[i] <= t) {
done = false; // There is a pending process
// If remaining burst time is more than the quantum
if (rem_bt[i] > quantum) {
t += quantum;
rem_bt[i] -= quantum;
}
// If process can be completed in the remaining burst time
else {
t += rem_bt[i];
wt[i] = t - bt[i] - at[i]; // Calculate waiting time
rem_bt[i] = 0; // Process is finished
}
}
}
// If all processes are done, exit the loop
if (done == true)
break;
}
}
// Function to calculate turn around time
void findTurnAroundTime(int processes[], int n, int bt[], int wt[], int tat[]) {
// Turnaround time is burst time + waiting time
for (int i = 0; i < n; i++)
tat[i] = bt[i] + wt[i];
}
// Function to calculate average time and display process info
11
void findavgTime(int processes[], int n, int bt[], int at[], int quantum) {
int wt[n], tat[n], total_wt = 0, total_tat = 0;
// Function to find waiting time for all processes
findWaitingTime(processes, n, bt, at, wt, quantum);
// Function to find turn around time for all processes
findTurnAroundTime(processes, n, bt, wt, tat);
// Display processes along with their burst time, arrival time, waiting time, and turn around time
printf("\nProcesses Arrival time Burst time Waiting time Turnaround time\n");
for (int i = 0; i < n; i++) {
total_wt += wt[i];
total_tat += tat[i];
printf(" %d\t\t%d\t\t%d\t\t%d\t\t%d\n", processes[i], at[i], bt[i], wt[i], tat[i]);
}
printf("\nAverage waiting time = %.2f", (float)total_wt / (float)n);
printf("\nAverage turnaround time = %.2f\n", (float)total_tat / (float)n);
}
// Driver code
int main() {
int n = 5; // Number of processes
int quantum = 3; // Time quantum
// Process IDs
int processes[] = {1, 2, 3, 4, 5};
// Burst times
int bt[] = {5, 10, 2, 1, 5};
// Arrival times
int at[] = {0, 1, 3, 4, 6};
// Call the Round Robin scheduling function
findavgTime(processes, n, bt, at, quantum);
return 0;
}
OUTPUT :
12
Write a program to implement Classical Inter Process
Communication Problem (Producer Consumer).
PROGRAM :
#include<bits/stdc++.h>
using namespace std;
const int BUFFER_SIZE = 5;
queue<int> buffer;
sem_t empty, full;
pthread_mutex_t mutex;
void* produce(void* arg) {
int item;
while (true) {
item = rand() % 100;
sem_wait(&empty);
pthread_mutex_lock(&mutex);
buffer.push(item);
cout << "Producer produced: " << item << endl;
pthread_mutex_unlock(&mutex);
sem_post(&full);
sleep(1);
}
}
void* consume(void* arg) {
int item;
while (true) {
sem_wait(&full);
pthread_mutex_lock(&mutex);
item = buffer.front();
buffer.pop();
cout << "Consumer consumed: " << item << endl;
pthread_mutex_unlock(&mutex);
sem_post(&empty);
sleep(2);
}
13
}
int main() {
pthread_t producerThread, consumerThread;
sem_init(&empty, 0, BUFFER_SIZE);
sem_init(&full, 0, 0);
pthread_mutex_init(&mutex, NULL);
pthread_create(&producerThread, NULL, produce, NULL);
pthread_create(&consumerThread, NULL, consume, NULL);
pthread_join(producerThread, NULL);
pthread_join(consumerThread, NULL);
sem_destroy(&empty);
sem_destroy(&full);
pthread_mutex_destroy(&mutex);
return 0;
}
OUTPUT :
14
Write a program to implement Classical Inter Process
Communication Problem (Reader Writers).
PROGRAM :
#include<bits/stdc++.h>
using namespace std;
mutex mtx;
condition_variable cv;
int read_count = 0;
bool writing = false;
void reader(int id) {
while (true) {
{
unique_lock<mutex> lock(mtx);
while (writing) {
cv.wait(lock);
}
read_count++;
}
cout << "Reader " << id << " is reading." << endl;
{
unique_lock<mutex> lock(mtx);
read_count--;
if (read_count == 0) {
cv.notify_all();
}
}
this_thread::sleep_for(chrono::milliseconds(500));
}
}
void writer(int id) {
while (true) {
15
{
unique_lock<mutex> lock(mtx);
while (read_count > 0 || writing) {
cv.wait(lock);
}
writing = true;
}
cout << "Writer " << id << " is writing." << endl;
{
unique_lock<mutex> lock(mtx);
writing = false;
}
cv.notify_all();
this_thread::sleep_for(chrono::milliseconds(1000));
}
}
int main() {
thread readers[5];
thread writers[2];
for (int i = 0; i < 5; i++) {
readers[i] = thread(reader, i + 1);
}
for (int i = 0; i < 2; i++) {
writers[i] = thread(writer, i + 1);
}
for (int i = 0; i < 5; i++) {
readers[i].join();
}
for (int i = 0; i < 2; i++) {
writers[i].join();
}
return 0;
}
16
OUTPUT :
17
Write a program to implement
Optimal Page Replacement algorithm.
PROGRAM :
#include<bits/stdc++.h>
using namespace std;
int findOptimalPage(vector<int>& pages, vector<int>& page_frame, int page_index, int
frame_size) {
int optimal_index = -1;
int farthest = page_index;
for (int i = 0; i < frame_size; i++) {
int j;
for (j = page_index; j < pages.size(); j++) {
if (page_frame[i] == pages[j]) {
if (j > farthest) {
farthest = j;
optimal_index = i;
}
break;
}
}
if (j == pages.size()) {
return i;
}
}
return (optimal_index == -1) ? 0 : optimal_index;
}
void optimalPageReplacement(vector<int>& pages, int frame_size) {
vector<int> page_frame(frame_size, -1);
int page_faults = 0;
for (int i = 0; i < pages.size(); i++) {
bool page_found = false;
for (int j = 0; j < frame_size; j++) {
18
if (page_frame[j] == pages[i]) {
page_found = true;
break;
}
}
if (!page_found) {
int replace_index = findOptimalPage(pages, page_frame, i + 1, frame_size);
page_frame[replace_index] = pages[i];
page_faults++;
cout << "Page fault: " << pages[i] << " replaced page " << page_frame[replace_index] <<
endl;
} else {
cout << "Page hit: " << pages[i] << endl;
}
cout << "Current page frame: ";
for (int k = 0; k < frame_size; k++) {
if (page_frame[k] != -1) {
cout << page_frame[k] << " ";
}
}
cout << endl;
}
cout << "Total page faults: " << page_faults << endl;
}
int main() {
int frame_size, num_pages;
cout << "Enter number of pages: ";
cin >> num_pages;
vector<int> pages(num_pages);
cout << "Enter page numbers:\n";
for (int i = 0; i < num_pages; i++) {
cin >> pages[i];
}
cout << "Enter number of page frames: ";
cin >> frame_size;
19
optimalPageReplacement(pages, frame_size);
return 0;
}
OUTPUT :
20
Write a program to implement
FIFO Page Replacement algorithm.
PROGRAM :
#include<bits/stdc++.h>
using namespace std;
void fifoPageReplacement(vector<int>& pages, int frame_size) {
vector<int> page_frame(frame_size, -1);
queue<int> fifo_queue;
int page_faults = 0;
for (int page : pages) {
bool page_found = false;
for (int j = 0; j < frame_size; j++) {
if (page_frame[j] == page) {
page_found = true;
break;
}
}
if (!page_found) {
if (fifo_queue.size() == frame_size) {
int oldest_page = fifo_queue.front();
fifo_queue.pop();
for (int j = 0; j < frame_size; j++) {
if (page_frame[j] == oldest_page) {
page_frame[j] = page;
break;
}
}
} else {
for (int j = 0; j < frame_size; j++) {
if (page_frame[j] == -1) {
page_frame[j] = page;
break;
}
21
}
}
fifo_queue.push(page);
page_faults++;
cout << "Page fault: " << page << " added to frame." << endl;
} else {
cout << "Page hit: " << page << endl;
}
cout << "Current page frame: ";
for (int k = 0; k < frame_size; k++) {
if (page_frame[k] != -1) {
cout << page_frame[k] << " ";
}
}
cout << endl;
}
cout << "Total page faults: " << page_faults << endl;
}
int main() {
int frame_size, num_pages;
cout << "Enter number of pages: ";
cin >> num_pages;
vector<int> pages(num_pages);
cout << "Enter page numbers:\n";
for (int i = 0; i < num_pages; i++) {
cin >> pages[i];
}
cout << "Enter number of page frames: ";
cin >> frame_size;
fifoPageReplacement(pages, frame_size);
return 0;
22
OUTPUT :
23
Write a program to implement
LRU Page Replacement algorithm.
PROGRAM :
#include<bits/stdc++.h>
using namespace std;
void lruPageReplacement(vector<int>& pages, int frame_size) {
unordered_map<int, list<int>::iterator> page_map;
list<int> lru;
int page_faults = 0;
for (int page : pages) {
if (page_map.find(page) == page_map.end()) {
if (lru.size() == frame_size) {
int lru_page = lru.back();
lru.pop_back();
page_map.erase(lru_page);
}
lru.push_front(page);
page_map[page] = lru.begin();
page_faults++;
cout << "Page fault: " << page << " added to frame." << endl;
} else {
lru.erase(page_map[page]);
lru.push_front(page);
page_map[page] = lru.begin();
cout << "Page hit: " << page << endl;
}
cout << "Current page frame: ";
for (int p : lru) {
cout << p << " ";
}
cout << endl;
}
cout << "Total page faults: " << page_faults << endl;
}
24
int main() {
int frame_size, num_pages;
cout << "Enter number of pages: ";
cin >> num_pages;
vector<int> pages(num_pages);
cout << "Enter page numbers:\n";
for (int i = 0; i < num_pages; i++) {
cin >> pages[i];
}
cout << "Enter number of page frames: ";
cin >> frame_size;
lruPageReplacement(pages, frame_size);
return 0;
}
OUTPUT :
25
Write a program to implement
Banker’s algorithms.
PROGRAM :
#include<bits/stdc++.h>
using namespace std;
class BankersAlgorithm {
private:
int processes;
int resources;
vector<int> available;
vector<vector<int>> max;
vector<vector<int>> allocation;
vector<vector<int>> need;
public:
BankersAlgorithm(int p, int r) : processes(p), resources(r) {
available.resize(resources);
max.resize(processes, vector<int>(resources));
allocation.resize(processes, vector<int>(resources));
need.resize(processes, vector<int>(resources));
}
void input() {
cout << "Enter available resources:\n";
for (int i = 0; i < resources; i++) {
cin >> available[i];
}
cout << "Enter maximum resources needed for each process:\n";
for (int i = 0; i < processes; i++) {
cout << "Process " << i + 1 << ": ";
for (int j = 0; j < resources; j++) {
cin >> max[i][j];
}
}
26
cout << "Enter currently allocated resources for each process:\n";
for (int i = 0; i < processes; i++) {
cout << "Process " << i + 1 << ": ";
for (int j = 0; j < resources; j++) {
cin >> allocation[i][j];
}
}
for (int i = 0; i < processes; i++) {
for (int j = 0; j < resources; j++) {
need[i][j] = max[i][j] - allocation[i][j];
}
}
}
bool isSafe() {
vector<bool> finish(processes, false);
vector<int> work = available;
vector<int> safe_sequence;
int count = 0;
while (count < processes) {
bool found = false;
for (int p = 0; p < processes; p++) {
if (!finish[p]) {
int j;
for (j = 0; j < resources; j++) {
if (need[p][j] > work[j]) {
break;
}
}
if (j == resources) {
for (int k = 0; k < resources; k++) {
work[k] += allocation[p][k];
}
safe_sequence.push_back(p);
finish[p] = true;
found = true;
count++;
}
}
}
27
if (!found) {
cout << "System is not in a safe state." << endl;
return false;
}
}
cout << "System is in a safe state.\nSafe sequence is: ";
for (int i : safe_sequence) {
cout << "P" << i + 1 << " ";
}
cout << endl;
return true;
}
};
int main() {
int processes, resources;
cout << "Enter number of processes: ";
cin >> processes;
cout << "Enter number of resources: ";
cin >> resources;
BankersAlgorithm ba(processes, resources);
ba.input();
ba.isSafe();
return 0;
}
OUTPUT :
28