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

Operating Systems

Uploaded by

laluyazeed8
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)
21 views

Operating Systems

Uploaded by

laluyazeed8
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/ 495

OPERATING SYSTEM

BITS Pilani
Pilani Campus
BITS Pilani
Pilani Campus

INTRODUCTION TO OPERATING
SYSTEM
Learning Objectives
 Definition of OS
 Components of Computer System
 Mainframe Systems
 Computer System operation
 Single and Multi processor System
 Components of Operating System

3
Lecture 1 BITS Pilani, Pilani Campus
OPERATING SYSTEM -
DEFINITION
An operating system is a program that manages a computer’s
hardware. It also provides a basis for application programs and
acts as an intermediary between the computer user and the
computer hardware. An amazing aspect of operating systems
is how they vary in accomplishing these tasks. Mainframe
operating systems are designed primarily to optimize
utilization of hardware. Personal computer (PC) operating
systems support complex games, business applications, and
everything in between. Operating systems for mobile
Computers provide an environment in which a user can easily
interface with the computer to execute programs. Thus, some
operating systems are designed to be convenient, others to be
efficient, and others to be some combination of the two.

4
Lecture I BITS Pilani, Pilani Campus
Components of Computer
System
Before we can explore the details of computer system
operation, we need to know something about system structure.
We thus discuss the basic functions of system startup, I/O, and
storage early in this chapter. We also describe the basic
computer architecture that makes it possible to write a
functional operating system. Because an operating system is
large and complex, it must be created piece by piece. Each of
these pieces should be a well-delineated portion of the
system, with carefully defined inputs, outputs, and functions
A computer system has four components. They are
1. Hardware 3. Users
2. Application Programs 4. Operating System
5
Lecture I BITS Pilani, Pilani Campus
Components of Computer
System

User1 User 2 .......... User n

Compiler Assembler database systems

System and Application Programs

Operating System

Computer Hardware

6
Lecture I BITS Pilani, Pilani Campus
Components of OS

Application Programs - These are the softwares such as


Word Processors, spreadsheets, Compilers and Web Browsers
to be used to solve the computing problems of the user
Users – They are the people who are using the computer
system to solve the problems
Operating System – It is a system software that controls and
coordinates the use of the hardware among the various
application programs of the various users. An operating
system is similar to a government. Like a government, it
performs no useful function by itself. It simply provides an
environment within which other programs can do useful work.

7
Lecture I BITS Pilani, Pilani Campus
Components of Computer
System
The view of the computer varies from users accordingly to
the
interface provided to them. Most of the users sit in front of
the
computer and execute the programs with the help of
keyboard, mouse , System unit and Monitor. In some cases,
users are provided with the GUI [ Graphical User Interface ]
feature to perform their tasks easily.

8
Lecture I BITS Pilani, Pilani Campus
MAINFRAME SYSTEMS

Mainframe computers are the computers which are used to


solve Commercial and Scientific applications. Growth of the
computer systems ranges from batch systems to time –shared
systems.
1.Batch Systems
Early computers were bigger machine with the input devices ,
card readers, tape drives and the common output devices
such as line printers and card punches. The users could not
interact wit the computer system directly. Instead , the user
prepared a job [ consisted of program, data and control
information] and submitted to the computer for the execution .

9
Lecture I BITS Pilani, Pilani Campus
MAINFRAME SYSTEMS

The jobs were entered in the punched cards.


The operating systems were simple and their tasks were to
transfer control automatically fro job to another. They resided
in the memory as

Operating
System

User Program
area

MEMORY LAY OUT


10
Lecture I BITS Pilani, Pilani Campus
MAINFRAME SYSTEMS

To speed up the processing, the similar jobs were batched


together and were executed. In this type of systems, the CPU is
often idle because the I/O devices are slower than memory
speed.
2.Multi programmed Systems
The most important aspect of job scheduling is the ability of
multi programming. Multi programming increases the CPU
utilization by organizing the jobs in a such a way that CPU
executed them efficiently.
In multi programmed systems, the operating system keeps
several jobs in the memory simultaneously as follow:
The jobs that are kept in the memory are the jobs to be
11
Lecture I BITS Pilani, Pilani Campus
MAINFRAME SYSTEMS

executed and were kept in the job pool. The operating system
picks and executed one of the jobs in the memory . When a
job waits for some task such waiting for an input, the operating
system switches to other job. If this job needs some wait, the
CPU is switched to another job, and so on. As long as one job
needs to be executed, the CPU is never idle.
If many jobs are waiting for the execution and are ready to be
brought into memory for the execution, the operating system
chooses one among them. This decision is called as Job
Scheduling.

12
Lecture I BITS Pilani, Pilani Campus
MAINFRAME SYSTEMS

3.Time sharing systems


These are the extension of multi programming systems. Multi
programming environment use the resources effectively but
they do not provide user interaction with the computer system.
But in time sharing systems, the CPU executes multiple jobs by
switching among them but he switches occur frequently and
the user can interact with each program is running.
4. Desktop System
These are the personal computers. They are dedicated to
single user. They consists of key boards, mouse , display unit
and small printers. These are used for user conveniences. May
run on different types of operating systems (Windows, Unix ,
13
Lecture I BITS Pilani, Pilani Campus
MAINFRAME SYSTEMS
Linux, MacOs)
5. Distributed Systems
These are the systems that can distribute the computation
among several physical processors.
They are loosely coupled systems. Each processor has its own
local memory and processors communicate with one another
through various communication lines such as high speed
buses or telephone lines. The advantages are
- Resource Sharing
- Computation speed up
- Reliability
- Communication

14
Lecture I BITS Pilani, Pilani Campus
MAINFRAME SYSTEMS
(i) Client Server Model
Client-server architecture is a network model where every
process or computer on a network is a server or a client. The
client-servers are the robust computers that are dedicated to
managing the printers, disk drives, and network traffic. Clients
are workstations or PCs on which the users run their
applications. Clients mainly rely on the servers for resources,
like devices, files, and processing power.
A client-server architecture or model is an application network
separating tasks between the clients and servers that live in
the same system or have to communicate through a computer
network.

15
Lecture I BITS Pilani, Pilani Campus
OPERATING SYSTEM

16
Lecture I BITS Pilani, Pilani Campus
MAINFRAME SYSTEMS
(ii) Peer – to - Peer Model
A peer-to-peer (P2P) service is a decentralized platform
whereby two individuals interact directly with each other,
without intermediation by a third party. Instead, the buyer
and
the seller transact directly with each other via the P2P
service.
Benefits
- A peer-to-peer service is a platform that directly connects
parties to a transaction without the third-party
intermediary.
- Peer-to-peer platforms offer services such as payment
processing, information about buyers and sellers, and
quality assurance to their users.
17
Lecture I BITS Pilani, Pilani Campus
MAINFRAME SYSTEMS

18
Lecture I BITS Pilani, Pilani Campus
MAINFRAME SYSTEMS

6.Clustering systems
These are the systems supporting clustering. Clustering allows
two or more systems to share storage. They are either
Asymmetric Clustering or Symmetric Clustering.
In symmetric clustering, all ‘n’ hosts are running the
Application. In asymmetric clustering, all servers run the
application and one server acts as a stand by
Advantages
- High performance
- Fault tolerant because failure one node does not affect the
whole system
- Scalability is achieved because we can add new nodes
19
Lecture I BITS Pilani, Pilani Campus
MAINFRAME SYSTEMS

7. Real Time Systems


These are the systems which have time constraints. These
systems give the guarantee that the maximum time for critical
operations to complete on time. This type of system is called as
Hard Real Time system. But some of these systems give the
guarantee that the maximum time for critical task but no
assurance is given for completing them on time. These are
called as soft real time systems.
8.Hand held systems
They are usually smaller in size with small amount of memory,
slow processors and a small display screen. Hand held
systems are Personal Digital Assistants (PDAs) such as cellular
20
Lecture I BITS Pilani, Pilani Campus
MAINFRAME SYSTEMS

phones with connectivity to a network such as the internet.


They have between 512 KB to 8 MB memories. Some hand
held
systems use wireless technology such as Blue tooth allowing
remote access to e-mail and web browsing.

21
Lecture I BITS Pilani, Pilani Campus
Computer System operation

A modern general-purpose computer system consists of one


or more CPUs and a number of device controllers connected
through a common bus that provides access to shared
memory
(See the Figure ). Each device controller is in charge of a
specific type of device (for example, disk drives, audio
devices, or video displays). The CPU and the device
controllers can execute in parallel, competing for memory
cycles. To ensure orderly access to the shared memory, a
memory controller synchronizes access to the memory.
on-line

22
Lecture I BITS Pilani, Pilani Campus
Computer System operation

23
Lecture I BITS Pilani, Pilani Campus
Computer System operation

For a computer to start running — for instance, when it is


powered up or rebooted — it needs to have an initial
program
to run. This initial program, or bootstrap program, tends to
be
simple. Typically, it is stored within the computer hardware in
read-only memory (ROM) or electrically erasable
programmable read-only memory (EEPROM), known by the
general term firmware. It initializes all aspects of the system,
from CPU registers to device controllers to memory contents.
The bootstrap program must know how to load the
operating
system and how to start executing that system.
24
Lecture I BITS Pilani, Pilani Campus
Computer System operation

To accomplish this goal, the bootstrap program must locate


the operating-system kernel and load it into memory.Once the
kernel is loaded and executing, it can start providing services
to the system and its users. Some services are provided
outside of the kernel, by system programs that are loaded into
memory at boot time to become system processes, or
system daemons that run the entire time the kernel is
running. On UNIX, the first system process is “init,” and it starts
many other daemons. Once this phase is complete, the system
is fully booted, and the system waits for some event to occur.
The occurrence of an event is usually signaled by an interrupt
from either the hardware or the software. Hardware
25
Lecture I BITS Pilani, Pilani Campus
Computer System operation

may trigger an interrupt at any time by sending a signal to the


CPU, usually by way of the system bus. Software may trigger an
interrupt by executing a special operation called a system
call (also called a monitor call).
Storage Structure
The CPU can load instructions only from memory, so any
programs to run must be stored there. General-purpose
computers run most of their programs from rewritable
memory, called main memory (also called random-access
memory, or RAM). Main memory commonly is implemented
in a semiconductor technology called dynamic random-
access memory (DRAM).
26
Lecture I BITS Pilani, Pilani Campus
Computer System operation

Computers use other forms of memory as well. We have


already mentioned read-only memory, ROM) and electrically
erasable programmable read-only memory, EEPROM).
Because ROM cannot be changed, only static programs, such
as the bootstrap program described earlier, are stored there.
The immutability of ROM is of use in game cartridges.
EEPROM can be changed but cannot be changed frequently
and so contains mostly static programs. For example,
smartphones have EEPROM to store their factory-installed
programs.
All forms of memory provide an array of bytes. Each byte has
its own address. Interaction is achieved through a sequence of
27
Lecture I BITS Pilani, Pilani Campus
Computer System operation

load or store instructions to specific memory addresses. The


load instruction moves a byte or word from main memory to an
internal register within the CPU, whereas the store instruction
moves the content of a register to main memory. Aside from
explicit loads and stores, the CPU automatically loads
instructions from main memory for execution.
Thus, most computer systems provide secondary storage as
an extension of main memory. The main requirement for
secondary storage is that it be able to hold large quantities of
data permanently.
The most common secondary-storage device is a magnetic
disk, which provides storage for both programs and data. Most
28
Lecture I BITS Pilani, Pilani Campus
Computer System operation

programs (system and application) are stored on a disk until


they are loaded into memory. Many programs then use the
disk as both the source and the destination of their processing.
Hence, the proper management of disk storage is of central
importance to a computer system.
In a larger sense, however, the storage structure that we have
described— consisting of registers, main memory, and
magnetic disks — is only one of many possible storage
systems. Others include cache memory, CD-ROM, magnetic
tapes, and so on. Each storage system provides the basic
functions of storing a datum and holding that datum until it is
retrieved at a later time. The main differences among the
29
Lecture I BITS Pilani, Pilani Campus
Computer System operation
various storage systems lie in speed, cost, size, and volatility.
The wide variety of storage systems can be organized in a
hierarchy (See the Figure ) according to speed and cost. The
higher levels are expensive, but they are fast. As we move
down the hierarchy, the cost per bit generally decreases,
whereas the access time generally increases. This trade-off is
reasonable; if a given storage system were both faster and less
expensive than another— other properties being the same —
then there would be no reason to use the slower, more
expensive memory. In fact, many early storage devices,
including paper tape and core memories, are relegated to
museums now that magnetic tape and semiconductor
memory have become faster and cheaper.
30
Lecture I BITS Pilani, Pilani Campus
Computer System operation
Registers

Cache

Mai Memory

Solid Disk

Magnetic disk

Optical disk

Magnetic tape
31
Lecture 1 BITS Pilani, Pilani Campus
Computer System operation

The top four levels of memory in Figure may be constructed


using semiconductor memory.
In addition to differing in speed and cost, the various storage
systems are either volatile or nonvolatile. As mentioned earlier,
volatile storage loses its contents when the power to the
device is removed. In the absence of expensive battery and
generator backup systems, data must be written to
nonvolatile storage for safekeeping. In the hierarchy shown in
Figure, the storage systems above the solid-state disk are
volatile, whereas those including the solid-state disk and
below are nonvolatile.

32
Lecture I BITS Pilani, Pilani Campus
Computer System operation

Solid-state disks have several variants but in general are


faster than magnetic disks and are nonvolatile. Another form
of
solid-state disk is flash memory, which is popular in cameras
and personal digital assistants (PDAs), in robots, and
increasingly for storage on general-purpose computers.
Flash
memory is slower than DRAM but needs no power to retain
its
contents. Another form of nonvolatile storage is NVRAM,
which is DRAM with battery backup power. This memory can
be as fast as DRAM and (as long as the battery lasts) is
nonvolatile.
33
Lecture I BITS Pilani, Pilani Campus
Single and Multi processor
Systems
A computer system can be organized in a number of different
ways, which we can categorize roughly according to the
number of general-purpose processors used.
Single-Processor Systems
Until recently, most computer systems used a single processor.
On a single- processor system, there is one main CPU capable
of executing a general-purpose instruction set, including
instructions from user processes. Almost all single- processor
systems have other special-purpose processors as well. They
may come in the form of device-specific processors, such as
disk, keyboard, and graphics controllers; or, on mainframes,
they may come in the form of more general-purpose
34
Lecture I BITS Pilani, Pilani Campus
Single and Multi processor
Systems
processors, such as I/O processors that move data rapidly
among the components of the system.
Multiprocessor Systems
Within the past several years, multiprocessor systems (also
known as parallel systems or multicore systems) have
begun to dominate the landscape of computing. Such systems
have two or more processors in close communication, sharing
the computer bus and sometimes the clock, memory, and
peripheral devices. Multiprocessor systems first appeared
prominently appeared in servers and have since migrated to
desktop and laptop systems. Recently, multiple processors
have appeared on mobile devices such as smartphones and
35
Lecture I BITS Pilani, Pilani Campus
Single and Multi processor
Systems
tablet computers.
Multiprocessor systems have three main advantages:
Increased throughput. By increasing the number of
processors, we expect to get more work done in less time. The
speed-up ratio with N processors is not N, however; rather, it is
less than N. When multiple processors cooperate on a task, a
certain amount of overhead is incurred in keeping all the parts
working correctly. This overhead, plus contention for shared
resources, lowers the expected gain from additional
processors. Similarly, N programmers working closely
together do not produce N times the amount of work a single
programmer would produce.
36
Lecture I BITS Pilani, Pilani Campus
Computer System
Architecture
-Economy of scale. Multiprocessor systems can cost less than
equivalent multiple single-processor systems, because they
can share peripherals, mass storage, and power supplies. If
several programs operate on the same set of data, it is cheaper
to store those data on one disk and to have all the processors
share them than to have many computers with local disks and
many copies of the data.
Increased reliability. If functions can be distributed properly
among several processors, then the failure of one processor
will not halt the system, only slow it down. If we have ten
processors and one fails, then each of the remaining nine
processors can pick up a share of the work of the failed
processor.
37
Lecture I BITS Pilani, Pilani Campus
Components of Operating
System
An Operating System (OS) is an interface between a computer
user and computer hardware. An operating system is a
software which performs all the basic tasks like file
management, memory management, process management,
handling input and output, and controlling peripheral devices
such as disk drives and printers.
Some popular Operating Systems include Linux Operating
System, Windows OS, VMS, OS/400, AIX, z/OS, etc.
Definition
An operating system is a software that acts as an interface
between the user and the computer hardware and controls the
execution of all kinds of programs.

38
Lecture I BITS Pilani, Pilani Campus
Components of Operating
System
Following are some of important functions of an operating
System.
• Memory Management
• Processor Management
• Device Management
• File Management
• Security
• Control over system performance
• Job accounting
• Error detecting aids
• Coordination between other software and users

39
Lecture I BITS Pilani, Pilani Campus
Components of Operating
System
Memory Management
Memory management refers to management of Primary
Memory or Main Memory. Main memory is a large array of
words or bytes where each word or byte has its own address.
Main memory provides a fast storage that can be accessed
directly by the CPU. For a program to be executed, it must in
the main memory. An Operating System does the following
activities for memory management −
• Keeps tracks of primary memory, i.e., what part of it are in
use by whom, what part are not in use.
• In multiprogramming, the OS decides which process will
get memory when and how much.
40
Lecture I BITS Pilani, Pilani Campus
Components of Operating
System
• Allocates the memory when a process requests it to do so.
• De-allocates the memory when a process no longer needs
it or has been terminated.
Processor Management
In multiprogramming environment, the OS decides which
process gets the processor when and for how much time.
This
function is called process scheduling. An Operating System
does the following activities for processor management −
• Keeps tracks of processor and status of process. The
program responsible for this task is known as traffic
controller.
• Allocates the processor (CPU) to a process.
41
Lecture I BITS Pilani, Pilani Campus
Components of Operating
System
• De-allocates processor when a process is no longer
required.
Device Management
An Operating System manages device communication via
their
respective drivers. It does the following activities for device
management −
• Keeps tracks of all devices. Program responsible for this
task is known as the I/O controller.
• Decides which process gets the device when and for how
much time.
• Allocates the device in the efficient way.
• De-allocates devices.
42
Lecture I BITS Pilani, Pilani Campus
Components of Operating
System
File Management
A file system is normally organized into directories for easy
navigation and usage. These directories may contain files
and
other directions.
An Operating System does the following activities for file
management −
• Keeps track of information, location, uses, status etc. The
collective facilities are often known as file system.
• Decides who gets the resources.
• Allocates the resources.
• De-allocates the resources.
43
Lecture I BITS Pilani, Pilani Campus
Components of Operating
System
Other Important Activities
Following are some of the important activities that an
Operating System performs −
Security − By means of password and similar other
techniques, it prevents unauthorized access to programs and
data.
Control over system performance − Recording delays
between request for a service and response from the system.
Job accounting − Keeping track of time and resources used by
various jobs and users.
Error detecting aids − Production of dumps, traces, error
messages, and other debugging and error detecting aids.
44
Lecture I BITS Pilani, Pilani Campus
Components of Operating
System
Coordination between other softwares and users −
Coordination and assignment of compilers, interpreters,
assemblers and other software to the various users of the
computer systems.

45
Lecture I BITS Pilani, Pilani Campus
Thanks

46
BITS Pilani, Pilani Campus
OPERATING SYSTEM

BITS Pilani
Pilani Campus
BITS Pilani
Pilani Campus

INTRODUCTION TO OPERATING
SYSTEM
Learning Objectives
 Operating System services
 System calls
- Definition
- Types of System calls

3
Lecture - 2 BITS Pilani, Pilani Campus
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 programs. The specific
services provided, of course, differ from one operating
system
to another, but we can identify common classes. These
operating system services are provided for the convenience
of
the programmer, to make the programming task easier.
The following Figure shows one view of the various
operating-
system services and how they interrelate.

4
Lecture - 2 BITS Pilani, Pilani Campus
OPERATING SYSTEM
SERVICES
User and other system programs

GUI Batch Command Line


User Interface

SYSTEM CALLS

Program I/O file Communication Resource Accounting


execution operations systems allocation

error detection Services Protection and


Security

OPERATING SYSTEM

Hardware
5
Lecture - 2 BITS Pilani, Pilani Campus
OPERATING SYSTEM
SERVICES
One set of operating system services provides functions that
are helpful to the user.
• User interface. Almost all operating systems have a user
interface (UI). This interface can take several forms. One is a
command-line interface (CLI), which uses text commands
and a method for entering them (say, a keyboard for typing in
commands in a specific format with specific options). Another
is a batch interface, in which commands and directives to
control those commands are entered into files, and those files
are executed. Most commonly, a graphical user interface
(GUI) is used.

6
Lecture - 2 BITS Pilani, Pilani Campus
OPERATING SYSTEM
SERVICES
• Program execution. The system must be able to load a
program into memory and to run that program. The
program
must be able to end its execution, either normally or
abnormally (indicating error).
• I/O operations.A running program may require I/O, which
may involve a file or an I/O device. For specific devices,
Special functions may be desired (such as recording to a CD
Or DVD drive or blanking a display screen). For efficiency and
protection, users usually cannot control I/O devices directly.
Therefore, the operating system must provide a means to do
I/O
7
Lecture - 2 BITS Pilani, Pilani Campus
OPERATING SYSTEM
SERVICES
• File-system manipulation. The file system is of particular
interest. Obviously, programs need to read and write files
and
directories. They also need to create and delete them by
name,
search for a given file, and list file information. Finally, some
operating systems include permissions management to allow
or deny access to files or directories based on file ownership.
Many operating systems provide a variety of file systems,
sometimes to allow personal choice and sometimes to
provide
specific features or performance characteristics.
8
Lecture - 2 BITS Pilani, Pilani Campus
OPERATING SYSTEM
SERVICES
• Communications. There are many circumstances in which
one process needs to exchange information with another
process. Such communication may occur between processes
that are executing on the same computer or between
processes that are executing on different computer systems
tied together by a computer network
Error detection. The operating system needs to be detecting
and correcting errors constantly. Errors may occur in the CPU
and memory hardware (such as a memory error or a power
failure), in I/O devices (such as a parity error on disk, a
connection failure on a network, or lack of paper in the
printer), and in the user program (such as an arithmetic
9
Lecture - 2 BITS Pilani, Pilani Campus
OPERATING SYSTEM
SERVICES
overflow, an attempt to access an illegal memory location, or a
too-great use of CPU time). For each type of error, the
operating system should take the appropriate action to ensure
correct and consistent computing.
Another set of operating system functions exists not for
helping the user but rather for ensuring the efficient operation
of the system itself. Systems with multiple users can gain
efficiency by sharing the computer resources among the
users.
• Resource allocation. When there are multiple users or
multiple jobs running at the same time, resources must be
allocated to each of them.
10
Lecture - 2 BITS Pilani, Pilani Campus
OPERATING SYSTEM
SERVICES
• Accounting. We want to keep track of which users use how
much and what kinds of computer resources. This record
keeping may be used for accounting (so that users can be
billed) or simply for accumulating usage statistics. Usage
statistics may be a valuable tool for researchers who wish to
reconfigure the system to improve computing services.
• Protection and security. The owners of information stored
in a multiuser or networked computer system may want to
control use of that information. Security starts with requiring
each user to authenticate himself or herself to the system,
usually by means of a password, to gain access to system
resources.
11
Lecture - 2 BITS Pilani, Pilani Campus
SYSTEM CALLS

A system call is a method for programs to interact with the


operating system. It provides an interface between a process
and the operating system to allow user – level processes to
request services of the operating system.
Services Provided by the System calls
1. In process control,
The system call performs the task of process creating, process
termination, etc.,
Functions:
- End and Abort
- Load and Execute
- Create and terminate process
12
Lecture - 2 BITS Pilani, Pilani Campus
SYSTEM CALLS

- Wait and assigned event


