0% found this document useful (0 votes)
45 views13 pages

82 Abhishek OS EXP4

The document discusses two CPU scheduling algorithms: first come first serve (FCFS) and priority scheduling. It provides details on how each algorithm works, including examples and pseudocode. FCFS is non-preemptive and processes are executed in the order of their arrival. Priority scheduling is preemptive and the process with the highest priority is selected to execute.
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)
45 views13 pages

82 Abhishek OS EXP4

The document discusses two CPU scheduling algorithms: first come first serve (FCFS) and priority scheduling. It provides details on how each algorithm works, including examples and pseudocode. FCFS is non-preemptive and processes are executed in the order of their arrival. Priority scheduling is preemptive and the process with the highest priority is selected to execute.
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/ 13

Roll No: 82 Name: Abhishek A.

Goykar

ATHARVA EDUCATIONAL TRUST'S


ATHARVA COLLEGE OF ENGINEERING
(Approved by AICTE, Recognized by the Government of Maharashtra
& Affiliated to University of Mumbai )
Department of Computer Engineering
Academic Year 2021-22

Experiment No. 4: To Study about CPU Scheduling Algorithms.


a. Write a program to demonstrate the concept of non-preemptive
scheduling algorithms.(FCFS)
b. Write a program to demonstrate the concept of preemptive scheduling
algorithms.(Priority)

Aim :- To Study about CPU Scheduling Algorithms.


a. Write a program to demonstrate the concept of non-preemptive
scheduling algorithms.(FCFS)
b. Write a program to demonstrate the concept of preemptive
scheduling algorithms.(Priority)

Software Used :-

Theory :-

1) FCFS CPU Scheduling (Non Pre-emptive) –

First Come First Serve is an Non-preemptive Scheduling


algorithm where each process is executed according to its arrival time.
First Come First Serve (FCFS) is also known as First In First Out
(FIFO) scheduling algorithm is the easiest and simplest CPU scheduling
algorithm where the process which arrives first in the ready queue is
Roll No: 82 Name: Abhishek A.
Goykar

ATHARVA EDUCATIONAL TRUST'S


ATHARVA COLLEGE OF ENGINEERING
(Approved by AICTE, Recognized by the Government of Maharashtra
& Affiliated to University of Mumbai )
Department of Computer Engineering
Academic Year 2021-22

executed first by the CPU. New process is executed only when the
current process is executed fully by the CPU.
Example :

Let's take an example of The FCFS scheduling algorithm. In the Following


schedule, there are 5 processes with process ID P0, P1, P2, P3 and P4. P0
arrives at time 0, P1 at time 1, P2 at time 2, P3 arrives at time 3 and Process P4
arrives at time 4 in the ready queue. The processes and their respective Arrival
and Burst time are given in the following table.
The Turnaround time and the waiting time are calculated by using the following
formula.
1. Turn Around Time = Completion Time - Arrival Time

2. Waiting Time = Turnaround time - Burst Time


The average waiting Time is determined by summing the respective waiting
time of all the processes and divided the sum by the total number of processes.

Process Arrival Burst Completion Turn Around Waiting


ID Time Time Time Time Time

0 0 2 2 2 0

1 1 6 8 7 1

2 2 4 12 10 6

3 3 9 21 18 9
Roll No: 82 Name: Abhishek A.
Goykar

ATHARVA EDUCATIONAL TRUST'S


ATHARVA COLLEGE OF ENGINEERING
(Approved by AICTE, Recognized by the Government of Maharashtra
& Affiliated to University of Mumbai )
Department of Computer Engineering
Academic Year 2021-22

4 6 12 33 29 17

Avg Waiting Time=31/5

(Gantt chart)

Advantages of FCFS

Here, are pros/benefits of using FCFS scheduling algorithm:


 The simplest form of a CPU scheduling algorithm
 Easy to program
 First come first served

Disadvantages of FCFS

Here, are cons/ drawbacks of using FCFS scheduling algorithm:


 It is a Non-Preemptive CPU scheduling algorithm, so after the
process has been allocated to the CPU, it will never release the
CPU until it finishes executing.
 The Average Waiting Time is high.
 Short processes that are at the back of the queue have to wait for
the long process at the front to finish.
 Not an ideal technique for time-sharing systems.
