Program 5a):
AIM: Write a C program to implement the various process
scheduling mechanisms such as FCFS scheduling.
#include<stdio.h>
void main()
{
int i,n,sum,wt,tat,twt,ttat;
int t[10];
float awt,atat;
clrscr();
printf("Enter number of
processors:\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter the Burst Time of the
process %d",i+1);
scanf("\n %d",&t[i]);
}
printf("\n\n FIRST COME FIRST SERVE
SCHEDULING ALGORITHM \n");
printf("\n Process ID \t Waiting Time \t
Turn Around Time \n");
printf("1 \t\t 0 \t\t %d \n",t[0]);
sum=0;
twt=0;
ttat=t[0];
for(i=1;i<n;i++)
{
sum+=t[i-1];
wt=sum;
tat=sum+t[i];
twt=twt+wt;
ttat=ttat+tat;
printf("\n %d \t\t %d \t\t
%d",i+1,wt,tat);
printf("\n\n");
}
awt=(float)twt/n;
atat=(float)ttat/n;
printf("\n Average Waiting Time
%4.2f",awt);
printf("\n Average Turnaround Time
%4.2f",atat);
getch();
}
Program 5b):
AIM: Write a C program to implement the various process
scheduling mechanisms such as SJF Scheduling .
#include<stdio.h>
void main()
{
int i,j,k,n,sum,wt[10],tt[10],twt,ttat;
int t[10],p[10];
float awt,atat;
clrscr();
printf("Enter number of process\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter the Burst Time of
Process %d",i);
scanf("\n %d",&t[i]);
}
for(i=0;i<n;i++)
p[i]=i;
for(i=0;i<n;i++)
{
for(k=i+1;k<n;k++)
{
if(t[i]>t[k])
{
int temp;
temp=t[i];
t[i]=t[k];
t[k]=temp;
temp=p[i];
p[i]=p[k];
p[k]=temp;
}
}
printf("\n\n SHORTEST JOB FIRST
SCHEDULING ALGORITHM");
printf("\n PROCESS ID \t BURST TIME
\t WAITING TIME \t TURNAROUND TIME \n\n");
wt[0]=0;
for(i=0;i<n;i++)
{
sum=0;
for(k=0;k<i;k++)
{
wt[i]=sum+t[k];
sum=wt[i];
}
}
for(i=0;i<n;i++)
{
tt[i]=t[i]+wt[i];
}
for(i=0;i<n;i++)
{
printf("%5d \t\t5%d \t\t %5d \t\t
%5d \n\n",p[i],t[i],wt[i],tt[i]);
}
twt=0;
ttat=t[0];
for(i=1;i<n;i++)
{
twt=twt+wt[i];
ttat=ttat+tt[i];
}
awt=(float)twt/n;
atat=(float)ttat/n;
printf("\n AVERAGE WAITING TIME
%4.2f",awt);
printf("\n AVERAGE TURN AROUND TIME
%4.2f",atat);
getch();
}
}
Program
5c):
AIM: Write a C program to
implement the various process scheduling mechanisms such as Round Robin
Scheduling.
#include<stdio.h>
#include<conio.h>
void main()
{
int
ts,pid[10],need[10],wt[10],tat[10],i,j,n,n1;
int bt[10],flag[10],ttat=0,twt=0;
float awt,atat;
printf("\t\t ROUND ROBIN SCHEDULING
\n");
printf("Enter the number of Processors
\n");
scanf("%d",&n);
n1=n;
printf("\n Enter the Timeslice \n");
scanf("%d",&ts);
for(i=1;i<=n;i++)
{
printf("\n Enter the process ID
%d",i);
scanf("%d",&pid[i]);
printf("\n Enter the Burst Time for the
process");
scanf("%d",&bt[i]);
need[i]=bt[i];
}
for(i=1;i<=n;i++)
{
flag[i]=1;
wt[i]=0;
}
while(n!=0)
{
for(i=1;i<=n;i++)
{
if(need[i]>=ts)
{
for(j=1;j<=n;j++)
{
if((i!=j)&&(flag[i]==1)&&(need[j]!=0))
wt[j]+=ts;
}
need[i]-=ts;
if(need[i]==0)
{
flag[i]=0;
n--;
}
}
else
{
for(j=1;j<=n;j++)
{
if((i!=j)&&(flag[i]==1)&&(need[j]!=0))
wt[j]+=need[i];
}
need[i]=0;
n--;
flag[i]=0;
}
}
}
for(i=1;i<=n1;i++)
{
tat[i]=wt[i]+bt[i];
twt=twt+wt[i];
ttat=ttat+tat[i];
}
awt=(float)twt/n1;
atat=(float)ttat/n1;
printf("\n\n
ROUND ROBIN SCHEDULING ALGORITHM \n\n");
printf("\n\n
Process \t Process ID \t BurstTime \t
Waiting Time \t TurnaroundTime \n ");
for(i=1;i<=n1;i++)
{
printf("\n %5d \t %5d \t\t %5d \t\t %5d
\t\t %5d \n", i,pid[i],bt[i],wt[i],tat[i]);
}
printf("\n
The average Waiting Time=4.2f",awt);
printf("\n
The average Turn around Time=4.2f",atat);
getch();
}
program 5d):
AIM: Write a C program to implement the various process
scheduling mechanisms such as Priority
Scheduling.
#include
<stdio.h>
#include
<conio.h>
void main()
{
int
i,j,n,tat[10],wt[10],bt[10],pid[10],pr[10],t,twt=0,ttat=0;
float awt,atat;
clrscr();
printf("\n-----------PRIORITY
SCHEDULING--------------\n");
printf("Enter the No of Process:
");
scanf("%d", &n);
for (i=0;i<n;i++)
{
pid[i] = i;
printf("Enter
the Burst time of Pid %d : ",i);
scanf("%d",&bt[i]);
printf("Enter
the Priority of Pid %d : ",i);
scanf
("%d",&pr[i]);
}
// Sorting start
for (i=0;i<n;i++)
for(j=i+1;j<n;j++)
{
if (pr[i]
> pr[j] )
{
t
= pr[i];
pr[i]
= pr[j];
pr[j]
= t;
t
= bt[i];
bt[i]
= bt[j];
bt[j]
= t;
t
= pid[i];
pid[i]
= pid[j];
pid[j]
= t;
}
}
//Sorting finished
tat[0] = bt[0];
wt[0] = 0;
for (i=1;i<n;i++)
{
wt[i] = wt[i-1] + bt[i-1];
tat[i] = wt[i] + bt[i];
}
printf("\n---------------------------------------------------------------\n");
printf("Pid\t Priority\tBurst
time\t WaitingTime\tTurnArroundTime\n");
printf("\n--------------------------------------------------------------\n");
for(i=0;i<n;i++)
{
printf("\n%d\t\t%d\t%d\t\t%d\t\t%d",pid[i],pr[i],bt[i],wt[i],tat[i]);
}
for(i=0;i<n;i++)
{
ttat = ttat+tat[i];
twt = twt + wt[i];
}
awt = (float)twt / n;
atat = (float)ttat / n;
printf("\n\nAvg.Waiting Time:
%f\nAvg.Turn Around Time: %f\n",awt,atat);
getch();
}
No comments:
Post a Comment