CPU Scheduling Algorithms Experiment 4
CPU Scheduling Algorithms Experiment 4
a) FCFS
b) SJF
c) Priority
Aim: Write a C program to implement the various process scheduling mechanisms such as FCFS
scheduling.
Algorithm:
total+=wt[i];
}
avg_wt=(float)total/n; //average waiting time
total=0;
printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i]; //calculate turnaround time
total+=tat[i];
printf("\np%d\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
}
Algorithm:
1: Start the process
2: Accept the number of processes in the ready Queue
3: For each process in the ready Q, assign the process id and accept the CPU burst time 4:
Start the Ready Q according the shortest Burst time by sorting according to lowest to
highest burst time.
5: Set the waiting time of the first process as ‘0’ and its turnaround time as its burst time.
6: For each process in the ready queue, calculate
(a) Waiting time for process(n)= waiting time of process (n-1) + Burst time of process(n-1)
(b) Turn around time for Process(n)= waiting time of Process(n)+ Burst time for process(n)
7: Calculate
(c) Average waiting time = Total waiting Time / Number of process
• Average Turnaround time = Total Turnaround Time / Number of process
8: 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=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0; //waiting time for first process will be zero
total+=wt[i];
}
Output:
c) Priority
Aim: Write a C program to implement the various process scheduling mechanisms such as
Priority Scheduling.
Algorithm:
1: Start the process
2: Accept the number of processes in the ready Queue
3: For each process in the ready Q, assign the process id and accept the CPU burst time 4: Sort
the ready queue according to the priority number.
5: Set the waiting of the first process as ‘0’ and its burst time as its turn around time
6: For each process in the Ready Q calculate
(e) Waiting time for process(n)= waiting time of process (n-1) + Burst time of process(n-1)
(f) Turn around time for Process(n)= waiting time of Process(n)+ Burst time for process(n)
7: Calculate
(g) Average waiting time = Total waiting Time / Number of process
(h) Average Turnaround time = Total Turnaround Time / Number of process Step 8: Stop the
process
Program:
#include<stdio.h>
int main()
{
int bt[20],p[20],wt[20],tat[20],pri[20],i,j,k,n,total=0,pos,temp;
float avg_wt,avg_tat;
printf("Enter number of process:");
scanf("%d",&n);
temp=bt[i];
bt[i]=bt[k];
bt[k]=temp;
temp=pri[i];
pri[i]=pri[k];
pri[k]=temp;
}
wt[0]=0; //waiting time for first process will be zero
total+=wt[i];
}
avg_wt=(float)total/n; //average waiting time
total=0;