D
EPARTMENT OF COMPUTER SCIENCE AND
ENGINEERING
Lab Report
Course Code: CSE-3632
Course Title: Operating Systems Lab.
Student Name: MD ARIFUL ISLAM
Student ID: C191079
Semester: Autumn-2021
Section: 6BM
Student Contact No: 01521219552
Student Email: imariful18@[Link]
Student g-suite: c191079@[Link]
Course Instructor: Mr. Md Shafiul Alam Farhad
Assistant professor,
Department of CSE, CUET.
Experiment No: 01
SJF Non-Preemptive CPU Scheduling Algorithm:
DESCRIPTION: To calculate the average waiting time in the shortest job first
algorithm the sorting of the process based on their burst time in ascending order
then calculate the waiting time of each process as the sum of the bursting times
of all the process previous or before to that process.
Source Code:
#include <iostream>
using namespace std;
#define ll long long int
int main()
{
int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp;
float avg_wt,avg_tat;
cout<<"Enter number of process:";
cin>>n;
cout<<endl;
cout<<"Enter Burst Time:"<<endl;
for(i=0; i<n; i++)
{
cout<<"p"<<i+1<<":";
cin>>bt[i];
p[i]=i+1;
}
for(i=0; i<n; i++)
{
pos=i;
for(j=i+1; j<n; j++)
{
if(bt[j]<bt[pos])
pos=j;
}
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0;
for(i=1; i<n; i++)
{
wt[i]=0;
for(j=0; j<i; j++)
wt[i]=wt[i]+bt[j];
total=total+wt[i];
}
avg_wt=(float)total/n;
total=0;
cout<<endl;
cout<<"Process\t Burst Time \tWaiting Time\tTurnaround Time";
for(i=0; i<n; i++)
{
tat[i]=bt[i]+wt[i];
total+=tat[i];
cout<<"\nP["<<p[i]<<"]\t\t "<<bt[i]<<"\t\t "<<wt[i]<<"\t\t\t"<<tat[i];
}
avg_tat=(float)total/n;
cout<<"\n\nAverage Waiting Time= "<<avg_wt <<" milliseconds";
cout<<"\nAverage Turnaround Time= "<<avg_tat <<" milliseconds"<<endl;
}
Input & output:
Discussion: Shortest Job first has the advantage of having a minimum average
waiting time among all scheduling algorithms. It is a Greedy Algorithm. It may
cause starvation if shorter processes keep coming. This problem can be solved
using the concept of ageing. SJF can be used in specialized environments where
accurate estimates of running time are available.
Experiment No:02
Priority Non-Preemptive CPU Scheduling Algorithm:
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.
Source Code:
#include<stdio.h>
main()
{
int p[20],bt[20],pri[20], wt[20],tat[20],i, k, n, temp;
float wtavg,tatavg;
printf("Enter the number of processes: ");
scanf("%d",&n);
for(i=0; i<n; i++)
{
p[i] = i;
printf("Enter the Burst Time & Priority of Process %d : ",i);
scanf("%d%d",&bt[i], &pri[i]);
}
for(i=0; i<n; i++)
for(k=i+1; k<n; k++)
if(pri[i] > pri[k])
{
temp=p[i];
p[i]=p[k];
p[k]=temp;
temp=bt[i];
bt[i]=bt[k];
bt[k]=temp;
temp=pri[i];
pri[i]=pri[k];
pri[k]=temp;
}
wtavg = wt[0] = 0;
tatavg = tat[0] = bt[0];
for(i=1; i<n; i++)
{
wt[i] = wt[i-1] + bt[i-1];
tat[i] = tat[i-1] + bt[i];
wtavg = wtavg + wt[i];
tatavg = tatavg + tat[i];
}
printf("\nPROCESS\t\tPRIORITY\tBURST TIME\tWAITING TIME\
tTURNAROUNDTIME");
for(i=0; i<n; i++)
printf("\n%d \t\t %d \t\t %d \t\t %d \t\t %d ",p[i],pri[i],bt[i],wt[i],tat[i]);
printf("\nAverage Waiting Time is : %f",wtavg/n);
printf("\nAverage Turnaround Time is : %f",tatavg/n);
return 0;
}
Input & output:
Discussion: In the Non-Preemptive Priority scheduling, The Processes are
scheduled according to the priority number assigned to them. Once the process
gets scheduled, it will run till the completion. Generally, the lower the priority
number, the higher is the priority of the process. The people might get confused
with the priority numbers, hence in the GATE, there clearly mention which one is
the highest priority and which one is the lowest one.