- Allocates and De allocates
2. In file management,
The system call performs file manipulations
Functions:
- Create a file
- Open and Close file
- Read, Write and Reposition
- Delete a file
- Get and Set file attributes
3. In device management,
The system call does the jobs of device manipulation
13
Lecture - 2 BITS Pilani, Pilani Campus
SYSTEM CALLS

Functions:
- Request and Release device
- Get and Set device attribute
4.In Information Maintenance
The system call handles information and its transfer between
the user and the OS
Functions:
Get the time or setting the time
Get the process and device attributes
5. Communication
The system call is used for inter-process communication

14
Lecture - 2 BITS Pilani, Pilani Campus
SYSTEM CALLS
Functions:
Create and delete communication
Sending and receiving message
Attach and detach remote services
The following are the example for System calls in Windows
and Unix OS
Windows Unix
Process CreateProcess() fork()
Control ExitProcess() exit()
File CreateFile() open()
Manipulation ReadFile() read()
WriteFile() write()

15
Lecture - 2 BITS Pilani, Pilani Campus
SYSTEM CALLS
CloseHandle() close()
Device SetConsoleMode() ioctl()
Manipulation ReadConsole() read()
WriteConsole() write()

Information GetCurrentProcessID() getpid()


Maintenance SetTimer() alarm()
Sleep() sleep()

Communication CreatePipe() pipe()


CreateFileMapping() shm open()
MapViewOfFile() mmap()

16
Lecture - 2 BITS Pilani, Pilani Campus
SYSTEM CALLS

Protection SetFileSecurity() chmod()


InitlializeSecurityDescriptor() umask()
SetSecurityDescriptorGroup() chown()

17
Lecture - 2 BITS Pilani, Pilani Campus
Thanks

18
BITS Pilani, Pilani Campus
OPERATING SYSTEM

BITS Pilani SESSION 3


Pilani Campus
BITS Pilani
Pilani Campus

CONCEPTS OF PROCESS
Learning Objectives
 What is a process?
 States of a process
 Process Control Block
 Process Scheduling – Basics
- Process Scheduling queues
- Two process model
- Scheduler and its types
 Context Switching
3
Lecture - III BITS Pilani, Pilani Campus
PROCESS CONCEPT
Process Concept
Early computers had complete control over the execution of
programs because they allowed only one program to execute.
But current day computer systems permit multiple programs to
be loaded into the memory and to be executed concurrently. It
requires firmer control which resulted in the notion of a
Process.
A process is a program in execution. A process is not only the
program but it is also includes the program counter, Stack and
a data section.
• Program Counter – It specifies the next instruction to be
executed

4
Lecture - III BITS Pilani, Pilani Campus
PROCESS CONCEPT
• Stack – It is used to store temporary data such as local
variables, parameters and return address
• Data Section – It contains the global variables
States of a Process
As process executes, its state changes. The state of a process
represents the part by which the current activity of that
process. Each process may be in any of the following states:
(i) New
(ii) Running
(iii) Waiting
(iv) Ready
(v) Terminated
5
Lecture - III BITS Pilani, Pilani Campus
PROCESS CONCEPT
• New State – The process is being created
• Running State – Instructions of the process are being
executed
• Waiting State – The running process is waiting for some
event to occur
• Ready State – The process is waiting to be assigned to a
processor
• Terminated state – The process had finished its execution.

The following diagram represents the different states of the


process :

6
Lecture - III BITS Pilani, Pilani Campus
States of a PROCESS

new terminated

Admitted Interrupt exit

ready running
Schedule

I/O or event I/O event


completion waiting event wait

7
Lecture - III BITS Pilani, Pilani Campus
PROCESS CONCEPT
Process Control Block
Each process is represented in the operating system by a
process control block (PCB). The process control block is also
called as a task control block. It contains many pieces of
information associated with specified process.
Process state – The state may be new, ready, wait, running,
waiting, and so on.,
Program Counter – It indicates the address of the next
instruction to be executed for the running process
CPU registers – They include accumulators , index registers ,
Stack pointers, general – purpose registers and condition –
code information. It is used to save information when an
interrupt occurs.
8
Lecture - III BITS Pilani, Pilani Campus
PROCESS CONTROL
BLOCK

Pointer Process State

Process Number

Program Counter

Registers

Memory Limits

List of open files


9
Lecture - III BITS Pilani, Pilani Campus
PROCESS CONCEPT
CPU Scheduling information – The information includes a
process priority , pointers to scheduling queues and
scheduling parameters.
Memory Management information – This information
include the value of the base and limit registers, the page
tables , depending on the memory system
Accounting information – It includes the amount of CPU ,
real
time used, time limits, account numbers, job or process
numbers and so on.,
I/O status information – It includes the list of I / O devices
allocated to this process, a list of open files and so on
10
Lecture - III BITS Pilani, Pilani Campus
PROCESS SCHEDULING
Process Scheduling
The process scheduling is the activity of the process manages
that handles the removal of running process from the CPU and
the selection of another process on the basis of a strategy.
Process Scheduling is an essential part of multi programming
OS.
Process Scheduling Queue
The OS maintains all the PCB’s in process scheduling queues.
The operating system maintains a separate queue of each
process states and PCB’s of all processes in the same
execution state are placed in the queue. When the state of the
process is changed, its PCB is unlinked from its current queue
and moved to its new state queue.
11
Lecture - III BITS Pilani, Pilani Campus
PROCESS SCHEDULING
QUEUES
The OS maintains the following important process
scheduling
queues:
Job queue – This queue keeps all the process in the system
Ready queue – This queue keeps a set of all processes
residing in main memory that are ready and waiting to
execute. A new process is always put in this queue.
Device queue – The process which are blocked due to
unavailability of an I/O device constitute this queue.
The operating system uses different policies to manage each
queue. The OS scheduler determines how to move processes
between the ready and run queues which can only have one
entry per processor core on the system.
12
Lecture - III BITS Pilani, Pilani Campus
PROCESS SCHEDULING
QUEUES
1 2
Ready Queue CPU
4 3
I/O I/O queue I/O request

time slice expired

5
child executes fork a child
6
interrupt occurs waits for an
interrupt

13
Lecture - III BITS Pilani, Pilani Campus
PROCESS SCHEDULING
Two Process Model
Two process models refers to running and non – running states
as
• Running – When a new process is created , it enters into the
system as in the running state
• Not Running – Processes that are not running are kept in
the queue, waiting for their turn to execute. Each entry in the
queue is a pointer to a particular process. Queue is
implemented in linked list structure.
When a process is interrupted, the process is transferred in
the waiting queue. If the process has completed or aborted,
the process is discarded. In either case, the component
dispatcher selects a process from the queue to execute.
14
Lecture - III BITS Pilani, Pilani Campus
PROCESS SCHEDULING
Scheduler
Scheduler is a special system software which handles process
scheduling in various ways. Their main task is to select the
jobs to be submitted into the system and to decide which
process to run. There are three types of schedulers. They are
(i) Long term Scheduler
(ii) Short term Scheduler
(iii) Medium time scheduler
Long Time Scheduler – It is also called as job scheduler or
high level scheduler. A long term scheduler determines which
processes are admitted to the system for processing. It selects
the processes from the queue and load them into the memory
for execution. The primary objective of the job scheduler is
15
Lecture - III BITS Pilani, Pilani Campus
PROCESS SCHEDULING
to provide a balanced mix of jobs, such as I/O bound and
processor bound
Short term Scheduler – It is also called as CPU scheduler. Its
main objective is to increase system performance in
accordance with some criteria. Short term schedulers are also
known as dispatchers , makes the decision of which process to
execute. Short term schedulers are faster than long – term
scheduler.
Medium Scheduler – Medium schedule is part of swapping. It
removes the processes from the memory. It reduces the
degree of multi programming. The medium term scheduler is
in charge of handling swapped – out processes.

16
Lecture - III BITS Pilani, Pilani Campus
PROCESS SCHEDULING
A running process may become suspended if it makes an I/O
request. A suspended process can not make any progress
towards completion. The suspended process is moved to the
secondary storage. This process is called swapping. The
process is said to be swapped out or rolled out.
Comparison between Schedulers
Long term Short term Medium Term
Job Scheduler CPU scheduler Process swapping scheduler

Speed is less Speed is faster Speed is medium

Controls the degree of Lesser control over Reduces the degree


multi programming degree of multiprogramming of multi programming

Selects process from pool Selects processes which Reintroduce the process
and loads into memory are ready execute into memory and execution
is continued.
17
Lecture - III BITS Pilani, Pilani Campus
CONTEXT SWITCHING
Context Switching
It is the mechanism to store and restore or context of CPU in
process control block so that process execution can be
resumed for the same point at a later time. Using this
technique, a context switch controls multiples processes to
share a single CPU. Context Switching is an essential part of
multi tasking operating system features.
When the scheduler switches the CPU from executing one
process to execute another process , the state from the current
running process is stored into the PCB. After this, the ready
state e process is allowed to execute / run. It is loaded into the
memory from its PCB, set the program counter and register.

18
Lecture - III BITS Pilani, Pilani Campus
CONTEXT SWITCHING

19
Lecture - III BITS Pilani, Pilani Campus
CONTEXT SWITCHING
In the above figure, you can see that initially, the process P1 is
in the running state and the process P2 is in the ready state.
Now, when some interruption occurs then you have to switch
the process P1 from running to the ready state after saving the
context and the process P2 from ready to running state. The
following steps will be performed:
1. Firstly, the context of the process P1 i.e. the process present
in the running state will be saved in the Process Control Block
of process P1 i.e. PCB1.
2. Now, you have to move the PCB1 to the relevant queue i.e.
ready queue, I/O queue, waiting queue, etc.
3. From the ready state, select the new process that is to be
executed i.e. the process P2.
20
Lecture - III BITS Pilani, Pilani Campus
CONTEXT SWITCHING
4. Now, update the Process Control Block of process P2 i.e.
PCB2 by setting the process state to running. If the process
P2
was earlier executed by the CPU, then you can get the
position
of last executed instruction so that you can resume the
execution of P2.
5. Similarly, if we want to execute the process P1 again, then
we have to follow the same steps as mentioned above(from
step 1 to 4).

21
Lecture - III BITS Pilani, Pilani Campus
Thanks

22
BITS Pilani, Pilani Campus
OPERATING SYSTEM

BITS Pilani
Pilani Campus
BITS Pilani
Pilani Campus

CONCEPTS OF PROCESS -
CONTINUATION
Learning Objectives
 Inter process Communication (IPC)
- What is IPC?
- Methods of Inter process Communication
 Threads
- What is a thread?
- Single and multi threaded programming
- Benefits of multi threaded programming
- Multi threading Models

3
Lecture - IV BITS Pilani, Pilani Campus
PROCESS CONCEPT
Processes executing concurrently in the operating system
may
be either independent processes or coordinating processes.
Independent Processes – They can not affect or be affected
by other processes executing in the system
Coordinating Processes – They can affect or can affected by
other processes executing in the system.
Any process that shares data with other processes is a
coordinating process
There are several reasons for providing an environment that
allows process coordination
1. Information Sharing – Several processes want to share
the information
4
Lecture - IV BITS Pilani, Pilani Campus
PROCESS CONCEPT
2. Modularity – The complex problem can broken down into
several tasks. Each task is the process. The process of breaking
the system into multiple modules is called as Modularity.
There are various advantages of modularity. Some of them are
- Debugging is easy
- Work load can be shared
- Reducing the cost
3. Computation Speed – Concurrent execution of multiple
parts of a program reduces the time and computation speed is
maximized.
4. Convenience – If we allow the processes to cooperate with
each other , it will be convenient to the user to multi tasking

5
Lecture - IV BITS Pilani, Pilani Campus
PROCESS CONCEPT
Cooperating processes require an inter process
communication (IPC) mechanism that will allow them to
exchange data and information between them.
There are two fundamental models inter process
communication are
1. Shared memory
2. Message Passing
Shared Memory
In the shared memory model, a region of memory that is
shared by coordinating processes is established.
Processes can then exchange information by reading and
writing data to the shared region.
The following picture represents is
6
Lecture - IV BITS Pilani, Pilani Campus
PROCESS CONCEPT

Process A
Writing
Shared Memory

Process B Reading

Kernel

If process a wants to communicate data with other process B, A


writes the data on this memory area which will be read by the
process B.
7
Lecture - IV BITS Pilani, Pilani Campus
PROCESS CONCEPT
Message Passing
In the message passing model, communication takes place
by
means of messages exchanged between the cooperating
processes as follows:

Process A M

Process B M

Kernel M
8
Lecture - IV BITS Pilani, Pilani Campus
PROCESS CONCEPT
The other inter process communication are
(i) Pipe
(ii) Socket
(iii)File
Kernel
Kernel is the essential part of the operating system. It
provides basic services for all other parts of the operating
system. It is the main layer between operating system and
the
Hardware and it helps with process and memory
management,
file system, device control and networking
9
Lecture - IV BITS Pilani, Pilani Campus
THREAD
A thread is a basic unit of CPU utilization; it comprises 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 other operating-system resources, such as
open files and signals. A traditional (or heavyweight) process
has a single thread of control. If a process has multiple threads
of control, it can perform more than one task at a time.
The following figure illustrates the difference between a
traditional single- threaded process and a multithreaded
process.
Most software applications that run on modern computers are
multithreaded. An application typically is implemented as a
separate process with several threads of control.
10
Lecture - IV BITS Pilani, Pilani Campus
THREAD

Code Data Files Code Data Files

registers Stack registers registers registers

Stack Stack Stack

Thread
Thread

Single thread process Multiple thread process


11
Lecture - IV BITS Pilani, Pilani Campus
THREAD
A web browser might have one thread display images or text
while another thread retrieves data from the network, for
example. A word processor may have a thread for displaying
graphics, another thread for responding to keystrokes from the
user, and a third thread for performing spelling and grammar
checking in the background. In certain situations, a single
application may be required to perform several similar tasks.
For example, a web server accepts client requests for web
pages, images, sound, and so forth. A busy web server may
have several (perhaps thousands of) clients concurrently
accessing it. If the web server ran as a traditional single-
threaded process, it would be able to service only one client at
a time, and a client might have to wait a very long time for
12
Lecture - IV BITS Pilani, Pilani Campus
THREAD
its request to be serviced.
One solution is to have the server run as a single process that
accepts requests. When the server receives a request, it
creates a separate process to service that request. In fact, this
process-creation method was in common use before threads
became popular. Process creation is time consuming and
resource intensive, however. If the new process will perform
the same tasks as the existing process, why incur all that
overhead? It is generally more efficient to use one process that
contains multiple threads. If the web-server process is
multithreaded, the server will create a separate thread that
listens for client requests. When a request is made, rather than
creating another process, the server creates a new thread to
13
Lecture - IV BITS Pilani, Pilani Campus
THREAD
service the request and resume listening for additional
requests. It is illustrated in the following diagram:

(1) request (2) create a new thread


to service the request

Client Server thread

(3) resume listening for


additional requests

Multithreaded server architecture


14
Lecture - IV BITS Pilani, Pilani Campus
THREAD
Benefits
The benefits of multithreaded programming can be broken
down into four major categories:
Responsiveness. Multithreading an interactive application
may allow a program to continue running even if part of it is
blocked or is performing a lengthy operation, thereby
increasing responsiveness to the user. This quality is
especially useful in designing user interfaces. For instance,
consider what happens when a user clicks a button that results
in the performance of a time-consuming operation. A single-
threaded application would be unresponsive to the user until
the operation had completed. In contrast, if the time-
consuming operation is performed in a separate thread, the
15
Lecture - IV BITS Pilani, Pilani Campus
THREAD
application remains responsive to the user.
Resource sharing. Processes can only share resources
through techniques such as shared memory and message
passing. Such techniques must be explicitly arranged by the
programmer. However, threads share the memory and the
resources of the process to which they belong by default. The
benefit of sharing code and data is that it allows an application
to have several different threads of activity within the same
address space.
Economy. Allocating memory and resources for process
creation is costly. Because threads share the resources of the
process to which they belong, it is more economical to create
and context-switch threads. Empirically gauging the
16
Lecture - IV BITS Pilani, Pilani Campus
THREAD
difference in overhead can be difficult, but in general it is
significantly more time consuming to create and manage
processes than threads. In Solaris, for example, creating a
process is about thirty times slower than is creating a thread,
and context switching is about five times slower.
Scalability. The benefits of multithreading can be even
greater in a multiprocessor architecture, where threads may
be
running in parallel on different processing cores. A single-
threaded process can run on only one processor, regardless
how many are available. We explore this issue further in the
following section.
17
Lecture - IV BITS Pilani, Pilani Campus
THREAD
Multithreading Models
Support for threads may be provided either at the user level,
for user threads, or by the kernel, for kernel threads. User
threads are supported above the kernel and are managed
without kernel support, whereas kernel threads are supported
and managed directly by the operating system. Virtually all
contemporary operating systems— including Windows, Linux,
Mac OS X, and Solaris— support kernel threads.
Ultimately, a relationship must exist between user threads and
kernel threads. In this section, we look at three common ways
of establishing such a relationship: the many-to-one model, the
one-to-one model, and the many-to- many model.

18
Lecture - IV BITS Pilani, Pilani Campus
THREAD
Many – to – One Model
The many-to-one model (See the figure ) maps many user-
level threads to one kernel thread. Thread management is
done by the thread library in user space, so it is efficient.
However, the entire process will block if a thread makes a
blocking system call.
Also, because only one thread can access the kernel at a time,
multiple threads are unable to run in parallel on multicore
systems. Green threads — a thread library available for
Solaris systems and adopted in early versions of Java— used
the many-to-one model. However, very few systems continue
to use the model because of its inability to take advantage of
multiple processing cores.

19
Lecture - IV BITS Pilani, Pilani Campus
THREAD – Many –to - One
Model

User
Thread

K Kernel Thread

20
BITS Pilani, Pilani Campus
THREAD – One – to One
Model
One – to – One Model
The one-to-one model (See the Figure ) maps each user thread
to a kernel thread. It provides more concurrency than the
many-to-one model by allowing another thread to run when a
thread makes a blocking system call. It also allows multiple
threads to run in parallel on multiprocessors. The only
drawback to this model is that creating a user thread requires
creating the corresponding kernel thread. Because the
overhead of creating kernel threads can burden the
performance of an application, most implementations of this
model restrict the number of threads supported by the system.
Linux, along with the family of Windows operating systems,
implement the one-to-one model.
21
Lecture - IV BITS Pilani, Pilani Campus
THREAD

User
Thread

K K K K Kernel
Thread

22
Lecture - IV BITS Pilani, Pilani Campus
THREAD
Many-to-Many Model
The many-to-many model (See the Figure ) multiplexes many
user-level threads to a smaller or equal number of kernel
threads. The number of kernel threads may be specific to
either a particular application or a particular machine (an
application may be allocated more kernel threads on a
multiprocessor than on a single processor).
Let’s consider the effect of this design on concurrency.
Whereas the many- to-one model allows the developer to
create as many user threads as she wishes, it does not result in
true concurrency, because the kernel can schedule only one
thread at a time. The one-to-one model allows greater
concurrency, but the developer has to be careful not to create
23
Lecture - IV BITS Pilani, Pilani Campus
THREAD
too many threads within an application (and in some
instances
may be limited in the number of threads she can
create). The many-to-many model suffers from neither of
these
shortcomings: developers can create as many user threads
as
necessary, and the corresponding kernel threads can run in
parallel on a multiprocessor. Also, when a thread performs a
blocking system call, the kernel can schedule another thread
for execution.

24
Lecture - IV BITS Pilani, Pilani Campus
THREAD – Many – to –
Many Model

User Thread

K K K Kernel
Thread

25
Lecture - IV BITS Pilani, Pilani Campus
Thanks

26
BITS Pilani, Pilani Campus
OPERATING SYSTEM

BITS Pilani
Pilani Campus
BITS Pilani
Pilani Campus

PROCESS SCHEDULING
Learning Objectives
 CPU Scheduling
- What is Process Scheduling?
 Types of Scheduling algorithms
 Various Methods of Process Scheduling
- First Come First Served
- Shortest Job Next
- Priority Scheduling
- Round Robin Scheduling
- Multiple Queue Scheduling
3
Lecture - V BITS Pilani, Pilani Campus
THREAD LIBARARIES
Thread libraries are the APIs to be used for creating ,
executing and terminating the threads.
There are two types of threads. They are
1. PTHREAD (POSIX)
2. Java Threads
PTHREAD
It is an API to create and run a thread. The API is written in C
language.
The following is the code written for finding the sum of first ‘n’
numbers.
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
4
Lecture - V BITS Pilani, Pilani Campus
THREAD LIBARARIES
int sum=0;
void *run(void *p);
int main(int ac,char *argv[])
{
pthread_t tid;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_create(&tid,&attr,run,argv[1]);
pthread_join(tid,NULL);
printf("\n Summation is %d",sum);
return 0;
}

5
Lecture - V BITS Pilani, Pilani Campus
THREAD LIBARARIES
void *run(void *p)
{
int i,up=atoi(p);
for(i=1;i<=up;i++)sum+=i;
pthread_exit(0);
}
Java Thread libraries have lot of built –in- methods for
managing the threads. The important thread methods are:
Start() – Making the thread to be ready
Run() – Execute the thread

6
Lecture - V BITS Pilani, Pilani Campus
PROCESS SCHEDULING
CPU Scheduling is a process of determining which process will
own
CPU for execution while another process is on hold. The main task
of
CPU scheduling is to make sure that whenever the CPU remains
idle,
the OS at least select one of the processes available in the ready
queue for execution. The selection process will be carried out by
the
CPU scheduler. It selects one of the processes in memory that are
ready for execution.
Types of CPU Scheduling
Here are two kinds of Scheduling methods:

7
Lecture - V BITS Pilani, Pilani Campus
PROCESS SCHEDULING
Preemptive Scheduling
In Preemptive Scheduling, the tasks are mostly assigned with their
priorities. Sometimes it is important to run a task with a higher
priority before another lower priority task, even if the lower priority
task is still running. The lower priority task holds for some time and
resumes when the higher priority task finishes its execution.
Non-Preemptive Scheduling
In this type of scheduling method, the CPU has been allocated to a
specific process. The process that keeps the CPU busy will release
the CPU either by switching context or terminating. It is the only
method that can be used for various hardware platforms. That’s
because it doesn’t need special hardware (for example, a timer) like
preemptive scheduling.
8
Lecture - V BITS Pilani, Pilani Campus
PROCESS SCHEDULING
When scheduling is Preemptive or Non-Preemptive?
To determine if scheduling is preemptive or non-preemptive,
consider these four parameters:
• A process switches from the running to the waiting state.
• Specific process switches from the running state to the ready state.
• Specific process switches from the waiting state to the ready state.
• Process finished its execution and terminated.
Only conditions 1 and 4 apply, the scheduling is called non-
preemptive.
All other scheduling are preemptive.
Important CPU scheduling Terminologies
Burst Time/Execution Time: It is a time required by the process to
complete execution. It is also called running time.

9
Lecture - V BITS Pilani, Pilani Campus
PROCESS SCHEDULING
Arrival Time(AT): when a process enters in a ready state
Completion Time (CT): It is the finishing or completion time of the
process in the system
Turnaround Time (TA): It is the time difference between
Completion Time and Arrival Time
TA = CT - AT
Waiting Time (WT): It is the time difference between Turnaround
Time and Burst Time
WT = TA - BT
CPU Scheduling Criteria
A CPU scheduling algorithm tries to maximize and minimize the
following:

10
Lecture - V BITS Pilani, Pilani Campus
PROCESS SCHEDULING
Maximizing:
CPU utilization: CPU utilization is the main task in which the
operating system needs to make sure that CPU remains as busy as
possible. It can range from 0 to 100 percent. However, for the RTOS, it
can be range from 40 percent for low-level and 90 percent for the
high-level system.
Throughput: The number of processes that finish their execution per
unit time is known Throughput. So, when the CPU is busy executing
the process, at that time, work is being done, and the work
completed per unit time is called Throughput.
Minimizing:
Waiting time: Waiting time is an amount that specific process needs
to wait in the ready queue.

11
Lecture - V BITS Pilani, Pilani Campus
PROCESS SCHEDULING
Response time: It is an amount to time in which the request was
submitted until the first response is produced.
Turnaround Time: Turnaround time is an amount of time to execute
a specific process. It is the calculation of the total time spent waiting
to get into the memory, waiting in the queue and, executing on the
CPU. The period between the time of process submission to the
completion time is the turnaround time.
First Come First Serve (FCFS)
• Jobs are executed on first come, first serve basis.
• It is a non-preemptive, pre-emptive scheduling algorithm.
• Easy to understand and implement.
• Its implementation is based on FIFO queue.
• Poor in performance as average wait time is high.

12
Lecture - V BITS Pilani, Pilani Campus
PROCESS SCHEDULING
Example:- The following table represents the time of arrival and
burst time of 4 processes

Process Arrival Burst Solution:


Time(AT) Time(BT) The Gantt Chart for the Jobs is

P0 0 5 P0 P1 P2 P3

P1 1 3 0 5 8 16 22

P2 2 8

P3 3 6

13
Lecture - V BITS Pilani, Pilani Campus
PROCESS SCHEDULING

Process Arrival Burst Completion Turnaround Waiting

Time(AT) Time(BT) Time(CT) Time(TA) Time(WA)

P0 0 5 5 5 0

P1 1 3 8 7 4

P2 2 8 16 14 6

P3 3 6 22 19 13

Average Waiting Time = ( 0 + 4 + 6 + 13 ) / 4 = 23 / 4 = 5.75


14
Lecture - V BITS Pilani, Pilani Campus
PROCESS SCHEDULING
Shortest Job Next (SJN)
• This is also known as shortest job first, or SJF
• This is a non-preemptive, pre-emptive scheduling algorithm.
• Best approach to minimize waiting time.
• Easy to implement in Batch systems where required CPU time is
known in advance.
• Impossible to implement in interactive systems where required
CPU time is not known.
• The processer should know in advance how much time process
will take.
The problem mentioned for FCFS is solved as follows:

15
Lecture - V BITS Pilani, Pilani Campus
PROCESS SCHEDULING

Process Arrival Burst Solution:


Time(AT) Time(BT) The Gantt Chart for the Jobs is

P0 0 5 P0 P1 P3 P2

P1 1 3 0 5 8 14 22

P2 2 8

P3 3 6

Note:- Important Point is that though P1 has shortest time than P0, P0 arrives
at time 0 (When the entire system begins) and P1 comes after 1 unit of time
16
Lecture - V BITS Pilani, Pilani Campus
PROCESS SCHEDULING

Process Arrival Burst Completion Turnaround Waiting

Time(AT) Time(BT) Time(CT) Time(TA) Time(WA)

P0 0 5 5 5 0

P1 1 3 8 7 4

P2 2 8 22 20 12

P3 3 6 14 11 5

Average Waiting Time = ( 0 + 4 + 12 + 5 ) / 4 = 21 / 4 = 5.25


17
Lecture - V BITS Pilani, Pilani Campus
PROCESS SCHEDULING
Priority Based Scheduling
• Priority scheduling is a non-preemptive algorithm and one of
the most common scheduling algorithms in batch systems.
• Each process is assigned a priority. Process with highest priority
is to be executed first and so on.
• Processes with same priority are executed on first come first
served basis.
• Priority can be decided based on memory requirements, time
requirements or any other resource requirement.

18
Lecture - V BITS Pilani, Pilani Campus
PROCESS SCHEDULING

Process Arrival Burst Priority


Time(AT) Time(BT)

P0 0 5 1
P1 1 3 2
P2 2 8 1
P3 3 6 3

Solution: The Gantt Chart for the Jobs is

P0 P3 P1 P2

0 5 11 14 22

19
Lecture - V BITS Pilani, Pilani Campus
PROCESS SCHEDULING

Process Arrival Burst Completion Turnaround Waiting

Time(AT) Time(BT) Time(CT) Time(TA) Time(WA)

P0 0 5 5 5 0

P1 1 3 11 10 7

P2 2 8 14 12 4

P3 3 6 22 19 13

Average Waiting Time = ( 0 + 7 + 4 + 13 ) / 4 = 24 /4 =6


20
Lecture - V BITS Pilani, Pilani Campus
PROCESS SCHEDULING
Shortest Remaining Time
• Shortest remaining time (SRT) is the preemptive version of the
SJN algorithm.
• The processor is allocated to the job closest to completion but it
can be preempted by a newer ready job with shorter time to
completion.
• Impossible to implement in interactive systems where required
CPU time is not known.
• It is often used in batch environments where short jobs need to
give preference.
Round Robin Scheduling
• Round Robin is the preemptive process scheduling algorithm.
• Each process is provided a fix time to execute, it is called
a quantum.
21
Lecture - V BITS Pilani, Pilani Campus
PROCESS SCHEDULING
• Once a process is executed for a given time period, it is
preempted and other process executes for a given time period.
• Context switching is used to save states of preempted
processes.
The problem is solved with the quantum time 3 as follows:
Process Arrival Burst
Time(AT) Time(BT)
P0 0 5
P1 1 3
P2 2 8 Solution: The Gantt Chart is
P3 3 6 drawn as follows:

P0 P1 P2 P3 P0 P2 P3 P2

0 3 6 9 12 14 17 20 22
22
Lecture - V BITS Pilani, Pilani Campus
PROCESS SCHEDULING

Process Arrival Burst Completion Waiting


Time(AT) Time(BT) Time(CT) Time

P0 0 5 14 0+(12-3) = 9

P1 1 3 6 (3 - 1) = 2

P2 2 8 22 (6 – 2) + ( 14 – 9 ) +
(20 – 17 ) = 12

P3 3 6 20 (9 – 3 ) + (17 - 12) =
11

Average Waiting Time = ( 9 +2 + 12 + 11 ) / 4 = 34 / 4 = 8.5


23
Lecture - V BITS Pilani, Pilani Campus
PROCESS SCHEDULING
Multiple-Level Queues Scheduling
• Multiple-level queues are not an independent scheduling
algorithm. They make use of other existing algorithms to group
and schedule jobs with common characteristics.
• Multiple queues are maintained for processes with common
characteristics.
• Each queue can have its own scheduling algorithms.
• Priorities are assigned to each queue.
For example, CPU-bound jobs can be scheduled in one queue and
all I/O-bound jobs in another queue. The Process Scheduler then
alternately selects jobs from each queue and assigns them to the
CPU based on the algorithm assigned to the queue.

24
Lecture - V BITS Pilani, Pilani Campus
PROCESS SCHEDULING

System Processes
Highest Priority FCFS

Interactive Processes
CPU
Medium Priority SJF

Batch Processes
Lowest Priority RR

25
Lecture - V BITS Pilani, Pilani Campus
Thanks

26
BITS Pilani, Pilani Campus
OPERATING SYSTEM

BITS Pilani SESSION 6


Pilani Campus
BITS Pilani
Pilani Campus

PROCESS SCHEDULING AND


ALGORITHM EVALUATION
Learning Objectives
 Multiple Queue Scheduling
- Advantages and Disadvantages
- Worked Example
 Multiple Processor Scheduling
 Algorithm evaluation
- Deterministic Modeling
- Queuing Modeling
- Simulations
 Thread Scheduling

3
Lecture - VI BITS Pilani, Pilani Campus
MULTIPLE-LEVEL QUEUES
SCHEDULING
Advantages of Multiple Level Queues Scheduling:
• Multilevel Queue Scheduling has some advantages like:
• Multilevel queue scheduling helps us apply different
scheduling algorithms for different processes.
• It will have a low scheduling overhead.
Disadvantages:
• Some of the disadvantages of Multilevel queue scheduling
are as follows:
• There are chances of starving for the lower priority
processes.
• It is inflexible in nature.

4
Lecture - VI BITS Pilani, Pilani Campus
MULTIPLE-LEVEL QUEUES
SCHEDULING
Example of Multilevel queue scheduling:
Consider below table of four processes under Multilevel
queue scheduling. Queue number denotes the queue of the
process.

Process Arrival Time CPU Burst Time Queue


Number
P1 0 4 1
P2 0 3 1
P3 0 8 2
P4 10 5 1

5
Lecture - VI BITS Pilani, Pilani Campus
MULTIPLE-LEVEL QUEUES
SCHEDULING
Priority of queue 1 is greater than queue 2. queue 1 uses
Round Robin (Time Quantum = 2) and queue 2 uses FCFS.
Below is the Gantt chart of the problem :

P1 P2 P1 P2 P3 P4 P5

0 2 4 6 7 10 15 23
At starting both queues have process so process in queue 1
(P1, P2) runs first (because of higher priority) in the round
robin fashion and completes after 7 units then process in
queue 2 (P3) starts running (as there is no process in queue
1)
but while it is running P4 comes in queue 1 and interrupts P3
6
Lecture - VI BITS Pilani, Pilani Campus
MULTIPLE-LEVEL QUEUES
SCHEDULING
and start running for 5 second and after its completion P3 takes
the
CPU and completes its execution.

7
Lecture - VI BITS Pilani, Pilani Campus
MULTIPLE PROCESSOR
SCHEDULING
MULTIPLE PROCESSOR SCHEDULING
In multiple-processor scheduling multiple CPU’s are
available and hence Load Sharing becomes possible.
However multiple processor scheduling is more complex as
compared to single processor scheduling. In multiple
processor scheduling there are cases when the processors are
identical i.e. HOMOGENEOUS, in terms of their functionality,
we can use any processor available to run any process in the
queue.
Approaches to Multiple-Processor Scheduling
One approach is when all the scheduling decisions and I/O
processing are handled by a single processor which is called
the Master Server and the other processors executes only the
8
Lecture - VI BITS Pilani, Pilani Campus
MULTIPLE PROCESSOR
SCHEDULING
user code. This is simple and reduces the need of data sharing.
This
entire scenario is called Asymmetric Multiprocessing.
A second approach uses Symmetric Multiprocessing where each
processor is self scheduling. All processes may be in a common
ready queue or each processor may have its own private queue for
ready processes. The scheduling proceeds further by having the
scheduler for each processor examine the ready queue and select
a
process to execute.

9
Lecture - VI BITS Pilani, Pilani Campus
ALGORITHM EVALUATION
Selecting an algorithm from many scheduling algorithms is a
difficult process. Defining the criteria to select the algorithm is
the first problem. Normally, criteria are often defined in terms
of CPU utilization, response time and throughput. The criteria
may include the several measures such as
• Maximize CPU utilization under the constraint that the
maximum response time is 1 second.
• Maximize throughput such that turnaround time is ( on
average ) linearly proportional to total execution time
Once the selection criteria is defined, we want to evaluate the
various algorithms under consideration.
There are three types of algorithm evaluation methods. They
are
:
10
Lecture - VI BITS Pilani, Pilani Campus
ALGORITHM EVALUATION
1. Deterministic Modeling
2. Queuing Models
3. Simulations
Deterministic Modeling
One major class of evaluation methods is called analytic evaluation.
Analytic evaluation uses the given algorithm and the system
workload to produce a formula or number that evaluates the
performance of the algorithm for that workload.
One type of analytic evaluation is deterministic modelling.
For example, assume that we have the workload for 5 processes is
given below

11
Lecture - VI BITS Pilani, Pilani Campus
ALGORITHM EVALUATION
Process Burst Time
P1 10
P2 29
P3 3
P4 7
P5 12
Calculate the average waiting time for each method.
For FCFS,
Average Waiting time = ( 0 + 10 + 39 + 42 + 49 ) / 5 = 28
milliseconds
For SJF,
Average Waiting time = ( 10 + 32 + 0 + 3 + 20 ) / 5 = 13
milliseconds
12
Lecture - VI BITS Pilani, Pilani Campus
ALGORITHM EVALUATION
For RR( with quantum time 3),
Average Waiting time = ( 0 + 32 + 30 + 23 + 40 ) / 5 = 23 milliseconds
In this case, the SJF policy results in less than one – half of the
average time required for FCFS scheduling and Round Robin
Scheduling gives an intermediate value.
Deterministic modelling is simple as we know the exact numbers.
Queuing Models
The computer system is described as a network of server. Each
server has a queue of waiting processes. The CPU is a server with its
ready queue and I/O system with its ready queue. Knowing arrival
rates and service rates, we can determine utilization, average queue
length , average waiting time and so on. This is called as queuing
network analysis.

13
Lecture - VI BITS Pilani, Pilani Campus
ALGORITHM EVALUATION
As an example, let ‘n’ be the average queue length ( excluding the
process being serviced), let W be the average waiting time in the
queue and ƛ be average rate for new processes in the queue(such as
3 processes per second). We expect that during the time W that a
process waits, ƛ X W new processes will arrive in the queue. If the
system is in a steady state, then the number of processes leaving the
queue is equal to the number of processes that arrive.
So,
n=ƛXW
This equation is called as Little’s formula.
Simulations
To get more accurate evaluation of scheduling algorithm, we use
simulations.

14
Lecture - VI BITS Pilani, Pilani Campus
ALGORITHM EVALUATION
Simulation imitates the operation of real world processes or
systems
with the use of models. The model represents the key behaviours
and characteristics of the selected process or system while the
simulation represents how the model evolves under different
conditions over time.

15
Lecture - VI BITS Pilani, Pilani Campus
THREAD SCHEDULING
Many computer configurations have a single CPU. Hence,
threads run one at a time in such a way as to provide an
illusion of concurrency. Execution of multiple threads on a
single CPU in some order is called scheduling. The Java
runtime environment supports a very simple, deterministic
scheduling algorithm called fixed-priority scheduling. This
algorithm schedules threads on the basis of their priority
relative to other Runnable threads.
When a thread is created, it inherits its priority from the thread
that created it. You also can modify a thread's priority at any
time after its creation by using the setPriority method. Thread
other threads.

16
Lecture - VI BITS Pilani, Pilani Campus
THREAD SCHEDULING
Thread priorities are integers ranging between MIN_PRIORITY
And MAX_PRIORITY (Constants defined in the Thread Class).
The higher the integer, the higher the priority. At any given
time, when multiple threads are ready to be executed, the
runtime system chooses for execution the Runnable thread that
has the highest priority. Only when that thread stops, yields, or
becomes Not Runnable will a lower-priority thread start
executing. If two threads of the same priority are waiting for
the CPU, the scheduler arbitrarily chooses one of them to run.
The chosen thread runs until one of the following conditions is
true:
• A higher priority thread becomes runnable.

17
Lecture - VI BITS Pilani, Pilani Campus
THREAD SCHEDULING
• It yields, or its run method exits.
• On systems that support time-slicing, its time allotment has
expired.
Then the second thread is given a chance to run, and so on,
until the interpreter exits.
The Java runtime system's thread scheduling algorithm is also
preemptive. If at any time a thread with a higher priority than
all other Runnable threads becomes Runnable, the runtime
system chooses the new higher-priority thread for execution.
The new thread is said to preempt the other threads.
http://www.btechsmartclass.com/java/java-threads-
priority.html

18
Lecture - VI BITS Pilani, Pilani Campus
Thanks

19
BITS Pilani, Pilani Campus
OPERATING SYSTEM

BITS Pilani
Pilani Campus
BITS Pilani
Pilani Campus

PROCESS SYNCHRONIZATION
Learning Objectives
 What is Process Synchronization?
 How does Synchronization work?
 Elements of Critical Section
 General format of the Sections of a program
 What is Critical Section Problem?
 Rules for Critical Section
 Solutions to the Critical section
 Synchronization Hardware
 Semaphore

3
Lecture - VII BITS Pilani, Pilani Campus
PROCESS SYNCHRONIZATION

Process Synchronization is the task of coordinating the


execution of processes in a way that no two processes can
have access to the same shared data and resources.
It is specially needed in a multi-process system when multiple
processes are running together, and more than one processes
try to gain access to the same shared resource or data at the
same time.
This can lead to the inconsistency of shared data. So the
change made by one process not necessarily reflected when
other processes accessed the same shared data. To avoid this
type of inconsistency of data, the processes need to be
synchronized with each other.

4
Lecture - VII BITS Pilani, Pilani Campus
PROCESS SYNCHRONIZATION

How Process Synchronization Works?


For Example, process A changing the data in a memory
location while another process B is trying to read the data
from
the same memory location. There is a high probability that
data read by the second process will be erroneous.

Write Read

Process 1 Data Process 2

MEMORY
5
Lecture - VII BITS Pilani, Pilani Campus
PROCESS SYNCHRONIZATION

Sections of a Program
Here, are four essential elements of the critical section:
• Entry Section: It is part of the process which decides the
entry of a particular process.
• Critical Section: This part allows one process to enter and
modify the shared variable.
• Exit Section: Exit section allows the other process that are
waiting in the Entry Section, to enter into the Critical
Sections. It also checks that a process that finished its
execution should be removed through this Section.
• Remainder Section: All other parts of the Code, which is
not in Critical, Entry, and Exit Section, are known as the
Remainder Section.
6
Lecture - VII BITS Pilani, Pilani Campus
PROCESS SYNCHRONIZATION
Do
{

Entry Section

Critical Section Code Segment

Exit Section

Remainder Section

} while(true);
7
Lecture - VII BITS Pilani, Pilani Campus
PROCESS SYNCHRONIZATION

What is Critical Section Problem?


A critical section is a segment of code which can be accessed
by a signal process at a specific point of time. The section
consists of shared data resources that required to be accessed
by other processes.
In the critical section, only a single process can be executed.
Other processes, waiting to execute their critical section, need
to wait until the current process completes its execution.
Rules for Critical Section
The critical section need to must enforce all three rules:
(i) Mutual Exclusion: Mutual Exclusion is a special type of
binary semaphore which is used for controlling access to the
shared resource. It includes a priority inheritance mechanism
8
Lecture - VII BITS Pilani, Pilani Campus
PROCESS SYNCHRONIZATION

to avoid extended priority inversion problems. Not more than


one process can execute in its critical section at one time.
(ii) Progress: This solution is used when no one is in the
Critical section, and someone wants in. Then those processes
not in their reminder section should decide who should go in,
in a finite time.
(iii) Bound Waiting: When a process makes a request for
getting into critical section, there is a specific limit about
number of processes can get into their critical section. So,
when the limit is reached, the system must allow request to the
process to get into its critical section.

9
Lecture - VII BITS Pilani, Pilani Campus
PROCESS SYNCHRONIZATION

Solutions To The Critical Section


In Process Synchronization, critical section plays the main role
so that the problem must be solved.
Here are some widely used methods to solve the critical
section problem.
Solution 1:-
We declare an array : boolean flag[2];
If Flag[i] is true then
Pi is ready to enter into the critical region.
The structure is :
do
{
Flag[i]=true;
10
Lecture - VII BITS Pilani, Pilani Campus
PROCESS SYNCHRONIZATION

while(flag[j]) ;
Critical Section;
flag[i]=false;
remainder section;
} While(1);
For Process Pi, sets Flag[i] to be true, signaling that it is ready
to enter into its critical section. Then Pi checks whether it can
enter into its critical section. If not, Pi waits till Pj complete its
[Flag[j] becomes False]. Then Pi is allowed to enter its critical
section. On exiting the critical sectionm Pi sets Flag[i] to be
false.
Mutual exclusion satisfied but Progress requirement is not
satisfied.
11
Lecture - VII BITS Pilani, Pilani Campus
PROCESS SYNCHRONIZATION

2.Peterson Solution
Peterson's solution is widely used solution to critical section
problems. This algorithm was developed by a computer
scientist Peterson that's why it is named as a Peterson's
solution.
In this solution, when a process is executing in a critical state,
then the other process only executes the rest of the code,
and
the opposite can happen. This method also helps to make
sure
that only a single process runs in the critical section at a
specific time.
Example : 12
Lecture - VII BITS Pilani, Pilani Campus
PROCESS SYNCHRONIZATION

P1 P2 P3 …… Pn
Flag[1]=False Flag[2]=False Flag[3]=False … Flag[n]=False
Do
{
Flag[j]=true;
turn=j;
while (Flag[j] && turn == j) ;
Critical Section
Flag[i]=False;
Remainder Section
} While (1);

13
Lecture - VII BITS Pilani, Pilani Campus
PROCESS SYNCHRONIZATION
• Assume there are N processes (P1, P2, ... PN) and every
process at some point of time requires to enter the Critical
Section
• A FLAG[] array of size N is maintained which is by default
false. So, whenever a process requires to enter the critical
section, it has to set its flag as true. For example, If Pi wants
to enter it will set FLAG[i]=TRUE.
• Another variable called TURN indicates the process number
which is currently wafting to enter into the CS.
• The process which enters into the critical section while
exiting would change the TURN to another number from the
list of ready processes.
Example: turn is 2 then P2 enters the Critical section and while
exiting turn=3 and therefore P3 breaks out of wait loop.
14
Lecture - VII BITS Pilani, Pilani Campus
PROCESS SYNCHRONIZATION

Synchronization Hardware
Some times the problems of the Critical Section are also
resolved by hardware. Some operating system offers a lock
functionality where a Process acquires a lock when entering
the Critical section and releases the lock after leaving it.So
when another process is trying to enter the critical section, it
will not be able to enter as it is locked. It can only do so if it is
free by acquiring the lock itself.
Mutex Locks
Synchronization hardware not simple method to implement for
everyone, so strict software method known as Mutex Locks was
also introduced.

15
Lecture - VII BITS Pilani, Pilani Campus
PROCESS SYNCHRONIZATION

In this approach, in the entry section of code, a LOCK is


obtained over the critical resources used inside the critical
section. In the exit section that lock is released.
SEMAPHORE
Semaphore is simply a variable that is non-negative and
shared between threads. It is another algorithm or solution to
the critical section problem. It is a signaling mechanism and a
thread that is waiting on a semaphore, which can be signaled
by another thread.
Semaphore is also used for process synchronization. Assume
that multiple processes want to enter into critical section.
Semaphore Is the synchronization tool with the help of a
variable to access the critical section in mutually exclusive
16
Lecture - VII BITS Pilani, Pilani Campus
PROCESS SYNCHRONIZATION

way. It ensures that only one process is permitted to enter into


the critical section. To do this, semaphore uses two atomic
functions :wait ( ) and signal().
The code for the functions are:
Wait(S) : while S ≤ 0 do no – operation [ Not permitted
inside the critical section]
S=S-1;
Signal(S) : S=S+1;
It uses two atomic operations, 1)wait, and 2) signal for the
process synchronization.
Let mutex is the short form of mutual exclusion variable. Then
the critical section code can be written as

17
Lecture - VII BITS Pilani, Pilani Campus
PROCESS SYNCHRONIZATION

Repeat
Wait(mutex) Entry section
Critical Section
Signal(mutex) Exit section
Remainder section
Until (True);
It specifies that any process is allowed to enter into critical
section if mutex is 1. Before executing any process, assume
that mutex=1
When P1 wants to enter into the critical section but P0 is
already in the critical section, the value of mutex is 0. So it can
not enter into the critical section. So it has to wait till mutex is
changed to 1. Mutex is changed to 1 only when the running
18
Lecture - VII BITS Pilani, Pilani Campus
PROCESS SYNCHRONIZATION

process P0 has exited the critical section and execute the


Signal() function to make the value 0 mutex as 1 [Signal
function increments the value of mutex by 1].
CLASSICAL SYNCHRONIZATION PROBLEMS
The following are the classical examples of for
synchronization problems.
1. Producer and Consumer problem
2. Reader and Writer Problem
3. Diner – Philosopher Problem

19
Lecture - VII BITS Pilani, Pilani Campus
Thanks

20
BITS Pilani, Pilani Campus
OPERATING SYSTEM

BITS Pilani SESSION 8


Pilani Campus
BITS Pilani
Pilani Campus

CLASSICAL PROBLEMS
Learning Objectives
 Classical problem 1- Bounded Buffer problem / Producer and
Consumer problem
- Definition
- How the problem is synchronized with Semaphore?
- Structures of Producer and Consumer
 Classical problem 2 – Dining and Philosopher problem
