unit 3 cpu scheduling algorithms
unit 3 cpu scheduling algorithms
Process in memory
When a program is loaded into the memory and it becomes a process, it can be
divided into four sections ─ stack, heap, text and data. The following image
shows a simplified layout of a process inside main memory −
When a process executes, it passes through different states. These stages may
differ in different operating systems, and the names of these states are also not
standardized.
The process address space is the set of logical addresses that a process
references in its code. For example, when 32-bit addressing is in use,
addresses can range from 0 to 0x7fffffff; that is, 2^31 possible numbers,
for a total theoretical size of 2 gigabytes.
The operating system takes care of mapping the logical addresses to physical
addresses at the time of memory allocation to the program. There are three
types of addresses used in a program before and after memory is allocated :
Symbolic addresses
The addresses used in a source code. The variable names, constants, and
instruction labels are the basic elements of the symbolic address space.
Relative addresses
Physical addresses
The loader generates these addresses at the time when a program is loaded
into main memory.
The set of all logical addresses generated by a program is referred to as
a logical address space. The set of all physical addresses corresponding to
these logical addresses is referred to as a physical address space.
1. Job queue – It helps you to store all the processes in the system.
2. Ready queue – This type of queue helps you to set every process
residing in the main memory, which is ready and waiting to execute.
3. Device queues – It is a process that is blocked because of the absence of
an I/O device.
1. Every new process first put in the Ready queue and waits until it is
selected for execution or it is dispatched.
2. One of the processes is allocated the CPU and it is executing.
3. The process should issue an I/O request
4. Then, it should be placed in the I/O queue.
5. The process should create a new subprocess
6. The process should be waiting for its termination.
7. It should remove forcefully from the CPU, as a result interrupt. Once
interrupt is completed, it should be sent back to ready queue
Scheduling Concepts
1. CPU and I/O Bursts
Typical process execution pattern: use the CPU for a while (CPU burst), then
do some I/O operations (IO burst).
CPU bound processes perform I/O operations infrequently and tend to have
long CPU bursts.
I/O bound processes spend less time doing computation and tend to have short
CPU bursts.
2. Types of CPU Scheduling
CPU scheduling decisions may take place under the following four
circumstances:
1. When a process switches from the running state to the waiting state(for
I/O request or invocation of wait for the termination of one of the child
processes).
2. When a process switches from the running state to the ready state (for
example, when an interrupt occurs).
3. When a process switches from the waiting state to the ready state(for
example, completion of I/O).
4. When a process terminates.
When Scheduling takes place only under circumstances 1 and 4, we say
the scheduling scheme is non-preemptive; otherwise the scheduling
scheme is preemptive.
Non-Preemptive Scheduling
This scheduling method is used by the Microsoft Windows 3.1 and by the
Apple Macintosh operating systems.
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.
3. Schedulers
Schedulers are special system software which handles process scheduling in
various ways. Their main task is to select the jobs to be submitted into the
system and to decide which process to run. Schedulers are of three types −
1. Long-Term Scheduler
2. Short-Term Scheduler
3. Medium-Term Scheduler
Long Term Scheduler
It selects processes from the queue and loads them into memory for
execution.
CPU scheduler selects a process among the processes that are ready to
execute and allocates CPU to one of them.
4. Dispatcher
Switching context
Switching to user mode
Jumping to the proper location in the user program to restart that program
from where it left last time.
When the scheduler switches the CPU from executing one process to execute
another, the state from the current running process is stored into the process
control block. After this, the state for the process to run next is loaded from its
own PCB and used to set the PC, registers, etc. At that point, the second process
can start executing.
Comparison among Schedulers
Scheduling Algorithms
Scheduling Criteria
1. CPU Utilization
We want to keep the CPU as busy as possible. CPU utilization may range from
0 to 100 percent. In a real system, it should range from 40 percent (for a lightly
loaded system) to 90 percent (for a heavily used system).
2. Throughput
It is the total number of processes completed per unit time or rather say total
amount of work done in a unit of time. This may range from 10/second to
1/hour depending on the specific processes.
3. Turnaround Time
The interval from the time of submission of a process to the time of completion
is the turnaround time. Turnaround time is the sum of the periods spent waiting
to get into memory, waiting in the ready queue, executing on the CPU, and
doing I/O.
4. Waiting Time
The sum of the periods spent waiting in the ready queue amount of time a
process has been waiting in the ready queue to acquire get control on the CPU.
5. Response Time
Amount of time it takes from when a request was submitted until the first
response is produced. Remember, it is the time till the first response and not the
completion of process execution (final response).
Note: In general CPU utilization and Throughput are maximized and other
factors are reduced for proper optimization.
Scheduling Algorithms
A Process Scheduler schedules different processes to be assigned to the CPU
based on particular scheduling algorithms. These algorithms are either non-
preemptive or preemptive. There are six popular process scheduling algorithms:
Shortest-Job-First(SJF) Scheduling
Priority Scheduling
of processes. First come first serve scheduling algorithm states that the
process that requests the CPU first is allocated the CPU first.
algorithm.
A perfect real life example of FCFS scheduling is buying tickets at
ticket counter.
Advantage of FCFS Scheduling
1. Simple to implement and understand.
2. Minimum overheads.
Shortest Job First scheduling works on the process with the shortest burst
This is the best approach to minimize waiting time. This is used in Batch
Systems.
It is of two types:
1. Non Pre-emptive
2. Pre-emptive
available at the same time. (either Arrival time is 0 for all, or Arrival time
This leads to the problem of Starvation, where a shorter process has to wait for
a long time until the current longer process gets executed. This happens if
shorter jobs keep coming, but this can be solved using the concept of aging.
Processes with same priority are executed on first come first served
basis.
A process is considered blocked when it is ready to run but has to wait for the
CPU as some other process is running currently.
But in case of priority scheduling if new higher priority processes keeps coming
in the ready queue then the processes waiting in the ready queue with lower
priority may have to wait for long durations before getting the CPU for
execution.
To prevent starvation of any process, we can use the concept of aging where we
keep on increasing the priority of low-priority process based on the its waiting
time.
For example, if we decide the aging factor to be 0.5 for each day of waiting,
then if a process with priority 20(which is comparitively low priority) comes in
the ready queue. After one day of waiting, its priority is increased to 19.5 and so
on.
4. Round Robin (RR)Scheduling
Round Robin is the preemptive process scheduling
algorithm
A fixed time is allotted to each process, called quantum,
for execution.
Once a process is executed for given time period that
process is preemptied and other process executes for
given time period.
Context switching is used to save states of preemptied
processes.
5. Multilevel Queue Scheduling
Multiple-level queues are not an independent scheduling
algorithm. They make use of other existing algorithms to
group and schedule jobs with common characteristics.
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. Each
queue has its own scheduling algorithm.
For example: A common division is made between
foreground (or interactive) processes and background
(or batch) processes. These two types of processes have
different response-time requirements, and so might have
different scheduling needs. In addition, foreground
processes may have priority over background processes.
1. System Processes
2. Interactive Processes
3. Interactive Editing Processes
4. Batch Processes
5. Student Processes
Each queue has absolute priority over lower-priority
queues.
The primary difference is that threads within the same process run in a shared
memory space, while processes run in separate memory spaces.
Threads are not independent of one another like processes are, and as a result
threads share with other threads their code section, data section, and OS
resources (like open files and signals). But, like process, a thread has its own
program counter (PC), register set, and stack space.
Types of Thread
There are two types of threads:User Threads and Kernel Threads
User threads are above the kernel and without kernel support. These are the
threads that application programmers use in their programs.
In this case, the thread management kernel is not aware of the existence of
threads.
The thread library contains code for creating and destroying threads, for
passing message and data between threads, for scheduling thread execution and
for saving and restoring thread contexts. The application starts with a single
thread.
Advantages
Kernel threads are supported within the kernel of the OS itself. In this case, thread
management is done by the Kernel. There is no thread management code in the
application area.
Kernel threads are supported directly by the operating system. Any application
can be programmed to be multithreaded. All of the threads within an application are
supported within a single process.
All modern OSs support kernel level threads, allowing the kernel to perform multiple
simultaneous tasks and/or to service multiple kernel system calls simultaneously.
The Kernel maintains context information for the process as a whole and for
individual’s threads within the process.
Scheduling by the Kernel is done on a thread basis. The Kernel performs thread
creation, scheduling and management in Kernel space. Kernel threads are
generally slower to create and manage than the user threads.
Advantages
Kernel can simultaneously schedule multiple threads from the same process on
multiple processes.
If one thread in a process is blocked, the Kernel can schedule another thread of
the same process.
Kernel routines themselves can be multithreaded.
Disadvantages
Kernel threads are generally slower to create and manage than the user
threads.
Transfer of control from one thread to another within the same process
requires a mode switch to the Kernel.
Multithreading Models
The user threads must be mapped to kernel threads, by one of the following strategies:
In the many to one model, many user-level threads are all mapped onto a
single kernel thread.
Thread management is handled by the thread library in user space, which is
efficient in nature.
The one to one model creates a separate kernel thread to handle each and every
user thread.
Most implementations of this model place a limit on how many threads can be
created.
Linux and Windows from 95 to XP implement the one-to-one model for
threads.
Many to Many Model
The many to many model multiplexes any number of user threads onto an
equal or smaller number of kernel threads, combining the best features of the
one-to-one and many-to-one models.
Users can create any number of the threads.
Blocking the kernel system calls does not block the entire process.
Processes can be split across multiple processors.
Deadlocks
Deadlock is a situation where a set of processes are blocked because each process is holding
a resource and waiting for another resource acquired by some other process.
System Model
2. Hold and wait: a process holding at least one resource is waiting to acquire
additional resources held by other processes
4. Circular wait: there exists a set {P0, P1, …, Pn} of waiting processes such that P0 is
waiting for a resource that is held by P1, P1 is waiting for a resource that is held by P2,
…, Pn–1 is waiting for a resource that is held by Pn, and Pn is waiting for a resource
that is held by P0.
Resource-Allocation Graph
Note:
Deadlock detection and recovery – Allow the system to enter a deadlock state,
detect it and recover. Abort a process or preempt some resources when deadlocks are
detected.
Ignore the problem all together - If deadlocks only occur once a year or so, it may
be better to simply let them happen and reboot as necessary than to incur the constant
overhead and system performance penalties associated with deadlock prevention or
detection. This is the approach that both Windows and UNIX take.
If deadlocks are neither prevented nor detected, then when a deadlock occurs
the system will gradually slow down, as more and more processes become
stuck waiting for resources currently held by the deadlock and by other
waiting processes. Unfortunately this slowdown can be indistinguishable from
a general system slowdown when a real-time process has heavy computing
needs.
1. Deadlock Prevention
Restrain the ways request can be made:
Mutual Exclusion – not required for sharable resources (e.g., read-only files); must
hold for non-sharable resources
Hold and Wait – must guarantee that whenever a process requests a resource, it does
not hold any other resources
Require process to request and be allocated all its resources before it begins
execution, or allow process to request resources only when the process has
none allocated to it.
No Preemption –
Process will be restarted only when it can regain its old resources, as well as
the new ones that it is requesting
Circular Wait – impose a total ordering of all resource types, and require that each
process requests resources in an increasing order of enumeration
2. Deadlock Avoidance
Requires that the system has some additional a priori information available
Simplest and most useful model requires that each process declare the maximum
number of resources of each type that it may need.
Safe State
When a process requests an available resource, system must decide if immediate
allocation leaves the system in a safe state
System is in safe state if there exists a sequence <P1, P2, …, Pn> of ALL the
processes in the systems such that for each Pi, the resources that Pi can still request
can be satisfied by currently available resources + resources held by all the Pj, with j <
i
That is:
If Pi resource needs are not immediately available, then Pi can wait until all Pj
have finished
Request edge converted to an assignment edge when the resource is allocated to the
process
Example:
2.Banker’s Algorithm (for multiple instance of a resource type)
When a process gets all its resources it must return them in a finite amount of
time
Safety Algorithm
1. Let Work and Finish be vectors of length m and n, respectively. Initialize:
Work = Available
3. Work=Work+Allocationi
Finish[i] = true
go to step 2
4. If Finish [i] == true for all i, then the system is in a safe state
Resource-Request Algorithm for Process Pi
1. Single Instance of Each Resource Type: All resources have only a single
instance.
2. Pi Pj if Pi is waiting for Pj
Periodically invoke an algorithm that searches for a cycle in the graph. If there is a cycle,
there exists a deadlock
Available: A vector of length m indicates the number of available resources of each type
Initialize:
(a)Finish[i] == false
(b)Requesti Work
If detection algorithm is invoked arbitrarily, there may be many cycles in the resource
graph and so we would not be able to tell which of the many deadlocked processes
“caused” the deadlock.
1. Process Termination
2. Resource Preemption