Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

EXPERIMENT: - 4

Implementation of FCFS Scheduling algorithm


First Come First Serve Scheduling Algorithm: - In the "First come first serve" scheduling algorithm,
as the name suggests, the process which arrives first, gets executed first, or we can say that the process which
requests the CPU first, gets the CPU allocated first.

• First Come First Serve is just like FIFO (First in First out) Queue data structure, where the data
element which is added to the queue first, is the one who leaves the queue first.

• This is used in Batch Systems.

• It's easy to understand and implement programmatically, using a Queue data structure, where a new
process enters through the tail of the queue, and the scheduler selects process from the head of the
queue.

• A perfect real life example of FCFS scheduling is buying tickets at ticket counter.

Calculating Average Waiting Time: - For every scheduling algorithm, Average waiting time is a crucial
parameter to judge its performance.

AWT or Average waiting time is the average of the waiting times of the processes in the queue, waiting for
the scheduler to pick them for execution.
Lower the Average Waiting Time, better the scheduling algorithm.
consider the processes P1, P2, P3, P4 given in the below table, arrives for execution in the same order,
with Arrival Time 0, and given Burst Time, let's find the average waiting time using the FCFS scheduling
algorithm.
The average waiting time will be 18.75 ms
For the above given processes, first P1 will be provided with the CPU resources,

• Hence, waiting time for P1 will be 0

• P1 requires 21 ms for completion, hence waiting time for P2 will be 21 ms

• Similarly, waiting time for process P3 will be execution time of P1 + execution time for P2, which will
be (21 + 3) ms = 24 ms.

• For process P4 it will be the sum of execution times of P1, P2 and P3.

The Gantt chart above perfectly represents the waiting time for each process.

Problems with FCFS Scheduling

Below we have a few shortcomings or problems with the FCFS scheduling algorithm: -

1. It is Non Pre-emptive algorithm, which means the process priority doesn't matter.

If a process with very least priority is being executed, more like daily routine backup process, which
takes more time, and all of a sudden some other high priority process arrives, like interrupt to avoid
system crash, the high priority process will have to wait, and hence in this case, the system will crash,
just because of improper process scheduling.

2. Not optimal Average Waiting Time.

3. Resources utilization in parallel is not possible, which leads to Convoy Effect, and hence poor resource
(CPU, I/O etc.) utilization.

What is Convoy Effect?

Convoy Effect is a situation where many processes, who need to use a resource for short time are blocked by
one process holding that resource for a long time.
This essentially leads to poort utilization of resources and hence poor performance.

Program for FCFS Scheduling: - Here we have a simple C++ program for processes with arrival
time as 0.

If you are not familiar with C++ language, we would recommend you to first Learn C++ language.
In the program, we will be calculating the Average waiting time and Average turnaround time for a
given array of Burst times for the list of processes.
#include<iostream>

using namespace std;

// function to find the waiting time for all processes


void findWaitingTime(int processes[], int n, int bt[], int wt[])
{
// waiting time for first process will be 0
wt[0] = 0;

// calculating waiting time


for (int i = 1; i < n ; i++)
{
wt[i] = bt[i-1] + wt[i-1];
}
}

// function to calculate turn around time


void findTurnAroundTime( int processes[], int n, int bt[], int wt[], int tat[])
{
// calculating turnaround time by adding
// bt[i] + wt[i]
for (int i = 0; i < n ; i++)
{
tat[i] = bt[i] + wt[i];
}
}

// function to calculate average time


void findAverageTime( int processes[], int n, int bt[])
{
int wt[n], tat[n], total_wt = 0, total_tat = 0;

// function to find waiting time of all processes


findWaitingTime(processes, n, bt, wt);

// function to find turn around time for all processes


findTurnAroundTime(processes, n, bt, wt, tat);

// display processes along with all details


cout << "Processes "<< " Burst time "<< " Waiting time " << " Turn around time\n";

// calculate total waiting time and total turn around time


for (int i = 0; i < n; i++)
{
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
cout << " " << i+1 << "\t\t" << bt[i] <<"\t "<< wt[i] <<"\t\t " << tat[i] <<endl;
}

cout << "Average waiting time = "<< (float)total_wt / (float)n;


cout << "\nAverage turn around time = "<< (float)total_tat / (float)n;
}

// main function
int main()
{
// process ids
int processes[] = { 1, 2, 3, 4};
int n = sizeof processes / sizeof processes[0];
// burst time of all processes
int burst_time[] = {21, 3, 6, 2};

findAverageTime(processes, n, burst_time);

return 0;
}

Output: -

Here we have simple formulae for calculating various times for given processes: -

Completion Time: - Time taken for the execution to complete, starting from arrival time.

Turn Around Time: - Time taken to complete after arrival. In simple words, it is the difference between
the Completion time and the Arrival time.

Waiting Time: - Total time the process has to wait before it's execution begins. It is the difference between
the Turn Around time and the Burst time of the process.

