Nothing Special   »   [go: up one dir, main page]

First Come First Served Scheduling

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

1

First Come First Served Scheduling

Aim:
To implement the FCFS Problem using C language constructs.

Objective:
1. This is one of the very brute force algorithm. A process that requests for the CPU first
is allocated the CPU first. Hence, the name first come first serve.
2. The FCFS algorithm is implemented by using first-in-first-out (FIFO) queue structure
for the ready queue.
3. When a process joins the ready queue its PCB is linked to the tail of the FIFO queue.
4. When the CPU is idle, the process at the head of the FIFO queue is allocated the CPU
and deleted from the queue.
5. To implement the queue using arrays.

Algorithm:
1. The arrays for processes, waiting time, turnaround time, burst time, and arrival time are
pname[ ][ ], wt[ ], tt[ ], bt[ ], at[ ] respectively.
2. The user is prompted for NAME, BURST TIME, and ARRIVAL TIME using printf() and
values for the arrays are accepted using scanf( ) functions.
3. The process at the head is deleted from the queue and is assigned to a temp variable.
4. The total waiting time, turn around time and their average is processed and printed using
printf( ) function inside a for( ) loop.
5. The GANT CHART is displayed using printf( ) function and the various formatting
escape sequences.

Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>

void main( )
{
float avgwt, avgtt;
char pname[10][10];
int wt[10], tt[10], bt[10], at[10], t, q, I, n, sum=0, ttime, j, ss=0;
printf(“\n\nEnter the number of processes:”);
scanf(“%d”, &n);
printf(“\n\nEnter the NAME, BURST TIME and ARRIVAL TIME of the process:”);
for(i=0; i<n; i++)
{
2

printf(“\n\n NAME:”);
scanf(“%s”, &pname[i]);
printf(“\n\n BURST TIME:”);
scanf(“%d”, &bt[i]);
printf(“\n\n ARRIVAL TIME:”);
scanf(“%d”, &at[i]);
}
for(i=0; i<n; i++)
for(j=i+1; j<n; j++)
{
if(at[i] > at[j])
{
t = at[i];
at[i] = at[j];
at[j] = t;
q = bt[i];
bt[i] = bt[j];
bt[j] = q;
strcpy(c[i], pname[i]);
strcpy(pname[i], pname[j]);
strcpy(pname[j], c[i]);
}
}
wt[0] = 0;
for(i=0; i<n; i++)
{
wt[i+1] = wt[i] + bt[i];
sum = sum + (wt[i+1] – at[i]);
tt[i] = ss + bt[i];
}
avgwt = (float) wt[i]/n;
avgtt = (float) sum/n;
printf(“\n\nAverage Waiting Time = %f”, avgwt);
printf(“\n\nAverage Turn-around Time = %f”, avgtt);
printf(“\n\n GANTT CHART\N”);
for(i=0; i<n; i++)
{
printf(“|\t%s\t”, pname[i]);
}
printf(“\n”);
for(i=0; i<n; i++)
{
3

printf(“%d\t\t”, wt[i]);
}
printf(“%d\n”, ss);
printf(“\n”);
getch( );
}

Output:
Enter the number of processes: 3
Enter the NAME, BURST TIME and ARRIVAL TIME of the process:
NAME: P1
BURST TIME: 24
ARRIVAL TIME: 0
NAME: P2
BURST TIME: 3
ARRIVAL TIME: 0
NAME: P3
BURST TIME: 3
ARRIVAL TIME: 0
Average Waiting Time = 10.000000
Average Turn-around Time = 27.000000
GANTT CHART
| P1 | P2 | P3
0| 24 | 3 | 3

You might also like