- Definition
- Solving the problem with Semaphore
 What are monitors?
 Solving Dining and Philosopher problem using monitors

3
Lecture - VIII BITS Pilani, Pilani Campus
PROCESS SYNCHRONIZATION- Bounded
Buffer Problem
- This problem is also called as producer – consumer problem
- It is problem based on synchronization
- There are two enteritis such as Producer and Consumer
- Producer produces a product and it should be placed on the
container [buffer]
- Consumer consumes the product that is available in the
container
- The container should be used by only one of them , either the
producer or the consumer
1 2 3 4 ……… N

Product Consumer

4
Lecture - VIII BITS Pilani, Pilani Campus
PROCESS SYNCHRONIZATION
Solution to the problem is ,
- Create two counting semaphores “Full” and “Empty” to track
whether the buffer/ container is full or empty
- When the container is full, the producer can not place the
product in it
- When the container is empty, the consumer can not take any
product to consume
The procedure for the problem is written as follows:
Structure of Producer
While (True)
{
wait(empty);
wait(mutex);
Add Item into the buffer
5
Lecture - VIII BITS Pilani, Pilani Campus
PROCESS SYNCHRONIZATION
signal(mutex);
signal(empty);
}
Structure of the Consumer
While (True)
{
wait(full);
wait(mutex);
Delete Item from the buffer
signal(mutex);
signal(empty);
}

6
Lecture - VIII BITS Pilani, Pilani Campus
PROCESS SYNCHRONIZATION
The following are the variables what we use,
• Full represents how many elements / products are in the
container / buffer
• Empty represents how many elements / products can be
inserted into the container / buffer
• N is the size of the buffer/ container
Initially, mutex is = 1.
Case 1: if consumer tries to consume a product from the empty
container,
Mutex = 1, Full = 0 and Empty = N [ All the cells are free]
while(true) – Condition is true
wait(full) -> wait(0) : do no-operation;
Case 2: if producer tries to add a product ,

7
Lecture - VIII BITS Pilani, Pilani Campus
PROCESS SYNCHRONIZATION
while (true ) // Initially true
waif(empty) -> wait(N); Here N is not <=0, so permits the
operation, N = N - 1

wait(mutext) -> wait(1): Here 1 is not <=0, so permits the


operation, mutex = mutex-1 =0
Add Item to the Buffer / Container
signal(mutex) -> Increments mutex by 1. Now mutex = 1
signal(full) -> Increments full by 1. Full = 1
Now the Buffer has a single product as

P Empty = n - 1
Full = 1

8
Lecture - VIII BITS Pilani, Pilani Campus
PROCESS SYNCHRONIZATION
Now if Consumer wants to consume the product, the code is
executed as follows:
while(true) – Condition is true
wait(full) -> wait(1) : Here 1 is not <=0, so
permits the operation, full= full-1 =0
wait(mutex) -> wait(1) : Here 1 is not <=0, so
permits the operation, mutex=
mutex-1 =0
Removes the Product from the buffer
Signal(mutex) -> mutext = mutex + 1 = 1
Signal(empty) -> empty = empty + 1 = N – 1 + 1=
N

9
Lecture - VIII BITS Pilani, Pilani Campus
PROCESS SYNCHRONIZATION – Dining
Philosopher Problem
What is Dining Philosopher Problem?
It states that there are 5 philosophers (may be more than 5 also)
sharing a circular table and they eat and think alternatively. Thre 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 only eat if there are both the chopsticks
available. Otherwise the philosopher puts down their chopstick and
begin thinking again.
It is a classical synchronization problem as it demonstrates a large
class of concurrency control problem.
Solution:
The Solution for this 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
10
Lecture - VIII BITS Pilani, Pilani Campus
PROCESS SYNCHRONIZATION
Semaphore. The structure of the chopstick is
semaphore Chopstick [5];
Initially the elements of the chopstick are initialized to as they are
on
the table and picked by any philosopher.
The Structure of random philosopher i to eat is given as follows:
While (True)
{
wait(Chopstick[i] );
wait(Chopstick[ (i+1) % 5 );
Eating Rice ;
Signal(Chopstick(i]);
Signal(Chopstick[( i + 1 ) % 5 );
}
11
Lecture - VIII BITS Pilani, Pilani Campus
PROCESS SYNCHRONIZATION
Working out the procedure:
Initially, all the chopsticks [i] = 1 , i = 0 to 4 [When chopstick[i]=1, it
is free that is un used now].
Now Philosopher [2] wants to eat. Then the procedure is executed as
follows:
while ( true ) // Initially True
{
wait(chopstick[i])->wait(chopstick [ 2 ] ) – wait[1]
- Here 1 is not <=0, so permits the operation,
Chopstick[2]= Chopstick[2]-1=1-1=0
wait(chopstick[(i+1)%5])->wait(chopstick [ 3 ] ) – wait[1]
- Here 1 is not <=0, so permits the operation,
Chopstick[3]= Chopstick[3]-1=1-1=0

12
Lecture - VIII BITS Pilani, Pilani Campus
PROCESS SYNCHRONIZATION
The Philosopher can eat
Signal(Chopstick[i]) – Signal(Chopstick[2])
Chopstick[2]=Chopstick[2]+1 =1
Signal (Chopstick[(i+1) % 5 ])- Signal(Chopstick[2])
Chopstick[2]=Chopstick[2]+1 =1
Now both the chopsticks are available.
Note:- When Philosopher[2] was eating (Chopstick[2]=0 and
Chopstick[3]=0), if philosopher[1] also wants to eat ,
Executing the same procedure, we have
While (True)
{
wait(chopstick[1]) -> wait(1); No problem Chopstick is available
wait(chopstick[(i+1)%5]->wait(chopstick[2]); Stopped as
Chopstick[2]=0;
13
Lecture - VIII BITS Pilani, Pilani Campus
PROCESS SYNCHRONIZATION
Monitors
- Are also used for synchronization
- High level abstraction that provides and effective mechanism
for process synchronization
- A monitor type presents a set of operations that provide
mutual exclusive within the monitor
- To enter into the critical section, wait( ) function is called
whereas the signal( ) function when we exit form the critical
section. If the wait() and signal() functions are not properly
coded, it will lead to a very big problem in the entire system
- The monitor type also contains the declaration of variables
whose values are accessed by the functions declared inside the
abstraction
The Syntax for a monitor is

14
Lecture - VIII BITS Pilani, Pilani Campus
PROCESS SYNCHRONIZATION
monitor monitor_name
{
Shared variable declaration;
procedure P1()
{
______
______
}
procedure P2()
{
______
______
}
15
Lecture - VIII BITS Pilani, Pilani Campus
PROCESS SYNCHRONIZATION
procedure P3()
{
______
______
}
initialization Code()
{
______
______
}
} // End of the Monitor

16
Lecture - VIII BITS Pilani, Pilani Campus
PROCESS SYNCHRONIZATION
The monitor also consists of conditional construct and is declared as
condition x,y;
• The operation that can be invoked on a condition variable ae wait()
and Signal()
• The operation x.wait() means that the process invoking tjis is
suspended until another process invokes x.signal()
• X.signal() operation resumes exactly one suspended process.
The Dining Philosopher problem is solved with the monitor as
monitor DiningPhilosophers
{
enum { THINKING,HUNGRY,EATING } State [5];
condition self[5];
void PickUp(int i)
{
17
Lecture - VIII BITS Pilani, Pilani Campus
PROCESS SYNCHRONIZATION
state[i]=HUNGRY;
test(i);
if (state[i]!=EATING) self[i].wait();
}
void putdown(int i)
{
state[i]=THINKING;
// Test the left and right neighbors
test((i+4)%5);
test((i+1)%5);
}
void test(int i)
{

18
Lecture - VIII BITS Pilani, Pilani Campus
PROCESS SYNCHRONIZATION
if((state[(i+4)%5] != EATING ) &&
(state[i]= = HUNGRY ) &&
(state[ (i+1) % 5 ] != EATING ) )
{
state[i]=EATING;
self[i].signal();
}
}
Initialization_Code()
{
for (i=0;i<5;i++)
state[i]=THINKING;
}
}
19
Lecture - VIII BITS Pilani, Pilani Campus
Thanks

20
BITS Pilani, Pilani Campus
OPERATING SYSTEM

BITS Pilani SESSION 9


Pilani Campus
BITS Pilani
Pilani Campus

DEADLOCK
Learning Objectives
 What is Deadlock?
 Example for Deadlock
 Necessary conditions for deadlock
 Deadlock handling mechanisms
- Deadlock Prevention
- Deadlock Avoidance
- Deadlock Prevention and Recovery

3
Lecture - IX BITS Pilani, Pilani Campus
DEADLOCK

Deadlock
In an operating system, there are many processes (processes
are programs that are currently being executed) running
continuously. All of these processes are important for the
functioning of the computer. It defines the basic unit of work that
has to be implemented by the system. Processes follow
sequential execution.
These processes require some resources to run or finish their
execution. Sequential order is followed here. The process first
requests for a resource. If it is available, the OS grants the
resource. If not, the process has to wait. After the process has
used the resource, it is released. If a problem arises during the
waiting and releasing, it means that there is a deadlock situation.
4
Lecture - IX BITS Pilani, Pilani Campus
DEADLOCK

What is Deadlock?
A deadlock is a common situation in operating systems where a
process waiting for a resource can be executed because that
resource is currently held by another process and is being
utilised for its execution, therefore, the process does not get
executed. Moreover, many other processes may also be waiting
for a resource to be released. A deadlock situation is created
Let us assume that there are two processes called P1 and P2.
Resources R1 and R2 are assigned to them respectively. So if
P1 needs R2 to complete its execution, but it is held by P2, P1
has to wait. Similarly, P2 may be waiting for R1 that is held by
P1. Thus, P2 has to wait until P1 has released the resource. As
a result, a deadlock situation arises and both processes are not
5
Lecture - IX BITS Pilani, Pilani Campus
DEADLOCK

able to complete their execution. As the processes are blocked,


the system may become unresponsive.
So deadlock is the situation in which multiple processes are
waiting infinitely in which there is no progress for waiting
processes.

6
Lecture - IX BITS Pilani, Pilani Campus
DEADLOCK

Example of Deadlock
• A real-world example would be traffic, which is going only in
one direction.
• Here, a bridge is considered a resource.
• So, when Deadlock happens, it can be easily resolved if one
car backs up (Preempt resources and rollback).
• Several cars may have to be backed up if a deadlock situation
occurs.
• So starvation is possible.

7
Lecture - IX BITS Pilani, Pilani Campus
DEADLOCK

8
Lecture - IX BITS Pilani, Pilani Campus
DEADLOCK
Necessary conditions for deadlock
1. Mutual exclusion
The processes are permitted to use the shared resource in
mutual exclusive way. Only one process is permitted to use a
shared resource. When a resource is used by another process, a
new process is permitted to use it. If mutual exclusion is not
followed, there is a possibility of deadlock
2. Hold and Wait condition
The process which asks for a resource is holding a resource
already. If so, a deadlock may occur
3. Pre emption
When shared resource can be pre-empted, there is a possibility of
Deadlock. Pre emption allocates the resource held by a process to
another process.
9
Lecture - IX BITS Pilani, Pilani Campus
DEADLOCK
4. Circular wait
If the processes are waiting in the circular fashion such as the
following diagram, deadlock may occur.

10
Lecture - IX BITS Pilani, Pilani Campus
DEADLOCK

If all the above conditions are satisfied, deadlock may occur.


Deadlock Handling Mechanism
Deadlock detection, deadlock prevention and deadlock
avoidance are the main methods for handling deadlocks.
– Deadlock Prevention
– Deadlock avoidance
– Deadlock Detection & Recovery
Deadlock Prevention
In this method, we have to include the preventive mechanism to
stop the deadlock to occur. In this procedure, any one of the
necessary condition mentioned above should be made as False.
Deadlock prevention procedure removes mutual exclusion by
making all the resources sharable
11
Lecture - IX BITS Pilani, Pilani Campus
DEADLOCK

Deadlock Prevention procedure removes hold and wait condition


by not permitting any process to waits for any resource if it holds
another process already. It is done in any of the following
procedure:
• A process must acquire all the necessary resources before
entire system starts. It reduces the utilization of the resources
• A process that holds some resources requests for other
resources is asked to release the acquired resource first.
Starvation is the problem of this method
• Apply wait time out procedure. In this procedure, a process
that holds some resources is asked to release the acquired
resource for a particular period of time though the resources
may be requested or not requested by another process.
12
Lecture - IX BITS Pilani, Pilani Campus
DEADLOCK

Deadlock prevention procedure pre-empts by removing a


process forcefully. It removes the waiting process and not the
running process. When a process requests a resource, any one
of the following cases is selected:
Case 1 : If the resource is available [not used / not held by
another process], the resource is allocated to the process
Case 2: If the resource is unavailable [used / held by another
process], the resource is released from the holding process
which waits for another resource.
Deadlock prevention procedure removes the circular wait
condition by using following procedure:
Assign the integer to the resources available.
The process Pi which holds Rj is not permitted to request for
13
Lecture - IX BITS Pilani, Pilani Campus
DEADLOCK

the Resource Rk where [k>j].


To request a resource Rj, the process must release all Ri such
that i>=j.
Here P1 holds R2, it can’t hold R1 because R2 > R1.
Example:
Assume that there are three resources such as Printer, CPU and
Memory. The integer numbers assigned to them are as follows:
Printer 1
CPU 5
Memory 6
Now a process P1 holds Printer and Memory [integer numbers 1
and 6 respectively]. If it requests for the resource > 6, there will
be no problem. But if it requests CPU [integer number 5], it has
14
Lecture - IX BITS Pilani, Pilani Campus
DEADLOCK

to release Memory and then it can hold CPU.


DEADLOCK AVOIDANCE
In deadlock avoidance, the resource for any process is granted if
the resulting state of the system does not cause any deadlock in
the system. The state of the system is continuously checked for
Safe and Unsafe states.
In order to avoid deadlock, the process must tell OS , the
maximum number of resources a process can request to
complete its execution. The deadlock avoidance algorithm
examines the resource allocations so that there can never be a
circular wait condition

15
Lecture - IX BITS Pilani, Pilani Campus
DEADLOCK

The deadlock Avoidance method is used by the operating


system in order to check whether the system is in a safe state or
in an unsafe state and in order to avoid the deadlocks, the
process must need to tell the operating system about the
maximum number of resources a process can request in order to
complete its execution.
How does Deadlock Avoidance work?
In this method, the request for any resource will be granted only
if the resulting state of the system doesn't cause any deadlock in
the system. This method checks every step performed by the
operating system. Any process continues its execution until the
system is in a safe state. Once the system enters into an unsafe
state, the operating system has to take a step back.
16
Lecture - IX BITS Pilani, Pilani Campus
DEADLOCK

With the help of a deadlock-avoidance algorithm, you can


dynamically assess the resource-allocation state so that there
can never be a circular-wait situation.
According to the simplest and useful approach, any process
should declare the maximum number of resources of each type it
will need. The algorithms of deadlock avoidance mainly examine
the resource allocations so that there can never be an
occurrence of circular wait conditions.
Safe State and Unsafe State
A state is safe if the system can allocate resources to each
process( up to its maximum requirement) in some order and still
avoid a deadlock. Formally, a system is in a safe state only, if
there exists a safe sequence. So a safe state is not a
17
Lecture - IX BITS Pilani, Pilani Campus
DEADLOCK

deadlocked state and conversely a deadlocked state is an


unsafe state.
In an Unsafe state, the operating system cannot prevent
processes from requesting resources in such a way that any
deadlock occurs. It is not necessary that all unsafe states are
deadlocks; an unsafe state may lead to a deadlock

The above Figure shows the Safe, unsafe, and deadlocked state
spaces
18
Lecture - IX BITS Pilani, Pilani Campus
DEADLOCK
Deadlock Avoidance Example
Let us consider a system having 12 magnetic tapes and three
processes P1, P2, P3. Process P1 requires 10 magnetic tapes,
process P2 may need as many as 4 tapes, process P3 may
need up to 9 tapes. Suppose at a time to, process P1 is holding
5 tapes, process P2 is holding 2 tapes and process P3 is
holding 2 tapes. (There are 3 free magnetic tapes)
Processes Maximum Needs Current Needs

P1 10 5

P2 4 2

P3 9 2

19
Lecture - IX BITS Pilani, Pilani Campus
DEADLOCK

So at time t0, the system is in a safe state. The sequence is


<P2,P1,P3> satisfies the safety condition. Process P2 can
immediately be allocated all its tape drives and then return them.
After the return the system will have 5 available tapes, then
process P1 can get all its tapes and return them ( the system will
then have 10 tapes); finally, process P3 can get all its tapes and
return them (The system will then have 12 available tapes).
A system can go from a safe state to an unsafe state. Suppose
at time t1, process P3 requests and is allocated one more tape.
The system is no longer in a safe state. At this point, only
process P2 can be allocated all its tapes. When it returns them
the system will then have only 4 available tapes. Since P1 is
allocated five tapes but has a maximum of ten so it may request
20
Lecture - IX BITS Pilani, Pilani Campus
DEADLOCK

5 more tapes. If it does so, it will have to wait because they are
unavailable. Similarly, process P3 may request its additional 6
tapes and have to wait which then results in a deadlock.
The mistake was granting the request from P3 for one more
tape. If we made P3 wait until either of the other processes had
finished and released its resources, then we could have avoided
the deadlock
Note: In a case, if the system is unable to fulfill the request of all
processes then the state of the system is called unsafe.
The main key of the deadlock avoidance method is whenever
the request is made for resources then the request must only be
approved only in the case if the resulting state is a safe state.
21
Lecture - IX BITS Pilani, Pilani Campus
DEADLOCK

Deadlock Avoidance Solution


Deadlock Avoidance can be solved by two different algorithms:
1. Resource allocation Graph
2. Banker's Algorithm

22
Lecture - IX BITS Pilani, Pilani Campus
Thanks

23
BITS Pilani, Pilani Campus
OPERATING SYSTEM

BITS Pilani SESSION 10


Pilani Campus
BITS Pilani
Pilani Campus

DEADLOCK - CONTINUATION
Learning Objectives
 Dead lock avoidance
- Safe and Unsafe states
- Resource allocation graph
- Banker’s Algorithm
 Deadlock Detection and Recovery
- Detection
(i) Wait for Graph
(ii) Banker’s Algorithm
- Recovery
(i) Process Termination
(ii) Resource Termination/ Pre-emption
3
Lecture - X BITS Pilani, Pilani Campus
DEADLOCK AVOIDANCE

In deadlock avoidance, the resource for any process is granted if


the resulting state of the system does not cause any deadlock in
the system. The state of the system is continuously checked for
Safe and Unsafe states.
In order to avoid deadlock, the process must tell OS , the
maximum number of resources a process can request to
complete its execution. The deadlock avoidance algorithm
examines the resource allocations so that there can never be a
Circular wait condition
How does Deadlock avoidance work?
Let's understand the working of Deadlock Avoidance with the
help of an intuitive example.
4
Lecture - X BITS Pilani, Pilani Campus
DEADLOCK AVOIDANCE

Process Maximum Required current Available

P1 9 5

P2 5 2

P3 3 1

Let us assume that there are three processes P1, P2, P3. Some
more information on which the processes tells the Operating
System are :
• P1 process needs a maximum of 9 resources (Resources like
tape drive or printer etc..) to complete its execution. P1 is
currently allocated with 5 Resources and needs 4 more to
complete its execution.
5
Lecture - X BITS Pilani, Pilani Campus
DEADLOCK AVOIDANCE

• P2 process needs a maximum of 5 resources and is currently


allocated with 2 resources. So it needs 3 more resources to
complete its execution.
• P3 process needs a maximum of 3 resources and is currently
allocated with 1 resource. So it needs 2 more resources to
complete its execution.
Operating System knows that only 2 resources out of the total
available resources are currently free.
But only 2 resources are free now. Can P1, P2, and P3 satisfy their
requirements? Let's try to find out.
As only 2 resources are free for now, then only P3 can satisfy its
need for 2 resources. If P3 takes 2 resources and completes its
execution, then P3 can release its 3 (1+2) resources. Now the
three free resources which P3 released can satisfy the need
6
Lecture - X BITS Pilani, Pilani Campus
DEADLOCK AVOIDANCE

of P2. Now, P2 after taking the three free resources, can


complete its execution and then release 5 (2+3) resources. Now
five resources are free. P1 can now take 4 out of the 5 free
resources and complete its execution. So, with 2 free resources
available initially, all the processes were able to complete their
execution leading to Safe State. The order of execution of the
processes was <P3, P2, P1>.
What if initially there was only 1 free resource available? None of
the processes would be able to complete its execution. Thus
leading to an unsafe state.
We use two words, safe and unsafe states. What are those
states? Let's understand these concepts.
7
Lecture - X BITS Pilani, Pilani Campus
DEADLOCK AVOIDANCE

Safe State and Unsafe State


Safe State - In the above example, we saw that Operating
System was able to satisfy the need of all three
processes, P1, P2, and P3, with their resource requirements. So
all the processes were able to complete their execution in a
certain order like P3->P2->P1.
So, If Operating System is able to allocate or satisfy the
maximum resource requirements of all the processes in any
order then the system is said to be in Safe State.
So safe state does not lead to Deadlock.
Unsafe State - If Operating System is not able to prevent
Processes from requesting resources which can also lead to
Deadlock, then the System is said to be in an Unsafe State.
8
Lecture - X BITS Pilani, Pilani Campus
DEADLOCK AVOIDANCE

Unsafe State does not necessarily cause deadlock it may or


may not causes deadlock.
Deadlock Avoidance Solution
Deadlock Avoidance can be solved by two different algorithms:
• Resource allocation Graph
• Banker's Algorithm
We will discuss both algorithms in detail in their separate article.
Resource Allocation Graph
Resource Allocation Graph (RAG) is used to represent the state
of the System in the form of a Graph. The Graph contains all
processes and resources which are allocated to them and also
the requesting resources of every Process. Sometimes if the
number of processes is less, We can easily identify a deadlock
9
Lecture - X BITS Pilani, Pilani Campus
DEADLOCK AVOIDANCE

in the System just by observing the Graph, which can not be


done easily by using tables that we use in Banker's algorithm.
Resource Allocation Graph has a process vertex represented by
a circle and a resource vertex represented by a box. The
instance of the resources is represented by a dot inside the box.
The instance can be single or multiple instances of the resource.
An example of RAG is shown below.
Banker's Algorithm
Banker's algorithm does the same as we explained the Deadlock
avoidance with the help of an example. The algorithm
predetermines whether the System will be in a safe state or not
by simulating the allocation of the resources to the processes
according to the maximum available resources.
10
Lecture - X BITS Pilani, Pilani Campus
DEADLOCK AVOIDANCE

It makes an "s-state" check before actually allocating the


resources to the Processes.
When there are more number of Processes and many
Resources, then Banker's Algorithm is useful.
It is used to avoid deadlocks when multiple instances of each
resource type are present. This is not possible, using the
methods like safe state and resource allocation graphs. It is
similar to a banking system where a bank never allocates cash
in such a way that it could not satisfy the needs of all its
customers and also it cannot allocate more than what is
available. Here, customers are analogous to processes, cash to
resources, and bank to the operating system.
11
Lecture - X BITS Pilani, Pilani Campus
DEADLOCK AVOIDANCE

A process must specify in the beginning the maximum number of


instances of each resource type it may require. It is obvious that
this number should not be more than the available. When the
process requests resources, the system decides whether
allocation will result in deadlock or not. If not, resources are
allocated otherwise process has to wait.
The following are the various data structures that have to be
created to implement Banker's algorithm. If 'n' is the number of
processes and 'm' is the number of resources.
Max : A 'n × m' matrix indicating the maximum resources
required by each process.
Allocation : A 'n × m' matrix indicating the number of resources
already allocated to each process. 12
Lecture - X BITS Pilani, Pilani Campus
DEADLOCK AVOIDANCE

Need : A 'n × m' matrix indicates the number of resources required by


each process.
Available : It is a vector of size 'm' which indicates the resources that
are still available (not allocated to any process).
Request : It is a vector of size 'm' which indicates that process Pi has
requested some resources.
Each row of matrices "allocation" and "need" can be referred to
as vectors. Then "allocation" indicates the resources currently
allocated to process Pi and "need" refers to resources required
by Pi. The following algorithm is used to determine whether the
request can be safely granted or not.
Step 1 - If Requesti ≤ Needi, then proceed to step two, otherwise raise
an exception saying the process has exceeded its maximum claim.
13
Lecture - X BITS Pilani, Pilani Campus
DEADLOCK AVOIDANCE

Step 2 - If Requesti ≤ Available, then proceed to step three,


otherwise block Pi because resources are not available.
Step 3 - Allocate resources to Pi as follows,
Available = Available - Requesti
Allocationi = Allocationi + Requesti
Needi = Needi - Requesti
Safety Algorithm :
The job of the banker's algorithm is to perform allocation, it will
not see whether this allocation has resulted in a safe or unsafe
state. It is the safety algorithm that is called immediately after the
banker's algorithm to check for the system state after allocation.
The following is the safety algorithm that requires m x
n2 operations to find the system state.
14
Lecture - X BITS Pilani, Pilani Campus
DEADLOCK AVOIDANCE

Step 1 - Assume work and finish as vectors of length 'm' and 'n'
respectively.
Work = Available
Finish[i] = 'false'
Step 2 - Find 'i' such that,
Finish[i] = 'false'
Needi ≤ Work
If no such 'i' is found jump to step four.
Step 3 - Work = Work + Allocation
Finish[i] = 'true'
Jump to step two.
Step 4 - If finish[i] = True for all then the system is in a safe
state. 15
Lecture - X BITS Pilani, Pilani Campus
DEADLOCK AVOIDANCE

Example of Banker’s Algorithm


Consider the following problem:

Allocation Max Available


Processes
A B C A B C A B C

P0 1 1 2 4 3 3 2 1 0

P1 2 1 2 3 2 2

P2 4 0 1 9 0 2

P3 0 2 0 7 5 3

P4 1 1 2 1 1 2
16
Lecture - X BITS Pilani, Pilani Campus
DEADLOCK AVOIDANCE

Do the following:
(i) Calculate the content of the need matrix?
(ii) Is the system in a safe state?
(iii) Determine the total amount of resources of each type?
Solution:
1. Content of the need matrix can be calculated by using the
below formula
Need = Max – Allocation

17
Lecture - X BITS Pilani, Pilani Campus
DEADLOCK AVOIDANCE

Safe sequence:
For process P0, Need = (3, 2, 1) and
Available = (2, 1, 0)
Need ? Available = False
So, the system will move for 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)
18
Lecture - X BITS Pilani, Pilani Campus
DEADLOCK DETECTION &
RECOVERY
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
19
Lecture - X BITS Pilani, Pilani Campus
DEADLOCK AVOIDANCE

= (4, 2, 2) + (1, 1, 2)
= (5, 3, 4) now, (New Available)
6. Now again check for Process P2, Need = (5, 0, 1)
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 20
Lecture - X BITS Pilani, Pilani Campus
DEADLOCK AVOIDANCE
Request of 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 system is in a safe state.
3. The total amount of resources = sum of columns of allocation
+ Available = [8 5 7] + [2 1 0] = [10 6 7]
21
Lecture - X BITS Pilani, Pilani Campus
DEADLOCK DETECTION &
RECOVERY
If a system does not employ either a deadlock-prevention or
deadlock-avoidance algorithm, then there are chances of
occurrence of a deadlock. In this case, the system may provide
two things: An algorithm is used to examines the state of the
system in order to determine whether a deadlock has
occurred. An algorithm that is used to recover from the
deadlock. Thus order to get rid of deadlocks the operating
system periodically checks the system for any deadlock. After
Finding the deadlock the operating system will recover from it
using recovery techniques.
Now, the main task of the operating system is to detect the
deadlocks and this is done with the help of Resource Allocation
Graph.
22
Lecture - X BITS Pilani, Pilani Campus
DEADLOCK DETECTION &
RECOVERY

23
Lecture - X BITS Pilani, Pilani Campus
DEADLOCK DETECTION &
RECOVERY
Single Instance of Each Resource Type
If all the resources have only a single instance, then a deadlock-
detection algorithm can be defined that mainly uses the variant
of the resource-allocation graph and is known as a wait-for
graph. This wait-for graph is obtained from the resource-
allocation graph by removing its resource nodes and collapsing
its appropriate edges.
An edge from Pi to Pj in a wait-for graph simply implies that
process Pi is basically waiting for process Pj in order to release a
resource that it needs. An edge Pi, Pj exists in a wait-for graph if
and only if the corresponding resource allocation graph contains
two edges Pi,Rq and Rq,Pj for a resource Rq.
24
Lecture - X BITS Pilani, Pilani Campus
DEADLOCK DETECTION &
RECOVERY
A deadlock exists in the system if and only if there is a cycle in
the wait-for graph. In order to detect the deadlock, the system
needs to maintain the wait-for graph and periodically system
invokes an algorithm that searches for the cycle in the wait-
for graph.
The algorithm that is used to detect the cycle in the graph mainly
requires n² operations; where n indicates the number of vertices
in the graph.
Multiple Instances of Each Resource Type
The above scheme that is a wait-for graph is not applicable to
the resource-allocation system having multiple instances of each
resource type. Now we will move towards a deadlock detection
algorithm that is is applicable for such systems.
25
Lecture - X BITS Pilani, Pilani Campus
DEADLOCK DETECTION &
RECOVERY
This algorithm mainly uses several time-varying data structures
that are similar to those used in Banker's Algorithm.
Recovery From Deadlock
When a detection algorithm determines that a deadlock exists
then there are several available alternatives. There one
possibility and that is to inform the operator about the deadlock
and let him deal with this problem manually.
Another possibility is to let the system recover from the deadlock
automatically. These are two options that are mainly used to
break the deadlock.

26
Lecture - X BITS Pilani, Pilani Campus
DEADLOCK DETECTION &
RECOVERY

27
Lecture - X BITS Pilani, Pilani Campus
DEADLOCK DETECTION &
RECOVERY
Process Termination
In order to eliminate deadlock by aborting the process, we will
use one of two methods given below. In both methods, the
system reclaims all resources that are allocated to the
terminated processes.
Aborting all deadlocked Processes Clearly, this method is
helpful in breaking the cycle of deadlock, but this is an expensive
approach. This approach is not suggestible but can be used if
the problem becomes very serious. If all the processes are killed
then there may occur insufficiency in the system and all
processes will execute again from starting.
Abort one process at a time until the elimination of the
deadlock cycle This method can be used but we have to decide
28
Lecture - X BITS Pilani, Pilani Campus
DEADLOCK DETECTION &
RECOVERY
which process to kill and this method incurs considerable
overhead. The process that has done the least amount of work is
killed by the Operating system firstly.
Resource Pre-emption
In order to eliminate the deadlock by using resource preemption,
we will successively preempt some resources from processes
and will give these resources to some other processes until the
deadlock cycle is broken and there is a possibility that the
system will recover from deadlock. But there are chances that
the system goes into starvation.

29
Lecture - X BITS Pilani, Pilani Campus
DEADLOCK DETECTION &
RECOVERY
The issues in the pre emption are :
(i) Selecting a victim – Which process is to be selected which
may lead to starvation
(ii) Rollback – We have to rollback to the beginning of the
process

30
Lecture - X BITS Pilani, Pilani Campus
Thanks

31
BITS Pilani, Pilani Campus
OPERATING SYSTEM

BITS Pilani SESSION 11


Pilani Campus
BITS Pilani
Pilani Campus

MEMORY MANAGEMENT
Learning Objectives
 Swapping
 Contiguous Memory allocation
- Fixed Size partition
- Variable size partition
- Block allocation list
- Bit Map
 Non Contiguous Memory allocation
- Segmentation
- Paging
- Segmentation with Paging

3
Lecture - XI BITS Pilani, Pilani Campus
MEMORY MANAGEMENT

SWAPPING
We know that all the processes are executed in the memory. A
process can be swapped temporarily out of memory to a backing
store and then brought back into memory for continued
execution.
For example, assume that a multiprogramming environment with
a round-robin CPU algorithm. When a quantum expires, the
memory manager will swap out the process that has just
reached the quantum time and to swap in another process to the
memory. Like this, when each process finishes its quantum time,
it will be swapped with another process. The quantum time must
be sufficiently large that reasonable amounts of computing are
done between swaps.
4
Lecture - XI BITS Pilani, Pilani Campus
MEMORY MANAGEMENT

It is depicted in the following diagram: Backing Store

Operating
System Process
1. Swapped out P1

2. Swapped In Process
P2

User Space

5
Lecture - XI BITS Pilani, Pilani Campus
MEMORY MANAGEMENT

When we use priority-based scheduling algorithm, another


swapping policy is used. If a higher priority process arrives and
wants to execute, the memory manager swaps out the lower
priority process so that higher priority process is executed. Once
the higher priority process finishes its execution, the lower
priority process is swapped back in and continued. This variant
swapping is sometimes is called as Roll out, Roll in.
Normally a process that is swapped out will be swapped back
into the same memory space that was occupied previously. This
restricted is dictated by the method of address binding.
Swapping requires backing store. It is commonly a fast disk. The
disk must have enough space to accommodate swapped out
process images for all the users. There are two alternatives to
6
Lecture - XI BITS Pilani, Pilani Campus
MEMORY MANAGEMENT

keep the swapped out process images on disk.


The first alternative is to create a separate swap file for each
swapped out process. But this method increases the number of
files and directory entries. It creates increased overhead that
requires high search time.
The second alternative is to create a common swap file that is
kept in the disk and the location of each swapped out process
image has to be noted in the common swap disk.
The major part of the swap time is transfer time. The total
transfer time is directly proportional to the amount of memory
swapped.

7
Lecture - XI BITS Pilani, Pilani Campus
MEMORY MANAGEMENT

CONTIGUOUS MEMORY ALLOCATION


The main memory has to accommodate both the operating
system and user space. Now, here the user space has to
accommodate various user processes. We also want these
several user processes must reside in the main memory at the
same time.
Contiguous memory allocation is a memory allocation method
that allocates a single contiguous section of memory to a
process or a file.
In Contiguous memory allocation, when the process arrives from
the ready queue to the main memory for execution, the
contiguous memory blocks are allocated to the process
according to its requirement. Now, to allocate the contiguous
8
Lecture - XI BITS Pilani, Pilani Campus
MEMORY MANAGEMENT

space to user processes, the memory can be divided either in


the fixed-sized partition or in the variable-sized partition.

9
Lecture - XI BITS Pilani, Pilani Campus
MEMORY MANAGEMENT

Fixed-Sized Partition: In the fixed-sized partition, the memory is


divided into fixed-sized blocks and each block contains exactly
one process. But, the fixed-sized partition will limit the degree of
multiprogramming as the number of the partition will decide the
number of processes.
Variable-Size Partition: In the variable size partition method,
the operating system maintains a table that contains the
information about all memory parts that are occupied by the
processes and all memory parts that are still available for the
processes.
Initially, the whole memory space is available for the user
processes as a large block, a hole. Eventually, when the
processes arrive in the memory, executes, terminates and
10
Lecture - XI BITS Pilani, Pilani Campus
MEMORY MANAGEMENT

leaves the memory we will see the set of holes of variable sizes.
In the figure above, we can see that when file A and file C
release the memory allocated to them, creates the holes in the
memory of variable size.
In the variable size partition method, the operating system
analyses the memory requirement of the process and see
whether it has a memory block of the required size.
If it finds the match, then it allocates that memory block to the
process. If not, then it searches the ready queue for the process
that has a smaller memory requirement.
The operating system allocates the memory to the process until
it cannot satisfy the memory requirement of the next process in
the ready queue. It stops allocating memory to the process if it
11
Lecture - XI BITS Pilani, Pilani Campus
MEMORY MANAGEMENT

does not have a memory block (hole) that is large enough to


hold that process.
If the memory block (hole) is too large for the process it
gets spilt into two parts. One part of the memory block is
allocated to the arrived process and the other part is returned to
the set of holes. When a process terminates and releases the
memory allocated to it, the released memory is then placed back
to the set of holes. The two holes that are adjacent to each
other, in the set of holes, are merged to form one large hole.
The operating system uses either the block allocation list or the
bit map to select the hole from the set of holes.

12
Lecture - XI BITS Pilani, Pilani Campus
MEMORY MANAGEMENT

1. Block Allocation List


Block allocation list maintains two tables. One table contains the
entries of the blocks that are allocated to the various files. The
other table contains the entries of the holes that are free and can
be allocated to the process in the waiting queue.
Now, as we have entries of free blocks which one must be
chosen can be decided using either of these strategies: first-fit,
best-fit, worst-fit strategies.
First-fit
Here, the searching starts either at the beginning of the table or
from where the previous first-fit search has ended. While
searching, the first hole that is found to be large enough for a
process to accommodate is selected.
13
Lecture - XI BITS Pilani, Pilani Campus
MEMORY MANAGEMENT

Best-fit
This method needs the list of free holes to be sorted according to
their size. Then the smallest hole that is large enough for the
process to accommodate is selected from the list of free holes.
This strategy reduces the wastage of memory as it does not
allocate a hole of larger size which leaves some amount of
memory even after the process accommodates the space.
Worst-fit
This method requires the entire list of free holes to be sorted.
Here, again the largest hole among the free holes is selected.
This strategy leaves the largest leftover hole which may be
useful for the other process.
14
Lecture - XI BITS Pilani, Pilani Campus
MEMORY MANAGEMENT

2. Bit Map
The bit map method only keeps track of the free or allocated
block. One block is represented by one bit, bit 0 resembles the
free block and bit 1 resembles that the block is allocated to a file
or a process.

15
Lecture - XI BITS Pilani, Pilani Campus
MEMORY MANAGEMENT

It does not have entries of the files or processes to which the


specific blocks are allocated. Normally, implementing the first fit
will search the number of consecutive zeros/free blocks required
by a file of process. Having found that much of consecutive
zeros it allocates a file or process to those blocks.
But implementing best-fit or worse-fit will be expensive, as the
table of free blocks sorted according to the hole size has to be
maintained. But the bit map method is easy to implement.
Non-contiguous memory allocation
In the non-contiguous memory allocation the available free
memory space are scattered here and there and all the free
memory space is not at one place. So this is time-consuming. In
the non-contiguous memory allocation, a process will
16
Lecture - XI BITS Pilani, Pilani Campus
MEMORY MANAGEMENT

acquire the memory space but it is not at one place it is at the


different locations according to the process requirement. This
technique of non-contiguous memory allocation reduces the
wastage of memory which leads to internal and external
fragmentation. This utilizes all the free memory space which is
created by a different process.

17
Lecture - XI BITS Pilani, Pilani Campus
MEMORY MANAGEMENT

18
Lecture - XI BITS Pilani, Pilani Campus
MEMORY MANAGEMENT

Non-contiguous memory allocation is of different types,


1. Paging
2. Segmentation
3. Segmentation with paging
i) Paging
A non-contiguous policy with a fixed size partition is called
paging. A computer can address more memory than the amount
of physically installed on the system. This extra memory is
actually called virtual memory. Paging technique is very
important in implementing virtual memory. Secondary memory is
divided into equal size partition (fixed) called pages. Every
process will have a separate page table. The entries in the page
table are the number of pages a process. At each entry either
19
Lecture - XI BITS Pilani, Pilani Campus
MEMORY MANAGEMENT

we have an invalid pointer which means the page is not in main


memory or we will get the corresponding frame number. When
the frame number is combined with instruction of set D than we
will get the corresponding physical address. Size of a page table
is generally very large so cannot be accommodated inside the
PCB, therefore, PCB contains a register value PTBR( page table
base register) which leads to the page table.
Advantages: It is independent of external fragmentation.
Disadvantages:
• It makes the translation very slow as main memory access
two times.
• A page table is a burden over the system which occupies
considerable space.
20
Lecture - XI BITS Pilani, Pilani Campus
MEMORY MANAGEMENT

ii) Segmentation
Segmentation is a programmer view of the memory where
instead of dividing a process into equal size partition we divided
according to program into partition called segments. The
translation is the same as paging but paging segmentation is
independent of internal fragmentation but suffers from external
fragmentation. Reason of external fragmentation is program can
be divided into segments but segment must be contiguous in
nature.
iii) Segmentation with paging
In segmentation with paging, we take advantages of both
segmentation as well as paging. It is a kind of multilevel paging
but in multilevel paging, we divide a page table into equal size
21
Lecture - XI BITS Pilani, Pilani Campus
MEMORY MANAGEMENT

partition but here in segmentation with paging, we divide it


according to segments. All the properties are the same as that of
paging because segments are divided into pages

22
Lecture - XI BITS Pilani, Pilani Campus
MEMORY MANAGEMENT

23
Lecture - XI BITS Pilani, Pilani Campus
Thanks

24
BITS Pilani, Pilani Campus
OPERATING SYSTEM

BITS Pilani SESSION 12


Pilani Campus
BITS Pilani
Pilani Campus

MEMORY MANAGEMENT
Learning Objectives
 Segmentation
• What is segmentation?
• Why segmentation is required?
 Translation of Logical address into physical
address by Segment table
 Advantages and disadvantages of segmentation
 Structure of the page table
• Hierarchical Paging
• Hashed Page Tables
• Inverted Page Tables
3
Lecture - XII BITS Pilani, Pilani Campus
MEMORY MANAGEMENT –
SEGMENTATION
Segmentation
• It is a memory allocation technique to store parts of a single
process in various locations
• It is a variable size partitioning scheme
• It is a technique in non contiguous memory allocation like
paging
• In segmentation, process is divided into variable sized
partitions called as segments
• Secondary and main memory are divided into unequal size
partitions

4
Lecture - XII BITS Pilani, Pilani Campus
MEMORY MANAGEMENT –
SEGMENTATION
In Operating Systems, Segmentation is a memory management
technique in which the memory is divided into the variable size
parts. Each part is known as a segment which can be allocated
to a process.
The details about each segment are stored in a table called a
segment table. Segment table is stored in one (or many) of the
segments.
Segment table contains mainly two information about segment:
Base: It is the base address of the segment
Limit: It is the length of the segment.

5
Lecture - XII BITS Pilani, Pilani Campus
MEMORY MANAGEMENT –
segmentation
What is the need of Segmentation?
Till now, we were using Paging as our main memory
management technique. Paging is more close to the Operating
system rather than the User. It divides all the processes into the
form of pages regardless of the fact that a process can have
some relative parts of functions which need to be loaded in the
same page.
Operating system doesn't care about the User's view of the
process. It may divide the same function into different pages and
those pages may or may not be loaded at the same time into the
memory. It decreases the efficiency of the system.
It is better to have segmentation which divides the process into
the segments. Each segment contains the same type of
6
Lecture - XII BITS Pilani, Pilani Campus
MEMORY MANAGEMENT –
segmentation
functions such as the main function can be included in one
segment and the library functions can be included in the other
segment.
MAIN SUB1 SUB2
Statement 1 Statement 1 Statement 1
Statement 2 Statement 2 Statement 2
------------- --------------- --------------
------------- --------------- Statement K
-------------- Statement L
--------------
Statement M

7
Lecture - XII BITS Pilani, Pilani Campus
MEMORY MANAGEMENT –
segmentation
Translation of Logical address into physical address by
Segment table
CPU generates a logical address which contains two parts:
1. Segment Number
2. Offset
For Example:
Suppose a 16 bit address is used with 4 bits for the segment
number and 12 bits for the segment offset so the maximum
segment size is 4096 and the maximum number of segments
that can be refereed is 16.
When a program is loaded into memory, the segmentation
system tries to locate space that is large enough to hold the first
segment of the process, space information is obtained from
8
Lecture - XII BITS Pilani, Pilani Campus
MEMORY MANAGEMENT –
segmentation
the free list maintained by memory manager. Then it tries to
locate space for other segments. Once adequate space is
located for all the segments, it loads them into their respective
areas.
The operating system also generates a segment map table for
each program.

9
Lecture - XII BITS Pilani, Pilani Campus
MEMORY MANAGEMENT –
segmentation

10
Lecture - XII BITS Pilani, Pilani Campus
MEMORY MANAGEMENT –
segmentation
With the help of segment map tables and hardware assistance,
the operating system can easily translate a logical address into
physical address on execution of a program.
The Segment number is mapped to the segment table. The limit
of the respective segment is compared with the offset. If the
offset is less than the limit then the address is valid otherwise it
throws an error as the address is invalid.
In the case of valid addresses, the base address of the segment
is added to the offset to get the physical address of the actual
word in the main memory.
The above figure shows how address translation is done in case
of segmentation.
11
Lecture - XII BITS Pilani, Pilani Campus
MEMORY MANAGEMENT –
segmentation
Advantages of Segmentation
• No internal fragmentation
• Average Segment Size is larger than the actual page size.
• Less overhead
• It is easier to relocate segments than entire address space.
• The segment table is of lesser size as compared to the page
table in paging.
Disadvantages
• It can have external fragmentation.
• It is difficult to allocate contiguous memory to variable sized
partition.
• Costly memory management algorithms.

12
Lecture - XII BITS Pilani, Pilani Campus
STRUCTURE OF PAGE TABLE

The data structure that is used by the virtual memory system in


the operating system of a computer in order to store the mapping
between physical and logical addresses is commonly known
as Page Table.
We know that the logical address that is generated by the CPU
is translated into the physical address with the help of the page
table.
Thus page table mainly provides the corresponding frame
number (base address of the frame) where that page is stored in
the main memory.

13
Lecture - XII BITS Pilani, Pilani Campus
STRUCTURE OF PAGE TABLE

14
Lecture - XII BITS Pilani, Pilani Campus
STRUCTURE OF PAGE TABLE

Characteristics of the Page Table


Some of the characteristics of the Page Table are as follows:
• It is stored in the main memory.
• Generally; the Number of entries in the page table = the
Number of Pages in which the process is divided.
• PTBR means page table base register and it is basically used
to hold the base address for the page table of the current
process.
• Each process has its own independent page table.
Techniques used for Structuring the Page Table
Some of the basic techniques that are used for structuring
the Page table are as follows:
• Hierarchical Paging
15
Lecture - XII BITS Pilani, Pilani Campus
MEMORY MANAGEMENT –
PAGE TABLE STRUCTURE
• Hashed Page Tables
• Inverted Page Tables
Hierarchical Paging
Another name for Hierarchical Paging is multilevel paging.
• There might be a case where the page table is too big to fit in
a contiguous space, so we may have a hierarchy with several
levels.
• In this type of Paging the logical address space is broke up
into Multiple page tables.
• Hierarchical Paging is one of the simplest techniques and for
this purpose, a two-level page table and three-level page
table can be used.

16
Lecture - XII BITS Pilani, Pilani Campus
MEMORY MANAGEMENT –
PAGE TABLE STRUCTURE
Two Level Page Table
Consider a system having 32-bit logical address space and a
page size of 1 KB and it is further divided into:
• Page Number consisting of 22 bits.
• Page Offset consisting of 10 bits.
As we page the Page table, the page number is further divided
into :
Page Number consisting of 12 bits.
Page Offset consisting of 10 bits.
Thus the Logical address is as follows:

17
Lecture - XII BITS Pilani, Pilani Campus
STRUCTURE OF PAGE TABLE

In the above diagram,


P1 is an index into the Outer Page table.
P2 indicates the displacement within the page of the Inner
page Table.
As address translation works from outer page table inward so is
known as forward-mapped Page Table.
Below given figure below shows the Address Translation scheme
for a two-level page table
18
Lecture - XII BITS Pilani, Pilani Campus
STRUCTURE OF PAGE TABLE

Below given figure below shows the Address Translation scheme


for a two-level page table

19
Lecture - XII BITS Pilani, Pilani Campus
MEMORY MANAGEMENT –
PAGE TABLE STRUCTURE
Three Level Page Table
For a system with 64-bit logical address space, a two-level
paging scheme is not appropriate. Let us suppose that the page
size, in this case, is 4KB.If in this case, we will use the two-page
level scheme then the addresses will look like this:

20
Lecture - XII BITS Pilani, Pilani Campus
STRUCTURE OF PAGE TABLE

Thus in order to avoid such a large table, there is a solution and


that is to divide the outer page table, and then it will result in
a Three-level page table:

Hashed Page Tables


This approach is used to handle address spaces that are larger
than 32 bits.
In this virtual page, the number is hashed into a page table.
This Page table mainly contains a chain of elements hashing to
the same elements.
21
Lecture - XII BITS Pilani, Pilani Campus
STRUCTURE OF PAGE TABLE

Each element mainly consists of :


1. The virtual page number
2. The value of the mapped page frame.
3.A pointer to the next element in the linked list.
The following figure shows the address translation scheme of the
Hashed Page Table:

22
Lecture - XII BITS Pilani, Pilani Campus
STRUCTURE OF PAGE TABLE

The Virtual Page numbers are compared in this chain searching


for a match; if the match is found then the corresponding
physical frame is extracted.
In this scheme, a variation for 64-bit address space commonly
uses clustered page tables.
Clustered Page Tables
These are similar to hashed tables but here each entry refers to
several pages (that is 16) rather than 1.
Mainly used for sparse address spaces where memory
references are non-contiguous and scattered
Inverted Page Tables
The Inverted Page table basically combines A page table and A
frame table into a single data structure.
23
Lecture - XII BITS Pilani, Pilani Campus
STRUCTURE OF PAGE TABLE
• There is one entry for each virtual page number and a real
page of memory
• And the entry mainly consists of the virtual address of the
page stored in that real memory location along with the
information about the process that owns the page.
• Though this technique decreases the memory that is needed
to store each page table; but it also increases the time that is
needed to search the table whenever a page reference
occurs.
The following figure shows the address translation scheme of the
Inverted Page Table:

24
Lecture - XII BITS Pilani, Pilani Campus
STRUCTURE OF PAGE TABLE

25
Lecture - XII BITS Pilani, Pilani Campus
Thanks

26
BITS Pilani, Pilani Campus
OPERATING SYSTEM

BITS Pilani SESSION 13


Pilani Campus
BITS Pilani
Pilani Campus

PAGE REPLACEMENT AND FILE


MANAGEMENT
Learning Objectives
 Page replacement algorithms
 What is a File?
 File Structure
 File types
 File Access mechanisms
 Space allocation for the files
 File system structure
 Directory
• Definition
• Structures - Single level, two level , hierarchical , tree structured and
Acyclic graph directory structures

3
Lecture - XIII BITS Pilani, Pilani Campus
PAGE REPLACEMENT
ALGORITHM
Page Fault in OS-
• A page fault occurs when a page referenced by the CPU is
not found in the main memory.
• The required page has to be brought from the secondary
memory into the main memory.
• A page has to be replaced if all the frames of main memory
are already occupied.
Page replacement is a process of swapping out an existing page
from the frame of a main memory and replacing it with the req
Page replacement is required when-
• All the frames of main memory are already occupied.
• Thus, a page has to be replaced to create a room for the
required page.
4
Lecture - XIII BITS Pilani, Pilani Campus
PAGE REPLACEMENT
ALGORITHM
Page Replacement Algorithms-
Page replacement algorithms help to decide which page must
be swapped out from the main memory to create a room for the
incoming page.
Various page replacement algorithms are-
• FIFO Page Replacement Algorithm
• LIFO Page Replacement Algorithm
• LRU Page Replacement Algorithm
• Optimal Page Replacement Algorithm
• Random Page Replacement Algorithm
A good page replacement algorithm is one that minimizes
the number of page faults.
5
Lecture - XIII BITS Pilani, Pilani Campus
PAGE REPLACEMENT
ALGORITHM
FIFO Page Replacement Algorithm
• As the name suggests, this algorithm works on the principle of
“First in First out“.
• It replaces the oldest page that has been present in the main
memory for the longest time.
• It is implemented by keeping track of all the pages in a queue.

LIFO Page Replacement Algorithm


• As the name suggests, this algorithm works on the principle of
“Last in First out“.
• It replaces the newest page that arrived at last in the main
memory.
• It is implemented by keeping track of all the pages in a stack.
6
Lecture - XIII BITS Pilani, Pilani Campus
PAGE REPLACEMENT
ALGORITHM
LRU Page Replacement Algorithm
• As the name suggests, this algorithm works on the principle of
“Least Recently Used“.
• It replaces the page that has not been referred by the CPU for
the longest time.
Optimal Page Replacement Algorithm
• This algorithm replaces the page that will not be referred by
the CPU in future for the longest time.
• It is practically impossible to implement this algorithm.
• This is because the pages that will not be used in future for
the longest time can not be predicted.
• However, it is the best known algorithm and gives the least
number of page faults.
7
Lecture - XIII BITS Pilani, Pilani Campus
PAGE REPLACEMENT
ALGORITHM
Hence, it is used as a performance measure criterion for other algorithms.
Random Page Replacement Algorithm
• As the name suggests, this algorithm randomly replaces any page.
• So, this algorithm may behave like any other algorithm like FIFO, LIFO,
LRU, Optimal etc.
PROBLEMS BASED ON PAGE REPLACEMENT ALGORITHMS
Problem-01: A system uses 3 page frames for storing process
pages in main memory. It uses the First in First out (FIFO) page
replacement policy. Assume that all the page frames are initially
empty. What is the total number of page faults that will occur
while processing the page reference string given below-
4 , 7, 6, 1, 7, 6, 1, 2, 7, 2. Also calculate the hit ratio
and miss ratio.

8
Lecture - XIII BITS Pilani, Pilani Campus
PAGE REPLACEMENT
ALGORITHM
Solution-
Total number of references = 10

From here,Total number of page faults occurred = 6


Calculating Hit ratio-
Total number of page hits = Total number of references – Total number
of page misses or page faults 9
Lecture - XIII BITS Pilani, Pilani Campus
PAGE REPLACEMENT
ALGORITHM
= 10 – 6
=4
Thus, Hit ratio = Total number of page hits / Total number of references
= 4 / 10
= 0.4 or 40%
Calculating Miss ratio-
Total number of page misses or page faults = 6
Thus, Miss ratio = Total number of page misses / Total number of
references
= 6 / 10 = 0.6 or 60%
Alternatively,
Miss ratio
= 1 – Hit ratio
= 1 – 0.4 = 0.6 or 60%
10
Lecture - XIII BITS Pilani, Pilani Campus
FILE MANAGEMENT –
BASICS
File
A file is a named collection of information that is recorded on
secondary storage device such as magnetic disks, magnetic
tapes and optical disks.
File Structure
• A file has a certain defined structure according to its type.
• A text file is a collection of characters organized
• A source file is a collection of procedures and functions.
• An object file is a collection of bytes organized into blocks that
are understandable by the computer.
• When OS defines different file structures, it also contains the
code to support these file structures.
11
Lecture - XIII BITS Pilani, Pilani Campus
FILE MANAGEMENT –
BASICS
File Type
File type refers to the ability of the OS to distinguish different
types of file such as text files, source files and binary files etc.
MS-DOS and UNIX operating system have the following types of
files:
(i) Ordinary files
• These files contain user information such as text, databases
or an executable program.
• Users can perform various operations on such files like add,
modify, delete or even remove the entire file.
(ii) Directory files
These files contain list of file names and other information
related to these files.
12
Lecture - XIII BITS Pilani, Pilani Campus
FILE MANAGEMENT –
BASICS
(iii) Special files
• These files are also known as device files.
• These files represent physical device like disks, terminals,
printers, networks, tape drive etc.
File Access Mechanisms
File access mechanism refers to the manner in which the
records of a file are be accessed. Some of the file mechanisms
are:
1. Sequential access
2. Direct/Random access
3. Indexed sequential access

13
Lecture - XIII BITS Pilani, Pilani Campus
FILE MANAGEMENT –
BASICS
Sequential access
A sequential access is that in which the records are accessed
sequentially, i.e., the information in the file is processed in order,
one record after the other. This access method is the most
primitive one.
Direct/Random access
• Random access file organization provides, accessing the
records directly.
• The records need not be in any sequence within the file and
they need not be in adjacent locations on the storage
medium.
Indexed sequential access
• This mechanism is built up on base of sequential access.
14
Lecture - XIII BITS Pilani, Pilani Campus
FILE MANAGEMENT –
BASICS
• An index is created for each file which contains address of
various records.
• Index is searched sequentially and its pointer is used to
access the file directly.
Space Allocation
OS allocates the disk spaces for the files. OS deploy following
three methods to allocate disk space to files.
1. Contiguous Allocation
2. Linked Allocation
3. Indexed Allocation
Contiguous Allocation
• Each file occupies a contiguous address space on disk.
• Easy to implement.
15
Lecture - XIII BITS Pilani, Pilani Campus
FILE MANAGEMENT –
BASICS
• External fragmentation is a major issue for this technique.

16
Lecture - XIII BITS Pilani, Pilani Campus
FILE MANAGEMENT –
BASICS
Linked Allocation
• Each file contains a list of links to disk blocks.
• Directory contains link / pointer to first block of a file.
• There is no external fragmentation
• Effectively used in sequential access.
• Inefficient in case of direct accessing

17
Lecture - XIII BITS Pilani, Pilani Campus
FILE MANAGEMENT –
BASICS

18
Lecture - XIII BITS Pilani, Pilani Campus
FILE MANAGEMENT –
BASICS
Indexed Allocation
• Provides solutions to problems for contiguous and linked
allocation.
• An index block is created having all pointers to files.
• Each file has its own index block which stores the addresses
of disk space occupied by the file.
• Directory contains the addresses of index blocks of files.

19
Lecture - XIII BITS Pilani, Pilani Campus
FILE MANAGEMENT –
BASICS

20
Lecture - XIII BITS Pilani, Pilani Campus
FILE MANAGEMENT –
BASICS
File System Structure
File System provides efficient access to the disk by allowing data
to be stored, located and retrieved in a convenient way. A file
System must be able to store the file, locate the file and retrieve
the file.
Most of the Operating Systems use layering approach for every
task including file systems. Every layer of the file system is
responsible for some activities.
The diagram shown below, elaborates how the file system is
divided in different layers, and also the functionality of each
layer.

21
Lecture - XIII BITS Pilani, Pilani Campus
FILE MANAGEMENT –
BASICS

22
Lecture - XIII BITS Pilani, Pilani Campus
FILE MANAGEMENT –
BASICS
• When an application program asks for a file, the request is
directed to the logical file system.
• The logical file system contains the Meta data of the file and
directory structure. If the application program doesn't have the
required permissions of the file then this layer will throw an error.
Logical file systems also verify the path to the file.
• Generally, files are divided into various logical blocks. Files are to
be stored in the hard disk and to be retrieved from the hard disk.
Hard disk is divided into various tracks and sectors. So, in order
to store and retrieve the files, the logical blocks need to be
mapped to physical blocks. This mapping is done by File
organization module. It is also responsible for free space
management.
• Once File organization module has identified the physical block
of the application program needs, it passes this
23
Lecture - XIII BITS Pilani, Pilani Campus
FILE MANAGEMENT –
BASICS
information to basic file system. The basic file system is
responsible for issuing the commands to I/O control to
fetch those blocks.
•I/O controls contain the codes by using which it can access hard
disk. These codes are known as device drivers.
Directory
•Directory can be defined as the listing of the related files on the
disk
• A directory can be viewed as a file which contains the Meta
data of the bunch of files.
•Every Directory permits the user to do a number of common
operations on the file:
- File Creation
24
Lecture - XIII BITS Pilani, Pilani Campus
FILE MANAGEMENT –
Directory
- Search for the file
- File deletion
- Renaming the file
- Traversing Files
- Listing of files
Types of Directory Structures
The following are the most common types of directories
1. Single-level directory structure
Single level directory structure has only one directory which is
called the root directory. The users are not allowed to create
subdirectories under the root directory. All the files created by the
several users are present in the root directory only.

25
Lecture - XIII BITS Pilani, Pilani Campus
FILE MANAGEMENT –
Directory
As we can see in the diagram below all the file F1, F2, F3, F4
created by the different users are present at the root directory.

26
Lecture - XIII BITS Pilani, Pilani Campus
FILE MANAGEMENT –
Directory
There is one drawback of Single-level directory structure, a
user cannot use the same file name used by another user in
the system. Even if the file with the same name is created the
old file will get destroyed first and replaced by the new file having
the same name.
2. Two-level directory structure
In Two-level directory structure, the users create directory
directly inside the root directory. But once a user creates such
directory, further he cannot create any subdirectory inside that
directory. Observe the figure below, 4 users have created their
separate directory inside the root directory. But further, no
subdirectory is created by the users.
27
Lecture - XIII BITS Pilani, Pilani Campus
FILE MANAGEMENT –
Directory

28
Lecture - XIII BITS Pilani, Pilani Campus
FILE MANAGEMENT –
Directory
This two-level structure allows each user to keep their files
separately inside their own directory. This structure allows to use
the same name for the files but under different user directories.
3. Hierarchical Directory Structure
In Hierarchical directory structure, the users can create
directories under the root directory and can also create sub-
directories under this structure. As the user is free to create
many sub-directories, it can create different sub-directories for
different file types.

29
Lecture - XIII BITS Pilani, Pilani Campus
FILE MANAGEMENT –
Directory

30
Lecture - XIII BITS Pilani, Pilani Campus
FILE MANAGEMENT –
Directory
Here, the files are accessed by their location using the path.
There are two types of paths to locate the file in this directory
structure
Absolute Path
Here, the path for the desired file is described by considering the
root directory as the base directory.
Relative Path
Here, either the user’s directory is considered as the base
directory or the desired file directory is considered as the base
directory.

31
Lecture - XIII BITS Pilani, Pilani Campus
FILE MANAGEMENT –
Directory
4. Acyclic-Graph Directory Structure
This problem can be solved by the acyclic-graph directory
structure. As this directory structure allows a directory or a file to
have many parent directories. So, a shared file in a directory can
be pointed by the other user directories who have access to that
shared file using the links.
In the diagram below we can see that the directory having file
F7 and F8 have two parent directories.

32
Lecture - XIII BITS Pilani, Pilani Campus
FILE MANAGEMENT –
Directory

33
Lecture - XIII BITS Pilani, Pilani Campus
Thanks

34
BITS Pilani, Pilani Campus
OPERATING SYSTEM

BITS Pilani SESSION 14


Pilani Campus
BITS Pilani
Pilani Campus

FILE MANAGEMENT
Learning Objectives
 What is Free space management?
 Types of free space management methods
- Bit Vector
- Linked List
- Grouping
- Counting
 Recovery
 What is NFS?
 Benefits of NFS

3
Lecture - XIV BITS Pilani, Pilani Campus
FILES AND DIRECTORY
MANAGEMENT
What is free space management?
The operating system manages the free space in the hard disk.
This is known as free space management. The operating system
maintains a free space list to keep track of the free disk space.
The free space list consists of all free disk blocks that are not
allocated to any file or directory. For saving a file in the disk, the
operating system searches the free space list for the required
disk space and then allocates that space to the file. When a file
is deleted, the space allocated to it is added to the free space
list.
There are four methods of doing free space management. These
are -
4
Lecture - XIV BITS Pilani, Pilani Campus
FILES AND DIRECTORY
MANAGEMENT

5
Lecture - XIV BITS Pilani, Pilani Campus
FILES AND DIRECTORY
MANAGEMENT
Bit Vector
The first method that we will discuss is the bit vector method.
Also known as the bit map, this is the most frequently used
method to implement the free space list. In this method, each
block in the hard disk is represented by a bit (either 0 or 1). If a
block has a bit 0 means that block is allocated to a file, and if a
block has a bit 1 means that block is not allocated to any file, i.e.,
the block is free.
For example, consider a disk having 16 blocks where block
numbers 2, 3, 4, 5, 8, 9, 10, 11, 12 and 13 are free, and the rest
of the blocks, i.e., block numbers 0, 1, 6, 7, 14 and 15 are
allocated to some files. The bit vector for this disk will look like
this-
6
Lecture - XIV BITS Pilani, Pilani Campus
FILES AND DIRECTORY
MANAGEMENT

We can find the free block number from the bit vector using the
following method-

We will now find the first free block number in the above
example.
7
Lecture - XIV BITS Pilani, Pilani Campus
FILES AND DIRECTORY
MANAGEMENT
The first group of 8 bits (00111100) constitutes a non-zero word
since all bits are not 0. After finding the non-zero word, we will
look for the first 1 bit. This is the third character of the non-zero
word. Hence, offset = 3.
Therefore, the first free block number = 8 * 0 + 3 = 3.
Advantages
The advantages of the bit vector method are-
• It is simple to understand.
• It is an efficient method.
• It occupies less memory.
Disadvantages
The disadvantages of the bit vector method are-
For finding a free block, the operating system may need to
8
Lecture - XIV BITS Pilani, Pilani Campus
FILES AND DIRECTORY
MANAGEMENT
search the entire bit vector.
Keeping the bit vector in the main memory is possible for smaller
disks but not for larger ones. For example, a 1.3 GB disk with
512-byte blocks would need a bit vector of over 332 KB to track
its free blocks. Giving away 332 KB just to maintain its free block
space is not so efficient in the long run.
Linked List
Another method of doing free space management is a linked list.
In this method, all the free blocks existing in the disk are linked
together in a linked list. The address of the first free block is
stored somewhere in the memory. Each free block contains a
pointer that contains the address to the next free block. The last
free block points to null, indicating the end of the linked list.
9
Lecture - XIV BITS Pilani, Pilani Campus
FILES AND DIRECTORY
MANAGEMENT
For example, consider a disk having 16 blocks where block
numbers 3, 4, 5, 6, 9, 10, 11, 12, 13 and 14 are free, and the rest
of the blocks, i.e., block numbers 1, 2, 7, 8, 15 and 16 are
allocated to some files. If we maintain a linked list, then Block 3
will contain a pointer to Block 4, Block 4 will contain a pointer to
Block 5. Similarly, Block 5 will point to Block 6, Block 6 will point
to Block 9, Block 9 will point to Block 10, Block 10 will point to
Block 11, Block 11 will point to Block 12, Block 12 will point to
Block 13 and Block 13 will point to Block 14. Block 14 will point
to null. The address of the first free block, i.e., Block 3, will be
stored somewhere in the memory. This is also represented in the
following figure-
10
Lecture - XIV BITS Pilani, Pilani Campus
FILES AND DIRECTORY
MANAGEMENT

11
Lecture - XIV BITS Pilani, Pilani Campus
FILES AND DIRECTORY
MANAGEMENT
Advantages
The advantages of the linked list method is
• There is no wastage of space in this method.
Disadvantages
The disadvantages of the linked list method are-
• This method is inefficient since we need to read each block to
traverse the list, which takes more I/O time.
• There is an overhead of maintaining the pointer.
Grouping
The third method of free space management is grouping. This
method is the modification of the linked list method. In this
method, the first free block stores the addresses of the n free
blocks. The first n-1 of these blocks are free. The last block in
12
Lecture - XIV BITS Pilani, Pilani Campus
FILES AND DIRECTORY
MANAGEMENT
these n free blocks contains the addresses of the next n free
blocks, and so on.
For example, consider a disk having 16 blocks where block
numbers 3, 4, 5, 6, 9, 10, 11, 12, 13 and 14 are free, and the rest
of the blocks, i.e., block numbers 1, 2, 7, 8, 15 and 16 are
allocated to some files.
If we apply the Grouping method considering n to be 3, Block 3
will store the addresses of Block 4, Block 5 and Block 6.
Similarly, Block 6 will store the addresses of Block 9, Block 10
and Block 11. Block 11 will store the addresses of Block 12,
Block 13 and Block 14. This is also represented in the following
figure-
13
Lecture - XIV BITS Pilani, Pilani Campus
FILES AND DIRECTORY
MANAGEMENT

This method overcomes the disadvantages of the linked list


method. The addresses of a large number of free blocks can be
found quickly, just by going to the first free block or the nth free
block. There is no need to traverse the whole list, which was the
situation in the linked list method.
14
Lecture - XIV BITS Pilani, Pilani Campus
FILES AND DIRECTORY
MANAGEMENT
Advantages
The advantages of the grouping method is
• The addresses of a large number of free blocks can be found
quickly.
Counting
This is the fourth method of free space management. This
method is also a modification of the linked list method. This
method takes advantage of the fact that several contiguous
blocks may be allocated or freed simultaneously. In this method,
a linked list is maintained but in addition to the pointer to the next
free block, a count of free contiguous blocks that follow the first
block is also maintained. Thus each free block in the disk will
contain two things-
15
Lecture - XIV BITS Pilani, Pilani Campus
FILES AND DIRECTORY
MANAGEMENT
• A pointer to the next free block.
• The number of free contiguous blocks following it.
For example, consider a disk having 16 blocks where block
numbers 3, 4, 5, 6, 9, 10, 11, 12, 13 and 14 are free, and the rest
of the blocks, i.e., block numbers 1, 2, 7, 8, 15 and 16 are
allocated to some files.
If we apply the counting method, Block 3 will point to Block 4 and
store the count 4 (since Block 3, 4, 5 and 6 are contiguous).
Similarly, Block 9 will point to Block 10 and keep the count 6
(since Block 9, 10, 11, 12, 13 and 14 are contiguous). This is
also represented in the following figure-

16
Lecture - XIV BITS Pilani, Pilani Campus
FILES AND DIRECTORY
MANAGEMENT

This method also overcomes the disadvantages of the linked list


method since there is no need to traverse the whole list.
Advantages
The advantages of the counting method are-
• Fast allocation of a large number of consecutive free blocks.
• The overall list is smaller in size.
Disadvantages
The disadvantages of the counting method is
Each free block requires more space for keeping the count in the
disk.

17
Lecture - XIV BITS Pilani, Pilani Campus
FILES AND DIRECTORY
MANAGEMENT
Recovery
Files and directories are kept both in main memory and on disk,
and care must be taken to ensure that system failure does not
Result in loss of data or in data inconsistency. We deal with
these issues in the following sections.
Consistency Checking
Some directory information is kept in main memory to speed up
access. The directory information in main memory is generally
more up to date than is the corresponding information on the
disk, because cached directory information is not necessarily
written to disk as soon as the update takes place. Consider,
then, the possible effect of a computer crash. Cache and buffer
contents, as well as I/O operations in progress, can be
18
Lecture - XIV BITS Pilani, Pilani Campus
FILES AND DIRECTORY
MANAGEMENT
lost, and with them any changes in the directories of opened
files. Such an event can leave the file system in an inconsistent
state: The actual state of some files is not as described in the
directory structure. Frequently, a special program is run at reboot
time to check for and correct disk inconsistencies.
In contrast, the loss of a directory entry on an indexed allocation
system can be disastrous, because the data blocks have no
knowledge of one another. For this reason, UNIX caches
directory entries for reads; but any data write that results in
space allocation, or other metadata changes, is done
synchronously, before the corresponding data blocks are written.
Of course, problems can still occur if a synchronous write is
interrupted by a crash.
19
Lecture - XIV BITS Pilani, Pilani Campus
FILES AND DIRECTORY
MANAGEMENT
Backup and Restore
Magnetic disks sometimes fail, and care must be taken to
ensure that the data lost in such a failure are not lost forever. To
this end, system programs can be used to back up data from
disk to another storage device, such as a floppy disk, magnetic
tape, optical disk, or other hard disk.
Recovery from the loss of an individual file, or of an entire disk,
may then be a matter of restoring the data from backup. To
minimize the copying needed, we can use information from each
file's directory entry. For instance, if the backup program knows
when the last backup of a file was done, and the file's last write
date in the directory indicates that the file has not changed since
that date, then the file does not need to be copied again. A
20
Lecture - XIV BITS Pilani, Pilani Campus
FILES AND DIRECTORY
MANAGEMENT
typical backup schedule may then be as follows:
• Day 1. Copy to a backup medium all files from the disk. This is
called a full backup.
• Day 2. Copy to another medium all files changed since day 1.
This is an incremental backup.
• Day 3. Copy to another medium all files changed since day 2.
• Day N. Copy to another medium all files changed since day
N— 1. Then go back to Day 1. The new cycle can have its
backup written over the previous set or onto a new set of backup
media.
In this manner, we can restore an entire disk by starting restores
with the full backup and continuing through each of the
incremental backups. Of course, the larger the value of N, the
21
Lecture - XIV BITS Pilani, Pilani Campus
FILES AND DIRECTORY
MANAGEMENT
greater the number of tapes or disks that must be read for a
complete restore. An added advantage of this backup cycle is
that we can restore any file accidentally deleted during the cycle
by retrieving the deleted file from the backup of the previous day.
The length of the cycle is a compromise between the amount of
backup medium needed and the number of days back from
which a restore can be done. To decrease the number of tapes
that must be read, to do a restore, an option is to perform a full
backup and then each day back up all files that have changed
since the full backup. In this way, a restore can be done via the
most recent incremental backup and. the full backup, with no
other incremental backups needed. The trade-off is that more
files will be modified each day, so each successive incremental
22
Lecture - XIV BITS Pilani, Pilani Campus
FILES AND DIRECTORY
MANAGEMENT
backup involves more files and more backup media.
A user may notice that a particular file is missing or corrupted
long after the damage was done. For this reason, we usually
plan to take a full backup from time to time that will be saved
"forever." It is a good idea to store these permanent backups far
away from the regular backups to protect against hazard, such
as a fire that destroys the computer and all the backups too. And
if the backup cycle reuses media, we must take care not to reuse
the media too many times—if the media wear out, it might not
be possible to restore any data from the backups.

23
Lecture - XIV BITS Pilani, Pilani Campus
FILES AND DIRECTORY
MANAGEMENT
Network File System (NFS)
The Network File System (NFS) is an IP-based file-sharing
protocol that is used by NAS systems to allow multiple remote
systems to connect to a shared file system. NFS uses file-level
data access and the target (or destination) NAS device controls
the storage device.

24
Lecture - XIV BITS Pilani, Pilani Campus
FILES AND DIRECTORY
MANAGEMENT
It is a “distributed” file system protocol, which means that its data
is stored on a server and that it uses a network protocol to share
– and restrict – access to the file system, using the same coding
that is used for local files. NFS has three components: a client,
a server, and the protocol that enables the two to communicate.
It allows a user on Computer 1 (the “client”) to access File A on
Computer 2 over a network in much the same way they would
access a file on Computer 1’s own hard drive. To Computer 1,
NFS is ‘invisible’; while NFS is keeping track of where files are
located and transporting data, Computer 1 ‘sees’ a file system
that looks local. The naming convention used by NFS makes it
impossible to tell from File A’s name that it’s being accessed
remotely.
25
Lecture - XIV BITS Pilani, Pilani Campus
FILES AND DIRECTORY
MANAGEMENT
And if File A is being modified on Computer 2, those
modifications will be seen on Computer 1. Similarly, if the file is
moved to a different server, the user of Computer 1 won’t know
it.
While NFS is OS- (operating system) independent, it is typically
used on Unix systems, macOS, and Linux. It is also hardware-,
network architectures, and transport protocol independent, and
works well in both small and large environments.
Distributed file systems such as NFS offer many benefits.
Storing data on servers frees up the resources of client
machines. Distributing data (e.g., important administrative data)
to multiple clients is easier, and it is even possible to transfer
files between computers that use different operating systems
26
Lecture - XIV BITS Pilani, Pilani Campus
FILES AND DIRECTORY
MANAGEMENT
(i.e., between computers in “heterogeneous environments”). For
example, you could use a Windows NFS file server in a Linux
environment to give Linux client computers access to files on
that Windows NFS server. And rather than every user in an
organization having their home directory on their individual
network machine, all the home directories could be set up on an
NFS server and then accessed as appropriate by everyone on
the network.
The benefits of the NFS service are
• Multiple clients can use the same files, which allows everyone
on the network to use the same data, accessing it on remote
hosts as if it were accessing local files.
27
Lecture - XIV BITS Pilani, Pilani Campus
FILES AND DIRECTORY
MANAGEMENT
• Computers share applications, which eliminates the needs for
local disk space and reduces storage costs.
• All users can read the same files, so data can remain up-to-
date, and it’s consistent and reliable.
• Mounting the file system is transparent to all users.
• Support for heterogeneous environments allows us to run
mixed technology from multiple vendors and use
interoperable components.
• System admin overhead is reduced due to centralization of
data.

28
Lecture - XIV BITS Pilani, Pilani Campus
Thanks

29
BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus

OVERVIEW OF MASS STORAGE


Learning Objectives
 Storage Structure
 Disk structure
 SAN and NAS
 Disk Scheduling algorithms
- First Come First Serve
- Shortest seek time first
- SCAN
- LOOK
- C-SCAN
- C-LOOK
2
Lecture - XV BITS Pilani, Pilani Campus
DISK MANAGEMENT –
STORAGE STRUCTURE
Basically we want the programs and data to reside in main
memory permanently. This arrangement is usually not possible
for the following two reasons:
1. Main memory is usually too small to store all needed
programs and data permanently.
2. Main memory is a volatile storage device that loses its
contents when power is turned off
There are two types of storage devices:-
Volatile Storage Device –
It loses its contents when the power of the device is removed.
Non-Volatile Storage device –
It does not loses its contents when the power is removed. It
holds all the data when the power is removed.
3
Lecture - XV BITS Pilani, Pilani Campus
DISK MANAGEMENT –
STORAGE STRUCTURE
Secondary storage is used as an extension of main memory.
Secondary storage devices can hold the data permanently.
Storage devices consists of registers, cache,
Electronic-Disk, Magnetic disk , optical disk , Magnetic Tapes.
Each storage system provides the basic system of storing a
datum and of holding the datum until it is retrieved at a later time.
All the storage devices differ in speed, cost, size and volatility.
The most common Secondary-storage device is a Magnetic-disk,
which provides storage for both programs and data.
The following figure shows the hierarchy of storage

4
Lecture - XV BITS Pilani, Pilani Campus
DISK MANAGEMENT –
STORAGE STRUCTURE

5
Lecture - XV BITS Pilani, Pilani Campus
DISK MANAGEMENT –
STORAGE STRUCTURE
In this hierarchy all the storage devices are arranged according
to speed and cost. The higher levels are expensive, but they are
fast. As we move down the hierarchy, the cost per bit generally
decreases, where as the access time generally increases.
The storage systems above the Electronic disk are Volatile,
where as those below are Non-Volatile.
An Electronic disk can be either designed to be either Volatile or
Non-Volatile. During normal operation, the electronic disk stores
data in a large DRAM array, which is Volatile. But many
electronic disk devices contain a hidden magnetic hard disk and
a battery for backup power. If external power is interrupted, the
electronic disk controller copies the data from RAM to the
magnetic disk. When external power is restored, the
6
Lecture - XV BITS Pilani, Pilani Campus
DISK MANAGEMENT –
STORAGE STRUCTURE
controller copies the data back into the RAM.
The design of a complete memory system must balance all the
factors. It must use only as much expensive memory as
necessary while providing as much inexpensive, Non-Volatile
memory as possible. Caches can be installed to improve
performance where a large access-time or transfer-rate disparity
exists between two components.

7
Lecture - XV BITS Pilani, Pilani Campus
DISK MANAGEMENT –
STORAGE STRUCTURE
Secondary storage devices are those devices whose storage is
non volatile. It means that the stored data will be intact even if
the system is turned off. Here are a few things worth noting
about secondary storage.
• Secondary storage is also called auxiliary storage.
• Secondary storage is less expensive when compared to
primary memory like RAMs.
• The speed of the secondary storage is also lesser than that of
primary storage.
• Hence, the data which is less frequently accessed is kept in
the secondary storage.

8
Lecture - XV BITS Pilani, Pilani Campus
DISK MANAGEMENT – MAGNETIC DISK
STRUCTURE

Magnetic Disk Structure


In modern computers, most of the secondary storage is in the
form of magnetic disks. Hence, knowing the structure of a
magnetic disk is necessary to understand how the data in the
disk is accessed by the computer.
Structure of a magnetic disk
A magnetic disk contains several platters. Each platter is divided
into circular shaped tracks. The length of the tracks near the
centre is less than the length of the tracks farther from the
centre. Each track is further divided into sectors.
Tracks of the same distance from centre form a cylinder. A read-
write head is used to read data from a sector of the magnetic
Disk.
9
Lecture - XV BITS Pilani, Pilani Campus
DISK MANAGEMENT – MAGNETIC DISK
STRUCTURE

10
Lecture - XV BITS Pilani, Pilani Campus
DISK MANAGEMENT – MAGNETIC DISK
STRUCTURE

The speed of the disk is measured as two parts:


Transfer rate: This is the rate at which the data moves from disk
to the computer.
Random access time: It is the sum of the seek time and
rotational latency.
Seek time is the time taken by the arm to move to the required
track. Rotational latency is defined as the time taken by the
arm to reach the required sector in the track.
Even though the disk is arranged as sectors and tracks
physically, the data is logically arranged and addressed as an
array of blocks of fixed size. The size of a block can
be 512 or 1024 bytes. Each logical block is mapped with a sector
on the disk, sequentially.
11
Lecture - XV BITS Pilani, Pilani Campus
DISK MANAGEMENT – SAN
AND NAS
Storage Area Network (SAN) is used for transferring the data
between the servers and the storage devices’ fiber channels and
switches. In SAN (Storage Area Network), data is identified by
disk block. Protocols that are used in SAN are SCSI (Small
Computer System Interface), SATA (Serial Advanced Technology
Attachment), etc.
Components of Storage Area Network (SAN):
1.Node ports
2. Cables
3. Interconnect devices such as Hubs, switches, directors
4. Storage arrays
5. SAN management Software
12
Lecture - XV BITS Pilani, Pilani Campus
DISK MANAGEMENT – SAN
AND NAS
Network Attached Storage (NAS), data is identified by file
name as well as byte offset. In-Network Attached Storage, the
file system is managed by Head units such as CPU and Memory.
In this for backup and recovery, files are used instead of the
block-by-block copying technique.
Components of Network Attached Storage (NAS):
1. Head unit: CPU, Memory
2. Network Interface Card (NIC)
3. Optimized operating system
4. Protocols
5. Storage protocols: ATA (Advanced Technology Attachment),
SCSI, FC (Fibre Channel)
13
Lecture - XV BITS Pilani, Pilani Campus
DISK MANAGEMENT – SAN
AND NAS
The difference between Storage Area Network (SAN) and
Network Attached Storage (NAS) are as follows:
SAN NAS

SAN stands for Storage Area Network. NAS stands for Network Attached Storage.
Data is identified by disk block Data is identified by file name as well as byte
offset.
File system is managed by servers File system is managed by Head unit.
It is more costly. It is less expensive than SAN.
It is more complex than NAS. It is less complex than SAN.
Protocols used in SAN are: SCSI, SATA, etc. Protocols used in NAS are: File server, CIFS
(Common Internet File System), etc.
For backups and recovery Block by block For backups and recovery in NAS, Files are used.
copying technique is used.
Gives high performance in high-speed for Not suitable for that environment
high-speed traffic systems.
It has lower latency. Compared to SAN, NAS has higher latency.
SAN supports virtualization. NAS does not support virtualization.
14
Lecture - XV BITS Pilani, Pilani Campus
DISK MANAGEMENT – DISK
SCHEDULING ALGORITHMS
Disk Scheduling Algorithms
On a typical multiprogramming system, there will usually be
multiple disk access requests at any point of time. So those
requests must be scheduled to achieve good efficiency. Disk
scheduling is similar to process scheduling. Some of the disk
scheduling algorithms are
1. First Come First Serve
2. Shortest Seek time first
3. SCAN algorithm
4. LOOK
5. C-SCAN
6. C-LOOK
15
Lecture - XV BITS Pilani, Pilani Campus
DISK MANAGEMENT – DISK
SCHEDULING ALGORITHMS
First Come First Serve
This algorithm performs requests in the same order asked by the
system. Let's take an example where the queue has the
following requests with cylinder numbers as follows:
98, 183, 37, 122, 14, 124, 65, 67
Assume the head is initially at cylinder 56. The head moves in
the given order in the queue i.e., 56→98→183→...→67.

16
Lecture - XV BITS Pilani, Pilani Campus
DISK MANAGEMENT – DISK
SCHEDULING ALGORITHMS

17
Lecture - XV BITS Pilani, Pilani Campus
DISK MANAGEMENT – DISK
SCHEDULING ALGORITHMS
Shortest Seek Time First (SSTF)
Here the position which is closest to the current head position is
chosen first. Consider the previous example where disk queue
looks like,
98, 183, 37, 122, 14, 124, 65, 67
Assume the head is initially at cylinder 56. The next closest
cylinder to 56 is 65, and then the next nearest one is 67,
then 37, 14, so on.

18
Lecture - XV BITS Pilani, Pilani Campus
DISK MANAGEMENT – DISK
SCHEDULING ALGORITHMS

19
Lecture - XV BITS Pilani, Pilani Campus
DISK MANAGEMENT – DISK
SCHEDULING ALGORITHMS
SCAN algorithm
This algorithm is also called the elevator algorithm because of
it's behavior. Here, first the head moves in a direction (say
backward) and covers all the requests in the path. Then it moves
in the opposite direction and covers the remaining requests in
the path. This behavior is similar to that of an elevator. Let's take
the previous example,
98, 183, 37, 122, 14, 124, 65, 67
Assume the head is initially at cylinder 56. The head moves in
backward direction and accesses 37 and 14. Then it goes in the
opposite direction and accesses the cylinders as they come in
the path.
20
Lecture - XV BITS Pilani, Pilani Campus
DISK MANAGEMENT – DISK
SCHEDULING ALGORITHMS

21
Lecture - XV BITS Pilani, Pilani Campus
DISK MANAGEMENT – DISK
SCHEDULING ALGORITHMS
LOOK
In this algorithm, the disk arm moves in a particular direction till
the last request is found in that direction and serves all of them
found in the path, and then reverses its direction and serves the
requests found in the path again up to the last request found.
The only difference between SCAN and LOOK is, it doesn't go to
the end it only moves up to which the request is found.
Eg. Suppose the order of requests are 70, 140, 50, 125, 30, 25,
160 and the initial position of the Read-Write head is 60. And it is
given that the disk arm should move towards the larger value.

22
Lecture - XV BITS Pilani, Pilani Campus
DISK MANAGEMENT – DISK
SCHEDULING ALGORITHMS

Seek Time = Distance Moved by the disk arm


= (170-60)+(170-25)
= 235
23
Lecture - XV BITS Pilani, Pilani Campus
DISK MANAGEMENT – DISK
SCHEDULING ALGORITHMS
C-SCAN
This algorithm is the same as the SCAN algorithm. The only
difference between SCAN and C-SCAN is, it moves in a
particular direction till the last and serves the requests in its path.
Then, it returns in the opposite direction till the end and doesn't
serve the request while returning. Then, again reverses the
direction and serves the requests found in the path. It moves
circularly.
Eg. Suppose the order of requests are 70, 140, 50, 125, 30, 25,
160 and the initial position of the Read-Write head is 60. And it is
given that the disk arm should move towards the larger value.

24
Lecture - XV BITS Pilani, Pilani Campus
DISK MANAGEMENT – DISK
SCHEDULING ALGORITHMS

Seek Time = Distance Moved by the disk arm


= (170-60)+(170-0)+(50-0)
=330

25
Lecture - XV BITS Pilani, Pilani Campus
DISK MANAGEMENT – DISK
SCHEDULING ALGORITHMS
C-LOOK
This algorithm is also the same as the LOOK algorithm. The only
difference between LOOK and C-LOOK is, it moves in a
particular direction till the last request is found and serves the
requests in its path. Then, it returns in the opposite direction till
the last request is found in that direction and doesn't serve the
request while returning. Then, again reverses the direction and
serves the requests found in the path. It also moves circularly.
Eg. Suppose the order of requests are 70, 140, 50, 125, 30, 25,
160 and the initial position of the Read-Write head is 60. And it is
given that the disk arm should move towards the larger value.

26
Lecture - XV BITS Pilani, Pilani Campus
DISK MANAGEMENT – DISK
SCHEDULING ALGORITHMS

Seek Time = Distance Moved by the disk arm


= (160-60)+(160-25)+(50-25)
= 260

27
Lecture - XV BITS Pilani, Pilani Campus
Thanks

28
BITS Pilani, Pilani Campus
OPERATING SYSTEM

BITS Pilani SESSION :16


Pilani Campus
BITS Pilani
Pilani Campus

SWAP SPACE AND RAID


Learning Objectives
 What is Swap Space?
 Advantages of Swap Space
 What is swap space management
 RAID
- Need
- RAID 0
- RAID 1
- RAID 3
- RAID 5
- RAID 6
3
Lecture - XVI BITS Pilani, Pilani Campus
SWAP SPACE MANAGEMENT

Most of the time, some amount of physical memory in a


computer is swapped on disk. Swap space is a space on a hard
disk that is a substitute for physical memory. It is used as virtual
memory, which contains images of the running process in the
memory. Whenever our computer runs short of physical memory,
it uses its virtual memory and stores information in memory on a
disk.
The process of swapping or interchanging of data between
virtual memory and real memory is called swapping and the
space on disk is known as swap space. Swap space helps the
computer's operating system pretend that it has more RAM than
it actually has. It is also called a swap file.
4
Lecture - XVI BITS Pilani, Pilani Campus
SWAP SPACE MANAGEMENT

Virtual memory is a combination of RAM and disk space that


running processes can use. Swap space is the portion of
virtual memory on the hard disk, used when RAM is full. Swap
space can be useful to computers in the following various ways,
such as:
• It can be used as a single contiguous memory which reduces
I/O operations to read or write a file.
• Applications that are not used or less frequently used can be
kept in a swap file.
• Having sufficient swap files helps the system keep some
physical memory free all the time.
• The space in physical memory that has been freed due to
swap space can be used by OS for other important tasks.
5
Lecture - XVI BITS Pilani, Pilani Campus
SWAP SPACE MANAGEMENT

Operating systems such as Windows, Linux, etc. systems


provide a certain amount of swap space by default which users
can change according to their needs. If we don't want to use
virtual memory, we can easily disable it together. Still, if we run
out of memory, then the kernel will kill some of the processes to
create a sufficient amount of space in physical memory so that it
totally depends upon the user whether he wants to use swap
space or not.
What is Swap-Space Management?
Managing the Swap-space is another low-level task of the
operating system. Virtual memory uses disk space as an
extension of main memory. Since disk access is much slower
than memory access, using swap space significantly decreases
6
Lecture - XVI BITS Pilani, Pilani Campus
SWAP SPACE MANAGEMENT

system performance. The main goal for the design and


implementation of swap space is to provide the best throughput
for the virtual memory system.
Swap space is used in various ways by different operating
systems, depending on the memory-management algorithms in
use. For example, systems that implement swapping may use
swap space to hold an entire process image, including the code
and data segments. Paging systems may simply store pages
that have been pushed out of the main memory. The amount of
swap space needed on a system can vary depending on the
amount of physical memory, the amount of virtual memory it is
backing, and how it is used. It can range from a
few megabytes of disk space to gigabytes.
7
Lecture - XVI BITS Pilani, Pilani Campus
SWAP SPACE MANAGEMENT

Note that it may be safer to overestimate than to underestimate


the amount of swap space required because if a system runs
out of swap space, it may be forced to abort processes or may
crash entirely. Overestimation wastes disk space that could
otherwise be used for files, but it does no other harm. Some
systems recommend the amount to be set aside for swap space.
Solaris, for example, suggests setting swap space equal to the
amount by which virtual memory exceeds page-able physical
memory. Previously, Linux suggested setting swap space to
double the amount of physical memory, although most Linux
systems now use considerably less swap space. There is
currently much debate in the Linux community about whether to
set aside swap space at all.
8
Lecture - XVI BITS Pilani, Pilani Campus
SWAP SPACE MANAGEMENT

Some operating systems, including Linux, allow multiple swap


spaces. These swap spaces are usually put on separate disks
so the load placed on the I/O system by paging and swapping
can be spread over the system's I/O devices.
Uses of Swap Space
The different operating system uses Swap-space in various
ways. The systems that are implementing swapping may use
swap space to hold the entire process, including image, code,
and data segments.
Swapping is a memory management technique used in multi-
programming to increase the number of processes sharing the
CPU. It is a technique of removing a process from the main
memory, storing it into secondary memory, and then bringing it
9
Lecture - XVI BITS Pilani, Pilani Campus
SWAP SPACE MANAGEMENT

back into the main memory for continued execution. This action
of moving a process out from main memory to secondary
memory is called Swap Out. The action of moving a process out
from secondary memory to main memory is called Swap In.
Paging systems may simply store pages that have been pushed
out of the main memory. The need for swap space on a system
can vary from megabytes to gigabytes. Still, it also depends on
the amount of physical memory, the virtual memory it is backing,
and how it uses the virtual memory.
It is safer to overestimate than to underestimate the amount of
swap space required because if a system runs out of swap
space, it may be forced to abort the processes or may crash
entirely. Overestimation wastes disk space that could be used for
10
Lecture - XVI BITS Pilani, Pilani Campus
SWAP SPACE MANAGEMENT

files, but it does not harm others. The following table shows
different systems using the amount of swap space:
S.No. System Swap Space

1. Solaris Swap space is equal to the amount of


physical memory.

2. Linux Swap space is double the amount of


physical memory.

Solaris setting swap space equal to the amount by which virtual


memory exceeds page-able physical memory.
Previously, Linux has suggested setting swap space to double
the amount of physical memory. Today, this limitation is gone,
and most Linux systems use considerably less swap space.
11
Lecture - XVI BITS Pilani, Pilani Campus
SWAP SPACE MANAGEMENT

Including Linux, some operating systems allow the use of


multiple swap spaces, including both files and dedicated swap
partitions. The swap spaces are placed on the disk, so the load
on the I/O by the paging and swapping will spread over the
system's bandwidth.

12
Lecture - XVI BITS Pilani, Pilani Campus
RAID

RAID stands for ‘Redundant Arrays of Independent Disks’ or


‘Redundant Arrays of Inexpensive Disks. This technology
plays a vital role in storing large amounts of data while
preserving Data Integrity. It helps in real-time data recovery
when a hard drive fails. In other words, this technology either
divides or duplicates the task of one hard disk between multiple
disks.
This is done to create data redundancy in case of a drive failure.
RAID Mode also called Raid level is set for different application
requirements. E.g. when mode “RAID 0” is set, then the system
splits the data evenly between two or more disks.

13
Lecture - XVI BITS Pilani, Pilani Campus
RAID

RAID Configuration Levels


Different levels represent specific configuration of Disk Arrays.
Only few configurations are practical for most of the processing
systems. Hence RAID – 0 ,1,3,5 and 6 are discussed below.
RAID – 0 (Non-Redundant Configuration)
RAID – 1 (Mirrored Configuration)
RAID – 3 (Bit-Interleaved Parity)
RAID – 5 (Block-Interleaved Distributed-Parity)
RAID – 6 (P+Q Redundancy)
RAID – 0 (Non-Redundant Configuration)
This is the fastest RAID mode which stripes the data onto each
disk evenly. Data Striping refers to the distribution of data over
multiple disks to make them appear as a single, large disk. This
14
Lecture - XVI BITS Pilani, Pilani Campus
RAID

configuration offers best ‘Write’ performance and it does not


employ redundancy at all. ‘Read’ performance is low in this
configuration.
If one physical disk in the array fails, the data is lost. This type of
configuration is preferred in super-computing environments
where performance and capacity, rather than reliability are the
primary concerns. The following figure shows Non-Redundant
Configuration where the Data is distributed evenly and striped
across three disks.

