Term Project: Jannat Rehman F2018266200 Topic: FCFS Section: W1
Term Project: Jannat Rehman F2018266200 Topic: FCFS Section: W1
Jannat Rehman
F2018266200
Topic: FCFS
Section: W1
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.
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:
1 3 13
2 1 9
3 2 3
P2 P3 P1
0 9 12 25
TurnaroundTime (T.A)|:It is the sum of execution time of process and wait time.
#include<studio.h>
Struct PCB {
Int pid arrival, burst, turnaround;
};
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;
}
}
}
pline(44) ;
avg = sum/ (float)num;
printf (\nTotal Turnaround Time : %f.”, sum);
printf (\nAverage Turnaround Time : % 3f.”, avg);
}
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.