0% found this document useful (0 votes)
12 views

4 B - C - D - Program

The document describes four different CPU scheduling algorithms: shortest job first, priority scheduling, and round robin. It provides code samples and explanations for how each algorithm works, including sorting processes, calculating waiting times, turnaround times, and average metrics. For each algorithm, it gives sample input and output to demonstrate the behavior of the code.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

4 B - C - D - Program

The document describes four different CPU scheduling algorithms: shortest job first, priority scheduling, and round robin. It provides code samples and explanations for how each algorithm works, including sorting processes, calculating waiting times, turnaround times, and average metrics. For each algorithm, it gives sample input and output to demonstrate the behavior of the code.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

4 b) Shortest job first (preemptive)

#include<stdio.h>
#include<string.h>
int main()
{
// Variable and array declarations
int bt[20], at[10], n, i, j, temp, st[10], ft[10], wt[10], tat[10];
int totwt = 0, tottat = 0;
float awt, atat;
char p[10][10], t[10];

// User input for the number of processes


printf("Enter the number of processes:");
scanf("%d", &n);

// Input process details (name, arrival time, execution time)


for (i = 0; i < n; i++)
{
printf("Enter process name, arrival time & execution time:");
scanf("%s%d%d", p[i], &at[i], &bt[i]);
}

// Sort processes based on burst time using simple sorting algorithm


for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
{
if (bt[i] < bt[j])
{
// Swap arrival times, burst times, and process names
temp = at[i];
at[i] = at[j];
at[j] = temp;
temp = bt[i];
bt[i] = bt[j];
bt[j] = temp;
strcpy(t, p[i]);
strcpy(p[i], p[j]);
strcpy(p[j], t);
}
}

// Calculate scheduling metrics for each process


for (i = 0; i < n; i++)
{
if (i == 0)
st[i] = at[i];
else
st[i] = ft[i - 1];
wt[i] = st[i] - at[i];
ft[i] = st[i] + bt[i];
tat[i] = ft[i] - at[i];
totwt += wt[i];
tottat += tat[i];
}

// Calculate average waiting time and average turnaround time


awt = (float)totwt / n;
atat = (float)tottat / n;

// Print process details and metrics


printf("\nName\tArrival Time\tExecution Time\tWaiting Time\tTurnaround
Time");
for (i = 0; i < n; i++)
printf("\n%s\t%5d\t\t%5d\t\t%5d\t\t%5d", p[i], at[i], bt[i], wt[i], tat[i]);
printf("\nAverage waiting time: %f", awt);
printf("\nAverage turnaround time: %f", atat);

return 0;
}

INPUT:
Enter the number of process:3
Enter process name, arrival time& execution time:p1
2
5
Enter process name, arrival time& execution time:p2
5
2
Enter process name, arrival time& execution time:p3
0
9

OUTPUT:

Pame atrrivaltime executiontime waitingtime tattime


p2 5 2 0 2
p1 2 5 5 10
p3 0 9 12 21
Average waiting time is:5.666667Average turnaroundtime is:11.000000

4 c) PRIORITY:
AIM: To write a c program to simulate the CPU scheduling priority algorithm.
DESCRIPTION:
To calculate the average waiting time in the priority algorithm, sort the burst times
according to their priorities and then calculate the average waiting time of the
processes. The waiting time of each process is obtained by summing up the burst
times of all the previous processes.
PROGRAM:
#include<stdio.h>
Int main()
{
int p[20],bt[20],pri[20], wt[20],tat[20],i, k, n, temp; float wtavg,
tatavg;
clrscr();
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(i=0;i<n;i++)
for(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;
}
wtavg = wt[0] = 0;
tatavg = tat[0] = bt[0];
for(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("\nPROCESS\t\tPRIORITY\tBURST TIME\tWAITING TIME\
tTURNAROUND
TIME");
for(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);
}

INPUT
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
OUTPUT
PROCESS PRIORITY BURST TIME WAITING TIME TURNAROUND TIME
11101
42516
0 3 10 6 16
2 4 2 16 18
3 5 1 18 19
Average Waiting Time is --- 8.200000
Average Turnaround Time is --- 12.000000

4 D). ROUND ROBIN:


AIM: To simulate the CPU scheduling algorithm round-robin.
DESCRIPTION:
To aim is to calculate the average waiting time. There will be a time slice, each
process should e executed within that time-slice and if not it will go to the waiting
state so first check whether the burst time is less than the time-slice. If it is less than it
assign the waiting time to the sum of the total times. If it is greater than the burst-time
then subtract the time slot from the actual burst time and increment it by time-slot and
the loop continues until all the processes are completed.

#include<stdio.h>
int main()
{
int i,j,n,bu[10],wa[10],tat[10],t,ct[10],max;
float awt=0,att=0,temp=0;

printf("Enter the no of processes -- ");


scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter Burst Time for process %d -- ", i+1);
scanf("%d",&bu[i]);
ct[i]=bu[i];
}
printf("\nEnter the size of time slice -- ");
scanf("%d",&t);
max=bu[0];
for(i=1;i<n;i++)
if(max<bu[i])
max=bu[i];
for(j=0;j<(max/t)+1;j++)
for(i=0;i<n;i++)
if(bu[i]!=0)
if(bu[i]<=t) {
tat[i]=temp+bu[i];
temp=temp+bu[i];
bu[i]=0;
}
else {
bu[i]=bu[i]-t;
temp=temp+t;
}
for(i=0;i<n;i++){
wa[i]=tat[i]-
ct[i]; att+=tat[i];
awt+=wa[i];}
printf("\nThe Average Turnaround time is -- %f",att/n);
printf("\nThe Average Waiting time is -- %f ",awt/n);
printf("\n\tPROCESS\t BURST TIME \t WAITING TIME\tTURNAROUND TIME\
n");
for(i=0;i<n;i++)
printf("\t%d \t %d \t\t %d \t\t %d \n",i+1,ct[i],wa[i],tat[i]);
}

INPUT:
Enter the no of processes – 3
Enter Burst Time for process 1 – 24
Enter Burst Time for process 2 -- 3
Enter Burst Time for process 3 – 3
Enter the size of time slice – 3
OUTPUT:
PROCESS BURST TIME WAITING TIME TURNAROUNDTIME
1 24 6 30
2347
3 3 7 10
The Average Turnaround time is – 15.666667 The
Average Waiting time is ------------ 5.666667

You might also like