OS Practical File
OS Practical File
PRACTICAL FILE
10
11
EXPERIMENT-01
Aim:
WAP to implement First Come First Serve Algorithm.
Code:
#include<iostream>
using namespace std;
int main()
{ int n,bt[20],wt[20],tat[20],avwt=0,avtat=0,i,j;
cout<<"Enter total number of processes(maximum 20):";
cin>>n;
return 0;
}
Output:
EXPERIMENT-02
Aim:
WAP to implement Shortest Job First Algorithm.
Code:
#include <iostream>
#include <climits> // for INT_MAX
using namespace std;
int main() {
int n, bt[20], at[20], wt[20], tat[20], completed[20] = {0};
int total_wt = 0, total_tat = 0, completed_processes = 0, current_time = 0;
// Find the process with the shortest burst time that has arrived
for (int i = 0; i < n; i++) {
if (at[i] <= current_time && !completed[i] && bt[i] < min_bt) {
min_bt = bt[i];
shortest_job = i;
}
}
// If a valid shortest job is found, execute it
if (shortest_job != -1) {
current_time += bt[shortest_job]; // Move time forward by the burst time
of the shortest job
wt[shortest_job] = current_time - at[shortest_job] - bt[shortest_job]; //
Waiting time
tat[shortest_job] = current_time - at[shortest_job]; // Turnaround time
total_wt += wt[shortest_job];
total_tat += tat[shortest_job];
completed[shortest_job] = 1; // Mark the process as completed
completed_processes++;
} else {
// If no process has arrived yet, increment time to avoid getting stuck
current_time++;
}
}
return 0;
}
Output:
EXPERIMENT-03
Aim:
WAP to implement Priority Scheduling Algorithm.
Code:
#include <iostream>
using namespace std;
int main() {
int n, bt[20], at[20], pr[20], wt[20], tat[20], pid[20];
int completed[20] = {0};
int total_wt = 0, total_tat = 0, current_time = 0, completed_processes = 0;
cout << "Enter Process Burst Time, Arrival Time, and Priority:\n";
for (int i = 0; i < n; i++) {
pid[i] = i + 1;
cout << "p[" << pid[i] << "] Burst Time: ";
cin >> bt[i];
cout << "p[" << pid[i] << "] Arrival Time: ";
cin >> at[i];
cout << "p[" << pid[i] << "] Priority: ";
cin >> pr[i];
}
return 0;
}
Output:
EXPERIMENT-04
Aim:
WAP to implement Round Robin Algorithm.
Code:
#include <iostream>
using namespace std;
void findWaitingTime(int processes[], int n, int bt[], int wt[], int quantum, int at[])
{
int rem_bt[n];
for (int i = 0; i < n; i++) {
rem_bt[i] = bt[i];
}
int t = 0;
while (true) {
bool done = true;
if (at[i] > t) {
t = at[i];
}
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];
}
}
void findAvgTime(int processes[], int n, int bt[], int at[], int quantum) {
int wt[n], tat[n], total_wt = 0, total_tat = 0;
int main() {
int n;
cout << "Enter the number of processes: ";
cin >> n;
int processes[n];
int burst_time[n];
int arrival_time[n];
cout << "Enter Process Burst Time and Arrival Time:\n";
for (int i = 0; i < n; i++) {
processes[i] = i + 1;
cout << "P[" << i + 1 << "] Arrival Time: ";
cin >> arrival_time[i];
cout << "P[" << i + 1 << "] Burst Time: ";
cin >> burst_time[i];
}
int quantum;
cout << "Enter the time quantum: ";
cin >> quantum;
return 0;
}
Output: