C OSlm
C OSlm
LABORATORY MANUAL
OPERATING SYSTEM
Subject Code: 1330301
Integrated M.Sc. SEM. III (Computer Science), Year 2022-23
2023-24 (Odd Semester)
CERTIFICATE
Date of submission:
#include <iostream>
using namespace std;
int* at, *bt, *tat, *wt, *ct, n, sum_wt, sum_tat;
class fcfs
{
public:
int curr;
fcfs (int a)
{
at = (int*)malloc(a*sizeof(int));
bt = (int*)malloc(a*sizeof(int));
tat = (int*)malloc(a*sizeof(int));
wt = (int*)malloc(a*sizeof(int));
ct = (int*)malloc(a*sizeof(int));
n = a;
sum_wt = sum_tat = 0;
}
void process()
{
curr = at[0];
for(int i = 0; i < n; i++)
{
wt[i] = curr - at[i];
sum_wt += wt[i];
curr = max(curr, at[i]) + bt[i];
ct[i] = at[i] + bt[i] + wt[i];
tat[i] = ct[i] - at[i];
sum_tat += tat[i];
}
}
void display()
{
cout << "\n\nArrival Time Burst Time Waiting Time Turn Around Time Completion Time" << endl;
for(int i = 0; i < n; i++)
{
cout << " " << at[i] << "\t\t" << bt[i] << "\t\t" << wt[i] << "\t\t" << tat[i] << "\t\t" << ct[i] << endl;
}
cout << "\nAverage Waiting Time = " << (float)sum_wt/n;
cout << "\nAverage Turn Around Time = " << (float)sum_tat/n << endl << endl;
}
};
int main()
{
cout << "Enter number of processes" << endl;
cin >> n;
fcfs obj(n);
at[0] = 0;
cout << "\nEnter Burst Time 1: ";
cin >> bt[0];
for(int i = 1; i < n; i++)
{
cout << "Enter Arrival Time " << i+1 << ": ";
cin >> at[i];
cout << "Enter Burst Time " << i+1 << ": ";
cin >> bt[i];
}
obj.process();
obj.display();
Program 7
To write a program demonstrating non-pre-emptive Shortest Job First
scheduling.
#include <iostream>
using namespace std;
int mat[10][7];
void arrangeArrival(int num, int mat[][7])
{
for (int i = 0; i < num; i++)
{
for (int j = 0; j < num - i - 1; j++)
{
if (mat[j][2] > mat[j + 1][2])
{
for (int k = 0; k < 3; k++)
{
int temp = mat[j][k];
mat[j][k] = mat[j+1][k];
mat[j+1][k] = temp;
}
}
}
}
}
void completionTime(int num, int mat[][7])
{
int k = 0;
do{
int low = k;
for(int i = 0; i < num; i++)
{
if(mat[low][1] > mat[i][1] && mat[i][6])
low = i;
}
int curr = mat[low][1];
mat[low][4] = 0;
mat[low][3] = mat[low][1] + mat[low][2];
curr = mat[low][3];
mat[low][5] = mat[low][3] - mat[low][1];
mat[low][6] = 0;
k++;
for(int i = 0; i < num; i++)
{
if(curr >= mat[i][1] && mat[i][6])
{
mat[i][4] = curr - mat[i][1];
mat[i][3] = mat[i][1] + mat[i][2] + mat[i][4];
curr = mat[i][3];
mat[i][5] = mat[i][3] - mat[i][1];
mat[i][6] = 0;
k++;
}
}
}while(k < num);
}
int main()
{
int num;
cout << "Enter number of Process: ";
cin >> num;
cout << "...Enter the process ID...\n\n";
for (int i = 0; i < num; i++)
{
cout << "...Process " << i + 1 << "...\n";
cout << "Enter Process Id: ";
cin >> mat[i][0];
cout << "Enter Arrival Time: ";
cin >> mat[i][1];
cout << "Enter Burst Time: ";
cin >> mat[i][2];
mat[i][6] = 1;
cout << "\n";
}
arrangeArrival(num, mat);
completionTime(num, mat);
cout << "\n\nProcess ID\tArrival Time\tBurst Time\tCompletion Time\tWaiting Time\tTurnaround
Time\n";
for (int i = 0; i < num; i++)
cout << " " << mat[i][0] << "\t\t" << mat[i][1] << "\t\t" << mat[i][2] << "\t\t" << mat[i][3] << "\t\t"
<<
mat[i][4] << "\t\t" << mat[i][5] << "\n";
}
Program 8
To write a program demonstrating non-pre-emptive Longest Job First
scheduling.
#include <iostream>
using namespace std;
int mat[10][7];
void arrangeArrival(int num, int mat[][7])
{
for (int i = 0; i < num; i++)
{
for (int j = 0; j < num - i - 1; j++)
{
if (mat[j][2] < mat[j + 1][2])
{
for (int k = 0; k < 3; k++)
{
int temp = mat[j][k];
mat[j][k] = mat[j+1][k];
mat[j+1][k] = temp;
}
}
}
}
}
void completionTime(int num, int mat[][7])
{
int k = 0;
do{
int low = k;
for(int i = 0; i < num; i++)
{
if(mat[low][1] > mat[i][1] && mat[i][6])
low = i;
}
int curr = mat[low][1];
mat[low][4] = 0;
mat[low][3] = mat[low][1] + mat[low][2];
curr = mat[low][3];
mat[low][5] = mat[low][3] - mat[low][1];
mat[low][6] = 0;
k++;
for(int i = 0; i < num; i++)
{
if(curr >= mat[i][1] && mat[i][6])
{
mat[i][4] = curr - mat[i][1];
mat[i][3] = mat[i][1] + mat[i][2] + mat[i][4];
curr = mat[i][3];
mat[i][5] = mat[i][3] - mat[i][1];
mat[i][6] = 0;
k++;
}
}
}while(k < num);
}
int main()
{
int num;
cout << "Enter number of Process: ";
cin >> num;
cout << "...Enter the process ID...\n\n";
for (int i = 0; i < num; i++)
{
cout << "...Process " << i + 1 << "...\n";
cout << "Enter Process Id: ";
cin >> mat[i][0];
cout << "Enter Arrival Time: ";
cin >> mat[i][1];
cout << "Enter Burst Time: ";
cin >> mat[i][2];
mat[i][6] = 1;
cout << "\n";
}
arrangeArrival(num, mat);
completionTime(num, mat);
cout << "\n\nProcess ID\tArrival Time\tBurst Time\tCompletion Time\tWaiting Time\tTurnaround
Time\n";
for (int i = 0; i < num; i++)
cout << " " << mat[i][0] << "\t\t" << mat[i][1] << "\t\t" << mat[i][2] << "\t\t" << mat[i][3] << "\t\t"
<<
mat[i][4] << "\t\t" << mat[i][5] << "\n";
}
Program 9
To write a program demonstrating non-pre-emptive Priority scheduling.
#include <iostream>
using namespace std;
int mat[10][8];
void arrangeArrival(int num, int mat[][8])
{
for (int i = 0; i < num; i++)
{
for (int j = 0; j < num - i - 1; j++)
{
if (mat[j][3] < mat[j + 1][3])
{
for (int k = 0; k < 4; k++)
{
int temp = mat[j][k];
mat[j][k] = mat[j+1][k];
mat[j+1][k] = temp;
}
}
}
}
}
void completionTime(int num, int mat[][8])
{
int k = 0;
int low = k;
for(int i = 0; i < num; i++)
{
if(mat[low][1] > mat[i][1] && mat[i][7])
low = i;
}
int curr = mat[low][1];
mat[low][4] = curr + mat[low][2];
curr = mat[low][4];
mat[low][6] = mat[low][4] - mat[low][1];
mat[low][5] = mat[low][6] - mat[low][2];
mat[low][7] = 0;
k++;
for(int i = 0; i < num && k < num; i++)
{
if(curr >= mat[i][1] && mat[i][7])
{
mat[i][4] = curr + mat[i][2];
curr = mat[i][4];
mat[i][6] = mat[i][4] - mat[i][1];
mat[i][5] = mat[i][6] - mat[i][2];
mat[i][7] = 0;
k++;
i = -1;
}
}
}
int main()
{
int num;
cout << "Enter number of Process: ";
cin >> num;
int mat[num][8];
cout << "...Enter the process ID...\n\n";
for (int i = 0; i < num; i++)
{
cout << "...Process " << i + 1 << "...\n";
cout << "Enter Process Id: ";
cin >> mat[i][0];
cout << "Enter Arrival Time: ";
cin >> mat[i][1];
cout << "Enter Burst Time: ";
cin >> mat[i][2];
cout << "Enter Priority: ";
cin >> mat[i][3];
mat[i][4] = mat[i][5] = mat[i][6] = 0;
mat[i][7] = 1;
cout << "\n";
}
arrangeArrival(num, mat);
completionTime(num, mat);
cout << "\n\nProcess ID\tArrival Time\tBurst Time\tPriority\tCompletion Time\tWaiting Time\
tTurnaround
Time\n";
for (int i = 0; i < num; i++)
cout << " " << mat[i][0] << "\t\t" << mat[i][1] << "\t\t" << mat[i][2] << "\t\t" << mat[i][3] << "\t\t"
<<
mat[i][4] << "\t\t" << mat[i][5] << "\t\t" << mat[i][6] << "\n";
int sum1 = 0;
int sum2 = 0;
for(int i = 0; i < num; i++)
{
sum1 += mat[i][5];
sum2 += mat[i][6];
}
cout << "\n\nAverage Waiting Time = " << (float)(sum1)/num << "\nAverage Turnaround Time = "
<<
(float)(sum2)/num;
}
Program 10
To write a program demonstrating pre-emptive Shortest Remaining Time First
(SRTF) scheduling.
#include<iostream>
using namespace std;
int main()
{
int a[10],b[10],x[10];
int waiting[10],turnaround[10],completion[10];
int i,j,smallest,count=0,time,n;
double avg=0,tt=0,end;
cout << "Enter number of Processes: ";
cin >> n;
for (int i = 0; i < n; i++)
{
cout << "...Process " << i + 1 << "...\n";
cout << "Enter Arrival Time: ";
cin >> a[i];
cout << "Enter Burst Time: ";
cin >> b[i];
}
for(i=0; i<n; i++)
x[i]=b[i];
b[9]=9999;
for(time=0; count!=n; time++)
{
smallest=9;
for(i=0; i<n; i++)
{
if(a[i]<=time && b[i]<b[smallest] && b[i]>0 )
smallest=i;
}
b[smallest]--;
if(b[smallest]==0)
{
count++;
end=time+1;
completion[smallest] = end;
waiting[smallest] = end - a[smallest] - x[smallest];
turnaround[smallest] = end - a[smallest];
}
}
cout << "\n\nProcess ID" << "\t" << "Arrival Time" << "\t" << "Burst Time" << "\t" << "Waiting
Time" <<
"\t" << "Turnaround Time" << "\t" << "Completion Time" << endl;
for(i=0; i<n; i++)
{
cout<<"p"<<i+1<<"\t\t"<<a[i]<<"\t\t"<<x[i]<<"\t\t"<<waiting[i]<<"\t\t"<<turnaround[i]<<"\t\
t"<<completion
[i]<<endl;
avg = avg + waiting[i];
tt = tt + turnaround[i];
}
cout<<"\n\nAverage Waiting time ="<<avg/n << "\n";
cout<<"Average Turnaround time ="<<tt/n<<endl;
}