0% found this document useful (0 votes)
59 views

Process Scheduling - Module2

The document discusses CPU scheduling concepts and algorithms. It begins by explaining that CPU scheduling allows multiple processes to share a CPU by switching between them rapidly, improving productivity. Common CPU scheduling algorithms include first-come, first-served (FCFS), shortest job first (SJF), and priority scheduling. FCFS considers processes in the order they arrive, while SJF selects the process with the shortest estimated CPU burst time. The goal of scheduling is to maximize CPU utilization and minimize average wait times.

Uploaded by

Shreya shresth
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Process Scheduling - Module2

The document discusses CPU scheduling concepts and algorithms. It begins by explaining that CPU scheduling allows multiple processes to share a CPU by switching between them rapidly, improving productivity. Common CPU scheduling algorithms include first-come, first-served (FCFS), shortest job first (SJF), and priority scheduling. FCFS considers processes in the order they arrive, while SJF selects the process with the shortest estimated CPU burst time. The goal of scheduling is to maximize CPU utilization and minimize average wait times.

Uploaded by

Shreya shresth
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 61

Process Scheduling

Module 2
Introduction

● CPU Scheduling is the basis of multiprogrammed OS

● By switching the CPU among processes, the OS can make the computer
more productive

● In this chapter we discuss the basic CPU scheduling concepts , different


Scheduling algorithms and also consider the problem of selecting an
algorithm for a particular system
Basic concepts
● In a single processor system, only one process can run at a time and others
has to wait for the CPU. When that process needs to perform I/O request the
CPU may sit idle and all this waiting time may get wasted.
● In multiprogramming, the maximum CPU utilization is obtained by,
1. Keeping several processes in memory at one time.
2. Every time a running process has to wait, another process can take over use of
the CPU
3. Short-term scheduler will assign the process to the processer.

● The problem of determining when processors should be assigned and to which


processes is called processor scheduling or CPU scheduling.
Continued

● When more than one process is runnable, the operating system must decide
which one first.

● The part of the operating system concerned with the above decision is called
the scheduler

● Algorithm used to achive the above decision is called the scheduling


algorithm.
CPU-I/O Burst Cycle

● The success of CPU Scheduling depends on an observed property of


processes

● Process execution consists of a cycle of a CPU time burst and an I/O time
burst (i.e. wait).

● Processes alternate between these two states (i.e., CPU burst and I/O
burst). Eventually, the final CPU burst ends with a system request to
terminate execution
Figure: Alternating sequence of CPU and I/O bursts & CPU
Burst Histogram
CPU Scheduler

● Whenever the CPU becomes idle, the OS must select the processes in the
ready queue to be executed

● The selection process is carried out by the short-term scheduler (or CPU
Scheduler)

● The scheduler selects the process from the processes in memory that are
ready to execute and allocate the CPU to that process
Preemptive & Non-Preemptive Scheduling

● CPU scheduling decision may take place under the following four
circumstances:
1. When a process switches from running to waiting state.
For ex: as a result of I/O request or the invocation of wait for the termination
of one of the child processes
2. When a process switches from running to ready state.
For ex: when an interrupt occurs
3. When a process switches from waiting to ready state.
For example at completion of I/O
4. When a processes switches from running to terminated state.
Continued

● For circumstances 1 and 4, there is no choice in terms of scheduling. A new


process must be selected for execution - scheduling scheme is non
preemptive or cooperative

● For circumstances 2 and 3 there is a choice between preemptive and non-


preemptive scheduling

● Under nonpreemptive 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.
Continued…

● Preemptive is not allowed in following conditions


1. Processes are accessing the shared data
2. Process is executing in kernel mode
3. Interrupts occurring during crucial OS activities
Dispatcher
● The dispatcher is the module that gives control of the CPU to the process
selected by the short-term scheduler. This function involves the following:
1. Switching context
2. Switching to user mode
3. Jumping to the proper location in the user program to restart that program
● The time it takes for the dispatcher to stop one process and start another
process is called dispatch latency.
● The dispatcher needs to run as fast as possible, since it is invoked during
process context switch.
Scheduling Criteria
● Different CPU scheduling algorithms may have different properties and the choice of
a particular algorithm may favor one class of processes over another.
● Many criteria have been suggested for choosing which CPU scheduling algorithms
to use in a particular situation. They are as follows,
1. CPU utilization –The percentage of time that the CPU is busy in executing a process.
CPU utilization can range from 0 to 100 percent.
In a real systems, it should range from 40% ( for a lightly loaded system) to 90%
( for a heavily loaded system)

2. Throughput – Number of processes that are completed per time unit.


For long processes this rate is one process per hour and for short processes this rate is 10
processes per hour
Continued
3. Response time – Amount of time it takes from when a request was submitted
until the first response occurs (but not the time it takes to output the entire
response).

4. Waiting time –The sum of amount of time a process wait in ready queue
(doesn’t affect the amount of time during which a process executes or does I/O;)
5. Turnaround time – The amount of time to execute a particular process from
the time of submission through the time of completion. (completion time – arrival
time)
Scheduling Algorithm Optimization Criteria
● Max CPU utilization
● Max throughput
● Min turnaround time
● Min waiting time
● Min response time (minimize the variance)
Scheduling Algorithms
● CPU Scheduling deals with the problem of deciding which of the
processes in the ready queue is to be allocated the CPU
➢ Terminologies for CPU scheduling algorithms:
1. Arrival Time: Time at which the process arrives in the ready queue.
2. Completion Time: Time at which process completes its execution.
3. Burst Time: Time required by a process for CPU execution.
4. Turn Around Time: Time Difference between completion time and arrival time.
Turn Around Time = Completion Time – Arrival Time
5. Waiting Time(W.T): Time Difference between turn around time and burst time.
Waiting Time = Turn Around Time – Burst Time
First-Come, First-Served Scheduling

● FCFS is the very simplest scheduling algorithm.


● The process that requests the CPU first is allocated the CPU first.
● The implementation of the FCFS policy is easily managed with a FIFO queue.
When a process enters the ready queue, its PCB is linked onto the tail of the
queue.
● When the CPU is free, it is allocated to the process at the head of the queue.
● The FCFS scheduling algorithm is non-preemptive.
● FCFS may experience convoy effect where the shorter CPU burst / IO burst
processes needs to wait for one long-running process to finish using the CPU
or any device. This problem results in lower CPU and device utilization.
(shown in Ex.1)
● The higher utilization might be possible if the short processes were allowed to
run first.( shown in Ex.2)
Examples on FCFS

Ex.1 Assume processes arrives in the order P1,P2,P3 at t=0. Show the FCFS
result on Gantt chart.
Process Burst time
P1 24
P2 3
P3 3
Gantt chart:
The average waiting time= ((0-0)+(24-0)+(27-0))/3= 17 ms
The average turnaround time=((24-0)+(27-0)+(30-0))/3= 27 ms
Ex.2 Suppose that the processes arrive in the order: P2 , P3 , P1
The Gantt chart for the schedule is:

Waiting time for P1 = 6-0=6; P2 = 0-0=0; P3 = 3-0=3


Average waiting time: (6 + 0 + 3)/3 = 3ms (Much better than Ex. #1)
Average turnaround time: ( (30-0) + (3-0) + (6-0))/3 = 13ms
Shortest-Job-First Scheduling
● The SJF algorithm associates with each process the length of its next CPU
burst.
● When the CPU becomes available, it is assigned to the process that has the
smallest next CPU burst (in the case of matching bursts, FCFS is used)
● The SJF scheduling algorithm is provably optimal.
● The difficulty with the SJF algorithm is knowing the length of the next
CPU request.
● SJF algorithm cannot be implemented at the level of short-term CPU
scheduling. There is no way to know the length of the next CPU burst.
Determining Length of Next CPU Burst
● SJF algorithm Can only estimate the length of the next CPU burst– should be
similar to the previous one
○ Then pick process with shortest predicted next CPU burst
● Can be done by using the length of previous CPU bursts, using exponential
averaging
1. t n = actual length of n th CPU burst
2.  n +1 = predicted value for the next CPU burst
3.  , 0    1
4. Define :  n+=1 =  t n + (1 −  ) n .
● Commonly, α set to ½
●  =0
○ n+1 = n
○ Recent history does not count
●  =1
○ n+1 =  tn
○ Only the actual last CPU burst counts

Figure shows an exponential


average with  = 1/2 and 0= 10
● Two schemes:
1. Nonpreemptive – Once the CPU is given to the process, it cannot be preempted until
it completes its CPU burst
2. Preemptive – if a new process arrives with a CPU burst length less than the
remaining time of the current executing process, preempt. This scheme is known as
the Shortest-Remaining-Time-First (SRTF).
Process Arrival Time Burst Time
P1 0.0 6
P2 0.0 8
P3 0.0 7
P4 0.0 3

● SJF scheduling chart

P4 P1 P3 P2
0 3 9 16 24

● Average waiting time = (3 + 16 + 9 + 0) / 4 = 7ms


Process Arrival Time Burst Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5
● Preemptive SJF Gantt Chart

P1 P2 P4 P1 P3
0 1 5 10 17 26

● Average waiting time = [(10-1)+(1-1)+(17-2)+5-3)]/4 = 26/4 = 6.5 ms


Ex #1: Non-Preemptive SJF(simultaneous arrival)
Process Arrival Time Burst Time
P1 0.0 6
P2 0.0 4
P3 0.0 1
P4 0.0 5

Ex #2: Non-Preemptive SJF ( varied arrival time)


Process Arrival Time Burst Time

P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
Examples on SJF
Ex #1: Non-Preemptive SJF(simultaneous arrival)

Process Arrival Time Burst Time

P1 0.0 6
P2 0.0 4
P3 0.0 1
P4 0.0 5

The Gantt chart for the schedule is:

p1 p2 p3 p4

The average waiting time= ((10-0)+(1-0)+(0-0)+(5-0))/4= 4 ms

Average turnaround time = (1 + 5 + 10 + 16)/4 = 8ms (finish time – arrival time)


Ex #2: Non-Preemptive SJF ( varied arrival time)
Process Arrival Time Burst Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4

The Gantt chart for the schedule is:

Average waiting time = ( (0 – 0) + (8 – 2) + (7 – 4) + (12 – 5) )/4

= (0 + 6 + 3 + 7)/4 = 4ms

Average turnaround time:= ( (7 – 0) + (12 – 2) + (8 - 4) + (16 – 5))/4

= ( 7 + 10 + 4 + 11)/4 = 8ms
Ex #3: Preemptive SJF ( SRTF)
Process Arrival Time Burst Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
The Gantt chart for the schedule is:

Average waiting time


= ( [(0 – 0) + (11 - 2)] + [(2 – 2) + (5 – 4)] + (4 - 4) + (7 – 5) )/4
= 9 + 1 + 0 + 2)/4 = 3ms
Average turnaround time = [16 + (7-2) + (5-4) + (11-5)]/4 = 7.25ms
Priority Scheduling

● The SJF algorithm is a special case of the general priority scheduling algorithm.
● A priority number (integer) is associated with each process.
● The CPU is allocated to the process with the highest priority (smallest CPU burst =
highest priority)
● Priority scheduling can be either preemptive or non-preemptive.
○ A preemptive approach will preempt the CPU if the priority of the newly-arrived
process is higher than the priority of the currently running process
○ A non-preemptive approach will simply put the new process (with the highest
priority) at the head of the ready queue
Contd

● The main problem with priority scheduling is starvation, that is, low priority
processes may never get a chance to execute.
● A solution is aging. As time progresses, the priority of a process waiting for
the long time in the ready queue is increased
Problems on priority Scheduling
Ex. Consider the following set of processes arriving at t=0, in the order p1, p2,p3,p4,p5
with the burst time as shown. Using priority scheduling we would schedule these
processes as shown in the Gantt chart.(Non-preemptive)
Process Burst time Priority
P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2

Gantt chart

The average waiting time is (6-0)+(0-0)+(16-0)+(18-0)+(1-0)= 8.2 milliseconds.


