Operating System Practical File
Operating System Practical File
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.
Input:
Step 1: Initialization
Create arrays:
o wt[] to store waiting time of each process
o tat[] to store turnaround time of each process
Set wt[0] = 0
(First process ko wait nahi karna padta)
For every process i from 1 to n-1, calculate:
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:
#include<iostream>
// processes
wt[0] = 0;
// bt[i] + wt[i]
<< " Waiting time " << " Turn around time\n";
// around time
cout << " " << i+1 << "\t\t" << bt[i] <<"\t "
}
DEPARTMENT OF COMPUTER
SCIENCE ENGINEERING IIND Year
TH
PRACTICAL FILE IV Semester
// Driver code
int main()
//process id's
findavgTime(processes, n, burst_time);
return 0;
OUTPUT
1 10 0 10
2 4 10 14
3 7 14 21
EXPERIMENT NO.2
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.
Input:
n: Number of processes
bt[]: Burst time of each process
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]
SOURCE CODE:
#include <iostream>
int main() {
int A[100][4];
DEPARTMENT OF COMPUTER
SCIENCE ENGINEERING IIND Year
TH
PRACTICAL FILE IV Semester
cin >> n;
A[i][0] = i + 1;
index = i;
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;
A[i][2] = 0;
A[i][2] += A[j][1];
total += A[i][2];
avg_wt = (float)total / n;
total = 0;
// data.
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
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
EXPERIMENT NO.3
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:
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
cpp
CopyEdit
tat[i] = bt[i] + wt[i]
cpp
CopyEdit
average_wt = total_wt / n
average_tat = total_tat / n
SOURCE CODE:
// bt[i] + wt[i]
for (int i = 0; i < n; i++)
tat[i] = bt[i] + wt[i];
}
// Driver code
int main()
{
// process id's
int processes[] = {1, 2, 3};
int n = sizeof processes / sizeof processes[0];
// 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
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:
SOURCE CODE:
struct Process {
int pid; // Process ID
int bt; // CPU Burst time required
int priority; // Priority of this process
};
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
EXPERIMENT NO.5
DESCRIPTION:
SOURCE CODE:
#include <iostream>
using namespace std;
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;
}
}
// Driver code
int main() {
DEPARTMENT OF COMPUTER
SCIENCE ENGINEERING IIND Year
TH
PRACTICAL FILE IV Semester
return 0;
}
OUTPUT
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;
frame.push_back(page);
faults++;
}
}
return faults;
}
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;
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
EXPERIMENT NO.7
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.
SOURCE CODE:
// rpc_server.cpp
#include <iostream>
#include <netinet/in.h>
#include <unistd.h>
#include <cstring>
return a + b;
int main() {
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(8080);
listen(server_fd, 3);
close(new_socket);
close(server_fd);
return 0;
OUTPUT
Server Terminal:
Sent result: 30
Client Terminal: