0% found this document useful (0 votes)
8 views14 pages

Program

The document contains multiple C programs that implement different CPU scheduling algorithms: First-Come-First-Serve (FCFS), Shortest Job First (SJF), Round Robin, and Priority Scheduling. Each program prompts the user for process burst times and, in some cases, priorities, then calculates and displays average waiting and turnaround times along with Gantt charts. The programs utilize structures to manage process data and perform sorting and calculations to simulate the scheduling behavior.

Uploaded by

Jenifer M
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)
8 views14 pages

Program

The document contains multiple C programs that implement different CPU scheduling algorithms: First-Come-First-Serve (FCFS), Shortest Job First (SJF), Round Robin, and Priority Scheduling. Each program prompts the user for process burst times and, in some cases, priorities, then calculates and displays average waiting and turnaround times along with Gantt charts. The programs utilize structures to manage process data and perform sorting and calculations to simulate the scheduling behavior.

Uploaded by

Jenifer M
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/ 14

3115212050 40

Program:
/* FCFS Scheduling- fcfs.c */
#include <stdio.h>
struct process
{
int pid;
int btime;
int wtime;
int ttime;
} p[10];

main()
{
int i,j,k,n,ttur,twat;
float awat,atur;

printf("Enter no. of process : ");


scanf("%d", &n);
for(i=0; i<n; i++)
{
printf("Burst time for process P%d (in ms) : ",(i+1));
scanf("%d", &p[i].btime);
p[i].pid = i+1;
}

p[0].wtime = 0;
for(i=0; i<n; i++)
{
p[i+1].wtime = p[i].wtime + p[i].btime;
p[i].ttime = p[i].wtime + p[i].btime;
}
ttur = twat = 0;
for(i=0; i<n; i++)
{
ttur += p[i].ttime;
twat += p[i].wtime;
311521205056

}
awat = (float)twat / n;
atur = (float)ttur / n;

printf("\n FCFS Scheduling\n\n");


for(i=0; i<28; i++)
printf("-");
printf("\nProcess B-Time T-Time W-Time\n");
for(i=0; i<28; i++)
printf("-");
for(i=0; i<n; i++)
printf("\n P%d\t%4d\t%3d\t%2d", p[i].pid,p[i].btime,p[i].ttime,p[i].wtime);
printf("\n");
for(i=0; i<28; i++)
printf("-");

printf("\n\nAverage waiting time : %5.2fms", awat);


printf("\nAverage turn around time : %5.2fms\n", atur);
printf("\n\nGANTT Chart\n");
printf("-");
for(i=0; i<(p[n-1].ttime + 2*n); i++)
printf("-");
printf("\n");
printf("|");
for(i=0; i<n; i++)
{
k = p[i].btime/2;
for(j=0; j<k; j++)
printf(" ");
printf("P%d",p[i].pid);
for(j=k+1; j<p[i].btime; j++)
printf(" ");
printf("|");
}
printf("\n");
printf("-");
for(i=0; i<(p[n-1].ttime + 2*n); i++)
3115212050 4
0
printf("-");
printf("\n");
printf("0");
for(i=0; i<n; i++)
{
for(j=0; j<p[i].btime; j++)
printf(" ");
printf("%2d",p[i].ttime);
}
}

Output:

Result:
311521205056

Program:
/* SJF Scheduling – sjf.c */

#include <stdio.h>
struct process
{
int pid;
int btime;
int wtime;
int ttime;
} p[10], temp;
main()
{
int i,j,k,n,ttur,twat; float awat,atur;
printf("Enter no. of process : ");
scanf("%d", &n);
for(i=0; i<n; i++)
{
printf("Burst time for process P%d (in ms) : ",(i+1));
scanf("%d", &p[i].btime);
p[i].pid = i+1;
}
for(i=0; i<n-1; i++)

{
for(j=i+1; j<n; j++)
{
if((p[i].btime > p[j].btime) ||
(p[i].btime == p[j].btime && p[i].pid > p[j].pid))
{
temp = p[i];
p[i] = p[j];
p[j] = temp;
}
311521205056

}
}
p[0].wtime = 0;
for(i=0; i<n; i++)
{
p[i+1].wtime = p[i].wtime + p[i].btime;
p[i].ttime = p[i].wtime + p[i].btime;
}
ttur = twat = 0;
for(i=0; i<n; i++)
{
ttur += p[i].ttime;
twat += p[i].wtime;
}
awat = (float)twat / n;
atur = (float)ttur / n;

printf("\n SJF Scheduling\n\n");


for(i=0; i<28; i++)
printf("-");
printf("\nProcess B-Time T-Time W-Time\n");
for(i=0; i<28; i++)
printf("-");
for(i=0; i<n; i++)
printf("\n P%-4d\t%4d\t%3d\t%2d",
p[i].pid,p[i].btime,p[i].ttime,p[i].wtime);
printf("\n");
for(i=0; i<28; i++)
printf("-");
printf("\n\nAverage waiting time : %5.2fms", awat); printf("\nAverage turn
around time : %5.2fms\n", atur);

printf("\n\nGANTT Chart\n");
printf("-");
for(i=0; i<(p[n-1].ttime + 2*n); i++)
printf("-"); printf("\n|");
311521205056
for(i=0; i<n; i++)
{
k = p[i].btime/2;
for(j=0; j<k; j++)
printf(" ");
printf("P%d",p[i].pid);
for(j=k+1; j<p[i].btime; j++)
printf(" ");
printf("|");
}
printf("\n-");
for(i=0; i<(p[n-1].ttime + 2*n); i++)
printf("-");
printf("\n0");
for(i=0; i<n; i++)
{
for(j=0; j<p[i].btime; j++)
printf(" ");
printf("%2d",p[i].ttime);
}
}

Output:

Result:
311521205056

Program:
#include <stdio.h>
void
main ()
{
int i, x = -1, k[10], m = 0, n, t, s = 0;
int a[50], temp, b[50], p[10], bur[10], bur1[10];
int wat[10], tur[10], ttur = 0, twat = 0, j = 0;
float awat, atur;

printf ("Enter no. of process : ");


scanf ("%d", &n);
for (i = 0; i < n; i++)
{
printf ("Burst time for process P%d : ", (i + 1));
scanf ("%d", &bur[i]);
bur1[i] = bur[i];
}
printf ("Enter the time slice (in ms) : ");
scanf ("%d", &t);

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


{
b[i] = bur[i] / t;
if ((bur[i] % t) != 0)
b[i] += 1;
m += b[i];
}

printf ("\n\t\tRound Robin Scheduling\n");

printf ("\nGANTT Chart\n");


for (i = 0; i < m; i++)
printf (" ");
printf ("\n");
311521205056

a[0] = 0;
while (j < m)
{
if (x == n - 1)
x = 0;
else
x++;
if (bur[x] >= t)
{
bur[x] -= t;
a[j + 1] = a[j] + t;
if (b[x] == 1)
{
p[s] = x;
k[s] = a[j + 1];
s++;
}
j++;
b[x] -= 1;
printf (" P%d |", x + 1);
}
else if (bur[x] != 0)
{
a[j + 1] = a[j] + bur[x];
bur[x] = 0;
if (b[x] == 1)
{
p[s] = x;
k[s] = a[j + 1];
s++;
}
j++;
b[x] -= 1;
printf (" P%d |", x + 1);
}
}
311521205056

printf ("\n");
for (i = 0; i < m; i++)
printf (" ");
printf ("\n");

for (j = 0; j <= m; j++)


printf ("%d\t", a[j]);

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


{
for (j = i + 1; j < n; j++)
{
if (p[i] > p[j])
{
temp = p[i];
p[i] = p[j];
p[j] = temp;

temp = k[i];
k[i] = k[j];
k[j] = temp;
}
}
}
for (i = 0; i < n; i++)
{
wat[i] = k[i] - bur1[i];
tur[i] = k[i];
}
for (i = 0; i < n; i++)
{
ttur += tur[i];
twat += wat[i];
}

printf ("\n\n");
for (i = 0; i < 30; i++)
311521205056
printf ("-");
printf ("\nProcess\tBurst\tTrnd\tWait\n");
for (i = 0; i < 30; i++)
printf ("-");
for (i = 0; i < n; i++)
printf ("\nP%-4d\t%4d\t%4d\t%4d", p[i] + 1, bur1[i], tur[i], wat[i]);
printf ("\n");
for (i = 0; i < 30; i++)
printf ("-");

awat = (float) twat / n;


atur = (float) ttur / n;
printf ("\n\nAverage waiting time : %.2f ms", awat);
printf ("\nAverage turn around time : %.2f ms\n", atur);
}

Output:

Result:
311521205056

Program:

#include <stdio.h>

struct process
{
int pid; int btime;
int pri; int wtime;
int ttime;
} p[10], temp;
main()
{
int i,j,k,n,ttur,twat; float
awat,atur;
printf("Enter no. of process : "); scanf("%d",
&n);
for(i=0; i<n; i++)
{
printf("Burst time for process P%d (in ms) : ", (i+1)); scanf("%d",
&p[i].btime);
printf("Priority for process P%d : ", (i+1)); scanf("%d", &p[i].pri);
p[i].pid = i+1;
}
for(i=0; i<n-1; i++)
{
for(j=i+1; j<n; j++)
311521205056

{
if((p[i].pri > p[j].pri) ||
(p[i].pri == p[j].pri && p[i].pid > p[j].pid) )
{
temp = p[i]; p[i] =
p[j]; p[j] = temp;
}
}
}
p[0].wtime = 0; for(i=0; i<n;
i++)
{
p[i+1].wtime = p[i].wtime + p[i].btime; p[i].ttime =
p[i].wtime + p[i].bt
ttur = twat = 0; for(i=0; i<n; i++)
{
ttur += p[i].ttime; twat +=
p[i].wtime;
}
awat = (float)twat / n; atur =
(float)ttur / n;
printf("\n\t Priority Scheduling\n\n"); for(i=0; i<38; i++)
printf("-");
printf("\nProcess B-Time Priority T-Time W-Time\n");
for(i=0; i<38; i++)
printf("-"); for (i=0; i<n;
i++)
311521205056

printf("\n P%-4d\t%4d\t%3d\t%4d\t%4d",
p[i].pid,p[i].btime,p[i].pri,p[i].ttime,p[i].wtime);
printf("\n"); for(i=0; i<38;
i++)
printf("-");
printf("\n\nAverage waiting time : %5.2fms", awat);
printf("\nAverage turn around time : %5.2fms\n", atur);
printf("\n\nGANTT Chart\n"); printf("-
");
for(i=0; i<(p[n-1].ttime + 2*n); i++) printf("-");
printf("\n|"); for(i=0; i<n;
i++)
{
k = p[i].btime/2; for(j=0; j<k;
j++)printf(" ");
printf("P%d",p[i].pid);
for(j=k+1; j<p[i].btime;
j++)printf(" ");
printf("|");
}
printf("\n-");
for(i=0; i<(p[n-1].ttime + 2*n); i++) printf("-");
printf("\n0"); for(i=0; i<n;
i++)
{
for(j=0; j<p[i].btime; j++)printf(" ");
printf("%2d",p[i].ttime);
}}
311521205056

Output:

Result:

You might also like