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

OS R-23

The document provides an overview of operating systems, detailing their functions, types, and components. It explains the roles of hardware and software, the management of processes, memory, files, and I/O systems, as well as the importance of security and networking. Additionally, it covers various computing environments and the concept of free and open-source operating systems.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
102 views

OS R-23

The document provides an overview of operating systems, detailing their functions, types, and components. It explains the roles of hardware and software, the management of processes, memory, files, and I/O systems, as well as the importance of security and networking. Additionally, it covers various computing environments and the concept of free and open-source operating systems.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 121

LECTURE NOTES

OPERATING SYSTEMS
(2024 – 2025)

II B. Tech II Semester (R-23)


Subject Code: 23A35401T

Mr. B.V. Chandra Sekhar, M. Tech.,(Ph. D)


Assistant Professor.

Department of Computer Science and Engineering

RAJEEV GANDHI MEMORIAL COLLEGE OF ENGINEERING & TECHNOLOGY


(Autonomous)
Approved by AICTE,
New Delhi. Accredited by NAAC with A+ Grade.
Affiliated to J.N.T.University, Ananthapuram.
Nandyal – 518501. Kurnool (dist.), A.P.
UNIT-1
Operating Systems Overview: Introduction, Operating system functions, Operating systems
operations, Computing environments, Free and Open-Source Operating Systems
System Structures: Operating System Services, User and Operating-System Interface, system
calls, Types of System Calls, system programs, Operating system Design and Implementation,
Operating system structure, Building and Booting an Operating System, Operating system
debugging

Operating Systems Overview: Introduction


System is nothing but environment. Environment consists of components.
Operating system:-
⇒ The basic objective of the operating system is to operate several components
associated with computer system.
⇒ System is an environment consists of several components.
⇒ System having two components (basically)
i) Hardware components
ii) Software components.
i) Hardware components:-
⇒ There are physical components.
⇒ The components which are visible, touchable part of the computer
system that can perform basic function is known as hardware.
Eg. Input devices, O/P devices, m/m, Processor.
ii) Software components:-
⇒ These are the untouchable components having logical existence.
⇒ These are the set of programs that controls the operation
of the physical components.
⇒ Programs are the set of instructions.
Software
System S/W Application S/W
System software:-
⇒ It is meant for running of a system.
⇒ It is a set of programs designed to make function able of different components.
Application software:-
⇒ These are the S/W related to user’s requirement.
⇒ Thus application software can’t be executed alone. It always take the help of
system software to be executed.
⇒ These are the set of programs that are designed to execute diff. app.
Program. e.g. word processor, spread sheet, railway/ air reservation.

Mr.B V CHANDRA SEKHAR CSE(R-23) Page 1


What is an Operating System?
An operating system is system software that acts as an interface between a user of a
computer and the computer hardware. It is software that manages the computer hardware and
allows the user to execute programs in a convenient and efficient manner.

Operating system goals:

• Execute user programs and make solving user problems easier

• Make the computer system convenient to use

• Use the computer hardware in an efficient manner

What Operating Systems do?

Abstract view of the components of a computer system.

 Computer system divided into 4 components:


 Hardware – provides basic computing resources
 CPU, memory, I/O devices
 Operating system
 Controls and coordinates use of hardware among various applications and users
 Application programs – define the ways in which the system resources are used
to solve the computing problems of the users

Mr.B V CHANDRA SEKHAR CSE(R-23) Page 2


 Word processors, compilers, web browsers, database systems, video games
 Users
 People, machines, other computers
Abstract view of the components of a computer system.

 Operating system can be viewed as a resource allocator.

 The OS acts as the manager of the resources (such as CPU time, memory space, files
storage space, I/O devices) and allocates them to specific programs and users as necessary
for tasks.

 An operating system is a control program. It controls the execution of user programs to


prevent errors and improper use of computer.

Views of an OS:-
The operating system from two view points:
o User View
o System View
 User view:-
o In user view the operating system is designed for only fulfilling the requirement of the
user.
o These systems are designed for the single-user experience.
o Some operating systems are designed to maximize resource utilization to assure that all available
CPUtime, memory, and I/O are used efficiently and no individual user takes more than his share.
o These are multi-user systems where terminals are connected to mainframe or
minicomputers.
o users share resources and may exchange information.

 System view:-
o In this view OS is designed to be a resource allocator or resource manager.
o The program that is most intimately involved with the hardware.
o The operating system is a resource allocator.
o The following resources may be required to solve a problem:
o CPU time
o memory space
o file-storage space
o I/O devices
o etc.
o The operating system acts as the manager of these resources.

 Types of Operating Systems:


1. Batch Operating System – This type of operating system does not interact with the computer
directly. There is an operator which takes similar jobs having the same requirement and groups
them into batches. It is the responsibility of the operator to sort jobs with similar needs.
Mr.B V CHANDRA SEKHAR CSE(R-23) Page 3
2. Time-Sharing Operating Systems –

Each task is given some time to execute so that all the tasks work smoothly. Each user gets the time of CPU
as they use a single system. These systems are also known as Multitasking Systems. The task can be from a
single user or different users also. The time that each task gets to execute is called quantum. After this time
interval is over OS switches over to the next task.

3. Distributed Operating System

These types of the operating system are a recent advancement in the world of computer technology and
are being widely accepted all over the world and, that too, with a great pace. Various autonomous
interconnected computers communicate with each other using a shared communication network.
Independent systems possess their own memory unit and CPU. These are referred to as loosely coupled
systems or distributed systems.

4. Real-Time Operating System –

These types of OSs serve real-time systems. The time interval required to process and respond to inputs is
very small. This time interval is called response time.

Real-time systems are used when there are time requirements that are very strict like missile systems, air
traffic control systems, robots, etc.

Two types of Real-Time Operating System which are as follows:

Mr.B V CHANDRA SEKHAR CSE(R-23) Page 4


• Hard Real-Time Systems: These OSs are meant for applications where time constraints are very strict and
even the shortest possible delay is not acceptable. These systems are built for saving life like automatic
parachutes or airbags which are required to be readily available in case of any accident. Virtual memory is
rarely found in these systems.

• Soft Real-Time Systems: These OSs are for applications where for time-constraint is less strict

 Operating system functions


1. Process Management
 A process is a program in execution.
 The operating system is responsible for managing the processes i.e assigning the processor
to a process at a time.
 This is known as process scheduling. The different algorithms used for process scheduling
are FCFS (first come first served), SJF (shortest job first), priority scheduling, round robin
scheduling etc
 A process needs certain resources, including CPU time, memory, files, and I/O devices, to
accomplish its task.
 The operating system is responsible for the following activities in connection with process
management.
✦ Process creation and deletion.
✦ process suspension and resumption.
2. Main-Memory Management
 This module of an OS takes care the allocation and deallocation of memory to
various processes according to their requirement.
 It is a repository of quickly accessible data shared by the CPU and I/O devices.
 Main memory is a volatile storage device. It loses its contents in the case of system
failure.
 The operating system is responsible for the following activities in connections with
memory management: It mainly follow the two approaches

Mr.B V CHANDRA SEKHAR CSE(R-23) Page 5


1. Contagious memory – a) MFT(Multiprogramming Fixed Task)
b) MVT (Multiprogramming with Variable Task)
2. Non- Contagious memory:- a) Paging
b) Segmentation
3. File Management
A file is a collection of related information defined by its creator. Commonly, files represent
programs (both source and object forms) and data.
The operating system is responsible for the following activities in connections with file
management:
✦ File creation and deletion.
✦ Directory creation and deletion.
✦ Support of primitives for manipulating files and directories.
✦ Mapping files onto secondary storage.
✦ File backup on stable (non-volatile) storage media.
4. I/O System Management
 An Operating System manages the devices communication via their respective devices
 It keep track of all devices connected to the system
 If device which process gets access to a certain device and for how long
 Ex:-print the document to connect the I/O devices and communicate them
The I/O system consists of:
✦ A buffer-caching system
✦ A general device-driver interface
✦ Drivers for specific hardware devices
5. Secondary-Storage Management
Since main memory (primary storage) is volatile (not permanently) and too small to
accommodate all data and programs permanently, the computer system must provide secondary
storage (hard disk )to back up main memory.
Most modern computer systems use disks as the principle on-line storage medium, for both
programs and data.
The operating system is responsible for the following activities in connection with disk
management:
✦ Free space management
✦ Storage allocation
✦ Disk scheduling
6. Networking (Distributed Systems)
A distributed system is a collection processors that do not share memory or a clock. Each
processor has its own local memory.
The processors in the system are connected through a communication network.
 Communication takes place using a protocol.
 A distributed system provides user access to various system resources.

Mr.B V CHANDRA SEKHAR CSE(R-23) Page 6


 Access to a shared resource allows:
 Computation speed-up
 Increased data availability
✦ Enhanced reliability
7. Protection System (Security)
Protection refers to a mechanism for controlling access by programs, processes, or users to
both system and user resources.
Privacy provides by operating system that means passwords, so that unauthorized
applications can’t access program or data
The protection mechanism must:
✦ distinguish between authorized and unauthorized usage.

 Operating systems operations


 Modern operating systems are interrupt driven. Events are almost always signalled by the
occurrence of an interrupt or a trap
 Two types of interrupt
1. Hardware interrupt
2. Software interrupt
1. Hardware interrupt: The interrupt will generated by hardware devices i.e Hardware
interrupt
2. Software interrupt: A trap (or an exception) is a software-generated interrupt caused
either by an error or by a specific request from a user program that an operating-system
service be performed.
 When the CPU is executing the program it ay receives either h/w or s/w interrupts with the
help of operating system. It will handle the corresponding interrupts by using the following
operations.
Dual-Mode Operation & timer based operation
Since the operating system and the user programs share the hardware and software resources of the
computer system, it has to be made sure that an error in a user program cannot cause problems to
other programs and the Operating System running in the system. The approach taken is to use a
hardware support that allows us to differentiate among various modes of execution.
The system can be assumed to work in two separate modes of operation:
1. User mode
2. Kernel mode (supervisor mode, system mode, or privileged mode).

Mr.B V CHANDRA SEKHAR CSE(R-23) Page 7


 In order to maintain the mode A hardware bit of the computer, called the mode bit, is used
to indicate the current mode: kernel (0) or user (1).
 With the mode bit, we are able to distinguish between a task that is executed by the
operating system and one that is executed by the user.
 When the computer system is executing a user application, the system is in user mode.
 Now the system call is interrupt (trap) now whenever it receive the interrupt then the mode
bit will change from 1 to 0 . so system call service is need
 When a user application requests a service from the operating system (via a system call), the
transition from user to kernel mode takes place.
 The operating system (via a system call) is executed in kernel mode
 When the kernel mode is zero then the cpu start execution in kernel mode the ode bit change
0 to 1,CPU starts reaming instructions.
Timer based operations.
Timer to prevent infinite loop / process hogging resources set interrupt after specific period
• Timer based operations we have fixed time period & variable time period
• In fixed time, the interrupt time is fixed i.e at particular point of time that was fixed at that time interrupt
is generated.
• Example 1 second at the given time interrupt will be generated.
• Where as in variable time period the time is not fixed for example 1millisecond to 1second in the given
period of time the interrupt will be generated.
• Initially counter set to Zero.
• Operating system decrements counter for every operation.
• When counter zero generate an interrupt
• Set up before scheduling process to regain control or terminate program that exceeds allotted time
 Computing Environments
 Computer environment is a collection of computers, machines, software and network that
support the processing and exchange as electronic information
 To develop and execute the program in different environment

Mr.B V CHANDRA SEKHAR CSE(R-23) Page 8


Types of Computing Environments
A computer system uses many devices, arranged in different ways to solve many problems. This constitutes
a computing environment where many computers are used to process and exchange information to handle
multiple issues.
The different types of Computing Environments are –

Personal Computing Environment


• In the personal computing environment, there is a single computer system.
• All the system processes are available on the computer and executed there. The different devices that
constitute a personal computing environment are laptops, mobiles, printers, computer systems, scanners
etc.
Time Sharing Computing Environment
• The time sharing computing environment allows multiple users to share the system simultaneously.
• Each user is provided a time slice and the processor switches rapidly among the users according to it.
Because of this, each user believes that they are the only ones using the system.
Client Server Computing Environment
• In client server computing, the client requests a resource and the server provides that resource.
• A server may serve multiple clients at the same time while a client is in contact with only one server.
Both the client and server usually communicate via a computer network but sometimes they may reside in
the same system.
Distributed Computing Environment
• A distributed computing environment contains multiple nodes that are physically separate but linked
together using the network. All the nodes in this system communicate with each other and handle
processes in tandem.
• Each of these nodes contains a small part of the distributed operating system software.
Cloud Computing Environment
• The computing is moved away from individual computer systems to a cloud of computers in cloud
computing environment
• . The cloud users only see the service being provided and not the internal details of how the service is
provided. This is done by pooling all the computer resources and then managing them using software.

Mr.B V CHANDRA SEKHAR CSE(R-23) Page 9


 Free and Open-Source Operating Systems
• Operating systems made available in source-code format rather than closed source in binary code
• Open source operating system source code is revelled.
• Source code is used to build the operating system.
• Source code is in readable format present in the form of programming language that can be visible
publically, so that code can be modified.
• Counter to the copy protection and Digital Rights Management (DRM) movement
• Started by Free Software Foundation (FSF), which has “copy left” GNU Public License (GPL)
• Examples include Android os, chrome os and Linux operating system.
 System Structures:
An operating system is a structure that allows the user application programs to interact
with the system hardware. An easy way to do this is to create the operating system in
parts. Each of these parts should be well defined with clear inputs, outputs and functions.
Following are various popular implementations of Operating System structures.
 Simple Structure
 Monolith Structure
 Micro-Kernel Structure
 Exo-Kernel Structure
 Layered Structure
 Modular Structure

Simple Structure:

The Simple structured operating systems do not have a well-defined structure. These systems will be
simple, small and limited systems. Example: MS-DOS.

 In MS-DOS, the interfaces and levels of functionality are not well separated.
 In MS-DOS application programs are able to access the basic I/O routines. This causes the entire
systems to be crashed when user programs fail.
Advantages
 Easy Development - In simple operation system, being very few interfaces, development is easy
especially when only limited functionalities are to be delivered.
 Better Performance - Such a sytem, as have few layers and directly interects with hardware, can
provide a better performance as compared to other types of operating systems.

Mr.B V CHANDRA SEKHAR CSE(R-23) Page 10


Disadvantages
 Frequent System Failures - Being poorly designed, such a system is not robust. If one program fails,
entires operating system crashses. Thus system failures are quiet frequent in simple operating
systems.
 Poor Maintainability - As all layers of operating systems are tightly coupled, change in one layer
can impact other layers heavily and making code unmanageable over a period of time.

Non Simple Structure -- UNIX


UNIX is a powerful Operating System initially developed by Ken Thompson, Dennis Ritchie at AT&T Bell
laboratories in 1970.

Beyond simple but not fully layered


The structure of Unix OS Layers are as follows:

Monolith Structure
 In monolith structured operating system, a central piece of code called kernel is responsible for all
major operations of an operating system.
 Such operations include file management, memory management, device management and so on.
 The kernel is the main component of an operating system and it provides all the services of an
operating system to the application programs and system programs.
 The kernel has access to the all the resources and it acts as an interface with application programs
and the underlying hardware.
 A monolithic kernel structure promotes timesharing, multiprogramming model and was used in
old banking systems.
Mr.B V CHANDRA SEKHAR CSE(R-23) Page 11
Advantages
 Easy Development - As kernel is the only layer to develop with all major functionalities, it is easier
to design and develop.
 Performance - As Kernel is responsible for memory management, other operations and have direct
access to the hardware, it performs better.
Disadvantages
 Crash Prone - As Kernel is responsible for all functions, if one function fails entire operating system
fails.
 Difficult to enhance - It is very difficult to add a new service without impacting other services of a
monolith operating system.
Micro-Kernel Structure

 Moves as much from the kernel into user space


 Mach example of microkernel
 Mac OS X kernel (Darwin) partly based on Mach
 Communication takes place between user modules using message passing
 Microkernel is the important functionalities will be placed in kernel. whereas un-necessary details
are placed in user mode.
 Here Microkernel contain only necessary components (functionalities) such as IPC(inter process
communication),memory management and cpu scheduling.
 The code will become small in micro kernel because it remove un-necessary components of kernel
and place them into user mode
 So size of the kernel is will become quite easy. That’s way it is called micro kernel

Mr.B V CHANDRA SEKHAR CSE(R-23) Page 12


Benefits:
 Easier to extend a microkernel
 Easier to port the operating system to new architectures
 More reliable (less code is running in kernel mode)
 More secure
Detriments:
 Performance overhead of user space to kernel space communication
Layered Approach
 The operating system is divided into a number of layers (levels), each built on top of lower layers.
 The bottom layer (layer 0), is the hardware; the highest (layer N) is the user interface.
 With modularity, layers are selected such that each uses functions (operations) and services of only
lower-level layers
 Here each layer use the services and provide by its both lower and upper layers.
 Ex:- If the layer 1 use the services and provided by at the same time layer zero and layer 2 also.
 The main advantage of this layered approach is it very simple to construct as well as debugging.
Advantages:
 High Customizable - Being layered, each layer implmentation can be customized easily. A new
functionality can be added without impacting other modules as well.
 Verifiable - Being modular, each layer can be verified and debugged easily.
Disadvantages:
 Less Performant - A layered structured operating system is less performant as compared to basic
structured operating system.
 Complex designing - Each layer is to planned carefully as each layer communicates with lower layer
only and a good design process is required to create a layered operating system.

Modular Structure
 Modular structure operating system works on the similar principle as a monolith but with better
design.
 A central kernel is responsible for all major operations of operating system.
 This kernel has set of core functionality and other services are loaded as modules dynamically to
the kernel at boot time or at runtime.

Mr.B V CHANDRA SEKHAR CSE(R-23) Page 13


 Sun Solaris OS is one of the examples of Modular structured operating system.
Ex:-Solaris Modular Approach

Advantages
 High Customizable - Being modular, each module implementation can be customized easily. A new
functionality can be added without impacting other modules as well.
 Verifiable - Being modular, each layer can be verified and debugged easily.
Disadvantages
 Less Performance - A modular structured operating system is less performance as compared to
basic structured operating system.
 Complex designing - Each module is to planned carefully as each module communicates with
kernal. A communication API is to be devised to facilitate the communication.

 Operating System Services


An operating system provides an environment for the execution of programs. It provides certain services to
programs and to the users of those

OS provide services for the users of the system, including:

Views of an OS:-
There are two views related to an OS, i.e.
i) User view
ii) System view.

Mr.B V CHANDRA SEKHAR CSE(R-23) Page 14


Following are a few common services provided by an operating system –

 User Interfaces
 Program execution
 I/O operations
 File System manipulation
 Communication
 Error Detection
 Resource Allocation
 Accounting
 Protection & security

Services to provide by user point of view

 User Interfaces – Operating System is interface between user and computer .It Means
users can provide interface for accessing the computer.we have different type of
interfaces

 CLI(command-line interface) ( e.g. sh, csh, ksh, tcsh, etc.)

 

 GUI(Graphical User Interface) (e.g. Windows, X-Windows, KDE, Gnome, etc.), or a


batch command systems.
 In Command Line Interface (CLI) - commands are given to the system.
 In Batch interface - commands and directives to control these commands are
put in a file and then the file is executed.

 In GUI systems - windows with pointing device to get inputs and keyboard
to enter the text

 Program Execution - The OS must be able to load a program into RAM, run the
program, and terminate the program, either normally or abnormally.
 I/O Operations - The OS is responsible for transferring data to and from I/O devices,
including keyboards, terminals, printers, and files. For specific devices, special
functions are provided (device drivers) by OS.
 File-System Manipulation – Programs need to read and write files or directories. The
services required to create or delete files, search for a file, list the contents of a file and
change the file permissions are provided by OS.
 Communications - Inter-process communications, IPC, either between processes
running on the same processor, or between processes running on separate processors or

Mr.B V CHANDRA SEKHAR CSE(R-23) Page 15


separate machines. May be implemented by using the service of OS- like shared
memory or message passing.
 Error Detection - Both hardware and software errors must be detected and handled
appropriately by the OS. Errors may occur in the CPU and memory hardware (such as
power failure and memory error), in I/O devices (such as a parity error on tape, a
connection failure on a network, or lack of paper in the printer), and in the user
program (such as an arithmetic overflow, an attempt to access an illegal memory
location).

OS provide services for the efficient operation of the system, including:

 Resource Allocation – Resources like CPU cycles, main memory, storage space, and
I/Odevices must be allocated to multiple users and multiple jobs at the same time.
 Accounting – There are services in OS to keep track of system activity and resource
usage, either for billing purposes or for statistical record keeping that can be used to
optimize future performance.
 Protection and Security – The owners of information (file) in multiuser or networked
computer system may want to control the use of that information. When several separate
processes execute concurrently, one process should not interfere with other or with OS.
Protection involves ensuring that all access to system resources is controlled. Security of
the system from outsiders must also be done, by means of a password.

 User Operating-System Interface


 Two ways that users interface with the operating system:
 Command Interpreter(Command-line interface)
 Graphical User Interface(GUI)

Command Interpreter (Command-line interface)


 Main function of command interpreter is to get and execute the next user- specified command.
 Many of the commands are used to manipulate, create, copy, print, execute, etc. files.
 Two general ways to implement these commands:
 Command interpreter self contains code to execute command;
 Commands are implemented through system programs.

Graphical User Interface (GUI)


 User-friendly desktop metaphor interface

Mr.B V CHANDRA SEKHAR CSE(R-23) Page 16



Usually mouse, keyboard, and monitor

Icons represent files, programs, actions, etc

Various mouse buttons over objects in the interface cause various actions (provide
information, options, execute function, open directory (known as a folder)
 Invented at Xerox PARC
 Many systems now include both CLI and GUI interfaces
 Microsoft Windows is GUI with CLI “command” shell
 Apple Mac OS X is “Aqua” GUI interface with UNIX kernel underneath and shells available
 Unix and Linux have CLI with optional GUI interfaces (CDE, KDE, GNOME)
 System Calls
What are system calls in Operating System?
 The interface between a process and an operating system is provided by system
calls.
 In general, system calls are available as assembly language instructions.
 When a program executes a system call, it transitions from user mode to kernel
mode, granting access to system resources and functionalities that are otherwise
restricted.

An example to illustrate how system calls are used: writing a simple program to read data from
one file and copy them to another file.
 There are number of system calls used to finish this task. The first system call is to
write a message on the screen (monitor). Then to accept the input filename. Then another
system call to write message on the screen, then to accept the output filename.
 When the program tries to open the input file, it may find that there is no file of that
name or that the file is protected against access. In these cases, the program should print
a message on the console (another system call) and then terminate abnormally (another
system call) and create a new one (another system call).
 Now that both the files are opened, we enter a loop that reads from the input file

Mr.B V CHANDRA SEKHAR CSE(R-23) Page 17


(another system call) and writes to output file (another system call).
 Finally, after the entire file is copied, the program may close both files (another system
call), write a message to the console or window (system call), and finally terminate
normally (final system call).
 Most programmers do not use the low-level system calls directly, but instead use an
"Application Programming Interface", API.
 Instead of direct system calls provides for greater program portability between different
systems. The API then makes the appropriate system calls through the system call
interface, using a system call table to access specific numbered system calls.

Each system call has a specific numbered system call. The system call table (consisting of
system call number and address of the particular service) invokes a particular service
routine for a specific system call.
 The caller need know nothing about how the system call is implemented or what it
doesduring execution.


Three general methods used to pass parameters to OS are –
 To pass parameters in registers.
 If parameters are large blocks, address of block (where parameters are stored in
memory) issent to OS in the register. (Linux & Solaris).
 Parameters can be pushed onto the stack by program and popped off the stack by OS.

Mr.B V CHANDRA SEKHAR CSE(R-23) Page 18


 Types of System Calls in OS
There are commonly five types of system calls. These are as follows:
1. Process Control
2. File Management
3. Device Management
4. Information Maintenance
5. Communication

1. Process Control
These system calls are used for process management tasks such as creating, terminating, and controlling
processes. Process control refers to the management and manipulation of processes within an operating
system. System calls related to process control allow for the creation, termination, and manipulation of
processes, as well as providing mechanisms for process synchronization and communication.
Examples include fork(), exec(), wait(), and exit().

 fork(): Creates a new process by duplicating the calling process. The new process, known as the child
process, has its own address space but shares the code and data of the parent process.

 exec(): Replaces the current process with a new process loaded from an executable file. It loads a new
program into the current process's address space and starts its execution.

 wait(): Suspends the calling process until one of its child processes terminates. It allows the parent
process to synchronize its execution with the termination of child processes.

 exit(): Terminates the calling process and releases all associated resources. It also returns an exit status to
the parent process or the operating system.

2. File Management
 File management in an operating system involves the creation, deletion, manipulation, and
organization of files and directories. System calls related to file management provide interfaces for
performing these operations, such as opening, closing, reading from, and writing to files, as well as
navigating directory structures.
 System calls in this category are used for file-related operations such as creating, opening, reading,
writing, closing, and deleting files. Examples include open(), close(), read(), write(), and unlink().

Mr.B V CHANDRA SEKHAR CSE(R-23) Page 19


 open(): Opens a file or creates a new file if it does not exist. It returns a file descriptor that can be
used for subsequent read or write operations.
 close(): Closes the specified file descriptor, releasing any associated resources.
 read(): Reads data from an open file descriptor into a buffer.
 write(): Writes data from a buffer to an open file descriptor.
 unlink(): Deletes the specified file from the file system.

3. Device Management
These system calls manage devices such as printers, disk drives, and network interfaces. They allow
processes to perform operations like reading from and writing to devices.
Examples include read(), write(), ioctl(), and open().

 open(): Opens a device file or a communication channel to a device.


 close(): Closes the device file or communication channel.
 read(): Reads data from the device into a buffer.
 write(): Writes data from a buffer to the device.
 ioctl(): Performs I/O control operations on devices, such as configuring device parameters or
sending control commands.
4. Information Maintenance
System calls in this category provide information about system resources, processes, and their attributes.
This ensures efficient storage, retrieval, and manipulation of data while safeguarding against unauthorized
access and maintaining system reliability.
Examples include getpid(), getuid(), getgid(), and time().
 getpid(): Retrieve the process ID of the current process.
 time(): Get the current time in seconds since the Unix epoch.
 gettimeofday(): Get the current time with microsecond precision.
 sysinfo(): Retrieve overall system statistics such as memory usage and system uptime.
5. Communication
System calls in this category facilitate communication between processes.Communication facilitates data
sharing, resource sharing, and cooperation among processes, allowing them to work together towards
common goals.
Examples include pipe(), shmget(), msgget(), send(), and recv().
 Pipe (): Creates a unidirectional communication channel between two processes.
 shmget(): Allocates a shared memory segment and returns its identifier.
 msgget(): Creates or gets a message queue identifier.
 Send (): Sends a message through a message queue or other communication channel.
 recv(): Receives a message from a message queue or other communication channel.

Example of System Call In Operating System


Some common examples of system calls include open(), read(), write(), fork(), exec(), exit(), and wait(),
among others. Each system call has a specific purpose and allows processes to perform various tasks that
require privileged access to system resources.

Mr.B V CHANDRA SEKHAR CSE(R-23) Page 20


System Call Type Windows Example Unix Example

Process Control Create Process(), Exit Process() Fork(), exit()

File Management Create File(), Read File() open(), read()

Device Management Set Console Mode(), Read Console() Ioctl(), read()

Information Maintenance Get Current Process ID(), Set Timer() getpid(), alarm()

Communication Create Pipe(), Create File Mapping() Pipe(), shmget()

 System programs
System Programs are the collection of software required for program execution. They are also known as
system utilities. System programs can be simple, involving only system calls as well as complex ones.
System Programming can be defined as the act of building Systems Software using System Programming
Languages. According to Computer Hierarchy, Hardware comes first then is Operating System, System
Programs, and finally Application Programs.

System Programs can be divided into these categories:


 File Management:
 Status Information:
 File Modification:
 Programming Language Support:
 Program Loading and Execution:
 Communications:

Mr.B V CHANDRA SEKHAR CSE(R-23) Page 21


 File Management:
o Allows us to Create, delete, copy, rename, print, dump and generally
manipulate files and directories.
o A file is a collection of specific information stored in the memory of a
computer system. File management is defined as the process of
manipulating files in the computer system i.e. process of creating,
modifying and deleting files.
o It helps to create new files in the computer system and placing them at
specific locations.
o It helps in easily and quickly locating these files in the computer
system.
o It helps to store files in separate folders known as directories.
 Status Information:
o Information like date, time, amount of available memory, disk space
and number of users etc are provided by such system programs.
o Some also provide details like performance, logging and debugging
information which is more complex.
o All this information is formatted and displayed on output devices or
printed.
o Windows use the regedit which can run that command it will show all the
status of system
o Terminal or other devices or files or a window of GUI is used for showing
the output of programs.
 File Modification:
o Several editors like e.g. text editors (i.e Notepad, word pad)in order to
create and modify files.
o For searching contents of files or perform transformations of files we use
special commands.
o Ex:-Linux/Unix use the grep command
o The grep command in Unix/Linux is a powerful tool used for searching and
manipulating text patterns within files.
o The 'grep' command stands for "global regular expression print". grep
command filters the content of a file which makes our search easy.
 Programming Language Support:
o For common programming languages, we use Compilers, Assemblers,
Debuggers and interpreters which are already provided to users.
o It provides all support to users.
o We can run any programming language. All languages of importance
are already provided.
 Program Loading and Execution:
o When the program is ready after Assembling and compilation, it
must be loaded into memory execution.
Mr.B V CHANDRA SEKHAR CSE(R-23) Page 22
o A loader is part of an operating system that is responsible for loading
programs and libraries.
o It is one of the essential stages for starting a program.
o Loaders, linkage editors and overlay loaders are provided by the system.
 Communications:
o Virtual connections among processes, users and computer systems are
provided by the programs.
o Users can send messages to another user on their screen, can send
email, browse on web pages, can transfer the files from one user to
another etc.

 Operating System Design and Implementation


What is operating system design and implementation?
 The operating system is needed to design and implement because without proper
design and implementation any system cannot work properly.
 There are many problems that can occur while designing and implementing an
operating system. These are covered in operating system design and
implementation.
Operating System Design Goals

 It is quite complicated to define all the goals and specifications of the operating system while
designing it.
 The design changes depending on the type of the operating system i.e if it is batch system, time
shared system, single user system, multi user system, distributed system etc.
 There are basically two types of goals while designing an operating system. These are
 User Goals
 System Goals
 User Goals
 Design goals are the objectives of the operating system. They must be met to fulfill design
requirements and they can be used to evaluate the design.
 The operating system should be convenient, easy to use, reliable, safe and fast according to the
users.
 However, these specifications are not very useful as there is no set method to achieve these goals.
 System Goals
 The operating system should be easy to design, implement and maintain.
 These are specifications required by those who create, maintain and operate the operating system.
 It should be flexible, reliable and error free& efficient.

 Operating System Mechanisms and Policies

 Important principle to separate

Mr.B V CHANDRA SEKHAR CSE(R-23) Page 23


 Policy: What will be done?

 Mechanism: How to do it?

 Mechanisms determine how to do something; policies decide what will be done

o The separation of policy from mechanism is a very important principle, it allows maximum
flexibility if policy decisions are to be changed later

o Policy may changes over time and this would be lead to change in mechanism

 Building and Booting an Operating System


 Operating systems generally designed to run on a class of systems with variety of peripherals
 Commonly, operating system already installed on purchased computer
o But can build and install some other operating systems
o If generating an operating system from scratch
 Write the operating system source code
 Configure the operating system for the system on which it will run
 Compile the operating system
 Install the operating system
 Boot the computer and its new operating system

What is Booting?
Booting – starting a computer by loading the kernel (or) The operating system is loaded via
bootstrapping procedure, often known as booting.
When a computer system is started, there is a mechanism in the system that loads the operating
system from the secondary storage into the main memory, or RAM, of the system. This is
called the booting process of the system.
 What happens in the Process of Booting?
Booting happens when you start the computer. This happens when we turned ON the power or
the computer restarts. The system BIOS (Basic Input/Output System) makes the peripheral devices
active. Further, it requires that the boot device loads the operating system into the main memory.
 Boot Devices
Booting can be done either through hardware (pressing the start button) or by
giving software commands. Therefore, a boot device is a device that loads the operating system.
Examples are the hard drive, floppy disk drive, CD drive, etc. Among them, the hard drive is the
most used one.
 Types of Booting
There are two types of booting:
 Cold Booting
A cold boot is also called a hard boot. It is the process when we first start the computer. In other
words, when the computer is started from its initial state by pressing the power button it is called
cold boot. The instructions are read from the ROM and the operating system is loaded in the main
memory.

Mr.B V CHANDRA SEKHAR CSE(R-23) Page 24


 Warm Booting
Warm Boot is also called soft boot. It refers to when we restart the computer. Here, the computer
does not start from the initial state. When the system gets stuck sometimes it is required to restart it
while it is ON. Therefore, in this condition the warm boot takes place. Restart button or
CTRL+ALT+DELETE keys are used for warm boot.
 Steps of Booting
We can describe the boot process in six steps:
1. The Startup
It is the first step that involves switching the power ON. It supplies electricity to the main
components like BIOS and processor.
2. BIOS: Power On Self Test
It is an initial test performed by the BIOS. Further, this test performs an initial check on the
input/output devices, computer’s main memory, disk drives, etc. Moreover, if any error occurs, the
system produces a beep sound.
3. Loading of OS
In this step, the operating system is loaded into the main memory. The operating system starts
working and executes all the initial files and instructions.
4. System Configuration
In this step, the drivers are loaded into the main memory. Drivers are programs that help in the
functioning of the peripheral devices.
5. Loading System Utilities
System utilities are basic functioning programs, for example, volume control, antivirus, etc. In this
step, system utilities are loaded into the memory.
6. User Authentication
If any password has been set up in the computer system, the system checks for user authentication.
Once the user enters the login Id and password correctly the system finally starts.
 Operating system debugging
 Debugging is the activity of finding and fixing errors in a system, both in hardware and in software.
 Debugging can also include performance tuning, which seeks to improve performance by removing
processing bottlenecks (points in a system or process where the flow or speed of movement is
limited or constrained).
Failure Analysis
 If a process fails, operating systems write the error information to a log file to alert system operators
or users that the problem occurred.
 A failure in the kernel is called a crash.
 When a crash occurs, error information is saved to a log file, and the memory state is saved to a
crash dump.
 If in the file-system code kernel failure occurs then the kernel should try to save the state of the file
on the file system before rebooting.
Performance Tuning
Performance tuning seeks to improve performance by removing processing bottlenecks. To identify
bottlenecks, we monitor system performance.

Mr.B V CHANDRA SEKHAR CSE(R-23) Page 25


UNIT-II
Processes: Process Concept, Process scheduling, Operations on processes, Inter-process
communication. Threads and Concurrency: Multithreading models, Thread libraries, threading
issues. CPU Scheduling: Basic concepts, Scheduling criteria, Scheduling algorithms, multiple
processor scheduling.

Processes Concept:
 A process is a program under execution.
 Its current activity is indicated by PC (Program Counter) and the contents of the
processor's registers.

The Process

Process memory is divided into four sections as shown in the figure below:

 The stack is used to store temporary data such as local variables, functionparameters,
function return values, return address etc.
 The heap which is memory that is dynamically allocated during process run time
 The data section stores global variables.
 The text section comprises the compiled program code.
 Note that, there is a free space between the stack and the heap. When the stack is full, it
grows downwards and when the heap is full, it grows upwards.

Process State
A Process has 5 states. Each process may be in one of the following states –
1. New - The process is in the stage of being created.
2. Ready - The process has all the resources it needs to run. It is waiting to be assigned to
the processor.

3. Running – Instructions are being executed.


4. Waiting - The process is waiting for some event to occur. For example, the process may be
waiting for keyboard input, disk access request, inter-process messages, a timer to go off,
or a child process to finish.
5. Terminated - The process has completed its execution.

Diagram of process state

Process Control Block


While creating a process, the operating system performs several operations. To identify the
processes, it assigns a process identification number (PID) to each process. As the operating
system supports multi-programming, it needs to keep track of all the processes. For this task, the
process control block (PCB) is used to track the process’s execution status. Each block of memory
contains information about the process state, program counter, stack pointer, status of opened files,
scheduling algorithms, etc.For each process there is a Process Control Block (PCB), which stores
the process-specific information as shown below –
 Process State – The state of the process may be new, ready, running, waiting, and so on.
 Program counter – The counter indicates the address of the next instruction to be
executed for this process.
 CPU registers - The registers vary in number and type, depending on the computer
architecture. They include accumulators, index registers, stack pointers, and general-
purpose registers. Along with the program counter, this state information must be saved
when an interrupt occurs, to allow the process to be continued correctly afterward.
 CPU scheduling information- This information includes a process priority, pointers to
scheduling queues, and any other scheduling parameters.
 Memory-management information – This includes information such as the value of the
base and limit registers, the page tables, or the segment tables.
 Accounting information – This information includes the amount of CPU and real time
used, time limits, account numbers, job or process numbers, and so on.
 I/O status information – This information includes the list of I/O devices allocated
to the process, a list of open files, and so on.
The PCB simply serves as the repository for any information that may vary from
process toprocess.

Process Control Block (PCB)

Fig: Diagram Showing CPU Switch Process To Process

Threads

What is Thread in Operating Systems?

In a process, a thread refers to a single sequential activity being executed. These activities are also known as
thread of execution or thread control. Now, any operating system process can execute a thread. We can say
that a process can have multiple threads.

 A thread is a single sequence stream within a process.


 Threads are also called lightweight processes as they possess some of the properties of processes.
Each thread belongs to exactly one process.
 For example, when a process is running a word-processor program, a single thread of instructions is
being executed..

Process Scheduling
 The objective of multiprogramming is to have some process running at all times, so as to
maximize CPU utilization. Maintains scheduling queues of processes
There are 3 types of scheduling queues .
They are :
1. Job Queue
2. Ready Queue
3. Device Queue
 As processes enter the system, they are put into a job queue.
 The processes that are residing in main memory and are ready and waiting to
execute are kept on a list called the ready queue.
 The list of processes waiting for an I/O device is kept in a device queue for
that particular device.
 Processes migrate among the various queues

A common representation of process scheduling is a queueing diagram, such as that in Figure .Each
rectangular box represents a queue. Two types of queues are present: the ready queue and a set of
device queues. The circles represent the resources that serve the queues, and the arrows indicate the
flow of processes in the system.
A new process is initially put in the ready queue. It waits there until it is selected for
execution, or dispatched. Once the process is allocated the CPU and is executing, one of
several events could occur:
• The process could issue an I/O request and then be placed in an I/O queue.
• The process could create a new child process and wait for the child’s termination.
• The process could be removed forcibly from the CPU, as a result of an interrupt, and be
put back in the ready queue.

Schedulers
 A process migrates between the various scheduling queues throughout its lifetime.
 The operating system must select, for scheduling purposes, processes from these queues in
some fashion.
 The selection process is carried out by the appropriate scheduler.
 There are three different types of schedulers. They are:
1. Long-term Scheduler or Job Scheduler
2. Short-term Scheduler or CPU Scheduler
3. Medium term Scheduler
 Long-term scheduler(or job scheduler) – selects which processes should be
brought into the ready queue
 Short-term scheduler(or CPU scheduler) – selects which process should be
executed next and allocates CPU
 Sometimes the only scheduler in a system
 Short-term scheduler is invoked very frequently (milliseconds)=> (must be fast)
 Long-term scheduler is invoked very infrequently (seconds, minutes)=>(may be
slow)
 The main difference between these two schedulers is The long-term scheduler
executes much less frequently; minutes may separate the creation of one new process
and the next.
 The long-term scheduler controls the degree of multiprogramming (the number of
processes in memory).
 Processes can be described as either:
 I/O-bound process– spends more time doing I/O than computations, many short
CPU bursts
 CPU-bound process– spends more time doing computations; few very long CPU
bursts
Medium term Scheduler
 Some operating systems, such as time-sharing systems, may introduce an additional,
intermediate level of scheduling. 
 The key idea is medium-term scheduler, removes processes from memoryand thus
reduces the degree of multiprogramming. 
 At some later time, the process can be reintroduced into memory and its execution can be
continued where it left off. This scheme is called swapping 

Context Switch
 Context switch = saving the state of the old process and switching the CPU to another process
 The context of a process is represented in the PCB of a process
 (It includes the value of the CPU registers, process state, and memory-management information)
 Context-switch time is pure overhead, because the system does no useful work while switching
 Context-switch time is highly dependent on hardware support (e.g. some processors
provide multiple sets of registers)
CPU Switch from Process to Process

Operations on Processes
The processes in most systems can execute concurrently, and they may be created and deleted
dynamically
1. Process Creation:

 During the course of execution, a process may create several new processes.
 As mentioned earlier, the creating process is called a parent process, and the new processes
are called the children of that process.
 Each of these new processes may in turn create other processes, forming a tree of processes.
 Most operating systems (including UNIX, Linux, and Windows) identify processes
according to a unique process identifier (or pid), which is typically an integer number.
 The ‘pid’ provides a unique value for each process in the system, and it can be used as an
index to access various attributes of a process within the kernel.

 UNIX example
 fork system call creates new process
 exec system call used after a fork to replace the process’ memory space with a new program
Process Termination

 A process terminates when it finishes executing its last statement and asks the operating
system to delete it, by using the exit () system call. All of the resources assigned to the
process like memory, open files, and I/O buffers, are de-allocated by the operating
system.
 A process can cause the termination of another process by using appropriate system call.
The parent process can terminate its child processes by knowing of the PID of the child.
 A parent may terminate the execution of children for a variety of reasons, such as:
 The child has exceeded its usage of the resources, it has been allocated.
 The task assigned to the child is no longer required.
 The parent is exiting, and the operating system terminates all the children. This
is called cascading termination.

 A process that has terminated, but whose parent has not yet called wait(), is
known as a zombie process. All processes transition to this state when they
terminate, but generally they exist as zombies only briefly.

Inter proces Communication


What is Inter process Communication?
Inter process communication (IPC) is a process that allows different processes of a computer system to share
information. IPC lets different programs run in parallel, share data, and communicate with each other. It’s
important for two reasons: First, it speeds up the execution of tasks, and secondly, it ensures that the tasks
run correctly and in the order that they were executed.

Interposes Communication- Processes executing may be either co-operative or independent


processes.

 Independent Processes – processes that cannot affect other processes or be affected by


other processes executing in the system.
 Cooperating Processes – processes that can affect other processes or be affected by
other processes executing in the system.
Co-operation among processes are allowed for following reasons –
 Information Sharing - There may be several processes which need to access the same
File. So the information must be accessible at the same time to all users.
 Computation speedup - Often a solution to a problem can be solved faster if the problem
can be broken down into sub-tasks, which are solved simultaneously (particularly when
multiple processors are involved.)
 Modularity - A system can be divided into cooperating modules and executed by sending
information among one another.
 Convenience - Even a single user can work on multiple tasks by information sharing.
Cooperating processes require an inter process communication (IPC) mechanism that will allow
them to exchange data and information. There are two fundamental models of inter process
communication: Cooperating processes require some type of inter-process communication.

This is allowed bytwo models:

 There are two fundamental models of intercrosses communication:


1. Shared Memory systems:- In the shared-memory model, a region of memory that is shared by
cooperating processes is established. Processes can then exchange information by reading and writing
data to the shared region.
2. Message passing systems:-In the message-passing model, communication takes place by means of
messages exchanged between the cooperating processes.

1) Shared-Memory Systems
 A region of shared-memory is created within the address space of a process, which needs
to communicate. Other process that needs to communicate uses this shared memory.
Ex: Producer-Consumer problem
There are two processes: Producer and Consumer .
The producer produces some items and the Consumer consumes that item. The two processes
share a common space or memory location known as a buffer where the item produced by the
Producer is stored and from which the Consumer consumes the item if needed. There are two
versions of this problem: the first one is known as the unbounded buffer problem in which the
Producer can keep on producing items and there is no limit on the size of the buffer, the second one
is known as the bounded buffer problem in which the Producer can produce up to a certain number
of items before it starts waiting for Consumer to consume it.
Message Passing
When two or more processes participate in inter-process communication, each process sends messages to the
others via Kernel. Here is an example of sending messages between two processes: – Here, the process sends a
message like “M” to the OS kernel. This message is then read by Process B. A communication link is required
between the two processes for successful message exchange. There are several ways to create these links.

Communication in client–server systems:


There are two levels of communication
 Low–level form of communication–eg. Socket
 High–level form of communication –eg. RPC,RMI
1. Sockets

 A socket is defined as an end point for communication.


 A socket is made up of an IP address concatenated with a port number.
 It uses a client-server architecture.
 All port below 1024 are well known ports(eg. 23 is for telnet,21 for ftp ,80
for http etc)
 If a client makes a request for a connection, it is assigned a port number by
the host computer (>1024)

 The above diagram shows that a client on host X with IP address


146.86.5.2/1625 wants to communicate with a web server at
161.25.19.8/80
 All connections established are unique.
Multithreaded Programming
Objectives:
 To introduce the notion of a thread - a fundamental unit of CPU utilization that forms the basis
ofmultithreaded computer systems.
 To discuss the APIs for the Pthreads, Win32, and Java thread libraries.
 To examine issues related to multithreaded programming.
Overview

 A thread is the basic unit of CPU utilization.


 It is sometimes called as a lightweight process.
 It consists of a thread ID ,a program counter, a register set and a stack.
 It shares with other threads belonging to the same process its code section
data section, and resources such as open files and signals.

Multithreaded Server Architecture:

 A traditional or heavy weight process has a single thread of control.

 If the process has multiple threads of control,it can do more than one task at a time
Benefits of multithreaded programming

 Responsiveness
 Resource Sharing
 Economy
 Scalability

TYPES OF THREADS:

 User Level Threads − User managed threads. User threads are supported above the kernel
and are managed without kernel support i.e., they are implemented by thread library at the
user level.
 Kernel Level Threads − Operating System managed threads acting on kernel, an operating
system core.

User thread and Kernel threads

User Threads:

 Thread management done by user-level threads library


 Thread creation, management and scheduling are done in user space.

 Fast to create and manage

 When a user thread performs a blocking system call, it will cause the entire process to block
even if other threads are available to run within the application.

 Example: POSIX Pthreads, Mach C-threads and Solaris 2 UI-threads.

Kernel Threads:

 Supported directly by the OS.

 Thread creation , management and scheduling are done in kernel space

 Slow to create and manage

 When a kernel thread performs a blocking system call ,the kernel schedules another thread in the
application for execution.

 Examples
o Windows XP/2000
o Solaris
o Linux
o Tru64 UNIX
o Mac OS X
Multithreading Models:

 Many-to-One
 One-to-One
 Many-to-Many

1. Many-to-One

 Many user-level threads mapped to single kernel thread


 Thread management is done in user space, so it is efficient
 The entire process will block if a thread makes a blocking call
 Multiple threads can’t run in parallel on multiprocessors
 Examples:
⏵ Solaris Green Threads
⏵ GNU Portable Threads
Advantages:
 Thread management is done by the thread library in user space, so it is efficient.

Disadvantages:
 The entire process will block if a thread makes a blocking system-call.
 Multiple threads are unable to run in parallel on multiprocessors.

2. One-to-One:

 Each user-level thread maps to kernel thread


 More concurrency than the many-to-one model because another thread can run when a
thread makes a blocking system call
 Multiple threads can run in parallel on multiprocessors
 Disadvantage: creating a user thread requires creating the corresponding kernel thread
Advantages:
 It provides more concurrency by allowing another thread to run when a thread makes a
blocking system-call.
 Multiple threads can run in parallel on multiprocessors
Disadvantages:
 Creating a user thread requires creating the corresponding kernel thread
Examples
o Windows NT/XP/2000
o Linux
o Solaris 9 and later

3. Many-to-Many Model:

 Many user-level threads are multiplexed to a smaller / equal number of kernel threads

 Developers can create as many user threads as necessary

 The kernel threads can run in parallel on a multiprocessor

 When a thread performs a blocking system call, the kernel can schedule another thread for
execution

Advantages:

 Developers can create as many user threads as necessary


 The kernel threads can run in parallel on a multiprocessor.
 When a thread performs a blocking system-call, kernel can schedule another thread for
execution.
Thread Libraries:
 Thread library provides programmer with API for creating and managing threads
 Three main thread libraries in use today:
o POSIX Pthreads
o Win32 threads
o Java threads

PThreads

 May be provided either as user-level or kernel-level


 A POSIX standard (IEEE 1003.1c) API for thread creation and synchronization

 API specifies behavior of the thread library, implementation is up to


development of the library
 Common in UNIX operating systems (Solaris, Linux, Mac OS X)

Java Threads:

 Java threads are managed by the JVM


 Typically implemented using the threads model provided by underlying OS
 Java threads may be created by:
o Extending Thread class
o Implementing the Runnable interface

Windows XP Threads
 Implements the one-to-one mapping
 Each thread contains
o A thread id
o Register set
o Separate user and kernel stacks
o Private data storage area
Threading Issues:
 Semantics of fork() and exec() system calls
 Thread cancellation
 Signal handling
 Thread pools
 Thread specific data

1. fork() and exec() system calls.


A fork() system call may duplicate all threads or duplicate only the thread that invoked fork().

If a thread invoke exec() system call ,the program specified in the parameter to exec will replace
the entire process.

2. Thread Cancellation:

 Thread cancellation is the task of terminating a thread before it has completed.

 A thread that is to be cancelled is called a target thread. There are two types of cancellation
namely

1. Asynchronous Cancellation – One thread immediately terminates the target thread.

2. Deferred Cancellation – The target thread can periodically check if it should terminate, and
does so in an orderly fashion.

3. Signal handling

1. A signal is a used to notify a process that a particular event has occurred.


2. A generated signal is delivered to the process.
a. Deliver the signal to the thread to which the signal applies.
b. Deliver the signal to every thread in the process.
c. Deliver the signal to certain threads in the process.
d. Assign a specific thread to receive all signals for the process.
3. Once delivered the signal must be handled.
a. Signal is handled by
i. A default signal handler
ii. A user defined signal handler
Thread Pools:
 The idea is to create a number of threads at process startup and place them into a pool,
where they sit and wait for work
 When a server receives a request, it awakens a thread from this pool
 If one is available the request is passed to it for service
 Once the service is completed, the thread returns to the pool and wait for more work
 Benefits of thread pools:
 It is faster to service a request with an existing thread
 A thread pool limits the number of threads that exist
 Potential problems with a multithreaded server:
 It takes time to create a thread before servicing a request
 Unlimited threads could exhaust system resources (CPU time)
 Thread pools are a solution to these problems:
 At process startup, several threads are created and placed into a pool, where
they sit and wait for work
 When a server receives a request, it awakens a thread from this pool, passing
it the request to service
 When the thread finishes its service it returns to the pool
5. Thread specific data
Threads belonging to a process share the data of the process. However each thread might need its
own copy of certain data known as thread-specific data

CPU Scheduling:
Basic Concepts

 CPU scheduling is the task of selecting a waiting process from the ready queue and
allocating the CPU to it
o The CPU is allocated to the selected process by the dispatcher
 In a uni-processor system, only one process may run at a time; any other process must wait
until the CPU is rescheduled
 The objective of multiprogramming is to have some process running at all times, in order to
maximize CPU utilization
 CPU–I/O Burst Cycle – Process execution consists of a cycle of CPU execution and I/O
wait
 CPU burst distribution

2.3.6 Scheduling Criteria:

In choosing which algorithm to use in a particular situation, we must consider the properties of the various
algorithms. Many criteria have been suggested for comparing CPU scheduling algorithms.
Which characteristics are used for comparison can make a substantial difference in which algorithm is
judged to be best. The criteria include the following:

• CPU utilization. We want to keep the CPU as busy as possible. Conceptually, CPU utilization can range
from 0 to 100 percent. In a real system, it should range from 40 percent (for a lightly loaded system) to 90
percent (for a heavily used system).
• Throughput. The number of processes completed per time unit
• Turnaround time.
 The time interval from process submission to completion
 Formula: Time of completion – Time of submission
 Formula: CPU burst time + Waiting time (includes I/O)
• Waiting time.
 The sum of the periods spent waiting in the ready queue
 Formula: Turnaround time – CPU burst time
• Response time. The amount of time it takes to start responding, but not the time it takes to output that
response
We want to maximize CPU utilization, and minimize turnaround, waiting & response time

Scheduling Algorithms

Preemptive Scheduling:

 Preemptive scheduling is used when a process switches from running state to ready state or
from waiting state to ready state.
 There sources (mainly CPU cycles)are allocated to the process for the limited amount
oftimeandthenistakenaway,andtheprocessisagainplacedbackinthereadyqueue if that process
still has CPU burst time remaining.
 That process stays in ready queue till it gets next chance to execute.

Non-Preemptive Scheduling:

 Non-preemptive Scheduling is used when a process terminates, or a process switches from


running to waiting state.
 In this scheduling, once there sources (CPU cycles)is allocated to a process , the process
holds the CPU till it gets terminated or it reaches a waiting state.
 Incaseofnon-preemptiveschedulingdoesnotinterruptaprocessrunningCPUin middle of the
execution.
 Instead, it waits till the process complete its CPU burst time and then it can allocate the
CPU to another process.

Basis for
Preemptive Scheduling Non Preemptive Scheduling
Comparison
Once resources are allocated to a
There sources are allocated to a process, the process holds it till it
Basic
process for a limited time. completes its burst time or switches to
waiting state.
Process can be interrupted in Process can not be interrupted till it
Interrupt
between. terminates or switches to waiting state.
If a high priority process
If a process with long burst time is
frequently arrives in the ready
Starvation running CPU ,then an other process with
queue ,low priority process may
less CPU burst time may starve.
starve.
Preemptive scheduling has
Non-preemptive scheduling does not
Overhead overhead so f scheduling the
have overheads.
processes.
Flexibility Preemptive scheduling is flexible. Non-preemptive scheduling is rigid.
Preemptive scheduling is cost Non-preemptive scheduling is not cost
Cost
associated. associative.
OperatingSystemHandout

Unit II–CPUSchedulingandAlgorithmSection
Scheduling types
SchedulingObjectives
 BeFairwhileallocatingresourcestothe processes
 Maximizethroughputof the system
 Maximizenumberofusers receivingacceptableresponse times.
 Bepredictable
 Balanceresourceuse
 Avoidindefinitepostponement
 EnforcePriorities
 Givepreferenceto processes holdingkeyresources
 Givebetterservicetoprocesses thathavedesirablebehaviour patterns

CPUandI/OBurst Cycle:
 Processexecutionconsistsofacycleof CPUexecutionandI/Owait.
 Processesalternatebetweenthesetwostates.
 ProcessexecutionbeginswithaCPUburst,followedbyanI/Oburst,thenanotherCPU burst
... etc
 The last CPU burst will end with a system request to terminate execution rather than
with another I/O burst.
 Theduration oftheseCPU bursthavebeen measured.
 An I/O-bound program would typically have many short CPU bursts, A CPU-bound
program might have a few very long CPU bursts.
 Thiscanhelptoselect anappropriateCPU-schedulingalgorithm.

Page1of 16
OperatingSystemHandout

PreemptiveScheduling:
 Preemptive scheduling is used when a process switches from running state to ready
state or from waiting state to ready state.
 Theresources (mainlyCPUcycles)areallocated to theprocess forthelimitedamount
oftimeandthenistakenaway,andtheprocessisagainplacedbackinthereadyqueue if that
process still has CPU burst time remaining.
 Thatprocess staysinreadyqueuetillit getsnext chanceto execute.

Non-PreemptiveScheduling:
 Non-preemptive Scheduling is used when a process terminates, or a process switches
from running to waiting state.
 Inthisscheduling,oncetheresources(CPUcycles)isallocatedtoaprocess,theprocess holds
the CPU till it gets terminated or it reaches a waiting state.
 Incaseofnon-preemptiveschedulingdoesnotinterruptaprocessrunningCPUin middle of
the execution.
 Instead,itwaitstilltheprocesscompleteitsCPUbursttimeandthenitcanallocatethe CPU to
another process.

Basis for
PreemptiveScheduling NonPreemptiveScheduling
Comparison
Once resources are allocated to a
Theresourcesareallocatedtoa process, the process holds it till it
Basic
process for a limited time. completesitsbursttimeorswitchesto
waiting state.
Processcanbeinterruptedin Process can not be interrupted till it
Interrupt
between. terminatesorswitchestowaitingstate.
If a high priority process
If a process with long burst time is
frequently arrives in the ready
Starvation runningCPU,thenanotherprocesswith
queue,lowpriorityprocessmay
less CPU burst time may starve.
starve.
Preemptive scheduling has
Non-preemptiveschedulingdoesnot have
Overhead overheadsofschedulingthe
overheads.
processes.
Flexibility Preemptiveschedulingisflexible. Non-preemptiveschedulingis rigid.
Preemptiveschedulingiscost Non-preemptiveschedulingisnotcost
Cost
associated. associative.

SchedulingCriteria

 There are several different criteria to consider when trying to select the "best"
scheduling algorithm for a particular situation and environment, including: 
o CPU utilization - Ideally the CPU would be busy 100% of the time, so
astowaste0CPUcycles.OnarealsystemCPUusageshouldrangefrom 40% (
lightly loaded ) to 90% ( heavily loaded. )
o Throughput- Number of processescompletedperunittime. Mayrange
from 10 / second to 1 / hour depending on the specific processes.

Page2of 16
OperatingSystemHandout

o Turnaround time - Time required for a particular process to complete,


from submission time to completion.
o Waitingtime-Howmuchtimeprocessesspendinthereadyqueue waiting
their turn to get on the CPU.
o Responsetime-Thetimetakeninaninteractiveprogramfromthe
issuanceofacommandtothecommenceofaresponsetothatcommand.

Inbrief:
ArrivalTime:Timeatwhichtheprocessarrivesinthereadyqueue.
CompletionTime:Timeatwhichprocesscompletesitsexecution.
BurstTime:TimerequiredbyaprocessforCPUexecution.Turn Around Time: Time
Difference between completion time and arrival time.Turn Around Time =
Completion Time – Arrival Time
Waiting Time(W.T): Time Difference between turnaround time and burst time.
Waiting Time = Turn Around Time – Burst Time

4.2TypesofSchedulingAlgorithm

(a) FirstComeFirstServe(FCFS)
InFCFSScheduling
 Theprocesswhicharrivesfirstinthereadyqueueisfirstlyassignedthe CPU.
 Incaseofatie,processwithsmallerprocessidisexecutedfirst.
 Itisalwaysnon-preemptiveinnature.
 Jobsareexecutedon firstcome, firstservebasis.
 Itisanon-preemptive,pre-emptiveschedulingalgorithm.
 Easyto understand and implement.
 ItsimplementationisbasedonFIFOqueue.
 Poorinperformanceasaverage waittimeishigh.

Advantages-
 Itissimpleandeasyto understand.
 Itcanbeeasilyimplementedusingqueuedatastructure.
 Itdoesnotleadtostarvation.
Disadvantages-
 Itdoesnotconsiderthepriorityorbursttimeoftheprocesses.
 Itsuffersfromconvoyeffecti.e.processeswithhigherbursttimearrivedbefore the
processes with smaller burst time.

Page3of 16
OperatingSystemHandout

Example1:

Example2:
Consider the processes P1, P2, P3 given in the below table, arrives for execution in
the same order, with Arrival Time 0, and given Burst Time,
PROCESS ARRIVALTIME BURSTTIME
P1 0 24
P2 0 3
P3 0 3
Ganttchart

P1 P2 P3
0 24 27 30

Page4of 16
OperatingSystemHandout

PROCESS WAITTIME TURNAROUNDTIME


P1 0 24
P2 24 27
P3 27 30

TotalWait Time =0 +24 +27 =51 ms

AverageWaitingTime=(TotalWaitTime)/(Totalnumberofprocesses) =51/3=17ms Total Turn

Around Time: 24 + 27 + 30 = 81 ms

AverageTurnAroundtime =(TotalTurnAroundTime)/ (Totalnumberof processes)


=81 / 3 = 27 ms
Throughput=3 jobs/30sec=0.1 jobs/sec
Example3:
Considertheprocesses P1,P2,P3,P4given inthebelowtable,arrives forexecution in the
same order, with given Arrival Time and Burst Time.
PROCESS ARRIVALTIME BURSTTIME
P1 0 8
P2 1 4
P3 2 9
P4 3 5

Ganttchart
P1 P2 P3 P4
0 8 12 21 26

PROCESS WAITTIME TURNAROUNDTIME


P1 0 8 – 0 =8
P2 8 – 1 =7 12 – 1 =11
P3 12 – 2 =10 21 – 2 =19
P4 21 – 3 =18 26 – 3 =23

TotalWait Time:=0 +7 +10 +18 =35 ms

AverageWaitingTime=(TotalWaitTime)/(Totalnumberofprocesses)=35/4=8.75ms Total Turn

Around Time: 8 + 11 + 19 + 23 = 61 ms

AverageTurnAroundtime=(TotalTurnAroundTime)/(Totalnumberofprocesses) 61/4 =
15.25 ms

Throughput:4 jobs/26sec=0.15385 jobs/sec

Page5of 16
OperatingSystemHandout

(b) ShortestJobFirst(SJF)
 Processwhichhavetheshortestbursttimearescheduledfirst.
 Iftwoprocesseshavethesamebusttime,thenFCFSisusedtobreakthetie.
 Thisisanon-pre-emptive,pre-emptiveschedulingalgorithm.
 Bestapproachtominimizewaiting time.
 Easyto implementin Batchsystems where required CPUtimeis known inadvance.
 ImpossibletoimplementininteractivesystemswhererequiredCPUtimeisnot
known.
 Theprocessershouldknow inadvancehowmuchtimeprocess will take.
 Pre-emptive mode of Shortest Job First is called as Shortest Remaining
TimeFirst (SRTF).

Advantages-
 SRTFisoptimalandguaranteestheminimumaveragewaitingtime.
 Itprovidesastandardforotheralgorithmssincenootheralgorithmperforms better
than it.

Disadvantages-
 Itcannotbeimplementedpracticallysincebursttimeoftheprocessescannot be
known in advance. 
 Itleadstostarvationforprocesseswithlargerbursttime.
 Prioritiescannotbesetfortheprocesses.
 Processeswithlargerbursttimehavepoorresponsetime.

Example-01:
Considerthesetof5processeswhosearrivaltimeandbursttimearegivenbelow-
ProcessId Arrivaltime Bursttime
P1 3 1
P2 1 4
P3 4 2
P4 0 6
P5 2 3
Solution-
IftheCPUschedulingpolicyisSJFnon-preemptive,calculatetheaveragewaiting time
and average turnaround time.
GanttChart-

Now,weknow-
 TurnAroundtime=Exittime–Arrivaltime
 Waitingtime=TurnAroundtime–Bursttime

Page6of 16
OperatingSystemHandout

ProcessId Exit time TurnAroundtime Waiting time


P1 7 7 – 3 =4 4 – 1 =3
P2 16 16 – 1 =15 15 – 4 =11
P3 9 9 – 4 =5 5 – 2 =3
P4 6 6 – 0 =6 6 – 6 =0
P5 12 12 – 2 =10 10 – 3 =7
Now,
 AverageTurnAroundtime=(4+15+5+6+ 10)/5=40/5=8unit
 Averagewaitingtime=(3+11+3+0+7)/5=24/5=4.8unit

Example-02:
Considerthesetof5processeswhosearrivaltimeandbursttimearegivenbelow-
ProcessId Arrivaltime Bursttime
P1 3 1
P2 1 4
P3 4 2
P4 0 6
P5 2 3
IftheCPUschedulingpolicyisSJFpre-emptive,calculatetheaveragewaitingtimeand
average turnaround time.
Solution-
GanttChart-

ProcessId Exit time TurnAroundtime Waiting time


P1 4 4 – 3 =1 1 – 1 =0
P2 6 6 – 1 =5 5 – 4 =1
P3 8 8 – 4 =4 4 – 2 =2
P4 16 16 – 0 =16 16 – 6 =10
P5 11 11 – 2 =9 9 – 3 =6

Now,

 AverageTurnAroundtime=(1+5+4+16 +9)/5=35 /5=7unit


 Averagewaitingtime=(0+1+2+10+6)/5=19/5=3.8unit

Page7of 16
OperatingSystemHandout

Example-03:

Considerthesetof6processeswhosearrivaltimeandbursttimearegivenbelow-

ProcessId Arrivaltime Bursttime


P1 0 7
P2 1 5
P3 2 3
P4 3 1
P5 4 2
P6 5 1

IftheCPUschedulingpolicyisshortestremainingtimefirst, calculatetheaverage waiting


time and average turnaround time.
Solution-
GanttChart-

Now,weknow-
 TurnAroundtime=Exittime–Arrivaltime
 Waitingtime=TurnAroundtime–Bursttime

ProcessId Exit time TurnAroundtime Waitingtime


P1 19 19–0=19 19–7=12
P2 13 13–1=12 12–5=7
P3 6 6–2=4 4–3=1
P4 4 4–3=1 1–1=0
P5 9 9–4=5 5–2=3
P6 7 7–5=2 2–1=1

Now,
 AverageTurnAroundtime=(19+12+4+1+5+2)/6=43/6=7.17unit
 Averagewaitingtime=(12+7+1+0+3+ 1)/6=24/6=4unit

Page8of 16
OperatingSystemHandout

Example-04:

Considerthesetof3processeswhosearrivaltimeandbursttimearegivenbelow-

ProcessId Arrivaltime Bursttime


P1 0 9
P2 1 4
P3 2 9

IftheCPUschedulingpolicyisSRTF,calculatetheaveragewaitingtimeandaverage turn
around time.

Solution-
GanttChart-

Now,weknow-
 TurnAroundtime=Exittime–Arrivaltime
 Waitingtime=TurnAroundtime–Bursttime

ProcessId Exit time TurnAroundtime Waitingtime


P1 13 13–0=13 13–9=4
P2 5 5–1=4 4–4=0
P3 22 22-2=20 20–9=11

Now,
 AverageTurnAroundtime=(13+4+20)/3=37/3 =12.33 unit
 Averagewaitingtime=(4+0+11)/3=15/3=5unit

Example-05:

Considerthesetof4processeswhosearrivaltimeandbursttimearegivenbelow-

ProcessId Arrivaltime Bursttime


P1 0 20
P2 15 25
P3 30 10
P4 45 15

Page9of 16
OperatingSystemHandout

IftheCPUschedulingpolicyisSRTF,calculatethewaitingtimeofprocessP2.

Solution-

GanttChart-

Now,weknow-
 TurnAroundtime=Exittime–Arrivaltime
 Waitingtime=TurnAroundtime–Bursttime

Thus,
 TurnAroundTimeofprocessP2=55–15=40 unit
 WaitingtimeofprocessP2=40–25=15 unit

(c) RoundRobinScheduling
 CPUisassignedtotheprocessonthebasisofFCFSforafixedamountoftime.
 Thisfixedamountoftimeiscalledastimequantumortimeslice.
 Afterthetimequantumexpires,therunningprocessispreemptedandsenttothe ready
queue.
 Then,theprocessorisassignedtothenextarrivedprocess.
 Itisalwayspreemptiveinnature.

Page10of 16
OperatingSystemHandout

Advantages-

 Itgivesthebestperformanceintermsofaverageresponsetime.
 Itisbestsuitedfortimesharingsystem,clientserverarchitectureand
interactive system.

Disadvantages-

 Itleadstostarvationforprocesseswithlargerbursttimeastheyhavetorepeat the
cycle many times. 
 Itsperformanceheavilydependsontimequantum.
 Prioritiescannotbesetfortheprocesses.

Withdecreasingvalueoftimequantum,
 Numberofcontextswitchincreases
 Responsetimedecreases
 Chancesofstarvationdecreases

Thus,smallervalueoftimequantumisbetterintermsofresponsetime.

Withincreasingvalueoftimequantum,
 Numberofcontextswitchdecreases
 Responsetimeincreases
 Chancesofstarvationincreases

Thus,highervalueoftimequantumisbetterintermsofnumberofcontextswitch.

 Withincreasingvalueoftimequantum,RoundRobinSchedulingtendsto
become FCFS Scheduling.
 Whentimequantumtendstoinfinity,RoundRobinSchedulingbecomesFCFS
Scheduling.
 TheperformanceofRoundRobinschedulingheavilydependsonthevalueof time
quantum.
 Thevalueoftimequantumshouldbesuchthatitisneithertoobignortoo small.

Example-01:
Considerthesetof5processeswhosearrivaltimeandbursttimearegivenbelow-

ProcessId Arrivaltime Bursttime


P1 0 5
P2 1 3
P3 2 1
P4 3 2
P5 4 3

Page11of 16
OperatingSystemHandout

IftheCPUschedulingpolicyisRoundRobinwithtimequantum=2unit,calculate the
average waiting time and average turnaround time.
Solution-
ReadyQueue- P5,P1,P2,P5,P4,P1,P3,P2,P1
GanttChart-

Now,weknow-
 TurnAroundtime=Exittime –Arrival time
 Waitingtime =Turn Around time – Burst time
ProcessId Exit time TurnAroundtime Waiting time
P1 13 13 – 0 =13 13 – 5 =8
P2 12 12 – 1 =11 11 – 3 =8
P3 5 5 – 2 =3 3 – 1 =2
P4 9 9 – 3 =6 6 – 2 =4
P5 14 14 – 4 =10 10 – 3 =7
Now,
 AverageTurn Around time =(13 +11+3 +6+10) / 5 =43 / 5 = 8.6 unit
 Averagewaitingtime =(8 +8 +2 +4+7) / 5 =29 / 5 = 5.8unit
Problem-02:
Considerthesetof6processeswhosearrivaltimeandbursttimearegivenbelow-
ProcessId Arrivaltime Bursttime
P1 0 4

P2 1 5

P3 2 2

P4 3 1

P5 4 6

P6 6 3
IftheCPUschedulingpolicyisRoundRobinwithtimequantum=2, calculatetheaverage waiting
time and average turnaround time.
Solution-
Ready Queue- P5,P6,P2,P5, P6,P2,P5,P4, P1,P3,P2, P1
Ganttchart-

Now,weknow-
 TurnAroundtime=Exittime –Arrival time
 Waitingtime =Turn Around time – Burst time

Page12of 16
OperatingSystemHandout

ProcessId Exit time TurnAroundtime Waiting time


P1 8 8 – 0 =8 8 – 4 =4
P2 18 18 – 1 =17 17 – 5 =12
P3 6 6 – 2 =4 4 – 2 =2
P4 9 9 – 3 =6 6 – 1 =5
P5 21 21 – 4 =17 17 – 6 =11
P6 19 19 – 6 =13 13 – 3 =10
Now,
 AverageTurn Aroundtime =(8 +17+4 +6+17 +13) / 6 =65 / 6 = 10.84unit
 Average waiting time = (4 + 12 + 2 + 5 + 11 + 10) / 6 = 44 / 6 = 7.33 unit
Problem-03:Considerthesetof6processeswhosearrivaltimeandbursttimeare given
below-
ProcessId Arrivaltime Bursttime
P1 5 5
P2 4 6
P3 3 7
P4 1 9
P5 2 2
P6 6 3
IftheCPUschedulingpolicyisRoundRobinwithtimequantum=3,calculatethe average
waiting time and average turnaround time.
Solution-
ReadyQueue- P3,P1,P4,P2,P3,P6,P1,P4,P2,P3,P5,P4
Ganttchart-

Now,weknow-
 TurnAroundtime=Exittime –Arrival time
 Waitingtime =Turn Around time – Burst time
ProcessId Exit time TurnAroundtime Waiting time
P1 32 32 – 5 =27 27 – 5 =22
P2 27 27 – 4 =23 23 – 6 =17
P3 33 33 – 3 =30 30 – 7 =23
P4 30 30 – 1 =29 29 – 9 =20
P5 6 6 – 2 =4 4 – 2 =2
P6 21 21 – 6 =15 15 – 3 =12

Page13of 16
OperatingSystemHandout

Now,

 AverageTurn Aroundtime =(27 +23+30 +29+4 +15) / 6 =128 / 6=21.33 unit


 Averagewaitingtime =(22 +17 +23 +20+2 +12) / 6 =96 / 6 = 16 unit

(d) PriorityScheduling
 Outofalltheavailableprocesses,CPUisassignedtotheprocesshavingthe highest
priority.
 Incaseofatie,itis brokenbyFCFSScheduling.
 PrioritySchedulingcanbeusedinbothpreemptiveandnon-preemptivemode.

 Thewaitingtimefortheprocesshavingthehighestprioritywillalwaysbezeroin
preemptive mode.
 Thewaitingtimefortheprocesshavingthehighestprioritymaynotbezeroinnon-
preemptive mode.
Priorityschedulinginpreemptiveandnon-preemptivemodebehavesexactlysameunder following
conditions-
 Thearrivaltimeofalltheprocessesissame
 Alltheprocessesbecome available
Advantages-
 Itconsidersthepriorityoftheprocessesandallowstheimportantprocessesto run
first.
 Priorityschedulinginpre-emptivemodeisbestsuitedforrealtimeoperating
system.
Disadvantages-
 ProcesseswithlesserprioritymaystarveforCPU.
 Thereisnoideaofresponsetimeandwaitingtime.

Problem-01:
Considerthesetof5processeswhosearrivaltimeandbursttimearegivenbelow-
ProcessId Arrivaltime Bursttime Priority

P1 0 4 2

P2 1 3 3

P3 2 1 4

P4 3 5 5

P5 4 2 5

If the CPU scheduling policy is priority non-preemptive, calculate the average waiting time
and average turnaround time. (Higher number represents higher priority)

Page14of 16
OperatingSystemHandout

Solution-
GanttChart-

Now,weknow-
 TurnAroundtime=Exittime –Arrival time
 Waitingtime =Turn Around time – Burst time
ProcessId Exit time TurnAroundtime Waiting time
P1 4 4 – 0 =4 4 – 4 =0
P2 15 15 – 1 =14 14 – 3 =11
P3 12 12 – 2 =10 10 – 1 =9
P4 9 9 – 3 =6 6 – 5 =1
P5 11 11 – 4 =7 7 – 2 =5
Now,
 AverageTurn Aroundtime =(4 +14+10 +6+7) / 5 =41 / 5 = 8.2 unit
 Averagewaitingtime =(0 +11 +9 +1+5) / 5 = 26 / 5 = 5.2 unit

Problem-02:Considerthesetof5processeswhosearrivaltimeandbursttimeare given
below-
ProcessId Arrivaltime Bursttime Priority
P1 0 4 2
P2 1 3 3
P3 2 1 4
P4 3 5 5
P5 4 2 5
IftheCPUschedulingpolicyisprioritypreemptive,calculatetheaveragewaiting time
and average turn around time. (Higher number represents higher priority).
Solution-
GanttChart-

Now,weknow-
 TurnAroundtime=Exittime –Arrival time
 Waitingtime =Turn Around time – Burst time
ProcessId Exit time TurnAroundtime Waiting time
P1 15 15 – 0 =15 15 – 4 =11
P2 12 12 – 1 =11 11 – 3 =8
P3 3 3 – 2 =1 1 – 1 =0
P4 8 8 – 3 =5 5 – 5 =0
P5 10 10 – 4 =6 6 – 2 =4

Page15of 16
OperatingSystemHandout

Now,
 AverageTurn Aroundtime =(15 +11+1 +5+6) / 5 =38 / 5 = 7.6 unit
 Averagewaitingtime =(11 +8 +0 +0+4) / 5 = 23 / 5 = 4.6 unit

(d) MultilevelQueueScheduling
A multi-level queue scheduling algorithm partitions the ready queue into several separate
queues. The processes are permanently assigned to one queue, generally based on some
propertyoftheprocess,suchasmemorysize,processpriority,orprocesstype.Eachqueuehas its own
scheduling algorithm.
Letus consideranexampleofamultilevelqueue-schedulingalgorithmwithfivequeues:
1. System Processes
2. InteractiveProcesses
3. InteractiveEditingProcesses
4. BatchProcesses
5. Student Processes
Each queue has absolute priority over lower-priority queues. No process in the batch queue,
for example, could run unless the queues for system processes, interactive processes, and
interactiveeditingprocesseswereallempty.Ifaninteractiveeditingprocessenteredtheready queue
while a batch process was running, the batch process will be pre-empted.

Page16of 16
UNIT – III

Synchronization Tools: The Critical Section Problem, Peterson’s Solution, Mutex Locks, Semaphores,
Monitors, Classic problems of Synchronization. Deadlocks: system Model, Deadlock characterization,
Methods for handling Deadlocks, Deadlock prevention, Deadlock avoidance, Deadlock detection,
Recovery from Deadlock.

 Introduction:
On the basis of synchronization, processes are categorized as one of the following two types:
• Independent Process: The execution of one process does not affect the execution of other processes.
• Cooperative Process: A process that can affect or be affected by other processes executing in the
system. Process synchronization problem arises in the case of Cooperative process also because
resources are shared in Cooperative processes.

 Race Condition

A race condition is an undesirable situation that occurs when a device or system attempts to perform
two or more operations at the same time

 Critical Section

The regions of a program that try to access shared resources and may cause race conditions are called
critical section. To avoid race condition among the processes, we need to assure that only one process
at a time can execute within the critical section.

A Critical Section Environment contains:


• Entry Section Code requesting entry into the critical section.
• Critical Section Code in which only one process can execute at any one time.
• Exit Section The end of the critical section, releasing or allowing others in.
• Remainder Section Rest of the code AFTER the critical section.
 The Critical Section Problem
 Consider a system consisting of n processes {Po, P1 , ... ,Pn-1}.
 Each process has a segment of code, called a critical section in which the process may be
Changing common variables, updating a table, writing a file, and soon
 The important feature of the system is that, when one process is executing in its critical
section, no other process is to be allowed to execute in its critical section. That is, no
two processes are executing in their critical sections at the same time.
 The critical-section problem is to design a protocol that the processes can use to
cooperate.
The general structure of a typical process Pi is shown in below figure.

 Each process must request permission to enter its critical section. The section of code
implementing this request is the entry section.

CSE-II-II(R-23) Page 1
 The critical section may be followed by an exit section. The remaining code is the reminder
section.

A solution to the critical-section problem must satisfy the following three requirements:

1. Mutual exclusion: If process Pi is executing in its critical section, then no other processes can be
executing in their critical sections.

2. Progress: If no process is executing in its critical section and some processes wish to enter their
critical sections, then only those processes that are not executing in their remainder sections can
participate in deciding which will enter its critical section next, and this selection cannot be postponed
indefinitely.

3. Bounded waiting: There exists a bound, or limit, on the number of times that other processes are
allowed to enter their critical sections after a process has made a request to enter its critical section and
before that request is granted.

 Peterson's Solution

 This is a classic software-based solution to the critical-section problem. There are no


guarantees that Peterson's solution will work correctly on modern computer
architectures
 Peterson's solution provides a good algorithmic description of solving the critical- section
problem and illustrates some of the complexities involved in designing software that addresses
the requirements of mutual exclusion, progress, and bounded waiting.

Peterson's solution is restricted to two processes that alternate execution between their critical sections
and remainder sections. The processes are numbered Po and P1 or Pi and Pj where j = 1-i Peterson's
solution requires the two processes to share two data items:

int turn;
boolean flag[2];

CSE-II-II(R-23) Page 2
turn: The variable turn indicates whose turn it is to enter its critical section. Ex: if turn == i, then process
Pi is allowed to execute in its critical section
flag: Initialized to FALSE, initially no one is interested in entering the critical section .The flag array is
used to indicate if a process is ready to enter its critical section. Ex: if flag [i] is true, this value indicates
that Pi is ready to enter its critical section.
1. To enter the critical section, process Pi first sets flag [i] to be true and then sets turn to the value j,
thereby asserting that if the other process wishes to enter the critical section, it can do so.
2. If both processes try to enter at the same time, turn will be set to both i and j at roughly the same
time. Only one of these assignments will last, the other will occur but will be over written immediately.
3. The eventual value of turn determines which of the two processes is allowed to enter its critical
section first.

The structure of process Pi in Peterson's solution

Peterson’s Solution preserves all three conditions:

 Mutual Exclusion is assured as only one process can access the critical section at any time.
 Progress is also assured, as a process outside the critical section does not block other processes
from entering the critical section.
 Bounded Waiting is preserved as every process gets a fair chance.

Disadvantages of Peterson’s solution:

 It involves busy waiting.(In the Peterson’s solution, the code statement- “while(flag[j] &&
turn == j);” is responsible for this. Busy waiting is not favored because it wastes CPU cycles
that could be used to perform other tasks.)
 Itislimitedto2processes.
 Peterson’s solution cannot be used in modern CPU architectures.

CSE-II-II(R-23) Page 3
 SYNCHRONIZATION HARDWARE

 Problems of Critical Section are also solvable by hardware.


 The solution to the critical-section problem requires a simple tool-a lock.
 Some systems provide a lock functionality where a Process acquires a lock while entering the CS
and releases the lock after leaving it. Thus another process trying to enter CS cannot enter as
the entry is locked. It can only do so if it is free by acquiring the lock itself
MUTEX LOCKS:
As the synchronization hardware solution is not easy to implement for everyone, a strict software
approach called Mutex Locks.
That is, a process must acquire a lock before entering a critical section and it releases the lock when it
exits the critical section
Solution to the critical-section problem using locks
do {
Acquire lock
Critical section
Release lock
Remainder section
} while (TRUE);

 Both these instructions are atomic instructions which mean that when a process is executing
any of these instructions it cannot be preempted until the instruction is complete.
TestAndSet() and Swap() instruction Instructions:
 Many modern computer systems provide special hardware instructions that allow to test and
modify the content of a word or to swap the contents of two words atomically, that is, as one
uninterruptible unit. Two such solutions are:
1. TestAndSet() instruction
2. Swap() instruction
 Special instructions such as test_and_set () and swap() instructions are used to solve the critical-
section problem.
 The test_and_set () instruction can be defined as shown in Figure. The important characteristic
of this instruction is that it is executed atomically
TestAndSet() and Swap() instruction Instructions: TestAndSet() instruction uses a boolean variable lock.
The initial value of the lock is false. The variable lock ensures mutual exclusion. If the value of lock is
false, this means that no process is in its critical section. Hence, the value true means that some process
is running in its critical section.

TestAndSet Instruction
Definition:
boolean TestAndSet (boolean *target)
{
boolean rv = *target;
*target = TRUE;
return rv:

CSE-II-II(R-23) Page 4
}
Solution using TestAndSet
Shared boolean variable lock., initialized to false.
Solution:
do {
while ( TestAndSet (&lock ))
; // do nothing
// critical section
lock = FALSE;
// remainder section
} while (TRUE);
Swap Instruction
Definition:
void Swap (boolean *a, boolean *b)
{
boolean temp = *a;
*a = *b;
*b = temp:
}
Solution using Swap
Shared Boolean variable lock initialized to FALSE; Each process has a local Boolean variable key
Solution:
do {
key = TRUE;
while ( key == TRUE)
Swap (&lock, &key );

// critical section
lock = FALSE;
// remainder section
} while (TRUE);
 Swap() it is executed atomically. If the machine supports the Swap() instruction, then mutual
exclusion can be provided as follows.
 A global Boolean variable lock is declared and is initialized to false. In addition, each process has
a local Boolean variable key. The structure of process Pi is shown in below.
 SEMAPHORE
 A semaphore is a synchronization tool is used solve various synchronization problem and can be
implemented efficiently.
 A semaphore is a signaling mechanism and a thread that is waiting on a semaphore can be
signaled by another thread.

CSE-II-II(R-23) Page 5
 This is different than a mutex as the mutex can be signaled only by the thread that is called the
wait function.
 Semaphores do not require busy waiting.
 A semaphore S is an integer variable that is accessed only through two standard atomic
operations: wait () and signal (). The wait () operation was originally termed P and signal() was
called V.
Definition of wait():
wait(S){
whileS<=0;//busywait
S--;
}

Definition of signal():

signal(S){

S++;}
 All modifications to the integer value of the semaphore in the wait () and signal()
operations must be executed indivisibly. That is, when one process modifies the
semaphore value, no other process can simultaneously modify that same
semaphore value.
PROPERTIES OF SEMAPHORES
 It's simple and always have a non-negative Integer value.
 Works with many processes.
 Can have many different critical sections with different semaphores.
 Each critical section has unique access semaphores.
TYPES OF SEMAPHORES
1. BINARY SEMAPHORE:
 The value of a binary semaphore can range only between 0 and 1.
 Binary semaphores are known as mutex locks, as they are locks that provide mutual exclusion.
Binary semaphores to deal with the critical-section problem for multiple processes.
 Then processes share a semaphore, mutex, initialized to 1.
Each process Pi is organized as shown in below figure
do {
wait(mutex);
//CriticalSectionsignal
(mutex);
//remaindersection

CSE-II-II(R-23) Page 6
}while (TRUE);
2. Counting semaphore

 The value of a counting semaphore can range over an unrestricted domain.


 Counting semaphores can be used to control access to a given resource consisting of a finite
number of instances. The semaphore is initialized to the number of resources available.
 When the count for the semaphore goes to 0, all resources are being used. After that, processes
that wish to use a resource will block until the count becomes greater than 0.
LIMITATIONS OF SEMAPHORES
 Priority Inversion is a big limitation of semaphores.
 Their use is not enforced, but is by convention only.
 With improper use, a process may block indefinitely. Such a situation is called Deadlock.
Semaphore implementation with no busy waiting
 Semaphore Implementation with no Busy waiting
 With each semaphore there is an associated waiting queue. Each entry in a waiting queue has
two data items:
 value (of type integer)
 pointer to next record in the list
 Two operations:
 block – place the process invoking the operation on the appropriate waiting queue.
 wakeup – remove one of processes in the waiting queue and place it in the ready queue.
Implementation of wait:
wait(semaphore *S) {
S->value--;
if (S->value < 0) {
add this process to S->list;
block();
}
}
Implementation of signal:
signal(semaphore *S) {
S->value++;
if (S->value <= 0) {
remove a process P from S->list;
wakeup(P);
}
}

CSE-II-II(R-23) Page 7
 Monitor
Monitors in Process Synchronization:
 The monitor is one of the ways to achieve Process synchronization.
 The monitor is supported by programming languages to achieve mutual exclusion between
processes.
 For example Java Synchronized methods. Java provides wait() and notify() constructs.
1. It is the collection of condition variables and procedures combined together in a special kind
of module or a package.
2. The processes running outside the monitor can’t access the internal variable of the monitor
but can call procedures of the monitor.
3. Only one process at a time can execute code inside monitors.

Syntax:

Condition Variables:
Two different operations are performed on the condition variables of the monitor.
1. Wait.
2. signal.
Let say we have 2 condition variables
condition x, y; // Declaring variable
Wait operation x.wait() : Process performing wait operation on any condition variable are suspended.
The suspended processes are placed in block queue of that condition variable.
Note: Each condition variable has its unique block queue.
Signal operation x.signal(): When a process performs signal operation on condition variable, one of the
blocked processes is given chance.
If (x block queue empty)
// Ignore signal
else

CSE-II-II(R-23) Page 8
// Resume a process from block queue.

Monitor with Condition Variables

 Classical problems of Synchronization


Classical problems of Synchronization with Semaphore Solution: The following problems of
synchronization are considered as classical problems:
1. Bounded-buffer (or Producer-Consumer) Problem,
2. Readers and Writers Problem,
3. Dining-Philosphers Problem
1. BOUNDED BUFFER PROBLEM
 Bounded buffer problem, which is also called producer consumer problem, is one of the classic
problems of synchronization.
 This is a generalization of the producer-consumer problem where in access is controlled to a
shared group of buffers of a limited size. (Producer consumer problem to solve bounded buffer
problem)
 In this solution, the two counting semaphores "full" and "empty" keep track of the current
number of full and empty buffers respectively ( and initialized to 0 and N respectively. )
 The binary semaphore mutex controls access to the critical section. The producer and consumer
processes are nearly identical - One can think of the producer as producing full buffers, and the
consumer producing empty buffers.
o N buffers, each can hold one item.
o Semaphore, mutex initialized=1
o Semaphore, full=0 empty=N
Full=0, mutex=1, empty=1

Problem Statement:

CSE-II-II(R-23) Page 9
There is a buffer of n slots and each slot is capable of storing one unit of data. There are two processes
running, namely, producer and consumer, which are operating on the buffer.

Bounded Buffer Problem

A producer tries to insert data into an empty slot of the buffer. A consumer tries to remove data from a
filled slot in the buffer. As you might have guessed by now, those two processes won’t produce the
expected output if they are being executed concurrently.

There needs to be a way to make the producer and consumer work in an independent manner.

Solution:

One solution of this problem is to use semaphores. The semaphores which will be used here are:

 m, a binary semaphore which is used to acquire and release the lock.


 empty, a counting semaphore whose initial value is the number of slots in the buffer, since,
initially all slots are empty.
 full, a counting semaphore whose initial value is 0.

At any instant, the current value of empty represents the number of empty slots in the buffer and full
represents the number of occupied slots in the buffer.

Producer Operation:
The pseudocode of the producer function looks like this:
do {
wait(empty);//waituntilempty>0andthendecrement ‘empty’
wait(mutex);//acquirelock
/* performthe
insert operation in a slot */
signal(mutex); // release lock
signal(full);//increment‘full’
} while(TRUE)

CSE-II-II(R-23) Page 10
 Looking at the above code for a producer, we can see that a producer first waits until there is
atleast one empty slot.
 Then it decrements the empty semaphore because, there will now be one less empty slot, since
the producer is going to insert data in one of those slots.
 Then, it acquires lock on the buffer, so that the consumer cannot access the buffer until
producer completes its operation.
 After performing the insert operation, the lock is released and the value of full is incremented
because the producer has just filled a slot in the buffer.
Consumer Operation:
The pseudocode of the consumer function looks like this:

do {
wait(full); // wait until full>0 and then decrement ‘full’
wait(mutex); // acquire the lock
/* perform the remove operation
in a slot */
signal(mutex); // release the lock
signal(empty); // increment ‘empty’
} while(TRUE);

 The consumer waits until there is atleast one full slot in the buffer.
 Then it decrements the full semaphore because the number of occupied slots will be decreased
by one, after the consumer completes its operation.
 After that, the consumer acquires lock on the buffer.
 Following that, the consumer completes the removal operation so that the data from one of the
full slots is removed.
 Then, the consumer releases the lock.
 Finally, the empty semaphore is incremented by 1, because the consumer has just removed data
from an occupied slot, thus making it empty.
2. READERS WRITER PROBLEM:
Readers writer problem is another example of a classic synchronization problem. There are many
variants of this problem, one of which is examined below.
A data set is shared among a number of concurrent processes
 Readers – only read the data set; they do not perform any updates
 Writers – can both read and write.
 Problem – allow multiple readers to read at the same time. Only one single writer can access the
shared data at the same time.
The Problem Statement
There is a shared resource which should be accessed by multiple processes. There are two types of
processes in this context. They are reader and writer. Any number of readers can read from the shared
resource simultaneously, but only one writer can write to the shared resource. When a writer is writing
data to the resource, no other process can access the resource. A writer cannot write to the resource if
there are non zero number of readers accessing the resource at that time.
The Solution
 From the above problem statement, it is evident that readers have higher priority than writer. If
a writer wants to write to the resource, it must wait until there are no readers currently
accessing that resource.

CSE-II-II(R-23) Page 11
 Here, we use one mutex m and a semaphore w. An integer variable read_count is used to
maintain the number of readers currently accessing the resource.
 The variable read_count is initialized to 0. A value of 1 is given initially to m and w.
 Instead of having the process to acquire lock on the shared resource, we use the mutex m to
make the process to acquire and release lock whenever it is updating the read_count variable
The code for the writer process looks like this:
while(TRUE)
{
wait(w);
/* perform the write operation */
signal(w);
}
And, the code for the reader process looks like this:
while(TRUE)
{
//acquire lock
wait(m);
read_count++;
if(read_count == 1)
wait(w);
//release lock
signal(m);
/* perform the reading operation */
// acquire lock
wait(m);
read_count--;
if(read_count == 0)
signal(w);
// release lock
signal(m);
}
Explanation:
 As seen above in the code for the writer, the writer just waits on the w semaphore until it gets a
chance to write to the resource.
 After performing the write operation, it increments w so that the next writer can access the
resource.
 On the other hand, in the code for the reader, the lock is acquired whenever the read_count is
updated by a process.
 When a reader wants to access the resource, first it increments the read_count value, then
accesses the resource and then decrements the read_count value.
 The semaphore w is used by the first reader which enters the critical section and the last reader
which exits the critical section.
 The reason for this is, when the first readers enters the critical section, the writer is blocked
from the resource. Only new readers can access the resource now.
 Similarly, when the last reader exits the critical section, it signals the writer using the w
semaphore because there are zero readers now and a writer can have the chance to access the
resource.

CSE-II-II(R-23) Page 12
3. DINING PHILOSOPHERS PROBLEM (DPP)
 The dining philosopher's problem is the classical problem of synchronization
 Which says that Five philosophers are sitting around a circular table and their job is to think and
eat alternatively.
 There is a bowl of rice for each of the philosophers and 5 chopsticks.
 A philosopher needs both their right and left chopstick to eat.
 A hungry philosopher may only eat if there are both chopsticks available. Otherwise a
philosopher puts down their chopstick and begins thinking again.

Solution of Dining Philosophers Problem


A solution of the Dining Philosophers Problem is to use a semaphore to represent a chopstick. A
chopstick can be picked up by executing a wait operation on the semaphore and released by executing a
signal semaphore.
The structure of the chopstick is shown below:
semaphore chopstick [5];
Initially the elements of the chopstick are initialized to 1 as the chopsticks are on the table and not
picked up by a philosopher.
The structure of a random philosopher i is given as follows:
do {
wait( chopstick[i] );
wait( chopstick[ (i+1) % 5] );
...
//EAT
...
signal( chopstick[i] );
signal( chopstick[ (i+1) % 5] );
...
//THINK
...
} while(TRUE);
In the above structure, first wait operation is performed on chopstick[i] and chopstick[ (i+1) % 5]. This
means that the philosopher i has picked up the chopsticks on his sides. Then the eating function is
performed. After that, signal operation is performed on chopstick[i] and chopstick[ (i+1) % 5]. This
means that the philosopher i has eaten and put down the chopsticks on his sides. Then the philosopher
goes back to thinking
Several possible remedies to the deadlock problem are replaced by:
 Allow at most four philosophers to be sitting simultaneously at the table.

CSE-II-II(R-23) Page 13
 Allow a philosopher to pickup her chopsticks only if both chopsticks are available.
 Use an asymmetric solution—that is, an odd-numbered philosopher picks up first her left
chopstick and then her right chopstick, whereas an even numbered philosopher picks up her
right chopstick and then her left chopstick.

Difficulty with the solution


The above solution makes sure that no two neighboring philosophers can eat at the same time. But this
solution can lead to a deadlock. This may happen if all the philosophers pick their left chopstick
simultaneously. Then none of them can eat and deadlock occurs.
 Some of the ways to avoid deadlock are as follows:
 There should be at most four philosophers on the table.
 An even philosopher should pick the right chopstick and then the left chopstick while an odd
philosopher should pick the left chopstick and then the right chopstick.
 A philosopher should only be allowed to pick their chopstick if both are available at the same
time.
CRITICAL REGIONS:
 A critical region is a section of code that is always executed under mutual exclusion.
 Critical regions shift the responsibility for enforcing mutual exclusion from the programmer
(where it resides when semaphores are used) to the compiler.
 They consist of two parts:
o Variables that must be accessed under mutual exclusion.
o A new language statement that identifies a critical region in which the variables are accessed.
EXAMPLE
var v :
shared T;
...
region v do
begin
...
end;
CONDITIONAL CRITICAL REGIONS:
Conditional critical regions (CCRs) are regions in a program that can only be executed by one process at
a time. They are used to design and describe process synchronization in real-time systems

CSE-II-II(R-23) Page 14
Deadlocks
Deadlocks: system Model
INTRODUCTION TO DEADLOCK:-
Def:-Deadlocks are a set of blocked processes each holding a resource and waiting to acquire a resource
held by another process.
Or
A Deadlock is a situation where each of the computer process waits for a resource which is being
assigned to some another process. In this situation, none of the process gets executed since the
resource it needs, is held by some other process which is also waiting for some other resource to be
released.
Or
When processes request a resource and if the resources are not available at that time the process enters
into waiting state. Waiting process may not change its state because the resources they are requested
are held by other process. This situation is called deadlock. The situation where the process waiting for
the resource i.e., not available is called deadlock.
Bridge Crossing Example

 Traffic only in one direction


 Each section of a bridge can be viewed as a resource
 If a deadlock occurs, it can be resolved if one car backs up (preempt resources and rollback)
 Several cars may have to be backed up if a deadlock occurs
 Starvation is possible
 Note – Most OSes do not prevent or deal with deadlocks
System Model
A process must request a resource before using it and it must release the resource after using it. It can
request any number of resources to carry out a designated task. The amount of resource requested may
not exceed the total number of resources available.

CSE-II-II(R-23) Page 15
A process may utilize the resources in only the following sequences:
Request:-If the request is not granted immediately then the requesting process must wait it can acquire
the resources.
Use:-The process can operate on the resource.
Release:-The process releases the resource after using it.
Let us assume that there are three processes P1, P2 and P3. There are three different resources R1, R2
and R3. R1 is assigned to P1, R2 is assigned to P2 and R3 is assigned to P3.
After some time, P1 demands for R2 which is being used by P2. P1 halts its execution since it can't
complete without R2. P2 also demands for R3 which is being used by P3. P2 also stops its execution
because it can't continue without R3. P3 also demands for R1 which is being used by P1 therefore P3
also stops its execution.
In this scenario, a cycle is being formed among the three processes. None of the process is progressing
and they are all waiting. The computer becomes unresponsive since all the processes got blocked

DIFFERENCE BETWEEN STARVATION AND DEADLOCK:-

Sr. Deadlock Starvation

1 Deadlock is a situation where no process Starvation is a situation where the


got blocked and no process proceeds low priority process got blocked and
the high priority processes proceed.

2 Deadlock is an infinite waiting. Starvation is along waiting but not


infinite.

3 Every Deadlock is always a starvation. Every starvation need not be


deadlock.

4 The requested resource is blocked by the The requested resource is


other process. continuously be used by the higher
priority processes.

5 Deadlock happens when Mutual It occurs due to the uncontrolled


exclusion, hold and wait, No preemption priority and resource management.
and circular wait occur simultaneously.

NECESSARY CONDITIONS FOR DEADLOCKS :( DEADLOCK CHARACTERIZATION)

CSE-II-II(R-23) Page 16
1. Mutual Exclusion: only one process at a time can use a resource
2. Hold and wait: a process holding at least one resource is waiting to acquire additional resources
held by other processes
3. No preemption: a resource can be released only voluntarily by the process holding it, after that
process has completed its task
4. Circular wait: there exists a set {P0 , P1 , …, Pn } of waiting processes such that P0 is waiting for a
resource that is held by P1 , P1 is waiting for a resource that is held by P2 , …, Pn–1 is waiting for
a resource that is held by Pn , and Pn is waiting for a resource that is held by P0 .

RESOURCEALLOCATIONGRAPH
Resource Allocation Graph (RAG) is a graph that represents the state of a Directed Graph.
Components Of RAG-There are two major components of a Resource Allocation Graph-
1. Vertices
2. Edges
1. Vertices-
There are following types of vertices in a Resource Allocation Graph

 Process Vertices
 Resource Vertices

Process Vertices Process vertices represent the


processes. They are drawn as a circle
by mentioning the name of
process inside the circle.

Resource Vertices
Resource vertices represent the resources. Depending on the number of instances that exists in the
system, resource vertices may be single instance or multiple instances. They are drawn as a rectangle by
mentioning the dots inside the rectangle. The number of dots inside the rectangle indicates the number
of instances of that resource existing in the system.

2. Edges
There are two types of edges in a Resource Allocation Graph
1. Assign Edges
2. Request Edges

CSE-II-II(R-23) Page 17
Assign Edges: Assign edges represent the assignment of resources to the processes. They are drawn as
an arrow where the head of the arrow points to the process and tail of the process points to the
instance of the resource.

Request Edges: Request edges represent the waiting state of processes for the resources. They are
drawn as an arrow where the head of the arrow points to the instance of the resource and tail of the
process points to the process. If a process requires ‘n’ instances of a resource type, then ‘n’ assign edges
will be drawn.

EXAMPLE FOR RESOURCE ALLOCATION GRAPH


Ex:1
It gives the following information-
• There exist three processes in the system namely P1, P2 and P3.
• There exist two resources in the system namely R1 and R2.
• There exists a single instance of resource R1 and two instances of resource R2.
• Process P1 holds one instance of resource R1 and is waiting for an instance of resource R2.
• Process P2 holds one instance of resource R2 and is waiting for an instance of resource R1.
• Process P3 holds one instance of resource R2 and is not waiting for anything.

EXAMPLE OF A RESOURCE ALLOCATION GRAPH


Ex:2
As shown in below figures, the RAG illustrates the following 3 situation
1) RAG with a deadlock 2) RAG with a cycle and deadlock 3) RAG with a cycle but no deadlock

CSE-II-II(R-23) Page 18
Basic Facts
 If a graph contain no cycle=> no dead lock
 If graph contains a cycle
o if only one instance per resource type, then deadlock
o if several instances per resource type, possibility of deadlock
Methods for Handling Deadlocks
There are three ways of handling deadlocks:
1. Deadlock prevention
2. Deadlock avoidance
3. Deadlock detection and recovery.
1. Deadlock prevention:
 Deadlock happens only when Mutual Exclusion, hold and wait, No preemption and circular wait
holds simultaneously. If it is possible to violate one of the four conditions at any time then the
deadlock can never occur in the system.
 The idea behind the approach is very simple that we have to fail one of the four conditions but
there can be a big argument on its physical implementation in the system.
2. Deadlock avoidance:
 In deadlock avoidance, the operating system checks whether the system is in safe state or in
unsafe state at every step which the operating system performs.
 The process continues until the system is in safe state. Once the system moves to unsafe state,
the OS has to backtrack one step.
3. Deadlock detection and recovery:
This approach let the processes fall in deadlock and then periodically check whether deadlock occur in
the system or not. If it occurs then it applies some of the recovery methods to the system to get rid of
deadlock

CSE-II-II(R-23) Page 19
 Deadlock Prevention
To prevent the system from deadlocks, one of the four discussed conditions that may create a deadlock
should be discarded.
• Deadlocks can be eliminated by preventing at least one of the four required conditions:
1) Mutual exclusion
2) Hold-and-wait
3) No preemption
4) Circular-wait.
Mutual Exclusion: In general, we do not have systems with all resources being sharable. Some resources
like printers, processing units are non-sharable. So it is not possible to prevent deadlocks by denying
mutual exclusion.
• This condition must hold for non-sharable resources.
 For example: A printer cannot be simultaneously shared by several processes.
• On the other hand, shared resources do not lead to deadlocks.
 For example: Simultaneous access can be granted for read-only file.
• A process never waits for accessing a sharable resource.
• In general, we cannot prevent deadlocks by denying the mutual-exclusion condition because some
resources are non-sharable by default.
Hold and Wait
Whenever a process requests a resource, it does not hold any other resources
 Require process to request and be allocated all its resources before it begins execution, or allow
process to request resources only when the process has none
 Low resource utilization; starvation possible
No Preemption –
 If a process that is holding some resources requests another resource that cannot be
immediately allocated to it, then all resources currently being held are released
 Preempted resources are added to the list of resources for which the process is waiting
 Process will be restarted only when it can regain its old resources, as well as the new ones that it
is requesting
Circular Wait –
One protocol to ensure that the circular wait condition never holds is “Impose a linear ordering of all
resource types.” Then, each process can only request resources in an increasing order of priority.

For example, set priorities for r1 = 1, r2 = 2, r3 = 3, and r4 = 4. With these priorities, if process P wants to
use r1 and r3, it should first request r1, then r3.

Another protocol is “Whenever a process requests a resource rj, it must have released all resources rk
with priority (rk) ≥ priority (rj).

CSE-II-II(R-23) Page 20
 Deadlock Avoidance
 Given some additional information on how each process will request resources, it is possible to
construct an algorithm that will avoid deadlock states. The algorithm will dynamically examine
the resource allocation operations to ensure that there won't be a circular wait on resources.
 When a process requests a resource that is already available, the system must decide whether
that resource can immediately be allocated or not. The resource is immediately allocated only if
it leaves the system in a safe state.
 A state is safe if the system can allocate resources to each process in some order avoiding a
deadlock. A deadlock state is an unsafe state
Safe State
• A state is safe if the system can allocate all resources requested by all processes without entering a
deadlock state.
• A state is safe if there exists a safe sequence of processes {P0, P1, P2, ..., PN} such that the requests of
each process(Pi) can be satisfied by the currently available resources.
• If a safe sequence does not exist, then the system is in an unsafe state, which may lead to deadlock. •
All safe states are deadlock free, but not all unsafe states lead to deadlocks.

Safe, unsafe, and deadlock state spaces

Basic Facts
 If a system is in safe stateno deadlocks
 If a system is in unsafe state possibility of deadlock
 Avoidanceensures that a system will never enter an unsafe state.
Avoidance algorithms
 Single instance of a resource type
o Use a resource-allocation graph
 Multiple instances of a resource type
o Use the banker’s algorithm
Resource-Allocation-Graph Algorithm
 If resource categories have only single instances of their resources, then deadlock states can be
detected by cycles in the resource-allocation graphs.
 In this case, unsafe states can be recognized and avoided by augmenting the resource-allocation
graph with claim edges (denoted by a dashed line).
 Claim edge Pi → Rj indicated that process Pi may request resource Rj at some time in future.
 The important steps are as below:

CSE-II-II(R-23) Page 21
Deadlock Detection using RAG
• If a cycle is being formed in a Resource allocation graph where all the resources have the single
instance then the system is deadlocked.
• In Case of Resource allocation graph with multi-instanced resource types, Cycle is a necessary
condition of deadlock but not the sufficient condition.
• The following example contains three processes P1, P2, P3 and three resources R2, R2, R3. All the
resources are having single instances each.
• If we analyze the graph then we can find out that there is a cycle formed in the graph since the system
is satisfying all the four conditions of deadlock.

Methods for handling Deadlocks:-


 Ensure that the system will never enter a deadlock state.
 Allow the system to enter a deadlock state and then recover.
 Ignore the problem and pretend that deadlocks never occur in the system; used by most
operating systems, including UNIX.
1. Deadlock Prevention
2. Deadlock Avoidance
3. Deadlock Detection
1. DEADLOCK DETECTION
Restrain the ways request can be made.
 Mutual Exclusion – not required for sharable resources; must hold for non sharable resources.
 Hold and Wait – must guarantee that whenever a process requests a resource, it does not hold
any other resources
o Require process to request and be allocated all its resources before it begins execution,
or allow process to request resources only when the process has none

CSE-II-II(R-23) Page 22
o Low resource utilization; starvation possible
 No Preemption –
o If a process that is holding some resources requests another resource that cannot be
immediately allocated to it, then all resources currently being held are released
o Preempted resources are added to the list of resources for which the process is waiting
o Process will be restarted only when it can regain its old resources, as well as the new
ones that it is requesting
o Circular Wait – impose a total ordering of all resource types, and require that each process
requests resources in an increasing order of enumeration
2. DEADLOCK AVOIDANCE
Requires that the system has some additional a priori information available
 Simplest and most useful model requires that each process declare the maximum number of
resources of each type that it may need
 The deadlock-avoidance algorithm dynamically examines the resource-allocation state to ensure
that there can never be a circular-wait condition
 Resource-allocation state is defined by the number of available and allocated resources, and
the maximum demands of the processes
Safe State
 When a process requests an available resource, system must decide if immediate allocation
leaves the system in a safe state.
 System is in safe state if there exists a safe sequence of all processes.
 Sequence <P1, P2, …, Pn> is safe if for each Pi, the resources that Pi can still request can be
satisfied by currently available resources + resources held by all the Pj, with j<I.
– If Pi resource needs are not immediately available, then
Pi can wait until all Pj have finished.
– When Pj is finished, Pi can obtain needed resources,
execute, return allocated resources, and terminate.
– When Pi terminates, Pi+1 can obtain its needed
Basic Facts
 If a system is in safe state ⇒ no deadlocks.
 If a system is in unsafe state ⇒ possibility of deadlock.
 Avoidance ⇒ ensure that a system will never enter an unsafe state.
Safe, Unsafe , Deadlock State

Avoidance algorithms
 Single instance of a resource type
o Use a resource-allocation graph
 Multiple instances of a resource type

CSE-II-II(R-23) Page 23
o Use the banker’s algorithm
Banker's Algorithm in Operating System
 Banker's algorithm is a deadlock avoidance algorithm. It is named so because this algorithm is
used in banking systems to determine whether a loan can be granted or not.
 Consider there are n account holders in a bank and the sum of the money in all of their accounts
is S. Every time a loan has to be granted by the bank, it subtracts the loan amount from the total
money the bank has. Then it checks if that difference is greater than S. It is done because, only
then, the bank would have enough money even if all the n account holders draw all their money
at once.

Banker's algorithm works in a similar way in computers.

Whenever a new process is created, it must specify the maximum instances of each resource type that it
needs, exactly.

 This algorithm is applicable to the system with multiple instances of each resource types.
 However, this algorithm is less efficient then the resource-allocation-graph algorithm.
 When a process starts up, it must declare the maximum number of resources that it may need.
 This number may not exceed the total number of resources in the system.
 When a request is made, the system determines whether granting the request would leave the
system in a safe state.
 If the system in a safe state, the resources are allocated;
else
 the process must wait until some other process releases enough resources.

• Assumptions:
Let n = number of processes in the system
Let m = number of resources types.

1. Available
It is an array of length m. It represents the number of available resources of each type. If Available[j] = k,
then there are k instances available, of resource type Rj.
2. Max
It is an n x m matrix which represents the maximum number of instances of each resource that a process
can request. If Max[i][j] = k, then the process Pi can request atmost k instances of resource type Rj.
3) Allocation [n][m]
It is an n x m matrix which represents the number of resources of each type currently allocated to each
process. If Allocation[i][j] = k, then process Pi is currently allocated k instances of resource type Rj.
4) Need [n][m]
It is a two-dimensional array. It is an n x m matrix which indicates the remaining resource needs of each
process. If Need[i][j] = k, then process Pi may need k more instances of resource type Rj to complete its
task.
Need[i][j] = Max[i][j] - Allocation [i][j]

The Banker’s algorithm has two parts:


1) Safety Algorithm
2) Resource – Request Algorithm

CSE-II-II(R-23) Page 24
Safety Algorithm
This algorithm is used for finding out whether a system is in safe state or not
Assumptions:
 Work is a working copy of the available resources, which will be modified during the analysis.
 Finish is a vector of boolean values indicating whether a particular process can finish.

Step 1:
Let Work and Finish be two vectors of length m and n respectively.
Initialize:
Work = Available
Finish[i] = false for i=1,2,3,…….n
Step 2:
Find an index(i) such that both
a) Finish[i] = false
b) Need i <= Work.
If no such i exist, then go to step 4
Step 3:
Set:
Work = Work + Allocation(i)
Finish[i] = true
Go to step 2
Step 4:
If Finish[i] = true for all i, then the system is in safe state.

Resource-Request Algorithm
Now the next algorithm is a resource-request algorithm and it is mainly used to determine whether
requests can be safely granted or not.

Let Request(i) be the request vector for the process Pi. If Requesti[j]==k, then process Pi wants k instance
of Resource type Rj. When a request for resources is made by the process Pi, the following are the actions
that will be taken:

1. If Request(i) <= Need(i), then go to step 2;else raise an error condition, since the process has
exceeded its maximum claim.

2. If Request(i) <= Available(i) then go to step 3; else Pi must have to wait as resources are not available.

3. Now we will assume that resources are assigned to process Pi and thus perform the following steps:

Available = Available - Request(i);

Allocation(i) = Allocation(i) + Request(i);

Need(i) = Need(i) - Request(i);

CSE-II-II(R-23) Page 25
Banker's Algorithm Example::

Let us consider the following snapshot for understanding the banker's algorithm:

Example:1 Considering a system with five processes P0 through P4 and three resources of type A, B, C.
Resource type A has 10 instances, B has 5 instances and type C has 7 instances. Suppose at time
t0 following snapshot of the system has been taken:

1. calculate the content of the need matrix?


2. Check if the system is in a safe state?
3. Determine the total sum of each type of resource?

What will be the content of the Need matrix?

Need [i, j] = Max [i, j] – Allocation [i, j]

Need [i] = Max [i] - Allocation [i]


Need for P1: (7, 5, 3) - (0, 1, 0) = 7, 4, 3
Need for P2: (3, 2, 2) - (2, 0, 0) = 1, 2, 2
Need for P3: (9, 0, 2) - (3, 0, 2) = 6, 0, 0
Need for P4: (2, 2, 2) - (2, 1, 1) = 0, 1, 1
Need for P5: (4, 3, 3) - (0, 0, 2) = 4, 3, 1

Hence, we created the context of need matrix.

CSE-II-II(R-23) Page 26
Apply the Banker's Algorithm:

Available Resources of A, B and C are 3, 3, and 2.

Now we check if each type of resource request is available for each process.
Step 1: For Process P1:
Need <= Available
7, 4, 3 <= 3, 3, 2 condition is false.
So, we examine another process, P2.
Step 2: For Process P2:
Need <= Available
1, 2, 2 <= 3, 3, 2 condition true
New available = available + Allocation
(3, 3, 2) + (2, 0, 0) => 5, 3, 2
Similarly, we examine another process P3.
Step 3: For Process P3:
P3 Need <= Available
6, 0, 0 < = 5, 3, 2 condition is false.
Similarly, we examine another process, P4.
Step 4: For Process P4:
P4 Need <= Available
0, 1, 1 <= 5, 3, 2 condition is true
New Available resource = Available + Allocation
5, 3, 2 + 2, 1, 1 => 7, 4, 3
Similarly, we examine another process P5.
Step 5: For Process P5:
P5 Need <= Available
4, 3, 1 <= 7, 4, 3 condition is true
New available resource = Available + Allocation
7, 4, 3 + 0, 0, 2 => 7, 4, 5
Now, we again examine each type of resource request for processes P1 and P3.
Step 6: For Process P1:
P1 Need <= Available
7, 4, 3 <= 7, 4, 5 condition is true
New Available Resource = Available + Allocation
7, 4, 5 + 0, 1, 0 => 7, 5, 5
So, we examine another process P2.
Step 7: For Process P3:
P3 Need <= Available
6, 0, 0 <= 7, 5, 5 condition is true
New Available Resource = Available + Allocation
7, 5, 5 + 3, 0, 2 => 10, 5, 7
Hence, we execute the banker's algorithm to find the safe state and the safe sequence like P2, P4, P5,
P1 and P3.

CSE-II-II(R-23) Page 27
Let us consider the following snapshot for understanding the banker's algorithm:

Example 2:

Allocation Max Available


Processes
ABC ABC ABC

P0 112 433 210

P1 212 322

P2 401 902

P3 020 753

P4 112 112

1. calculate the content of the need matrix?


2. Check if the system is in a safe state?
3. Determine the total sum of each type of resource?

Solution:

1. The Content of the need matrix can be calculated by using the formula given below:
Need = Max – Allocation

2. Let us now check for the safe state.


Safe sequence:

1. For process P0, Need = (3, 2, 1) and

Available = (2, 1, 0)

Need <=Available = False

CSE-II-II(R-23) Page 28
So, the system will move to the next process.

2. For Process P1, Need = (1, 1, 0)

Available = (2, 1, 0)

Need <= Available = True

Request of P1 is granted.

Available = Available +Allocation

= (2, 1, 0) + (2, 1, 2)

= (4, 2, 2) (New Available)

3. For Process P2, Need = (5, 0, 1)

Available = (4, 2, 2)

Need <=Available = False

So, the system will move to the next process.

4. For Process P3, Need = (7, 3, 3)

Available = (4, 2, 2)

Need <=Available = False

So, the system will move to the next process.

5. For Process P4, Need = (0, 0, 0)

Available = (4, 2, 2)

Need <= Available = True

Request of P4 is granted.

Available = Available + Allocation

= (4, 2, 2) + (1, 1, 2)

= (5, 3, 4) now, (New Available)

6. Now again check for Process P2, Need = (5, 0, 1)

CSE-II-II(R-23) Page 29
Available = (5, 3, 4)

Need <= Available = True

Request of P2 is granted.

Available = Available + Allocation

= (5, 3, 4) + (4, 0, 1)

= (9, 3, 5) now, (New Available)

7. Now again check for Process P3, Need = (7, 3, 3)

Available = (9, 3, 5)

Need <=Available = True

The request for P3 is granted.

Available = Available +Allocation

= (9, 3, 5) + (0, 2, 0) = (9, 5, 5)

8. Now again check for Process P0, = Need (3, 2, 1)

= Available (9, 5, 5)

Need <= Available = True

So, the request will be granted to P0.

Safe sequence: < P1, P4, P2, P3, P0>

The system allocates all the needed resources to each process. So, we can say that the system is in a
safe state.

3. The total amount of resources will be calculated by the following formula:

The total amount of resources= sum of columns of allocation + Available

= [8 5 7] + [2 1 0] = [10 6 7]

CSE-II-II(R-23) Page 30
Resource-Request Algorithm
Example:
Question: Consider the following snapshot of a system:
Answer the following questions using Banker's algorithm. i) What is the content of the matrix need? ii) Is
the system in a safe state? iii) If a request from process P1 arrives for (1 0 2) can the request be granted
immediately?

Solution (i):
• The content of the matrix Need is given by
Need = Max - Allocation
• So, the content of Need Matrix is:

Solution (iii): P1 requests (1 0 2) i.e. Request[P1]=1 0 2


• To decide whether the request is granted, we use Resource Request algorithm.
Step 1: Request [P1]<=Need[P1] i.e. (1 0 2) <= (1 2 2) --> true.
Step 2: Request [P1]<=Available i.e. (1 0 2) <=(3 3 2) --> true.
Step 3: Available = Available – Request [P1] = (3 3 2) - (1 0 2)= (2 3 0)
Allocation [P1] = Allocation [P1] + Request[P1] = (2 0 0) + (1 0 2)= (3 0 2)
Need [P1] = Need[P1] – Request[P1] = (1 2 2) - (1 0 2)= (0 2 0)
• We arrive at the following new system state:
The content of the matrix Need is given by
Need = Max – Allocation
• So, the content of Need Matrix is:

CSE-II-II(R-23) Page 31
3. Deadlock Detection
• If a system does not use deadlock-prevention or deadlock-avoidance algorithm then a deadlock may
occur.
• In this environment, the system must provide
1) An algorithm to examine the system-state to determine whether a deadlock has occurred.
2) An algorithm to recover from the deadlock.
Single Instance of Each Resource Type
 If all the resources have only a single instance then we can define deadlock detection algorithm
that uses a variant of resource allocation graph called a wait for graph.
 This graph is obtained by removing the nodes of type resources and removing appropriate edges
 If all the resources have only a single instance, then deadlock detection-algorithm can be
defined using a wait-for-graph.
 The wait-for-graph is applicable to only a single instance of a resource type.
 A wait-for-graph (WAG) is a variation of the resource-allocation-graph.
 The wait-for-graph can be obtained from the resource-allocation-graph by
o → removing the resource nodes and
o → collapsing the appropriate edges.
• An edge from Pi to Pj implies that process Pi is waiting for process Pj to release a resource that Pi
needs.
• An edge Pi → Pj exists if and only if the corresponding graph contains two edges
1) Pi → Rq and
2) Rq → Pj.
• For example:
Consider resource-allocation-graph shown in Figure a
Corresponding wait-for-graph is shown in Figure b.

CSE-II-II(R-23) Page 32
Figure (a) Resource-allocation-graph Figure (b) Corresponding wait-for-graph.
• A deadlock exists in the system if and only if the wait-for-graph contains a cycle.
• To detect deadlocks, the system needs to
→ maintain the wait-for-graph and
→ periodically execute an algorithm that searches for a cycle in the graph.
Several Instances of a Resource Type
• The wait-for-graph is applicable to only a single instance of a resource type.
• Problem: However, the wait-for-graph is not applicable to a multiple instance of a resource type.
• Solution: The following detection-algorithm can be used for a multiple instance of a resource type.
• Assumptions:
Let ‘n’ be the number of processes in the system
Let ‘m’ be the number of resources types.
• Following data structures are used to implement this algorithm.
1) Available [m]
 This vector indicates the no. of available resources of each type.
 If Available[j]=k, then k instances of resource type Rj is available.
2) Allocation [n][m]
 This matrix indicates no. of resources currently allocated to each process.
 If Allocation[i,j]=k, then Pi is currently allocated k instances of Rj.
3) Request [n][m]
 This matrix indicates the current request of each process.
 If Request [i, j] = k, then process Pi is requesting k more instances of resource type Rj.
DETECTION ALGORITHM

CSE-II-II(R-23) Page 33
DETECTION-ALGORITHM USAGE
 When, and how often, to invoke depends on:
 How often a deadlock is likely to occur?
 How many processes will need to be rolled back? one for each disjoint cycle If detection
algorithm is invoked arbitrarily, there may be many cycles in the resource graph and so we
would not be able to tell which of the many deadlocked processes ―caused the deadlock.
Recovery from deadlock
1. Process Termination:
 Abort all deadlocked processes
 Abort one process at a time until the deadlock cycle is eliminated
 For process termination, following factors need to be considered:
1) The priority of process.
2) The time taken by the process for computation & the required time for complete execution.
3) The no. of resources used by the process.
4) The no. of extra resources required by the process for complete execution.
5) The no. of processes that need to be terminated for deadlock-free execution.
6) The process is interactive or batch.
2. Resource Preemption
 Selecting a victim – minimize cost.
 Rollback – return to some safe state, restart process for that state.
 Starvation – same process may always be picked as victim, include number of rollback in cost
factor.

CSE-II-II(R-23) Page 34
UNIT-4
Memory-Management Strategies: Introduction, Contiguous memory allocation, Paging, Structure of the
Page Table, Swapping. Virtual Memory Management: Introduction, Demand paging, Copy-on-write,
Page replacement, Allocation of frames, Thrashing. Storage Management: Overview of Mass Storage
Structure, HDD Scheduling.

Introduction
 Memory management is the functionality of an operating system which handles or manages
primary memory and moves processes back and forth between main memory and disk during
execution.
 Memory management keeps track of each and every memory location, regardless of either it is
allocated to some process or it is free.
 It checks how much memory is to be allocated to processes.
 It decides which process will get memory at what time.
 It tracks whenever some memory gets freed or unallocated and correspondingly it updates the
status.
Memory Management
Logical address – is reference to memory location independent of the current assignment of data to
memory; a translation must be made to physical address before the memory access can be achieved. It
is generated by the CPU; also referred to as virtual address.
Physical address or Absolute Address is actual memory location in memory. It is the address seen by the
memory unit (HW).
Logical and physical addresses are the same in compile-time and load-time address-binding schemes;
logical (virtual) and physical addresses differ in execution-time address-binding scheme.
A relative address is a particular example of logical address, in which the address is expressed as a
location relative to some known point, usually a value in a processor register.
Base and Limit Registers
 A pair of base and limit registers define the logical address space
 CPU must check every memory access generated in user mode to be sure it is between base and
limit for that user
WHAT IS MEMORY?
Computer memory can be defined as a collection of some data represented in the binary format. On the
basis of various functions, memory can be classified into various categories.
A computer device that is capable to store any information or data temporally or permanently is called
storage device.
The main memory can be broadly allocated in two ways:
 Contiguous memory allocation
 Non-contiguous memory allocation
MEMORY ALLOCATION STRATEGIES:
Contiguous Memory Allocation:
 In contiguous memory allocation each process is contained in a single contiguous block of
memory.
 Memory is divided into several fixed size partitions.
 Each partition contains exactly one process.
 When a partition is free, a process is selected from the input queue and loaded into it.
 The free blocks of memory are known as holes.
 The set of holes is searched to determine which hole is best to allocate.
 Contiguous Technique can be divided into:
o 1. Fixed (or static) partitioning
o 2. Variable (or dynamic) partitioning
 Fixed Partition Scheme:
 In the fixed partition scheme, memory is divided into fixed number of partitions i.e. number of
partitions in RAM is fixed but the size of each partition may or may not be the same.
 Degree of multi-programming is restricted by number of partitions in the memory.
 In every partition only one process will be accommodated.
 Here partitions are made before execution or during system configure.
Advantages of Fixed Partitioning –
1. Easy to implement:
2. Little OS overhead:
Disadvantages of Fixed Partitioning –
1. Internal Fragmentation
2. External Fragmentation
3. Limit process size
4. Limitation on Degree of Multiprogramming
Variable Partitioning
 It is a part of Contiguous allocation technique.
 It is used to alleviate the problem faced by Fixed Partitioning.
 In contrast with fixed partitioning, partitions are not made before the execution or during
system configure.
 Various features associated with variable Partitioning
1. Initially RAM is empty and partitions are made during the run-time according to
process’s need instead of partitioning during system configure.
2. The size of partition will be equal to incoming process.
3. The partition size varies according to the need of the process so that the internal
fragmentation can be avoided to ensure efficient utilization of RAM.
4. Number of partitions in RAM is not fixed and depends on the number of incoming
process and Main Memory’s size.

Advantages of Variable Partitioning


1. No Internal Fragmentation
2. No restriction on Degree of Multiprogramming
3. No Limitation on the size of the process
Disadvantages of Variable Partitioning
1. Difficult Implementation
2. External Fragmentation
Fragmentation:
A fragmentation is defined as when the process is loaded and removed after execution from memory, it
creates a small free hole. Fragmentation is generally termed as the inability to use the available
memory.
There are two types of fragmentation:
 Internal Fragmentation:
o Internal fragmentation occurs when memory blocks are allocated to the process more
than their requested size. Due to this some unused space is leftover and creates and
internal fragmentation problem i.e. the memory that is internal to a partition but is of
no use.
 External Fragmentation:
o In external fragmentation, we have free memory blocks (holes), but we cannot assign it
to a process because blocks are not contiguous i.e. the memory is fragmented into large
number of small holes.

Example: As illustrated in above figure, first process is only consuming 1MB out of 4MB in main
memory.
Hence, internal fragmentation in first block is (4-1) = 3MB
Sum of Internal Fragmentation in every block = (4-1) + (8-7)+(8-7)+(16-14)
= 3+1+1+2 = 7MB
Suppose process P5 of size 7MB comes. But this process cannot be accommodated in spite of available
free space because of contiguous allocation.
Hence, 7 MB becomes part of external fragmentation.

1. Paging: Paging is a memory management technique that divides physical memory into fixed-size
pages and virtual memory into pages of the same size. The operating system loads pages of a
process into physical memory as needed, allowing the process to access a larger virtual address
space than is available in physical memory.
2. Segmentation: Segmentation is another memory management technique that divides memory
into segments or sections of varying sizes, which are used to store different parts of a program or
process.
3. Swapping: Swapping is a memory management technique that temporarily removes a process
from memory and stores it on disk, freeing up physical memory for other processes. When the
process needs to run again, it is swapped back into memory.
4. Fragmentation: Fragmentation occurs when memory becomes fragmented, or broken into small
pieces, making it difficult to allocate contiguous blocks of memory for processes. This can be
addressed by defragmenting memory or using dynamic memory allocation.
5. Caching: Caching is a memory management technique that stores frequently accessed data in
fast memory (cache) to reduce the number of times the data needs to be retrieved from slower
memory (such as RAM or disk).
6. Garbage Collection: Garbage collection is a memory management technique used in higher-
level programming languages to automatically deallocate memory that is no longer in use by a
program or process. This helps to prevent memory leaks and ensures that memory is used
efficiently.

Paging
 Paging is a memory management scheme that eliminates the need for contiguous allocation of
physical memory. In paging, secondary memory and main memory are divided into equal fixed
size partitions.
The partitions of secondary memory are called as pages.
The partitions of main memory are called as frames.
Each process is divided into parts where size of each part is same as page size.
(Or)
 Divide logical memory (programs) into blocks of same size called pages. Divide physical memory
into equal sized partitions called frames (size is power of 2, between ½K and 8K). The size of the
page and frame must be equal

When a process is to be executed, its pages are loaded into any available memory frames from the disk.
Page table is used to map the page number to corresponding frame number. The page table contains
the base address of the page (frame) in memory. Page number will be used as index for the page table.
Whenever program is loaded into memory the corresponding frame number is updated to page table. In
the fig, page 0 is loaded into frame 1, page 1 is loaded into frame 4, page 2 is loaded into frame 3 and
page 3 is loaded into frame 7. The frame numbers are updates to page table. If a frame is allocated to
one process it cannot be allocated to another process.
Paging Model of Logical and Physical Memory

Example
 Let us consider the main memory size 16 Kb and Frame size is 1 KB therefore the main memory
will be divided into the collection of 16 frames of 1 KB each.
 There are 4 processes in the system that is P1, P2, P3 and P4 of 4 KB each. Each process is
divided into pages of 1 KB each so that one page can be stored in one frame.
 Initially, all the frames are empty therefore pages of the processes will get stored in the
contiguous way.
 Frames, pages and the mapping between the two is shown in the image below.
 Let us consider that, P2 and P4 are moved to waiting state after some time. Now, 8 frames
becomeemptyandthereforeotherpagescanbe loaded inthat emptyplace. TheprocessP5of size 8
KB (8 pages) is waiting inside the ready queue.
 Given the fact that, we have 8 non contiguous frames available in the memoryand paging
providesthe flexibilityofstoringtheprocessatthedifferent places.Therefore,wecanloadthe pages
of process P5 in the place of P2 and P4.

Conversion of Logical Address into Physical Address

In order to execute any instructions the CPU always generates a Logical Address. But, the Physical
Address is needed to access the main memory.
The page number is used as index for the page table. The page number is replaced with corresponding
frame number
The Logical Address generated by the CPU has two parts:
1. Page Number (p) - It is the number of bits required to represent the pages in the Logical Address
Space. It is used as an index in a page table that contains the base address of a page in the
physical memory.
2. Page Offset (d) - It denotes the page size or the number of bits required to represent a word on
a page. It is combined with the Page Number to get the Physical Address.
The Physical Address also consists of two parts:
1. Frame Number (f) - It is the number of bits required to represent a frame in the Physical Address
Space. It is the location of the required page inside the Main Memory.
2. Frame Offset (d) - It is the page size or the number of bits required to represent a word in a
frame. It is equal to the Page Offset.
Page Table

Def: - Page Table is a data structure used by the virtual memory system to store the mapping
between logical addresses and physical addresses.

Page table accepts the inputs of page numbers as that produce the output of the frame numbers

The Page Table contains the base address of each page inside the Physical Memory. It is then combined
with Page Offset to get the actual address of the required data in the main memory.

The Page Number is used as the index of the Page Table which contains the base address which is the
Frame Number. Page offset is then used to retrieve the required data from the main memory.

Example of Paging
 If Logical Address = 2 bits, then Logical Address Space = 22 words and vice versa.

 If Physical Address = 4 bits, then Physical Address Space = 24 words and vice versa.

 Now, consider an example:

 Let Page Size = 1 bit, Logical Address = 2 bits, Physical Address= 4 bits.

 Frame Size = Page Size = 21 = 2 Words. It means every page will contain 2 words.

 Number of Pages = Logical Address Space / Page Size =22 / 21 = 2

 Number of Frames =Physical Address Space/ Frame Size = 24 / 21 = 8

Advantages of Paging
 It is one of the easiest Memory Management Algorithms.
 Paging helps in storing a process at non-contagious locations in the main memory.
 Paging removes the problem of External Fragmentation.
Disadvantages of Paging
 It may cause Internal Fragmentation.
 More Memory is consumed by the Page Tables.
 It faces a longer memory lookup time.

Structure of Page Tables:


Def: -
 Page Table is a data structure used by the virtual memory system to store the mapping between
logical addresses and physical addresses.
 Page table accepts the inputs of page numbers as that produce the output of the frame numbers
 There are three types of tables
1. Hierarchical Page Table
2. Hashed Page Table
3. Inverted Page Table
1. Hierarchical Page Table:
 Break up the logical address space into multiple page tables
 A simple technique is a two-level page table.
 Most modern computer systems support a large logical address space (2 32 or 264).
 In such case page table it becomes excessively large. For example, consider a system with a 32-
bit logical address space; if page size in such a system is 4KB (212), then the page table consists
of one million entries.
 Assume each entry consists of 4 bytes; each process may need up to 4MB of physical address
space for the page table alone.
 Clearly, we would not want to allocate the page table contiguously in main memory. One simple
solution to this problem is to divide the page table into smaller pieces.
Two level Paging algorithm:
For a 32-bit machine with a page size of 4KB, a logical address is divided into page number consisting of
20 bits and a page offset consisting of 12 bits. The page number is further divided into a 10-bit page
number and a 10-bit page offset. Thus, a logical address is as follows:

A Two-level page level page –scheme


So we need to divide the page table this division of page table can be accomplished in several ways. You
can perform two-level division, three-level division on page table and so on
Let us discuss two-level paging in which a page table itself is paged. So we will have two-page tables’
inner page table and outer page table. We have a logical address with page number 20 and page offset 12.
As we are paging a page table the page number will further get split to 10-bit page number and 10 bit
offset as you can see in the image below.
Where p1 is an index into the outer page table and p2 is the displacement within the page of outer page
table. This scheme is also known as forward-mapped page table.
The address translation scheme is mentioned
Fig:Address translation for a two level 32 bit paging architecture.
2.Hashed Page table:
A common approach for handling address spaces larger than 32 bits is to use a Hashed Page Table with a
hash value being the virtual page number. Each entry in the hash table consists a linked list of elements
that hash to the same location (Collisions). Each element consists of three fields (1)The virtual page
number, (2) the value of mapped page frame, (3) A pointer to the next element in the linked list. The
algorithm works as follows: The virtual page numbering the virtual page address is hashed into the hash
table. The virtual page number is compared with the field 1 in the first element in the linked list. If there
is a match, the corresponding page frame (field2) is used to form desired physical address. If there is no
match, subsequent entries in the linked list are searched for a matching value page number.

3. Inverted Page Table

 The concept of normal paging says that every process maintains its own page table which
includes the entries of all the pages belonging to the process. The large process may have a page
table with millions of entries. Such a page table consumes a large amount of memory.
 Consider we have six processes in execution. So, six processes will have some or the other of
their page in the main memory which would compel their page tables also to be in the main
memory consuming a lot of space. This is the drawback of the paging concept.
 The inverted page table is the solution to this wastage of memory. The concept of an inverted
page table involves the existence of single-page table which has entries of all the pages (may they
belong to different processes) in main memory along with the information of the process to which
they are associated. To get a better understanding consider the figure below of inverted page
table.
 Segmentation:
 Segmentation is a memory-management scheme that supports this user view of memory. The
user's view is mapped into the Physical Memory.
 In which memory and process are divide into variable size portions
 In Segmentation, a process is divided into multiple segments. The size of each segment is not
necessarily the same which is different from paging. In Paging, a process was divided into equal
partitions called pages. The module contained in a segment decides the size of the segment.
 The partitions of secondary memory area units are known as segments.
 The details concerning every segment are stored in a table known as segmentation table.
 Segment table contains two main data concerning segment, one is Base and Limit
 Base:- which is the bottom address of the segment and
 Limit:-which is the length of the segment.

Why Segmentation is used?


 The purpose of segmentation is to overcome the paging. Paging is a method or technique which
is used for non-contiguous memory allocation. It is a fixed-size partitioning theme (scheme). In
paging, both main memory and secondary memory are divided into equal fixed-size partitions.
 In segmentation, the process isn’t divided indiscriminately into mounted(fixed) size pages. It is a
variable-size partitioning theme. in segmentation, secondary and main memory are not divided
into partitions of equal size.
Translation of a logical address to a physical address.
In segmentation, the CPU generates a logical address that contains the Segment number and
segment offset. If the segment offset is a smaller amount than the limit then the address called valid
address otherwise it throws miscalculation because the address is invalid.
The CPU generates the logical address which consists of two parts:
1. Segment Number(s) - It is the number of bits required to represent a Segment. It is used as an
index in the Segment Table.
2. Segment Offset(d) - It is the number of bits required to represent the size of a Segment.
3.
Example of Segmentation in OS
Consider that a user program has been divided into five segments and they are numbered from
segment 0 to segment 4, as you can see them in the logical address space. You also have a segment
table which has entries for this segment with their base address in physical memory and their limit.

Now suppose the CPU calls for segment number 2 which is 400 bytes long and it resides at 4300
memory location. The CPU wants to refer the 53rd byte of segment 2. So, here the input we get from
CPU is segment number 2 and 53 as an offset.
Now the offset is in between 0 and limit of the segment 2 i.e. 400. So, the condition got verified and
the offset is being added to the base address of the segment 2 to reach the 53rd byte of the segment
2 in physical memory. In case you try to access the 453rd byte of segment 2 then it would result in a
trap to the operating system as offset value 453 is greater than the limit of segment 2.
Difference between Paging and Segmentation
Sr. No. Key Paging Segmentation

In Paging, a process In Segmentation, a process


address space is broken address space is broken in
1 Memory Size
into fixed sized blocks varying sized blocks called
called pages. sections.

Compiler is responsible to
Operating System divides calculate the segment size,
2 Accountability
the memory into pages. the virtual address and actual
address.

Page size is determined by Section size is determined by


3 Size
available memory. the user.

Paging technique is faster


Segmentation is slower than
4 Speed in terms of memory
paging.
access.

Paging can cause internal Segmentation can cause


fragmentation as some external fragmentation as
5 Fragmentation
pages may go some memory block may not
underutilized. be used at all.

During paging, a logical


During segmentation, a logical
Logical address is divided into
6 address is divided into section
Address page number and page
number and section offset.
offset.

During paging, a logical


During segmentation, a logical
address is divided into
7 Table address is divided into section
page number and page
number and section offset.
offset.

Page table stores the page Segmentation table stores the


8 Data Storage
data. segmentation data.
Advantages of Segmentation
1. Segmentation is more close to the programmer’s view of physical memory.
2. Segmentation prevents internal fragmentation.
3. Segmentation prevents the CPU overhead as the segment contains an entire module of at once.
Disadvantages of Segmentation
1. The segmentation leads to external fragmentation.

 Swapping
Swapping in OS is a memory management technique that temporarily swaps processes from main
memory to secondary memory or vice versa which helps to increase the degree of multi-
programming and increase main memory utilization.
There are two steps in the process of Swapping in the operating system:
1. Swap In: Swap-in is the process of bringing a process from secondary storage/hard disc to main
memory (RAM).
2. Swap Out: Swap-out takes the process out of the Main memory and puts it in the secondary
storage.
In the above diagram, process P1 is swap out so the process with more memory requirement or
higher priority will be executed to increase the overall efficiency of the operating system. While the
process P2 is swap in for its execution from the secondary memory to the main memory(RAM).
 Example
Let's understand this with the help of an example. The process size of the user's process is 4096KB,
and the transfer rate is 1 Mbps. Now we'll find out how long it will take to move from main memory
to secondary memory.
User process size = 4096KB
Data transfer Rate = 1024 Kbps
Time = User process size / Data transfer Rate
4096 / 1024 = 4 seconds
4000 milliseconds
The process will now take 4000 milliseconds.
 Advantages of Swapping in OS

 Swapping processes improve the degree of multi-programming.


 It provides the advantages of Virtual Memory for the user.
 Swapping reduces the average waiting time for the process because it allows multiple processes to
execute simultaneously.
 It helps in better memory management and efficient utilization of RAM.

 Disadvantages of Swapping in OS

 The swapping algorithm must be perfect; otherwise, the number of Page Faults will increase, and
performance will decrease.
 Inefficiency will occur when there are common/shared resources between many processes.
 The user will lose information if there is heavy swapping and the computer loses its power.

 Virtual Memory Management


 Virtual memory involves separation of logical memory from physical memory. This
separation allows an extremely large virtual memory to be provided for programmers when
only smaller memory is available. The programmer need not concentrate on the amount of
physical memory.
 A computer can address more memory than the amount physically installed on the system.
This extra memory is actually called virtual memory and it is a section of a hard disk that's
set up to emulate the computer's RAM.
 The main visible advantage of this scheme is that programs can be larger than physical
memory. Virtual memory serves two purposes. First, it allows us to extend the use of
physical memory by using disk. Second, it allows us to have memory protection, because
each virtual address is translated to a physical address.

How Virtual Memory Works?

 Virtual Memory is a technique that is implemented using both hardware and software. It maps
memory addresses used by a program, called virtual addresses, into physical addresses in
computer memory.
 All memory references within a process are logical addresses that are dynamically translated
into physical addresses at run time. This means that a process can be swapped in and out of the
main memory such that it occupies different places in the main memory at different times
during the course of execution.
 A process may be broken into a number of pieces and these pieces need not be continuously
located in the main memory during execution. The combination of dynamic run-time address
translation and the use of a page or segment table permit this.
If these characteristics are present then, it is not necessary that all the pages or segments are
present in the main memory during execution. This means that the required pages need to be
loaded into memory whenever required. Virtual memory is implemented using Demand Paging or
Demand Segmentation.

 Demand Paging
A demand paging system is quite similar to a paging system with swapping where processes reside in
secondary memory and pages are loaded only on demand, not in advance. When a context switch occurs,
the operating system does not copy any of the old program’s pages out to the disk or any of the new
program’s pages into the main memory Instead, it just begins executing the new program after loading the
first page and fetches that program’s pages as they are referenced.
While executing a program, if the program references a page which is not available in the main memory
because it was swapped out a little ago, the processor treats this invalid memory reference as a page
fault and transfers control from the program to the operating system to demand the page back into the
memory.

Advantages

Following are the advantages of Demand Paging −

 Large virtual memory.


 More efficient use of memory.
 There is no limit on degree of multiprogramming.
Disadvantages
 Number of tables and the amount of processor overhead for handling page interrupts are greater than in
the case of the simple paged management techniques
Page Fault in OS:-
 When the page referenced by the CPU is not found in the main memory then the situation is termed as
Page Fault.
 Whenever any page fault occurs, then the required page has to be fetched from the secondary memory into
the main memory.

 Page Replacement Algorithms


What is Page Replacement Algorithm and why it is needed?

In paging, processes are divided into pages and main memory is divided into frames. Pages of a process are loaded
into frames of main memory when required.

Page Replacement Algorithm is used when a page fault occurs.

Page Fault means the page referenced by the CPU is not present in the main memory.
When the CPU generates the reference of a page, if there is any vacant frame available in the main memory then
the page is loaded in that vacant frame. In another case, if there is no vacant frame available in the main memory,
it is required to replace one of the pages in the main memory with the page referenced by the CPU.

Page Replacement Algorithm is used to decide which page will be replaced to allocate memory to the current
referenced page.

Page Replacement Algorithms

First In First Out (FIFO) -This algorithm is similar to the operations of the queue. All the pages are stored in the
queue in the order they are allocated frames in the main memory. The one which is allocated first stays in the
front of the queue. The one which is allocated the memory first is replaced first. The one which is at the front of
the queue is removed at the time of replacement.

Example: Consider the Pages referenced by the CPU in the order are 6, 7, 8, 9, 6, 7, 1, 6, 7, 8, 9, 1

 As in the above figure shown, Let there are 3 frames in the memory.
 6, 7, 8 are allocated to the vacant slots as they are not in memory.
 When 9 comes page fault occurs, it replaces 6 which is the oldest in memory or front element of the
queue.
 Then 6 comes (Page Fault), it replaces 7 which is the oldest page in memory now.
 Similarly, 7 replaces 8, 1 replaces 9.
 Then 6 comes which is already in memory (Page Hit).
 Then 7 comes (Page Hit).
 Then 8 replaces 6, 9 replaces 7. Then 1 comes (Page Hit).
Number of Page Faults = 9
 While using the First In First Out algorithm, the number of page faults increases by increasing the number
of frames. This phenomenon is called Belady's Anomaly.
 Let's take the same above order of pages with 4 frames.
 In the above picture shown, it can be seen that the number of page faults is 10.
 There were 9 page faults with 3 frames and 10 page faults with 4 frames.
 The number of page faults increased by increasing the number of frames.

Optimal Page Replacement - In this algorithm, the page which would be used after the longest interval is replaced.
In other words, the page which is farthest to come in the upcoming sequence is replaced.
Example: Consider the Pages referenced by the CPU in the order are 6, 7, 8, 9, 6, 7, 1, 6, 7, 8, 9, 1, 7, 9, 6

 First, all the frames are empty. 6, 7, 8 are allocated to the frames (Page Fault).
 Now, 9 comes and replaces 8 as it is the farthest in the upcoming sequence. 6 and 7 would come earlier
than that so not replaced.
 Then, 6 comes which is already present (Page Hit).
 Then 7 comes (Page Hit).
 Then 1 replaces 9 similarly (Page Fault).
 Then 6 comes (Page Hit), 7 comes (Page Hit).
 Then 8 replaces 6 (Page Fault) and 9 replaces 8 (Page Fault).
 Then 1, 7, 9 come respectively which are already present in the memory.
 Then 6 replaces 9 (Page Fault), it can also replace 7 and 1 as no other page is present in the upcoming
sequence.
The number of Page Faults = 8
 This is the most optimal algorithm but is impractical because it is impossible to predict the upcoming page
references.

Least Recently Used - This algorithm works on previous data. The page which is used the earliest is replaced or
which appears the earliest in the sequence is replaced.
Example: Consider the Pages referenced by the CPU in the order are 6, 7, 8, 9, 6, 7, 1, 6, 7, 8, 9, 1, 7, 9, 6
 First, all the frames are empty. 6, 7, 8 are allocated to the frames (Page Fault).
 Now, 9 comes and replaces 6 which is used the earliest (Page Fault).
 Then, 6 replaces 7, 7 replaces 8, 1 replaces 9 (Page Fault).
 Then 6 comes which is already present (Page Hit).
 Then 7 comes (Page Hit).
 Then 8 replaces 1, 9 replaces 6, 1 replaces 7, and 7 replaces 8 (Page Fault).
 Then 9 comes (Page Hit).
 Then 6 replaces 1 (Page Fault).
The number of Page Faults = 12

 Copy on Write
 Copy-on-Write(CoW) is mainly a resource management technique that allows the parent and child
process to share the same pages of the memory initially. If any process either parent or child modifies the
shared page, only then the page is copied.
 The CoW is basically a technique of efficiently copying the data resources in the computer system. In this
case, if a unit of data is copied but is not modified then "copy" can mainly exist as a reference to the
original data.
 But when the copied data is modified, then at that time its copy is created(where new bytes are actually
written )as suggested from the name of the technique.
 The main use of this technique is in the implementation of the fork system call in which it shares the virtual
memory/pages of the Operating system.
 Recall in the UNIX(OS), the fork() system call is used to create a duplicate process of the parent
process which is known as the child process.
 The CoW technique is used by several Operating systems like Linux, Solaris, and Windows XP.
 The CoW technique is an efficient process creation technique as only the pages that are modified
are copied.

Free pages in this technique are allocated from a pool of zeroed-out pages.
The Copy on Write(CoW) Technique
The main intention behind the CoW technique is that whenever a parent process creates a child process both parent
and child process initially will share the same pages in the memory.

 These shared pages between parent and child process will be marked as copy-on-write which means that if
the parent or child process will attempt to modify the shared pages then a copy of these pages will be
created and the modifications will be done only on the copy of pages by that process and it will not affect
other processes.

Now its time to take a look at the basic example of this technique:
Let us take an example where Process A creates a new process that is Process B, initially both these processes will
share the same pages of the memory.
Figure: Above figure indicates parent and child process sharing the same pages
Now, let us assume that process A wants to modify a page in the memory. When the Copy-on-write(CoW) technique
is used, only those pages that are modified by either process are copied; all the unmodified pages can be easily
shared by the parent and child process.

Figure: After Page Z is modified by Process A


Whenever it is determined that a page is going to be duplicated using the copy-on-write technique, then it is
important to note the location from where the free pages will be allocated. There is a pool of free pages for such
requests; provided by many operating systems.

 Allocation of Frames in OS
 The main memory of the operating system is divided into various frames.
 The process is stored in these frames, and once the process is saved as a frame, the CPU may run it.
 As a result, the operating system must set aside enough frames for each process.
 As a result, the operating system uses various algorithms in order to assign the frame.
There are mainly five ways of frame allocation algorithms in the OS. These are as follows:

1. Equal Frame Allocation


2. Proportional Frame Allocation
3. Priority Frame Allocation
4. Global Replacement Allocation
5. Local Replacement Allocation

1. Equal Frame Allocation:


In equal frame allocation, the processes are assigned equally among the processes in the OS. For example, if the
system has 30 frames and 7 processes, each process will get 4 frames. The 2 frames that are not assigned to any
system process may be used as a free-frame buffer pool in the system.
Disadvantage
In a system with processes of varying sizes, assigning equal frames to each process makes little sense. Many allotted
empty frames will be wasted if many frames are assigned to a small task.
2. Proportional Frame Allocation

The proportional frame allocation technique assigns frames based on the size needed for execution and the total
number of frames in memory.

The allocated frames for a process pi of size si are ai = (si/S)*m, in which S represents the total of all process sizes,
and m represents the number of frames in the system.
Disadvantage
The only drawback of this algorithm is that it doesn't allocate frames based on priority. Priority frame allocation solves
this problem.
3. Priority Frame Allocation
Priority frame allocation assigns frames based on the number of frame allocations and the processes. Suppose a
process has a high priority and requires more frames that many frames will be allocated to it. Following that, lesser
priority processes are allocated.
4. Global Replacement Allocation
When a process requires a page that isn't currently in memory, it may put it in and select a frame from the all frames
sets, even if another process is already utilizing that frame. In other words, one process may take a frame from
another.
Advantages
Process performance is not hampered, resulting in higher system throughput.
Disadvantages
The process itself may not solely control the page fault ratio of a process. The paging behavior of other processes also
influences the number of pages in memory for a process.
5. Local Replacement Allocation
When a process requires a page that isn't already in memory, it can bring it in and assign it a frame from its set of
allocated frames.
Advantages
The paging behavior of a specific process has an effect on the pages in memory and the page fault ratio.
Disadvantages
A low priority process may obstruct a high priority process by refusing to share its frames.
Global Vs. Local Replacement Allocation

The number of frames assigned to a process does not change using a local replacement strategy. On the other hand,
using global replacement, a process can choose only frames granted to other processes and enhance the number of
frames allocated.
Thrashing
Thrashing in an operating system (OS) is when a system spends too much time swapping pages instead
of doing useful work. This can lead to poor performance, such as slow response times and decreased
system efficiency.
Causes of thrashing:
 High degree of multiprogramming.
 Lack of frames.
 Page replacement policy.
Programs or workloads may cause thrashing, and it results in severe performance problems, such as:
 If CPU utilization is too low, we increase the degree of multiprogramming by introducing a new
system. A global page replacement algorithm is used. The CPU scheduler sees the decreasing
CPU utilization and increases the degree of multiprogramming.
 CPU utilization is plotted against the degree of multiprogramming.
 As the degree of multiprogramming increases, CPU utilization also increases.
 If the degree of multiprogramming is increased further, thrashing sets in, and CPU utilization
drops sharply.

 So, at this point, to increase CPU utilization and to stop thrashing, we must decrease the degree
of multiprogramming.
Techniques to Prevent Thrashing
The Local Page replacement is better than the Global Page replacement, but local page replacement has many
disadvantages, so it is sometimes not helpful. Therefore below are some other techniques that are used to handle
thrashing:
1. Locality Model
A locality is a set of pages that are actively used together. The locality model states that as a process executes, it
moves from one locality to another. Thus, a program is generally composed of several different localities which may
overlap.
For example, when a function is called, it defines a new locality where memory references are made to the function
call instructions, local and global variables, etc. Similarly, when the function is exited, the process leaves this locality.
2. Working-Set Model
This model is based on the above-stated concept of the Locality Model.
The basic principle states that if we allocate enough frames to a process to accommodate its current locality, it will
only fault whenever it moves to some new locality. But if the allocated frames are lesser than the size of the current
locality, the process is bound to thrash.
According to this model, based on parameter A, the working set is defined as the set of pages in the most recent 'A'
page references. Hence, all the actively used pages would always end up being a part of the working set.
3. Page Fault Frequency
A more direct approach to handle thrashing is the one that uses the Page-Fault Frequency concept.

 The problem associated with thrashing is the high page fault rate, and thus, the concept here is to control
the page fault rate.
 If the page fault rate is too high, it indicates that the process has too few frames allocated to it. On the
contrary, a low page fault rate indicates that the process has too many frames.
 Upper and lower limits can be established on the desired page fault rate, as shown in the diagram.
 If the page fault rate falls below the lower limit, frames can be removed from the process. Similarly, if the
page faults rate exceeds the upper limit, more frames can be allocated to the process.
 In other words, the graphical state of the system should be kept limited to the rectangular region formed in
the given diagram.
Storage Management: Overview of Mass-Storage Structure
Mass Storage System

• A storage device is any device used in a computer to store information. It will retain this information
when the computer is switched off.
• Type of storage device used varies based on the type of data and the rate at which it is created and
used.
• Examples of storage devices are hard disk, CD-ROM, magnetic tape and removable media etc.
• The computer has many types of data storage devices. Some of them can be classified as the
removable data storage devices and the others as the non removable data storage devices.

Magnetic Disks
 Traditional magnetic disks have the following basic structure:
o One or more platters in the form of disks covered with magnetic media. Hard
disk platters are made of rigid metal, while "floppy" disks are made of more flexible
plastic.
o Each platter has two working surfaces. Older hard disk drives would sometimes not use
the very top or bottom surface of a stack of platters, as these surfaces were more
susceptible to potential damage.
o Each working surface is divided into a number of concentric rings called tracks. The
collection of all tracks that are the same distance from the edge of the platter, ( i.e. all
tracks immediately above one another in the following diagram ) is called a cylinder.
o Each track is further divided into sectors, traditionally containing 512 bytes of data each,
although some modern disks occasionally use larger sector sizes. ( Sectors also include a
header and a trailer, including checksum information among other things. Larger sector
sizes reduce the fraction of the disk consumed by headers and trailers, but increase internal
fragmentation and the amount of disk that must be marked bad in the case of errors. )
o The data on a hard drive is read by read-write heads. The standard configuration ( shown
below ) uses one head per surface, each on a separate arm, and controlled by a
common arm assembly which moves all heads simultaneously from one cylinder to
another. ( Other configurations, including independent read-write heads, may speed up disk
access, but involve serious technical difficulties. )
o The storage capacity of a traditional disk drive is equal to the number of heads ( i.e. the
number of working surfaces ), times the number of tracks per surface, times the number of
sectors per track, times the number of bytes per sector. A particular physical block of data
is specified by providing the head-sector-cylinder number at which it is located.
HDD SCHEDULING (Disk scheduling algorithms)
Types of Disk Scheduling Algorithm in OS

1. FCFS (First Come First Serve)

FCFS is the simplest of all Disk Scheduling Algorithms. In FCFS, the requests are addressed in the order they
arrive in the disk queue. Let us understand this with the help of an example.

First Come First Serve

Example:
Suppose the order of request is- (82,170,43,140,24,16,190)
And current position of Read/Write head is: 50
So, total overhead movement (total distance covered by the disk arm) =
(82-50)+(170-82)+(170-43)+(140-43)+(140-24)+(24-16)+(190-16) =642
Advantages of FCFS
Here are some of the advantages of First Come First Serve.
 Every request gets a fair chance
 No indefinite postponement
Disadvantages of FCFS
Here are some of the disadvantages of First Come First Serve.
 Does not try to optimize seek time
 May not provide the best possible service
2. SSTF (Shortest Seek Time First)
In SSTF (Shortest Seek Time First), requests having the shortest seek time are executed first. So, the seek
time of every request is calculated in advance in the queue and then they are scheduled according to their
calculated seek time. As a result, the request near the disk arm will get executed first. SSTF is certainly an
improvement over FCFS as it decreases the average response time and increases the throughput of the
system. Let us understand this with the help of an example.
Example:
Suppose the order of request is- (82,170,43,140,24,16,190)
And current position of Read/Write head is: 50

Shortest Seek Time First


total overhead movement (total distance covered by the disk arm) =
(50-43)+(43-24)+(24-16)+(82-16)+(140-82)+(170-140)+(190-170) =208
Advantages of Shortest Seek Time First
Here are some of the advantages of Shortest Seek Time First.
 The average Response Time decreases
 Throughput increases
Disadvantages of Shortest Seek Time First
Here are some of the disadvantages of Shortest Seek Time First.
 Overhead to calculate seek time in advance
 Can cause Starvation for a request if it has a higher seek time as compared to incoming requests
 The high variance of response time as SSTF favors only some requests
3. SCAN
In the SCAN algorithm the disk arm moves in a particular direction and services the requests coming in its
path and after reaching the end of the disk, it reverses its direction and again services the request arriving
in its path. So, this algorithm works as an elevator and is hence also known as an elevator algorithm. As a
result, the requests at the midrange are serviced more and those arriving behind the disk arm will have to
wait.
Example:

SCAN Algorithm
Suppose the requests to be addressed are-82,170,43,140,24,16,190. And the Read/Write arm is at 50, and
it is also given that the disk arm should move “towards the larger value”.
Therefore, the total overhead movement (total distance covered by the disk arm) is calculated as
= (199-50) + (199-16) = 332
Advantages of SCAN Algorithm
Here are some of the advantages of the SCAN Algorithm.
 High throughput
 Low variance of response time
 Average response time
Disadvantages of SCAN Algorithm
Here are some of the disadvantages of the SCAN Algorithm.
 Long waiting time for requests for locations just visited by disk arm
4. C-SCAN
In the SCAN algorithm, the disk arm again scans the path that has been scanned, after reversing its
direction. So, it may be possible that too many requests are waiting at the other end or there may be zero
or few requests pending at the scanned area.
These situations are avoided in the CSCAN algorithm in which the disk arm instead of reversing its direction
goes to the other end of the disk and starts servicing the requests from there. So, the disk arm moves in a
circular fashion and this algorithm is also similar to the SCAN algorithm hence it is known as C-SCAN
(Circular SCAN).
Example:

Circular SCAN
Suppose the requests to be addressed are-82,170,43,140,24,16,190. And the Read/Write arm is at 50, and
it is also given that the disk arm should move “towards the larger value”.
So, the total overhead movement (total distance covered by the disk arm) is calculated as:
=(199-50) + (199-0) + (43-0) = 391
Advantages of C-SCAN Algorithm
Here are some of the advantages of C-SCAN.
 Provides more uniform wait time compared to SCAN.
5. LOOK
LOOK Algorithm is similar to the SCAN disk scheduling algorithm except for the difference that the disk arm
in spite of going to the end of the disk goes only to the last request to be serviced in front of the head and
then reverses its direction from there only. Thus it prevents the extra delay which occurred due to
unnecessary traversal to the end of the disk.
Example:

LOOK Algorithm
Suppose the requests to be addressed are-82,170,43,140,24,16,190. And the Read/Write arm is at 50, and
it is also given that the disk arm should move “towards the larger value”.
So, the total overhead movement (total distance covered by the disk arm) is calculated as:
= (190-50) + (190-16) = 314
6. C-LOOK
As LOOK is similar to the SCAN algorithm, in a similar way, C-LOOK is similar to the CSCAN disk scheduling
algorithm. In CLOOK, the disk arm in spite of going to the end goes only to the last request to be serviced in
front of the head and then from there goes to the other end’s last request. Thus, it also prevents the extra
delay which occurred due to unnecessary traversal to the end of the disk.
Example:
1. Suppose the requests to be addressed are-82,170,43,140,24,16,190. And the Read/Write arm is at
50, and it is also given that the disk arm should move “towards the larger value”

C-LOOK
So, the total overhead movement (total distance covered by the disk arm) is calculated as
= (190-50) + (190-16) + (43-16) = 341

What is RAID?

RAID (redundant array of independent disks) is a way of storing the same data in different places on
multiple hard disks or solid-state drives (SSDs) to protect data in the case of a drive failure. There are
different RAID levels, however, and not all have the goal of providing redundancy.

How RAID works

RAID works by placing data on multiple disks and allowing input/output (I/O) operations to overlap in a
balanced way, improving performance. Because using multiple disks increases the mean time between
failures, storing data redundantly also increases fault tolerance.

RAID arrays appear to the operating system (OS) as a single logical drive.

You might also like