0% found this document useful (0 votes)
20 views49 pages

Chapter 3 CPU Sch (2)

The document discusses CPU scheduling concepts, including basic principles, scheduling criteria, and various algorithms such as First-Come, First-Served (FCFS), Shortest Job First (SJF), and Round Robin (RR). It outlines the roles of CPU schedulers and dispatchers, as well as the importance of metrics like CPU utilization, turnaround time, and waiting time. Additionally, it covers the challenges of scheduling, such as starvation and the need for aging in priority scheduling.

Uploaded by

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

Chapter 3 CPU Sch (2)

The document discusses CPU scheduling concepts, including basic principles, scheduling criteria, and various algorithms such as First-Come, First-Served (FCFS), Shortest Job First (SJF), and Round Robin (RR). It outlines the roles of CPU schedulers and dispatchers, as well as the importance of metrics like CPU utilization, turnaround time, and waiting time. Additionally, it covers the challenges of scheduling, such as starvation and the need for aging in priority scheduling.

Uploaded by

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

CPU Scheduling

• Basic Concepts
• Scheduling Criteria
• Scheduling Algorithms
• Multiple-Processor Scheduling
• Real-Time Scheduling
• Algorithm Evaluation

Operating System Concepts


Basic Concepts
• Maximum CPU utilization obtained with
multiprogramming
• CPU–I/O Burst Cycle – Process execution
consists of a cycle of CPU execution and I/O
wait.
• CPU burst distribution

Operating System Concepts


CPU Scheduler
• Selects from among the processes in memory
that are ready to execute, and allocates the CPU
to one of them.
• CPU scheduling decisions may take place when a
process:
1. Switches from running to waiting state.
2. Switches from running to ready state.
3. Switches from waiting to ready.
4. Terminates.
• Scheduling under 1 and 4 is nonpreemptive.
• All other scheduling is preemptive.

Operating System Concepts


• Preemptive : Taking CPU from currently
executing process forcefully and assigning it to
new process(high priority process)
• Non- Preemptive: allowing currently executing
process to continue its execution even if
higher priority process comes.

Operating System Concepts


Dispatcher
• Dispatcher module gives control of the CPU to the
process selected by the short-term scheduler; this
involves:
– switching context
– switching to user mode
– jumping to the proper location in the user program to
restart that program
• Dispatch latency – time it takes for the dispatcher to
stop one process and start another running.

Operating System Concepts


Scheduling Criteria
• CPU utilization – keep the CPU as busy as possible
• Throughput – # of processes that complete their
execution per time unit
• Turnaround time – amount of time to execute a
particular process
• Waiting time – amount of time a process has been
waiting in the ready queue
• Response time – amount of time it takes from
when a request was submitted until the first
response is produced, not output (for time-
sharing environment)

Operating System Concepts


Optimization Criteria
• Max CPU utilization
• Max throughput
• Min turnaround time
• Min waiting time
• Min response time

Operating System Concepts


First-Come, First-Served (FCFS) Scheduling
Process Burst Time AT
P1 24 0
P2 3 0
P3 3 0
• Suppose that the processes arrive in the order: P1 , P2 , P3
The Gantt Chart for the schedule is:
P1 P2 P3

2 2 3
0
4 7 0

• Waiting time(ST-AT) for P1 = 0; P2 = 24; P3 = 27


• Average waiting time: (0 + 24 + 27)/3 = 17
• Turn around time(FT-AT): P1=24-0=24
• P2= 27-0=27
• P3= 30-0=30
• AVG TAT = (24+27+30)/3 =Operating
27 System Concepts
Suppose that the processes arrive in the order
P2 , P3 , P1 .
• The Gantt chart for the schedule is:

P2 P3 P1

3
0 3 6
0
• Waiting time for P1 = 6; P2 = 0; P3 = 3
• Average waiting time: (6 + 0 + 3)/3 = 3
• Much better than previous case.
• Convoy effect short process behind long
process Operating System Concepts
Process BT AT
P1 7 0
P2 5 1
P3 2 2
P4 1 3

Process BT AT
P1 3 0
P2 5 1
P3 8 5
P4 4 5
Operating System Concepts
Process BT AT P
P1 3 0 4
P2 2 0 5
P3 8 3 3
P4 4 6 1