Round-Robin Scheduling (preemptive)
● The round-robin (RR) scheduling algorithm is designed especially for
time sharing systems.
● It is similar to FCFS scheduling, but preemption is added to switch between
the processes.
● A small unit of time, called a time quantum or time slice is defined. A time
quantum is generally from 10 to 100 milliseconds.
● The ready queue is treated as a circular queue. The CPU scheduler goes
around the ready queue, allocating the CPU to each process for a time
interval of up to 1time quantum.
● 2 cases may arise for scheduler to pick up the next process in ready queue.
1. The process may have a CPU burst of less than 1 time quantum, where the process
itself will release the CPU voluntarily.
2. The CPU burst of the currently running process is longer than 1 time quantum, the
timer will go off and will cause an interrupt to the OS
● 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.
● The performance of the RR algorithm depends heavily on the size of the time
quantum.
1. Large time quantum -> the RR policy is same as the FCFS policy.
2. Small time quantum -> the RR approach is called processor sharing and creates
the appearance that each of n processes has its own processor running at 1/n the
speed of the real processor.
Problems on Round Robin Scheduling

Ex. Process Burst time (Assume time quantum=4ms, arrival time is t=0)
P1 24
P2 3
P3 3
The Gantt chart is:

The average waiting time is (10-4)+4+7 / 3= 5.66 milliseconds.


Average turn-around time = (30+7+10) / 3 = 15.6ms
● When the quantum size is too small, the number of context switches increases
which may be really a overhead for the system. It also increases the execution
time required and the average turnaround time.

● One rule of thumb is that 80% of the CPU bursts should be shorter than the
time quantum.
Problems on Round Robin Scheduling

Ex.2: Process Burst time (Assume time quantum=20ms, arrival time is t=0)
P1 53
P2 17
P3 68
P4 24

The Gantt chart is:


Problems on Round Robin Scheduling

Average waiting time


= ( [(0 – 0) + (77 - 20) + (121 – 97)] + (20 – 0) + [(37 – 0) + (97 - 57) + (134 – 117)] +
[(57 – 0) + (117 – 77)] ) / 4
= (0 + 57 + 24) + 20 + (37 + 40 + 17) + (57 + 40) ) / 4
= (81 + 20 + 94 + 97)/4
= 292 / 4 = 73 ms

Average turnaround time = (134 + 37 + 162 + 121) / 4 = 113.5 ms


Ex #1: Calculate the average waiting time and turn around time for the Process
P1,P2,P3,P4,P5 using FCFS, Non-preemptive SJF and Preemptive SJF algorithm
Process Arrival Time Burst Time
P1 0.0 6
P2 3.0 15
P3 5.0 3
P4 6.0 4
P5 9.0 2
Ex #2: Calculate the average waiting time and turn around time for the Process P1,P2,P3,P4
using Preemptive SJF and Priority Scheduling algorithm
Process Arrival Time Burst Time Priority
P1 0 21 3
P2 1 3 1
P3 2 6 4
P4 3 2 2
Ex #3: Calculate the average waiting time and turn around time for the Process P1,P2,P3,P4
using Priority Scheduling algorithm
Process Arrival Time Burst Time Priority
P1 0 5 1
P2 1 3 2
P3 2 8 1
P4 3 6 3

Ex #4: Calculate the average waiting time and turn around time for the Process
P1,P2,P3,P4,P5 using FCFS and Round Robin Scheduling algorithm (time quantum=2ms)
Process Arrival Time Burst Time
P1 0.0 3
P2 2.0 3
P3 3.0 2
P4 5.0 4
P5 7.0 2
Ex #5: Calculate the average waiting time and turn around time for the Process
P1,P2,P3,P4 using Round Robin Scheduling algorithm (time quantum=4ms)
Process Arrival Time Burst Time
P1 0 9
P2 1 5
P3 2 3
P4 3 4
1. Draw Gantt charts to illustrate the execution of the processes using the
following scheduling algorithm:
(1) FCFS, (2) nonpreemptive SJF, (3) preemptive SJF,
(4) nonpreemptive priority, (5) preemptive priority, and
(6) RR with time quantum=2
1. Calculate the average turnaround time when using each of the above
scheduling algorithms
2. Count the number of context switches when using each of the above
scheduling algorithms
Multilevel Queue scheduling
● Multi-level queue scheduling is used when processes can be classified into groups.
● For example, foreground (interactive) processes and background (batch)
processes
○ The two types of processes have different response-time requirements and so may have
different scheduling needs.
○ Also, foreground processes may have priority (externally defined) over background
processes.