Roll No: 82 Name: Abhishek A.
Goykar

ATHARVA EDUCATIONAL TRUST'S


ATHARVA COLLEGE OF ENGINEERING
(Approved by AICTE, Recognized by the Government of Maharashtra
& Affiliated to University of Mumbai )
Department of Computer Engineering
Academic Year 2021-22

 Because of its simplicity, FCFS is not very efficient.

2) Priority CPU Scheduling(Pre-emptive) –

In Preemptive Priority Scheduling, at the time of arrival of a


process in the ready queue, its Priority is compared with the priority of
the other processes present in the ready queue as well as with the one
which is being executed by the CPU at that point of time. The One with
the highest priority among all the available processes will be given the
CPU next.

The difference between preemptive priority scheduling and non


preemptive priority scheduling is that, in the preemptive priority
scheduling, the job which is being executed can be stopped at the arrival
of a higher priority job.

Once all the jobs get available in the ready queue, the algorithm
will behave as non-preemptive priority scheduling, which means the job
scheduled will run till the completion and no preemption will be done.

Advantages of Priority Scheduling Algorithm:

1. It is simple in use.
2. Important processes are never made to wait because of the
execution of less important processes.
Roll No: 82 Name: Abhishek A.
Goykar

ATHARVA EDUCATIONAL TRUST'S


ATHARVA COLLEGE OF ENGINEERING
(Approved by AICTE, Recognized by the Government of Maharashtra
& Affiliated to University of Mumbai )
Department of Computer Engineering
Academic Year 2021-22

3. Suitable for application with varying time and resource


requitements.

Disadvantages of Priority Scheduling Algorithm:

1. If suffers from the problem of starvation of lower priority


processes, since the continuous arrival of higher priority processes
will prevent lower priority processes indefinitely from acquiring
the CPU.
2. A Priority scheduling can leave some low priority waiting
processes indefinitely for CPU.
Roll No: 82 Name: Abhishek A.
Goykar

ATHARVA EDUCATIONAL TRUST'S


ATHARVA COLLEGE OF ENGINEERING
(Approved by AICTE, Recognized by the Government of Maharashtra
& Affiliated to University of Mumbai )
Department of Computer Engineering
Academic Year 2021-22

Program Code :-

1) FCFS CPU Scheduling (Non Pre-emptive) -

#include <stdio.h>
// Function to find the waiting time for all processes
int waitingtime(int proc[], int n,
int burst_time[], int wait_time[]) {
Roll No: 82 Name: Abhishek A.
Goykar

ATHARVA EDUCATIONAL TRUST'S


ATHARVA COLLEGE OF ENGINEERING
(Approved by AICTE, Recognized by the Government of Maharashtra
& Affiliated to University of Mumbai )
Department of Computer Engineering
Academic Year 2021-22

// waiting time for first process is 0


wait_time[0] = 0;
// calculating waiting time
for (int i = 1; i < n ; i++ )
wait_time[i] = burst_time[i-1] + wait_time[i-1] ;
return 0;
}
// Function to calculate turn around time
int turnaroundtime( int proc[], int n,
int burst_time[], int wait_time[], int tat[]) {
// calculating turnaround time by adding
// burst_time[i] + wait_time[i]
int i;
for ( i = 0; i < n ; i++)
tat[i] = burst_time[i] + wait_time[i];
return 0;
}
//Function to calculate average time
int avgtime( int proc[], int n, int burst_time[]) {
int wait_time[n], tat[n], total_wt = 0, total_tat = 0;
int i;
//Function to find waiting time of all processes
waitingtime(proc, n, burst_time, wait_time);
Roll No: 82 Name: Abhishek A.
Goykar

ATHARVA EDUCATIONAL TRUST'S


ATHARVA COLLEGE OF ENGINEERING
(Approved by AICTE, Recognized by the Government of Maharashtra
& Affiliated to University of Mumbai )
Department of Computer Engineering
Academic Year 2021-22

//Function to find turn around time for all processes


turnaroundtime(proc, n, burst_time, wait_time, tat);
//Display processes along with all details
printf("Processes Burst Waiting Turn around \n");
// Calculate total waiting time and total turn
// around time
for ( i=0; i<n; i++) {
total_wt = total_wt + wait_time[i];
total_tat = total_tat + tat[i];
printf(" %d\t %d\t\t %d \t%d\n", i+1, burst_time[i],
wait_time[i], tat[i]);
}
printf("Average waiting time = %f\n", (float)total_wt / (float)n);
printf("Average turn around time = %f\n", (float)total_tat / (float)n);
return 0;
}
// main function
int main() {
//process id's
int proc[] = { 1, 2, 3};
int n = sizeof proc / sizeof proc[0];
//Burst time of all processes
int burst_time[] = {5, 8, 12};
Roll No: 82 Name: Abhishek A.
Goykar

ATHARVA EDUCATIONAL TRUST'S


ATHARVA COLLEGE OF ENGINEERING
(Approved by AICTE, Recognized by the Government of Maharashtra
& Affiliated to University of Mumbai )
Department of Computer Engineering
Academic Year 2021-22

avgtime(proc, n, burst_time);
return 0;
}

