SlideShare a Scribd company logo
1 of 11
Download to read offline
CPU Scheduling
CPU Scheduling is a process of determining which process will own CPU for execution while
another process is on hold. The main task of CPU scheduling is to make sure that whenever the
CPU remains idle, the OS at least select one of the processes available in the ready queue for
execution. The selection process will be carried out by the CPU scheduler. It selects one of the
processes in memory that are ready for execution.
Important CPU scheduling Terminologies
 Burst Time/Execution Time: It is a time required by the process to complete execution.
It is also called running time.
 Arrival Time: when a process enters in a ready state
 Finish Time: when process complete and exit from a system
 Multiprogramming: A number of programs which can be present in memory at the same
time.
 Jobs: It is a type of program without any kind of user interaction.
 User: It is a kind of program having user interaction.
 Process: It is the reference that is used for both job and user.
 CPU/IO burst cycle: Characterizes process execution, which alternates between CPU and
I/O activity. CPU times are usually shorter than the time of I/O.
CPU Scheduling Criteria
A CPU scheduling algorithm tries to maximize and minimize the following:
Maximize:
 CPU utilization: CPU utilization is the main task in which the operating system needs to
make sure that CPU remains as busy as possible. It can range from 0 to 100 percent.
However, for the RTOS, it can be range from 40 percent for low-level and 90 percent for
the high-level system.
 Throughput: The number of processes that finish their execution per unit time is known
Throughput. So, when the CPU is busy executing the process, at that time, work is being
done, and the work completed per unit time is called Throughput.
Minimize:
 Waiting time: Waiting time is the total time spent by the process in the ready state waiting
for CPU. For example, consider the arrival time of all the below 3 processes to be 0 ms, 0
ms, and 2 ms.
Then the waiting time for all the 3 processes will be:
 P1: 0 ms
 P2: 8 ms because P2 have to wait for the complete execution of P1 and arrival time of
P2 is 0 ms.
 P3: 13 ms becuase P3 will be executed after P1 and P2 i.e. after 8+7 = 15 ms and the
arrival time of P3 is 2 ms. So, the waiting time of P3 will be: 15-2 = 13 ms.
Waiting time = Turnaround time - Burst time
 Response time: Response time is the time spent when the process is in the ready state and
gets the CPU for the first time. For example, consider the below processes:
Here, the response time of all the 3 processes are:
 P1: 0 ms
 P2: 7 ms because the process P2 have to wait for 8 ms during the execution of P1 and
then after it will get the CPU for the first time. Also, the arrival time of P2 is 1 ms. So,
the response time will be 8-1 = 7 ms.
 P3: 13 ms because the process P3 have to wait for the execution of P1 and P2 i.e. after
8+7 = 15 ms, the CPU will be allocated to the process P3 for the first time. Also, the
arrival of P3 is 2 ms. So, the response time for P3 will be 15-2 = 13 ms.
Response time = Time at which the process gets the CPU for the first time - Arrival time
 Turnaround Time: Turnaround time is the total amount of time spent by the process
from coming in the ready state for the first time to its completion.
Turnaround time = Burst time + Waiting time
Types of CPU scheduling Algorithm
 Non-Preemptive Scheduling: Under non-preemptive scheduling, once the CPU has been
allocated to a process, the process keeps the CPU until it releases the CPU either by
terminating or by switching to the waiting state. In non-preemptive scheduling, it does not
interrupt a process running CPU in the middle of the execution. Instead, it waits till the
process completes its CPU burst time, and then after that it can allocate the CPU to any
other process. Some Algorithms based on non-preemptive scheduling are: Shortest Job
First (SJF basically non-preemptive) Scheduling and Priority (non- preemptive version)
scheduling, etc.
 Preemptive Scheduling: In this type of Scheduling, the tasks are usually assigned with