● A multi-level queue scheduling algorithm partitions the ready queue into several
separate queues.
● The processes are permanently assigned to one queue, generally based on some
property of the process such as memory size, process priority, or process type .
Contd

● Each queue has its own scheduling algorithm

o The foreground queue might be scheduled using an RR algorithm.

o The background queue might be scheduled using an FCFS algorithm.

● In addition, there needs to be scheduling among the queues, which is


commonly implemented as fixed-priority preemptive scheduling.

● The foreground queue may have absolute priority over the background
queue.
● One example of a multi-level queue are the five queues as shown below.
● Each queue has absolute priority over lower priority queues. For example, no
process in the batch queue can run unless the queues above it are empty. And
whenever a new interactive process enters while executing batch processes, the
batch process would be preempted.
● However, this can result in starvation for the processes in the lower priority
queues.
● The solution is to introduce time slice among the queues.
● Each queue gets a certain portion of the CPU time, which it can then
schedule among its various processes.
o The foreground queue can be given 80% of the CPU time for RR
scheduling.
o The background queue can be given 20% of the CPU time for FCFS
scheduling.
Multilevel Feedback Queue scheduling
● The multilevel queue scheduling has the advantage of low scheduling
overhead, but it is inflexible. The multilevel feedback-queue scheduling
algorithm, in contrast, allows a process to move between queues.
● The idea is to separate processes according to the characteristics of their CPU
bursts. If a process uses too much CPU time,it will be moved to a lower-
priority queue. This scheme leaves I/O-bound and interactive processes in the
higher-priority queues.
● A process that waits too long in a lower-priority queue may be moved to a
higher-priority queue. This form of aging prevents starvation.
Contd

● A multilevel-feedback-queue scheduler is defined by the following


parameters:
1. Number of queues.
2. Scheduling algorithms for each queue.
3. Method used to determine when to promote a process.
4. Method used to determine when to demote a process.
5. Method used to determine which queue a process will enter when that
process needs service
● For example, consider a multilevel feedback-queue scheduler with three
queues numbered from 0 to 2 (Figure). The scheduler first executes all
processes in queue 0. Only when queue 0 is empty it will execute processes
in queue 1 and so on.
Contd
● A new job enters queue Q0 (RR) and is placed at the end. When it gains the CPU, the
job receives 8 milliseconds. If it does not finish in 8 milliseconds, the job is moved to
the end of queue Q1.
● A Q1 (RR) job receives 16 milliseconds. If it still does not complete, it is preempted
and moved to queue Q2 (FCFS).
● Processes in queue 2 are run on an FCFS basis but are run only when queues 0 and 1
are empty.
Multiple Processor scheduling
● If multiple CPUs are available, load sharing among them becomes
possible. But the scheduling problem becomes more complex.

● We consider the systems in which the processors are identical


(homogeneous) in terms of their functionality. So we can use any available
processor to run any process in the queue.

● Several concerns need to be consider (like scheduling, load balancing,


affinity etc.) in multiprocessor system

● Approaches to Multiple Processor scheduling


Two approaches: Asymmetric processing and symmetric processing.
Asymmetric multiprocessing (ASMP)
● One processor can be a master processor which can handles all scheduling
decisions, I/O processing, and other system activities.
● The other processors execute only user code.
● Because only one processor accesses the system data structures, the need for data
sharing is reduced.

Symmetric multiprocessing (SMP)


