0% found this document useful (0 votes)
43 views4 pages

CPU Scheduling Algorithms

The document contains code for implementing three CPU scheduling algorithms: 1) First Come First Serve (FCFS) which processes tasks in the order of arrival without preemption. 2) Shortest Job First (SJF) which selects the task with the shortest burst time to execute next. 3) Round Robin (RR) which allows preemption and allocates a time quantum to each task in a cyclic manner. The codes calculate waiting time, turnaround time and average waiting/turnaround time for tasks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views4 pages

CPU Scheduling Algorithms

The document contains code for implementing three CPU scheduling algorithms: 1) First Come First Serve (FCFS) which processes tasks in the order of arrival without preemption. 2) Shortest Job First (SJF) which selects the task with the shortest burst time to execute next. 3) Round Robin (RR) which allows preemption and allocates a time quantum to each task in a cyclic manner. The codes calculate waiting time, turnaround time and average waiting/turnaround time for tasks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

(1) FCFS

#include <stdio.h>

int main()

{
int n, bt[30], wait_t[30], turn_ar_t[30], av_wt_t = 0, avturn_ar_t = 0, i, j;
printf("Please enter the total number of processes(maximum 30):");
scanf("%d", &n);

printf("\nEnter The Process Burst Timen");


for (i = 0; i < n; i++)
{
printf("P[%d]:", i + 1);
scanf("%d", &bt[i]);
}

wait_t[0] = 0;

for (i = 1; i < n; i++)


{
wait_t[i] = 0;
for (j = 0; j < i; j++)
wait_t[i] += bt[j];
}

printf("\nProcess\t\tBurst Time\tWaiting Time\t\t\tTurnaround Time");

for (i = 0; i < n; i++)


{
turn_ar_t[i] = bt[i] + wait_t[i];
av_wt_t += wait_t[i];
avturn_ar_t += turn_ar_t[i];
printf("\nP[%d]\t\t%d\t\t\t%d\t\t\t\t%d", i + 1, bt[i], wait_t[i], turn_ar_t[i]);
}

av_wt_t /= i;
avturn_ar_t /= i;
printf("\nAverage Waiting Time:%d", av_wt_t);
printf("\nAverage Turnaround Time:%d", avturn_ar_t);

return 0;
}
(2) SJF

#include <stdio.h>
int main()
{
int bt[20], p[20], wt[20], tat[20], i, j, n, total = 0, totalT = 0, pos, temp;
float avg_wt, avg_tat;
printf("Enter number of process:");
scanf("%d", &n);
printf("\nEnter Burst Time:\n");
for (i = 0; i < n; i++)
{
printf("p%d:", i + 1);
scanf("%d", &bt[i]);
p[i] = i + 1;
}
for (i = 0; i < n; i++)
{
pos = i;
for (j = i + 1; j < n; j++)
{
if (bt[j] < bt[pos])
pos = j;
}
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;
printf("\nProcess\t Burst Time \tWaiting Time\t\tTurnaround Time");
for (i = 0; i < n; i++)
{
tat[i] = bt[i] + wt[i];
totalT += tat[i];
printf("\np%d\t\t %d\t\t %d\t\t\t%d", p[i], bt[i], wt[i], tat[i]);
}
avg_tat = (float)totalT / n;
printf("\n\nAverage Waiting Time=%f", avg_wt);
printf("\nAverage Turnaround Time=%f", avg_tat);
}
(3) ROUNDROBIN

#include <stdio.h>
int main()
{
int cnt, j, n, t, remain, flag = 0, tq;
int wt = 0, tat = 0, at[10], bt[10], rt[10];
printf("Enter Total Process:\t ");
scanf("%d", &n);
remain = n;
for (cnt = 0; cnt < n; cnt++)
{
printf("Enter Arrival Time and Burst Time for Process Process Number %d :", cnt + 1);
scanf("%d", &at[cnt]);
scanf("%d", &bt[cnt]);
rt[cnt] = bt[cnt];
}
printf("Enter Time Quantum:\t");
scanf("%d", &tq);
printf("\n\nProcess\t|Turnaround Time|Waiting Time\n\n");
for (t = 0, cnt = 0; remain != 0;)
{
if (rt[cnt] <= tq && rt[cnt] > 0)
{
t += rt[cnt];
rt[cnt] = 0;
flag = 1;
}
else if (rt[cnt] > 0)
{
rt[cnt] -= tq;
t += tq;
}
if (rt[cnt] == 0 && flag == 1)
{
remain--;
printf("P[%d]\t|\t%d\t|\t%d\n", cnt + 1, t - at[cnt], t - at[cnt] - bt[cnt]);
wt += t - at[cnt] - bt[cnt];
tat += t - at[cnt];
flag = 0;
}
if (cnt == n - 1)
cnt = 0;
else if (at[cnt + 1] <= t)
cnt++;
else
cnt = 0;
}
printf("\nAverage Waiting Time= %f\n", wt * 1.0 / n);
printf("Avg Turnaround Time = %f", tat * 1.0 / n);
return 0;
}

You might also like