Process BT AT P
P1 ` 3 0 4
P2 5 1 3
P3 8 3 2
P4 4 5 1
Operating System Concepts
Shortest-Job-First (SJR) Scheduling
• Associate with each process the length of its
next CPU burst. Use these lengths to schedule
the process with the shortest time.
• Two schemes:
– nonpreemptive – once CPU given to the process it
cannot be preempted until completes its CPU burst.
– preemptive – if a new process arrives with CPU
burst length less than remaining time of current
executing process, preempt. This scheme is known
as the Shortest-Remaining-Time-First (SRTF).

Operating System Concepts


SJF is optimal – gives minimum average waiting
time for a given set of processes.
ProcessArrival Time Burst Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4

Operating System Concepts


Process Arrival Time Burst Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
• SJF (preemptive)

Operating System Concepts


Determining Length of Next CPU
Burst
• Can only estimate the length.
• Can be done by using the length of previous
CPU bursts, using exponential averaging.

Operating System Concepts


Prediction of the Length of the Next CPU Burst

Operating System Concepts


Examples of Exponential Averaging
• α =0
– τn+1 = τn
– Recent history does not count.
• α =1
– τn+1 = tn
– Only the actual last CPU burst counts.
• If we expand the formula, we get:
τn+1 = α tn+(1 - α) α tn -1 + …
+(1 - α )j α tn -1 + …
+(1 - α )n=1 tn τ0

Operating System Concepts


• Since both α and (1 - α) are less than or equal to 1, each
successive term has less weight than its predecessor.
Priority Scheduling
• A priority number (integer) is associated with each
process
• The CPU is allocated to the process with the highest
priority (smallest integer ≡ highest priority).
– Preemptive
– nonpreemptive
• SJF is a priority scheduling where priority is the
predicted next CPU burst time.
• Problem ≡ Starvation – low priority processes may
never execute.
• Solution ≡ Aging – as time progresses increase the
priority of the process.
Operating System Concepts
BT AT P
P1 4 0 3
P2 6 1 2
P3 3 4 1

BT AT P
P1 2 0 3
P2 2 3 2
P3 3 2 1
P4 4 3 2

Operating System Concepts


Round Robin (RR)
• Each process gets a small unit of CPU time
(time quantum), usually 10-100 milliseconds.
After this time has elapsed, the process is
preempted and added to the end of the ready
queue.
• If there are n processes in the ready queue
and the time quantum is q, then each process
gets 1/n of the CPU time in chunks of at most
q time units at once. No process waits more
than (n-1)q time units.
• Performance
– q large ⇒ FIFO
– q small ⇒ q must be large with respect to context
switch, otherwise overhead is too high.
Example of RR with Time
Quantum = 20
Process Burst Time
P1 53
P2 17
P3 68
P4 24
P1 P2 P3 P4 P1 P3 P4 P1 P3 P3
2 3 5 7 9 11 12 13 15 16
0
0 7 7 7 7 7 1 4 4 2

• The Gantt chart is:


CPU Scheduling Algorithms(FCFS)
#include <stdio.h>
int main()
{int pid[15];
int bt[15];
int n;
printf("Enter the number of processes: ");
scanf("%d", &n);
printf("Enter process id of all the processes: ");
for(int i=0;i<n;i++)
scanf("%d", &pid[i]);
printf("Enter burst time of all the processes: ");
for(int i=0;i<n;i++)
scanf("%d", &bt[i]);
int i, wt[n];
wt[0]=0;

/
/for calculating waiting time of each process
for(i=1; i<n; i++)
{
wt[i]= bt[i-1]+ wt[i-1];}
printf("Process ID Burst Time Waiting Time TurnAround
Time\n");
float twt=0.0;
float tat= 0.0;
for(i=0; i<n; i++)
{printf("%d\t\t", pid[i]);
printf("%d\t\t", bt[i]);
printf("%d\t\t", wt[i]); }
//calculating and printing turnaround time of each process
printf("%d\t\t", bt[i]+wt[i]);
printf("\n");
//for calculating total waiting time
twt += wt[i];

//for calculating total turnaround time


tat += (wt[i]+bt[i]);
}
float att, awt;

//for calculating average waiting time


awt = twt/n;

//for calculating average turnaround time


att = tat/n;
printf("Avg. waiting time=%f\n", awt);
printf("Avg. turnaround time= %f", att);
}
Process Arrival Time Burst Time
P1 0 5
P2 0 11
P3 0 11
#include<stdio.h>
SJF SCHEDULING
int main()
{
int bt[20],p[20],wt[20],tat[20],i, j, n,
total=0,totalT=0,pos,temp;
float avg_wt, avg_tat;
printf("Enter number of process:");
scanf("%d",&n);

printf("\n Enter Burst Time:\n");


for(i=0;i<n;i++)
{ printf("p%d:",i+1);
scanf("%d", &bt[i]);
p[i]=i+1;}
//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;

//finding the waiting time of all the processes


for(i=1;i<n;i++)
{wt[i]=0;
for(j=0;j<i;j++)
//individual WT by adding BT of all previous
completed processes
wt[i]+=bt[j];
//total waiting time
total+=wt[i];
}
//average waiting time
avg_wt=(float)total/n;

printf("\nProcess\t Burst Time \tWaiting Time\


tTurnaround Time");
for(i=0;i<n;i++)
{
//turnaround time of individual processes
tat[i]=bt[i]+wt[i];
//total turnaround time
totalT+=tat[i];
printf("\np%d\t\t %d\t\t %d\t\t\t
%d",p[i],bt[i],wt[i],tat[i]);
}//average turnaround time
avg_tat=(float)totalT/n;
printf("\n\nAverage Waiting Time=%f",avg_wt);
printf("\nAverage Turnaround Time=%f",avg_tat);
}
Process Arrival Time Burst Time
P1 0 5
P2 0 4
P3 0 12
P4 0 7
FCFS
void sort()
struct Process
{ struct Process t;
{
int i, j;
char name[5];
for(i=0;i<n;i++)
int bt, wt, at, tat;
for(j=0;j<n-i-1;j++)
}p[10];
if(p[j].at > p[j+1].at)
{ t=
void accept()
p[j];
{ …..}
p[j] = p[j+1];
void display()
p[j+1] = t;
{ ….. }
}
}
int arrived(int time)
{ int i;
for(i=0;i<n;i++)
if(p[i].at<=time && p[i].bt!=0)
return i;
return 0;
}
void processinput()
for(i=0;i<n;i++)
{ int i=0,time=0;
{ p[i].tat=p[i].ct-p[i].at;
finish=k=0;
p[i].wt=p[i].tat-p[i].bt;
while(finish!=n)
tot_tat +=p[i].tat;
{ if(i=arrived(time))
tot_wt +=p[i].wt;
{ time=
}
time+p[i].bt;
avg_wt=tot_wt/n;
p[i].bt=0;
avg_tat=tot_tat/n;
p[i].ct=time;
}
finish++;
}
}

Operating System Concepts


Non-Preemptive SJF
int getmin(int t)
{ int i,mini,min=99;
for(i=0;i<n;i++)
if(p[i].at<=t && p[i].bt!=0 && p[i].bt<min)
{ min = p[i].bt;
mini = i;
}
return mini;
}
void processinput()
for(i=0;i<no;i++)
{ int i;
{ p[i].tat=p[i].ct-p[i].at;
finish=k=0;
p[i].wt=p[i].tat-p[i].bt;
while(finish!=n)
tot_tat +=p[i].tat;
{ if(arrived(time))
tot_wt +=p[i].wt;
{ i= getmin(time);
}
time= time+p[i].bt;
avg_wt=tot_wt/no;
p[i].bt=0;
avg_tat=tot_tat/no;
p[i].ct=time;
}
finish++;
}
}

Operating System Concepts


Non-Preemptive Priority
struct Process
{
int name;
int bt,wt,at,tat,pr;
}p[10];
int getmin(int t)
{ int i,mini,min=99;
for(i=0;i<n;i++)
if(p[i].at<=t && p[i].bt!=0 && p[i].Pr<min)
{ min = p[i].Pr;
mini = i;
}
return mini;
}
void processinput()
for(i=0;i<no;i++)
{ int i;
{ p[i].tat=p[i].ct-p[i].at;
finish=k=0,time=0;
p[i].wt=p[i].tat-p[i].bt;
while(finish!=n)
tot_tat +=p[i].tat;
{ if(arrived(time))
tot_wt +=p[i].wt;
{ i= getmin(time);
}
time= time+p[i].bt;
avg_wt=tot_wt/no;
p[i].bt=0;
avg_tat=tot_tat/no;
p[i].ct=time;
}
finish++;
}
}

Operating System Concepts


Preemptive SJF and Preemptive
Priority

Operating System Concepts


void processinput()
Else
{ int i,time=0;
{ time++;
finish=k=0;
} }
while(finish!=n)
for(i=0;i<no;i++)
{ if(arrived(time))
{ p[i].tat=p[i].ct-p[i].at;
{ i = getmin(time);
p[i].wt=p[i].tat-p[i].bt;
time++;
tot_tat +=p[i].tat;
p[i].bt--;
tot_wt +=p[i].wt;
p[i].ct=time;
}
if(p[i].bt==0)
avg_wt=tot_wt/no;
{
avg_tat=tot_tat/no;
finish++;
}
}
}

Operating System Concepts


ROUND ROBIN

Operating System Concepts


void processinput()
Else
{ int i=0;
finish=k=0;
{ time++;
while(finish!=n) }
{ if(i= arrived(time)) i=(i+1)%n;
{ if(p[i].bt >= TQ) }
{ time+=TQ; for(i=0;i<no;i++)
{ p[i].tat=p[i].ct-p[i].at;
p[i].bt=p[i].bt-TQ;
p[i].wt=p[i].tat-p[i].bt;
}
tot_tat +=p[i].tat;
else
{ tot_wt +=p[i].wt;
time+=p[i].bt; }
p[i].bt=0; avg_wt=tot_wt/no;
} avg_tat=tot_tat/no;
if(p[i].bt==0) }
{
p[i].ct=time; Operating System Concepts
BT AT P
P1 4 0 3
P2 3 2 1
P3 7 1 4
P4 15 3 2

Operating System Concepts


Operating System Concepts
Operating System Concepts
Operating System Concepts
Operating System Concepts
Operating System Concepts
Operating System Concepts

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy