0% found this document useful (0 votes)
2 views30 pages

Operating System Practical File

The document is a practical file for a Computer Science Engineering course, detailing experiments on CPU scheduling algorithms including First Come First Serve (FCFS), Shortest Job First (SJF), and Round Robin. Each experiment includes objectives, descriptions, algorithms, source code, and expected outputs. The file serves as a lab manual for students to implement and understand these scheduling techniques in C++.
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)
2 views30 pages

Operating System Practical File

The document is a practical file for a Computer Science Engineering course, detailing experiments on CPU scheduling algorithms including First Come First Serve (FCFS), Shortest Job First (SJF), and Round Robin. Each experiment includes objectives, descriptions, algorithms, source code, and expected outputs. The file serves as a lab manual for students to implement and understand these scheduling techniques in C++.
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/ 30

DEPARTMENT OF COMPUTER

SCIENCE ENGINEERING IIND Year


TH
PRACTICAL FILE IV Semester

LNCT (BHOPAL) INDORE CAMPUS

COMPUTER SCIENCE & ENGINEERING


DEPARTMENT

LAB MANNUAL
SUBJECT NAME:- Operating System
SUBJECT CODE: - CS405

SUBMITTED TO SUBMITTED BY
Asst Prof. Twinkal Manawat xyz

ENROLL. NO.-

SEMESTER-
DEPARTMENT OF COMPUTER
SCIENCE ENGINEERING IIND Year
TH
PRACTICAL FILE IV Semester

EXPERIMENT NO.1

AIM: To write a c program to simulate the CPU scheduling algorithm First Come First Serve (FCFS)

DESCRIPTION:

To calculate the average waiting time using the FCFS algorithm first the waiting time of the first process
is kept zero and the waiting time of the second process is the burst time of the first process and the
waiting time of the third process is the sum of the burst times of the first and the second process and so
on. After calculating all the waiting times the average waiting time is calculated as the average of all the
waiting times. FCFS mainly says first come first serve the algorithm which came first will be served first.

Algorithm for FCFS Scheduling

Input:

 n = Total number of processes


 bt[] = Burst time array (each process ko CPU kitne time ke liye chahiye)

Step 1: Initialization

 Create arrays:
o wt[] to store waiting time of each process
o tat[] to store turnaround time of each process

Step 2: Waiting Time Calculation

 Set wt[0] = 0
(First process ko wait nahi karna padta)
 For every process i from 1 to n-1, calculate:

Step 3: Turnaround Time Calculation

 For each process i from 0 to n-1, calculate:

Step 4: Average Time Calculation

 Initialize total_wt = 0 and total_tat = 0


 For each process i from 0 to n-1:
 Calculate average times:
DEPARTMENT OF COMPUTER
SCIENCE ENGINEERING IIND Year
TH
PRACTICAL FILE IV Semester

Step 5: Output

 Print each process with its burst time, waiting time, and turnaround time
 Print average waiting time and average turnaround time

SOURCE CODE:

// Online C++ compiler to run C++ program online

#include<iostream>

using namespace std;

// Function to find the waiting time for all

// processes

void findWaitingTime(int processes[], int n,

int bt[], int wt[])

// waiting time for first process is 0

wt[0] = 0;

// calculating waiting time

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

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


DEPARTMENT OF COMPUTER
SCIENCE ENGINEERING IIND Year
TH
PRACTICAL FILE IV Semester

// Function to calculate turn around time

void findTurnAroundTime( int processes[], int n,

int bt[], int wt[], int tat[])

// calculating turnaround time by adding

// bt[i] + wt[i]

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

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

//Function to calculate average time

void findavgTime( int processes[], int n, int bt[])

int wt[n], tat[n], total_wt = 0, total_tat = 0;

//Function to find waiting time of all processes

findWaitingTime(processes, n, bt, wt);

//Function to find turn around time for all processes


DEPARTMENT OF COMPUTER
SCIENCE ENGINEERING IIND Year
TH
PRACTICAL FILE IV Semester

