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

OS-Module-1 (6 Files Merged)

Uploaded by

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

OS-Module-1 (6 Files Merged)

Uploaded by

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

Module 1

INTRODUCTION TO OPERATING
SYSTEMS
Program

A Program is a set of statements or instructions which collectively performs a particular task.

Software

A Software is a set of programs which collectively performs a set of activities.

Types of software

1) System software
2) Application software
System software

Software which provides services to other software is called as system software.

Example system software are: Operating System, Compiler, Linker, Loader, Assembler, debugger, driver and so on.

System Software is used for operating the computer hardware.

System Software is generally installed in the computer when the operating system is installed.

The user does not interact with the system software because it works in the background.

System software provides platform for running application software.

Application software

Software which depends on other software for services is called as application software.

Example application software are: VLC player, web browser, word processor and so on.

Application software is used by the user to perform a specific task.


Application software are installed according to user’s requirements.

The user interacts with application software.

Application software can’t run without the presence of system software.

Computer

A computer contains number of hardware and software components.

Each component of the computer system is called a resource.

Some of the hardware resources are: processor, HD, RAM, keyboard, monitor and so on.

Some of the software resources are: operating system, compiler, interpreter, loader, assembler and so on.
Operating System
An Operating System is a system software that manages the computer hardware.

Operating System acts as an intermediary between the computer user and the computer hardware.

Some operating systems are designed to provide convenient communication between user and computer system, others
for efficient utilization of resources, and others to provide both.

Role of Operating System


A computer system can be logically divided into four components: the hardware, the operating system, the application
programs, and the users as shown in above figure.

The hardware (the central processing unit, the memory and the I/O devices) provides the computing resources for the
computer system.

The system programs (compilers, assemblers and so on) and application programs (word processors, spreadsheets, web
browsers and so on) help the user programs in their execution.

The operating system controls the hardware and coordinates the use of hardware among various user programs.
The role of operating system is fully explained by considering two views: user view and system view.

User view

Computer systems are divided into four categories from user point of view.

1) Personal computers

Used by single person for home applications. The Operating System used in personal computers should focus on the
following

Easy use of the system


Utilization of resources
Performance of the system

2) Thin clients

A thin client is a system which do not have any processing capability and storage capacity.

It is used for typing the program and displaying the output.

Thin clients are connected to a server system which has storage capacity and execution capability.
The programs typed in the thin clients are stored and executed in server system.

The results of execution are passed to client systems.

Ex: Oracle server

The operating system used in thin clients should concentrate on resource utilization.

3) Client – Server system

Number of client systems are connected to a server system.

A client system makes a request to a server system for any service.

The server system provides service to all client systems connected to it.

The Operating System used in client system should use resources of the client system as well as resources available at the
server system.
4) Home devices or devices used in automobiles

An example for home device is a microwave oven.

The Operating System used in home devices should work automatically without any intervention of user.

System view

From system point of view, the Operating System can be viewed as resource allocator.

The Operating System allocates the resources of the computer system to the programs that are currently running in the
system.
Computer System Organization
A modern computer system consists of one or more CPUs and number of devices.

The CPUs and devices are connected to the system bus as shown in diagram.

The devices are connected to the system bus through device controllers.

Each device controller controls the operations of a device or a set of devices.

A device controller maintains some local buffer storage and a set of special-purpose registers.

The device controller is responsible for moving the data between the devices that it controls and its local buffer storage.

operating systems have a device driver for each device controller.

When the computer system is booted, a program called bootstrap program starts running.

The bootstrap program resides in ROM or EEPROM.

Role of bootstrap program is locating the operating system, loading the operating system into RAM and starting the
execution of operating system.

When the operating system is started then the operating system controls the operations of computer system.
Interrupts

Consider a program performing I/O.

To start an I/O operation, the device driver loads the appropriate registers in the device controller.

The device controller, in turn, examines the contents of these registers to determine what action to take (such as “read a
character from the keyboard”).

The controller starts the transfer of data from the device to its local buffer.

Once the transfer of data is complete, the device controller informs the device driver that it has finished its operation by
raising an interrupt (a signal).

The device driver then returns the data if the operation was a read.

For other operations, the device driver returns status information such as “write completed successfully” or “device busy”.

When an interrupt is raised, the CPU stops what it is doing and immediately transfers execution to the interrupt service
routine.

The interrupt service routine executes; on completion, the CPU resumes the interrupted computation.
Following figure summarizes the interrupt-driven I/O cycle.
Storage Structure
Registers

Registers are small, high-speed memory units located in the CPU.

They are used to store the most frequently used data and instructions.

Registers have the fastest access time and the smallest storage capacity, typically ranging from 16 to 64 bits.

Cache Memory

Cache memory is a small, fast memory unit located close to the CPU.

It stores frequently used data and instructions that have been recently accessed from the main memory.

Cache memory is designed to minimize the time it takes to access data by providing the CPU with quick access to frequently
used data.

Main Memory

Main memory, also known as RAM (Random Access Memory), is the primary memory of a computer system.

It has a larger storage capacity than cache memory, but it is slower.


Main memory is used to store data and instructions that are currently in use by the CPU.

Secondary Storage

Secondary storage, such as hard disk drives (HDD) and solid-state drives (SSD), is a non-volatile memory unit that has a
larger storage capacity than main memory.

It is used to store data and instructions that are not currently in use by the CPU.

Secondary storage has the slowest access time and is typically the least expensive type of memory in the memory hierarchy.

Magnetic Disk

Magnetic disks are simply circular plates that are fabricated with either a metal or a plastic or a magnetized material.

The Magnetic disks work at a high speed inside the computer and these are frequently used.

Magnetic Tape

Magnetic tape is simply a magnetic recording device that is covered with a plastic film. It is generally used for the backup of
data.

In the case of a magnetic tape, the access time for a computer is a little slower and therefore, it requires some amount of
time for accessing the strip.
Operating System Operations
Following are the major operations or functions of an operating system

1. Process Management
2. Memory Management
3. File Management
4. I/O device Management
5. Protection
6. Security
7. Networking

Process Management

Process is a program in execution state.

To mange processes, the Operating System performs the following activities

1) creating and deleting processes


2) suspending and resuming processes
3) providing synchronization
4) providing communication
5) handling deadlocks
6) CPU scheduling
Creating and deleting processes

To create a new process, the operating system has to do the following activities

1) Move the program from secondary memory (HD) to primary memory (RAM)
2) Allocate CPU or processor to the program in primary memory

To delete a process, the Operating System has to move the program form primary memory (RAM) to secondary memory
(HD).

Suspending and resuming processes

Suspending a process is temporarily stopping the execution of process due to several reasons like requesting for the input
from the user.

Before suspending the process, the Operating System has to save the status of the execution of the process.

Resuming a suspended process is restarting the execution of the suspended process when the process becomes ready for
execution.

Before resuming the process, the Operating System has to restore the saved status of the process.
Providing synchronization

A resource that can be shared by a number of processes at a time is called Sharable resource.

Ex: RAM, File etc.

A resource that can be used by only one process at a time is called Non-sharable resource.

Ex: CPU, printer.

When a number of processes requests for a non-sharable resource at the same time, the Operating System has to provide
synchronous access to the resource by allocating the resource to only one process at a time.

Providing communication

Programs or processes communicate for sharing or accessing data.

Two methods used by operating system for providing communication between processes are:

1) Shared memory
2) Message passing
Handling deadlocks

A set of processes is said to be in deadlock state if each process in the set is waiting for another process in the same set of
processes.

The Operating System has to allocate the resources to processes such that the system should not go into the deadlock
state.

If a deadlock state occurs in the computer system, then the operating system has to detect the occurrence of deadlock
state and recover the computer system from deadlock state.

CPU Scheduling

When a number of programs are ready for execution, the operating system decides an order for executing the program.
Memory Management

Operating system has to manage both primary memory (RAM) and secondary memory (Hard disk).

For managing the primary memory, the Operating System is responsible for

1) Knowing which parts of the RAM are in use and by whom


2) Allocating & de-allocating space when required
3) Deciding which process needs to be moved between RAM and Hard disk

RAM
The hard disk contains number of storage blocks.

At any time, some of the blocks contains programs or data and remaining blocks are free.

For managing the secondary memory, the Operating System is responsible for

1) Free – space management


2) Storage allocation
3) Disk scheduling
File Management

File is a logical storage unit.

The Operating System maps files onto storage devices.

Files are organized into directories to make them easier to use.

The Operating System is responsible for the following activities for managing files

1) Creating and deleting files


2) Creating and deleting directories
3) Supporting primitives for manipulating files and directories
4) Backing up files on stable storage

I/O device Management

A computer system contains a number of input and output devices like keyboard, mouse, monitor, printer and so on.

Each device is controlled by a device driver.

The Operating System has to manage all these device drivers.


Networking

A network is a collection of systems connected to each other.

The Operating System used in a computer which is connected to a network must provide support for

1) Sharing the resources available in the network.


2) Dividing the program into parts and distributing them to different systems in the network.
3) Providing transparency to the users.

Protection and Security

Protection: restricting access to the resources of the system from the processes that are running in the system.

For example, if two or more processes request the CPU at the same time then the Operating System has to restrict the
access to the CPU by allocating the CPU to only one process at a time.

Security: restricting access to the system by the users.

Username and password are used to provide security.


Types of Operating Systems
1) Batch processing operating system
2) Multiprogramming operating system
3) Timesharing operating system
4) Distributed operating system
5) Real-time operating system
6) Embedded operating system
7) Mobile operating system
Batch processing operating system

If the computer system is running with batch processing operating system then the execution of programs in the computer
system is as follows:

The programs that users want to execute and the input data required for executing the programs are collected into a batch.

The batch of programs is then loaded into computer system for execution.

The batch of programs is then executed in sequential manner.

There is no interaction between users and computer during execution of the programs.

The outputs generated after execution of programs is collected into a batch and then distributed to users.

IBM’s Z/OS is an example Batch operating system.


Multiprogramming operating system

When only one program is loaded into main memory and if that program requires any i/o operation during its execution
then the CPU will be in idle state until the i/o operation is completed.

This leads to less utilization of resources (CPU, i/o devices).

To increase the utilization of resources, a number of programs is loaded into main memory.

During execution of a program, if the program requires any i/o operation then the CPU is switched to another program so
that the CPU is busy at all times.

Loading a number of programs at a time into main memory is called multiprogramming.

Multiprogramming operating system allows loading of a number of programs at a time into RAM.

Multiprogramming operating system switches the CPU from current program to another program when the current process
is completed or goes to the waiting state.

Windows and LINUX are examples for multiprogramming operating systems.


Timesharing operating system

Timesharing operating system loads a number of programs at a time into main memory.

Allow each program to execute for a certain amount of time only. After that the CPU is switched to another program.

Each program has equal chance of getting the CPU.

Timesharing operating system switches the CPU from current program to another program in the following cases:

1) when the current process is completed


2) when the current process goes to the waiting state
3) when the allocated time slice is over.

The user can interact with his program while it is running.

A time-shared operating system allows many users to share the computer simultaneously.

Windows and LINUX are examples for timesharing operating systems.


Distributed operating system

Distributed operating system manages a group of independent computers and makes them appear to be a single computer.

The group of computers is connected through a network.

Following are the features of distributed operating systems:

Resource sharing

Resources can be shared by the computer systems connected to the network.

For example, if a printer is connected to the network then all systems in the network can share the printer.

Reliability of resources

If any resource of any system fails then the resource of other system in the network can be used.

Speed up of computations

Programs can be executed in less time by dividing the program into number of parts and executing these parts on different
systems in the network.
Communication between systems

If a number of persons is involved in the development of any project then there should be some communication between
the systems that are being used by the persons.

This communication can be provided easily by the distributed operating system.

Providing Transparency

The distributed operating system hides the details of execution of the program from the user.

Amoeba and LOCUS are examples for distributed operating systems.

Real time operating system

Real time operating systems are used in computer systems which executes real time applications.

Real time applications have fixed time constraints on processing.

For example, if there is a satellite which sends some data for every 100 seconds and if a computer system receives and
stores that data then the operating system used in that computer system is a real time operating system.
In this example, the operating system has to receive and store the data within 99 seconds.

Windows CE and Symbian are examples for real time operating systems.

Embedded operating system

The operating systems designed for being used in embedded computer systems are known as embedded operating systems.

Embedded operating systems are designed to operate on small machines like PDAs with less autonomy.

Embedded operating systems are able to operate with a limited number of resources.

Embedded operating systems are very compact and extremely efficient.

Windows CE and FreeBSD are some examples of embedded operating systems.

Mobile operating system

A mobile operating system controls a mobile device and its design supports wireless communication and mobile
applications.

Tablet PCs and smart phones run on mobile operating systems.

Blackberry OS, Google's Android and Apple's iOS are some of the most known names of mobile operating systems.
System calls
The operating system provides services to the user programs through system calls.

System calls are generally developed in C and C++ languages.

Some system calls which directly interacts with hardware devices are developed in assembly language.

System calls are executed in kernel of operating system when a user program requests the operating system for any service.

Kernel is the major part in the operating system (like a heart in human body).

The programmers develop programs using Application Programming Interface (API).

The API contains set of functions/methods using which the programmer develops programs.

Each function or method in the API is linked to a system call.

When any API method is executed in the program then the corresponding system call is invoked.

For example, when a user program calls a scanf() function for reading data from keyboard then the system call ‘read()’ is
invoked and executed in the kernel of the operating system.
There are two main reasons for writing programs using API instead of system calls directly.

1) Program written using API can be executed on any system that supports the same API.
2) Working with system calls is more difficult than API.

System call interface

The run-time support system for most programming languages provides a system-call interface that serves as the link to
system calls made available by the operating system.

During execution of user program, when any API function is executed then the system-call interface identifies the
corresponding system call and activates that system call within the operating system.

The system-call interface also returns the status of the system call and any return values.
Types of system calls

1) Process control system calls


2) File management system calls
3) Device management system calls
4) Information maintenance system calls
5) Communication maintenance system calls
6) Protection and security maintenance system calls

Process control system calls

The various system calls for controlling processes are

1. end, abort
2. load, execute
3. create process, terminate process
4. get process attributes , set process attributes
5. wait, signal
File management system calls

1. create, delete
2. open, close
3. read, write, reposition
4. get file attributes, set file attributes

Device management system calls

1. request device, release device


2. read, write
3. get attributes, set attributes

Information maintenance system calls

1. get time or date, set time or date


2. get system data, set system data
Communication maintenance system calls

1. open connection, close connection


2. get hostid, get processid
3. send message, receive message
4. shared memory create, shared memory attach

protection

1. set permission, get permission


2. allow user, deny user
Dual mode operation of the computer system
A modern computer system operates in 2 modes in order to protect the operating system code from user processes and also
the code of each process from other processes.

The two modes are:


1. User mode
2. Kernel mode (supervisor mode, privileged mode or system mode)
A bit called ‘mode bit’ indicates the current mode.

The value of mode bit is ‘0’ for kernel mode and ‘1’ for user mode.
When the system is booted, the system is running in kernel mode.

When the operating system starts any user program then the mode of system is switched to user mode.

During execution, if the user program requests any service from the operating system then the mode is changed to kernel
mode.

After executing the system call, the mode is switched to user mode by setting mode bit to ‘1’ before passing the control to
user program.
To protect the operating system code, some of the machine instructions are designated as privileged instructions.
These privileged instructions are executed only in kernel mode.
The instruction used to change the mode bit is an example for privileged instruction.
When any privileged instruction is executed in user mode then the hardware informs to the operating system.
Virtualization
Virtualization is a technology that allows us to abstract the hardware of a single computer into several different execution
environments, thereby creating the illusion that each separate environment is running on its own private computer.

These environments can be viewed as different individual operating systems that may be running at the same time and may
interact with each other.

A user of a virtual machine can switch among the various operating systems.

Virtualization allows operating systems to run as applications within other operating systems.

With virtualization, an operating system that is natively compiled for a particular CPU architecture runs within another
operating system also native to that CPU.

VMware created a new virtualization technology in the form of an application that ran on Windows.