Output :-
Roll No: 82 Name: Abhishek A.
Goykar

ATHARVA EDUCATIONAL TRUST'S


ATHARVA COLLEGE OF ENGINEERING
(Approved by AICTE, Recognized by the Government of Maharashtra
& Affiliated to University of Mumbai )
Department of Computer Engineering
Academic Year 2021-22

2) Priority CPU Scheduling(Pre-emptive) –

#include<stdio.h>
struct process
{
int WT,AT,BT,TAT,PT;
};
struct process a[10];
int main()
{
int n,temp[10],t,count=0,short_p;
float total_WT=0,total_TAT=0,Avg_WT,Avg_TAT;
printf("Enter the number of the process\n");
scanf("%d",&n);
printf("Enter the arrival time , burst time and priority of the
process\n");
printf("AT BT PT\n");
for(int i=0;i<n;i++)
{
scanf("%d%d%d",&a[i].AT,&a[i].BT,&a[i].PT);
// copying the burst time in
// a temp array fot futher use
temp[i]=a[i].BT;
Roll No: 82 Name: Abhishek A.
Goykar

ATHARVA EDUCATIONAL TRUST'S


ATHARVA COLLEGE OF ENGINEERING
(Approved by AICTE, Recognized by the Government of Maharashtra
& Affiliated to University of Mumbai )
Department of Computer Engineering
Academic Year 2021-22

}
// we initialize the burst time
// of a process with maximum
a[9].PT=10000;
for(t=0;count!=n;t++)
{
short_p=9;
for(int i=0;i<n;i++)
{
if(a[short_p].PT>a[i].PT && a[i].AT<=t && a[i].BT>0)
{
short_p=i;
}
}
a[short_p].BT=a[short_p].BT-1;
// if any process is completed
if(a[short_p].BT==0)
{
// one process is completed
// so count increases by 1
count++;
a[short_p].WT=t+1-a[short_p].AT-temp[short_p];
a[short_p].TAT=t+1-a[short_p].AT;
Roll No: 82 Name: Abhishek A.
Goykar

ATHARVA EDUCATIONAL TRUST'S


ATHARVA COLLEGE OF ENGINEERING
(Approved by AICTE, Recognized by the Government of Maharashtra
& Affiliated to University of Mumbai )
Department of Computer Engineering
Academic Year 2021-22

// total calculation
total_WT=total_WT+a[short_p].WT;
total_TAT=total_TAT+a[short_p].TAT;
}
}
Avg_WT=total_WT/n;
Avg_TAT=total_TAT/n;
// printing of the answer
printf("ID WT TAT\n");
for(int i=0;i<n;i++)
{
printf("%d %d\t%d\n",i+1,a[i].WT,a[i].TAT);
}
printf("Avg waiting time of the process is %f\n",Avg_WT);
printf("Avg turn around time of the process is %f\n",Avg_TAT);
return 0;
}

Output :-
Roll No: 82 Name: Abhishek A.
Goykar

ATHARVA EDUCATIONAL TRUST'S


ATHARVA COLLEGE OF ENGINEERING
(Approved by AICTE, Recognized by the Government of Maharashtra
& Affiliated to University of Mumbai )
Department of Computer Engineering
Academic Year 2021-22

Conclusion:

Hence, we successfully studied and implemented the Non-Pre-


emptive FCFS and Pre-emptive Priority CPU Scheduling Algorithms

You might also like