5a. Write A C Program To Simulate FCFS CPU Scheduling ALGORITHM
5a. Write A C Program To Simulate FCFS CPU Scheduling ALGORITHM
Program Description:
In this algorithm the processes which arrive first is given the CPU after finishing its request only it will
allow CPU to execute other process.
Example:
Algorithm:
Step 3: For each process in the ready queue, assign the process id and accept the CPU burst time
Step 4: Set the waiting of the first process as ‘0’ and its burst time as its turnaround time
(i) Waiting time for process(n)= Waiting time of process (n-1) + Burst time of process(n-1)
(ii) Turnaround time for Process(n)= Waiting time of Process(n)+ Burst time for process(n)
Step 6: Calculate
#include<stdio.h>
int main()
{
int bt[20], wt[20], tat[20], n;
float wtavg, tatavg;
printf("\nEnter the number of processes -- ");
scanf("%d", &n);
for(int i=0;i<n;i++)
{
printf("\nEnter Burst Time for Process %d -- ", i);
scanf("%d", &bt[i]);
}
wt[0] = wtavg = 0;
tat[0] = tatavg = bt[0];
for(int 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("\t PROCESS \tBURST TIME \t WAITING TIME\t TURNAROUND TIME\n");
for(int i=0;i<n;i++)
printf("\n\t P%d \t\t %d \t\t %d \t\t %d", i, bt[i], wt[i], tat[i]);
printf("\nAverage Waiting Time -- %f", wtavg/n);
printf("\nAverage Turnaround Time -- %f", tatavg/n);
return 0;
}
OUTPUT
PROCESS BURST TIME WAITING TIME TURNAROUND TIME
P0 24 0 24
P1 3 24 27
P2 3 27 30
Average Waiting Time-- 17.000000
Average Turnaround Time -- 27.000000
5b. Write a C program to simulate SJF CPU scheduling algorithm
Program Description:
SJF works on the process with shortest burst time first. That is schedules the processes according to the
burst time
Example: Consider the below processes available in the ready queue for execution with arrival time as 0
and given burst times. The process 4 will be picked up first as it has shortest burst time, then p2,
followed by p3 and at last p1.
SJF Algorithm
Step 1: Start the program
Step 2: Accept the number of processes in the ready queue
Step 3: For each process in the ready queue, assign the process id and accept the CPU burst time
Step 4: Start the ready queue according the shortest burst time by sorting according to lowest to
highest burst time.
Step 5: Set the waiting time of the first process as ‘0’ and its turnaround time as its burst time.
Step 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)
Step 6: Calculate
(c) Average Waiting time = Total Waiting Time / Number of process
(d) Average Turnaround time = Total Turnaround Time / Number of process
Step 7: Stop the program
Program Code:
#include<stdio.h>
int main()
{
int p[20], bt[20], wt[20], tat[20], n, temp;
float wtavg=0, tatavg=0;
printf("\nEnter the number of processes -- ");
scanf("%d", &n);
for(int i=0;i<n;i++)
{
p[i]=i;
printf("Enter Burst Time for Process %d -- ", i);
scanf("%d", &bt[i]);
}
for(int i=0;i<n;i++)
for(int k=i+1;k<n;k++)
if(bt[i]>bt[k])
{
temp=bt[i];
bt[i]=bt[k];
bt[k]=temp;
temp=p[i];
p[i]=p[k];
p[k]=temp;
}
wt[0] = 0;
tat[0] = bt[0];
for(int 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("\n\t PROCESS \tBURST TIME \t WAITING TIME\t TURNAROUND TIME\n");
for(int i=0;i<n;i++)
printf("\n\t P%d \t\t %d \t\t %d \t\t %d", p[i], bt[i], wt[i], tat[i]);
printf("\nAverage Waiting Time -- %f", wtavg/n);
printf("\nAverage Turnaround Time -- %f", tatavg/n);
return 0;}
OUTPUT:
Enter the number of processes -- 4
Enter Burst Time for Process 0 -- 6
Enter Burst Time for Process 1 -- 8
Enter Burst Time for Process 2 -- 7
Enter Burst Time for Process 3 -- 3
OUTPUT
PROCESS BURST TIME WAITING TIME TURNAROUND TIME
P3 3 0 3
P0 6 3 9
P2 7 9 16
P1 8 16 24
Average Waiting Time -- 7.000000
Average Turnaround Time -- 13.000000
5c. Write a C program to simulate Priority CPU scheduling algorithm
Program Description:
Each process is assigned a priority. Process with the highest priority is to be executed first and so on.
Processes with the same priority are executed on first come first served basis.
Example:
Algorithm:
Program Code:
#include<stdio.h>
int main()
{
int p[20],bt[20],pri[20], wt[20],tat[20], n, temp;
float wtavg=0, tatavg=0;
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(int i=0;i<n;i++)
for(int 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;
}
wt[0] = 0;
tat[0] = bt[0];
for(int 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];
return 0;
}
printf("\nPROCESS\t\tPRIORITY\tBURST TIME\tWAITING TIME\tTURNAROUND TIME");
for(int 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);
}
OUTPUT
Enter the number of processes -- 5
Enter the Burst Time & Priority of Process 0 --- 10 3
Enter the Burst Time & Priority of Process 1 --- 1 1
Enter the Burst Time & Priority of Process 2 --- 2 4
Enter the Burst Time & Priority of Process 3 --- 1 5
Enter the Burst Time & Priority of Process 4 --- 5 2
Program Description:
In Round Robin Scheduling, CPU is assigned to the process on the basis of FCFS for a fixed amount of
time. This fixed amount of time is called as time quantum or time slice. After the time quantum expires,
the running process is preempted and sent to the ready queue. Then, the processor is assigned to the next
arrived process. It is always preemptive in nature.
Algorithm:
(i) waiting time (i) = waiting time (i) + remaining burst time(i);
Step 7: Calculate
(g) Average Waiting time = Total Waiting Time / Number of process
(h) Average Turnaround time = Total Turnaround Time / Number of process
Program:
#include<stdio.h>
int main()
{
int wt[10],tat[10],rbt[10],bt[30],ts,n,i,x=0,tot=0;
float totwt=0, totat=0;
printf("Enter the no of processes:");
scanf("%d",&n);
printf("Enter the time quantum:");
scanf("%d",&ts);
for(i=0;i<n;i++)
{
printf("enter burst time for process %d:",i+1);
scanf("%d",&bt[i]);
rbt[i]=bt[i];
}
for(i=0;i<n;i++)
tot=tot+bt[i];
while(x!=tot)
{
for(i=0;i<n;i++)
{
if(rbt[i]>ts)
{
x=x+ts;
rbt[i]=rbt[i]-ts;
}
else
if((rbt[i]<=ts)&&rbt[i]!=0)
{
x=x+rbt[i];
wt[i] = x - bt[i];
tat[i] = bt[i] + wt[i];
rbt[i]=0;}
}
}
printf("Process_no\t Burst time\tWait time\tTurn around time\n");
for ( i = 0; i < n ; i++)
printf("%d\t%d\t%d\t%d\n",i+1,bt[i],wt[i], tat[i]);
for (int i = 0; i < n ; i++)
totat=totat+tat[i];
for ( i = 0; i < n ; i++)
totwt = totwt + wt[i];
printf("average waiting time=%f",totwt/n);
printf("\naverage waiting time=%f",totat/n);
return 0;
}