OS Unit-1 Material
OS Unit-1 Material
3. OPERATING SYSTEMS
OPERATIONS
3.1. Operating systems operations
Modern operating systems are interrupt driven. If there are no processes to execute, no I/O
devices to service, and no users to whom to respond, an operating system will sit quietly, waiting for
something to happen.
An interrupt is a signal emitted by hardware or software when a process or an event needs
immediate attention.
There are two types of interrupts, they are:
1. Hardware Interrupts
2. Software Interrupts (also called trap or exception )
Hardware interrupt is an interrupt generated from an external device or hardware. For
example, pressing a keyboard key or moving mouse that causes the processor to read the keystroke
or mouse position
A trap (or an exception or software interrupt) is a software-generated interrupt caused
either by an error. For example, division by zero or invalid memory access.
The interrupt-driven nature of an operating system defines that system’s general structure.
For each type of interruption, separate segments of code in the operating system determine which
action to take. An interrupt service routine is provided that is responsible for dealing with the
interrupt.
The operating systems share the hardware and software resources of the computer system to
user programs. If there is an error in a user program could cause problems only for the one program
or many processes could be affected. For example, if a process gets stuck in an infinite loop, this
loop could prevent the correct operation of many other processes.
To protect these types of errors, operating system will run in dual-mode operation.
1. Dual-Mode Operation
Dual-mode operation allows OS to protect itself and other system components. Dual-mode
operation consists of two modes:
1. User Mode
2. Kernel Mode (also called Supervisor Mode, System Mode, Or Privileged Mode)
A mode bit is added to the hardware of the computer to indicate the current mode: kernel (0) or user
(1).With the mode bit, we are able to distinguish between a task that is executed on behalf of the operating
system and one that is executed on behalf of the user.
At system boot time the hardware starts in kernel mode. The operating system is then loaded and starts user
applications in user mode. Whenever a trap or interrupt occurs, the hardware switches from user mode to
kernel mode (that is, changes the state of the mode bit to 0). Thus, whenever the
operating system gains control of the computer, it is in kernel mode. The system always switches to
user mode (by setting the mode bit to 1) before passing control to a user program. This is shown in
below figure.
The dual mode of operation protects the operating system from errant users. We accomplish
this protection by specifying some machine instructions that may cause harm as privileged
instructions. The instruction to switch to kernel mode, I/O control, timer management, and interrupt
management are few examples of a privileged instruction.
The hardware allows privileged instructions to be executed only in kernel mode. If an
attempt is made to execute a privileged instruction in user mode, the hardware does not execute the
instruction but rather treats it as illegal and traps it to the operating system.
Life cycle of instruction:
Initial control resides in the operating system, where instructions are executed in kernel
mode. When control is given to a user application, the mode is set to user mode. Eventually, control
is switched back to the operating system via an interrupt, a trap, or a system call.
2. Timer
Timer prevents a user program from running too long.
A timer can be set to interrupt the computer after a specified period. The period may be fixed
(for example, 1/60 second) or variable (for example, from 1 millisecond to 1 second).
A variable timer is generally implemented by a fixed-rate clock and a counter. The operating
system sets the counter. Every time the clock ticks, the counter is decremented. When the counter
reaches 0, an interrupt occurs.
3.2. Differences between hardware interrupt and software interrupt:
It has lowest priority than software It has highest priority among all
3
interrupts interrupts.
Keystrokes and mouse movements are All system calls are examples of
6
examples of hardware interrupt. software interrupts
4. Computing environments
1. Traditional Computing:
As computing matures, the lines separating many of the traditional computing environments
are blurring.
Office environment
A few years ago, this environment consisted of PCs connected to a network, with servers
providing file and print services. Terminals attached to mainframes providing batch and timesharing.
Now a day’s the environment is web based computing, Companies establish portals, which
provide web accessibility to their internal servers. Network computers are the terminals.
Home networks
At home, most users had a single computer with an internet connection to the office. Some
homes even have firewalls to protect their networks from security breaches
Time-sharing systems
Time-sharing systems used a timer and scheduling algorithms to cycle processes rapidly
through the CPU, giving each user a share of the resources.
2. Client–Server Computing:
Personal Computers (Clients) connected to Servers (Centralized Systems). Server systems
satisfy requests generated by client systems. This is called a client–server system, has the general
structure depicted in below Figure.
All Open Source Software under free software But all free software does not come under
3
terminology. open source terminology.
5.8. Differences between Open Source Software and Closed Source Software:
S.No. Open Source Software Closed Source Software
Open source software refers to the computer Closed source software refers to the computer
1 software which source is open means the software which source code is closes means
general public can access and use. public is not given access to the source code.
3 The price of open source software is very less. The price of closed source software is high.
Programmers freely provide improvement for Programmers are hired by the software
4
recognition if their improvement is accepted. firm/organization to improve the software.
Open software can be installed into any Closed software needs have a valid license before
5
computer. installation into any computer.
6 Open source software fails fast and fix faster. Closed source software has no room for failure.
1. User interface:
Almost all operating systems have a user interface (UI). This interface can take several
forms.
1. Command-line interface (CLI), which uses text commands and a method for entering
them.
2. Batch interface, in which commands and directives to control those commands are
entered into files, and those files are executed.
3. Graphical user interface (GUI) is used. Here, the interface is a window system with a
pointing device to direct I/O, choose from menus, and make selections and a keyboard to
enter text.
Note:
Many systems now include both CLI and GUI interfaces:
1. Microsoft Windows is GUI with CLI “command” shell.
2. Apple Mac OS X as “Aqua” GUI interface with UNIX kernel underneath and shells available.
3. Solaris is CLI with optional GUI interfaces (Java Desktop, KDE).
8. Systems calls
8.1. System Calls:
Definition:
System calls provide an interface to the services provided by an operating system
Typically system calls written in a high-level language (C or C++).
Example:
o Let consider, copy one file to another file. The sequence of system calls invoked in this
example is shown in below figure:
Why use APIs rather than system calls?
The developers design system calls using Application Program Interface (API) rather than direct
development.
Three most common APIs are Win32 API for Windows, POSIX API for POSIX-based systems
(including virtually all versions of UNIX, Linux, and Mac OS X), and Java API for the Java
virtual machine (JVM)
API Example:
. Process Control in Single Tasking (MS DOS) and Multi-Tasking (FreeBSD) Systems:
Process Control in MS DOS:
The MS-DOS operating system is an example of a single-tasking system.
It has a command interpreter that is invoked when the computer is started, shown in below figure.
o
Because MS-DOS is single-tasking, it uses a simple method to run a program and does not create a
new process. It loads the program into memory, writing over most of itself to give the program as
much memory as possible, shown in below figure.
o
Next, it sets the instruction pointer to the first instruction of the program. The program then runs, and
either an error cause a trap, or the program executes a system call to terminate.
In either case, the error code is saved in the system memory for later use.
Following this action, the small portion of the command interpreter that was not overwritten
resumes execution.
Its first task is to reload the rest of the command interpreter from disk. Then the command
interpreter makes the previous error code available to the user or to the next program.
Process Control in FreeBSD:
o FreeBSD (derived from Berkeley UNIX) is an example of a multitasking system.
o When a user logs on to the system, the shell of the user’s choice is run. This shell is similar to
the
o MS-DOS shell in that it accepts commands and executes programs that the user requests.
o Since FreeBSD is a multitasking system, the command interpreter may continue running
while another program is executed, as shown in below figure.
o
o To start a new process, the shell executes a fork() system call. Then, the selected
program is loaded into memory via an exec() system call, and the program is executed.
o Depending on the way the command was issued, the shell then either waits for the
process to finish or runs the process “in the background.”
o When the process is done, it executes an exit() system call to terminate, returning to the
invoking process a status code of 0 or a nonzero error code.
o This status or error code is then available to the shell or other programs.
o
9. Operating system structure
Application Programs
2. Layered Approach:
In this layered approach, structure the operating system is broken into number of layers
(levels).
The bottom layer (layer 0) is the hardware and the topmost layer (layer N) is the user interface.
Each layer uses the functions of the lower level layers only.
The below figure illustrates the layered structure of the operating system.
Advantages:
This simplifies the debugging process as if lower level layers are debugged and an error occurs
during debugging then the error must be on that layer only.
Operating system can retain greater control over the computer and over the applications
that make use of the computer.
o Disadvantages:
o Each layer, the data needs to be modified and passed on which adds overhead to the system.
o Less efficient than other structures
o Careful planning of the layers and implementation is necessary.
3. Micro-kernels:
The micro-kernels structure designs the operating system by removing all non-essential
components from the kernel and implementing them as system and user programs. This result
in a smaller kernel called the micro-kernel.
Communication takes place between user modules using message passing.
Tru64 UNIX is implemented with Mach kernel and Mac OS X kernel (also known as Darwin)
is implemented with Mach micro kernel.
Mac OS X structure is illustrated in below figure:
Program VS Process:
o A program is a passive entity, such as a file containing a list of instructions stored on
disk (also called an executable file).
o A process is an active entity, with a program counter specifying the next instruction to
execute and a set of associated resources.
memory.
Program exists at a single place and Process exists for a limited span of time as it
3.
continues to exist until it is deleted. gets terminated after the completion of task.
Program does not have any resource Process has a high resource requirement; it
5. requirement; it only requires memory space needs resources like CPU, memory address,
for storing the instructions. I/O during its lifetime.
2. Process scheduling
2.1. Process Scheduling:
The goal of multiprogramming is to maximize CPU usage, with certain processes running at
all times.
The goal of time sharing is to change the CPU frequently between processes so that users
can interact with each program while it is running.
To achieve these objectives, the process scheduler selects the process available for execution
on the CPU.
For a single-processor system, there will be one processor and one process will execute at a
time, the rest will have to wait until the CPU is free and rescheduled.
2.1.1. Scheduling Queues
o As processes enter the system, they are inserting into a job queue.
o Job queue consists of all processes in the system.
o The processes that are residing in main memory and are ready and waiting to execute are
kept on a list called the ready queue.
o A ready-queue is implemented using linked list, contains header points to the first and
final PCBs in the list.
o Each PCB includes a pointer field that points to the next PCB in the ready queue.
o When a process is allocated the CPU, it executes for a while and waits for the occurrence
of a particular event, such as the completion of an I/O request.
o The list of processes waiting for a particular I/O device is called a device queue. Each
device has its own device queue. See the below figure:
The main idea behind the medium-term scheduler is that sometimes it is beneficial to
remove processes from memory (and from active contention for the CPU) and thereby reduce the
level of multiprogramming. Later, the process can be reintroduced into memory, and its execution
can be continued where it left off. This scheme is called swapping. The process is swapped out,
and is later swapped in, by the medium-term scheduler.
3. Operations on processes
3.1. Operations on Processes:
The processes in most systems can execute concurrently, and they may be created and
deleted dynamically. Thus, these systems must provide a mechanism for process creation and
termination.
3.1.1. Process Creation:
o Process may create several new processes, via a create-process system call, during the
execution.
o The creating process is called a parent process, and the new processes are called the
children of that process.
o Each new process may create other processes, forming a tree of processes.
o Generally, process identified and managed via a process identifier (pid).
o pid is unique integer value.
o Resource Sharing:
Parent and children share all resources.
Children share subset of parent’s resources.
o Execution:
Parent and children execute concurrently.
Parent waits until children terminate.
o Address space:
The child process is a duplicate of the parent process.
The child process has a new program loaded into it.
o Example:
Unix Operating System:
1. Each process is identified by its process identifier (pid).
2. A new process is created by the fork() system call.
3. The new process consists of a copy of the address space of the original
process. This mechanism allows the parent process to communicate easily
with its child process.
4. Both processes (the parent and the child) continue execution at the
instruction after the fork(), with one difference: the return value for the
fork() is zero for the new (child) process, whereas the (nonzero) process
identifier of the child is returned to the parent.
5. The exec() system call is used after a fork() system call by one of the two
processes to replace the process’s memory space with a new program.
6. The parent waits for the child process to complete with the wait() system
call.
7. When the child process completes the parent process resumes from the
call to wait(), where it completes using the exit() system call.
8. This is also illustrated in below figure.
o Producer-Consumer Problem:
Paradigm for cooperating processes, producer process produces information that
is consumed by a consumer process.
For example,
1. A compiler may produce assembly code, which is consumed by an
assembler.
2. The assembler may produce object modules, which are consumed by the
loader.
3. In client–server paradigm, A Web server produces HTML files, images
consumed by client.
o In IPC, shared memory is the solution for producer-consumer problem.
o A producer can produce one item while the consumer is consuming another item.
o The producer and consumer must be synchronized, so that the consumer does not try to
consume an item that has not yet been produced.
o Two types of buffers can be used for producer-consumer problem:
1. Unbounded buffer:
Unbounded buffer has no limit on the size of the buffer.
The consumer may have to wait for new items, but the producer can
always produce new items.
2. Bounded buffer:
Bounded buffer has limit on the size of the buffer, which is fixed buffer
size.
In this case, the consumer must wait if the buffer is empty, and the
producer must wait if the buffer is full.
2. Message Passing:
o In the message passing model, communication takes place by means of messages
exchanged between the cooperating processes.
o The message passing is illustrated in below figure:
Indirect Communication:
In indirect communication, the messages are sent to and received from
mailboxes, or ports.
The send() and receive() primitives are defined as:
send(A, message)—Send a message to mailbox A.
receive(A, message)—Receive a message from mailbox A.
A communication link in this scheme has the following properties:
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, there may be a
number of different links, with each link corresponding to one
mailbox.
2. Synchronization:
Message passing may be blocking or non-blocking — also known as
synchronous and asynchronous.
Blocking send: The sending process is blocked until the message is received by
the receiving process or by the mailbox.
Non-blocking send: The sending process sends the message and resumes
operation.
Blocking receive: The receiver blocks until a message is available.
Non-blocking receive: The receiver retrieves either a valid message or a null.
3. Buffering:
Whether communication is direct or in-direct, messages exchanged by
communicating processes reside in a temporary queue.
Basically, such queues can be implemented in three ways:
1. 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.
2. 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 must block until space is available in the queue.
3. Unbounded capacity. The queue’s length is potentially infinite; thus, any
number of messages can wait in it. The sender never blocks.
The zero-capacity case is sometimes referred to as a message system with no
buffering; the other cases are referred to as systems with automatic buffering.
o All connections must be unique. Therefore, if another process also on host X wished to
establish another connection with the same Web server, it would be assigned a port
number greater than 1024 and not equal to 1625.
Multithreaded Programming
Overview:
Thread - Definition:
A thread is a basic unit of CPU utilization; it comprises a thread ID, a program counter, a
register set, and a stack.
Single Thread and Multithreaded Process:
A traditional (or heavyweight) process has a single thread of control.
If a process has multiple threads of control, it can perform more than one task at a time.
Below figure illustrates the difference between a traditional single-threaded process and a
multithreaded process.
Process VS Thread
2. The process takes more time to terminate. The thread takes less time to terminate.
3. It takes more time for creation. It takes less time for creation.
If one process is blocked then it will not If a user-level thread is blocked, then all other
10.
affect the execution of other processes user-level threads are blocked.
The process has its own Process Control Thread has Parents’ PCB, its own Thread Control
11.
Block, Stack, and Address Space. Block, and Stack and common Address space.
1. Multithreading models
1.1. Multithreaded Programming:
User Threads:
o User threads supported at user level, and are managed without kernel support.
Kernel Threads:
o Kernel threads are supported and managed directly by the operating system.
o Example:
Operating systems—including Windows, Linux, Mac OS X, Solaris, and Tru64
UNIX (formerly Digital UNIX)—support kernel threads.
There are 3 ways of establishing relationship between user threads and kernel threads.
1. Many-to-One Model
2. One-to-One Model
3. Many-to-Many Model
o The many-to-one model maps many user-level threads to one kernel thread.
o Thread management is done by the thread library in user space, so it is efficient; but the
entire process will block if a thread makes a blocking system call.
o Also, because only one thread can access the kernel at a time, multiple threads are unable
to run in parallel on multiprocessors.
o Green threads — a thread library available for Solaris—uses this model.
2. One-to-One Model:
o One-to-One model is illustrated in below figure:
o The one-to-one model maps each user thread to a kernel thread.
o It provides more concurrency than the many-to-one model by allowing another thread to
run when a thread makes a blocking system call; it also allows multiple threads to run in
parallel on multiprocessors.
o The only drawback to this model is that creating a user thread requires creating the
corresponding kernel thread.
Because the overhead of creating kernel threads can burden the performance of
an application.
o Linux, along with the family of Windows operating systems, implement the one-to-one
model.
3. Many-to-Many Model:
o Many-to-Many model is illustrated in below figure:
2. Thread libraries
2.1. Thread Libraries:
A thread library provides the programmer with an API for creating and managing threads.
There are two ways of implementing a thread library.
First approach:
o It is to provide a library entirely in user space with no kernel support.
o All code and data structures for the library exist in user space.
o Invoking a function in the library results in a local function call in user space and not a
system call.
Second approach:
o It is to implement a kernel-level library supported directly by the operating system.
o All code and data structures for the library exist in kernel space.
o Invoking a function in the API for the library results in a system call to the kernel.
Three main thread libraries are in use today:
1. POSIX Pthreads
2. Win32 threads
3. Java threads
1. POSIX Pthreads:
o May be provided either as user-level or kernel-level.
o A POSIX standard (IEEE 1003.1c) defining API for thread creation and synchronization.
o API specifies behaviour of the thread library, implementation is up to development of
the library Common in Solaris, Linux, Mac OS X andTru64 UNIX.
o pthread_create( ) function is used to create pthreads.
2. Win32 threads:
o The technique for creating threads using the Win32 thread library is similar to the
Pthreads technique in several ways.
o We must include the windows.h header file when using the Win32 API.
o Threads are created in theWin32 API using the CreateThread() function.
3. Java threads:
o Threads are the fundamental model of program execution in a Java program,
o The Java language and its API provide a rich set of features for the creation and
management of threads.
o All Java programs comprise at least a single thread of control—even a simple Java
program consisting of only a main() method runs as a single thread in the JVM.
o There are two techniques for creating threads in a Java program.
1. One approach is to create a new class that is derived from the Thread
class and to override its run() method.
2. The Second Approach is —to define a class that implements the Runnable
interface.
o Creating a Thread object in java does not specifically create the new thread; rather, it is
the start() method that creates the new thread.
o Calling the start() method for the new object does two things:
1. It allocates memory and initializes a new thread in the JVM.
2. It calls the run() method, making the thread eligible to be run by the JVM.
o When the summation java program runs, two threads are created by the JVM.
1. The first is the parent thread, which starts execution in the main() method.
2. The second thread is created when the start() method on the Thread object
is invoked.
Process Scheduling
1. Basic concepts
Single-Programming Systems (or) Single -Processing Systems (or) Single-Tasking Systems:
In a single- tasking system, only one process can run at a time; any others must wait until the
CPU is free and can be rescheduled.
A process is executed until it must wait, typically for the completion of some I/O request. In a
simple computer system, the CPU then just sits idle. All this waiting time is wasted; no useful
work is accomplished. This is overcome in multi-programming systems.
Multi-Programming Systems (or) Multi-Processing Systems (or) Multi-Tasking Systems:
In multi-programming, some process running at all times, in order to maximize CPU utilization.
In this, several processes are kept in memory at one time. Every time a process has to wait,
another process can capture CPU usage.
1.1. CPU–I/O Burst Cycle:
The below figure illustrates CPU-I/O Burst Cycle.
2. Scheduling criteria
Different CPU-scheduling algorithms have different properties.
The following characteristics are used for comparison of scheduling algorithms.
1. CPU utilization:
o We want to keep the CPU as busy as possible.
o In a real system, it should range from 40% (for a lightly loaded system) to 90% (for a
heavily used system).
2. Throughput:
o The number of processes that are completed per time unit, called throughput.
o For long processes, this rate may be one process per hour; for short transactions, it may
be ten processes per second.
3. Turnaround time:
o The interval from the time of submission of a process to the time of completion is the
turnaround time.
o 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:
o Waiting time is the total time spent in the ready queue.
5. Response time:
o Response time is an amount of time it takes from when a request was submitted until the
first response is produced, not output
Note:
The response time is better than turnaround time, because turnaround time is generally limited
by the speed of the output device.
The best scheduling algorithm is to maximize CPU utilization and throughput and to
minimize turnaround time, waiting time, and response time.
Gantt chart, which is a bar chart that illustrates a particular schedule, including the start and
finish times of each of the participating processes.
3. Scheduling algorithms
CPU scheduling deals with the problem of deciding which of the processes in the ready queue is
to be allocated the CPU.
There are many different CPU-scheduling algorithms. They are:
First-Come, First-Served Scheduling
Shortest-Job-First Scheduling
Priority Scheduling
Round-Robin Scheduling
Multilevel Queue Scheduling
Multilevel Feedback Queue Scheduling
o Convoy Effect:
1. If the CPU gets the processes of the higher burst time at the front end of the ready
queue then the processes of lower burst time may get blocked which means they
may never get the CPU if the job in the execution has a very high burst time. This
is called convoy effect or starvation.
2. Shortest-Job-First Scheduling (SJFS):
o SJF scheduling algorithm, schedules the processes according to their burst time.
o In SJF scheduling, the process with the lowest burst time, among the list of available
processes in the ready queue, is going to be scheduled next.
o However, it is very difficult to predict the burst time needed for a process hence this
algorithm is very difficult to implement in the system.
o Example:
1. Consider the following set of processes that arrive at time 0, with the length of the
CPU burst given in milliseconds:
Process Burst Time
P1 6
P2 8
P3 7
P4 3
2. The Gantt Chart for the SJFS is:
2. Consider the following set of processes with different arrival times and with the length
of the CPU burst given in milliseconds:
Process Arrival Time Burst Time
P1 0 2
P2 1 6
P3 2 4
P4 3 9
P5 6 12
Calculate Completion Time, Turn Around Time, Waiting Time and Average
Waiting Time using SJFS Algorithm.
3. Consider the following set of processes with different arrival times and with the length
of the CPU burst given in milliseconds:
Process Arrival Time Burst Time
P1 1 7
P2 3 3
P3 6 2
P4 7 10
P5 9 8
Calculate Completion Time, Turn Around Time, Waiting Time and Average
Waiting Time using SJFS Algorithm
4. Consider the following set of processes with different arrival times and with the length
of the CPU burst given in milliseconds:
Process Arrival Time Burst Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5
Calculate Completion Time, Turn Around Time, Waiting Time and Average
Waiting Time using
1. Pre-emptive SJFS (shortest-remaining-time-first
scheduling) Algorithm
2. Non-Pre-emptive SJFS Algorithm
3. Priority Scheduling:
o In Priority scheduling, there is a priority number assigned to each process.
o In some systems, the lower number has higher priority. While, in the others, the higher
number has higher priority.
o The Process with the higher priority among the available processes is given the CPU.
o There are two types of priority scheduling algorithm:
Non Pre-emptive priority scheduling
Pre-emptive Priority scheduling.
1. Non Pre-emptive priority scheduling:
2. In the Non Preemptive Priority scheduling, The Processes are scheduled according to the
priority number assigned to them.
3. Once the process gets scheduled, it will run till the completion.
4. Example
In the Example, there are 7 processes P1, P2, P3, P4, P5, P6 and P7. Their priorities,
Arrival Time and burst time are given in the table.
Solution:
We can prepare the Gantt chart according to the Non Pre-emptive priority
scheduling.
The Process P1 arrives at time 0 with the burst time of 3 units and the priority
number 2. Since No other process has arrived till now hence the OS will schedule
it immediately.
Meanwhile the execution of P1, two more Processes P2 and P3 are arrived. Since
the priority of P3 is 3 hence the CPU will execute P3 over P2.
Meanwhile the execution of P3, All the processes get available in the ready
queue. The Process with the lowest priority number will be given the priority.
Since P6 has priority number assigned as 4 hence it will be executed just after P3.
After P6, P4 has the least priority number among the available processes; it will
get executed for the whole burst time.
Since all the jobs are available in the ready queue hence All the Jobs will get
executed according to their priorities. If two jobs have similar priority number
assigned to them, the one with the least arrival time will be executed.
From the Gantt chart prepared, we can determine the completion time of every
process. The turnaround time, waiting time and response time will be determined.
Turn Around Time = Completion Time - Arrival Time
Waiting Time = Turn Around Time - Burst Time
Process Arrival Burst Completion Turnaround Waiting Response
Priority
Id Time Time Time Time Time Time
1 2 0 3 3 3 0 0
2 6 2 5 18 16 11 13
3 3 1 4 7 6 2 3
4 5 4 2 13 9 7 11
5 7 6 9 27 21 12 18
6 4 5 4 11 6 2 7
7 10 7 10 37 30 20 27
Average Waiting Time = (0+11+2+7+12+2+20)/7 = 54/7 = 7.71 units
Solution
GANTT chart Preparation
At time 0, P1 arrives with the burst time of 1 units and priority 2. Since no other
process is available hence this will be scheduled till next job arrives or its
completion (whichever is lesser).
During the execution of P3, three more processes P4, P5 and P6 becomes
available. Since, all these three have the priority lower to the process in execution
so PS can't preempt the process. P3 will complete its execution and then P5 will
be scheduled with the priority highest among the available processes.
Meanwhile the execution of P5, all the processes got available in the ready
queue. At this point, the algorithm will start behaving as Non Preemptive Priority
Scheduling. Hence now, once all the processes get available in the ready queue,
the OS just took the process with the highest priority and execute that process till
completion. In this case, P4 will be scheduled and will be executed till the
completion.
Since P4 is completed, the other process with the highest priority available in the
ready queue is P2. Hence P2 will be scheduled next.
P2 is given the CPU till the completion. Since its remaining burst time is 6 units
hence P7 will be scheduled after this.
The only remaining process is P6 with the least priority, the Operating System
has no choice unless of executing it. This will be executed at the last.
The Completion Time of each process is determined with the help of GANTT
chart. The turnaround time and the waiting time can be calculated by the
following formula.
Turnaround Time = Completion Time - Arrival Time
Waiting Time = Turn Around Time - Burst Time
Process Priority Arrival Burst Completion Turnaround Waiting
Id Time Time Time Time Time
1 2 0 1 1 1 0
2 6 1 7 22 21 14
3 3 2 3 5 3 0
4 5 3 6 16 13 7
5 4 4 5 10 6 1
6 10 5 15 45 40 25
7 9 15 8 30 20 12
Average Waiting Time = (0+14+0+7+1+25+12)/7 = 8.43 units
Drawback with priority scheduling algorithms:
A major problem with priority scheduling algorithms is indefinite blocking, or
starvation.
o A process that is ready to run but waiting for the CPU can be considered blocked.
A priority scheduling algorithm can leave some low priority processes waiting
indefinitely.
GANTT chart
The P1 will be executed for 4 units first.
Ready Queue
Meanwhile the execution of P1, four more processes P2, P3, P4 and P5 arrives in
the ready queue. P1 has not completed yet, it needs another 1 unit of time hence
it will also be added back to the ready queue.
Process P2 P3 P4 P5 P1
Remaining Burst Time 6 3 1 5 1
GANTT chart
After P1, P2 will be executed for 4 units of time which is shown in the Gantt
chart.
Ready Queue
During the execution of P2, one more process P6 is arrived in the ready queue.
Since P2 has not completed yet hence, P2 will also be added back to the ready
queue with the remaining burst time 2 units.
Process P3 P4 P5 P1 P6 P2
Remaining Burst Time 3 1 5 1 4 2
GANTT chart
After P1 and P2, P3 will get executed for 3 units of time since its CPU burst time
is only 3 seconds.
Ready Queue
Since P3 has been completed, hence it will be terminated and not be added to the
ready queue. The next process will be executed is P4.
Process P4 P5 P1 P6 P2
Remaining Burst Time 1 5 1 4 2
GANTT chart
After, P1, P2 and P3, P4 will get executed. Its burst time is only 1 unit which is
lesser then the time quantum hence it will be completed.
Ready Queue
The next process in the ready queue is P5 with 5 units of burst time. Since P4 is
completed hence it will not be added back to the queue.
Process P5 P1 P6 P2
Remaining Burst Time 5 1 4 2
GANTT chart
P5 will be executed for the whole time slice because it requires 5 units of burst
time which is higher than the time slice.
Ready Queue
P5 has not been completed yet; it will be added back to the queue with the
remaining burst time of 1 unit.
Process P1 P6 P2 P5
Remaining Burst Time 1 4 2 1
GANTT Chart
The process P1 will be given the next turn to complete its execution. Since it only
requires 1 unit of burst time hence it will be completed.
Ready Queue
P1 is completed and will not be added back to the ready queue. The next process
P6 requires only 4 units of burst time and it will be executed next.
Process P6 P2 P5
Remaining Burst Time 4 2 1
GANTT chart
P6 will be executed for 4 units of time till completion.
Ready Queue
Since P6 is completed, hence it will not be added again to the queue. There are
only two processes present in the ready queue. The Next process P2 requires only
2 units of time.
Process P2 P5
Remaining Burst Time 2 1
GANTT Chart
P2 will get executed again, since it only requires only 2 units of time hence this
will be completed.
Ready Queue
Now, the only available process in the queue is P5 which requires 1 unit of burst
time. Since the time slice is of 4 units hence it will be completed in the next
burst.
Process P5
Remaining Burst Time 1
GANTT chart
P5 will get executed till completion.
The completion time, Turnaround time and waiting time will be calculated as
shown in the table below.
Turn Around Time = Completion Time - Arrival Time
Waiting Time = Turn Around Time - Burst Time
o 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.
o Each queue has its own scheduling algorithm.
o 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.
o In addition, there must be scheduling among the queues, which is commonly implemented as
fixed-priority pre-emptive scheduling.
o For example, the foreground queue may have absolute priority over the background queue.
o Let’s look at an example of a multilevel queue scheduling algorithm with five queues, listed
below in order of priority:
1. System processes
2. Interactive processes
3. Interactive editing processes
4. Batch processes
5. Student processes
o Each queue has absolute priority over lower-priority queues.
o There is a 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.
o 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, whereas the
background queue receives 20 percent of the CPU to give to its processes on an FCFS
basis.
o In multilevel queue scheduling algorithm processes do not move from one queue to
the other, since processes do not change their foreground or background nature.
This setup has the advantage of low scheduling overhead, but it is inflexible.
6. Multilevel Feedback Queue Scheduling:
o The multilevel feedback queue scheduling algorithm allows a process to move between
queues.
o If a process uses too much CPU time, it will be moved to a lower-priority queue.
o This scheme leaves I/O-bound and interactive processes in the higher-priority queues.
o In addition, a process that waits too long in a lower-priority queue may be moved to a
higher-priority queue.
o This form of aging prevents starvation.
o For example, consider a multilevel feedback queue scheduler with three queues,
numbered from 0 to 2. See the below figure:
o The scheduler first executes all processes in queue 0. Only when queue 0 is empty will it
execute processes in queue 1.
o Similarly, processes in queue 2 will only be executed if queues 0 and 1 are empty.
o A process that arrives for queue 1 will pre-empt a process in queue 2. A process in queue
1 will in turn be preempted by a process arriving for queue 0.
o In general, a multilevel feedback queue scheduler is defined by the following
parameters:
1. The number of queues
2. The scheduling algorithm for each queue
3. The method used to determine when to upgrade a process to a higher priority
queue
4. The method used to determine when to demote a process to a lower priority
queue
5. The method used to determine which queue a process will enter when that
process needs service
o In the above figure, the processor can spend up to 50 percent of its time waiting for data
to become available from memory.
o
5. Virtualization and Scheduling:
o A system with virtualization, even a single-CPU system, frequently acts like a
multiprocessor system.
o The virtualization software presents one or more virtual CPUs to each of the virtual
machines running on the system and then schedules the use of the physical CPUs among
the virtual machines.
o In general, most virtualized environments have one host operating system and many
guest operating systems.
o The host operating system creates and manages the virtual machines, and each virtual
machine has a guest operating system installed and applications running within that
guest.
o Depending on how busy the system is, the time slice may take a second or more,
resulting in very poor response times for users logged into that virtual machine.
o Commonly, virtual machines are incorrect because timers take longer to trigger than they
would on dedicated CPUs.
o Virtualization can thus undo the good scheduling-algorithm efforts of the operating
systems within virtual machines.