Enrollment No:
LABORATORY MANUAL
OPERATING SYSTEM
Subject Code: 1330301
Integrated M.Sc. SEM. III (Computer Science), Year 2022-23
2023-24 (Odd Semester)
DEPARTMENT OF COMPUTER ENGINEERING
Gujarat Power Engineering and Research Institute, Mehsana
(A constituent college of Gujarat Technological University)
Near Toll Booth, Ahmedabad-Mehsana Express way, Village-Mewad
District: Mehsana - 382710 (Gujarat)
Contact No. +91-2762-285875/71
GUJARAT POWER ENGINEERING AND RESEARCH
INSTITUTE, MEHSANA
(A constituent college of Gujarat Technological University)
DEPARTMENT OF COMPUTER ENGINEERING
CERTIFICATE
This is to certify that Mr./Ms.________________________________
Enrollment No. ___________________ of semester _____________________has
satisfactorily completed the laboratory work in the course
_____________________________________ within the four walls of the Institute.
Date of submission:
Faculty In-charge Head of Department
Program 1
Write a program to implement First Come First Served (FCFS) job scheduling algorithm.
#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\n";
for(i=0;i<n;i++)
{
cout<<"P["<<i+1<<"]:";
cin>>bt[i];
}
wt[0]=0;
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";
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;
}
Program 2
Write a program to implement Shortest Job First (SJF) job scheduling
algorithm.
#include <bits/stdc++.h>
using namespace std;
struct Process {
int pid;
int bt;
int art;
};
Downloaded by mynameisis mylastnameisis (drjoshimail@protonmail.ch)lOMoARcPSD|20113481
void findWaitingTime(Process proc[], int n,
int wt[])
{
int rt[n];
for (int i = 0; i < n; i++)
rt[i] = proc[i].bt;
int complete = 0, t = 0, minm = INT_MAX;
int shortest = 0, finish_time;
bool check = false;
while (complete != n) {
for (int j = 0; j < n; j++) {
if ((proc[j].art <= t) &&
(rt[j] < minm) && rt[j] > 0) {
minm = rt[j];
shortest = j;
check = true;
}
}
if (check == false) {
t++;
continue;
}
\
rt[shortest]--;
minm = rt[shortest];
if (minm == 0)
minm = INT_MAX;
if (rt[shortest] == 0) {
complete++;
check = false;
finish_time = t + 1;
wt[shortest] = finish_time -
proc[shortest].bt -
proc[shortest].art;
if (wt[shortest] < 0)
wt[shortest] = 0;
}
t++;
}}
void findTurnAroundTime(Process proc[], int n,
int wt[], int tat[])
{
for (int i = 0; i < n; i++)
tat[i] = proc[i].bt + wt[i];
}
void findavgTime(Process proc[], int n)
{
int wt[n], tat[n], total_wt = 0,
total_tat = 0;
findWaitingTime(proc, n, wt);
findTurnAroundTime(proc, n, wt, tat);
cout << "Processes "
<< " Burst time "
<< " Waiting time "
<< " Turn around time\n";
for (int i = 0; i < n; i++) {
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
cout << " " << proc[i].pid << "\t\t"
<< proc[i].bt << "\t\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;
}
int main()
{
Process proc[] = { { 1, 6, 1 }, { 2, 8, 1 },
{ 3, 7, 2 }, { 4, 3, 3 } };
int n = sizeof(proc) / sizeof(proc[0]);
findavgTime(proc, n);
return 0;
}
Program 3
#include<iostream>
using namespace std;
void findWaitingTime(int processes[], int n,
int bt[], int wt[], int quantum)
{
int rem_bt[n];
for (int i = 0 ; i < n ; i++)
rem_bt[i] = bt[i];
while (1)
{
bool done = true;
for (int i = 0 ; i < n; i++)
{
if (rem_bt[i] > 0)
{
done = false
if (rem_bt[i] > quantum)
{
t += quantum;
rem_bt[i] -= quantum;
}
else
{
t = t + rem_bt[i];
wt[i] = t - bt[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 quantum)
{
int wt[n], tat[n], total_wt = 0, total_tat = 0;
findWaitingTime(processes, n, bt, wt, quantum);
findTurnAroundTime(processes, n, bt, wt, tat);
cout << "Processes "<< " Burst time "
<< " Waiting time " << " Turn around time\n";
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;
}
int main()
{
int processes[] = { 1, 2, 3};
int n = sizeof processes / sizeof processes[0];
int burst_time[] = {10, 5, 8};
int quantum = 2;
findavgTime(processes, n, burst_time, quantum);
return 0;
}
Program 4
Write a program to implement priority scheduling
algorithm.
#include<bits/stdc++.h>
using namespace std;
struct Process
{
int pid;
int bt;
int priority;
};
bool comparison(Process a, Process b)
{
return (a.priority > b.priority);
}
void findWaitingTime(Process proc[], int n,
int wt[])
{
wt[0] = 0;
for (int i = 1; i < n ; i++ )
wt[i] = proc[i-1].bt + wt[i-1] ;
}
void findTurnAroundTime( Process proc[], int n,
int wt[], int tat[])
{
for (int i = 0; i < n ; i++)
tat[i] = proc[i].bt + wt[i];
}
void findavgTime(Process proc[], int n)
{
int wt[n], tat[n], total_wt = 0, total_tat = 0;
findWaitingTime(proc, n, wt);
findTurnAroundTime(proc, n, wt, tat);
cout << "\nProcesses "<< " Burst time "
<< " Waiting time " << " Turn around time\n";
for (int i=0; i<n; i++)
{
total_wt = total_wt + wt[i];
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(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);
}
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;
}
Program 5
To write a program to create a child process using fork()
system call.
#include<sys/types.h>
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/wa
it.h>
#include<bits/st
dc++.h> using
namespace std;
int main()
{
int
ret,status;
long int
i=0;
while(i++
<5)
{
ret = fork();
if(ret<0)
{
perror("error in fork");
cout<<"the final value of i is
"<<i<<endl;
exit(1);
}
if(ret>0)
{
cout<<"I am in parent process context\n";
is"<<getppid()<<"and pid is "<<getpid()<<endl;
cout<<"in parent .. ppid
continue;
}
if(ret==0)
{
cout<<"I am in child process context\n";
cout<<"in parent .. ppid
is"<<getppid()<<"and pid is "<<getpid()<<endl; if(i==1 || i==2 ) exit(0);
if(i==3) exit(1); else
{
alarm(60);
while(1);
}
}
}
i
f
(
r
e
t
>
0
)
{
while(1){ ret =
waitpid(-1,&status,0);
if(ret>0)
{
if(WIFEXITED(status))
{
if(WEXITSTATUS(status) == 0)
{
printf("normal and successfull");
}
els
e
}
printf("normal, but not successfull");
}
{
else{ printf("abnormal (did not
succeed)");
}
}
if(ret<0)
{
e
x
i
t
(
0
)
;
}
}
}
return 0;
}
Program 6
To write a program demonstrating non-pre-emptive First Come First Serve job scheduling.
#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;
}