findTurnAroundTime(processes, n, bt, wt, tat);

//Display processes along with all details

cout << "Processes "<< " Burst time "

<< " Waiting time " << " Turn around time\n";

// Calculate total waiting time and total turn

// around time

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

total_wt = total_wt + wt[i];

total_tat = total_tat + tat[i];

cout << " " << i+1 << "\t\t" << bt[i] <<"\t "

<< wt[i] <<"\t\t " << tat[i] <<endl;

cout << "Average waiting time = "

<< (float)total_wt / (float)n;

cout << "\nAverage turn around time = "

<< (float)total_tat / (float)n;

}
DEPARTMENT OF COMPUTER
SCIENCE ENGINEERING IIND Year
TH
PRACTICAL FILE IV Semester

// Driver code

int main()

//process id's

int processes[] = { 1, 2, 3};

int n = sizeof processes / sizeof processes[0];

//Burst time of all processes

int burst_time[] = {10, 4, 7};

findavgTime(processes, n, burst_time);

return 0;

OUTPUT

Processes Burst time Waiting time Turn around time

1 10 0 10

2 4 10 14

3 7 14 21

Average waiting time = 8

Average turn around time = 15


DEPARTMENT OF COMPUTER
SCIENCE ENGINEERING IIND Year
TH
PRACTICAL FILE IV Semester

EXPERIMENT NO.2

AIM: To write a program to implement SJF CPU scheduling algorithm.

DESCRIPTION:

To calculate the average waiting time using the FCFS algorithm first the waiting time of the first
process is kept zero and the waiting time of the second process is the burst time of the first
process and the waiting time of the third process is the sum of the burst times of the first and the
second process and so on. After calculating all the waiting times the average waiting time is
calculated as the average of all the waiting times. FCFS mainly says first come first serve the
algorithm which came first will be served first.

Algorithm for SJF (Shortest Job First) Scheduling

Input:

 n: Number of processes
 bt[]: Burst time of each process

Step 1: Input Process Details

1. Read n (number of processes)


2. For each process i = 0 to n-1:
o Input bt[i] (burst time)
o Assign process ID pid[i] = i + 1

Step 2: Sort Processes by Burst Time

3. Sort all processes in ascending order of burst time using selection sort:
o For each i = 0 to n-1
 Find index min_index of process with smallest bt in remaining unsorted
array
 Swap bt[i] with bt[min_index]
 Swap pid[i] with pid[min_index]

Step 3: Calculate Waiting Time (WT)

4. Set wt[0] = 0 (first process has 0 waiting time)


DEPARTMENT OF COMPUTER
SCIENCE ENGINEERING IIND Year
TH
PRACTICAL FILE IV Semester

5. For each process i = 1 to n-1:


o wt[i] = bt[0] + bt[1] + ... + bt[i-1]
o (i.e., sum of burst times of all previous processes)

Step 5: Calculate Averages

7. Compute total of all wt[i] and tat[i]


8. Compute:
o avg_wt = total_wt / n
o avg_tat = total_tat / n

Step 6: Display Output

9. Print for each process:


o Process ID, Burst Time, Waiting Time, Turnaround Time
10. Print:

 Average Waiting Time


 Average Turnaround Time

SOURCE CODE:

#include <iostream>

using namespace std;