priorities. At times it is necessary to run a certain task that has a higher priority before
another task although it is running. Therefore, the running task is interrupted for some time
and resumed later when the priority task has finished its execution. Some Algorithms that
are based on preemptive scheduling are Round Robin Scheduling (RR), Shortest
Remaining Time First (SRTF), Priority (preemptive version) Scheduling, etc.
First Come First Serve (FCFS)
The process that requests the services of CPU first, get the CPU first. This is the philosophy used
by the first come first serve algorithm..
Important Points
 It is non-preemptive
 Average waiting time is not optimal
 Cannot utilize resources in parallel
Consider the following set of processes that arrive at time 0, with the length of the CPU burst given
in milliseconds:
If the processes arrive in the order P1, P2, P3, and are served in FCFS order, we get the result
shown in the following Gantt chart, which is a bar chart that illustrates a particular schedule,
including the start and finish times of each of the participating processes:
The waiting time is 0 milliseconds for process P1, 24 milliseconds for process P2, and 27
milliseconds for process P3. Thus, the average waiting time is (0 + 24 + 27)/3 = 17 milliseconds.
If the processes arrive in the order P2, P3, P1, however, the results will be as shown in the
following Gantt chart:
The average waiting time is now (6 + 0 + 3)/3 = 3 milliseconds. This reduction is substantial.
Thus, the average waiting time under an FCFS policy is generally not minimal and may vary
substantially if the processes’ CPU burst times vary greatly.
Sample Code
#include<stdio.h>
int main()
{
int n,bt[20],wt[20],tat[20],avwt=0,avtat=0,i,j;
printf("Enter total number of processes(maximum 20):");
scanf("%d",&n);
printf("Enter Process Burst Time n");
for(i=0;i<n;i++)
{
printf("P[%d]:",i);
scanf("%d",&bt[i]);
}
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
}
printf("Process Burst Time Waiting Time
Turnaround Time");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
avwt+=wt[i];
avtat+=tat[i];
printf("nP[%d] %d %d
%d",i,bt[i],wt[i],tat[i]);
}
avwt/=i;
avtat/=i;
printf("nAverage Waiting Time:%d",avwt);
printf("nAverage Turnaround Time:%d",avtat);
return 0;
}
Sample Output
Enter total number of processes(maximum 20):3
Enter Process Burst Time
P[0]:24
P[1]:3
P[2]:3
Process Burst Time Waiting Time Turnaround Time
P[0] 24 0 24
P[1] 3 24 27
P[2] 3 27 30
Average Waiting Time:17
Average Turnaround Time:27
Shortest Job First (SJF) Scheduling Algorithm
SJF 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. The full form of SJF is Shortest
Job First.
Sample Code
#include<stdio.h>
int main()
{
int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp;
float avg_wt,avg_tat;
printf("Enter number of process:");
scanf("%d",&n);
printf("nEnter Burst Time:n");
for(i=0;i<n;i++)
{
printf("p%d:",i);
scanf("%d",&bt[i]);
p[i]=i;
}
//sorting of burst times
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(bt[j]<bt[pos])
pos=j;
}
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
}
avg_wt=(float)total/n;
total=0;
printf("Process Burst Time Waiting Time
Turnaround Time");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
total+=tat[i];
printf("np%d %d %d
%d",p[i],bt[i],wt[i],tat[i]);
}
avg_tat=(float)total/n;
printf("nAverage Waiting Time=%f",avg_wt);
printf("nAverage Turnaround Time=%f",avg_tat);
}
Sample Output
Enter number of process:4
Enter Burst Time:
p0:6
p1:8
p2:7
p3:3
Process Burst Time Waiting Time Turnaround Time
p3 3 0 3
p0 6 3 9
p2 7 9 16
p1 8 16 24
Average Waiting Time=7.000000
Average Turnaround Time=13.000000

More Related Content

What's hot

What's hot (20)

OSCh6
OSCh6OSCh6
OSCh6
 
CPU scheduling algorithms in OS
CPU scheduling algorithms in OSCPU scheduling algorithms in OS
CPU scheduling algorithms in OS
 