● Each processor schedules itself.
● All processes may be in a common ready queue or each processor may have its
own ready queue.
● Either way, each processor examines the ready queue and selects a process to
execute.
● Virtually all modern operating systems support SMP, including Windows XP,
Solaris, Linux, and Mac OS X.
1. Processor Affinity
● When a process has been running on a specific processor the data most
recently accessed can be populated in and out of its associated cache
memory.
● If the process migrates to another processor then the contents of cache
memory must be invalidated for the processor being migrated from, and the
cache for the processor being migrated to must be re-populated.
● This may cause high cost of invalidating and re-populating caches.
● Thus, most SMP systems try to avoid migration of processes from one
processor to another and instead attempt to keep a process running on the
same processor. This is known as processor affinity.
Contd

Processor affinity takes several forms.

● soft affinity: When an operating system has a policy of attempting to keep a


process running on the same processor but not guaranteeing that it will do so
we have a situation known as soft affinity. Here, it is possible for a process
to migrate between processors.
● hard affinity : Some systems such as Linux also provide system calls that
support hard affinity, thereby allowing a process to specify that it is not to
migrate to other processors.
2. Load Balancing

● Efficient use of the CPUs requires load balancing to keep the workload
evenly distributed.

● There are two approaches for load balancing.

○ In a Push migration approach, a specific task regularly checks the


processor loads and redistributes the waiting processes as needed.

○ In a Pull migration approach, an idle processor pulls a waiting job from


the queue of a busy processor.
3. Symmetric Multithreading
● Symmetric multiprocessing systems allow several threads to run
concurrently by providing multiple physical processors.
● An alternative approach is to provide multiple logical rather than
physical processors.Such a strategy is known as symmetric
multithreading (SMT). This is also known as hyper threading technology
● The idea behind SMT is to create multiple logical processors on the same
physical processor
o This presents a view of several logical processors to the operating system,
even on a system with a single physical processor.
o Each logical processor has its own architecture state, which includes general-
purpose and machine-state registers.
o Each logical processor is responsible for its own interrupt handling.
o However, each logical processor shares the resources of its physical
processor, such as cache memory and buses.
● SMT is a feature provided in the hardware

● Performance gain is possible with SMT architecture.


Thread Scheduling
● User level threads are managed by the thread library and the kernel is
unaware of them
● To run on a CPU. user level threads must be mapped to an associated kernel
level thread, this mapping may be indirect and may use a light weight
process
● Many-to-one and many-to-many models, thread library schedules user-
level threads to run on LWP
● We discuss scheduling issues involving user level and kernel level
threads
Contention scope
● One distinction between user level and kernel level thread lies in how they are
scheduled.
● On system, implementing many to one and many to many models, the thread library
schedules user level threads on an available LWP. This scheme is known as process
contention scope (PCS)
● Here competition for the CPU takes place among threads of the same process
● To decide which kernel thread to schedule onto a CPU, the kernel uses system
contention scope(SCS)
● Competition for the CPU with the SCS scheduling takes place among all the threads
in the system
Pthread Scheduling

● The POSIX Pthread that API allows specifying either PCS or SCS during
thread creation

● Pthread identifies the following contention scope values:

● PTHREAD SCOPE PROCESS schedules threads using PCS scheduling

● PTHREAD SCOPE SYSTEM schedules threads using SCS scheduling.


Contd
● On System implementing the many to many model, PTHREAD SCOPE PROCESS
policy schedules user level threads onto available LWPs
● The PTHREAD SCOPE SYSTEM scheduling policy will create and bind an LWP
for each user level thread on many to many systems, effectively mapping threads
using the one to one policy
● The Pthread IPC provides the two functions for getting and setting the contention
scope policy

pthread_attr_setscope( pthread_attr_t * attr, int scope)

pthread_attr_getscope( pthread_attr_t * attr, int *scope)


Contd
● The first parameter for both functions contains a pointer to the attribute
set for the thread
● The second parameter for the pthread_attr_setscope is passed either the
PTHREAD_SCOPE_SYSTEM or PTHREAD_SCOPE_PROCESS value,
indicating how the contention scope is to be set
● The second parameter for getscope contains a pointer to an int value that
is set to the current value of the contention scope
● Both functions returns non zero values on error

You might also like