OS-Unit-II
OS-Unit-II
The Process
▸ Process memory is divided into four sections
▹ The text section comprises the compiled program code, read in from
non-volatile storage when the program is launched.
▹ The data section stores global and static variables, allocated and
initialized prior to executing main.
▹ The heap is used for dynamic memory allocation, and is managed via
calls to new, delete, malloc, free, etc.
▹ The stack is used for local variables. Space on the stack is reserved for
local variables when they are declared and the
space is freed up when the variables go out of scope.
Note : that the stack and the heap start at opposite ends of the process's free
space and grow towards each other. If they should ever meet, then either a stack
overflow error will occur, or else a call to new or malloc will fail due to
insufficient memory available.
Process State
Processes may be in one of 5 states,
• New - The process is in the stage of being created.
• Ready - The process has all the resources available
that it needs to run, but the CPU is not currently
working on this process's instructions.
• Running - The CPU is working on this process's instructions.
Waiting - The process cannot run at the moment,because it is
waiting for some resource to become
available or for some event to occur.
1
UNIT-2 OPERATING SYSTEMS, DEPT.OF CSE, ACET, SURAMPALEM
• For example the process may be waiting for keyboard input, disk access request, inter-process
messages, a timer to go off, or a child process to finish.
• Terminated - The process has completed.
Thread
Thread: A thread is the unit of execution within a process. A process can have Thread
anywhere from just one thread to many threads
Process Scheduling:
The act of determining which process is in the ready state, and should be moved to the running state
is known as Process Scheduling.
Process Scheduling Objectives
• The two main objectives of the process scheduling system are
• To keep the CPU busy at all times
• To deliver "acceptable" response times for all programs, particularly for interactive ones.
• The process scheduler must meet these objectives by implementing suitable policies for swapping processes in
and out of the CPU.
Scheduling Queues
• All processes, upon entering into the system, are stored in
the Job Queue.
• Processes in the Ready state are placed in the Ready Queue.
• Processes waiting for a device to become available are placed
in Device Queues. There are unique device queues available
for each I/O device.
• A new process is initially put in the Ready queue. It waits in the
ready queue until it is selected for execution(or dispatched).
2
UNIT-2 OPERATING SYSTEMS, DEPT.OF CSE, ACET, SURAMPALEM
Once the process is assigned to the CPU and is executing, one of the following several events can occur:
• The process could issue an I/O request, and then be placed in the I/O queue.
• The process could create a new sub process and wait for its termination.
• The process could be removed forcibly from the CPU, as a result of an interrupt, and be put back in the
ready queue.
The ready queue and various
There are three types of schedulers available:
I/O device queues
▸ Long Term Scheduler Seco Job
▸ Short Term Scheduler queue
ndary
▸ Medium Term Scheduler
mem will be
Long Term or job scheduler:
ory resides
• It brings the new process to the ‘Ready State’.
• It controls Degree of Multi-programming, i.e., number of process present in ready state at
any point of time.
• It is important that the long-term scheduler make a careful selection of both IO and CPU
bound process.
loa
• IO bound tasks are which use much of their time in input and output operations while CPU ds
bound processes are which spend their time on CPU.
• The job scheduler increases efficiency by maintaining a balance between the two.
(ready
Short term or CPU scheduler: queue)
• It is responsible for selecting one process from ready state for scheduling it on the running Main
state. memo resides
• Note: Short-term scheduler only selects the process to schedule it doesn’t load the ry in main
process on running. Here is when all the scheduling algorithms are used. The CPU memory
scheduler is responsible for ensuring there is no starvation
owing to high burst time processes.
Medium-term scheduler:
• It is responsible for suspending and resuming the
process.
• It mainly does swapping (moving processes from main
memory to disk and vice versa).
• Swapping may be necessary to improve the process mix
or because a change in memory requirements has
overcommitted available memory, requiring memory to
be freed up.
• It is helpful in maintaining a perfect balance between
the I/O bound and the CPU bound. It reduces the degree
of multiprogramming.
Addition of a medium-term scheduling to the queueing
diagram
3
UNIT-2 OPERATING SYSTEMS, DEPT.OF CSE, ACET, SURAMPALEM
• When system loads get high, this scheduler will swap one or more processes out of the ready queue system
for a few seconds, in order to allow smaller faster jobs to finish up quickly and clear the system.
Context Switch
▸ Whenever an interrupt arrives, the CPU must do a state-
save of the currently running process, then switch into
kernel mode to handle the interrupt, and then do a state-
restore of the interrupted process.
▸ Similarly, a context switch occurs when the time slice for
one process has expired and a new process is to be loaded
from the ready queue. This will be instigated by a timer
interrupt, which will then cause the current process's state
to be saved and the new process's state to be restored.
▸ Saving and restoring states involves saving and restoring all
of the registers and program counter(s), Diagram showing CPU switch from process
as well as the process control blocks described above. to process
▸ Context switching happens VERY VERY frequently, and the
overhead of doing the switching is just lost CPU time, so context switches ( state saves & restores ) need to be as
fast as possible. Some hardware has special provisions for speeding this up, such as a single machine instruction
for saving or restoring all registers at once.
Operations on Processes
▸ Process Creation
▸ Process Termination
Process Creation
▸ A process may create several new processes, via a create-process system call, during
the course of execution. p
▸ The creating process is called a parent process and the new processes are called the
children of that process
▸ Each of these new processes may in turn creates other processes forming a tree of p
p
processes
How Resource sharing will be done between parent and children
▸ Parent process can share all resource with children
▸ Parent can share some resource with children p p p p p
▸ Parent never share any resource with children
When a process creates a new process, two possibilities exist in term of execution:
▸ 1.The parent continues to execute concurrently with its children.
▸ 2.The parent waits until some or all of its children terminated
There are also two possibilities in terms of the address space of the new process
▸ 1.The child process is a duplicate of the parent process(it has the same program and data as the parent)
▸ 2.The child process had a new program loaded
into it.
▸ Unix examples:
▸ fork() system call creates a new process
▸ exec system call replaces newly created
process with new process
4
UNIT-2 OPERATING SYSTEMS, DEPT.OF CSE, ACET, SURAMPALEM
Process Termination
• A process terminates when its finishes executing its final statement and asks the operating system to delete it by
the exit() system call.
• At that point, the process may return a status value(typically an integer) to its parent process(via the wait() system
call).
• All the resources of the process-including physical and virtual memory open files, and I/O buffers are deallocated by
the operating system.
Termination can occur in other circumstances as well:
1. A process can cause the termination of another process via an appropriate system call
2. Usually such a system call can be invoked only by the parent of the process that is to be terminated
3. Otherwise user could arbitrarily kill each other’s jobs
Interprocess Communication
1. Processes executing concurrently in the operating system may be either independent processes or cooperating
processes.
2. A process is independent if it cannot affect or be affected by the other processes executing in the system. Any process
that does not share data with any other process is independent.
3. A process is cooperating if it can affect or be affected by the other processes executing in the system.
1. There are several reasons for providing an environment that allows process cooperation:
1. Information sharing
2. Computation speedup.
3. Modularity
4. Convenience
2. Information sharing. Since several users may be interested in the same piece of information (for instance, a shared
file), we must provide an environment to allow concurrent access to such information.
3. Computation speedup. If we want a particular task to run faster, we must break it into subtasks, each of which will be
executing in parallel with the others. Notice that such a speedup can be achieved only if the computer has multiple
processing cores.
4. Modularity. We may want to construct the system in a modular fashion dividing the system functions into separate
processes or threads,
5. Convenience. Even an individual user may work on many tasks at the same time. For instance, a user may be editing,
listening to music, and compiling in parallel.
5
UNIT-2 OPERATING SYSTEMS, DEPT.OF CSE, ACET, SURAMPALEM
Shared-Memory Systems
• Interprocess communication using shared memory requires communicating processes to
establish a region of shared memory.
Typically, a shared-memory region resides in the address space of the process creating the
shared-memory segment.
Normally, the operating system tries to prevent one process from accessing another
process’s memory.
Producer-Consumer Example Using Shared Memory
A producer process produces information that is consumed by a consumer process.
For example,
A compiler may produce assembly code that is consumed by an assembler.
The assembler, in turn, may produce object modules that are consumed by the loader.
Message-Passing Systems
Message passing provides a mechanism to allow processes to communicate and to synchronize their actions without
sharing the same address space.
• It is particularly useful in a distributed environment, where the communicating processes may
reside on different computers connected by a network.
• A message-passing facility provides at least two operations:
send(message) receive(message)
• Messages sent by a process can be either fixed or variable in size.
• Fixed-sized messages can be sent, the system-level implementation is
straightforward. (But makes the task of programming more difficult)
• Variable-sized messages require a more complex system level implementation,
( but the programming task becomes simpler)
• If processes P and Q want to communicate, they must send messages to and receive messages from each other:
• A communication link must exist between them. This link can be implemented in a variety of ways. Here are several
methods for logically implementing a link and the send()/receive() operations
6
UNIT-2 OPERATING SYSTEMS, DEPT.OF CSE, ACET, SURAMPALEM
• Blocked Sender / Blocked Receiver
• Non Blocked Sender /Non Blocked Receiver
Buffering
• The size of the buffer may 0 (ZERO) capacity
• The size of the buffer may be finite capacity
• The size of the buffer is infinite capacity
Direct Communication
Direct communication, each process that wants
to communicate must explicitly name the recipient
or sender of the communication.
• A communication link in this scheme has the following properties: P Q
• A link is established automatically between every pair of processes that
want to communicate. The processes need to know only each other’s
identity to communicate.
• A link is associated with exactly two processes.
• Between each pair of processes, there exists exactly one link.
Indirect Communication
The messages are sent to and received from mailboxes, or ports
• send(A, message)—Send a message to mailbox A.
• receive(A, message)—Receive a message from mailbox A.
In this scheme, a communication link has the following properties:
• A link is established between a pair of processes only if both members of the pair have a shared mailbox.
• A link may be associated with more than two processes.
• Between each pair of communicating processes, a number of different links may exist, with each link
corresponding to one mailbox..
Synchronization
• Message passing may be either blocking or nonblocking— also known as synchronous and asynchronous
1. Blocking send. The sending process is blocked until the message is received by the receiving process or by
the mailbox.
2. Blocking receive. The receiver blocks until a message is available.
3. Nonblocking send. The sending process sends the message and resumes operation.
4. Nonblocking receive. The receiver retrieves either a valid message or a null.
Buffering
• Whether communication is direct or indirect, messages exchanged by communicating processes reside in a
temporary queue. Basically, such queues can be implemented in three ways:
▸ Zero capacity. :The queue has a maximum length of zero; thus, the link cannot have any messages
waiting in it. In this case, the sender must block until the recipient receives the message.
▸ Bounded capacity. The queue has finite length n; thus, at most n messages can reside in it. If the queue is
not full when a new message is sent, the message is placed in the queue and the sender can continue
execution without waiting. The link’s capacity is finite, however. If the link is full, the sender must block
until space is available in the queue.
▸ Unbounded capacity. The queue’s length is potentially infinite; thus, any number of messages can wait in
it. The sender never blocks.
7
UNIT-2 OPERATING SYSTEMS, DEPT.OF CSE, ACET, SURAMPALEM
• Definition of Thread
1. A thread is the unit of execution within a
process. (Or)
2. A thread is a basic unit of CPU utilization,
consisting of a program counter, a stack, and a
set of registers, (and a thread ID. )
3. Threads are also known as Lightweight
processes.
4. A traditional (or heavyweight) process has a
single thread of control.
5. If a process has multiple threads of control, it
can perform more than one task at a time.
6. Most software applications that run on modern computers
are multithreaded.
7. An application typically is implemented as a separate
process with several threads of control
Example: word processor (MS Word)
The MS-Word process could involve many threads:
1. Interaction with the keyboard § Display of characters on
the display page
2. Regularly saving file to disk
3. Controlling spelling and grammar Etc.
4. All these threads would share the same document Multithreaded server architecture
Another example is a web server - Multiple threads allow for multiple requests to be satisfied simultaneously, without
having to service requests sequentially or to fork off separate processes for every incoming request.
Benefits of Multi-threading:
There are four major categories of benefits of multi-threading:
• Responsiveness - One thread may provide rapid response while other threads are blocked or slowed down doing
intensive calculations.
• Resource sharing - By default threads share common code, data, and other resources, which allows multiple tasks
to be performed simultaneously in a single address space.
• Economy – Allocating memory and resources for process creation is costly. Because threads share resources of
the process to which they belong, it is more economical to create and context-switches threads.
• Scalability: Utilization of multiprocessor architectures - A single threaded process can only run on one CPU, no
matter how many may be available, whereas the execution of a multi-threaded application may be split amongst
available processor
Types of Thread
• There are two types of threads:
8
UNIT-2 OPERATING SYSTEMS, DEPT.OF CSE, ACET, SURAMPALEM
User Threads
Kernel Threads
• User threads are above the kernel and without kernel support. These are the threads that application
programmers use in their programs.
Implementation of User Level thread is done by a thread library and is easy.
Example of User Level threads: Java thread, POSIX threads.
• Kernel threads are supported within the kernel of the OS itself. All modern OSs support kernel-level threads,
allowing the kernel to perform multiple simultaneous tasks and/or to service multiple kernel system calls
simultaneously.
While the Implementation of the kernel-level thread is done by the operating system and is complex.
Example of Kernel level threads: Window Solaris.
Multithreading Models
• The user threads must be mapped to kernel threads, by one of the following strategies:
o Many to One Model
o One to One Model
o Many to Many Model
Many-To-One Model
• 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 very efficient.
• However, the entire process will block if a thread makes a blocking system call.
• Because a single kernel thread can operate only on a single CPU, the many-to-one model does
not allow individual processes to be split across multiple CPUs.
• The disadvantage is when we considers multiprocessor system so this cannot be considered
because only one kernel is present so we can’t achieve the parallelism
One-To-One Model
• The one-to-one model creates a separate kernel thread to handle each user thread.
• One-to-one model overcomes the problems listed above involving blocking system calls
and the splitting of processes across multiple CPUs.
• However the overhead of managing the one-to-one model is more significant, involving
more overhead and slowing down the system.
• Most implementations of this model place a limit on how many threads can be created.
Many-To-Many Model
• The many-to-many model multiplexes any number of user threads onto
an equal or smaller number of kernel threads.
• The following diagram shows the many-to-many threading model where
4 user level threads are multiplexing with 3 kernel level threads.
• In this model, developers can create as many user threads as necessary
and the corresponding Kernel threads can run in parallel on a
multiprocessor machine.
• This model provides the best accuracy on concurrency and when a
thread performs a blocking system call, the kernel can schedule One popular variation of the many-
another thread for execution. to-many model is the two-tier
model, which allows either many-to-
many or one-to-one operation.
Thread libraries
1. Thread libraries provide programmers with an API for creating and managing threads.
9
UNIT-2 OPERATING SYSTEMS, DEPT.OF CSE, ACET, SURAMPALEM
2. Thread libraries may be implemented either in user space or in kernel space. The former involves API functions
implemented solely within user space, with no kernel support. The latter involves system calls, and requires a
kernel with thread library support.
3. There are three main thread libraries in use today:
1. POSIX Pthreads - may be provided as either a user or kernel library, as an extension to the POSIX
standard.
2. Win32 threads - provided as a kernel-level library on Windows systems.
3. Java threads - Since Java generally runs on a Java Virtual Machine, the implementation of threads is
based upon whatever OS and hardware the JVM is running on, i.e. either Pthreads or Win32 threads
depending on the system.
Threading Issues
1. There are a variety of issues to consider with multithreaded programming
Semantics of fork() and exec() system calls
1.
2. Thread cancellation
3. Signal handling
4. Thread pooling
5. Thread-specific data
Which version of fork() must be used totally depends upon the application.
1. Next system call i.e. exec() system call when invoked replaces the program along with all its threads with the
program that is specified in the parameter to exec(). Typically the exec() system call is lined up after the fork()
system call.
2. Here the issue is if the exec() system call is lined up just after the fork() system call then duplicating all the
threads of parent process in the child process by fork() is useless.
3. As the exec() system call will replace the entire process with the process provided to exec() in the parameter.
Thread Cancellation
1. Thread cancellation involves terminating a thread before it has completed.
2. A thread that is to be cancelled is often referred to as the target thread.
Cancellation of a target thread may occur in two different scenarios:
1. 1. Asynchronous cancellation:
1. One thread immediately terminates the target thread.
2. 2. Deferred cancellation.
1. The target thread periodically checks whether it should terminate, allowing it an opportunity to
terminate itself in an orderly fashion.
3. The issue related to the target threads are listed below:
10
UNIT-2 OPERATING SYSTEMS, DEPT.OF CSE, ACET, SURAMPALEM
1. What if the resources had been allotted to the cancel target thread?
2. What if the target thread is terminated when it was updating the data, it was sharing with some other
thread.
4. Here the asynchronous cancellation of the thread where a thread immediately cancels the target thread without
checking whether it is holding any resources or not creates troublesome.
5. However, in deferred cancellation, the thread that indicates the target thread about the cancellation, the target
thread crosschecks its flag in order to confirm that it should it be cancelled immediately or not. The thread
cancellation takes place where they can be cancelled safely such points are termed as cancellation points by
Pthreads.
Signal Handling
1. A signal is used in UNIX systems to notify a process that a particular event has occurred. A signal may be received
either synchronously or asynchronously,
2. Synchronous signals are delivered to the same process that performed the operation that caused the signal
1. Examples of synchronous signal include illegal memory access and division by 0.
3. Asynchronous Signal are generated by an event external to a running process, that process receives the signal
asynchronously.
1. Examples of such signals include terminating a process with specific keystrokes (such as <control><C>)
and having a timer expire.
4. Handling signals in single-threaded programs is straightforward: Signals are always delivered to a process.
5. However, delivering signals is more complicated in multithreaded programs, where a process may have several
threads. Where, then, should a signal be delivered?
6. In general, the following options exist:
1. 1. Deliver the signal to the thread to which the signal applies.
2. 2. Deliver the signal to every thread in the process.
3. 3. Deliver the signal to certain threads in the process.
4. 4. Assign a specific thread to receive all signals for the process.
7. So if the signal is synchronous it would be delivered to the specific thread causing the generation of the signal.
8. If the signal is asynchronous it cannot be specified to which thread of the multithreaded program it would be
delivered.
1. If the asynchronous signal is notifying to terminate the process the signal would be delivered to all the
thread of the process.
9. The issue of an asynchronous signal is resolved up to some extent in most of the multithreaded UNIX system.
10. Here the thread is allowed to specify which signal it can accept and which it cannot.
11. However, the Window operating system does not support the concept of the signal instead it uses asynchronous
procedure call (ACP) which is similar to the asynchronous signal of the UNIX system.
Thread Pool
1. When a user requests for a webpage to the server, the server creates a separate thread to service the request.
Although the server also has some potential issues. Consider if we do not have a bound on the number of actives
thread in a system and would create a new thread for every new request then it would finally result in exhaustion
of system resources.
2. The idea is to create a finite amount of threads when the process starts. This collection of threads is referred to as
the thread pool.
3. A thread pool is a group of threads that have been pre-created and are available to do work as needed
1. Threads may be created when the process starts
2. A thread may be kept in a queue until it is needed
3. After a thread finishes, it is placed back into a queue until it is needed again
4. Avoids the extra time needed to spawn new threads when they’re needed
4. In applications where threads are repeatedly being created/destroyed thread pools might provide a performance
benefit
11
UNIT-2 OPERATING SYSTEMS, DEPT.OF CSE, ACET, SURAMPALEM
Example: A server that spawns a new thread each time a client connects to the system and discards that thread when
the client disconnects
Scheduling
1. Scheduling of this kind is a fundamental operating-system function.
2. Almost all computer resources are scheduled before use.
3. The CPU is, of course, one of the primary computer resources. Thus, its
scheduling is central to operating-system design.
4. Scheduling of processes/work is done to finish the work on time.
5. CPU–I/O Burst Cycle
6. The success of CPU scheduling depends on an observed property of processes:
7. Process execution consists of a cycle of CPU execution and I/O wait.
8. Processes alternate between these two states.
1. Process execution begins with a CPU burst. Alternating sequence of CPU and
2. That is followed by an I/O burst. I/O bursts.
9. Another CPU burst, then another I/O burst, and so on.
10. Eventually, the final CPU burst ends with a system request to terminate execution
Process scheduling
The act of determining which process is in the ready state, and should be moved to the running state is known as Process
Scheduling.
CPU Scheduler
1. Short-term scheduler selects from among the processes in
ready queue, and allocates the CPU to one of them
12
UNIT-2 OPERATING SYSTEMS, DEPT.OF CSE, ACET, SURAMPALEM
Queue may be ordered in various ways
Non-Preemptive Scheduling
1. Non-Preemptive scheduling is used when a process terminates, or a process switches from running to waiting
state.
2. In this scheduling, once the resources(CPU Cycles) is allocated to a process, the process hold the CPU till it gets
terminated or it reaches a waiting state.
3. In case of non-preemptive scheduling does not interrupts a process running CPU in middle of the executing .
4. Instead it waits still the process completes its CPU burst time and then it can allocates the CPU to another process
Algorithms based on Non-Preemptive scheduling are:
1. Shortest Job First(SJF basically non Preemptive)
2. Priority (non-preemptive version)etc
3. First Come First Serve
Dispatcher
1. Another component involved in the CPU-scheduling function is the dispatcher.
2. The dispatcher is the module that gives control of the CPU to the process selected by the short-term scheduler.
3. 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
4. The dispatcher should be as fast as possible, since it is invoked during every process switch. The time it takes for
the dispatcher to stop one process and start another running is known as the dispatch latency.
Scheduling Criteria
13
UNIT-2 OPERATING SYSTEMS, DEPT.OF CSE, ACET, SURAMPALEM
Many criteria have been suggested for comparing CPU-scheduling algorithms. Which characteristics are used for
comparison can make a substantial difference in which algorithm is judged to be best.
The criteria include the following:
CPU utilization:
Keep the CPU as busy as possible
Throughput:
Number 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)
Scheduling Algorithms
• Different Scheduling Algorithms
• First Come First Serve(FCFS) Scheduling
• Shortest-Job-First(SJF) Scheduling
• Priority Scheduling
• Round Robin(RR) Scheduling
• Multilevel Queue Scheduling
• Multilevel Feedback Queue Scheduling
• Shortest Remaining Time First (SRTF)
• Longest Remaining Time First (LRTF)
Gantt chart
A Gantt chart is a bar chart that provides a visual view of tasks
scheduled over time.
14
UNIT-2 OPERATING SYSTEMS, DEPT.OF CSE, ACET, SURAMPALEM
4. When a process enters the ready queue, its PCB is linked onto the tail of the queue.
5. When the CPU is free, it is allocated to the process at the head of the queue.
6. The running process is then removed from the queue.
Example:
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
Example:2
15
UNIT-2 OPERATING SYSTEMS, DEPT.OF CSE, ACET, SURAMPALEM
The shaded box represents the idle time of CPU
0 3 4 9 13 17 19
Gantt chart
Calculation of the average waiting time and average Turnaround time, if FCFS Scheduling Algorithm is
followed
Waiting Time = Turn Around Time – Burst Time
Turn Around Time = Completion Time – Arrival Time
Average Turn Around time = (5+11+3+13+8)/5
= 40/5
= 8 ms
Average Waiting Time = (0+7+0+11+4)/5
= 22/5
= 4.4 ms
Convoy Effect
1. FCFS algorithm is non-pre-emptive in nature, that is, once
CPU time has been allocated to a process, other processes
can get CPU time only after the current process has
finished. This property of FCFS scheduling leads to the
situation called Convoy Effect.
2. Convoy Effect is phenomenon associated with the First
Come First Serve (FCFS) algorithm, in which the whole
Operating System slows down due to few slow processes.
3. Suppose there is one CPU intensive (large burst time)
process in the ready queue, and several other processes
with relatively less burst times but are Input/Output (I/O)
bound (Need I/O operations frequently)
16
UNIT-2 OPERATING SYSTEMS, DEPT.OF CSE, ACET, SURAMPALEM
Hence in Convoy Effect, one slow process slows down the performance of the entire set of processes, and leads to wastage
of CPU time and other devices.
Shortest-Job-First Scheduling
1. A different approach to CPU scheduling is the shortest-job-first (SJF) scheduling algorithm.
2. This algorithm associates with each process the length of the process’s next CPU burst.
3. When the CPU is available, it is assigned to the process that has the smallest next CPU burst.
4. This is the best approach to minimize waiting time.
5. If the next CPU bursts of two processes are the same, FCFS scheduling is used to break the tie.
6. A more appropriate term for this scheduling method would be the shortest-next- CPU-burst algorithm, because
scheduling depends on the length of the next CPU burst of a process, rather than its total length.
Example of SJF Scheduling (Non-Preemptive)
The following set of processes, with the length of the CPU burst given in milliseconds:
By comparison, if we were using the FCFS scheduling scheme, the average waiting time would be
10.25 milliseconds.
Example 2:
Consider the below processes available in the ready queue for execution, with arrival time as 0 for all and given burst times.
Gantt chart:
Example: 3
17
UNIT-2 OPERATING SYSTEMS, DEPT.OF CSE, ACET, SURAMPALEM
Problem with Non Pre-emptive SJF
• If the arrival time for processes are different, which means all the processes are not available in the ready queue
at time 0, and some jobs arrive after some time, in such situation, sometimes process with short burst time have
to wait for the current process's execution to finish, because in Non Pre-emptive SJF, on arrival of a process with
short duration, the existing job/process's execution is not halted/stopped to execute the short job first.
Example:SJF (Preemptive)
Consider the set of 5 processes whose arrival time and burst time are given below-
If the CPU scheduling policy is SJF preemptive, calculate the average waiting time and average turnaround time.
18
UNIT-2 OPERATING SYSTEMS, DEPT.OF CSE, ACET, SURAMPALEM
Gantt chart:
0 1 2 5 7 13 20
Example:
Consider the following set of processes, assumed to have arrived at time 0 in the order P1, P2, · · ·, P5, with the length of the
CPU burst given in milliseconds:
19
UNIT-2 OPERATING SYSTEMS, DEPT.OF CSE, ACET, SURAMPALEM
Using priority scheduling, we would schedule these processes according to the following
Consider the below table for processes with their respective CPU burst times and the
priorities.
Example:
Consider the set of 5 processes whose arrival time and burst time are given below-
If the CPU scheduling policy is priority non-preemptive, calculate the average waiting time and average turnaround time.
(Higher number represents higher priority)
20
UNIT-2 OPERATING SYSTEMS, DEPT.OF CSE, ACET, SURAMPALEM
Example: Priority Scheduling Preemptive
Consider the set of processes with arrival time (in milliseconds), CPU burst time (in milliseconds), and priority (0 is the
highest priority) shown below. None of the processes have I/O burst time.
Calculate the average waiting time (in milliseconds) of all the processes using pre-emptive priority scheduling algorithm
0 2 5 3 4 4 5 6
3 0 9 1 7
Waiting Time for p1= 38 ms
Waiting Time for p2= 0 ms
Waiting Time for p3= 37 ms
Waiting Time for p4= 28 ms
Waiting Time for p5= 42 ms
21
UNIT-2 OPERATING SYSTEMS, DEPT.OF CSE, ACET, SURAMPALEM
If the CPU scheduling policy is priority preemptive, calculate the average waiting time and average turn around time.
(Higher number represents higher priority)
Solution to Problem
1. A solution to the problem of indefinite blockage of low-priority processes is aging. Aging involves gradually
increasing the priority of processes that wait in the system for a long time.
1. For example, if priorities range from 127 (low) to 0 (high), we could increase the priority of a waiting
process by 1 every 15 minutes. Eventually, even a process with an initial priority of 127 would have
the highest priority in the system and would be executed. In fact, it would take no more than 32
hours for a priority-127 process to age to a priority-0 process.
Round-Robin Scheduling
Implementation RR scheduling
1. We keep ready queue as a FIFO queue of processes.
2. New processes are added to the tail of the ready queue.
3. The CPU scheduler picks the first process from the ready queue, sets a timer to interrupt after 1 time quantum, and
dispatches the process.
Example:
Consider the following set of processes that arrive at time 0, with the length of the CPU burst given in milliseconds: time
quantum of 4 milliseconds
=47/3
Solution: =15.66ms
=5.66 ms
Example: Consider the set of 4 processes whose arrival time and burst time are given below-
23
UNIT-2 OPERATING SYSTEMS, DEPT.OF CSE, ACET, SURAMPALEM
If the CPU scheduling policy is Round Robin with time quantum = 2 unit, calculate the average waiting time and average
turnaround time.
Ready Queue
Gantt chart
0 2 4 6 8 9 11 12
Consider the set of 5 processes whose arrival time and burst time are given below-
If the CPU scheduling policy is Round Robin with time quantum = 2 unit, calculate the average waiting time and average turn
around time.
1. Longest Job First (LJF): It is similar to SJF scheduling algorithm. But, in this scheduling algorithm, we give priority to
the process having the longest burst time. This is non-preemptive in nature i.e., when any process starts executing, can’t
be interrupted before complete execution.
2. Longest Remaining Time First (LRTF): It is preemptive mode of LJF algorithm in which we give priority to the
process having largest burst time remaining.
24
UNIT-2 OPERATING SYSTEMS, DEPT.OF CSE, ACET, SURAMPALEM
For example:
1. Processes that require a user to start them or to interact with them are called foreground processes. Processes that are
run independently of a user are referred to as background processes.
1. Programs and commands run as foreground processes by default. To run a process in the
background, place an ampersand (&) at the end of the command name that you use to start the
process.
2. Common division is made between foreground (interactive) processes and background (batch) processes.
3. These two types of processes have different response-time requirements different scheduling needs.
4. In addition, foreground processes may have priority (externally defined) over background processes.
5. According to the priority of process, processes are placed in the different queues. Generally high priority process are
placed in the top level queue. Only after completion of processes from top level queue, lower level queued processes
are scheduled. It can suffer from starvation.
6. An example of a multilevel queue scheduling algorithm with five queues, listed below in order of priority:
1. For example, separate queues might be used for foreground and background processes. The foreground queue
might be scheduled by an RR algorithm, while the background queue is scheduled by an FCFS algorithm.
In addition, there must be scheduling among the queues, which is commonly implemented as fixed-priority pre-emptive
scheduling
. For example, the foreground queue may have absolute priority over the background queue. Each queue has absolute
priority over lower-priority queues. No process in the batch
queue, for example, could run unless the queues for system
processes, interactive processes, and interactive editing
processes were all empty. If an interactive editing process
entered the ready queue while a batch process was running, the
batch process would be preempted.
Another possibility is to time-slice among the queues.
Here, each queue gets a certain portion of the CPU time,
which it can then schedule among its various processes. For
instance, in the foreground–background queue example, the
foreground queue can be given 80 percent of the CPU time for RR
scheduling among its processes, while the background queue
receives 20 percent of the CPU to give to its processes on an FCFS
basis.
Multilevel Feedback Queue Scheduling
1. The multilevel feedback queue scheduling algorithm, allows a process to move between queues.
2. The idea is to separate processes according to the characteristics of their CPU bursts.
3. If a process uses too much CPU time, it will be moved to a lower-priority queue.
4. This scheme leaves I/O-bound and interactive processes in the higher-priority queues.
5. In addition, a process that waits too long in a lower-priority queue may be moved to a higher-priority queue.
6. This form of aging prevents starvation.
Consider a multilevel feedback queue scheduler with three queues, numbered from 0 to 2
In general, a multilevel feedback queue scheduler is
defined by the following parameters: Highest priority
• The number of queues
• The scheduling algorithm for each queue Queue -0
• The method used to determine when to upgrade a
process to a higher priority queue
Queue -1
25
UNIT-2 OPERATING SYSTEMS, DEPT.OF CSE, ACET, SURAMPALEM
Low priority Queue -2
• The method used to determine when to demote a process to a lower priority queue
• The method used to determine which queue a process will enter when that process needs service
Multiple-Processor Scheduling
When multiple processors are available, then the scheduling gets more complicated, because now there is more than one
CPU which must be kept busy and in effective use at all times.
Load sharing revolves around balancing the load between multiple processors.
1. Multi-processor systems may be heterogeneous, (different kinds of CPUs), or homogenous, (all the same kind of CPU).
Even in the latter case there may be special scheduling constraints, such as devices which are connected via a private
bus to only one of the CPUs.
2. Approaches to Multiple-Processor Scheduling
3. One approach to multi-processor scheduling is asymmetric multiprocessing, in which one processor is the master,
controlling all activities and running all kernel code, while the other runs only user code. This approach is relatively
simple, as there is no need to share critical system data.
4. Another approach is symmetric multiprocessing, SMP, where each processor
schedules its own jobs, either from a common ready queue or from separate ready
queues for each processor.
5. Virtually all modern OS’s support SMP, including XP, Win 2000, Solaris, Linux, and
Mac OSX.
Processor Affinity
High cost of invalidating and repopulating caches, 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—that is, a
process has an affinity for the processor on which it is currently running.
26
UNIT-2 OPERATING SYSTEMS, DEPT.OF CSE, ACET, SURAMPALEM