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

Term Project: Jannat Rehman F2018266200 Topic: FCFS Section: W1

The document discusses First Come First Serve (FCFI) scheduling. It provides details on how FCFS works by allocating CPU to processes based on their order of arrival. It includes pseudocode for an FCFS scheduling algorithm. The pseudocode sorts processes by arrival time, calculates turnaround times by adding burst times, and outputs total and average turnaround times.

Uploaded by

Aden Tahir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

Term Project: Jannat Rehman F2018266200 Topic: FCFS Section: W1

The document discusses First Come First Serve (FCFI) scheduling. It provides details on how FCFS works by allocating CPU to processes based on their order of arrival. It includes pseudocode for an FCFS scheduling algorithm. The pseudocode sorts processes by arrival time, calculates turnaround times by adding burst times, and outputs total and average turnaround times.

Uploaded by

Aden Tahir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Term Project

Jannat Rehman
F2018266200
Topic: FCFS
Section: W1

First Come First Serve Scheduling(FCFS):

As the name suggests, the process which arrives first gets executed first or we can say that the
process which sends the request to CPU first, gets the CPU allocated first.

● In FCFS, CPU is allocated to the process in the order of their arrival.


● It is implemented with a FIFO (first in first out) queue.
● It is a non-preemptive scheduling algorithm.
● This is used in Batch System

Note:

Non-preemptive means algorithm designed such that once the process is allocated to CPU, it
doesn’t free the CPU until it completes its execution.
Preemptive algorithm is one in which the CPU is taken away from the process during execution. If
any high priority process arrives, CPU from the currently executing low priority process is allocated
to it.
Example:

If we assume that arrival time is 0. Consider the following example:

Process(Pid) Arrival Burst Time

1 3 13

2 1 9

3 2 3

The Gantt chart shows the result:

P2 P3 P1
0 9 12 25

TurnaroundTime (T.A)|:It is the sum of execution time of process and wait time.

TurnaroundTime = Burst Time + Wait Time

T.A time of P2 = 9+0 = 9


T.A time of P3 = 3+9 = 12
T.A time of P1 = 13+12 = 25

Total T.A time = T.A time of P2 + T.A time of P3 + T.A time of P1


= 9 + 12 + 25
= 46

Average T.A time = Total T.A time / No.of Processes


= 46 / 3
= 15.33
Program for FCFS Scheduling

#include<studio.h>
Struct PCB {
Int pid arrival, burst, turnaround;
};

Void pline(int x);


Void main()
{

Int i, num, j;
Float avg = 0.0, sum=0.0;
Struct PCB p[10], Temp;
printf(“Enter the total number of processes: ”);
scanf(“%d” , &num);
for(i=0; i <num; i++)
{
printf(“Enter Arrival time and Burst time for process %d: /n”, i+1);
scanf (“%d %d” , &p[i] , arrival, &p[i] , burst);
P[ i ] . pid = i+1;
}
for( i=0; i < num-1; i++)
{
for( j=0; j < n - i - 1; j++)
{
if ( p [ j ].arrival > p[ j+1 ], arrival )
{
temp = p [ j ];
p [ j ] = p [ j+1 ];
p [ j+1 ] = temp;

}
}
}

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


{
sum = sum + p[ i ].burst;
P[ i ].turnaround = sum;
}
sum = 0;
pline(44);
printf(“PID\ tArrival \ tBurst \ tTurnaround\n”);
pline(44);
for ( i = 0; i< num; i++)
{
printf(“%d\t%d\t%d\t%d\n”,p[ i ].pid, p[ i ].arrival, p[ i ].turnaround);
sum += p [ i ].turnaround;
}

pline(44) ;
avg = sum/ (float)num;
printf (\nTotal Turnaround Time : %f.”, sum);
printf (\nAverage Turnaround Time : % 3f.”, avg);

//pline function is used to draw line on output//


void pline(int x)
{
Int i;
for(i = 0; i < x; i++)
{
printf(“-”);

}
printf (“\n”);

Explanation:

We created a structure ‘PCB’ with attributes for process id as pid, burst time as burst and
turnaround time as turnaround. Next, we declared the pLine function, for formatting, which has its
body at end of the code. Its draws straight lines using hyphens according to the integer x given as
its parameter. Moving to the main body we did the declarations for counter variables i, j followed by
other custom struct declaration temp, p[10] and finally initializations for average, avg to 0.0 and
sum as 0. Before the first for loop, we prompt the user to enter the number of processes which we
store in num and use to stop counter for the loop. The Loop itself is used to prompt and populate
the attributes arrival, burst for p in each iteration, meaning for very process. Also, the pid for every
iteration is assigned, starting from 1, using the value I + 1. Moving on to the nested for loops, we
are first sorting the processing in ascending order on the basis for their arrival time for the
processes. They are swapped using the Temp if the arrival time for the next processes is less than
the previous one. Once in order, the proceeding For loop then calculates the turnaround and sum
using the relevant formulas as shown above. The last For loop is for printing all the attributes for p
including arrival, turnaround and pid for every process and, we calculate the sum by adding
turnaround times for each iteration. Later, we see that used this value of sum to calculate the
average turnaround time by dividing it by the total number of processes. We print the Total
turnaround time and average turnaround times repeatedly at the end.

You might also like