05 lcd slides 1 - CPU SCHEDULING (Powerpoint)
05 lcd slides 1 - CPU SCHEDULING (Powerpoint)05 lcd slides 1 - CPU SCHEDULING (Powerpoint)
05 lcd slides 1 - CPU SCHEDULING (Powerpoint)
 
Comparison Analysis of CPU Scheduling : FCFS, SJF and Round Robin
Comparison Analysis of CPU Scheduling : FCFS, SJF and Round RobinComparison Analysis of CPU Scheduling : FCFS, SJF and Round Robin
Comparison Analysis of CPU Scheduling : FCFS, SJF and Round Robin
 
Cpu scheduling
Cpu schedulingCpu scheduling
Cpu scheduling
 
Scheduling algorithms
Scheduling algorithmsScheduling algorithms
Scheduling algorithms
 
Os prj ppt
Os prj pptOs prj ppt
Os prj ppt
 
Scheduling algo(by HJ)
Scheduling algo(by HJ)Scheduling algo(by HJ)
Scheduling algo(by HJ)
 
Chapter6
Chapter6Chapter6
Chapter6
 
Cpu Scheduling Galvin
Cpu Scheduling GalvinCpu Scheduling Galvin
Cpu Scheduling Galvin
 
Os module 2 ba
Os module 2 baOs module 2 ba
Os module 2 ba
 
CPU Scheduling
CPU SchedulingCPU Scheduling
CPU Scheduling
 
CPU Scheduling Algorithm (SJF, Round-Robin, Priority)
CPU Scheduling Algorithm (SJF, Round-Robin, Priority)CPU Scheduling Algorithm (SJF, Round-Robin, Priority)
CPU Scheduling Algorithm (SJF, Round-Robin, Priority)
 
9 cm402.19
9 cm402.199 cm402.19
9 cm402.19
 
CPU Scheduling Algorithms
CPU Scheduling AlgorithmsCPU Scheduling Algorithms
CPU Scheduling Algorithms
 
Process and CPU scheduler
Process and CPU schedulerProcess and CPU scheduler
Process and CPU scheduler
 
Scheduling
SchedulingScheduling
Scheduling
 
Ch05
Ch05Ch05
Ch05
 
5 Process Scheduling
5 Process Scheduling5 Process Scheduling
5 Process Scheduling
 
Process Scheduling
Process SchedulingProcess Scheduling
Process Scheduling
 

Similar to Fcfs and sjf

Operating Systems Third Unit - Fourth Semester - Engineering
Operating Systems Third Unit  - Fourth Semester - EngineeringOperating Systems Third Unit  - Fourth Semester - Engineering
Operating Systems Third Unit - Fourth Semester - EngineeringYogesh Santhan
 
CPU scheduling
CPU schedulingCPU scheduling
CPU schedulingAmir Khan
 
Operating System-Process Scheduling
Operating System-Process SchedulingOperating System-Process Scheduling
Operating System-Process SchedulingShipra Swati
 
Preemptive process example.pptx
Preemptive process example.pptxPreemptive process example.pptx
Preemptive process example.pptxjamilaltiti1
 
Process management in os
Process management in osProcess management in os
Process management in osMiong Lazaro
 
Operating System Scheduling
Operating System SchedulingOperating System Scheduling
Operating System SchedulingVishnu Prasad
 
Process Scheduling Algorithms.pdf
Process Scheduling Algorithms.pdfProcess Scheduling Algorithms.pdf
Process Scheduling Algorithms.pdfRakibul Rakib
 
Cpu scheduling in operating System.
Cpu scheduling in operating System.Cpu scheduling in operating System.
Cpu scheduling in operating System.Ravi Kumar Patel
 
chapter 5 CPU scheduling.ppt
chapter  5 CPU scheduling.pptchapter  5 CPU scheduling.ppt
chapter 5 CPU scheduling.pptKeyreSebre
 
OS Process Chapter 3.pdf
OS Process Chapter 3.pdfOS Process Chapter 3.pdf
OS Process Chapter 3.pdfKp Sharma
 
Operating System 5
Operating System 5Operating System 5
Operating System 5tech2click
 