For the program above, we have considered the arrival time to be 0 for all the processes, try to implement a
program with variable arrival times.
EXPERIMENT: - 5
Implementation of SJF Scheduling algorithm

Shortest Job First (SJF): - Shortest Job First is an algorithm in which the process having the smallest
execution time is chosen for the next execution. This scheduling method can be preemptive or non-
preemptive. It significantly reduces the average waiting time for other processes awaiting execution.

Preemptive Scheduling: - Preemptive scheduling is used when a process switches from running state to
ready state or from waiting state to ready state. The resources (mainly CPU cycles) are allocated to the process
for the limited amount of time and then is taken away, and the process is again placed back in the ready queue
if that process still has CPU burst time remaining. That process stays in ready queue till it gets next chance to
execute.

Non-Preemptive Scheduling: - Non-preemptive Scheduling is used when a process terminates, or a


process switches from running to waiting state. In this scheduling, once the resources (CPU cycles) is
allocated to a process, the process holds the CPU till it gets terminated or it reaches a waiting state. In case of
non-preemptive scheduling does not interrupt a process running CPU in middle of the execution. Instead, it
waits till the process complete its CPU burst time and then it can allocate the CPU to another process.
Basic Terminology: -
• Burst time:-Burst time is the total time taken by the process for its execution on the CPU.
• Arrival time:-Arrival time is the time when a process enters into the ready state and is ready for
its execution.
• Response time:-Time at which the process gets the CPU for the First time - Arrival
time.
• Turnaround time = Completion Time - Arrival Time
• Waiting time = Turnaround time - Burst time
Algorithm: -
1. Sort all the process according to the arrival time.
2. Then select that process which has minimum arrival time and minimum Burst time.
3. After completion of process make a pool of process which after till the completion of previous process
and select that process among the pool which is having minimum Burst time.
Program for Shortest Job First Scheduling: -

#include<iostream>

using namespace std;

int mat[10][6];

void swap(int *a, int *b)

int temp = *a;

*a = *b;

*b = temp;

void arrangeArrival(int num, int mat[][6])

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

for(int j=0; j<num-i-1; j++)

if(mat[j][1] > mat[j+1][1])


{
for(int k=0; k<5; k++)

swap(mat[j][k], mat[j+1][k]);

void completionTime(int num, int mat[][6])

int temp, val;

mat[0][3] = mat[0][1] + mat[0][2];

mat[0][5] = mat[0][3] - mat[0][1];

mat[0][4] = mat[0][5] - mat[0][2];

for(int i=1; i<num; i++)

temp = mat[i-1][3];

int low = mat[i][2];


for(int j=i; j<num; j++)

if(temp >= mat[j][1] && low >= mat[j][2])

low = mat[j][2];

val = j;

mat[val][3] = temp + mat[val][2];

mat[val][5] = mat[val][3] - mat[val][1];

mat[val][4] = mat[val][5] - mat[val][2];

for(int k=0; k<6; k++)

swap(mat[val][k], mat[i][k]);

}
int main()

int num, temp;

cout<<"Enter number of Process: ";

cin>>num;

cout<<"...Enter the process ID...\n";

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

cout<<"...Process "<<i+1<<"...\n";

cout<<"Enter Process Id: ";

cin>>mat[i][0];

cout<<"Enter Arrival Time: ";

cin>>mat[i][1];

cout<<"Enter Burst Time: ";

cin>>mat[i][2];

cout<<"Before Arrange...\n";

cout<<"Process ID\tArrival Time\tBurst Time\n";


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

cout<<mat[i][0]<<"\t\t"<<mat[i][1]<<"\t\t"<<mat[i][2]<<"\n";

arrangeArrival(num, mat);

completionTime(num, mat);

cout<<"Final Result...\n";

cout<<"Process ID\tArrival Time\tBurst Time\tWaiting Time\tTurnaround Time\n";

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

cout<<mat[i][0]<<"\t\t"<<mat[i][1]<<"\t\t"<<mat[i][2]<<"\t\t"<<mat[i][4]<<"\t\t"<<mat[i][5]<<"\n";

}
}

Output: -
Description of Program: - In the above program, we calculate the average waiting and
average turnaround times of the jobs. We first ask the user to enter the number of processes
and store it in n. We then accept the burst times from the user. It is stored in the bt array.
After this, the burst times are sorted in the next section so the shortest one can be
executed first. Here selection sort is used to sort the array of burst time bt.
Waiting time of the first element is zero, the remaining waiting time is calculated by
using two for loop that runs from 1 to in that controls the outer loop and the inner loop
is controlled by another for loop that runs from j=0 to j<i. Inside the loop, the waiting
time is calculated by adding the burst time to the waiting time.
for(i=1;i<n;i++)
{wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
}
Total is the addition of all the waiting time together. The average waiting time is
calculated:

Avg-wt= (float) total/n;


And it is printed.
Next, the turnaround time is calculated by adding the burst time and the waiting time
Again, here the for loop is used. And the total variable here holds the total turnaround time.
After this the average turnaround time is calculated. This is how SJF takes place.

You might also like