int main() {

// Matrix for storing Process Id, Burst

// Time, Average Waiting Time & Average

// Turn Around Time.

int A[100][4];
DEPARTMENT OF COMPUTER
SCIENCE ENGINEERING IIND Year
TH
PRACTICAL FILE IV Semester

int i, j, n, total = 0, index, temp;

float avg_wt, avg_tat;

cout << "Enter number of process: ";

cin >> n;

cout << "Enter Burst Time:" << endl;

// User Input Burst Time and alloting Process Id.

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

cout << "P" << i + 1 << ": ";

cin >> A[i][1];

A[i][0] = i + 1;

// Sorting process according to their Burst Time.

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

index = i;

for (j = i + 1; j < n; j++)

if (A[j][1] < A[index][1])

index = j;
DEPARTMENT OF COMPUTER
SCIENCE ENGINEERING IIND Year
TH
PRACTICAL FILE IV Semester

temp = A[i][1];

A[i][1] = A[index][1];

A[index][1] = temp;

temp = A[i][0];

A[i][0] = A[index][0];

A[index][0] = temp;

A[0][2] = 0;

// Calculation of Waiting Times

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

A[i][2] = 0;

for (j = 0; j < i; j++)

A[i][2] += A[j][1];

total += A[i][2];

avg_wt = (float)total / n;

total = 0;

cout << "P BT WT TAT" << endl;


DEPARTMENT OF COMPUTER
SCIENCE ENGINEERING IIND Year
TH
PRACTICAL FILE IV Semester

// Calculation of Turn Around Time and printing the

// data.

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

A[i][3] = A[i][1] + A[i][2];

total += A[i][3];

cout << "P" << A[i][0] << " " << A[i][1] << " " << A[i][2] << " " << A[i][3] <<
endl;

avg_tat = (float)total / n;

cout << "Average Waiting Time= " << avg_wt << endl;

cout << "Average Turnaround Time= " << avg_tat << endl;

OUTPUT

Enter number of process: 3

Enter Burst Time:

P1: 5

P2: 3

P3: 4

P BT WT TAT
DEPARTMENT OF COMPUTER
SCIENCE ENGINEERING IIND Year
TH
PRACTICAL FILE IV Semester

P2 3 0 3

P3 4 3 7

P1 5 7 12

Average Waiting Time= 3.33333

Average Turnaround Time= 7.33333


DEPARTMENT OF COMPUTER
SCIENCE ENGINEERING IIND Year
TH
PRACTICAL FILE IV Semester

EXPERIMENT NO.3

AIM: To simulate the CPU scheduling algorithm round-robin.

DESCRIPTION:

To aim is to calculate the average waiting time. There will be a time slice, each process should be
executed within that time-slice and if not it will go to the waiting state so first check whether the burst
time is less than the time-slice. If it is less than it assign the waiting time to the sum of the total times. If
it is greater than the burst-time then subtract the time slot from the actual burst time and increment it
by time-slot and the loop continues until all the processes are completed.

Step-by-step Algorithm:

1. Initialize Remaining Burst Time:

cpp
CopyEdit
rem_bt[i] = bt[i] // copy burst time to rem_bt[]

2. Initialize Time = 0
3. Repeat Until All Processes are Done:
o Set done = true
o For each process i = 0 to n-1:
 If rem_bt[i] > 0:
 done = false
 If rem_bt[i] > quantum:
 Process for quantum time
 t = t + quantum
 rem_bt[i] -= quantum
 Else:
 Process for rem_bt[i] time
 t = t + rem_bt[i]
 wt[i] = t - bt[i]
 rem_bt[i] = 0
4. When All Processes are Done:
o Break loop
DEPARTMENT OF COMPUTER
SCIENCE ENGINEERING IIND Year
TH
PRACTICAL FILE IV Semester

5. Calculate Turn Around Time for each process:

cpp
CopyEdit
tat[i] = bt[i] + wt[i]

6. Calculate Average Waiting Time and Average Turn Around Time:

cpp
CopyEdit
average_wt = total_wt / n
average_tat = total_tat / n

7. Print Process Info (BT, WT, TAT)

SOURCE CODE:

// C++ program for implementation of RR scheduling


#include <iostream>
using namespace std;

// Function to find the waiting time for all


// processes
void findWaitingTime(int processes[], int n, int bt[], int wt[], int quantum)
{
// Make a copy of burst times bt[] to store remaining
// burst times.
int rem_bt[n];
for (int i = 0; i < n; i++)
rem_bt[i] = bt[i];

int t = 0; // Current time

// Keep traversing processes in round robin manner


// until all of them are not done.
while (1)
{
bool done = true;

// Traverse all processes one by one repeatedly


for (int i = 0; i < n; i++)
{
// If burst time of a process is greater than 0
DEPARTMENT OF COMPUTER
SCIENCE ENGINEERING IIND Year
TH
PRACTICAL FILE IV Semester

// then only need to process further


if (rem_bt[i] > 0)
{
done = false; // There is a pending process

if (rem_bt[i] > quantum)


{
// Increase the value of t i.e. shows
// how much time a process has been processed
t += quantum;

// Decrease the burst_time of current process


// by quantum
rem_bt[i] -= quantum;
}

// If burst time is smaller than or equal to


// quantum. Last cycle for this process
else
{
// Increase the value of t i.e. shows
// how much time a process has been processed
t = t + rem_bt[i];

// Waiting time is current time minus time


// used by this process
wt[i] = t - bt[i];

// As the process gets fully executed


// make its remaining burst time = 0
rem_bt[i] = 0;
}
}
}

// If all processes are done


if (done == true)
break;
}
}

// Function to calculate turn around time


void findTurnAroundTime(int processes[], int n, int bt[], int wt[], int tat[])
{
// calculating turnaround time by adding
DEPARTMENT OF COMPUTER
SCIENCE ENGINEERING IIND Year
TH
PRACTICAL FILE IV Semester

// bt[i] + wt[i]
for (int i = 0; i < n; i++)
tat[i] = bt[i] + wt[i];
}

// Function to calculate average time


void findavgTime(int processes[], int n, int bt[], int quantum)
{
int wt[n], tat[n], total_wt = 0, total_tat = 0;

// Function to find waiting time of all processes


findWaitingTime(processes, n, bt, wt, quantum);

// Function to find turn around time for all processes


findTurnAroundTime(processes, n, bt, wt, tat);

// Display processes along with all details


cout << "PN\t "
<< " \tBT "
<< " WT "
<< " \tTAT\n";

// Calculate total waiting time and total turn


// around time
for (int i = 0; i < n; i++)
{
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
cout << " " << i + 1 << "\t\t" << bt[i] << "\t " << wt[i] << "\t\t " << tat[i] << endl;
}

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


cout << "\nAverage turn around time = " << (float)total_tat / (float)n;
}

// Driver code
int main()
{
// process id's
int processes[] = {1, 2, 3};
int n = sizeof processes / sizeof processes[0];

// Burst time of all processes


int burst_time[] = {10, 5, 8};
DEPARTMENT OF COMPUTER
SCIENCE ENGINEERING IIND Year
TH
PRACTICAL FILE IV Semester

// Time quantum
int quantum = 2;
findavgTime(processes, n, burst_time, quantum);
return 0;
}

OUTPUT

PN BT WT TAT
1 10 13 23
2 5 10 15
3 8 13 21
Average waiting time = 12
Average turn around time = 19.6667
DEPARTMENT OF COMPUTER
SCIENCE ENGINEERING IIND Year
TH
PRACTICAL FILE IV Semester

EXPERIMENT NO.4

AIM: Write a program to implement priority CPU scheduling algorithm.

DESCRIPTION:

To calculate the average waiting time in the priority algorithm, sort the burst times according to their
priorities and then calculate the average waiting time of the processes. The waiting time of each process
is obtained by summing up the burst times of all the previous processes.

Step-by-Step Algorithm:

Step 1: Input Process Details

 Store process ID, burst time, and priority in a structure Process.

Step 2: Sort Processes by Priority


cpp
CopyEdit
sort(proc, proc + n, comparison);

 Sort all processes in descending order of priority using a custom comparator.

Step 3: Find Waiting Time


cpp
CopyEdit
wt[0] = 0
for i = 1 to n-1:
wt[i] = proc[i-1].bt + wt[i-1]

 First process gets 0 waiting time.


 Remaining processes' waiting time = cumulative burst time of previous processes.
DEPARTMENT OF COMPUTER
SCIENCE ENGINEERING IIND Year
TH
PRACTICAL FILE IV Semester

Step 4: Find Turn Around Time


cpp
CopyEdit
tat[i] = proc[i].bt + wt[i]

 Turn Around Time = Burst Time + Waiting Time

Step 5: Calculate Averages


cpp
CopyEdit
avg_wt = total_wt / n
avg_tat = total_tat / n

 Sum all waiting times and TATs, divide by total processes.

Step 6: Display Results

 Show Process ID, BT, WT, TAT for each.


 Print average WT and average TAT.

SOURCE CODE:

// C++ program for implementation of FCFS


// scheduling
#include <bits/stdc++.h>
using namespace std;

struct Process {
int pid; // Process ID
int bt; // CPU Burst time required
int priority; // Priority of this process
};

// Function to sort the Process acc. to priority


bool comparison(Process a, Process b)
{
return (a.priority > b.priority);
DEPARTMENT OF COMPUTER
SCIENCE ENGINEERING IIND Year
TH
PRACTICAL FILE IV Semester

// Function to find the waiting time for all


// processes
void findWaitingTime(Process proc[], int n, int wt[])
{
// waiting time for first process is 0
wt[0] = 0;

// calculating waiting time


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

// Function to calculate turn around time


void findTurnAroundTime(Process proc[], int n, int wt[],
int tat[])
{
// calculating turnaround time by adding
// bt[i] + wt[i]
for (int i = 0; i < n; i++)
tat[i] = proc[i].bt + wt[i];
}

// Function to calculate average time


void findavgTime(Process proc[], int n)
{
int wt[n], tat[n], total_wt = 0, total_tat = 0;

// Function to find waiting time of all processes


findWaitingTime(proc, n, wt);

// Function to find turn around time for all processes


findTurnAroundTime(proc, n, wt, tat);

// Display processes along with all details


cout << "\nProcesses "
<< " Burst time "
<< " Waiting time "
<< " Turn around time\n";

// Calculate total waiting time and total turn


// around time
for (int i = 0; i < n; i++) {
total_wt = total_wt + wt[i];
DEPARTMENT OF COMPUTER
SCIENCE ENGINEERING IIND Year
TH
PRACTICAL FILE IV Semester

total_tat = 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 / (float)n;
cout << "\nAverage turn around time = "
<< (float)total_tat / (float)n;
}

void priorityScheduling(Process proc[], int n)


{
// Sort processes by priority
sort(proc, proc + n, comparison);

cout << "Order in which processes gets executed \n";


for (int i = 0; i < n; i++)
cout << proc[i].pid << " ";

findavgTime(proc, n);
}

// Driver code
int main()
{
Process proc[]
= { { 1, 10, 2 }, { 2, 5, 0 }, { 3, 8, 1 } };
int n = sizeof proc / sizeof proc[0];
priorityScheduling(proc, n);
return 0;
}

OUTPUT

Order in which processes gets executed


132
Processes Burst time Waiting time Turn around time
1 10 0 10
3 8 10 18
2 5 18 23

Average waiting time = 9.33333


DEPARTMENT OF COMPUTER
SCIENCE ENGINEERING IIND Year
TH
PRACTICAL FILE IV Semester

Average turn around time = 17

EXPERIMENT NO.5

AIM: Write a program to implement Banker’s algorithms.

DESCRIPTION:

Banker's Algorithm is a deadlock avoidance algorithm used in operating systems. It checks


whether allocating resources to a process is safe or not before actually doing it.

 It ensures that the system never enters an unsafe state.


 If a process requests resources, the system checks using the Banker's algorithm whether it
can allocate resources without risking deadlock.

SOURCE CODE:

#include <iostream>
using namespace std;

const int P = 5; // Number of processes


const int R = 3; // Number of resources

// Function to find the need matrix


void calculateNeed(int need[P][R], int maxm[P][R], int allot[P][R]) {
for (int i = 0; i < P; i++)
for (int j = 0; j < R; j++)
need[i][j] = maxm[i][j] - allot[i][j];
}

// Function to check if all needs of a process can be satisfied


bool check(int need[], int avail[]) {
for (int i = 0; i < R; i++)
if (need[i] > avail[i])
return false;
return true;
DEPARTMENT OF COMPUTER
SCIENCE ENGINEERING IIND Year
TH
PRACTICAL FILE IV Semester

// Function to implement Banker's Algorithm


void isSafe(int processes[], int avail[], int maxm[][R], int allot[][R]) {
int need[P][R];
calculateNeed(need, maxm, allot);

bool finish[P] = {0};


int safeSeq[P];
int work[R];

// Initialize work as available


for (int i = 0; i < R; i++)
work[i] = avail[i];

int count = 0;
while (count < P) {
bool found = false;
for (int p = 0; p < P; p++) {
if (!finish[p] && check(need[p], work)) {
// Add allocated resources to work
for (int k = 0; k < R; k++)
work[k] += allot[p][k];

safeSeq[count++] = p;
finish[p] = true;
found = true;
}
}

if (!found) {
cout << "System is not in a safe state." << endl;
return;
}
}

// If system is in safe state


cout << "System is in a safe state.\nSafe sequence is: ";
for (int i = 0; i < P; i++)
cout << "P" << safeSeq[i] << " ";
cout << endl;
}

// Driver code
int main() {
DEPARTMENT OF COMPUTER
SCIENCE ENGINEERING IIND Year
TH
PRACTICAL FILE IV Semester

int processes[] = {0, 1, 2, 3, 4};

// Available instances of resources


int avail[] = {3, 3, 2};

// Maximum R that can be allocated to processes


int maxm[P][R] = {
{7, 5, 3},
{3, 2, 2},
{9, 0, 2},
{2, 2, 2},
{4, 3, 3}
};

// Resources allocated to processes


int allot[P][R] = {
{0, 1, 0},
{2, 0, 0},
{3, 0, 2},
{2, 1, 1},
{0, 0, 2}
};

isSafe(processes, avail, maxm, allot);

return 0;
}

OUTPUT

System is in a safe state.


Safe sequence is: P1 P3 P4 P0 P2
DEPARTMENT OF COMPUTER
SCIENCE ENGINEERING IIND Year
TH
PRACTICAL FILE IV Semester

EXPERIMENT NO.6

AIM: Write a program to implement & compare various page replacement algorithm.

DESCRIPTION:

1. FIFO (First-In-First-Out):
The oldest page in memory is replaced first. It’s simple but can suffer from Belady’s
Anomaly.
2. LRU (Least Recently Used):
Replaces the page that hasn’t been used for the longest time. It uses history to make a
decision.
3. Optimal Page Replacement:
Replaces the page that will not be used for the longest time in future. It is theoretical
and gives the best result but can't be implemented in real-time OS.

SOURCE CODE:

#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>
#include <climits>
using namespace std;

// Function to implement FIFO Page Replacement


int fifoPageReplacement(vector<int> pages, int capacity) {
vector<int> frame;
int faults = 0;

for (int page : pages) {


// If page not in frame
if (find(frame.begin(), frame.end(), page) == frame.end()) {
if (frame.size() == capacity)
frame.erase(frame.begin()); // Remove first inserted
DEPARTMENT OF COMPUTER
SCIENCE ENGINEERING IIND Year
TH
PRACTICAL FILE IV Semester

frame.push_back(page);
faults++;
}
}
return faults;
}

// Function to implement LRU Page Replacement


int lruPageReplacement(vector<int> pages, int capacity) {
vector<int> frame;
int faults = 0;

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


auto it = find(frame.begin(), frame.end(), pages[i]);

// Page not found


if (it == frame.end()) {
if (frame.size() == capacity)
frame.erase(frame.begin()); // Remove least recently used
faults++;
} else {
frame.erase(it); // Remove old position
}
frame.push_back(pages[i]); // Add current as most recently used
}
return faults;
}

// Function to implement Optimal Page Replacement


int optimalPageReplacement(vector<int> pages, int capacity) {
vector<int> frame;
int faults = 0;

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


if (find(frame.begin(), frame.end(), pages[i]) == frame.end()) {
if (frame.size() < capacity) {
frame.push_back(pages[i]);
} else {
// Find the page to be replaced
int farthest = -1, idx = -1;

for (int j = 0; j < frame.size(); j++) {


int k;
for (k = i + 1; k < pages.size(); k++) {
if (frame[j] == pages[k])
DEPARTMENT OF COMPUTER
SCIENCE ENGINEERING IIND Year
TH
PRACTICAL FILE IV Semester

break;
}

if (k > farthest) {
farthest = k;
idx = j;
}
}

frame[idx] = pages[i];
}
faults++;
}
}
return faults;
}

// Driver code
int main() {
vector<int> pages = {7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3};
int capacity = 3;

int fifo_faults = fifoPageReplacement(pages, capacity);


int lru_faults = lruPageReplacement(pages, capacity);
int optimal_faults = optimalPageReplacement(pages, capacity);

cout << "Page Reference String: ";


for (int p : pages) cout << p << " ";
cout << "\n\n";

cout << "Page Frame Size: " << capacity << endl;
cout << "FIFO Page Faults: " << fifo_faults << endl;
cout << "LRU Page Faults: " << lru_faults << endl;
cout << "Optimal Page Faults: " << optimal_faults << endl;

return 0;
}

OUTPUT

Page Reference String: 7 0 1 2 0 3 0 4 2 3 0 3

Page Frame Size: 3


FIFO Page Faults: 9
DEPARTMENT OF COMPUTER
SCIENCE ENGINEERING IIND Year
TH
PRACTICAL FILE IV Semester

LRU Page Faults: 8


Optimal Page Faults: 7

EXPERIMENT NO.7

AIM: Write a program to implement Remote Procedure Call (RPC).

DESCRIPTION:

Remote Procedure Call (RPC) is a protocol that allows a program to call a function or
procedure on a remote server as if it were a local function.

 Client sends a request to execute a function.


 Server executes the function and sends the result back.
 Enables inter-process communication across networks.
 Used in distributed systems, databases, and micro services.

SOURCE CODE:

// rpc_server.cpp

#include <iostream>

#include <netinet/in.h>

#include <unistd.h>

#include <cstring>

using namespace std;

int add(int a, int b) {


DEPARTMENT OF COMPUTER
SCIENCE ENGINEERING IIND Year
TH
PRACTICAL FILE IV Semester

return a + b;

int main() {

int server_fd, new_socket;

struct sockaddr_in address;

int addrlen = sizeof(address);

int buffer[2]; // For two integers

server_fd = socket(AF_INET, SOCK_STREAM, 0);

address.sin_family = AF_INET;

address.sin_addr.s_addr = INADDR_ANY;

address.sin_port = htons(8080);

bind(server_fd, (struct sockaddr *)&address, sizeof(address));

listen(server_fd, 3);

cout << "Server is waiting for connection...\n";

new_socket = accept(server_fd, (struct sockaddr *)&address,


(socklen_t*)&addrlen);
DEPARTMENT OF COMPUTER
SCIENCE ENGINEERING IIND Year
TH
PRACTICAL FILE IV Semester

read(new_socket, buffer, sizeof(buffer));

int result = add(buffer[0], buffer[1]);

send(new_socket, &result, sizeof(result), 0);

cout << "Sent result: " << result << endl;

close(new_socket);

close(server_fd);

return 0;

OUTPUT

Server Terminal:

Server is waiting for connection...

Sent result: 30

Client Terminal:

Result received from server (10 + 20): 30

You might also like