That application ran one or more guest copies of Windows or other native x86 operating systems, each running its own
applications. (See Figure 1.16.)

Windows was the host operating system, and the VMware application was the virtual machine manager (VMM).
The VMM runs the guest operating systems, manages their resource use, and protects each guest from the others.

On laptops and desktops, a VMM allows the user to install multiple operating systems to run applications written for
operating systems other than the native host.

For example, an Apple laptop running macOS on the x86 CPU can run a Windows 10 guest to allow execution of Windows
applications.
Module 2

PROCESS MANAGEMENT
Process
A program is a set of statements or instructions which collectively implements a task.

A process is a program being executed.

Program resides in secondary memory.

Process resides in primary memory.

The memory allocated by the operating system for a process consists of four parts as shown in following figure.
The text part contains instructions or statements of the process.

Data part contains global variables.

Stack contains temporary data like function parameters, return values and so on.

Heap part contains the objects that are dynamically created during execution of process.

Process States
A process at any particular time will be in any one of the following states

1) New - The process is about to be created but not yet created. It is the program that is present in secondary memory that
will be picked up by the Operating System to create the process.

2) Ready - The process is loaded into the main memory. The process here is ready to run and is waiting to get the CPU time
for its execution.

3) Running – The process is executing.

4) Waiting – The process requests access to I/O or needs input from the user. The process continues to wait in the main
memory and does not require CPU.
5) Exit – The execution of process is completed. The resources allocated to the process will be released or deallocated.

The change in state of a process is indicated through the following diagram

When the process is ready for execution on allocation of CPU then the process moves from new state to ready state.

When CPU is allocated to the process then the process moves from ready state to running state.
During execution of the process:

1) If the process is completed then the process moves from running state to exit state.

2) If any i/o operation is requested or the process waits for an event then the process moves from running state to waiting
state.

3) If the allocated time is over or an interrupt occurs then the process moves from running state to ready state.

When the requested i/o operation is completed or the event is completed then the process moves from waiting state to
ready state.

Process Control Block (PCB)


Operating system creates a separate process control block for each process that is running in the computer system.

Process control block contains information about the process.

The information stored in the process control block of a process is:


Process state
CPU registers
CPU scheduling information
Memory management information
Accounting information
I/O information
.
.
.

Process state: indicates current state of the process.

CPU registers: indicates the values stored in the registers.

CPU scheduling information: indicates scheduling information like priority of process.

Memory management information: indicates the starting and ending positions or addresses of process in the RAM.

Accounting information: indicates process id, amount of CPU time required and so on.

I/O status information: indicates the list of i/o devices allocated to the process, the list of open files and so on.
Context switch

Context switch is switching the CPU from one process to other process.

Operating system switches the CPU from the current process to another process when:

1) The execution of current process is completed


2) The execution of current process is suspended due to some I/O operation request
3) The allotted time for the current process is over

When the Operating System switches the CPU from one process to other process then the Operating System has to update
the PCBs of the processes.

The following diagram shows state change in process control block when context switch occurs.
Initially CPU is allocated to process P0.

While executing process P0, if process P0 invokes a system call or requests any i/o operation or an interrupt occurs then

1) the execution of process P0 is suspended


2) the state of process P0 is saved into PCB0
3) the state of process P1 is reloaded from PCB1
4) the CPU is allocated to process P1.

While executing process P1, if process P1 invokes a system call or requests any i/o operation or an interrupt occurs then

1) the execution of process P1 is suspended


2) the state of process P1 is saved into PCB1
3) the state of process P0 is reloaded from PCB0
4) the CPU is allocated to process P0.
Process Scheduling
To increase the utilization of CPU, the operating system loads a number of programs at a time into RAM.

When number of programs are ready for execution then the operating system has to decide an order for executing the
programs (i.e. the operating system has to schedule the execution of programs).

Scheduling queues

Operating system maintains two types of queues

1) Ready queue
2) Device queues

Ready queue

The ready queue contains the PCBs of processes that are ready for execution.

Device queue

A device queue is maintained for each device. The device queue of a device contains the PCBs of the processes that are
waiting for that device.
Queuing diagram

The queuing diagram shows how a process moves between different queues during its life time.
In the queuing diagram, each rectangle represents a queue and each circle represents a resource.

When a process enters into the system then it is put into ready queue.

A process waits in the ready queue until the CPU is allocated to the process.

When CPU is allocated to the process then the execution of the process begins.

During execution of the process:

1) The process may request for any i/o operation.

In this case, execution of the process is suspended and it is put into device queue of the device for which it made the
request.

The process waits in device queue till the completion of the i/o operation.

After the completion of i/o operation, the process is put into the ready queue.

2) The allocated time slice is completed.

In this case, execution of the process is suspended and is put into the ready queue.
3) The process may create a child process.

In this case, execution of the process is suspended and the process waits for the completion of child process.

After completion of child process, the process is put into the ready queue.

4) An interrupt may be raised.

In this case, execution of the process is suspended and the process waits for completion of processing of the interrupt.

After processing the interrupt, the process is put into ready queue.
Schedulers

A process moves around different queues during its life time.

The operating system uses different types of schedulers to select processes form these queues.

Different schedulers used by the operating system are:

1) Long Term Scheduler (LTS)


2) Medium Term Scheduler (MTS)
3) Short Term Scheduler (STS) or CPU scheduler

The following process state diagram shows when the operating system activates these schedulers.
When a process requests any i/o operation during its execution then the process moves to the waiting state.

A waiting process is in the RAM.

When number of processes in the RAM are in waiting state then the operating system moves some of the waiting processes
from RAM to hard disk in order to load new processes into RAM.

When a waiting process is moved from RAM to hard disk then the process is said to be in the blocked state.

After completion of the i/o operation, the process goes to the ready state from either the waiting or the blocked state.

Long term Scheduler

Long term scheduler selects one process (program) from the list of new processes (programs) in the hard disk and loads that
process into RAM.

Medium term scheduler

Medium term scheduler selects one process from the list of waiting processes in the hard disk and loads that process into
RAM.
Short term scheduler or CPU scheduler

Short term scheduler selects one process from the ready queue and allocates the CPU to that process.

CPU scheduler uses a Dispatcher module.

The Dispatcher module switches the CPU to the process selected from the ready queue.

The time required to switch the CPU to the selected process is called Dispatching Latency.

Short term scheduler uses various scheduling algorithms (CPU scheduling algorithms) for selecting a process from the ready
queue.

The CPU scheduling algorithms are divided into two categories

1) Preemptive
2) Non-preemptive

In preemptive scheduling, the CPU can be switched from current process to other process forcibly.

In non-preemptive scheduling, the CPU cannot be switched from current process to other process until the current process
releases the CPU.
Scheduling Criteria

Parameters used to evaluate the performance of CPU scheduling algorithms.

The different scheduling criteria are:

CPU utilization: the percentage of time for which the CPU is busy.

Throughput: the number of processes completed per unit time.

Turnaround time: difference between the time at which the process has arrived into system and the time at which the
process has completed its execution.

turnaround time=completion time-arrival time

Waiting time: the sum of periods for which the process is waiting in the ready queue.

waiting time=turnaround time-burst time

Response time: difference between the time at which the process has arrived into system and the time at which the first
response has come out from the process.
A good scheduling algorithm should maximize CPU utilization, throughput and minimize turnaround time, waiting time and
response time.

CPU scheduling algorithms

1) First Come First Serve (FCFS)


2) Shortest Job First (SJF)
3) Priority
4) Round Robin (RR)
5) Multilevel Queue
6) Multilevel Feedback Queue

First Come First Serve (FCFS) scheduling algorithm

FCFS is non-preemptive scheduling algorithm.

With FCFS scheduling, the processes are executed in the order in which they enter into the system.
Consider the following set of processes

Process Burst time Arrival time


P1 8 0
P2 5 0
P3 3 0
P4 6 0

The order of execution of these processes with FCFS is indicated with following Gantt chart

Gantt chart:
P1
P1 P2 P3 P2P4
0 8 13 16 22 P3
P4
Process Burst time Arrival time Turnaround time Waiting time
P1 8 0 8-0=8 8-8=0
P2 5 0 13-0=13 13-5=8
P3 3 0 16-0=16 16-3=13
P4 6 0 22-0=22 22-6=16

The average waiting time is (0+8+13+16)/4=9.25


Ex: Process Burst time Arrival time
P1 8 0
P2 5 1
P3 3 2
P4 6 4

The order of execution of these processes with FCFS is indicated with following Gantt chart

Gantt chart:

P1 P2 P3 P4

0 8 13 16 22

Process Burst time Arrival time Turnaround time Waiting time


P1 8 0 8-0=8 8-8=0
P2 5 1 13-1=12 12-5=7
P3 3 2 16-2=14 14-3=11
P4 6 4 22-4=18 18-6=12

The average waiting time is (0+7+11+12)/4=7.5


Advantages:

1) Simple and easy to implement.

Disadvantages:

1) Not suitable for time sharing operating systems.


2) Average waiting time is high.

Shortest Job First (SJF) scheduling algorithm

SJF has two versions: preemptive and non-preemptive.

With SJF algorithm, the process with least burst time is executed first.

If the burst time of two or more processes is same then the processes are executed in FCFS order.
Non-preemptive SJF

Process Burst time Arrival time


P1 8 0
P2 5 0
P3 3 0
P4 6 0

The order of execution of these processes with non-preemptive SJF is indicated with following Gantt chart

Gantt chart:

0 3 8 14 22

Process Burst time Arrival time Turnaround time Waiting time


P1 8 0 22-0=22 22-8=14
P2 5 0 8-0=8 8-5=3
P3 3 0 3-0=3 3-3=0
P4 6 0 14-0=14 14-6=8

The average waiting time is (14+3+0+8)/4=6.25


Ex: Process Burst time Arrival time
P1 8 0
P2 5 1
P3 3 2
P4 6 4

The order of execution of these processes with non-preemptive SJF is indicated with following Gantt chart

Gantt chart:

0 8 11 16 22

Process Burst time Arrival time Turnaround time Waiting time


P1 8 0 8-0=8 8-8=0
P2 5 1 16-1=15 15-5=10
P3 3 2 11-2=9 9-3=6
P4 6 4 22-4=18 18-6=12

The average waiting time is (0+10+6+12)/4=7


Preemptive SJF

With preemptive SJF, current process is allowed to execute till the arrival of next process.

At the arrival of next process, CPU is allocated to the process with shortest remaining burst time.

Another name of preemptive SJF is “Shortest Remaining Time First” (SRTF).

For example, consider the following set of processes


Process Burst time Arrival time
P1 8 0
P2 5 0
P3 3 0
P4 6 0

The order of execution of these processes with preemptive SJF is indicated with following Gantt chart

Gantt chart:

0 3 8 14 22

Process Burst time Arrival time Turnaround time Waiting time


P1 8 0 22-0=22 22-8=14
P2 5 0 8-0=8 8-5=3
P3 3 0 3-0=3 3-3=0
P4 6 0 14-0=14 14-6=8

The average waiting time is (14+3+0+8)/4=6.25


Ex: Process Burst time Arrival time
P1 8 0
P2 5 1
P3 3 2
P4 6 4

The order of execution of these processes with preemptive SJF is indicated with following Gantt chart

Gantt chart:

0 1 2 4 5 9 15 22

Process Burst time Arrival time Turnaround time Waiting time


P1 8 0 22-0=22 22-8=14
P2 5 1 9-1=8 8-5=3
P3 3 2 5-2=3 3-3=0
P4 6 4 15-4=11 11-6=5

The average waiting time is (14+3+0+5)/4=5.5


Advantages:

Compared to FCFS, average waiting time is less.

Disadvantages:

It can be used only when the burst times of processes are known in advance.

Generally, the burst times of processes are not available.

Priority scheduling algorithm

Priority scheduling also has two versions: preemptive and non-preemptive.

With priority algorithm, the process with highest priority is executed first.

If the priority of two or more processes is same then the processes are executed in FCFS order.

The priorities of processes are generally indicated as 1, 2, 3 and so on.

Lower number indicates highest priority.


Non-preemptive Priority

Process Burst time Arrival time Priority


P1 8 0 2
P2 5 0 3
P3 3 0 1
P4 6 0 4

The order of execution of these processes with non-preemptive Priority is indicated with following Gantt chart

Gantt chart:

0 3 11 16 22

Process Burst time Arrival time Turnaround time Waiting time


P1 8 0 11-0=11 11-8=3
P2 5 0 16-0=16 16-5=11
P3 3 0 3-0=3 3-3=0
P4 6 0 22-0=22 22-6=16

The average waiting time is (3+11+0+16)/4=7.5


Ex: Process Burst time Arrival time Priority
P1 8 0 2
P2 5 1 3
P3 3 2 1
P4 6 4 4

The order of execution of these processes with non-preemptive Priority is indicated with following Gantt chart

Gantt chart:

0 8 11 16 22

Process Burst time Arrival time Turnaround time Waiting time


P1 8 0 8-0=8 8-8=0
P2 5 1 16-1=15 15-5=10
P3 3 2 11-2=9 9-3=6
P4 6 4 22-4=18 18-6=12

The average waiting time is (0+10+6+12)/4=7


Preemptive Priority

With Preemptive Priority, current process is allowed to execute until the arrival of next process.

At the arrival of next process, CPU is allocated to the process with highest priority.
Process Burst time Arrival time Priority
P1 8 0 2
P2 5 0 3
P3 3 0 1
P4 6 0 4

The order of execution of these processes with preemptive Priority is indicated with following Gantt chart

Gantt chart:

0 3 11 16 22

Process Burst time Arrival time Turnaround time Waiting time


P1 8 0 11-0=11 11-8=3
P2 5 0 16-0=16 16-5=11
P3 3 0 3-0=3 3-3=0
P4 6 0 22-0=22 22-6=16

The average waiting time is (3+11+0+16)/4=7.5


Ex: Process Burst time Arrival time Priority
P1 8 0 2
P2 5 1 3
P3 3 2 1
P4 6 4 4

The order of execution of these processes with preemptive Priority is indicated with following Gantt chart

Gantt chart:

0 1 2 4 5 11 16 22

Process Burst time Arrival time Turnaround time Waiting time


P1 8 0 11-0=11 11-8=3
P2 5 1 16-1=15 15-5=10
P3 3 2 5-2=3 3-3=0
P4 6 4 22-4=18 18-6=12

The average waiting time is (3+10+0+12)/4=6.25


Disadvantages:

A process may continuously wait for the CPU. This situation is called starvation.

If there is a continuous flow of higher priority processes into the system then the lower priority processes will never get the
CPU.

So, the lower priority processes will wait continuously.

A solution to the problem of starvation is aging.

Aging is increasing the priority of processes which have been waiting for long time.
Round Robin (RR) scheduling algorithm

Round robin algorithm is designed for time sharing operating systems.

RR is similar to FCFS.

RR is preemptive scheduling algorithm.

A time quantum or time slice is used.

The time quantum is generally from 10 to 100 milliseconds.

With RR, the processes are executed in FCFS order and each process is allowed to execute for the time which is specified by
quantum time.

If the process is completed within the time specified by quantum time then it will be taken out from the ready queue.

Otherwise, it will be added at the end of ready queue.


Process Burst time Arrival time
P1 8 0
P2 5 0
P3 3 0
P4 6 0

quantum time=4

The order of execution of these processes with RR is indicated with following Gantt chart

Gantt chart:

0 4 8 11 15 19 20 22

Process Burst time Arrival time Turnaround time Waiting time


P1 8 0 19-0=19 19-8=11
P2 5 0 20-0=20 20-5=15
P3 3 0 11-0=11 11-3=8
P4 6 0 22-0=22 22-6=16

The average waiting time is (11+15+8+16)/4=12.5


Ex: Process Burst time Arrival time
P1 8 0
P2 5 1
P3 3 2
P4 6 4

Quantum time=4

The order of execution of these processes with RR is indicated with following Gantt chart

Gantt chart:

0 4 8 11 15 19 20 22

Process Burst time Arrival time Turnaround time Waiting time


P1 8 0 19-0=19 19-8=11
P2 5 1 20-1=19 19-5=14
P3 3 2 11-2=9 9-3=6
P4 6 4 22-4=18 18-6=12

The average waiting time is (11+14+6+12)/4=10.75


Advantages:

1) All processes are given equal priority.

Disadvantages:

1) The average waiting time is high.

2) If quantum time is less then more number of context switches will occur. More number of context switches leads to
wastage of time.

3) If quantum time is high then the processes have to wait for more time.
Multilevel Queue Scheduling

In multilevel queue scheduling, number of ready queues are maintained.

Initially, the processes are distributed to ready queues based on some property of processes like burst time, priority and so
on.

As an example, the processes with small burst time are placed in first ready queue, the processes with larger burst time are
placed in last ready queue.

Different scheduling algorithms can be used to execute the processes in different queues.

Initially, CPU is allocated to ready queue1 and the processes in ready queue1 are executed in the order specified by
corresponding scheduling algorithm.

After completion of all processes in ready queue1, CPU is allocated to ready queue2.

The processes in ready queue2 are executed in the order specified by corresponding scheduling algorithm.
After completion of all processes in ready queue2, CPU is allocated to ready queue3.

This procedure is repeated till the last queue is reached.

Priority will be given to the processes in higher level queues.

While executing any process in ready queue2, if any new process arrives into ready queue1 then the process in ready
queue2 is preempted and the CPU is switched to the process arrived into ready queue1.

Similarly, while executing any process in ready queue3, if any new process arrives into either ready queue1 or ready queue2
then the process in ready queue3 is preempted and the CPU is switched to the process arrived into ready queue1 or ready
queue2.
Multilevel Feedback Queue Scheduling

In multilevel feedback queue scheduling, number of ready queues are maintained.

Initially, all processes are placed in ready queue1.


Different scheduling algorithms can be used to execute processes in different queues.

Initially CPU is allocated to ready queue1.

The processes in ready queue1 are executed in the order specified by corresponding scheduling algorithm.

After completion of all processes in ready queue1, the CPU is allocated to ready queue2.

The processes in ready queue2 are executed in the order specified by corresponding scheduling algorithm.

After completion of all processes in ready queue1 and ready queue2, the CPU is allocated to ready queue3.

This procedure is repeated till the last ready queue is reached.

Priority will be given to the processes in higher level queues.

While executing any process in ready queue2, if any new process arrives into ready queue1 then the process in ready
queue2 is preempted and the CPU is switched to the process arrived into ready queue1.
Similarly, while executing any process in ready queue3, if any new process arrives into either ready queue1 or ready queue2
then the process in ready queue3 is preempted and the CPU is switched to the process arrived into ready queue1 or ready
queue2.

If a process in ready queue1 is not completed then it will be moved to ready queue2.

If a process in ready queue2 is not completed then it will be moved to ready queue3.

If a process in last ready queue is not completed then it will be moved to ready queue1.
Operations on Processes
There are two basic operations on processes:

1) Process creation
2) Process termination

Process Creation

A process may create several new processes during its execution.

The process which creates new processes is called the parent process and the created processes are called the child
processes.

Each child process may create other processes.

This creation leads to formation of a tree of processes.

Each process is associated with a unique integer number called process identifier which is used to identify the processes.

43
44
Each process requires some resources.

A child process obtains its required resources in any one of the following ways:

1) Directly from the operating system


2) From its parent process

The parent process has to distribute some resources and share some resources among its child processes.

In addition to resources, the parent process may pass some data to its child processes.

For example, if a child process is created for displaying an image file on the monitor then the parent process has to pass the
address of file to the child process.

The child process may be a duplicate of the parent process (child has the same program and data as parent) or the child has
a new program.

When a parent process creates a child process then the parent and child processes can execute concurrently or the parent
process waits for the completion of some or all of its child processes.

In UNIX operating system, the fork() system call is used to create a new process.
45
Fork() System Call

Fork() system call is used for creating a new process.

The process which invokes the fork() system call is called the parent process.

The newly created process is called the child process.

Both processes (parent and child) will run concurrently.

After a new child process is created, both processes will execute the next instruction following the fork() system call.

Separate copy of the variables is maintained in parent and child processes.

Changes made to the values of variables by parent process will not be reflected in the child process and vice versa.

Fork system call takes no parameters and returns an integer value.

Below are different values returned by fork().

Negative Value: creation of a child process was unsuccessful.


Zero: returned to the newly created child process.
Positive value: returned to parent process. The value contains process ID of newly created child process.
Predict the output of the following program

#include<stdio.h> Process hierarchy


#include<sys/types.h>
#include<unistd.h>
int main()
{
fork();
printf("Hello world!\n");
return 0;
}

Output:
Hello world!
Hello world!
Calculate the number of times hello is printed

#include<stdio.h> Process hierarchy


#include<sys/types.h>
#include<unistd.h>
int main()
{
fork();
fork();
fork();
printf("hello\n");
return 0;
}
Output:
hello
hello
hello
hello
hello
hello
hello
hello
Predict the output of the following program

#include<stdio.h> Process hierarchy


#include<sys/types.h>
#include<unistd.h>

void forkexample()
{
if (fork() == 0)
printf("Hello from Child!\n");

else
printf("Hello from Parent!\n"); Output:
} Hello from Child!
Hello from Parent!
int main()
{ (or)
forkexample();
return 0; Hello from Parent!
} Hello from Child!
Predict the output of the following program
Process hierarchy
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>

void forkexample()
{
int x = 1;

if (fork() == 0)
printf("Child has x = %d\n", ++x);
Output:
else
Parent has x = 0
printf("Parent has x = %d\n", --x); Child has x = 2
}
int main() (or)
{
forkexample(); Child has x = 2
return 0; Parent has x = 0
}
Predict output of below program

#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
int main()
{
fork();
((fork() && fork()) || fork());
fork();

printf("forked\n");
return 0;
}
#include<stdio.h>
int main()
{
fork(); /* A */
(( fork() /* B */ && fork() /* C */ ) || fork(); /* D */ ) Output:
fork(); /* E */
printf("forked\n"); Forked
return 0; Forked
} Forked
Forked
Process hierarchy Forked
Forked
Forked
Forked
Forked
Forked
Forked
Forked
Forked
Forked
Forked
Forked
Forked
Forked
Forked
forked
How many times fork() is invoked in the following program?

void main() Process hierarchy


{
int i = 0;
for (i = 0; fork();i++)
{
if(i == 5)
{
exit(0);
}
}
}

6 times
Mention the process hierarchy and output.

main() Process hierarchy


{
print(“a\n”);
if(fork() == 0)
{
print(“b\n”);
if(fork() == 0)
{
print(“c\n”);
exit();
}
print(“d\n”);
print(“e\n”);
exit();
}
Output:
print(“f\n”);
a
print(“g\n”); f
exit(); g
} b
d
e
c
Draw the process hierarchy to represent the process creation and mention how many times “OS” will be printed.

void main()
{
if(fork() && fork() || fork()) Process hierarchy
{
fork();
fork();
print(“OS”);
}
}

Output:
OS
OS
OS
OS
OS
OS
OS
OS
OS
OS
OS
OS
Process Termination

A process terminates when the last statement of the process is executed.

When a process terminates, it informs to its parent about its completion.

The operating system releases all resources from the completed process.

A parent process may terminate its child process when the child process uses its allocated resources for long time (or) the
task executed by the child process is no longer required (or) the parent is exiting.

Some operating systems do not allow a child process to exist when the parent process is terminated.

In this case, when the parent process terminates then the operating system terminates all children of the process.

This type of termination is called cascading termination.

56
Inter-process Communication
In a computer system, number of processes may be executing concurrently.

A process is called independent if it is not communicating with any other process.

A process is called cooperating if it is communicating with any other process.

Processes must communicate for

Sharing resources: for example, if two or more processes require data from same file (resource).

Speeding up the computation:

when a program has to be completed in less time then the program is divided into number of parts and the parts are
executed concurrently on the processors (processing units) of the system.

The parts must communicate with each other as they belong to the same program.

Two methods are used for implementing inter-process communication

1) Shared memory
2) Message passing 57
Shared Memory

A region of memory shared by processes is created.

The processes can exchange information by reading and writing data to the shared memory.

A process creates the shared memory segment.

Any process which wants to communicate with the process creating the shared memory must attach the shared memory to
its address space.

Generally, the operating system prevents one process from accessing other process memory.

This restriction is omitted in the shared memory method.

58
Producer – consumer problem

In this problem, there are two processes: producer and consumer.

There is a buffer for storing items.

The producer process stores items into the buffer and the consumer process takes items from the buffer.

Two pointers (in and out) are maintained for the buffer.

The in pointer points to the next free slot in the buffer and out pointer points to the slot from which item can be taken out
from the buffer.

A variable called ‘count’ indicates the number of items in the buffer.

The buffer, sizeofbuffer and count must be stored in shared memory as these are accessed by both processes.

The producer process has to wait until a free slot is available in the buffer.

The consumer process has to wait until an item is available in the buffer.

Using shared memory, large amount of data can be exchanged between processes in less time.
59
Message passing

With message passing, processes can communicate by exchanging short messages.

Two operations called send(message), receive(message) and a communication link are used for exchanging the messages.

The following three issues need to be considered.

1) Naming
2) Synchronization
3) Buffering 60
1) Naming

A direct communication link or an indirect communication link is used between the communicating processes.

Direct Communication:

With direct communication, the sender process has to mention the name of receiver process.

Similarly, the receiver process has to mention the name of sender process.

The syntax of send and receive operations is

send(P, message) - sends a message to process P.


receive(Q, message) - receives a message from process Q.

In Direct Communication:

1) A communication link is established between every pair of processes that want to communicate.
2) A link is associated with exactly two processes.
3) There exists exactly one link between every pair of processes.

61
Indirect communication:

The processes can communicate by sending and receiving messages from mailboxes.

The syntax of send and receive operations is

send(A, message) – sends a message to mailbox A.


receive(A, message) – receives a message from mailbox A.

In Indirect Communication:

1) A link is established between a pair of processes if both processes have a shared mailbox.
2) There exists more number of links between a pair of processes.

2) Synchronization

Communication between processes takes place through calls to send() and receive() operations.

Message passing is either blocking or non blocking – also known as synchronous and asynchronous.

62
Blocking send: the sending process is blocked until the message is received by the receiver or mailbox.

Non blocking send: the sending process continues its operations after sending the message.

Blocking receive: the receiver blocks until it receive the message.

Non blocking receive: the receiver does not block for the message to receive.

The sender and receiver can use any combination of send() and receive() operations.

3) Buffering

In both direct and indirect communication, the messages are stored in a buffer.

Three types of buffers can be used

Buffer with zero capacity: in this case, the sender must block until the receiver receives the message.

Buffer with bounded capacity: the buffer has a finite length. In this case, the sender can continue its execution after sending
a message if the buffer is not full.

Buffer with unbounded capacity: the buffer has infinite capacity. In this case, the sender never blocks.
63
If the processes running in different systems want to communicate then message passing is easier to implement than shared
memory.

Message passing allows exchanging of small amounts of data.

It takes more time to exchange messages as messages are exchanged through kernel.

64
Multithreaded Programming
A thread is part of a process.

A single threaded process can perform only one task at a time.

A multithreaded process can perform number of tasks at a time.

The following figure shows the difference between a single threaded process and a multithreaded process.

65
A thread is associated with a program counter, a set of registers and a stack.

The threads belonging to a process shares the code, data and resources of the process.

Most of the software running on modern computer systems are multithreaded.

The threads of a process may be performing different tasks or same task.

For example, in a word processor, a thread displays graphics, a thread responds to key strokes and a thread checks spelling
and grammar.

In a web server, there are number of threads one for each client request.

When a client makes a request, a thread is created by the server for processing that request.

Most operating system kernels are multithreaded.

Number of threads is running in the kernel.

Each thread implements a functionality of the operating system.

66
Benefits of multithreaded programs

1) Responsiveness

In a single threaded process, if the thread blocks then the user does not get response.

In a multithreaded process, if one or more threads are blocked then the user gets response from other threads of the
process.

Multithreading increases responsiveness to the user.

2) Resource sharing

In multithreading, all threads of a process shares resources allocated to that process automatically.

3) Economy

Creating a process is costly.

The operating system has to allocate number of resources to a process when it creates a process.

When a thread is created no need to allocate resources for the thread as the thread shares resources of the process in which
it is created. 67
Process switching is costly compared to thread switching.

4) Scalability

In a multiprocessor system, a single threaded process can use only one processor.

A multithreaded process can use all processors for running the threads in parallel.

Multithreading models

Two types of threads can be found in the computer system: User threads and Kernel threads

User threads (threads created in an application program) are created and managed by user.

User level threads are fast.

Creation of user level threads and switching between user level threads is easy.

Kernel threads (threads created in kernel) are managed by the operating system.

Kernel level threads are slower compared to user level threads due to management overhead.
68
Creation of kernel level threads and switching between kernel level threads is costly and time consuming.
A kernel level thread provide services to one or more user level threads.

Multithreading models indicate the relationship between user and kernel threads.

There are three types of models

1) Many-to-One model
2) One-to-One model
3) Many-to-Many model

69
1) Many-to-One model

Number of user threads is mapped to one kernel thread as shown in following figure.

The kernel thread provide services to only one user thread at a time.

If the kernel thread is blocked then the entire user process is blocked.

It is not possible to run user threads in parallel.

70
2) One-to-One model

Each user thread is mapped to a kernel thread as shown in following figure.

The user process is not blocked when one thread makes a blocking system call.

All user threads can run in parallel on multiple processors.

A drawback with this method is:

Creation of a user thread results in creation of a kernel thread.


71
3) Many-to-Many model

The user threads are mapped to less or equal number of kernel threads as shown in following figure

The user threads can run in parallel on multiple processors.

The user can create any number of user threads.

When a thread makes a blocking system call, the kernel can schedule another thread for execution.

72
One variation of many-to-many model is two-level model.

In two-level model, user threads are mapped to less or equal number of kernel threads and a user thread can be bound to a
kernel thread.

73
Module 3.1
PROCESS SYNCHRONIZATION
Producer – Consumer Problem
• There is a Buffer with some number of slots.
• One item can be stored in each slot of the buffer.
• In and out are pointers to the buffer.
• Count is a variable which indicates the number of items in the buffer.
• Producer is the process which inserts items into the buffer.
• In points to the next free slot of buffer into which the producer process inserts the next item.
• In pointer is moved to the next position after inserting an item into the buffer.
• Count is incremented by 1 after inserting an item into the buffer.
• Consumer is the process which removes items from the buffer.
• Out points to the slot from which the consumer process removes the item.
• Out pointer is moved to the next position after removing an item from the buffer.
• Count is decremented by 1 after removing an item from the buffer.

2
Producer Process Consumer Process
The code of producer process is: The code of consumer process is:
while (true) while(true)
{ {
while (count == buffer_size) while (count == 0)
; ;
Buffer[in] = item; item = Buffer[out];
in = (in + 1) % buffer_size; out = (out+1) % buffer_size;
count = count + 1; count = count -1;
} }

3
Producer Process Consumer Process
The code of producer process is: The code of consumer process is:
while (true) while(true)
{ {
P1: while (count == buffer_size) C1: while (count == 0)
P2: ; C2: ;
P3: Buffer[in] = item; C3: item = Buffer[out];
P4: in = (in + 1) % buffer_size; C4: out = (out+1) % buffer_size;
P5: register1 = count; C5: register2 = count;
P6: register1 = register1 + 1; C6: register2 = register2 - 1;
P7: count = register1; C7: count = register2;
} }
Serial Execution of processes
Processes are executed one after another.
CPU is switched to next process only after completion of the currently running process.
N number of processes can be serially executed in N! number of ways.
For example, two processes p1 and p2 can be executed serially in two ways:
1) p1, p2 2) p2, p1
Three processes p1, p2 and p3 can be executed serially in six ways:
1) p1, p2, p3 2) p1, p3, p2 3) p2, p1, p3 4) p2, p3, p1 5) p3, p1, p2 6) p3, p2, p1

Concurrent Execution (or) Parallel Execution of processes


CPU can be switched to next process during execution of the current process.
The number of concurrent executions possible with n number of processes depends on the number of statements in each
process.

5
Independent processes

Processes running in the computer system are said to be independent processes if they are not exchanging or sharing any
data.

Cooperating processes

Processes running in the computer system are said to be cooperating processes if they are exchanging or sharing any data.

Correct output is generated when the independent processes are executed either serially or concurrently.

Correct output is generated when the cooperating processes are executed serially.

Concurrent execution of cooperating processes may generate wrong output.


One example concurrent execution of producer & consumer processes that leads to wrong result is:
P1, P2, P3, P4, P5, P6, C1, C2, C3, C4, C5, C6, P7, C7.

while (true) while(true)


{ {
P1: while (count == buffer_size) C1: while (count == 0)
P2: ; C2: ;
P3: Buffer[in] = item; C3: item = Buffer[out];
P4: in = (in + 1) % buffer_size; C4: out = (out+1) % buffer_size;
P5: register1 = count; C5: register2 = count;
P6: register1 = register1 + 1; C6: register2 = register2 - 1;
P7: count = register1; C7: count = register2;
} }

7
Another example concurrent execution of producer & consumer processes that leads to wrong result is:
P1, P2, P3, P4, P5, P6, C1, C2, C3, C4, C5, C6, C7, P7.

while (true) while(true)


{ {
P1: while (count == buffer_size) C1: while (count == 0)
P2: ; C2: ;
P3: Buffer[in] = item; C3: item = Buffer[out];
P4: in = (in + 1) % buffer_size; C4: out = (out+1) % buffer_size;
P5: register1 = count; C5: register2 = count;
P6: register1 = register1 + 1; C6: register2 = register2 - 1;
P7: count = register1; C7: count = register2;
} }

8
Race condition
Race condition is getting different outputs for different concurrent executions of cooperating processes.

Ex: What are the different values that A and B get after the execution of the processes P1, P2 concurrently?
Initial values of A and B are 6 and 4 respectively.

Process P1: Process P2:

I1: A=A-B; I11: A=A+1;


I2: B=B+A; I12: B=B-1;

9
Concurrent execution sequence1: I1, I2, I11, I12
A=6-4=2
B=4+2=6
A=2+1=3
B=6-1=5
A=3, B=5 Process P1: Process P2:

I1: A=A-B; I11: A=A+1;


Concurrent execution sequence2: I1, I11, I12, I2 I2: B=B+A; I12: B=B-1;
A=6-4=2
A=2+1=3
B=4-1=3
B=3+3=6
A=3, B=6
Concurrent execution sequence3: I1, I11, I2, I12
A=6-4=2
A=2+1=3
B=4+3=7
B=7-1=6
A=3, B=6 Process P1: Process P2:

I1: A=A-B; I11: A=A+1;


Concurrent execution sequence4: I11, I12, I1, I2 I2: B=B+A; I12: B=B-1;
A=6+1=7
B=4-1=3
A=7-3=4
B=3+4=7
A=4, B=7
Concurrent execution sequence5: I11, I1, I2, I12
A=6+1=7
A=7-4=3
B=4+3=7
B=7-1=6
A=3, B=6 Process P1: Process P2:

I1: A=A-B; I11: A=A+1;


Concurrent execution sequence6: I11, I1, I12, I2 I2: B=B+A; I12: B=B-1;
A=6+1=7
A=7-4=3
B=4-1=3
B=3+3=6
A=3, B=6
The different possible values that A and B can take are:
A=3, B=5
A=3, B=6
A=4, B=7
Critical Section of a process
Critical section of a process is the set of statements in the process which access and modify the shared data.

The critical section of producer process is:

register1 = count;
register1 = register1 + 1;
count = register1;

The critical section of consumer process is:

register2 = count;
register2 = register2 - 1;
count = register2;
Critical Section Problem
When two or more cooperating processes are executing concurrently, then at a time only one process should execute the
statements in its critical section.
(or)
When one process is already executing the statements in its critical section, no other process is allowed to execute the
statements in its critical section.
(or)
No two processes are allowed to execute the statements in their critical section at the same time.

When the execution of cooperating processes is restricted in this way then the correct output is generated even if the
cooperating processes are executed concurrently.

Each process must request permission to start the execution of statements in its critical section.

The section of code implementing this request is the entry section.

The critical section may be followed by an exit section which allows the other process to start the execution of statements
in its critical section.

The general structure of a process is shown in the following figure.


15
while (true)
{
.
.

Entry Section

Critical Section

Exit Section

.
.
}

Solutions for Critical Section Problem (or) Solutions for Implementing Mutual Exclusion

1. Peterson’s Solution or Software Solution


2. Hardware Solution
3. Semaphore Solution
4. Monitor Solution
A solution to the critical section problem must satisfy the following three conditions:
1) Mutual exclusion
2) Progress
3) Bounded waiting

Mutual Exclusion

No two processes should execute the statements in their critical section at the same time.

Progress

At any time, at least one process should be in the running state or active. No deadlock should occur.

Bounded waiting

All processes should be given equal chance to execute the statements in their critical sections.

17
Peterson’s Solution
It is applicable to two processes only.

Two variables are used

o int turn;

o boolean flag[2];

turn variable indicates whose turn it is to execute the statements in its critical section.

turn=1 - indicates that the 1st process (producer process) can execute the statements in its critical section.

turn=2 - indicates that the 2nd process (consumer process) can execute the statements in its critical section.

flag indicates whether a process is ready to execute the statements in its critical section.

flag[1]=true - indicates that the 1st process (producer process) is ready to execute the statements in its critical section.

flag[2]=true - indicates that the 2nd process (consumer process) is ready to execute the statements in its critical section.

Initially, flag[1] and flag[2] are set to false.

18
The following procedure is used to solve the critical section problem:

When the 1st process (producer process) wants to execute the statements in its critical section then the 1st process
(producer process) sets flag[1] to true.

If the 2nd process (consumer process) is also ready to execute the statements in its critical section, then the 1st process
(producer process) allows the 2nd process (consumer process) to execute the statements in its critical section by setting the
turn value to 2.

The 1st process (producer process) waits until the 2nd process (consumer process) completes the execution of statements in
its critical section.

Then the 1st process (producer process) starts executing the statements in its critical section.

The 1st process (producer process) sets flag[1] to false after completing the execution of statements in its critical section.

Same procedure is followed by the 2nd process (consumer process) when it wants to execute the statements in its critical
section.

19
Peterson’s solution to the critical section problem of producer-consumer is as follows:

Producer Process Consumer Process

while (true) while(true)


{ {
while (count == buffer_size) while (count == 0)
; ;
Buffer[in] = item; item = Buffer[out];
in = (in + 1) % buffer_size; out = (out+1) % buffer_size;

flag[1]=true; flag[2]=true;
Entry turn=2; Entry turn=1;
Section while(flag[2]==true && turn==2) Section while(flag[1]==true && turn==1)
; ;

register1 = count; register2 = count;


register1 = register1 + 1; Critical Section register2 = register2 - 1; Critical Section
count = register1; count = register2;

Exit flag[1]=false; Exit flag[2]=false;


Section Section
} }
Hardware Solution

A variable named ‘lock’ is used.

boolean lock;

When a process wants to execute the statements in its critical section, the process checks the value of lock variable.

If the value of lock variable is true, then the process is not allowed to execute the statements in its critical section.

If the value of lock variable is false, then the process is allowed to execute the statements in its critical section.

Before executing the statements in the critical section, the process must set the value of lock variable to true.

After executing the statements in the critical section, the process must set the value of lock variable to false.

The structure of process is:

21
while (true)
{
.
.

Acquire lock;

Critical Section

Release lock;
.
.
}
Test_and_Set()

‘Test_and_Set’ is a hardware instruction which performs operations on the ‘lock’ variable to solve the critical section
problem.

It takes ‘lock’ variable as input and returns a boolean value.

The definition of Test_and_Set() is

boolean Test_and_Set(boolean lock)


{
boolean temp=lock;
lock=true;
return temp;
}

Test_and_Set() is an atomic instruction.

During the execution of Test_and_Set(), the CPU is not switched to any other process.

Initial value of ‘lock’ is false.


The solution to critical section problem of producer-consumer is

Producer Process Consumer Process

while (true) while(true)


{ {
while (count == buffer_size) while (count == 0)
; ;
Buffer[in] = item;
item = Buffer[out];
in = (in + 1) % buffer_size;
out = (out+1) % buffer_size;
Entry while(Test_and_Set(lock))
Section ; Entry while(Test_and_Set(lock))
Section ;
register1 = count;
register1 = register1 + 1; Critical Section register2 = count;
count = register1; register2 = register2 - 1; Critical Section
count = register2;
Exit lock=false;
Section Exit lock=false;
} Section
}
Swap()

‘Swap’ is another hardware instruction which is used to solve the critical section problem.

The definition of Swap() instruction is

void Swap(boolean lock, boolean key)


{
boolean temp=lock;
lock=key;
key=temp;
}

‘lock’ is a global variable.

‘key’ is a local variable.

Initial value of lock variable is false.

25
The solution to critical section problem of producer-consumer is

Producer Process Consumer Process

while (true) while(true)


{ {
while (count == buffer_size) while (count == 0)
; ;
Buffer[in] = item; item = Buffer[out];
in = (in + 1) % buffer_size; out = (out+1) % buffer_size;

Entry key1 = true; Entry key2 = true;


while(key2==true)
Section while(key1==true)
Section swap(lock, key2);
swap(lock, key1);
register2 = count;
register1 = count;
register2 = register2 - 1; Critical Section
register1 = register1 + 1; Critical Section
count = register2;
count = register1;
Exit lock=false;
Exit lock=false; Section
Section }
}
Semaphore Solution

Semaphore is an integer variable.

There are two types of semaphore:

1) Binary semaphore
2) Counting semaphore

Binary semaphore is used to solve the critical section problem.

Binary semaphore is also called as ‘mutex’ variable as binary semaphore is used to implement mutual exclusion.

Initial value of binary semaphore is 1.

Counting semaphore is used to provide synchronous access to a resource when the resource is requested by a number of
processes at a time.

Initial value of counting semaphore is equal to the number of instances of the resource.
A queue is associated with each semaphore variable.

Two atomic operations are defined on a semaphore variable:

1) wait()
2) signal()

The definition of wait() operation is:

wait(s)
{
s=s-1;
if(s<0)
{
Suspend the execution of the process which has invoked the wait() operation;
Insert that process in the queue associated with the semaphore variable s;
}
}
The definition of signal() operation is:

signal(s)
{
s=s+1;
if(s<=0)
{
remove a process from the queue associated with the semaphore variable s;
restart the execution of that removed process;
}
}

To solve the critical section problem, a process should invoke wait() operation on the semaphore variable before starting the
execution of statements in its critical section and should invoke signal() operation after executing the statements in its
critical section.

The structure of process is


.
.
wait(S);
Critical Section
signal(S);
.
.
The solution to critical section problem of producer-consumer is

Producer Process Consumer Process

while (true) while(true)


{ {
while (count == buffer_size) while (count == 0)
; ;
Buffer[in] = item; item = Buffer[out];
in = (in + 1) % buffer_size; out = (out+1) % buffer_size;

Entry wait(s); Entry wait(s);


Section Section
register1 = count; register2 = count;
register1 = register1 + 1; Critical Section register2 = register2 - 1; Critical Section
count = register1; count = register2;

Exit signal(s); Exit signal(s);


Section Section
} }
Classic problems of synchronization

1) Bounded buffer or producer-consumer problem


2) Readers-writers problem
3) Dining philosopher’s problem

Readers-Writers Problem

There is a database that can be shared by a number of concurrent processes.

Some processes named ‘Readers’ only read data from the database.

Other processes named ‘Writers’ write data to the database.

At a time, any number of Readers can read data from the database without any conflict.

But, when a Writer is writing data to the database then other Writers and Readers are not allowed to access the database.

Semaphore solution to the Readers-Writers Problem

The following semaphore variables are used:


semphore rw_mutex = 1;
semaphore mutex = 1;
int read_count = 0;

rw_mutex and mutex are binary semaphores.

read_count variable keeps track of how many Readers are currently reading from the database.

rw_mutex is used by both Readers and Writers to synchronously access the database.

mutex is used by Readers to synchronously access the read_count variable.

The code of a Writer process is:

while(true)
{
wait(rw_mutex);

Write data into the database;

signal(rw_mutex);
}
The code of a Reader process is:

while(true)
{
wait(mutex);
read_count = read_count + 1;
if(read_count == 1)
wait(rw_mutex);
signal(mutex);

Read data from the database;

wait(mutex);
read_count = read_count - 1;
if(read_count == 0)
signal(rw_mutex);
signal(mutex);
}
Dining Philosopher’s Problem

There are 5 philosophers.

A philosopher at any time is in either thinking or eating state.

The philosophers share a circular table surrounded by five chairs, each belonging to one philosopher.

In the center of the table is a bowl of rice, and the table is laid with five single chopsticks as shown in the diagram:
When a philosopher feels hungry then the philosopher tries to pick up the two chopsticks that are closest to him.

A philosopher may pick up only one chopstick at a time.

A philosopher cannot pick up a chopstick that is already in the hand of a neighbor.

When a hungry philosopher has both chopsticks at the same time, he eats without releasing the chopsticks.

When he is finished eating, he puts down both chopsticks and starts thinking again.

Semaphore solution to the Dining Philosopher’s problem

One simple solution is to represent each chopstick with a semaphore.

semaphore chopstick[5];

where all the elements of chopstick are initialized to 1.

A philosopher tries to grab a chopstick by executing a wait() operation on that semaphore.

A philosopher releases his chopsticks by executing the signal() operation on the appropriate semaphores.
The structure of philosopher i is:

while (true)
{
wait(chopstick[i]);
wait(chopstick[(i+1) % 5]);

Eat for a while;

signal(chopstick[i]);
signal(chopstick[(i+1) % 5]);

Think for a while;


}
Module 3.2
Deadlock
A process is a program in execution state.

A resource is any hardware or software component present in the computer system.

Some example resources are: RAM, HD, CPU, compiler, file and so on.

There are two types of resources: sharable and non-sharable.

A sharable resource can be used by a number of processes at a time.

A non-sharable resource can be used by only one process at a time.

A computer system may have one or more instances of each resource type.

For example, a computer system may have 3 CPUs, 2 printers and so on.

A number of processes may be executing in the computer system at any particular time.

Each process requires some resources.

Operating system allocates the resources to the processes running in the computer system.
When a process requires a resource then the process makes a request to the operating system.

The operating system allocates the requested resource if that resource is free otherwise the process has to wait.

Deadlock

A set of processes is said to be in deadlock state if each process in the set is waiting for an event (releasing of resource) that
can be caused by another process in the set.

Ex:
Necessary conditions for deadlock

The following four conditions must hold for the occurrence of deadlock state.
(or)
In a deadlock state, the following four conditions are satisfied.

Mutual Exclusion

Mutual exclusion condition indicates that a non-sharable resource should be used by only one process at a time.

Hold and Wait

Each process is holding some resources and waiting for other resources.

No-Preemption

A resource cannot be released from a process before completion of the process.

Circular Wait

There is a set of processes {P1, P2, P3, … , Pn} such that


P1 is waiting for P2
P2 is waiting for P3
.
.
.
.
Pn is waiting for P1

Resource Allocation Graph

Resource allocation graph is used to describe the deadlock state.

Nodes in the resource allocation graph represent both processes and resources.

Edges indicate both allocation of resources to processes and request to resources by the processes.

Processes are indicated using circles, resources are indicated using boxes.

A request edge is from a process to a resource.

An allocation edge is from a resource to a process.


If a resource has number of instances then that number of instances is indicated using number of dot symbols inside the box
representing that resource.

When a process Pi requests an instance of resource type Rj, a request edge is inserted in the resource-allocation graph.

When this request can be fulfilled, the request edge is instantaneously transformed to an allocation edge.

When the process no longer needs access to the resource, it releases the resource.

As a result, the allocation edge is deleted.

There is no cycle in above resource allocation graph. There is no deadlock.


There is no cycle. There is no deadlock.

There is a cycle:

P1 -> R2 -> P2 -> R1 -> P1

Processes P1 and P2 are in deadlock state.


There are two cycles:

P1 -> R1 -> P2 -> R3 -> P3 -> R2 -> P1


P2 -> R3 -> P3 -> R2 -> P2

Processes P1, P2, and P3 are in deadlock state.

There is a cycle:

P1 -> R1 -> P3 -> R2 -> P1

There is no deadlock.
Conclusions from the above examples are:

1) If there is no cycle in the Resource Allocation Graph then there is no deadlock state.

2) If each resource has only one instance then the presence of cycle in the resource allocation graph indicates the
occurrence of deadlock state.

3) If the resources have multiple instances then the presence of cycles in the resource allocation graph may or may not
indicate the occurrence of deadlock state.
Methods for handling deadlocks

1) Deadlock prevention
2) Deadlock avoidance
3) Deadlock detection
4) Deadlock recovery

Deadlock prevention provides a set of methods to ensure that at least one of the necessary conditions cannot hold.

These methods prevent deadlocks by constraining how requests for resources can be made.

Deadlock avoidance requires that the operating system be given additional information in advance concerning the resources
currently available, the resources currently allocated to each process, and the future requests and releases of each process.

With this additional knowledge, the operating system can decide for each request whether the request can be satisfied or
must be delayed.

Deadlock detection detects the occurrence of deadlock state in the computer system.

Deadlock recovery recovers the computer system from the deadlock state when it occurs.
Deadlock prevention

For a deadlock to occur, each of the four necessary conditions must hold.

We can prevent the occurrence of a deadlock by ensuring that at least one of these conditions cannot hold.

Mutual Exclusion

To ensure that mutual exclusion condition cannot hold, a resource can be allocated to a number of processes at a time.

This is possible only with sharable resources.

For non-sharable resources, it is not possible to ensure that mutual exclusion condition cannot hold.

Hold and Wait

Any one of the following two mechanisms is used to ensure that hold and wait condition cannot hold.

1) Allocate all required resources to the process before starting the execution of the process.

2) Allocate a subset of required resources to the process. Release this subset of resources from the process after using them
and allocate another subset of resources. Repeat this procedure till the completion of process.
No preemption

The following two mechanisms are used to ensure that no preemption condition cannot hold:

1) When a process requests for any resource which is currently allocated to a running process then the requesting process
will go the waiting state and the resources that are currently allocated to the requesting process are released.

Resources R1 and R2 are released from P1

2) When a process request for a resource which is currently allocated to a waiting process then the resource from the
waiting process is released and allocated to the requesting process.

Resources R3 is released from P2


Circular wait

The following mechanism is used to ensure that circular wait condition cannot hold:

An ordering (R1, R2, R3, …..) is given to the resources and the processes are allowed to request the resources in an increasing
order.

When a process request for a resource whose number is less than the number of any resource already allocated to the
process then that request is rejected by the operating system.

For example, if a process P1 is holding resources R2 and R5 and if it requests for resource R7 then the request is accepted.

If P1 request for R3 then that request is rejected.


Deadlock Avoidance

To apply deadlock avoidance method, the following information must be known in advance:

1) the resources currently available


2) the resources currently allocated to each process
3) the future requests and releases of each process

The following algorithms are used for deadlock avoidance:


1) Resource-Allocation-Graph algorithm
2) Banker’s algorithm

Resource-Allocation-Graph Algorithm

Resource-Allocation-Graph algorithm is used when each resource has only one instance.

In addition to the request and allocation edges, a claim edge is also included in the graph.

A claim edge Pi→Rj indicates that process Pi may request resource Rj at some time in the future.

This edge resembles a request edge in direction but is represented in the graph by a dashed line.
When process Pi requests resource Rj, the claim edge Pi → Rj is converted to a request edge.

Similarly, when a resource Rj is released by Pi, the allocation edge Rj →Pi is reconverted to a claim edge Pi → Rj.

Before process Pi starts executing, all its claim edges must already appear in the resource-allocation graph.

Now suppose that process Pi requests resource Rj.

The request can be granted only if converting the request edge Pi → Rj to an allocation edge Rj → Pi does not result in the
formation of a cycle in the resource-allocation graph.

To illustrate this algorithm, we consider the following resource-allocation graph.

Suppose that P2 requests R2.


Although R2 is currently free, we cannot allocate it to P2, since this action will create a cycle in the graph as shown below

A cycle indicates that the system is in an unsafe state.


Safe state

If all processes can be completed in any order with available resources then the system is said to be in safe state. Otherwise,
the system is said to be in unsafe state.

A safe state is not a deadlocked state.

An unsafe state may lead to a deadlock.

Safe Sequence

The order in which the processes can be completed is called the safe sequence.

Banker’s Algorithm for Deadlock Avoidance

Banker’s algorithm is used when resources have multiple instances or copies.

The following procedure is used to avoid the occurrence of deadlock state:

When any process request for resources then the algorithm checks whether the allocation of requested resources will lead
to (or) result in the safe state.

If the allocation of requested resources to the process will lead to safe state, then the operating system accepts the request.
Otherwise, rejects the request.
Four data structures are used in Banker’s algorithm for deadlock avoidance.

1) Available: is a vector of size m. Where m is the number of resource types.

Available vector indicates the number of copies of each resource type available in the system.

2) Max: is a matrix of size nxm. Where n is the number of processes.

Max matrix indicates the total number of copies of each resource type required by each process.

3) Allocation: is a matrix of size nxm.

Allocation matrix indicates the number of copies of each resource type allocated to each process.

4) Need: is a matrix of size nxm.

Need matrix indicates the number of copies of each resource type still required by each process.

Need matrix is calculated as

Need = Max - Allocation


Ex: n=4, m=2

Max Allocation Available Need


A B A B A B A B
P0 7 5 P0 0 1 3 3 P0 7 4
P1 3 2 P1 2 0 P1 1 2
P2 9 0 P2 3 0 P2 6 0
P3 2 2 P3 2 1 P3 0 1

Banker’s algorithm for deadlock avoidance has two parts:


1) Resource request algorithm (main algorithm)
2) Safety algorithm (sub algorithm)
Safety algorithm

This algorithm is used to check whether the system is in safe state or not.

Steps in the algorithm are:

1. Find a process Pi which is not marked as finished and Needi <= Available. If no such process exists then go to step 3.

2. Mark the process Pi as finished and update the Available vector as

Available = Available + Allocationi

Go to step 1.

3. If all processes are marked as finished, then indicate that the system is in safe state. Otherwise, indicate that the system is
in unsafe state.
Ex: n=4, m=2

Max Allocation Available Need


A B A B A B A B
P0 7 5 P0 0 1 3 3 P0 7 4
P1 3 2 P1 2 0 P1 1 2
P2 9 0 P2 3 0 P2 6 0
P3 2 2 P3 2 1 P3 0 1

Above state is safe state as all processes can be completed in the order: P1, P3, P0, P2.

At first, process P1 is selected as it is not marked as finished and its need1<=Available ((1,2)<=(3,3)).

Process P1 is marked as finished, the resources allocated to process P1 are released and then added to the available vector.

Available
5 3

Next, process P3 is selected as it is not marked as finished and its need3<=Available ((0,1)<=(5,3)).
Process P3 is marked as finished, the resources allocated to process P3 are released and then added to the available vector.

Available
7 4

Next, process P0 is selected as it is not marked as finished and its need0<=Available ((7,4)<=(7,4)).

Process P0 is marked as finished, the resources allocated to process P0 are released and then added to the available vector.

Available
7 5

Next, process P2 is selected as it is not marked as finished and its need2<=Available ((6,0)<=(7,5)).

Process P2 is marked as finished, the resources allocated to process P2 are released and then added to the available vector.

Available
10 5
Resource request algorithm

When a process Pi makes a Requesti for resources then the resource request algorithm is invoked to decide whether to
accept or reject the request.

The steps in the algorithm are:

1. If Requesti <= Available then go to step 2. Otherwise, the process Pi has to wait.

2. Pretend that the requested resources are allocated to the process and update the values of matrices as

Allocationi = Allocationi + Requesti


Needi = Needi - Requesti
Available = Available - Requesti

3. Check whether the resulting state is safe or not by calling the safety algorithm. If the resulting state is safe, then accept
the request. Otherwise, reject the request and restore the old state.
Ex: n=4, m=2

Max Allocation Available Need


A B A B A B A B
P0 7 5 P0 0 1 3 3 P0 7 4
P1 3 2 P1 2 0 P1 1 2
P2 9 0 P2 3 0 P2 6 0
P3 2 2 P3 2 1 P3 0 1

The above state is safe as the processes can be completed in the order: P1, P3, P0, P2.

If the process P1 makes a request

Request1 = (1, 0)

Then the operating system calls the resource request algorithm to decide whether the request can be accepted or not.
The resource request algorithm checks whether Request1<=Available or not.

As Request1<=Available ((1,0)<=(3,3)), the matrices are modified as

Allocation Need Available


A B A B A B
P0 0 1 P0 7 4 2 3
P1 3 0 P1 0 2
P2 3 0 P2 6 0
P3 2 1 P3 0 1

Now, the safety algorithm is called to determine whether the resulting state is safe or not.

The safety algorithm returns that the state is safe as the processes can be completed in the order: P1, P3, P0, P2.

So, the operating system accepts the request.


Ex: n=4, m=2

Max Allocation Available Need


A B A B A B A B
P0 7 6 P0 0 1 2 3 P0 7 5
P1 3 2 P1 3 0 P1 0 2
P2 9 3 P2 3 0 P2 6 3
P3 2 2 P3 2 1 P3 0 1

In the above state, if process P0 makes a request

Request0 = (0, 2)

Then the operating system calls the resource request algorithm to decide whether the request can be accepted or not.
The resource request algorithm checks whether Request0<=Available or not.

As Request0<=Available ((0,2)<=(2,3)), the matrices are modified as

Allocation Need Available


A B A B A B
P0 0 3 P0 7 3 2 1
P1 3 0 P1 0 2
P2 3 0 P2 6 3
P3 2 1 P3 0 1

Now, the safety algorithm is called to determine whether the resulting state is safe or not.

The safety algorithm returns that the state is unsafe as it is not possible to complete all processes.

So, the operating system rejects the request.


Deadlock detection

Deadlock detection method is used to check the occurrence of deadlock state in the computer system.

Two methods are used for detecting the occurrence of deadlock state.

1) Wait-for-graph algorithm
2) Banker’s algorithm

Wait-for-graph algorithm

Wait-for-graph algorithm is used when each resource has only one instance.

In this algorithm, a wait-for-graph is constructed based on the resource allocation graph.

Wait-for-graph contains only the process nodes and they are indicated by circles.

If a process Pi is waiting for a resource held by process Pj, then an edge is drawn from process Pi to process Pj in the wait-for-
graph.

Presence of cycles in the wait-for-graph indicates occurrence of the deadlock state.


Resource allocation graph Wait-for-graph

There are 2 cycles in the wait-for-graph

Cycle 1: P1->P3->P5->P1
Cycle 2: P1->P3->P4->P5->P1

So, deadlock has occurred in the system.

The processes P1, P3, P4 and P5 are in deadlock state.

Process P2 is not involved in any cycle. P2 is not in deadlock state.


Banker’s algorithm for Deadlock Detection

Banker’s algorithm is used when the resources have number of instances or copies.

The following data structures are used in this algorithm.

Available: It is a vector of size m. Where m is number of resource types.

Available vector indicates the number of available copies of each resource type.

Allocation: It is a matrix of size nxm. Where n is number of processes.

Allocation matrix indicates the number of copies of each resource type that are currently allocated to each process.

Request: It is a matrix of size nxm.

Request matrix indicates the number of copies of each resource type that are required by each process.
Algorithm

1. Find a process Pi which is not marked as finished and Requesti <= Available. If no such process exist then go to step 3.

2. Mark the process Pi as finished and update the Available vector as

Available = Available + Allocationi

Go to step 1.

3. If all processes are marked as finished, then indicate that deadlock state has not occurred. If some of the processes are
not marked as finished, then indicate that deadlock has occurred. The processes which are not marked as finished are in the
deadlock sate.
Ex:
Allocation Request Available
A B A B A B
P0 0 1 P0 0 0 0 0
P1 2 0 P1 2 0
P2 3 0 P2 0 0
P3 2 1 P3 1 0

At first, process P0 is selected as it is not marked as finished and its Request0 <= Available.

Process P0 is marked as finished and the available vector is updated as

Available
0 1

Next, process P2 is selected as it is not marked as finished and its Request2 <= Available.

Process P2 is marked as finished and the available vector is updated as

Available
3 1
Next, process P1 is selected as it is not marked as finished and its Request1 <= Available.

Process P1 is marked as finished and the available vector is updated as

Available
5 1

Finally, process P3 is selected as it is not marked as finished and its Request3 <= Available.

Process P3 is marked as finished and the available vector is updated as

Available
7 2

Deadlock state has not occurred in the system as all processes are marked as finished.
Ex: Allocation Request Available
A B C A B C A B C
P0 0 1 0 P0 0 0 0 0 0 0
P1 2 0 0 P1 2 0 2
P2 3 0 3 P2 0 0 1
P3 2 1 1 P3 1 0 0
P4 0 0 2 P4 0 0 2

First, process P0 is selected as it is not marked as finished and its Request0 <= Available.

Process P0 is marked as finished and the available vector is updated as

Available
0 1 0

The request of other processes cannot be satisfied with available resources.

So, the processes P1, P2, P3 and P4 are not marked as finished and it indicates the occurrence of deadlock state in the
system.

The processes P1, P2, P3 and P4 are in the deadlock state.


Deadlock Recovery

Any of the following two techniques is used to recover from the deadlock state:

1) Aborting the processes that are involved in the deadlock state.


2) Preempting the resources from the deadlocked processes.

Aborting the processes that are involved in the deadlock state

In this method, the processes which are involved in the deadlock state are terminated.

There are two ways for aborting the deadlocked processes.

1) Abort all processes at a time.


2) Abort the processes one by one until the deadlock cycle is eliminated.

In the first mechanism, all processes that are involved in the deadlock state are terminated or aborted.
Advantages
1. Deadlock can be recovered in less amount of time.
2. No need to apply the deadlock detection algorithm for number of times.
Disadvantage
1. Loosing of computations of all processes.

In the second mechanism, first one of the processes in the deadlock state is terminated.

After that the presence of deadlock state is checked.

If the deadlock state still exist, then one more process in the deadlock state is terminated.

This procedure is repeated until the system is recovered from the deadlock state.

Advantage
1. Less wastage in computation time compared to the first mechanism as only some processes in the deadlock state are
terminated.

Disadvantage
1. Need of applying the deadlock detection algorithm for number of times.
2. Some criteria have to be used to select a process for termination at each step.
The general criteria used to select a process for termination are:

1) Execution time

The process that has been executed for less amount of time is selected for termination.

2) No of Resources

The process which is holding less number of resources is selected for termination.

3) Priority

The process with low priority is selected for termination.


Preempting the resources from the deadlocked processes

In this method, resources will be preempted successively from the deadlocked processes until the system is recovered to
the normal state.

To use this mechanism for recovering from the deadlock state, the following factors are considered.

1. Selecting a victim (resource)


2. Rollback
3. Starvation

Selecting a victim: one of the deadlocked processes has to be selected and then one of the resources allocated to that
process needs to be selected based on some criteria like execution time, number of resources allocated, priority and so on.

Rollback: When a resource is preempted from a deadlocked process then that process needs to be restated from the
beginning.

Starvation: resources should not be preempted from the same process in each step.
Ex: n=5, m=4

Max Allocation Available

A B C D A B C D
A B C D
P0 0 0 1 2 P0 0 0 1 2
1 5 2 0
P1 1 7 5 0 P1 1 0 0 0
P2 2 3 5 6 P2 1 3 5 4
P3 0 6 5 2 P3 0 6 3 2
P4 0 6 5 6 P4 0 0 1 4

Need
A B C D
P0 0 0 0 0
P1 0 7 5 0
P2 1 0 0 2
P3 0 0 2 0
P4 0 6 4 2
Process P1 makes a request

Request1 = (0,4,2,0)

Can the request be accepted or rejected?

Allocation Need Available


A B C D A B C D A B C D
P0 0 0 1 2 P0 0 0 0 0 1 1 0 0
P1 1 4 2 0 P1 0 3 3 0
P2 1 3 5 4 P2 1 0 0 2
P3 0 6 3 2 P3 0 0 2 0
P4 0 0 1 4 P4 0 6 4 2
Module 4
MEMORY MANAGEMENT
Memory Hierarchy
Memory management deals with the allocation and de allocation of space in main memory for programs or processes.

Performance of computer system is high when the CPU is kept busy at all times.

A number of processes or programs is loaded into main memory at a time in order to keep the CPU busy at all times.

During the execution of a process, if the process waits for an input/output then the CPU is switched to other process in the
main memory.

Main Memory/Primary Memory/Random Access Memory

3
Logical address

A program or process is a collection of statements or instructions.

When the user wants to execute his/her program then the operating system loads the program into main memory.

CPU don’t know the location of program inside the main memory.

To execute a statement of the program, CPU generates the logical address of the statement.

For example, CPU generates the logical address 0 to execute the 1st statement in the program.

CPU generates the logical address 1 to execute the 2nd statement in the program.

The logical address range is 0 to n-1, where n is the number of statements in the program.

Logical address space

The set of logical addresses generated by the CPU during the execution of the program is called as logical address space.

For example, if the program has 10 statements then the logical address space includes the logical addresses 0, 1, 2, 3, 4, 5, 6,
7, 8, 9.
Physical address

Physical address refers to a location inside the main memory where a statement of the program is loaded.

Physical address space

The set of physical addresses of locations inside main memory where the statements of the program are loaded.

For example, if a program has 10 statements and are loaded into main memory locations whose address starts from 20340
then the physical address space includes the physical addresses 20340, 20341, 20342, 20343, 20344, 20345, 20346, 20347,
20348, 20349.

5
Memory Management Unit (MMU)

MMU maps the logical addresses to their corresponding physical addresses.

This mapping is also called as relocation.

The MMU maintains two registers: Base or relocation register and limit register.

The relocation register is used to store the starting physical address of the process.

The limit register is used to store the number of statements in the process.

6
Before starting the execution of a process, the starting physical address of the process is loaded into Relocation or Base
Register and the number of statements in the process is loaded into the Limit Register.

For example, if a process P1 is loaded into main memory at the address 1200 and the number of statements in the process
P1 is 5, when the execution of process P1 is started, the operating system loads the address 1200 into Relocation Register
and value 5 into Limit Register.

While executing the process, the logical address generated by the CPU for executing a statement is compared with the value
in Limit Register.

If the logical address value is less than the value in Limit Register then the logical address is added to the value in Relocation
Register to generate the corresponding physical address.

Otherwise, the MMU reports an error to the operating system.

7
Memory Management Techniques

The operating system uses the following techniques to allocate space for programs in the main memory.

1) Contiguous Memory Allocation


2) Paging
3) Segmentation

Contiguous Memory Allocation

Operating system loads the entire program as a single unit into the main memory.

Different approaches used in contiguous memory allocation are:

1) Equal size fixed partition


2) Unequal size fixed partition
3) Dynamic partition

8
Equal size fixed partition technique

Before loading any program into main memory, the operating system divides the space in main memory into equal size parts.

Only one program can be loaded into each partition of main memory.

Operating system can load a program into any free partition of main memory.

Main Memory

9
Advantage

To load a program, the operating system can select any free partition of main memory.

Disadvantages

1) Wastage of space inside the partition when the size of program is less than the size of partition.

Wastage of space inside the partition is called “internal fragmentation”.

2) When the size of program to be loaded is greater than the size of partition then it is not possible to load the program into
main memory.

Un-equal size fixed partition technique

Before loading any program into main memory, the operating system divides the space in main memory into different size
parts.

Only one program can be loaded into each partition of main memory.

Operating system has to select a suitable partition of main memory for loading a program.
10
Main Memory

Advantage

Internal Fragmentation is less when compared with equal size fixed partition technique.

Disadvantages

1) Operating System has to select a suitable partition for loading a program into main memory.

2) Wastage of space inside the partition when the size of program is less than the size of partition. 11
3) When the size of program to be loaded is greater than the size of partition then it is not possible to load the program into
main memory.

Dynamic partition technique

Operating system divides the space in main memory into parts at the time of loading programs into main memory.

Initially, main memory is divided into two parts.

Operating System is loaded into one part and the other part is used for loading user programs.

After loading a program say P1, the space in main memory is divided into three parts.

After loading another program say P2, the space in main memory is divided into four parts as shown in below figure.

12
Advantage

There is no wastage of memory inside partitions. So, no internal fragmentation.

Disadvantages

Some space is wasted outside of the partitions.

Wastage of space outside the partitions is called ‘External Fragmentation’.

Main Memory

13
Consider the above position of main memory which is the result of loading some programs into main memory and removing
the completed programs from main memory.

If the operating system wants to load a program say P20 with size 250 KB, then it is not possible to load that program into
main memory as there is no free part with size greater than or equal to 250KB in the main memory.

To avoid external fragmentation, compaction technique can be used.

Compaction

Compaction technique moves all free spaces in the main memory together.

After applying compaction technique to the above position of main memory, the position of main memory is as shown
below
Main Memory

14
With unequal size fixed partition and dynamic partition techniques, the operating system has to select a suitable partition in
the main memory for loading a program.

Operating System uses any of the following placement algorithms to select the suitable partition for loading the program.

Placement Algorithms

1) FIRST FIT
2) BEST FIT
3) WORST FIT

First fit algorithm scans the main memory either from the from starting or from the position where recent placement
happened and selects the first free partition whose size is greater than or equal to the size of program to be loaded.

Best fit algorithm scans the main memory from starting to ending and selects the free partition whose size is very close to
the size of program to be loaded.

Worst fit algorithm scans the main memory from starting to ending and selects the largest free partition.

15
Consider the following position of main memory.

To load the program P10 with size 150 KB, the partition selected by operating system with different algorithms is indicated in
below diagram.

16
Assume the memory is divided into six partitions with size 200 KB, 400 KB, 100 KB, 600 KB, 300 KB, 500 KB (in order).
Calculate the total internal fragmentation when the processes with size 336 KB, 96 KB, 173 KB, 415 KB, 245 KB (in order) are
placed with
i) First Fit algorithm
ii) Best Fit algorithm

First Fit Best Fit

Total internal fragmentation with First Fit= (200-96)+(400-336)+(600-173)+(300-245)+(500-415)=735 KB

Total internal fragmentation with Best Fit= (200-173)+(400-336)+(100-96)+(300-245)+(500-415)=235 KB


Given five memory partitions of 250 KB, 400 KB, 360 KB, 500 KB, and 180 KB (in order), how would the first-fit, best-fit
algorithms place processes of 315 KB, 221 KB, 135 KB, and 284 KB (in order)? Which algorithm makes the most efficient use
of memory?

Paging Technique

Before loading any program into main memory, the space in main memory is divided into equal size frames.

To load a program into main memory, the program is divided into equal size pages.

Size of page is equal to size of frame.

Generally, the page size/frame size is selected as a power of 2.

The pages of program are loaded into free frames of main memory.

After loading all pages of program, a page table is created for the program.

Number of entries in the page table of the program is equal to the number of pages in the program.

The entries of page table are indexed with page numbers.

The entries of page table are filled with the frame numbers in which the pages of the program are loaded.
The following diagram shows how programs are loaded into main memory with the paging technique.

19
Mapping Logical Address to Physical Address in Paging technique (or) Hardware support for Paging technique

20
To execute a statement of the program loaded into main memory using the paging technique, the CPU generates the logical
address of that statement.

This logical address consists of two parts: page number (p) and offset (d).

The page number is used as an index into the page table of the program to find the corresponding frame number.

The identified frame number is appended with offset of logical address to get the physical address of the statement.

The statement in the main memory at the generated physical address is transferred to the CPU for execution.

The offset identifies the statement inside the page or frame.

21
22
Consider a logical address space of 256 pages with 64 words per page mapped onto a physical memory of 4096 frames.
i. How many bits are required in the logical address?
ii. How many bits are required in the physical address?

Size of logical address = size of page number field + size of offset field = 8 bits (256=28) + 6 bits (64=26 ) = 14 bits

Size of physical address = size of frame number field + size of offset field = 12 bits (4096=212) + 6 bits (64=26) = 18 bits
Ex: Consider a machine with 128 MB physical memory and a 32 bit virtual address space. If the page size is 8 KB.
i) Find the number of bits in physical address
ii) Find the number of frames in the Main memory
iii) Find the number of bits allocated for offset
iv) Find the number of entries in page table

Size of physical memory/main memory/primary memory/RAM = 128 MB = 128 X 220 B = 27 X 220 B = 227 B
Size of virtual address/logical address = 32 bits
Size of program = 232 B
Size of page = 8 KB = 8 X 210 B = 23 X 210 B = 213 B
Size of frame = 213 B
i) Number of bits in physical address or size of physical address = 27 bits
ii) Number of frames in main memory = size of main memory/size of frame = 227 B / 213 B = 214 frames
iii) Number of bits allocated for offset = 13 bits
iv) Number of entries in page table=Number of pages in the program=size of program/size of page=232 B / 213 B = 219 entries
Ex: In a virtual memory system, size of virtual address is 64-bit, size of physical address is 60-bit, page size is 8 Kbyte and size
of each page table entry is 32 bytes. The main memory is byte addressable.
i) Calculate the Main memory size.
ii) Calculate the number of frames in the main memory.
iii) Calculate the page table size.
iv) Find the number of bits used for offset.

Size of virtual address/logical address = 64 bits


Size of physical address = 60 bits
Size of page = 8 KB = 8 X 210 B = 23 X 210 B = 213 B
Size of each page table entry = 32 B = 25 B
i) Size of physical memory/main memory/primary memory/RAM = 260 B
ii) Number of frames in the main memory = size of main memory/size of frame = 260 B / 213 B = 247 frames
iii) Size of page table = Number of entries in page table X Size of each entry in page table = Number of pages in the program
X 25 B = (size of program/size of page) X 25 B = (264 B / 213 B) X 25 B = 251 X 25 B = 256 B
iv) Number of bits used for offset = 13 bits
Ex: Size of logical address is 48-bit, page size is 4 Kbyte, size of main memory is 256 MB and size of each page table entry is
32 bytes. The main memory is byte addressable.
i) Find the number of bits in physical address
ii) Calculate the number of frames in the main memory.
iii) Calculate the page table size.
iv) Find the number of bits used for offset.

Ex: Calculate the size of memory if its address consists of 22 bits and the memory is 2-byte addressable.

Ex: Calculate the number of bits required in the address for memory having size of 16 GB. Assume the memory is 4-byte
addressable.

Ex: Consider a system with byte-addressable memory, 32 bit logical addresses, 4 kilobyte page size and page table entries of
4 bytes each. Calculate the size of the page table in the system in megabytes.

Ex: Consider a machine with 64 MB physical memory and a 32 bit virtual address space. If the page size is 4 KB, what is the
approximate size of the page table?

Ex: In a virtual memory system, size of virtual address is 32-bit, size of physical address is 30-bit, page size is 4 Kbyte and size
of each page table entry is 32-bit. The main memory is byte addressable. Which one of the following is the maximum
number of bits that can be used for storing protection and other information in each page table entry?
Translation Look-aside Buffer (TLB)
The operating system stores the page table of a process in the registers of computer system when the number of entries in
the page table is less. Otherwise, stores the page table in the main memory.
When the page table of process is stored in the registers, then one memory access is enough for getting a statement of the
process from main memory.
When the page table of process is stored in the main memory, then two memory accesses are required for getting a
statement of the process from main memory.
To get a statement from the main memory, frame number of that statement needs to be obtained first.
To get frame number of the statement, page table of the process stored in the main memory has to be accessed and it
requires one memory access.
One more memory access is to get the statement from the identified frame of the main memory.
To get a statement of the process from main memory with one memory access or to reduce the time for access, information
about frequently executed pages is stored in the translation look-aside buffer (TLB).
TLB contains a number of entries.
Each entry of the TLB contains a page number and its corresponding frame number.
TLB is associative in nature. All entries of TLB are searched at a time in parallel.
The following diagram shows the usage of TLB in paging.
When the CPU generates logical address of a statement, the page number of logical address is first searched in the TLB.

If the page number is found in the TLB (TLB hit) then the corresponding frame number is appended with the offset of logical
address to generate the corresponding physical address.

Otherwise (TLB miss), the page number of logical address is used as an index into the page table, the corresponding frame
number is identified and then appended with the offset of logical address to generate the physical address.

Hit ratio is the percentage of times that a particular page number is found in the TLB.

An 80 percent hit ratio means that we find the desired page number in the TLB 80 percent of the time.

The formula to calculate the effective memory access time is

Effective memory access time = probability of finding the page in the TLB x time to access the statement from the main
memory + probability of not finding the page in the TLB x time to access the statement from the main memory
If the hit ratio is 80 percent and it takes 20 nanoseconds to search the TLB, 100 nanoseconds to access the memory then the
effective memory access time is

Effective access time = 0.80 x (20+100) + 0.20 x (20+100+100) = 0.8 x 120 + 0.2 x 220 = 140 nanoseconds

If the hit ratio is 98 percent and it takes 20 nanoseconds to search the TLB, 100 nanoseconds to access the memory then the
effective memory access time is

Effective access time = 0.98 x 120 + 0.02 x 220 = 122 nanoseconds


Segmentation Technique

Segmentation is one of techniques used to load programs into main memory.

Operating system divides the space in main memory at the time of loading programs into main memory.

To load a program into main memory, the operating system divides the program into a number of segments of either equal
or unequal size.

The operating system then loads the segments of program into main memory wherever free space is available.

The operating system then creates a segment table for the program.

The number of entries in the segment table is equal to the number of segments in the program.

The entries of segment table are indexed with segment numbers.

Each entry of the segment table contains a base which indicates the starting address of the segment in the main memory,
and a limit/length which indicates the number of statements in that segment.

The following figure depicts how a program say P1 is loaded into main memory using segmentation technique.
31
The program P1 has 2000 statements and is divided into four segments.

The number of statements in the four segments are 500, 400, 300 and 800 respectively.

The segments are loaded into main memory and then a segment table is created for the program.

The segment table contains four entries. 32


Logical address to physical address mapping in Segmentation technique

To execute a statement of the program, the CPU generates the logical address of the statement.

The logical address consists of two parts: segment number and offset.

The segment number is used as an index into the segment table to identify the entry in the segment table.

Then, the offset value of logical address is compared with the limit value in the identified entry.

If the offset value is less than the limit value, then the offset value is added to the base value to generate the physical
address.

If the offset value is greater then the limit value, then an error is reported.

The following diagram depicts this mapping procedure.

33
Ex: Consider the following segment table:

Segment Base Length


0 620 450
1 1800 60
2 170 74
3 2145 321
4 1450 113

What are the physical addresses for the following logical addresses?
a) 0, 512
b) 1, 47
c) 2, 13
d) 3, 185

Physical addresses are:


a) Trap to the operating system as 512>450
b) 1800+47=1847 (47<60)
c) 170+13=183 (13<74)
d) 2145+185=2330 (185<321)
Module 5.1
Virtual Memory
Virtual Memory

In module 4, we discussed various memory-management strategies (contiguous allocation, paging, segmentation) used in
computer systems for loading programs into main memory.

All these strategies require that an entire program be in memory before it can execute.

Virtual memory is a technique that allows the execution of programs that are not completely in memory.

One major advantage of this scheme is that programs can be larger than physical memory.

Virtual memory allows to load more number of programs into main memory, thereby increases performance of computer
system.

Virtual memory also allows processes to share files and libraries, and to implement shared memory.
Demand Paging

Demand paging is commonly used in virtual memory systems.

With demand-paged virtual memory, pages are loaded only when they are demanded during program execution.

Pages that are never accessed are thus never loaded into physical memory.

The procedure used in demand paging technique to load programs into main memory is as follows:

Before loading any program into main memory, the space in main memory is divided into a number of equal size frames.

To load a program into the main memory, the program is divided into a number of equal size pages.

Loads the required pages of the program into the free frames of main memory.

Then, creates a page table for the program.

The Number of entries in the page table is equal to the number of pages in the program.
A valid-invalid bit is stored in each entry of the page table to indicate whether the page has been loaded into main memory or
not.

If the page is loaded into main memory, then the corresponding frame number is stored in the page table entry and the valid-
invalid bit is set to ‘v’.

If the page is not loaded into main memory, then the frame number field is empty and the valid-invalid bit is set to ‘i’.

To execute a statement of the program, CPU generates the logical address of the statement.

The logical address has two parts: page number and offset.

The page number of logical address is used as an index into the page table of the program.

If the entry of page table contains a frame number, then that frame number is appended with the offset of logical address to
generate the physical address.

If the entry of page table does not contain any frame number, then that situation is called a page fault.

A page fault occurs when the required page is not present in the main memory.
When a page fault occurs, the operating system has to load the required page into the main memory.
Operating system performs the following activities to load the required page when a page fault occurs.
1. Verifies whether any free frame is available in the main memory.
2. If any free frame is available, then loads the required page into the free frame and updates the page table of program.
3. Otherwise, replaces one of the pages in the main memory with the required page and updates the page table of
program. Operating System uses a page replacement algorithm for replacing one of the pages in the main memory.
4. Instructs the CPU to generate the same logical address again.

The following diagram depicts the activities performed by operating system to handle the page fault.
Free-Frame List

When a page fault occurs, the operating system must bring the desired page from secondary storage into main memory.

To resolve page faults, most operating systems maintain a free-frame list, a pool of free frames for satisfying such requests.

Operating system allocate free frames using a technique known as zero-fill-on-demand.

Zero-fill-on-demand frames are “zeroed-out” before being allocated, thus erasing their previous contents.

Performance of Demand Paging

Demand paging can affect the performance of a computer system.

Effective access time is calculated for a demand-paged memory as follows:

Effective access time = (1 − p) × memory access time + p × page fault time.

p is the probability of a page fault (0 ≤ p ≤ 1).


Memory access time indicates the time required to access the statement from main memory.

Page fault time is the time required to service the page fault i.e. the time required to load the required page into main
memory from secondary storage.

Ex: if the probability of occurring age fault is 0.2, memory access time is 100 nano seconds and the page fault time is 2000
nano seconds then the effective access time is

Effective access time=(1 - 0.2) X 100 + 0.2 X 2000 = 0.8 X 100 + 400 = 80 + 400 = 480 nano seconds

Page Replacement

During the execution of a program, if a page fault occurs then the operating system has to load the required page into the
main memory.

If the main memory is not having a free frame then the operating system has to replace one of the pages in the main
memory with the required page.

To select a page in main memory for replacement, the operating system uses different page replacement algorithms.
Page replacement algorithms

The following are different page replacement algorithms used by operating systems:

1. First in first out (FIFO) page replacement algorithm


2. Optimal page replacement algorithm
3. Least Recently Used (LRU) page replacement algorithm
4. Least Frequently Used (LFU) page replacement algorithm
5. Most Frequently Used (MFU) page replacement algorithm

Reference string
In a program the pages will be executed in any order and each page may be executed for any number of times.
Reference string indicates the order in which the pages of a program are executed or referenced.

10
First in first out page replacement algorithm

The page that was loaded for the first time into main memory is replaced by the requested page.

Ex1:-

Number of pages in the program = 8 (0 to 7)


Number of frames in the main memory = 3
Reference string: 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1

The replacement of pages in the main memory is shown in below figure:

Number of page faults=15 11


Ex2:-

Number of pages in the program = 8


Number of frames in the main memory = 4
Reference string: 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1

The replacement of pages in the main memory is shown in below figure:

Number of page faults=10


Number of page faults will be decreased by increasing the number of frames in main memory.

12
Ex3:

Number of pages in the program = 5 (1 to 5)


Number of frames in the main memory = 3
Reference String: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5

The replacement of pages in the main memory is shown in below figure:

Number of page faults=9

13
Ex4:

Number of pages in the program = 5 (1 to 5)


Number of frames in the main memory = 4
Reference String: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5

The replacement of pages in the main memory is shown in below figure:

14
Generally, the number of page faults will be decreased by increasing the number of frames in the main memory.

With FIFO algorithm, in some cases, the number of page faults increases when the number of frames in the main memory
are increased.

This situation is called “Belady’s Anamoly”.

Advantage:

Easy to implement when compared with other algorithms.

Disadvantages:

1) More number of page faults


2) Possibility of occurring Belady’s Anamoly.

To overcome from these drawbacks, Optimal page replacement algorithm is used.

15
Optimal page replacement algorithm

The page that will not be used for the longest period of time will be replaced by the requested page.

Ex1:

Number of pages in the program = 8 (0 to 7)


Number of frames in the main memory = 3
Reference string: 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1

The replacement of pages in the main memory is shown in below figure:

16
Ex2:

Number of pages in the program = 8


Number of frames in the main memory = 4
Reference string: 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1

The replacement of pages in the main memory is shown in below figure:

17
Ex3:

Number of pages in the program = 5 (1 to 5)


Number of frames in the main memory = 3
Reference String: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5

The replacement of pages in the main memory is shown in below figure:

18
Ex4:

Number of pages in the program = 5 (1 to 5)


Number of frames in the main memory = 4
Reference String: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5

The replacement of pages in the main memory is shown in below figure:

19
Advantages:

1) Less number of page faults compared to FIFO algorithm.


2) No chance of occurring Belady’s Anamoly.

Disadvantage:

This algorithm works based on the future references of pages.

If the operating system doesn’t know the order in which the pages will be referred, then it is not possible to calculate future
references for the pages and not possible to use this algorithm.

20
Least Recently Used Page Replacement Algorithm

The page that has not been used for the longest period of time is replaced by the requested page.

Ex1:

Number of pages in the program = 8


Number of frames in the main memory = 3
Reference string: 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1

The replacement of pages in the main memory is shown in below figure:

21
Ex2:

Number of pages in the program = 8


Number of frames in the main memory = 4
Reference string: 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1

The replacement of pages in the main memory is shown in below figure:

22
Advantages:

1) Less number of page faults compared to FIFO algorithm.


2) No chance of occurring Belady’s Anamoly.
3) Implementation is easy as it works based on the pages that have been referred or executed already.

Counting-Based Page Replacement

A reference count is maintained with each page loaded in the main memory.

Reference count of a page indicates the number of times that page is referenced or executed.

Two variants of counting based page replacement are:

1. Least Frequently Used (LFU) page replacement algorithm


2. Most Frequently Used (MFU) page replacement algorithm

Least Frequently Used (LFU) page replacement algorithm

Replaces the page with least reference count.

If two or more pages have the same least reference count, then out of the pages with least reference count the page that
23
was loaded first is replaced.
Ex1:

Number of pages in the program = 8


Number of frames in the main memory = 3
Reference string: 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1

The replacement of pages in the main memory is shown in below figure:

Number of page faults = 13

Most Frequently Used (MFU) page replacement algorithm

Replaces the page with highest reference count.

If two or more pages have the same highest reference count, then out of the pages with highest reference count the page
that was loaded first is replaced.
Ex1:

Number of pages in the program = 8


Number of frames in the main memory = 3
Reference string: 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1

The replacement of pages in the main memory is shown in below figure:

Number of page faults = 15


Modify bit or dirty bit

When a page fault occurs, if no frames are free, two page transfers (one for the page-out and one for the page-in) are
required as shown in the below figure.
This situation doubles the page-fault service time and increases the effective access time accordingly.

This overhead can be reduced by using a modify bit (or dirty bit).

When this scheme is used, each page has a modify bit associated with it.

The modify bit for a page is set whenever any byte in the page is written into, indicating that the page has been modified.

When a page is selected for replacement, its modify bit is examined.

If the bit is set, then the page must be written to the disk.

If the modify bit is not set, then the page is not required to be written to the disk. The page can be discarded.

This scheme reduces the time required to service a page fault.


Module 5.2
File Management
File

A file is a container of information that is stored in the secondary storage device (Hard disk).

Data cannot be written to disk unless they are within a file.

Files are broadly categorised into


1) program files
2) data files

Program files contain programs written in different programming languages.

Data files may contain numeric or text data, photos, music, video, and so on..

In general, a file is a sequence of bits, bytes, lines, or records, the meaning of which is defined by the file’s creator and user.

A file is named, for the convenience of its users, and is referred to by its name.

A name is usually a string of characters, such as example.c.


File Attributes

A file has an associated set of attributes.

A file’s attributes vary from one operating system to another.

The general attributes of a file are:

1) Name: a file has a name and is in human readable form.

2) Identifier: it is a unique number assigned by operating system. The operating system identifies a file by its identifier.

3) Type: indicates the type of information stored in the file.

4) Location: indicates the location or address of the file in the disk.

5) Size: indicates the current size of the file in bytes or words or blocks.

6) Protection: indicates the access permissions for different users.

7) Time, date and user identification: indicates the owner of file and the date and time on which the file is created and last
modified.
Directory

The information about all files is kept in the directory structure, which resides on the same device as the files themselves.

The directory has an entry for each file.

A directory entry consists of the file’s name and its unique identifier.

The identifier in turn locates the other file attributes.

File Operations

The seven basic operations performed on a file are:

1) Creating a file: to create a new file, the operating system has to allocate space for the file and create an entry for the file
in the directory.

2) Opening a file: a file has to be opened to perform any operations on the file. Upon successful opening of the file, a file
handle is returned using which operations are performed on the file.

3) Writing a file: to write to a file, file handle and the information to be written should be specified. The operating system
identifies the file and writes the information into the file at the position specified by file pointer and moves the file pointer
after completing the write operation.
4) Reading a file: to read from a file, file handle should be specified. The operating system identifies the file and then reads
the data from the position pointed by the file pointer and moves the file pointer after completing the read operation.

5) Repositioning within a file: repositions the file pointer. This operation is also called as file seek.

6) Deleting a file: the operating system search for the file, releases the file space and erase the directory entry of the file.

7) Truncating a file: the contents of the file are deleted but the space allocated to the file is not released.

Other operations that can be performed on a file are:

1) appending new information to the end of an existing file

2) renaming an existing file

3) create a copy of a file

4) copy the file to another i/o device

5) determining the status of a file


File Types

File type helps the operating system to decide what operations can be performed on the file.

File type is indicated through the name of file.

The file name is split into two parts: name and extension, separated by a period character.

Examples include resume.docx, server.c, and ReaderThread.cpp.

The following table depict some common file types, extensions and the content in that files.
Allocation Methods
Many files are stored on the same disk.

The main problem is how to allocate space to these files so that storage space is utilized effectively and files can be accessed
quickly.

To allocate space to these files three major methods are in wide use: contiguous, linked, and indexed.

Each method has advantages and disadvantages.

Some operating systems support all three.

An operating system uses one method for all files within a file-system type.
Contiguous Allocation

A set of contiguous blocks is allocated to each file.

If the file is n blocks long and starts at location b, then it occupies blocks b, b+1, b+2, ... ,b+n-1.

The directory entry for each file indicates the address of the starting block and the number of blocks allocated for that file.
Advantages:

1) Easy to implement

2) Supports both sequential and direct access to the blocks of file (For direct access to block i of a file that starts at block b,
we can immediately access block b + i ).

Disadvantages:

1) External fragmentation

As files are allocated and deleted, the free disk space is broken into pieces.

External fragmentation exists whenever free space is broken into chunks and when the largest contiguous chunk is
insufficient for a request.

Compaction technique can be used to solve the external fragmentation problem.

Compaction technique compact all free space into one contiguous space.

But, this compaction process requires lot of time.


2) Another problem with contiguous allocation is determining how much space is needed for a file.

When the file is created, the total amount of space it will need must be found and allocated.

If too little space is allocated to a file then the file cannot be extended.

If more space is allocated then some space may be wasted (internal fragmentation).

To minimize these drawbacks, some operating systems use a modified contiguous-allocation scheme.

In this scheme, a contiguous chunk of space is allocated initially; then, if that space is not enough, another chunk of
contiguous space, known as an extent is added.

The directory entry of the file now contains address of the starting block, block count, plus address of first block of the next
extent.
Linked Allocation

Linked allocation solves all problems of contiguous allocation.

With linked allocation, free blocks at any position of the disk can be allocated to a file.

For example, a file of five blocks may start at block 9 and continue at block 16, then block 1, then block 10, and finally block
25.

Each block allocated to the file contains a pointer to the next block allocated to the file.

The directory entry of a file contains a pointer to the first and last blocks of the file.
Advantages:

1) There is no external fragmentation with linked allocation, and any free block on the free-space list can be used to satisfy a
request.

2) The size of a file need not be declared when that file is created. A file can continue to grow as long as free blocks are
available.

Disadvantages:

1) Does not support direct access. To find the ith block of a file, we must start at the beginning of that file and follow the
pointers until we get to the ith block.

2) Another disadvantage is the space required for the pointers.

One solution to this problem is to collect blocks into multiples, called clusters and to allocate clusters rather than blocks.

For example, a cluster is defined as four blocks.

Pointers then use a much smaller percentage of the file's disk space.

Cluster mechanism improves disk throughput and decreases the space needed for block allocation and free-list
management.
But, this approach increases internal fragmentation, because more space is wasted when a cluster is partially full than when
a block is partially full.

3) Another problem with linked allocation is reliability. If a block allocated to a file is corrupted then it is not possible to
access the remaining blocks of the file.

File Allocation Table (FAT)

FAT is a variant of Linked allocation technique.

This simple but efficient method of disk-space allocation was used by the MS-DOS operating system.

A section of storage at the beginning of the disk is reserved for the FAT.

The FAT has one entry for each block in the disk and is indexed by block number.

Initially, all entries in FAT are filled with 0 to indicate that all blocks in the disk are initially free.

To store a file in the disk, free blocks at any position of the disk can be allocated to the file as in linked allocation technique.

In the directory entry, name of the file and number of the first block allocated to the file is stored.
In the FAT, the entry indexed by the first block number that is allocated to the file contains the number of the next block
allocated to the file.

This chain continues until it reaches the last block, which has a special end-of-file value as the table entry.

Consider a file occupying the blocks 217, 618, and 339.

Following figure shows how the blocks allocated to the file are linked with FAT.

Allocating a new block to a file is a simple matter of finding the first 0-valued table entry and replacing the previous end-of-
file value with the address of the new block.

The 0 is then replaced with the end-of-file value.


Indexed Allocation

Free blocks at any position of the disk can be allocated to a file as in linked allocation method.

The addresses of blocks allocated to the file are stored into another block called index block.

Each file has its own index block, which is an array of disk-block addresses.

The ith entry in the index block points to the ith block of the file.

The directory entry of the file contains the address of the index block.

To access the ith block, we use the pointer in the ith entry of index block.
Advantages:

1) There is no external fragmentation with indexed allocation, and any free block on the free-space list can be used to satisfy
a request.

2) The size of a file need not be declared when that file is created. A file can continue to grow as long as free blocks are
available.

3) Blocks of the file can be accessed directly. ith block of the file can be accessed directly using the pointer in the ith entry of
index block.

4) If one of the blocks allocated to the file is corrupted, still the remaining blocks of the file can be accessed.

Disadvantages:

1) Wastage of space in the index block of the file.

Consider a file which occupies only one or two blocks. With linked allocation, we lose the space of only one pointer per
block.

With indexed allocation, an entire index block must be allocated, even if only one or two pointers will be non-null.
2) Size of file is limited to number of pointers in the index block.

The following mechanisms are used to overcome this drawback:


1. Linked scheme
2. Multilevel index
3. Combined scheme

Linked scheme

If one index block is not enough to store the addresses of blocks allocated to a file then a number of index blocks are
allocated to the file and they are linked together.

The last entry in first index block contains the address of second index block.

This chain is repeated till the last index block of the file.

Multilevel index

This scheme uses a first-level index block to point to a set of second-level index blocks, which in turn point to the file blocks.

To access a block, the operating system uses the first-level index to find a second-level index block and then uses that block
to find the desired data block.
This approach could be continued to a third or fourth level, depending on the file size.

Combined scheme

This scheme is used in the Unix File System (UFS).

In this scheme, the first 15 pointers of the index block are stored in the file's inode.

The first 12 of these pointers point to direct blocks; that is, they contain addresses of blocks that contain data of the file.

Thus, the data for small files (of not more than 12 blocks) do not need a separate index block.

The next three pointers point to indirect blocks.

The first pointer points to a single indirect block, which is an index block containing not data but the addresses of blocks that
do contain data.

The second pointer points to a double indirect block, which contains the address of a block that contains the addresses of
blocks that contain pointers to the actual data blocks.

The last pointer contains the address of a triple indirect block.


Free Space Management

The operating system maintains a free-space list to keep track of free blocks in the disk.

The free-space list contains the addresses or numbers of free blocks in the disk.

To allocate blocks to a file, the operating system searches the free-space list and identifies the required number of free
blocks and allocates that blocks to the file.

The allocated blocks are then removed from the free-space list.

When a file is deleted, the blocks allocated to that file are added to the free-space list.

Methods for implementing Free Space List

1) Bit Vector
2) Linked List
3) Grouping
Bit Vector

The free-space list is implemented as a Bit Map or Bit Vector.

Each block is represented by one bit. If the block is free then the bit is 0; if the block is allocated then the bit is 1.

For example, consider a disk with 32 blocks (0 to 31) where blocks 2, 3, 4, 5, 8, 9, 10, 11, 12, 13, 17, 18, 25, 26, and 27 are
free and the rest of the blocks are allocated.

The free-space Bit Vector is

11000011000000111001111110001111

Advantage:

1) Easy to find first free block or n consecutive free blocks.


Disadvantage:

To store the Bit Vector, more space is required when the size of disk is large.

For example, if the size of disk is 1-TB (240 bytes) and size of each block in the disk is 4-KB (212 bytes) then the size of Bit
Vector is

240/212 = 228 bits = 225 bytes = 25 MB (32 MB)

32 MB space is required to store the Bit Vector.


Linked List

All free blocks in the disk are linked together.

The address of first free block is stored in a special location in the disk.

The first free block contains a pointer to the next free block, and so on.

For example, consider a disk where blocks 2, 3, 4, 5, 8, 9, 10, 11, 12, 13, 17, 18, 25, 26, and 27 are free and the rest of the
blocks are allocated.

In this situation, the address of block 2 is stored in the special location in the disk.

Block 2 will contain a pointer to block 3, which will point to block 4, which will point to block 5, which will point to block 8,
and so on.

Advantage:

No need to store the addresses of free blocks separately.

Disadvantage:

Difficult to get contiguous free blocks.


Grouping

The addresses of n free blocks are stored in the first free block.

The first n-1 of these blocks are actually free.

The last block contains the addresses of another n free blocks, and so on.

Assume the size of each block is 6 pointers.

Block number 2 contains the addresses of blocks 3, 4, 5, 8, 9, 10.

Block number 10 contains the addresses of blocks 11, 12, 13, 17, 18, 25.

Block number 25 contains the addresses of blocks 26, 27.

Advantage:

The addresses of a large number of free blocks can be found quickly.


Module 6
Storage Management and Security
Secondary-Storage Structure

The bulk of secondary storage for modern computers is provided by hard disk drives (HDDs) and nonvolatile memory (NVM)
devices.

we describe the basic mechanisms of these devices and explain how operating systems translate their physical properties to
logical storage via address mapping.

Hard Disk Drives

The following diagram shows the internal structure of a hard disk.

A hard disk consists of a number of platters.

Each disk platter has a flat circular shape, like a CD.

Diameter of platter ranges from 1.8 to 3.5 inches.

Information is stored on the two surfaces of platter.

A read-write head "flies" just above each surface of every platter.


The heads are attached to a disk arm that moves all the heads as a unit.

The surface of a platter is divided into circular tracks.

Each track is subdivided into hundreds of sectors.

The set of tracks that are at one arm position makes up a cylinder.

There may be thousands of cylinders in a disk.

Each sector has a fixed size and is the smallest unit of transfer.

The sector size was commonly 512 bytes until around 2010.

The storage capacity of common disk drives is measured in gigabytes and terabytes.

A disk drive with the cover removed is shown in the following figure.
When the disk is in use, a drive motor spins it at high speed (60 to 250 times per second).

The arm assembly moves in horizontal direction (back and forth).

The time required to access data from the disk is called access time.

The access time consists of the time necessary to move the disk arm to the desired cylinder, called the seek time and the
time necessary for the desired sector to rotate to the disk head, called the rotational latency.

Disks can transfer several megabytes of data per second, and they have seek times and rotational latencies of several
milliseconds.
Mapping logical blocks to physical sectors

In the logical view of disk, the disk is collection of blocks. Block is the smallest unit of transfer.

In the physical view of disk, the disk is collection of sectors. Sector is the smallest unit of transfer.

The size of a sector or block is usually 512 bytes.

The logical blocks are mapped to the physical sectors.

Sector 0 is the first sector of the first track on the outermost cylinder.

The mapping proceeds in order through that track, then through the rest of the tracks in that cylinder, and then through the
rest of the cylinders from outermost to innermost.

So, the block address is indicated as


<c, t, s>

‘c’ is the cylinder number, ‘t’ is the track number and ‘s’ is the sector number.
Ex: A hard disk has 63 sectors per track, 10 platters and 1000 cylinders.

1) Find the corresponding block number of the address <400, 16, 29>

400 x 20 x 63 + 16 x 63 + 29 = 504000 + 1008 + 29 = 505037

2) Find the address of block number 1039

Sector number = 1039 % 63 = 31


Track number = (1039 / 63) % 20 = 16 % 20 = 16
Cylinder number = 16 / 20 = 0

Address is <0, 16, 31>

3) Find the address of block number 3428

Sector number = 3428 % 63 = 26


Track number = (3428 / 63) % 20 = 54 % 20 = 14
Cylinder number = 54 / 20 = 2

Address is <2, 14, 26>


Formulae

1) Capacity of disk pack = Number of surfaces x Number of tracks per surface x Number of sectors per track x Storage
capacity of one sector

2) Average disk access time = Average Seek time + Average Rotational delay or latency + Transfer time

3) Average seek time = (k - 1) x t / 2

‘k’ is the number of tracks per surface


‘t’ is the time taken by head to move from one track to adjacent track

4) Average rotational latency = 1 / 2 x Time taken for full rotation

5) Transfer time = Number of bytes to be transferred / Data transfer rate

6) Data transfer rate = Number of heads x Capacity of one track x Number of rotations in one second

7) Formatted disk space = Total disk capacity - Formatting overhead

8) Formatting overhead = Number of sectors x Overhead per sector


Ex1: Consider a disk pack with the following specifications - 16 surfaces, 128 tracks per surface, 256 sectors per track and
512 bytes per sector. The disk is rotating at 3600 RPM. Average seek time is 11.5 msec. Format overhead is 32 bytes per
sector.

Answer the following questions-

1. What is the capacity of disk pack?


2. What is the data transfer rate?
3. What is the average access time?
4. What is the formatted disk space?

Capacity of disk pack = Number of surfaces x Number of tracks per surface x Number of sectors per track x Number of bytes
per sector = 16 x 128 x 256 x 512 bytes = 228 bytes = 256 MB

Data transfer rate = Number of heads x Capacity of one track x Number of rotations in one second = 16 x (256 x 512 bytes) x
(3600 / 60) rotations/sec = 24 x 28 x 29 x 60 bytes/sec = 60 x 221 bytes/sec = 120 MBps

Average rotational delay = 1/2 x Time taken for one full rotation = 1/2 x (1/3600) minutes = 1/2 x (60/3600) sec = 1/2 X 1/60
sec = 0.0083 sec = 0.0083 X 1000 msec = 8.3 msec

Average access time = Average seek time + Average rotational delay + Transfer time = 11.5 msec + 8.3 msec + 0 = 19.8 msec
Formatting overhead = Total number of sectors x overhead per sector = (16 x 128 x 256) x 32 bytes = 219 x 25 bytes = 224 bytes
= 16 MB

Formatted disk space = Total disk space – Formatting overhead = 256 MB - 16 MB = 240 MB

Ex2: What is the average access time for transferring 512 bytes of data with the following specifications-
• Average seek time = 5 msec
• Disk rotation = 6000 RPM
• Data rate = 40 KB/sec

Average rotational delay = 1/2 x Time taken for one full rotation = 1/2 x 1/6000 minutes = 1/2 X 60/6000 sec = 1/2 X 1/100
sec = 1/200 sec = 0.005 sec = 0.005 X 1000 msec = 5 msec

Transfer time = Number of bytes to be transferred / Data transfer rate = 512 bytes / 40 KB per sec = 29 bytes / 40 X 210 bytes
per sec = 0.0125 sec = 0.0125 X 1000 = 12.5 msec

Average access time = Average seek time + Average rotational delay + Transfer time = 5 msec + 5 msec + 12.5 msec = 22.6
msec
Ex3: Consider a typical disk that rotates at 15000 RPM and has a transfer rate of 50 x 106 bytes/sec. If the average seek time
of the disk is twice the average rotational delay. What is the average time (in milliseconds) to read or write a 512 byte sector
of the disk?

Average rotational delay = 1/2 x Time taken for one full rotation = 1/2 x 1/15000 minutes = 1/2 X 60/15000 sec = 2 msec

Average seek time = 2 x Average rotational delay = 2 x 2 msec = 4 msec

Transfer time = Number of bytes to be transferred / Data transfer rate = 512 bytes / (50 x 106 bytes/sec) = 10.24 x 10-6 sec =
0.01024 msec

Average time to read or write 512 bytes = Average seek time + Average rotational delay + Transfer time = 4 msec + 2 msec +
0.01024 msec = 6.11 msec
Ex4: A hard disk system has the following parameters-
• Number of tracks = 500
• Number of sectors per track = 100
• Number of bytes per sector = 500
• Time taken by the head to move from one track to another adjacent track = 1 msec
• Rotation speed = 600 RPM

What is the average time taken for transferring 250 bytes from the disk?

Average seek time = (500 -1) x 1 msec / 2 = 249.5 msec

Average rotational delay = 1/2 x Time taken for one full rotation = 1/2 x 1/600 minutes = 1/2 x 60/600 sec = 1/200 sec =
0.005 sec = 0.005 X 1000 msec = 50 msec

Capacity of one track = Number of sectors per track x Number of bytes per sector = 100 x 500 bytes = 50000 bytes

Data transfer rate = Number of heads x Capacity of one track x Number of rotations in one second = 1 x 50000 bytes x (600 /
60) = = 50000 x 10 bytes/sec = 5 x 105 bytes/sec

Transfer time = (250 bytes / 5 x 105 bytes) sec = 50 x 10-5 sec = 0.5 msec

Average time taken to transfer 250 bytes = Average seek time + Average rotational delay + Transfer time = 249.5 msec + 50
msec + 0.5 msec = 300 msec
Ex5: A certain moving arm disk storage with one head has the following specifications-
• Number of tracks per surface = 200
• Disk rotation speed = 2400 RPM
• Track storage capacity = 62500 bits

Calculate Average latency and Data transfer rate.

Average latency = 1/2 x Time taken for one full rotation = 1/2 x 1/2400 minutes = 1/2 X 60 / 2400 sec = 1/2 X 1/40 sec = 1/80
sec = 0.0125 sec = 0.0125 X 1000 msec = 12.5 msec

Data transfer rate = Number of heads x Capacity of one track x Number of rotations in one second = 1 x 62500 bits x (2400 /
60) = 2500000 bits/sec = 2.5 x 106 bits/sec
Disk Scheduling

Operating system has to use the hardware efficiently.

To use the disk efficiently, the operating system has to minimize the access time and maximize the bandwidth.

The access time and bandwidth are improved by managing the order in which the disk I/O requests are serviced.

The disk I/O requests are placed in the disk queue.

To service these requests, several disk scheduling algorithms are used.

The different disk scheduling algorithms are:

1) First Come First Serve (FCFS) Scheduling


2) Shortest Seek Time First (SSTF) Scheduling
3) SCAN Scheduling
4) C- SCAN Scheduling
5) LOOK Scheduling
6) C-LOOK Scheduling
First Come First Serve (FCFS) Scheduling

This algorithm is simple, but it does not provide the fastest service.

Consider a disk queue with requests for I/O to blocks on the cylinders 98, 183, 37, 122, 14, 124, 65, 67.

If the disk head is initially at cylinder 53, it will first move from 53 to 98, then to 183, 37, 122, 14, 124, 65, and finally to 67.

The total head movement is 640 cylinders.

This schedule is diagrammed in the following figure.


SSTF (Shortest Seek Time First) Scheduling

The SSTF algorithm selects the request with the least seek time from the current head position.

In other words, SSTF chooses the pending request closest to the current head position.

For the example request queue (98, 183, 37, 122, 14, 124, 65, 67), the closest request to the initial head position (53) is at
cylinder 65.

After processing the request at cylinder 65, the next closest request is at cylinder 67.

From there, the request at cylinder 37 is closer, so 37 is served next.

Continuing, we service the request at cylinder 14, then 98, 122, 124, and finally 183.

This scheduling method results in a total head movement of only 236 cylinders.

This schedule is diagrammed in the following figure.


SSTF scheduling may cause starvation of some requests.

Suppose that we have two requests in the queue, for cylinders 14 and 186, and while the request for 14 is being serviced, a
new request near 14 arrives.

This new request will be serviced next, making the request at 186 wait.

While this request is being serviced, another request close to 14 could arrive.

A continual stream of requests near one another can cause the request for cylinder 186 to wait indefinitely.
SCAN Scheduling

In the SCAN algorithm, the disk arm starts at one end of the disk and moves towards the other end, servicing requests along
the way, until it gets to the other end of the disk.

At the other end, the direction of head movement is reversed, and servicing continues.

The head continuously scans back and forth across the disk.

Before applying SCAN algorithm to schedule the requests, we need to know the direction of head movement in addition to
the head's current position.

Assuming that the disk arm is moving towards 0 and that the initial head position is again 53, the head will next service 37
and then 14.

At cylinder 0, the arm will reverse and will move towards the other end of the disk, servicing the requests at 65, 67, 98, 122,
124, and 183.

The total head movement is 236 cylinders.

This schedule is diagrammed in the following figure.


When the head reaches one end and reverses direction, very few requests are in front of the head, since these cylinders have
recently been serviced.

More requests will be at the other end of the disk and these requests have also waited the longest.
C-SCAN Scheduling

Circular SCAN (C-SCAN) is a variant of SCAN and provides a more uniform wait time.

Like SCAN, C-SCAN moves the head from one end of the disk to the other, servicing requests along the way.

When the head reaches the other end, it immediately returns to the beginning of the disk without servicing any requests on
the return trip.

This schedule is diagrammed in the following figure.

The total head movement is 383 cylinders.


LOOK Scheduling

Both SCAN and C-SCAN move the disk arm across the full width of the disk.

In practice, the arm goes only as far as the final request in each direction.

Then, it reverses direction immediately, without going all the way to the end of the disk.

Versions of SCAN and C-SCAN that follow this pattern are called LOOK and C-LOOK scheduling, because they look for a
request before continuing to move in a given direction.

The working of C-LOOK algorithm is scheduled in the following figure.


Ex: Consider a disk with 200 cylinders (0 to 199). Consider a disk queue with requests for I/O to blocks on the cylinders 82,
170, 43, 140, 24, 16, 190. The disk head is initially at cylinder 50. Head moves towards smallest cylinder number. Calculate
total head movement under
1) FCFS
2) SSTF
3) SCAN
4) C-SCAN
5) LOOK
6) C-LOOK

You might also like