Ex - No:7 (A) Date: Aim:: Cpu Scheduling Algorithms
Ex - No:7 (A) Date: Aim:: Cpu Scheduling Algorithms
AIM:
To write a C program to perform SJF scheduling algorithm.
ALGORITHM:
Step 1: Start the program.
Step 2: Read the number of requests.
Step 3: Input the processes along with their burst time (bt).
Step 4: Sort all the processes in increasing order according to burst time.( (i.e. the job
having least burst time is executed first).
Step 5: Print the order in which process gets executed.
Step 6: Find waiting time (wt) for all processes.
Step 7: As first process that comes need not to wait so waiting time for process 1 will be 0
i.e. wt[0] = 0.
Step 8: Find waiting time for all other processes i.e. for
process i -> wt[i] = bt[i-1] + wt[i-1] .
Step 9: Find turnaround time = waiting_time + burst_time for all processes.
Step 10: Find average waiting time = total_waiting_time / no_of_processes.
Step 11: Find average turnaround time = total_turn_around_time / no_of_processes.
Step 12: The gantt chart is drawn representing the process execution.
Step 13: Stop the process.
Program:
#include<stdio.h>
int main()
{
int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp;
float avg_wt,avg_tat;
printf("Enter number of process:");
scanf("%d",&n);
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]+=bt[j];
total+=wt[i];
}
avg_wt=(float)total/n;
total=0;
avg_tat=(float)total/n;
printf("\n\nAverage Waiting Time=%f",avg_wt);
printf("\nAverage Turnaround Time=%f\n",avg_tat);
}
OUTPUT:
Gantt chart
0 3 6 30
RESULT: Thus the C program to perform SJF scheduling algorithm has been executed successfully.
PREPARATION 30
LAB PERFORMANCE 30
REPORT 40
TOTAL 100
INITIAL OF THE FACULTY
Ex.No :7(B) IMPLEMENTATION OF FCFS SCHEDULING ALGORITHM
DATE:
AIM:
To write a C program to perform FCFS Scheduling algorithm.
ALGORITHM:
#include<stdio.h>
int main()
{
int n,bt[20],wt[20],tat[20],avwt=0,avtat=0,i,j;
printf("Enter total number of processes(maximum 20):");
scanf("%d",&n);
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
}
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
avwt+=wt[i];
avtat+=tat[i];
printf("\nP[%d]\t\t%d\t\t%d\t\t%d",i+1,bt[i],wt[i],tat[i]);
}
avwt/=i;
avtat/=i;
printf("\n\nAverage Waiting Time:%d",avwt);
printf("\nAverage Turnaround Time:%d",avtat);
return 0;
}
OUTPUT:
Gantt chart
0 24 27 30
RESULT:
Thus the C program to perform FCFS scheduling algorithm has been executed successfully.
PREPARATION 30
LAB PERFORMANCE 30
REPORT 40
TOTAL 100
AIM:
ALGORITHM:
Step 1: Start the program.
Step 2: Read the number of requests.
Step 3: Input the processes along with their burst time (bt) and priority.
Step 4: Sort the processes, burst time and priority according to the priority.
Step 5: Print the order in which process gets executed.
Step 6: Find waiting time (wt) for all processes.
Step 7: As first process that comes need not to wait so waiting time for process 1 will be 0
i.e. wt[0] = 0.
Step 8: Find waiting time for all other processes i.e. for
process i -> wt[i] = bt[i-1] + wt[i-1] .
Step 9: Find turnaround time = waiting_time + burst_time for all processes.
Step 10: Find average waiting time = total_waiting_time / no_of_processes.
Step 11: Find average turnaround time = total_turn_around_time / no_of_processes.
Step 12: The gantt chart is drawn representing the process execution.
Step 13: Stop the process.
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int x,n,p[10],pp[10],pt[10],w[10],t[10],awt,atat,i;
printf("Enter the number of process : ");
scanf("%d",&n);
printf("\n Enter process : time priorities \n");
for(i=0;i<n;i++)
{
printf("\nProcess no %d : ",i+1);
scanf("%d %d",&pt[i],&pp[i]);
p[i]=i+1;
}
for(i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++)
{
if(pp[i]<pp[j])
{
x=pp[i];
pp[i]=pp[j];
pp[j]=x;
x=pt[i];
pt[i]=pt[j];
pt[j]=x;
x=p[i];
p[i]=p[j];
p[j]=x;
}
}
}
w[0]=0;
awt=0;
t[0]=pt[0];
atat=t[0];
for(i=1;i<n;i++)
{
w[i]=t[i-1];
awt+=w[i];
t[i]=w[i]+pt[i];
atat+=t[i];
}
printf("\n\n Job \t Burst Time \t Wait Time \t Turn Around Time Priority \n");
for(i=0;i<n;i++)
printf("\n %d \t\t %d \t\t %d \t\t %d \t\t %d \n",p[i],pt[i],w[i],t[i],pp[i]);
awt/=n;
atat/=n;
printf("\n Average Wait Time : %d \n",awt);
printf("\n Average Turn Around Time : %d \n",atat);
getch();
}
OUTPUT:
Gantt chart
P4 P3 P2 P1
0 6 11 15 18
RESULT: Thus the C program to perform PRIORITY scheduling algorithm has been executed
Successfully
PREPARATION 30
LAB PERFORMANCE 30
REPORT 40
TOTAL 100
AIM:
ALGORITHM:
#include<stdio.h>
#include<conio.h>
void main()
{
printf("\n Process No \t\t Burst Time \t\t TAT \t\t Waiting Time ");
for(sum=0, i = 0; y!=0; )
{
if(temp[i] <= quant && temp[i] > 0)
{
sum = sum + temp[i];
temp[i] = 0;
count=1;
}
else if(temp[i] > 0)
{
temp[i] = temp[i] - quant;
sum = sum + quant;
}
if(temp[i]==0 && count==1)
{
y--;
printf("\nProcess No[%d] \t\t %d\t\t\t\t %d\t\t\t %d", i+1, bt[i], sum-at[i], sum-at[i]-bt[i]);
wt = wt+sum-at[i]-bt[i];
tat = tat+sum-at[i];
count =0;
}
if(i==NOP-1)
{
i=0;
}
else if(at[i+1]<=sum)
{
i++;
}
else
{
i=0;
}
}
avg_wt = wt * 1.0/NOP;
avg_tat = tat * 1.0/NOP;
printf("\n Average Turn Around Time: \t%f", avg_wt);
printf("\n Average Waiting Time: \t%f", avg_tat);
getch();
}
Gantt chart:
P1 P2 P3 P4 P1 P3 P4
0 6 11 17 23 25 29 34
OUTPUT:
RESULT: Thus the C program to perform ROUND ROBIN scheduling algorithm has been executed
Successfully.
PREPARATION 30
LAB PERFORMANCE 30
REPORT 40
TOTAL 100