15
Lecture - XVI BITS Pilani, Pilani Campus
RAID

Non-Redundant Configuration

16
Lecture - XVI BITS Pilani, Pilani Campus
RAID

RAID – 1 (Mirrored Configuration)


This type of configuration is implemented by using twice as
many disks as Non-Redundant Disk Array. It is also called as
Mirrored configuration in which write data is mirrored on two
separate disk systems. This ensures that that there are always
two copies of information.
If a disk fails, the other copy is used to implement the service
requests. It is widely used in Database applications. This is a
secure mode as there is no Data loss. The following figure
shows data is mirrored on disk 1 and 2. If Disk 1 fails, the data
can be retrieved from disk 2.

17
Lecture - XVI BITS Pilani, Pilani Campus
RAID

Mirrored Configuration
18
Lecture - XVI BITS Pilani, Pilani Campus
RAID

RAID – 3 (Bit-Interleaved Parity)


This type of configuration is conceptually interleaved bit-wise
over the data disks to increase the speed of access to blocks on
disk storage. The architecture has two or more data disks along
with an Error Correcting Code (ECC) disk which contains
Exclusive-OR of the data from other disks.
Each read and write request accesses all data disks and the
parity disk and hence only one request can be serviced in this
configuration. The parity disk has no data and only parity. The
data can be recovered from a failed disk drive by reconstructing
Exclusive-OR of the remaining drives and ECC drive. This type
of architecture is found in applications that require high
bandwidth but not high I/0 rates.
19
Lecture - XVI BITS Pilani, Pilani Campus
RAID

RAID – 3 Configuration
20
Lecture - XVI BITS Pilani, Pilani Campus
RAID

RAID – 5 (Block-Interleaved Distributed Parity)


Disk Arrays with this type of configuration provides small read,
large read, and large write performance of any redundant disk
array. To improve the write performance of Mirrored
Configuration and Bit-Interleaved Parity system, this architecture
was introduced in which Read and Write requests are performed
in parallel.
While servicing large requests in Block Interleaved Parity
Distribution, disk conflicts are less because, when the striping
units are traversed sequentially, each disk is accessed once.

21
Lecture - XVI BITS Pilani, Pilani Campus
RAID

Block-Interleaved Distributed Parity Configuration

22
Lecture - XVI BITS Pilani, Pilani Campus
RAID

RAID – 6 (P+Q Redundancy)


This is also called P + Q Redundancy configuration in which the
disk arrays are similar to Block-Interleaved Distributed Parity
disk arrays. It can perform small write operations using a read-
modify-write procedure. It requires more disk space as P and Q
information has to be updated. It is not yet accepted as a
standard RAID Configuration.

23
Lecture - XVI BITS Pilani, Pilani Campus
RAID

RAID 6 Configuration

24
Lecture - XVI BITS Pilani, Pilani Campus
RAID

Advantages of RAID
The advantages are:
• Transfer of large sequential files and graphic images is easier.
• Hardware based implementation is more robust.
• Software based implementation is cost-effective.
• Highest performance and Data protection can be achieved.
• Fault tolerance capacity is high.
• They require less power.
Disadvantages of RAID
The disadvantages include:
• In spite of using this technology, backup software is a must.
• Mapping Logic blocks onto physical locations is complex.
• Data chunk size affects the performance of disk array.
25
Lecture - XVI BITS Pilani, Pilani Campus
RAID - Problems

Problem 1:What is the average access time for transferring 512 bytes
of data with the following specifications-
Average seek time = 5 msec
Disk rotation = 6000 RPM
Data rate = 40 KB/sec
Controller overhead = 0.1 msec
Problem 2:A certain moving arm disk storage with one head has the
following specifications-
Number of tracks per surface = 200
Disk rotation speed = 2400 RPM
Track storage capacity = 62500 bits
Average latency = P msec
Data transfer rate = Q bits/sec
What is the value of P and Q?
26
Lecture - XVI BITS Pilani, Pilani Campus
RAID

Solution for Problem 1:


Average seek time = 5 msec
Disk rotation = 6000 RPM
Data rate = 40 KB/sec
Controller overhead = 0.1 msec
Time taken for one full rotation
= (60 / 6000) sec
= (1 / 100) sec
= 0.01 sec
= 10 msec
Average rotational delay
= 1/2 x Time taken for one full rotation
= 1/2 x 10 msec = 5 msec
27
Lecture - XVI BITS Pilani, Pilani Campus
RAID

Transfer time
= (512 bytes / 40 KB) sec
= 0.0125 sec
= 12.5 msec

Average access time


= Average seek time + Average rotational delay + Transfer time +
Controller overhead + Queuing delay
= 5 msec + 5 msec + 12.5 msec + 0.1 msec + 0
= 22.6 msec

28
Lecture - XVI BITS Pilani, Pilani Campus
RAID

Solution for Problem 2:


Number of tracks per surface = 200
Disk rotation speed = 2400 RPM
Track storage capacity = 62500 bits

Time taken for one full rotation


= (60 / 2400) sec
= (1 / 40) sec
= 0.025 sec
= 25 msec
Average latency or Average rotational latency
= 1/2 x Time taken for one full rotation
= 1/2 x 25 msec
= 12.5 msec

29
Lecture - XVI BITS Pilani, Pilani Campus
RAID

Data transfer rate


= Number of heads x Capacity of one track x Number of rotations in
one second
= 1 x 62500 bits x (2400 / 60)
= 2500000 bits/sec
= 2.5 x 106 bits/sec

Thus, P = 12.5 and Q = 2.5 x 106

30
Lecture - XVI BITS Pilani, Pilani Campus
Thanks

31
BITS Pilani, Pilani Campus
Deadlock handling

Prof J P Misra
BITS, Pilani

Operating Systems BITS Pilani, Pilani Campus


The Deadlock Problem

• A set of blocked processes each holding a resource and


waiting to acquire a resource held by another process in
the set
• Example1
System has 2 tape drives
P1 and P2 each hold one tape drive and each needs another one
• Example2
Semaphores A and B, initialized to 1

P0 P1
Wait (A); Wait(B)
Wait (B); Wait(A)

2 Operating Systems BITS Pilani, Pilani Campus


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.

3 Operating Systems BITS Pilani, Pilani Campus


System Model

• Resource types R1, R2, . . ., Rm


CPU cycles, memory space, I/O devices

• Each resource type Ri has Wi instances


• Each process utilizes a resource as follows:
1.Request
2.Use
3.Release

Request and release of resources is done through system calls

4 Operating Systems BITS Pilani, Pilani Campus


Deadlock Characterization
Deadlock can arise if four conditions hold simultaneously.

• Mutual exclusion: only one process at a time can use a


resource(i.e. resource is non-sharable).
• Hold and wait: a process holding at least one resource is
waiting to acquire additional resources held by other
processes.
• No preemption: a resource can be released only voluntarily
by the process holding it, after that process has completed its
task.
• 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.

5 Operating Systems BITS Pilani, Pilani Campus


Resource-allocation Graph

A set of vertices V and a set of edges E.


• V is partitioned into two types:
P = {P1, P2, …, Pn}, the set consisting of all the processes in
the system.

R = {R1, R2, …, Rm}, the set consisting of all resource types in


the system
• Request edge – directed edge Pi  Rj
• Assignment edge – directed edge Rj  Pi

6 Operating Systems BITS Pilani, Pilani Campus


Resource-Allocation Graph (Cont.)

• Process

• Resource Type with 4 instances

• Pi requests instance of Rj Pi
Rj

• Pi is holding an instance of Rj Pi
Rj

7 Operating Systems BITS Pilani, Pilani Campus


Example of a Resource Allocation Graph

8 Operating Systems BITS Pilani, Pilani Campus


Resource Allocation Graph With A Deadlock

9 Operating Systems BITS Pilani, Pilani Campus


Resource Allocation Graph With A Cycle But
No Deadlock

10 Operating Systems BITS Pilani, Pilani Campus


Basic Facts

• If graph contains no cycles  no deadlock.

• If graph contains a cycle 


If only one instance per resource type, then deadlock
If several instances per resource type, possibility of
deadlock

11 Operating Systems BITS Pilani, Pilani Campus


Methods for Handling Deadlocks

• Ensure that the system will never enter a deadlock


state.
(Prevention or Avoidance)

• Allow the system to enter a deadlock state and then


recover.

12 Operating Systems BITS Pilani, Pilani Campus


Deadlock Characterization
Deadlock can arise if four conditions hold simultaneously.

• Mutual exclusion: only one process at a time can use a


resource(i.e. resource is non-sharable).
• Hold and wait: a process holding at least one resource is
waiting to acquire additional resources held by other
processes.
• No preemption: a resource can be released only
voluntarily by the process holding it, after that process has
completed its task.
• 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.
13 Operating Systems BITS Pilani, Pilani Campus
Deadlock Prevention

•Ensure that one of the 4 necessary conditions for deadlock


does not hold.
•Constraints the way requests can be made for resources
• Mutual exclusion
– not required for sharable resources;
– Must hold for non-sharable resources.
Make non-sharable resources as sharable
some resources are intrinsically non sharable

14 Operating Systems BITS Pilani, Pilani Campus


Deadlock Prevention

• Hold and wait – must guarantee that whenever a


process requests a resource, it does not hold any
other resources
Require process to request and be allocated all its resources
before it begins execution,
Need to know all the requirement in the beginning itself

Allow process to request resources only when the process has


none.
If a process has resource and requires additional resources, it
must release all resources it is holding and then make request
Low resource utilization; Starvation possible

15 Operating Systems BITS Pilani, Pilani Campus


Deadlock Prevention (Cont.)

• No preemption – Approach -1
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.
Cannot be generally applied to resources like printers, tape
drives etc.

16 Operating Systems BITS Pilani, Pilani Campus


No preemption (cont-)

Yes
If resource is Allocate
available resource

NO

Yes
Resource allocated to Preempt the resource
Waiting process from waiting process

No

Make the requesting


process wait

17 Operating Systems BITS Pilani, Pilani Campus


Deadlock Prevention (Cont.)

• Circular wait – impose a total ordering of all


resource types, and require that each process
requests resources in an increasing order of
enumeration

Operating System Concepts

18 Operating Systems BITS Pilani, Pilani Campus


Dead lock Handling approaches

• Prevention
• Avoidance
• Detection & Recovery

Operating Systems 19
19 Operating Systems BITS Pilani, Pilani Campus
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 finds the resource-allocation state in order
to ensure that a circular-wait condition does not occur.

• Resource-allocation state is defined by


– the number of available resources
– the number of allocated resources,
– the maximum requirement of the processes
20 Operating Systems BITS Pilani, Pilani Campus
Safe State

• When a process requests an available resource,


the system needs to decide if immediate
allocation leaves the system in a safe state.
• A state is safe if system can allocate resources to
each process (Max requirement ) in some order
and still avoid deadlock
• System is in safe state if there exists a safe
sequence of all processes.

21 Operating Systems BITS Pilani, Pilani Campus


• 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
resources, and so on

22 Operating Systems BITS Pilani, Pilani Campus


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

23 Operating Systems BITS Pilani, Pilani Campus


Safe, Unsafe , Deadlock State

24 Operating Systems BITS Pilani, Pilani Campus


Example

Maximum Allocation
need
P0 10 5
P1 4 2
P2 9 2

•Total no of resources =12


• safe sequence < P1,P0,P2>

Operating Systems BITS Pilani, Pilani Campus


Resource-Allocation Graph Algorithm

• Claim edge Pi  Rj indicates that process Pi may


request resource Rj; represented by a dashed line.

• Claim edge converts to request edge when a


process requests a resource.

• When a resource is released by a process,


assignment edge reconverts to a claim edge.

• Resources must be claimed a priori in the system.

26 Operating Systems BITS Pilani, Pilani Campus


Resource-Allocation Graph For
Deadlock Avoidance

27 Operating Systems BITS Pilani, Pilani Campus


Unsafe State In Resource-Allocation
Graph

28 Operating Systems BITS Pilani, Pilani Campus


Banker’s Algorithm

• Multiple instances.

• Each process must a priori claim maximum use.

• When a process requests a resource it may have to


wait.

• When a process gets all its resources it must


return them in a finite amount of time.

29 Operating Systems BITS Pilani, Pilani Campus


Data Structures for the Banker’s Algorithm
Let n = number of processes, and m = number of resources types.

• Available: Vector of length m. If Available [ j] = k,


there are k instances of resource type Rj available.
• Max: n x m matrix. If Max [I , j] = k, then process
Pi may request at most k instances of resource type
Rj.
• Allocation: n x m matrix. If Allocation[I , j] = k
then Pi is currently allocated k instances of Rj.
• Need: n x m matrix. If Need[I , j] = k, then Pi may
need k more instances of Rj to complete its task.
Need [I , j] = Max[I , j] – Allocation [I , j]
30 Operating Systems BITS Pilani, Pilani Campus
Safety Algorithm

1.Let Work and Finish be vectors of length m and n, respectively.


Initialize:
1. Work = Available
2. Finish [i] = false for i =0,1, …, n-1.
2. Find an index i such that both:
1. (a) Finish [i] == false
2. (b) Needi  Work
3. If no such i exists, go to step 4.
3. Work = Work + Allocationi
Finish[ i ] = true
go to step 2.
4. If Finish [i ] == true for all i, then the system is in a safe state.

31 Operating Systems BITS Pilani, Pilani Campus


Safe State example

R1 R2 R3
P1 2 2 2
P2 0 0 1
P3 1 0 3
P4 4 2 0

32 Operating Systems BITS Pilani, Pilani Campus


Resource-Request Algorithm for Process Pi

Request i = request vector for process Pi.


If Request i [j] = k then process Pi wants k instances of resource type Rj.

1. If Request i  Need i then go to step 2.


Otherwise, raise error condition, since process has exceeded its maximum claim.

2. If Request i  Available, go to step 3.


Otherwise Pi must wait, since resources are not available.

3. Pretend to allocate requested resources to Pi by modifying the state as follows:


Available = Available –Requesti ;
Allocationi = Allocationi + Requesti ;
Needi = Needi – Requesti
• If safe  the resources are allocated to Pi.
• If unsafe  Pi must wait, and the old resource-allocation state is restored

33 Operating Systems BITS Pilani, Pilani Campus


Example of Banker’s Algorithm

• 5 processes P0 through P4; 3 resource types A


(10 instances), B (5instances) and C (7 instances).
• Snapshot at time T0:
Allocation Max Available
ABC ABC ABC
P0 0 1 0 7 5 3 332
P1 2 0 0 3 2 2
P2 3 0 2 9 0 2
P3 2 1 1 2 2 2
P4 0 0 2 4 3 3

34 Operating Systems BITS Pilani, Pilani Campus


Example (Cont.)

• The content of the matrix Need is equal to Max – Allocation.


Need
Allocation Max Available
ABC ABC ABC ABC
P0 743 P0 010 753 332
P1 200 322
P1 122 P2 302 902
P3 211 222
P2 600 P4 002 433
P3 011
P4 431
• The system is in a safe state since the sequence < P1, P3, P4, P2, P0>
satisfies safety criteria.

35 Operating Systems BITS Pilani, Pilani Campus


Example P1 Request (1,0,2) (Cont.)

• Check that Request  Available that is, (1,0,2)  (3,3,2)  true.


Allocation Need Available
ABC ABC ABC
P0 0 1 0 743 230
P1 3 0 2 020
P2 3 0 2 600
P3 2 1 1 011
P4 0 0 2 431
• Executing safety algorithm shows that sequence <P1, P3, P4, P0, P2>
satisfies safety requirement.
• Can request for (3,3,0) by P4 be granted?
• Can request for (0,2,0) by P0 be granted?
36 Operating Systems BITS Pilani, Pilani Campus
Deadlock Detection

• Allow system to enter deadlock state

• Detection algorithm

• Recovery scheme

37 Operating Systems BITS Pilani, Pilani Campus


Single Instance of Each Resource Type

• Maintain wait-for graph


Nodes are processes
Pi  Pj if Pi is waiting for Pj

• Periodically invoke an algorithm that searches for a


cycle in the graph.

38 Operating Systems BITS Pilani, Pilani Campus


Resource-Allocation Graph and Wait-for Graph

Resource-Allocation Graph Corresponding wait-for graph

Operating Systems BITS Pilani, Pilani Campus


Several Instances of a Resource Type

• Available: A vector of length m indicates the number of


available resources of each type.

• Allocation: An n x m matrix defines the number of


resources of each type currently allocated to each process.

• Request: An n x m 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.

40 Operating Systems BITS Pilani, Pilani Campus


Detection Algorithm
1. Let Work and Finish be vectors of length m and n,respectively.
Initialize:
(a) Work = Available
(b) For i = 1,2, …, n, if Allocationi  0, then
Finish[i] = false;otherwise, Finish[i] = true.
2. Find an index i such that both:
(a) Finish[i] == false
(b) Requesti  Work
If no such i exists, go to step 4.
3. ,Work = Work + Allocationi
Finish[i] = true
go to step 2.
4. If Finish[i] == false, for some i, 1  i  n, then the system is in
deadlock state. Moreover, if Finish[i] == false, then Pi is deadlocked.

41 Operating Systems BITS Pilani, Pilani Campus


Example of Detection Algorithm
• Five processes P0 through P4; three resource types
A (7 instances), B (2 instances), and C (6 instances).
• Snapshot at time T0:
Allocation Request Available
ABC ABC ABC
P0 0 1 0 000 000
P1 2 0 0 202
P2 3 0 3 000
P3 2 1 1 100
P4 0 0 2 002
• Sequence <P0, P2, P3, P4, P1> will result in Finish[i] = true for all
i.

42 Operating Systems BITS Pilani, Pilani Campus


Example (Cont.)

• P2 requests an additional instance of type C.


Request
ABC
P0 0 0 0
P1 2 0 1
P2 0 0 1
P3 1 0 0
P4 0 0 2
• State of system?
Can reclaim resources held by process P0, but insufficient
resources to fulfill other processes requests.
Deadlock exists, consisting of processes P1, P2, P3, and P4.
43 Operating Systems BITS Pilani, Pilani Campus
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?

• Invoke the algorithm every time request for


resource cannot be granted immediately.
• Once per hour
• When CPU utilization drops below 40%

44 Operating Systems BITS Pilani, Pilani Campus


Recovery from Deadlock: Process Termination
• Abort all deadlocked processes  May be expensive

• Abort one process at a time until the deadlock cycle is


eliminated Invoke deadlock detection algorithm
after every abort.

• In which order should we choose to abort?


Priority of the process.
How long process has computed, and how much longer to completion.
Resources the process has used.
Resources process needs to complete.
Is process interactive or batch?

45 Operating Systems BITS Pilani, Pilani Campus


Recovery from Deadlock: 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.

46 Operating Systems BITS Pilani, Pilani Campus


Thanks

47 Operating Systems BITS Pilani, Pilani Campus


Memory Management

Prof J P Misra
BITS, Pilani

BITS Pilani, Pilani Campus


We have learnt

 Concept of multiprogramming
 It is defined for uniprocessor systems
 More than on program can reside in memory but at any time only
one can be excuting .
 Concept of process
 Process is an instance of executing program
 Process can be in various state at different point of time

 Memory is one of the important resource which OS


need to manage

BITS Pilani, Pilani Campus


What is Memory

 Memory is a device which can store information. It supports


two operations
 Store ( write )
 Load (Read)

 Memory devices take some finite amount of time to


perform read / write operation

BITS Pilani, Pilani Campus


Memory subsystem design
requirement
 Storage capacity ( larger the better)

 Speed of access ( faster access)

 Cost of storage ( lower the better)

BITS Pilani, Pilani Campus


Types of memory

 Based on persistence of information


 Volatile
 Non volatile

 Based on technology/ material used


 Semiconductor memory

RAM (Random access memory)


ROM (Read only memory )
 Magnetic memory

Hard disk
Magnetic tape
 Optical memory

CD/DVD

BITS Pilani, Pilani Campus


Memory Management
• Three design constraints of memory subsystem
– large Size
– high Speed
– low Cost
• Across the spectrum of the technologies following
relationship holds
– Smaller access time , greater per bit cost
– Greater capacity, smaller per bit cost
– Greater capacity , greater access time

BITS Pilani, Pilani Campus


To meet the contradictory design requirement ,
organize memory in hierarchical manner

BITS Pilani, Pilani Campus


• As one goes down the Hierarchy , the following
conditions occur:

 Decreasing cost per bit


 Increasing capacity
 Increasing access time

BITS Pilani, Pilani Campus


Example (two level memory)
Processor has access to two level of memory

– Level 1 contains 1000 words and access time(T1) is 0.1


Micro second
– Level 2 contains 100,000 words and access time (T2) is 1
Micro second

If the word is found in level 1


– then it is accessed in 0.1 Micro sec
– else 1.1 micro sec

BITS Pilani, Pilani Campus


T1+T2

T2

T1

Fraction of access involving only level 1 1


0
BITS Pilani, Pilani Campus
Observations

 We observe that if memory access at level 2 is


less frequent then overall access time is close to
level 1 access time

 The basis for validity of this condition is a


principal known as locality of reference

 During course of execution of program , memory


references for both data and instruction tends to
cluster
BITS Pilani, Pilani Campus
Memory Management Requirement
Relocation : ability to load and execute
when programs are loaded at different
location at different point of time.
Protection
Sharing
Logical organization
Physical organization

BITS Pilani, Pilani Campus


BITS Pilani, Pilani Campus
Binding of Instructions and Data to Memory
Address binding of instructions and data to memory addresses can
occur at three different point of time.
– Compile time: If memory location known a priori, (starting
location) absolute code can be generated; must recompile code if
starting location changes. Example MS DOS .COM format
programs.
– Load time: Must generate relocatable code if memory location is
not known at compile time. Final binding is delayed until load
time.
– Execution time: Binding delayed until run time if the process can
be moved during its execution from one memory segment to
another. Need hardware support for address maps (e.g., base
and limit registers). Most general purpose operating systems use
this method.

BITS Pilani, Pilani Campus


Loading Program into main memory
it is assumed that OS occupies some fixed portion of
memory and rest is available to user processes.

Based on requirement and functionality different


memory management methods are adopted.
Issues:

BITS Pilani, Pilani Campus


Memory management issues

– contiguous memory allocation ?


– How much memory to allocate to each process ?
– How to partition memory ?
• Static / dynamic partitioning
• Equal / unequal partition

BITS Pilani, Pilani Campus


Fixed Partitioning

• Main memory is divided into number of fixed size


partition at system generation time.

– A processes can be loaded into a partition of equal or


greater size
Limitation:
• The maximum number of processes that can be in
system becomes fixed at time of system generation
• Could lead to inefficient utilization of memory .

BITS Pilani, Pilani Campus


Equal size fixed Partition

• Easy to implement

• If total user memory space is X and size of partition is Y,


( Y< X) then number of partitions in system will be X / Y.
This is the maximum number of processes that can be
loaded in the memory at any given time

• If program size is smaller than the size of partition, the


remaining space remains unutilized

• A program may be too big to fit into a partition

BITS Pilani, Pilani Campus


Overlays

• Needed when process is larger than amount of memory


allocated to it.

• Keep in memory only those instructions and data that


are needed at any given time

• Implemented by user, no special support provided from


operating system,
• programming of overlay structure is complex

BITS Pilani, Pilani Campus


Symbol table
(20 K)

Common
routine
(30 K)

Overlay driver
(10 K)

Pass 1 Pass 2
(70 K) (80 K)

BITS Pilani, Pilani Campus


Unequal size partition

• We create fixed number of unequal size partition


• Program is loaded into best fit partition
– processes are assigned in such a way as to minimize
wasted memory within a partition
• queue for each partition

BITS Pilani, Pilani Campus


Equal size partition Unequal size partition
BITS Pilani, Pilani Campus
BITS Pilani, Pilani Campus
Thank You

BITS Pilani, Pilani Campus

You might also like