0% found this document useful (0 votes)
4 views13 pages

OS Practical File

The document is a practical file for the Operating System Lab at Guru Tegh Bahadur Institute of Technology, detailing various scheduling algorithms implemented in C++. It includes experiments for First Come First Serve, Shortest Job First, Priority Scheduling, and Round Robin algorithms, complete with code and output for each. Each experiment outlines the aim, code, and results, providing a comprehensive overview of process scheduling techniques.

Uploaded by

kaurashmeet2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views13 pages

OS Practical File

The document is a practical file for the Operating System Lab at Guru Tegh Bahadur Institute of Technology, detailing various scheduling algorithms implemented in C++. It includes experiments for First Come First Serve, Shortest Job First, Priority Scheduling, and Round Robin algorithms, complete with code and output for each. Each experiment outlines the aim, code, and results, providing a comprehensive overview of process scheduling techniques.

Uploaded by

kaurashmeet2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Guru Tegh Bahadur Institute of Technology, Delhi

(Affiliated to Guru Gobind Singh Indraprastha University, Dwarka, New Delhi)

Department of Computer Science & Engineering

PRACTICAL FILE

Operating System Lab

(Paper code: CIC 353)

Submitted to: Submitted by:


MS. SWATI Name: ASHMEET KAUR
Class: CSE-03(5th sem)
Enroll. No.: 09576802722
Index

S.No. Name of Experiment Date T.Sign


1

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;

cout<<"\nEnter Process Burst Time aka DURATION \n";


for(i=0;i<n;i++)
{
cout<<"P["<<i+1<<"]:";
cin>>bt[i];
}
wt[0]=0; //waiting time for first process is 0
//calculating waiting time
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
}
cout<<"\nProcess\t\tBurst Time\tWaiting Time\tTurnaround Time";
//calculating turnaround time
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
avwt+=wt[i];
avtat+=tat[i];
cout<<"\nP["<<i+1<<"]"<<"\t\t"<<bt[i]<<"\t\t"<<wt[i]<<"\t\t"<<tat[i];
}
avwt/=i;
avtat/=i;
cout<<"\n\nAverage Waiting Time:"<<avwt;
cout<<"\nAverage Turnaround Time:"<<avtat;

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;

cout << "Enter total number of processes: ";


cin >> n;

// Input Burst Time and Arrival Time for each process


for (int i = 0; i < n; i++) {
cout << "p[" << i + 1 << "] Burst Time: ";
cin >> bt[i];
cout << "p[" << i + 1 << "] Arrival Time: ";
cin >> at[i];
}

// Loop until all processes are completed


while (completed_processes < n) {
int min_bt = INT_MAX;
int shortest_job = -1;

// 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++;
}
}

// Output the results


cout << "\nProcess\tBurst Time\tArrival Time\tWaiting Time\tTurnaround
Time\n";
for (int i = 0; i < n; i++) {
cout << "p[" << i + 1 << "]\t\t" << bt[i] << "\t\t" << at[i] << "\t\t" << wt[i] <<
"\t\t" << tat[i] << "\n";
}

// Output the average waiting and turnaround times


cout << "\nAverage Waiting Time: " << (float)total_wt / n;
cout << "\nAverage Turnaround Time: " << (float)total_tat / n;

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 total number of processes: ";


cin >> n;

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

while (completed_processes < n) {


int highest_priority = 10000;
int idx = -1;

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


if (at[i] <= current_time && !completed[i] && pr[i] < highest_priority) {
highest_priority = pr[i];
idx = i;
}
}
if (idx != -1) {
current_time += bt[idx];
tat[idx] = current_time - at[idx];
wt[idx] = tat[idx] - bt[idx];
total_wt += wt[idx];
total_tat += tat[idx];
completed[idx] = 1;
completed_processes++;
} else {
current_time++;
}
}

cout << "\nProcess\tPriority\tBurst Time\tArrival Time\tWaiting


Time\tTurnaround Time\n";
for (int i = 0; i < n; i++) {
cout << "p[" << pid[i] << "]\t" << pr[i] << "\t\t" << bt[i] << "\t\t" << at[i] <<
"\t\t" << wt[i] << "\t\t" << tat[i] << "\n";
}

cout << "\nAverage Waiting Time: " << (float)total_wt / n;


cout << "\nAverage Turnaround Time: " << (float)total_tat / n;

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;

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


if (rem_bt[i] > 0) {
done = false;

if (at[i] > t) {
t = at[i];
}

if (rem_bt[i] > quantum) {


t += quantum;
rem_bt[i] -= quantum;
} else {
t += rem_bt[i];
wt[i] = t - bt[i] - at[i];
rem_bt[i] = 0;
}
}
}
if (done == true)
break;
}
}

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;

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


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

cout << "Process\tArrival Time\tBurst Time\tWaiting Time\tTurnaround


Time\n";
for (int i = 0; i < n; i++) {
total_wt += wt[i];
total_tat += tat[i];
cout << "P[" << processes[i] << "]\t\t" << at[i] << "\t\t" << bt[i] << "\t\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;
}

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;

findAvgTime(processes, n, burst_time, arrival_time, quantum);

return 0;
}

Output:

You might also like