CPU SCHEDULING ALGORITHMS
PROGRAM CODE
#include<stdlib.h>
#include<conio.h>
#include<stdio.h>
#include<iostream.h>
#define MAX 40
typedef struct process
{
int process_no,arr_time,burst_time,priority,execute;
};
int accept(process []);
void display(process [],int);
void fcfs(process[],int);
void sjf(process[],int);
void priority(process[],int);
void display_seq(process[],int);
void display_sjf(process[],int);
void main()
{
int ch,n;
process proc[MAX];
while(1)
{
clrscr();
cout<<"\n\t\tMAIN MENU\n"
<<"\n\[Link] the Process Attributes.\n"
<<"\n\[Link] Come First Serve(FCFS) Scheduling Algorithm.\n"
<<"\n\[Link] Job First(SJF) Scheduling Algorithm.\n"
<<"\n\[Link] Scheduling Algorithm.\n"
<<"\n\[Link].\n"
<<"\n\tEnter the option : ";
cin>>ch;
switch(ch)
{
case 1:clrscr();
n=accept(proc);
clrscr();
display(proc,n);
break;
case 2:clrscr();
cout<<"\n\t\t\tFIRST COME FIRST SERVE\n";
fcfs(proc,n);
break;
case 3:clrscr();
1
CPU SCHEDULING ALGORITHMS
cout<<"\n\t\t\tSHORTEST JOB FIRST\n";
sjf(proc,n);
break;
case 4:clrscr();
cout<<"\n\t\t\tPRIORITY\n";
priority(proc,n);
break;
case 5:cout<<"\n\tThanks For Using The System...";
getch();
exit(0);
default:cout<<"\n\tPlease Enter a valid option...";
}
getch();
}
}
int accept(process proc[MAX])
{
int n;
cout<<"\n\tEnter No. of Processes : ";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"\n\tProcess "<<i+1<<" : ";
proc[i].process_no=i+1;
cout<<"\n\n\tEnter the Arrival Time : ";
cin>>proc[i].arr_time;
cout<<"\n\t Enter the Burst Time : ";
cin>>proc[i].burst_time;
cout<<"\n\t Enter the Priority : ";
cin>>proc[i].priority;
proc[i].execute=0;
}
return(n);
}
void display(process proc[MAX],int n)
{
cout<<"\n\tProcess\tArrival Time\tBurst Time\tPriority";
for(int i=0;i<n;i++)
{
cout<<"\n\n";
cout<<"\t P"<<proc[i].process_no<<"\t "<<proc[i].arr_time
<<"\t\t "<<proc[i].burst_time<<"\t\t "<<proc[i].priority;
}
}
2
CPU SCHEDULING ALGORITHMS
void fcfs(process proc[MAX],int n)
{
process temp,pro[MAX];
for(int i=0;i<n;i++)
pro[i]=proc[i];
for(i=0;i<n;i++)
for(int j=0;j<n-1;j++)
{
if(pro[j].arr_time>pro[j+1].arr_time)
{
temp=pro[j];
pro[j]=pro[j+1];
pro[j+1]=temp;
}
}
display(pro,n);
display_seq(pro,n);
}
void sjf(process proc[MAX],int n)
{
process temp,pro[MAX];
for(int i=0;i<n;i++)
pro[i]=proc[i];
for(i=0;i<n;i++)
for(int j=0;j<n-1;j++)
{
if(pro[j].arr_time>pro[j+1].arr_time)
{
temp=pro[j];
pro[j]=pro[j+1];
pro[j+1]=temp;
}
}
display(pro,n);
display_sjf(pro,n);
}
void priority(process proc[MAX],int n)
{
process temp,pro[MAX],pr[MAX];
int sum=0,k=0,flag;
for(int i=0;i<n;i++)
pro[i]=proc[i];
for(i=0;i<n;i++)
for(int j=0;j<n-1;j++)
{
if(pro[j].arr_time>pro[j+1].arr_time)
{
temp=pro[j];
pro[j]=pro[j+1];
3
CPU SCHEDULING ALGORITHMS
pro[j+1]=temp;
}
}
for(i=0;i<n;i++)
{
flag=0;
if(i==0)
sum=pro[0].burst_time;
else
sum=sum+pr[i].burst_time;
for(j=0;j<n;j++)
{
if(pro[j].arr_time<=sum && pro[j].execute==0)
{
pr[k++]=pro[j];
pro[j].execute=1;
flag=1;
}
}
if(flag==0 && k<n)
{
pr[k++]=pro[i+1];
}
for(int l=i+1;l<k;l++)
for(int m=i+1;m<k-1;m++)
{
if(pr[m].priority>pr[m+1].priority)
{
temp=pr[m];
pr[m]=pr[m+1];
pr[m+1]=temp;
}
}
}
display(pr,n);
display_seq(pr,n);
}
void display_seq(process pro[MAX],int n)
{
float wait_time,sum,total=0;
cout<<"\n\n\tSequence of Execution Of Process : \n\n\t";
for(int i=0;i<n;i++)
for(int j=0;j<pro[i].burst_time;j++)
cout<<"P"<<pro[i].process_no<<" ";
for(i=1;i<n;i++)
{
sum=0;
for(j=0;j<i;j++)
sum=sum+pro[j].burst_time;
4
CPU SCHEDULING ALGORITHMS
sum=sum-pro[i].arr_time+1;
total=total+sum;
}
wait_time=total/n;
cout<<"\n\n\tAverage Waiting time = "<<wait_time<<" ms ";
}
void display_sjf(process pro[MAX],int n)
{
process temp;
int x[MAX],i,j,m=0,flag,k,b,y=9999,t,a[MAX];
float wait_time,total;
x[0]=pro[0].process_no;
b=pro[0].burst_time=pro[0].burst_time-1;
for(i=1;i<n;i++)
{
flag=0;
if(pro[i].burst_time<b && b!=0)
{
flag=1;
}
if(b!=0)
{
if(flag==1)
{
b=pro[i].burst_time=pro[i].burst_time-1;
x[i]=pro[i].process_no;
}
else if(flag==0)
{
x[i]=x[i-1];
for(k=0;k<n;k++)
if(pro[k].process_no==x[i])
b=pro[k].burst_time=pro[k].burst_time-1;
}
}
else
{
for(k=0;k<n;k++)
if(pro[k].process_no==x[i-1])
pro[k].execute=1;
for(j=0;j<=i;j++)
{
if(pro[j].burst_time<y && pro[j].execute==0)
{
y=pro[j].burst_time;
t=j;
}
}
x[i]=pro[t].process_no;
5
CPU SCHEDULING ALGORITHMS
b=pro[t].burst_time=pro[t].burst_time-1;
}
}
for(i=0;i<n;i++)
for(j=0;j<n-1;j++)
{
if(pro[j].burst_time>pro[j+1].burst_time)
{
temp=pro[j];
pro[j]=pro[j+1];
pro[j+1]=temp;
}
}
cout<<"\n\n\tSequence of Execution Of Process : \n\n\t";
for(i=0;i<n;i++)
{
cout<<"P"<<x[i]<<" ";
a[m++]=x[i];
}
for(i=0;i<n;i++)
{
for(j=0;j<pro[i].burst_time;j++)
{
cout<<"P"<<pro[i].process_no<<" ";
a[m++]=pro[i].process_no;
}
}
total=0;
int cnt=0;
for(i=1;i<m;i++)
{
if(a[i]!=a[i-1])
for(j=0;j<n;j++)
if(a[i]==pro[j].process_no)
{ cnt=0;
for(k=0;k<i;k++)
{
if(a[k]==a[i])
cnt++;
}
total=total+i-pro[j].arr_time+1-cnt;
}
}
wait_time=total/n;
cout<<"\n\n\tAverage Waiting time = "<<wait_time<<" ms ";
}
6
CPU SCHEDULING ALGORITHMS
OUTPUT
MAIN MENU
[Link] the Process Attributes.
[Link] Come First Serve(FCFS) Scheduling Algorithm.
[Link] Job First(SJF) Scheduling Algorithm.
[Link] Scheduling Algorithm.
[Link].
Enter the option : 1
Enter No. of Processes : 5
Process 1 :
Enter the Arrival Time : 1
Enter the Burst Time : 4
Enter the Priority : 5
Process 2 :
Enter the Arrival Time : 5
Enter the Burst Time : 2
Enter the Priority : 2
7
CPU SCHEDULING ALGORITHMS
Process 3 :
Enter the Arrival Time : 2
Enter the Burst Time : 2
Enter the Priority : 4
Process 4 :
Enter the Arrival Time : 4
Enter the Burst Time : 3
Enter the Priority : 1
Process 5 :
Enter the Arrival Time : 3
Enter the Burst Time : 4
Enter the Priority : 3
Process Arrival Time Burst Time Priority
P1 1 4 5
P2 5 2 2
P3 2 2 4
P4 4 3 1
P5 3 4 3
8
CPU SCHEDULING ALGORITHMS
MAIN MENU
[Link] the Process Attributes.
[Link] Come First Serve(FCFS) Scheduling Algorithm.
[Link] Job First(SJF) Scheduling Algorithm.
[Link] Scheduling Algorithm.
[Link].
Enter the option : 2
FIRST COME FIRST SERVE
Process Arrival Time Burst Time Priority
P1 1 4 5
P3 2 2 4
P5 3 4 3
P4 4 3 1
P2 5 2 2
Sequence of Execution Of Process :
P1 P1 P1 P1 P3 P3 P5 P5 P5 P5 P4 P4 P4 P2 P2
Average Waiting time = 4.6 ms
9
CPU SCHEDULING ALGORITHMS
MAIN MENU
[Link] the Process Attributes.
[Link] Come First Serve(FCFS) Scheduling Algorithm.
[Link] Job First(SJF) Scheduling Algorithm.
[Link] Scheduling Algorithm.
[Link].
Enter the option : 3
SHORTEST JOB FIRST
Process Arrival Time Burst Time Priority
P1 1 4 5
P3 2 2 4
P5 3 4 3
P4 4 3 1
P2 5 2 2
Sequence of Execution Of Process :
P1 P3 P3 P1 P1 P1 P2 P2 P4 P4 P4 P5 P5 P5 P5
Average Waiting time = 3.6 ms
10
CPU SCHEDULING ALGORITHMS
MAIN MENU
[Link] the Process Attributes.
[Link] Come First Serve(FCFS) Scheduling Algorithm.
[Link] Job First(SJF) Scheduling Algorithm.
[Link] Scheduling Algorithm.
[Link].
Enter the option : 4
PRIORITY
Process Arrival Time Burst Time Priority
P1 1 4 5
P4 4 3 1
P2 5 2 2
P5 3 4 3
P3 2 2 4
Sequence of Execution Of Process :
P1 P1 P1 P1 P4 P4 P4 P2 P2 P5 P5 P5 P5 P3 P3
Average Waiting time = 4.6 ms
11
CPU SCHEDULING ALGORITHMS
MAIN MENU
[Link] the Process Attributes.
[Link] Come First Serve(FCFS) Scheduling Algorithm.
[Link] Job First(SJF) Scheduling Algorithm.
[Link] Scheduling Algorithm.
[Link].
Enter the option : 5
Thanks For Using The System...
12