Ch6
Ch6Ch6
Ch6C.U
 

Similar to Fcfs and sjf (20)

Operating Systems Third Unit - Fourth Semester - Engineering
Operating Systems Third Unit  - Fourth Semester - EngineeringOperating Systems Third Unit  - Fourth Semester - Engineering
Operating Systems Third Unit - Fourth Semester - Engineering
 
CPU scheduling
CPU schedulingCPU scheduling
CPU scheduling
 
Operating System-Process Scheduling
Operating System-Process SchedulingOperating System-Process Scheduling
Operating System-Process Scheduling
 
Preemptive process example.pptx
Preemptive process example.pptxPreemptive process example.pptx
Preemptive process example.pptx
 
Process management in os
Process management in osProcess management in os
Process management in os
 
Operating System Scheduling
Operating System SchedulingOperating System Scheduling
Operating System Scheduling
 
Cpu scheduling
Cpu schedulingCpu scheduling
Cpu scheduling
 
Process Scheduling Algorithms.pdf
Process Scheduling Algorithms.pdfProcess Scheduling Algorithms.pdf
Process Scheduling Algorithms.pdf
 
Cpu scheduling in operating System.
Cpu scheduling in operating System.Cpu scheduling in operating System.
Cpu scheduling in operating System.
 
chapter 5 CPU scheduling.ppt
chapter  5 CPU scheduling.pptchapter  5 CPU scheduling.ppt
chapter 5 CPU scheduling.ppt
 
Unit 2 notes
Unit 2 notesUnit 2 notes
Unit 2 notes
 
OS Process Chapter 3.pdf
OS Process Chapter 3.pdfOS Process Chapter 3.pdf
OS Process Chapter 3.pdf
 
OS_Ch6
OS_Ch6OS_Ch6
OS_Ch6
 
Operating System 5
Operating System 5Operating System 5
Operating System 5
 
Ch5
Ch5Ch5
Ch5
 
ERTS UNIT 5.pptx
ERTS UNIT 5.pptxERTS UNIT 5.pptx
ERTS UNIT 5.pptx
 
Os unit 2
Os unit 2Os unit 2
Os unit 2
 
Ch6
Ch6Ch6
Ch6
 
exp 3.docx
exp 3.docxexp 3.docx
exp 3.docx
 
CH06.pdf
CH06.pdfCH06.pdf
CH06.pdf
 

More from A. S. M. Shafi (20)

2D Transformation in Computer Graphics
2D Transformation in Computer Graphics2D Transformation in Computer Graphics
2D Transformation in Computer Graphics
 
3D Transformation in Computer Graphics
3D Transformation in Computer Graphics3D Transformation in Computer Graphics
3D Transformation in Computer Graphics
 
Projection
ProjectionProjection
Projection
 
2D Transformation
2D Transformation2D Transformation
2D Transformation
 
Line drawing algorithm
Line drawing algorithmLine drawing algorithm
Line drawing algorithm
 
Fragmentation
FragmentationFragmentation
Fragmentation
 
File organization
File organizationFile organization
File organization
 
Bankers algorithm
Bankers algorithmBankers algorithm
Bankers algorithm
 
RR and priority scheduling
RR and priority schedulingRR and priority scheduling
RR and priority scheduling
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
 
1D Array
1D Array1D Array
1D Array
 
2D array
2D array2D array
2D array
 
Stack push pop
Stack push popStack push pop
Stack push pop
 
Queue
QueueQueue
Queue
 
Searching
SearchingSearching
Searching
 
Sorting
SortingSorting
Sorting
 
Linked list
Linked listLinked list
Linked list
 
Sum of subset problem
Sum of subset problemSum of subset problem
Sum of subset problem
 
Quick sort
Quick sortQuick sort
Quick sort
 
N queens problem
N queens problemN queens problem
N queens problem
 

Recently uploaded

Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 

Recently uploaded (20)

9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 

Fcfs and sjf

  • 1. CPU Scheduling CPU Scheduling is a process of determining which process will own CPU for execution while another process is on hold. The main task of CPU scheduling is to make sure that whenever the CPU remains idle, the OS at least select one of the processes available in the ready queue for execution. The selection process will be carried out by the CPU scheduler. It selects one of the processes in memory that are ready for execution. Important CPU scheduling Terminologies  Burst Time/Execution Time: It is a time required by the process to complete execution. It is also called running time.  Arrival Time: when a process enters in a ready state  Finish Time: when process complete and exit from a system  Multiprogramming: A number of programs which can be present in memory at the same time.  Jobs: It is a type of program without any kind of user interaction.  User: It is a kind of program having user interaction.  Process: It is the reference that is used for both job and user.  CPU/IO burst cycle: Characterizes process execution, which alternates between CPU and I/O activity. CPU times are usually shorter than the time of I/O. CPU Scheduling Criteria A CPU scheduling algorithm tries to maximize and minimize the following:
  • 2. Maximize:  CPU utilization: CPU utilization is the main task in which the operating system needs to make sure that CPU remains as busy as possible. It can range from 0 to 100 percent. However, for the RTOS, it can be range from 40 percent for low-level and 90 percent for the high-level system.  Throughput: The number of processes that finish their execution per unit time is known Throughput. So, when the CPU is busy executing the process, at that time, work is being done, and the work completed per unit time is called Throughput. Minimize:  Waiting time: Waiting time is the total time spent by the process in the ready state waiting for CPU. For example, consider the arrival time of all the below 3 processes to be 0 ms, 0 ms, and 2 ms. Then the waiting time for all the 3 processes will be:  P1: 0 ms  P2: 8 ms because P2 have to wait for the complete execution of P1 and arrival time of P2 is 0 ms.  P3: 13 ms becuase P3 will be executed after P1 and P2 i.e. after 8+7 = 15 ms and the arrival time of P3 is 2 ms. So, the waiting time of P3 will be: 15-2 = 13 ms. Waiting time = Turnaround time - Burst time
  • 3.  Response time: Response time is the time spent when the process is in the ready state and gets the CPU for the first time. For example, consider the below processes: Here, the response time of all the 3 processes are:  P1: 0 ms  P2: 7 ms because the process P2 have to wait for 8 ms during the execution of P1 and then after it will get the CPU for the first time. Also, the arrival time of P2 is 1 ms. So, the response time will be 8-1 = 7 ms.  P3: 13 ms because the process P3 have to wait for the execution of P1 and P2 i.e. after 8+7 = 15 ms, the CPU will be allocated to the process P3 for the first time. Also, the arrival of P3 is 2 ms. So, the response time for P3 will be 15-2 = 13 ms. Response time = Time at which the process gets the CPU for the first time - Arrival time  Turnaround Time: Turnaround time is the total amount of time spent by the process from coming in the ready state for the first time to its completion. Turnaround time = Burst time + Waiting time Types of CPU scheduling Algorithm  Non-Preemptive Scheduling: Under non-preemptive scheduling, once the CPU has been allocated to a process, the process keeps the CPU until it releases the CPU either by terminating or by switching to the waiting state. In non-preemptive scheduling, it does not interrupt a process running CPU in the middle of the execution. Instead, it waits till the process completes its CPU burst time, and then after that it can allocate the CPU to any other process. Some Algorithms based on non-preemptive scheduling are: Shortest Job First (SJF basically non-preemptive) Scheduling and Priority (non- preemptive version) scheduling, etc.
  • 4.  Preemptive Scheduling: In this type of Scheduling, the tasks are usually assigned with priorities. At times it is necessary to run a certain task that has a higher priority before another task although it is running. Therefore, the running task is interrupted for some time and resumed later when the priority task has finished its execution. Some Algorithms that are based on preemptive scheduling are Round Robin Scheduling (RR), Shortest Remaining Time First (SRTF), Priority (preemptive version) Scheduling, etc.
  • 5. First Come First Serve (FCFS) The process that requests the services of CPU first, get the CPU first. This is the philosophy used by the first come first serve algorithm.. Important Points  It is non-preemptive  Average waiting time is not optimal  Cannot utilize resources in parallel Consider the following set of processes that arrive at time 0, with the length of the CPU burst given in milliseconds: If the processes arrive in the order P1, P2, P3, and are served in FCFS order, we get the result shown in the following Gantt chart, which is a bar chart that illustrates a particular schedule, including the start and finish times of each of the participating processes:
  • 6. The waiting time is 0 milliseconds for process P1, 24 milliseconds for process P2, and 27 milliseconds for process P3. Thus, the average waiting time is (0 + 24 + 27)/3 = 17 milliseconds. If the processes arrive in the order P2, P3, P1, however, the results will be as shown in the following Gantt chart: The average waiting time is now (6 + 0 + 3)/3 = 3 milliseconds. This reduction is substantial. Thus, the average waiting time under an FCFS policy is generally not minimal and may vary substantially if the processes’ CPU burst times vary greatly. Sample Code #include<stdio.h> int main() { int n,bt[20],wt[20],tat[20],avwt=0,avtat=0,i,j; printf("Enter total number of processes(maximum 20):"); scanf("%d",&n); printf("Enter Process Burst Time n"); for(i=0;i<n;i++) { printf("P[%d]:",i); scanf("%d",&bt[i]); } wt[0]=0; for(i=1;i<n;i++) { wt[i]=0; for(j=0;j<i;j++)
  • 7. wt[i]+=bt[j]; } printf("Process Burst Time Waiting Time Turnaround Time"); for(i=0;i<n;i++) { tat[i]=bt[i]+wt[i]; avwt+=wt[i]; avtat+=tat[i]; printf("nP[%d] %d %d %d",i,bt[i],wt[i],tat[i]); } avwt/=i; avtat/=i; printf("nAverage Waiting Time:%d",avwt); printf("nAverage Turnaround Time:%d",avtat); return 0; } Sample Output Enter total number of processes(maximum 20):3 Enter Process Burst Time P[0]:24 P[1]:3 P[2]:3 Process Burst Time Waiting Time Turnaround Time P[0] 24 0 24 P[1] 3 24 27 P[2] 3 27 30 Average Waiting Time:17
  • 8. Average Turnaround Time:27 Shortest Job First (SJF) Scheduling Algorithm SJF 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. The full form of SJF is Shortest Job First. Sample Code #include<stdio.h> int main() { int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp; float avg_wt,avg_tat; printf("Enter number of process:"); scanf("%d",&n); printf("nEnter Burst Time:n");
  • 9. for(i=0;i<n;i++) { printf("p%d:",i); scanf("%d",&bt[i]); p[i]=i; } //sorting of burst times for(i=0;i<n;i++) { pos=i; for(j=i+1;j<n;j++) { if(bt[j]<bt[pos]) pos=j; } temp=bt[i]; bt[i]=bt[pos]; bt[pos]=temp; temp=p[i]; p[i]=p[pos]; p[pos]=temp; } wt[0]=0; for(i=1;i<n;i++) { wt[i]=0; for(j=0;j<i;j++) wt[i]+=bt[j];
  • 10. total+=wt[i]; } avg_wt=(float)total/n; total=0; printf("Process Burst Time Waiting Time Turnaround Time"); for(i=0;i<n;i++) { tat[i]=bt[i]+wt[i]; total+=tat[i]; printf("np%d %d %d %d",p[i],bt[i],wt[i],tat[i]); } avg_tat=(float)total/n; printf("nAverage Waiting Time=%f",avg_wt); printf("nAverage Turnaround Time=%f",avg_tat); } Sample Output Enter number of process:4 Enter Burst Time: p0:6 p1:8 p2:7 p3:3 Process Burst Time Waiting Time Turnaround Time p3 3 0 3 p0 6 3 9 p2 7 9 16 p1 8 16 24
  • 11. Average Waiting Time=7.000000 Average Turnaround Time=13.000000