0% found this document useful (0 votes)
28 views63 pages

Os PPT-22

An operating system (OS) is a program that manages computer hardware and provides a platform for application programs, acting as an intermediary between users and hardware. The document outlines various types of computer systems, including mainframe, multi-programmed, time-sharing, and desktop systems, as well as different operating system architectures like client-server and peer-to-peer models. It also discusses the components of computer systems, the operation of OS, and the importance of memory management and storage structures.

Uploaded by

rpal2577
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)
28 views63 pages

Os PPT-22

An operating system (OS) is a program that manages computer hardware and provides a platform for application programs, acting as an intermediary between users and hardware. The document outlines various types of computer systems, including mainframe, multi-programmed, time-sharing, and desktop systems, as well as different operating system architectures like client-server and peer-to-peer models. It also discusses the components of computer systems, the operation of OS, and the importance of memory management and storage structures.

Uploaded by

rpal2577
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/ 63

Learning Objectives OPERATING SYSTEM -

DEFINITION
ØDefinition of OS An operating system is a program that manages a computer’s
hardware. It also provides a basis for application programs and
ØComponents of Computer System acts as an intermediary between the computer user and the
ØMainframe Systems computer hardware. An amazing aspect of operating systems
is how they vary in accomplishing these tasks. Mainframe
ØComputer System operation operating systems are designed primarily to optimize
ØSingle and Multi processor System utilization of hardware. Personal computer (PC) operating
systems support complex games, business applications, and
ØComponents of Operating System 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
1 efficient, and others to be some combination of the two. 2
Lecture 1 BITS Pilani, Pilani Campus Lecture I BITS Pilani, Pilani Campus

Components of Computer Components of Computer


System System
Before we can explore the details of computer system
operation, we need to know something about system structure. User1 User 2 .......... User n
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 Compiler Assembler database
functional operating system. Because an operating system is systems
large and complex, it must be created piece by piece. Each of
these pieces should be a well-delineated portion of the System and Application Programs
system, with carefully defined inputs, outputs, and functions
A computer system has four components. They are Operating System
1. Hardware 3. Users
2. Application Programs 4. Operating System Computer Hardware
3 4
Lecture I BITS Pilani, Pilani Campus Lecture I BITS Pilani, Pilani Campus

Components of OS Components of Computer


System
Application Programs - These are the softwares such as The view of the computer varies from users accordingly to the
Word Processors, spreadsheets, Compilers and Web Browsers interface provided to them. Most of the users sit in front of the
to be used to solve the computing problems of the user computer and execute the programs with the help of
Users – They are the people who are using the computer keyboard, mouse , System unit and Monitor. In some cases,
system to solve the problems users are provided with the GUI [ Graphical User Interface ]
Operating System – It is a system software that controls and feature to perform their tasks easily.
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.

5 6
Lecture I BITS Pilani, Pilani Campus Lecture I BITS Pilani, Pilani Campus
MAINFRAME SYSTEMS MAINFRAME SYSTEMS

Mainframe computers are the computers which are used to The jobs were entered in the punched cards.
solve Commercial and Scientific applications. Growth of the The operating systems were simple and their tasks were to
computer systems ranges from batch systems to time –shared transfer control automatically fro job to another. They resided
systems. in the memory as
1.Batch Systems
Early computers were bigger machine with the input devices , Operating
card readers, tape drives and the common output devices System
such as line printers and card punches. The users could not
interact wit the computer system directly. Instead , the user User Program
prepared a job [ consisted of program, data and control area
information] and submitted to the computer for the execution .
MEMORY LAY OUT
7 8
Lecture I BITS Pilani, Pilani Campus Lecture I BITS Pilani, Pilani Campus

MAINFRAME SYSTEMS MAINFRAME SYSTEMS

To speed up the processing, the similar jobs were batched executed and were kept in the job pool. The operating system
together and were executed. In this type of systems, the CPU is picks and executed one of the jobs in the memory . When a
often idle because the I/O devices are slower than memory job waits for some task such waiting for an input, the operating
speed. system switches to other job. If this job needs some wait, the
2.Multi programmed Systems CPU is switched to another job, and so on. As long as one job
The most important aspect of job scheduling is the ability of needs to be executed, the CPU is never idle.
multi programming. Multi programming increases the CPU If many jobs are waiting for the execution and are ready to be
utilization by organizing the jobs in a such a way that CPU brought into memory for the execution, the operating system
executed them efficiently. chooses one among them. This decision is called as Job
In multi programmed systems, the operating system keeps Scheduling.
several jobs in the memory simultaneously as follow:
The jobs that are kept in the memory are the jobs to be
9 10
Lecture I BITS Pilani, Pilani Campus Lecture I BITS Pilani, Pilani Campus

MAINFRAME SYSTEMS MAINFRAME SYSTEMS

3.Time sharing systems Linux, MacOs)


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

MAINFRAME SYSTEMS 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.

15 16
Lecture I BITS Pilani, Pilani Campus Lecture I BITS Pilani, Pilani Campus

MAINFRAME SYSTEMS MAINFRAME SYSTEMS

6.Clustering systems 7. Real Time Systems


These are the systems supporting clustering. Clustering allows These are the systems which have time constraints. These
two or more systems to share storage. They are either systems give the guarantee that the maximum time for critical
Asymmetric Clustering or Symmetric Clustering. operations to complete on time. This type of system is called as
In symmetric clustering, all ‘n’ hosts are running the Hard Real Time system. But some of these systems give the
Application. In asymmetric clustering, all servers run the guarantee that the maximum time for critical task but no
application and one server acts as a stand by assurance is given for completing them on time. These are
Advantages called as soft real time systems.
- High performance 8.Hand held systems
- Fault tolerant because failure one node does not affect the They are usually smaller in size with small amount of memory,
whole system slow processors and a small display screen. Hand held
- Scalability is achieved because we can add new nodes systems are Personal Digital Assistants (PDAs) such as cellular
17 18
Lecture I BITS Pilani, Pilani Campus Lecture I BITS Pilani, Pilani Campus
MAINFRAME SYSTEMS Computer System operation

phones with connectivity to a network such as the internet. A modern general-purpose computer system consists of one
They have between 512 KB to 8 MB memories. Some hand held or more CPUs and a number of device controllers connected
systems use wireless technology such as Blue tooth allowing through a common bus that provides access to shared memory
remote access to e-mail and web browsing. (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

19 20
Lecture I BITS Pilani, Pilani Campus Lecture I BITS Pilani, Pilani Campus

Computer System operation 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.

21 22
Lecture I BITS Pilani, Pilani Campus Lecture I BITS Pilani, Pilani Campus

Computer System operation Computer System operation

To accomplish this goal, the bootstrap program must locate may trigger an interrupt at any time by sending a signal to the
the operating-system kernel and load it into memory.Once the CPU, usually by way of the system bus. Software may trigger an
kernel is loaded and executing, it can start providing services interrupt by executing a special operation called a system
to the system and its users. Some services are provided call (also called a monitor call).
outside of the kernel, by system programs that are loaded into Storage Structure
memory at boot time to become system processes, or The CPU can load instructions only from memory, so any
system daemons that run the entire time the kernel is programs to run must be stored there. General-purpose
running. On UNIX, the first system process is “init,” and it starts computers run most of their programs from rewritable
many other daemons. Once this phase is complete, the system memory, called main memory (also called random-access
is fully booted, and the system waits for some event to occur. memory, or RAM). Main memory commonly is implemented
The occurrence of an event is usually signaled by an interrupt in a semiconductor technology called dynamic random-
from either the hardware or the software. Hardware access memory (DRAM).
23 24
Lecture I BITS Pilani, Pilani Campus Lecture I BITS Pilani, Pilani Campus
Computer System operation Computer System operation

Computers use other forms of memory as well. We have load or store instructions to specific memory addresses. The
already mentioned read-only memory, ROM) and electrically load instruction moves a byte or word from main memory to an
erasable programmable read-only memory, EEPROM). internal register within the CPU, whereas the store instruction
Because ROM cannot be changed, only static programs, such moves the content of a register to main memory. Aside from
as the bootstrap program described earlier, are stored there. explicit loads and stores, the CPU automatically loads
The immutability of ROM is of use in game cartridges. instructions from main memory for execution.
EEPROM can be changed but cannot be changed frequently Thus, most computer systems provide secondary storage as
and so contains mostly static programs. For example, an extension of main memory. The main requirement for
smartphones have EEPROM to store their factory-installed secondary storage is that it be able to hold large quantities of
programs. data permanently.
All forms of memory provide an array of bytes. Each byte has The most common secondary-storage device is a magnetic
its own address. Interaction is achieved through a sequence of disk, which provides storage for both programs and data. Most
25 26
Lecture I BITS Pilani, Pilani Campus Lecture I BITS Pilani, Pilani Campus

Computer System operation Computer System operation


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

Computer System operation Computer System operation


Registers

The top four levels of memory in Figure may be constructed


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

29 30
Lecture 1 BITS Pilani, Pilani Campus Lecture I BITS Pilani, Pilani Campus
Computer System operation Single and Multi processor
Systems
Solid-state disks have several variants but in general are A computer system can be organized in a number of different
faster than magnetic disks and are nonvolatile. Another form of ways, which we can categorize roughly according to the
solid-state disk is flash memory, which is popular in cameras number of general-purpose processors used.
and personal digital assistants (PDAs), in robots, and Single-Processor Systems
increasingly for storage on general-purpose computers. Flash Until recently, most computer systems used a single processor.
memory is slower than DRAM but needs no power to retain its On a single- processor system, there is one main CPU capable
contents. Another form of nonvolatile storage is NVRAM, of executing a general-purpose instruction set, including
which is DRAM with battery backup power. This memory can instructions from user processes. Almost all single- processor
be as fast as DRAM and (as long as the battery lasts) is systems have other special-purpose processors as well. They
nonvolatile. 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
31 32
Lecture I BITS Pilani, Pilani Campus Lecture I BITS Pilani, Pilani Campus

Single and Multi processor Single and Multi processor


Systems Systems
processors, such as I/O processors that move data rapidly tablet computers.
among the components of the system. Multiprocessor systems have three main advantages:
Multiprocessor Systems Increased throughput. By increasing the number of
Within the past several years, multiprocessor systems (also processors, we expect to get more work done in less time. The
known as parallel systems or multicore systems) have speed-up ratio with N processors is not N, however; rather, it is
begun to dominate the landscape of computing. Such systems less than N. When multiple processors cooperate on a task, a
have two or more processors in close communication, sharing certain amount of overhead is incurred in keeping all the parts
the computer bus and sometimes the clock, memory, and working correctly. This overhead, plus contention for shared
peripheral devices. Multiprocessor systems first appeared resources, lowers the expected gain from additional
prominently appeared in servers and have since migrated to processors. Similarly, N programmers working closely
desktop and laptop systems. Recently, multiple processors together do not produce N times the amount of work a single
have appeared on mobile devices such as smartphones and programmer would produce.
33 34
Lecture I BITS Pilani, Pilani Campus Lecture I BITS Pilani, Pilani Campus

Computer System Components of Operating


Architecture System
-Economy of scale. Multiprocessor systems can cost less than An Operating System (OS) is an interface between a computer
equivalent multiple single-processor systems, because they user and computer hardware. An operating system is a
can share peripherals, mass storage, and power supplies. If software which performs all the basic tasks like file
several programs operate on the same set of data, it is cheaper management, memory management, process management,
to store those data on one disk and to have all the processors handling input and output, and controlling peripheral devices
share them than to have many computers with local disks and such as disk drives and printers.
many copies of the data. Some popular Operating Systems include Linux Operating
Increased reliability. If functions can be distributed properly System, Windows OS, VMS, OS/400, AIX, z/OS, etc.
among several processors, then the failure of one processor Definition
will not halt the system, only slow it down. If we have ten An operating system is a software that acts as an interface
processors and one fails, then each of the remaining nine between the user and the computer hardware and controls the
processors can pick up a share of the work of the failed execution of all kinds of programs.
processor. 35 36
Lecture I BITS Pilani, Pilani Campus Lecture I BITS Pilani, Pilani Campus
Components of Operating Components of Operating
System System
Following are some of important functions of an operating Memory Management
System. Memory management refers to management of Primary
• Memory Management Memory or Main Memory. Main memory is a large array of
• Processor Management words or bytes where each word or byte has its own address.
• Device Management Main memory provides a fast storage that can be accessed
• File Management directly by the CPU. For a program to be executed, it must in
• Security the main memory. An Operating System does the following
• Control over system performance activities for memory management −
• Job accounting • Keeps tracks of primary memory, i.e., what part of it are in use
• Error detecting aids by whom, what part are not in use.
• Coordination between other software and users • In multiprogramming, the OS decides which process will get
memory when and how much.
37 38
Lecture I BITS Pilani, Pilani Campus Lecture I BITS Pilani, Pilani Campus

Components of Operating Components of Operating


System System
• Allocates the memory when a process requests it to do so. • De-allocates processor when a process is no longer required.
• De-allocates the memory when a process no longer needs it Device Management
or has been terminated. An Operating System manages device communication via their
Processor Management respective drivers. It does the following activities for device
In multiprogramming environment, the OS decides which management −
process gets the processor when and for how much time. This • Keeps tracks of all devices. Program responsible for this task
function is called process scheduling. An Operating System is known as the I/O controller.
does the following activities for processor management − • Decides which process gets the device when and for how
• Keeps tracks of processor and status of process. The much time.
program responsible for this task is known as traffic • Allocates the device in the efficient way.
controller. • De-allocates devices.
• Allocates the processor (CPU) to a process.
39 40
Lecture I BITS Pilani, Pilani Campus Lecture I BITS Pilani, Pilani Campus

Components of Operating Components of Operating


System System
File Management Other Important Activities
A file system is normally organized into directories for easy Following are some of the important activities that an
navigation and usage. These directories may contain files and Operating System performs −
other directions. Security − By means of password and similar other
An Operating System does the following activities for file techniques, it prevents unauthorized access to programs and
management − data.
• Keeps track of information, location, uses, status etc. The Control over system performance − Recording delays
collective facilities are often known as file system. between request for a service and response from the system.
• Decides who gets the resources. Job accounting − Keeping track of time and resources used by
• Allocates the resources. various jobs and users.
• De-allocates the resources. Error detecting aids − Production of dumps, traces, error
messages, and other debugging and error detecting aids.
41 42
Lecture I BITS Pilani, Pilani Campus Lecture I BITS Pilani, Pilani Campus
Components of Operating Learning Objectives
System
Coordination between other softwares and users − ØOperating System services
Coordination and assignment of compilers, interpreters,
ØSystem calls
assemblers and other software to the various users of the
computer systems. - Definition
- Types of System calls

43 44
Lecture I BITS Pilani, Pilani Campus Lecture - 2 BITS Pilani, Pilani Campus

OPERATING SYSTEM OPERATING SYSTEM


SERVICES SERVICES
An operating system provides an environment for the
User and other system programs
execution of programs. It provides certain services to
GUI Batch Command Line
programs and to the users of those programs. The specific User Interface
services provided, of course, differ from one operating system
SYSTEM CALLS
to another, but we can identify common classes. These
operating system services are provided for the convenience of
Program I/O file Communication Resource Accounting
the programmer, to make the programming task easier. execution operations systems allocation
The following Figure shows one view of the various operating-
system services and how they interrelate. error detection Services Protection and
Security

OPERATING SYSTEM

Hardware
45 46
Lecture - 2 BITS Pilani, Pilani Campus Lecture - 2 BITS Pilani, Pilani Campus

OPERATING SYSTEM OPERATING SYSTEM


SERVICES SERVICES
One set of operating system services provides functions that • Program execution. The system must be able to load a
are helpful to the user. program into memory and to run that program. The program
• User interface. Almost all operating systems have a user must be able to end its execution, either normally or
interface (UI). This interface can take several forms. One is a abnormally (indicating error).
command-line interface (CLI), which uses text commands • I/O operations.A running program may require I/O, which
and a method for entering them (say, a keyboard for typing in may involve a file or an I/O device. For specific devices,
commands in a specific format with specific options). Another Special functions may be desired (such as recording to a CD
is a batch interface, in which commands and directives to Or DVD drive or blanking a display screen). For efficiency and
control those commands are entered into files, and those files protection, users usually cannot control I/O devices directly.
are executed. Most commonly, a graphical user interface Therefore, the operating system must provide a means to do
(GUI) is used. I/O

47 48
Lecture - 2 BITS Pilani, Pilani Campus Lecture - 2 BITS Pilani, Pilani Campus
OPERATING SYSTEM OPERATING SYSTEM
SERVICES SERVICES
• File-system manipulation. The file system is of particular • Communications. There are many circumstances in which
interest. Obviously, programs need to read and write files and one process needs to exchange information with another
directories. They also need to create and delete them by name, process. Such communication may occur between processes
search for a given file, and list file information. Finally, some that are executing on the same computer or between
operating systems include permissions management to allow processes that are executing on different computer systems
or deny access to files or directories based on file ownership. tied together by a computer network
Many operating systems provide a variety of file systems, Error detection. The operating system needs to be detecting
sometimes to allow personal choice and sometimes to provide and correcting errors constantly. Errors may occur in the CPU
specific features or performance characteristics. 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
49 50
Lecture - 2 BITS Pilani, Pilani Campus Lecture - 2 BITS Pilani, Pilani Campus

OPERATING SYSTEM OPERATING SYSTEM


SERVICES SERVICES
overflow, an attempt to access an illegal memory location, or a • Accounting. We want to keep track of which users use how
too-great use of CPU time). For each type of error, the much and what kinds of computer resources. This record
operating system should take the appropriate action to ensure keeping may be used for accounting (so that users can be
correct and consistent computing. billed) or simply for accumulating usage statistics. Usage
Another set of operating system functions exists not for statistics may be a valuable tool for researchers who wish to
helping the user but rather for ensuring the efficient operation reconfigure the system to improve computing services.
of the system itself. Systems with multiple users can gain • Protection and security. The owners of information stored
efficiency by sharing the computer resources among the in a multiuser or networked computer system may want to
users. control use of that information. Security starts with requiring
• Resource allocation. When there are multiple users or each user to authenticate himself or herself to the system,
multiple jobs running at the same time, resources must be usually by means of a password, to gain access to system
allocated to each of them. resources.
51 52
Lecture - 2 BITS Pilani, Pilani Campus Lecture - 2 BITS Pilani, Pilani Campus

SYSTEM CALLS SYSTEM CALLS

A system call is a method for programs to interact with the - Wait and assigned event
operating system. It provides an interface between a process - Allocates and De allocates
and the operating system to allow user – level processes to 2. In file management,
request services of the operating system. The system call performs file manipulations
Services Provided by the System calls Functions:
1. In process control, - Create a file
The system call performs the task of process creating, process - Open and Close file
termination, etc., - Read, Write and Reposition
Functions: - Delete a file
- End and Abort - Get and Set file attributes
- Load and Execute 3. In device management,
- Create and terminate process The system call does the jobs of device manipulation
53 54
Lecture - 2 BITS Pilani, Pilani Campus Lecture - 2 BITS Pilani, Pilani Campus
SYSTEM CALLS SYSTEM CALLS

Functions: Functions:
- Request and Release device Create and delete communication
- Get and Set device attribute Sending and receiving message
4.In Information Maintenance Attach and detach remote services
The system call handles information and its transfer between The following are the example for System calls in Windows
the user and the OS and Unix OS
Functions: Windows Unix
Get the time or setting the time Process CreateProcess() fork()
Get the process and device attributes Control ExitProcess() exit()
5. Communication File CreateFile() open()
Manipulation ReadFile() read()
The system call is used for inter-process communication
WriteFile() write()
55 56
Lecture - 2 BITS Pilani, Pilani Campus Lecture - 2 BITS Pilani, Pilani Campus

SYSTEM CALLS SYSTEM CALLS

CloseHandle() close() Protection SetFileSecurity() chmod()


Device SetConsoleMode() ioctl() InitlializeSecurityDescriptor() umask()
Manipulation ReadConsole() read() SetSecurityDescriptorGroup() chown()
WriteConsole() write()

Information GetCurrentProcessID() getpid()


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

Communication CreatePipe() pipe()


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

57 58
Lecture - 2 BITS Pilani, Pilani Campus Lecture - 2 BITS Pilani, Pilani Campus

Learning Objectives PROCESS CONCEPT

Ø What is a process? Process Concept


Early computers had complete control over the execution of
Ø States of a process programs because they allowed only one program to execute.
Ø Process Control Block But current day computer systems permit multiple programs to
be loaded into the memory and to be executed concurrently. It
ØProcess Scheduling – Basics requires firmer control which resulted in the notion of a
- Process Scheduling queues Process.
A process is a program in execution. A process is not only the
- Two process model program but it is also includes the program counter, Stack and
- Scheduler and its types a data section.
• Program Counter – It specifies the next instruction to be
Ø Context Switching
executed
59 60
Lecture - III BITS Pilani, Pilani Campus Lecture - III BITS Pilani, Pilani Campus
PROCESS CONCEPT PROCESS CONCEPT
• Stack – It is used to store temporary data such as local • New State – The process is being created
variables, parameters and return address • Running State – Instructions of the process are being
• Data Section – It contains the global variables executed
States of a Process • Waiting State – The running process is waiting for some event
As process executes, its state changes. The state of a process to occur
represents the part by which the current activity of that • Ready State – The process is waiting to be assigned to a
process. Each process may be in any of the following states: processor
(i) New • Terminated state – The process had finished its execution.
(ii) Running
(iii) Waiting The following diagram represents the different states of the
(iv) Ready process :
(v) Terminated
61 62
Lecture - III BITS Pilani, Pilani Campus Lecture - III BITS Pilani, Pilani Campus

States of a PROCESS PROCESS CONCEPT


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

PROCESS CONTROL BLOCK PROCESS CONCEPT


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

PROCESS SCHEDULING PROCESS SCHEDULING


QUEUES
1 2 Two Process Model
Two process models refers to running and non – running states
Ready Queue CPU as
4 3
• Running – When a new process is created , it enters into the
I/O I/O queue I/O request
system as in the running state
time slice expired • Not Running – Processes that are not running are kept in
the queue, waiting for their turn to execute. Each entry in the
5 queue is a pointer to a particular process. Queue is
child executes fork a child
implemented in linked list structure.
6
interrupt occurs waits for an When a process is interrupted, the process is transferred in
interrupt the waiting queue. If the process has completed or aborted,
the process is discarded. In either case, the component
69 dispatcher selects a process from the queue to execute. 70
Lecture - III BITS Pilani, Pilani Campus Lecture - III BITS Pilani, Pilani Campus

PROCESS SCHEDULING PROCESS SCHEDULING


Scheduler to provide a balanced mix of jobs, such as I/O bound and
Scheduler is a special system software which handles process processor bound
scheduling in various ways. Their main task is to select the Short term Scheduler – It is also called as CPU scheduler. Its
jobs to be submitted into the system and to decide which main objective is to increase system performance in
process to run. There are three types of schedulers. They are accordance with some criteria. Short term schedulers are also
(i) Long term Scheduler known as dispatchers , makes the decision of which process to
(ii) Short term Scheduler execute. Short term schedulers are faster than long – term
(iii) Medium time scheduler scheduler.
Long Time Scheduler – It is also called as job scheduler or Medium Scheduler – Medium schedule is part of swapping. It
high level scheduler. A long term scheduler determines which removes the processes from the memory. It reduces the
processes are admitted to the system for processing. It selects degree of multi programming. The medium term scheduler is
the processes from the queue and load them into the memory in charge of handling swapped – out processes.
for execution. The primary objective of the job scheduler is 71 72
Lecture - III BITS Pilani, Pilani Campus Lecture - III BITS Pilani, Pilani Campus
PROCESS SCHEDULING CONTEXT SWITCHING
A running process may become suspended if it makes an I/O Context Switching
request. A suspended process can not make any progress It is the mechanism to store and restore or context of CPU in
towards completion. The suspended process is moved to the process control block so that process execution can be
secondary storage. This process is called swapping. The resumed for the same point at a later time. Using this
process is said to be swapped out or rolled out. technique, a context switch controls multiples processes to
Comparison between Schedulers share a single CPU. Context Switching is an essential part of
Long term Short term Medium Term multi tasking operating system features.
Job Scheduler CPU scheduler Process swapping scheduler When the scheduler switches the CPU from executing one
Speed is less Speed is faster Speed is medium process to execute another process , the state from the current
Controls the degree of Lesser control over Reduces the degree running process is stored into the PCB. After this, the ready
multi programming degree of multiprogramming of multi programming
state e process is allowed to execute / run. It is loaded into the
Selects process from pool
and loads into memory
Selects processes which
are ready execute
Reintroduce the process
into memory and execution
memory from its PCB, set the program counter and register.
is continued.
73 74
Lecture - III BITS Pilani, Pilani Campus Lecture - III BITS Pilani, Pilani Campus

CONTEXT SWITCHING 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
75 executed i.e. the process P2. 76
Lecture - III BITS Pilani, Pilani Campus Lecture - III BITS Pilani, Pilani Campus

CONTEXT SWITCHING Learning Objectives


4. Now, update the Process Control Block of process P2 i.e. Ø Inter process Communication (IPC)
PCB2 by setting the process state to running. If the process P2
was earlier executed by the CPU, then you can get the position
- What is IPC?
of last executed instruction so that you can resume the - Methods of Inter process Communication
execution of P2. Ø Threads
5. Similarly, if we want to execute the process P1 again, then
- What is a thread?
we have to follow the same steps as mentioned above(from
step 1 to 4). - Single and multi threaded programming
- Benefits of multi threaded programming
- Multi threading Models

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

PROCESS CONCEPT PROCESS CONCEPT


Cooperating processes require an inter process
communication (IPC) mechanism that will allow them to Process A
exchange data and information between them. Writing
There are two fundamental models inter process communication Shared Memory
are
1. Shared memory Process B Reading
2. Message Passing
Shared Memory Kernel
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 If process a wants to communicate data with other process B, A
writing data to the shared region. writes the data on this memory area which will be read by the
The following picture represents is 81 process B. 82
Lecture - IV BITS Pilani, Pilani Campus Lecture - IV BITS Pilani, Pilani Campus

PROCESS CONCEPT PROCESS CONCEPT


Message Passing The other inter process communication are
In the message passing model, communication takes place by (i) Pipe
means of messages exchanged between the cooperating (ii) Socket
processes as follows: (iii) File
Kernel
Process A M 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
Process B M Hardware and it helps with process and memory management,
file system, device control and networking

Kernel M
83 84
Lecture - IV BITS Pilani, Pilani Campus Lecture - IV BITS Pilani, Pilani Campus
THREAD 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 Code Data Files Code Data Files
other threads belonging to the same process its code section,
data section, and other operating-system resources, such as registers Stack registers registers registers
open files and signals. A traditional (or heavyweight) process
has a single thread of control. If a process has multiple threads Stack Stack
Stack
of control, it can perform more than one task at a time.
The following figure illustrates the difference between a Thread
traditional single- threaded process and a multithreaded Thread

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. 85 86
Lecture - IV BITS Pilani, Pilani Campus Single thread process Multiple
Lecture - IV thread process
BITS Pilani, Pilani Campus

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

THREAD THREAD
service the request and resume listening for additional Benefits
requests. It is illustrated in the following diagram: The benefits of multithreaded programming can be broken
down into four major categories:
(1) request (2) create a new thread Responsiveness. Multithreading an interactive application
to service the request
may allow a program to continue running even if part of it is
blocked or is performing a lengthy operation, thereby
Client Server thread
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
(3) resume listening for
additional requests 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-
Multithreaded server architecture 89 consuming operation is performed in a separate thread, the 90
Lecture - IV BITS Pilani, Pilani Campus Lecture - IV BITS Pilani, Pilani Campus
THREAD THREAD
application remains responsive to the user. difference in overhead can be difficult, but in general it is
Resource sharing. Processes can only share resources significantly more time consuming to create and manage
through techniques such as shared memory and message processes than threads. In Solaris, for example, creating a
passing. Such techniques must be explicitly arranged by the process is about thirty times slower than is creating a thread,
programmer. However, threads share the memory and the and context switching is about five times slower.
resources of the process to which they belong by default. The Scalability. The benefits of multithreading can be even
benefit of sharing code and data is that it allows an application greater in a multiprocessor architecture, where threads may be
to have several different threads of activity within the same running in parallel on different processing cores. A single-
address space. threaded process can run on only one processor, regardless
Economy. Allocating memory and resources for process how many are available. We explore this issue further in the
creation is costly. Because threads share the resources of the following section.
process to which they belong, it is more economical to create
and context-switch threads. Empirically gauging the 91 92
Lecture - IV BITS Pilani, Pilani Campus Lecture - IV BITS Pilani, Pilani Campus

THREAD THREAD
Multithreading Models Many – to – One Model
Support for threads may be provided either at the user level, The many-to-one model (See the figure ) maps many user-
for user threads, or by the kernel, for kernel threads. User level threads to one kernel thread. Thread management is
threads are supported above the kernel and are managed done by the thread library in user space, so it is efficient.
without kernel support, whereas kernel threads are supported However, the entire process will block if a thread makes a
and managed directly by the operating system. Virtually all blocking system call.
contemporary operating systems— including Windows, Linux, Also, because only one thread can access the kernel at a time,
Mac OS X, and Solaris— support kernel threads. multiple threads are unable to run in parallel on multicore
Ultimately, a relationship must exist between user threads and systems. Green threads — a thread library available for
kernel threads. In this section, we look at three common ways Solaris systems and adopted in early versions of Java— used
of establishing such a relationship: the many-to-one model, the the many-to-one model. However, very few systems continue
one-to-one model, and the many-to- many model. to use the model because of its inability to take advantage of
93 multiple processing cores. 94
Lecture - IV BITS Pilani, Pilani Campus Lecture - IV BITS Pilani, Pilani Campus

THREAD – Many –to - One THREAD – One – to One


Model Model
One – to – One Model
User Thread 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
K Kernel Thread 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,
95 implement the one-to-one model. 96
BITS Pilani, Pilani Campus Lecture - IV BITS Pilani, Pilani Campus
THREAD 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
User Thread 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
K K K K Kernel Thread 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
97 concurrency, but the developer has to be careful not to create 98
Lecture - IV BITS Pilani, Pilani Campus Lecture - IV BITS Pilani, Pilani Campus

THREAD THREAD – Many – to – Many


Model
too many threads within an application (and in some instances
may be limited in the number of threads she can User
create). The many-to-many model suffers from neither of these Thread
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.

K K K Kernel
Thread

99 100
Lecture - IV BITS Pilani, Pilani Campus Lecture - IV BITS Pilani, Pilani Campus

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
102
BITS Pilani, Pilani Campus Lecture - V BITS Pilani, Pilani Campus
THREAD LIBARARIES THREAD LIBARARIES
Thread libraries are the APIs to be used for creating , int sum=0;
executing and terminating the threads. void *run(void *p);
There are two types of threads. They are int main(int ac,char *argv[])
{
1. PTHREAD (POSIX)
pthread_t tid;
2. Java Threads
pthread_attr_t attr;
PTHREAD
pthread_attr_init(&attr);
It is an API to create and run a thread. The API is written in C pthread_create(&tid,&attr,run,argv[1]);
language. pthread_join(tid,NULL);
The following is the code written for finding the sum of first ‘n’
printf("\n Summation is %d",sum);
numbers.
return 0;
#include <stdio.h>
}
#include <pthread.h>
#include <stdlib.h>
103 104
Lecture - V BITS Pilani, Pilani Campus Lecture - V BITS Pilani, Pilani Campus

THREAD LIBARARIES PROCESS SHEDULING


void *run(void *p) CPU Scheduling is a process of determining which process will own
{ CPU for execution while another process is on hold. The main task of
int i,up=atoi(p); 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
for(i=1;i<=up;i++)sum+=i;
queue for execution. The selection process will be carried out by the
pthread_exit(0);
CPU scheduler. It selects one of the processes in memory that are
}
ready for execution.
Java Thread libraries have lot of built –in- methods for Types of CPU Scheduling
managing the threads. The important thread methods are: Here are two kinds of Scheduling methods:
Start() – Making the thread to be ready
Run() – Execute the thread

105 106
Lecture - V BITS Pilani, Pilani Campus Lecture - V BITS Pilani, Pilani Campus

PROCESS SHEDULING PROCESS SHEDULING


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

107 108
Lecture - V BITS Pilani, Pilani Campus Lecture - V BITS Pilani, Pilani Campus
PROCESS SHEDULING PROCESS SHEDULING
Arrival Time(AT): when a process enters in a ready state Maximizing:
Completion Time (CT): It is the finishing or completion time of the CPU utilization: CPU utilization is the main task in which the
process in the system operating system needs to make sure that CPU remains as busy as
Turnaround Time (TA): It is the time difference between possible. It can range from 0 to 100 percent. However, for the RTOS, it
Completion Time and Arrival Time can be range from 40 percent for low-level and 90 percent for the
TA = CT - AT high-level system.
Waiting Time (WT): It is the time difference between Turnaround Throughput: The number of processes that finish their execution per
Time and Burst Time unit time is known Throughput. So, when the CPU is busy executing
WT = TA - BT the process, at that time, work is being done, and the work
CPU Scheduling Criteria completed per unit time is called Throughput.
A CPU scheduling algorithm tries to maximize and minimize the Minimizing:
following: Waiting time: Waiting time is an amount that specific process needs
to wait in the ready queue.
109 110
Lecture - V BITS Pilani, Pilani Campus Lecture - V BITS Pilani, Pilani Campus

PROCESS SHEDULING PROCESS SHEDULING


Response time: It is an amount to time in which the request was Example:- The following table represents the time of arrival and
submitted until the first response is produced. burst time of 4 processes
Turnaround Time: Turnaround time is an amount of time to execute
a specific process. It is the calculation of the total time spent waiting Process Arrival Burst Solution:
to get into the memory, waiting in the queue and, executing on the Time(AT) Time(BT) The Gantt Chart for the Jobs is

CPU. The period between the time of process submission to the


P0 0 5 P0 P1 P2 P3
completion time is the turnaround time.
First Come First Serve (FCFS) P1 1 3 0 5 8 16 22
• Jobs are executed on first come, first serve basis.
• It is a non-preemptive, pre-emptive scheduling algorithm. P2 2 8
• Easy to understand and implement.
• Its implementation is based on FIFO queue. P3 3 6
• Poor in performance as average wait time is high.
111 112
Lecture - V BITS Pilani, Pilani Campus Lecture - V BITS Pilani, Pilani Campus

PROCESS SHEDULING PROCESS SHEDULING


Shortest Job Next (SJN)
Process Arrival Burst Completion Turnaround Waiting • This is also known as shortest job first, or SJF
Time(AT) Time(BT) Time(CT) Time(TA) Time(WA) • This is a non-preemptive, pre-emptive scheduling algorithm.
• Best approach to minimize waiting time.
P0 0 5 5 5 0
• Easy to implement in Batch systems where required CPU time is
known in advance.
P1 1 3 8 7 4
• Impossible to implement in interactive systems where required
CPU time is not known.
P2 2 8 16 14 6
• The processer should know in advance how much time process will
take.
P3 3 6 22 19 13
The problem mentioned for FCFS is solved as follows:

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

113 114
Lecture - V BITS Pilani, Pilani Campus Lecture - V BITS Pilani, Pilani Campus
PROCESS SHEDULING PROCESS SHEDULING

Process Arrival Burst Solution: Process Arrival Burst Completion Turnaround Waiting
Time(AT) Time(BT) The Gantt Chart for the Jobs is Time(AT) Time(BT) Time(CT) Time(TA) Time(WA)

P0 0 5 P0 P1 P3 P2 P0 0 5 5 5 0

P1 1 3 0 5 8 14 22 P1 1 3 8 7 4

P2 2 8 P2 2 8 22 20 12

P3 3 6 P3 3 6 14 11 5

Note:- Important Point is that though P1 has shortest time than P0, P0 arrives Average Waiting Time = ( 0 + 4 + 12 + 5 ) / 4 = 21 / 4 = 5.25
at time 0 (When the entire system begins) and P1 comes after 1 unit of time
115 116
Lecture - V BITS Pilani, Pilani Campus Lecture - V BITS Pilani, Pilani Campus

PROCESS SHEDULING PROCESS SHEDULING


Priority Based Scheduling
• Priority scheduling is a non-preemptive algorithm and one of the Process Arrival Burst Priority
most common scheduling algorithms in batch systems. Time(AT) Time(BT)
• Each process is assigned a priority. Process with highest priority is
to be executed first and so on. P0 0 5 1
• Processes with same priority are executed on first come first P1 1 3 2
served basis. P2 2 8 1
• Priority can be decided based on memory requirements, time P3 3 6 3
requirements or any other resource requirement.
Solution: The Gantt Chart for the Jobs is

P0 P3 P1 P2

0 5 11 14 22

117 118
Lecture - V BITS Pilani, Pilani Campus Lecture - V BITS Pilani, Pilani Campus

PROCESS SHEDULING PROCESS SHEDULING


Shortest Remaining Time
Process Arrival Burst Completion Turnaround Waiting • Shortest remaining time (SRT) is the preemptive version of the SJN
Time(AT) Time(BT) Time(CT) Time(TA) Time(WA) algorithm.
• The processor is allocated to the job closest to completion but it can
P0 0 5 5 5 0 be preempted by a newer ready job with shorter time to completion.
• Impossible to implement in interactive systems where required CPU
P1 1 3 11 10 7 time is not known.
• It is often used in batch environments where short jobs need to give
P2 2 8 14 12 4 preference.
Round Robin Scheduling
P3 3 6 22 19 13
• Round Robin is the preemptive process scheduling algorithm.
• Each process is provided a fix time to execute, it is called
a quantum.
Average Waiting Time = ( 0 + 7 + 4 + 13 ) / 4 = 24 / 4 = 6

119 120
Lecture - V BITS Pilani, Pilani Campus Lecture - V BITS Pilani, Pilani Campus
PROCESS SHEDULING PROCESS SHEDULING
• Once a process is executed for a given time period, it is preempted
and other process executes for a given time period. Process Arrival Burst Completion Waiting
• Context switching is used to save states of preempted processes. Time(AT) Time(BT) Time(CT) Time
The problem is solved with the quantum time 3 as follows:
Process Arrival Burst P0 0 5 14 0+(12-3) = 9
Time(AT) Time(BT)
P1 1 3 6 (3 - 1) = 2
P0 0 5
P1 1 3
P2 2 8 22 (6 – 2) + ( 14 – 9 ) +
P2 2 8 Solution: The Gantt Chart is (20 – 17 ) = 12
P3 3 6 drawn as follows:
P3 3 6 20 (9 – 3 ) + (17 - 12) = 11
P0 P1 P2 P3 P0 P2 P3 P2
Average Waiting Time = ( 9 +2 + 12 + 11 ) / 4 = 34 / 4 = 8.5
0 3 6 9 12 14 17 20 22
121 122
Lecture - V BITS Pilani, Pilani Campus Lecture - V BITS Pilani, Pilani Campus

PROCESS SHEDULING PROCESS SHEDULING


Multiple-Level Queues Scheduling
• Multiple-level queues are not an independent scheduling algorithm.
They make use of other existing algorithms to group and schedule System Processes
jobs with common characteristics. Highest Priority FCFS
• Multiple queues are maintained for processes with common
characteristics.
• Each queue can have its own scheduling algorithms.
Interactive Processes
• Priorities are assigned to each queue.
CPU
For example, CPU-bound jobs can be scheduled in one queue and
Medium Priority SJF
all I/O-bound jobs in another queue. The Process Scheduler then
alternately selects jobs from each queue and assigns them to the Batch Processes
CPU based on the algorithm assigned to the queue. Lowest Priority RR

123 124
Lecture - V BITS Pilani, Pilani Campus Lecture - V BITS Pilani, Pilani Campus

MULTIPLE-LEVEL QUEUES
Learning Objectives
SCHEDULING
Ø Multiple Queue Scheduling Advantages of Multiple Level Queues Scheduling:
• Multilevel Queue Scheduling has some advantages like:
- Advantages and Disadvantages • Multilevel queue scheduling helps us apply different
- Worked Example scheduling algorithms for different processes.
Ø Multiple Processor Scheduling • It will have a low scheduling overhead.
Disadvantages:
Ø Algorithm evaluation • Some of the disadvantages of Multilevel queue scheduling
- Deterministic Modeling are as follows:
- Queuing Modeling • There are chances of starving for the lower priority
processes.
- Simulations • It is inflexible in nature.
Ø Thread Scheduling
125 126
Lecture - VI BITS Pilani, Pilani Campus Lecture - VI BITS Pilani, Pilani Campus
MULTIPLE-LEVEL QUEUES MULTIPLE-LEVEL QUEUES
SCHEDULING SCHEDULING
Example of Multilevel queue scheduling: Priority of queue 1 is greater than queue 2. queue 1 uses
Consider below table of four processes under Multilevel Round Robin (Time Quantum = 2) and queue 2 uses FCFS.
queue scheduling. Queue number denotes the queue of the Below is the Gantt chart of the problem :
process.
P1 P2 P1 P2 P3 P4 P5
Process Arrival Time CPU Burst Time Queue Number
P1 0 4 1 0 2 4 6 7 10 15 20
P2 0 3 1 At starting both queues have process so process in queue 1
P3 0 8 2 (P1, P2) runs first (because of higher priority) in the round
robin fashion and completes after 7 units then process in
P4 10 5 queue 2 (P3) starts running (as there is no process in queue 1)
1
but while it is running P4 comes in queue 1 and interrupts P3
127 128
Lecture - VI BITS Pilani, Pilani Campus Lecture - VI BITS Pilani, Pilani Campus

MULTIPLE-LEVEL QUEUES MULTIPLE PROCESSOR


SCHEDULING SCHEDULING
and start running for 5 second and after its completion P3 takes the MULTIPLE PROCESSOR SCHEDULING
CPU and completes its execution. 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
129 the Master Server and the other processors executes only the 130
Lecture - VI BITS Pilani, Pilani Campus Lecture - VI BITS Pilani, Pilani Campus

MULTIPLE PROCESSOR
ALGORITHM EVALUATION
SCHEDULING
user code. This is simple and reduces the need of data sharing. This Selecting an algorithm from many scheduling algorithms is a
entire scenario is called Asymmetric Multiprocessing. difficult process. Defining the criteria to select the algorithm is
A second approach uses Symmetric Multiprocessing where each the first problem. Normally, criteria are often defined in terms
processor is self scheduling. All processes may be in a common
of CPU utilization, response time and throughput. The criteria
ready queue or each processor may have its own private queue for
may include the several measures such as
ready processes. The scheduling proceeds further by having the
• Maximize CPU utilization under the constraint that the
scheduler for each processor examine the ready queue and select a
maximum response time is 1 second.
process to execute.
• 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
131 132
Lecture - VI BITS Pilani, Pilani Campus : Lecture - VI BITS Pilani, Pilani Campus
ALGORITHM EVALUATION ALGORITHM EVALUATION
1. Deterministic Modeling Process Burst Time
2. Queuing Models P1 10
3. Simulations P2 29
Deterministic Modeling
P3 3
One major class of evaluation methods is called analytic evaluation.
P4 7
Analytic evaluation uses the given algorithm and the system
P5 12
workload to produce a formula or number that evaluates the
performance of the algorithm for that workload. Calculate the average waiting time for each method.
One type of analytic evaluation is deterministic modelling. For FCFS,
For example, assume that we have the workload for 5 processes is Average Waiting time = ( 0 + 10 + 39 + 42 + 49 ) / 5 = 28
given below milliseconds
For SJF,
Average Waiting time = ( 10 + 32 + 0 + 3 + 20 ) / 5 = 13
133
milliseconds 134
Lecture - VI BITS Pilani, Pilani Campus Lecture - VI BITS Pilani, Pilani Campus

ALGORITHM EVALUATION ALGORITHM EVALUATION


For RR( with quantum time), As an example, let ‘n’ be the average queue length ( excluding the
Average Waiting time = ( 0 + 32 + 30 + 23 + 40 ) / 5 = 23 milliseconds process being serviced), let W be the average waiting time in the
In this case, the SJF policy results in less than one – half of the queue and ƛ be average rate for new processes in the queue(such as
average time required for FCFS scheduling and Round Robin 3 processes per second). We expect that during the time W that a
Scheduling gives an intermediate value. process waits, ƛ X W new processes will arrive in the queue. If the
Deterministic modelling is simple as we know the exact numbers. system is in a steady state, then the number of processes leaving the
Queuing Models queue is equal to the number of processes that arrive.
The computer system is described as a network of server. Each So,
server has a queue of waiting processes. The CPU is a server with its n=ƛXW
ready queue and I/O system with its ready queue. Knowing arrival This equation is called as Little’s formula.
rates and service rates, we can determine utilization, average queue Simulations
length , average waiting time ans so on. This is called as queuing To get more accurate evaluation of scheduling algorithm, we use
network analysis. simulations.
135 136
Lecture - VI BITS Pilani, Pilani Campus Lecture - VI BITS Pilani, Pilani Campus

ALGORITHM EVALUATION THREAD SCHEDULING


Simulation imitates the operation of real world processes or systems Many computer configurations have a single CPU. Hence,
with the use of models. The model represents the key behaviours threads run one at a time in such a way as to provide an
and characteristics of the selected process or system while the illusion of concurrency. Execution of multiple threads on a
simulation represents how the model evolves under different
single CPU in some order is called scheduling. The Java
conditions over time.
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.
137 138
Lecture - VI BITS Pilani, Pilani Campus Lecture - VI BITS Pilani, Pilani Campus
THREAD SCHEDULING THREAD SCHEDULING

Thread priorities are integers ranging between MIN_PRIORITY • It yields, or its run method exits.
And MAX_PRIORITY (Constants defined in the Thread Class). • On systems that support time-slicing, its time allotment has
The higher the integer, the higher the priority. At any given expired.
time, when multiple threads are ready to be executed, the Then the second thread is given a chance to run, and so on,
runtime system chooses for execution the Runnable thread that until the interpreter exits.
has the highest priority. Only when that thread stops, yields, or The Java runtime system's thread scheduling algorithm is also
becomes Not Runnable will a lower-priority thread start preemptive. If at any time a thread with a higher priority than
executing. If two threads of the same priority are waiting for all other Runnable threads becomes Runnable, the runtime
the CPU, the scheduler arbitrarily chooses one of them to run. system chooses the new higher-priority thread for execution.
The chosen thread runs until one of the following conditions is The new thread is said to preempt the other threads.
true: http://www.btechsmartclass.com/java/java-threads-
• A higher priority thread becomes runnable. priority.html
139 140
Lecture - VI BITS Pilani, Pilani Campus Lecture - VI BITS Pilani, Pilani Campus

Learning Objectives PROCESS SYNCHRONIZATION

Ø What is Process Synchronization? Process Synchronization is the task of coordinating the


execution of processes in a way that no two processes can
Ø How does Synchronization work? have access to the same shared data and resources.
Ø Elements of Critical Section It is specially needed in a multi-process system when multiple
Ø General format of the Sections of a program processes are running together, and more than one processes
try to gain access to the same shared resource or data at the
Ø What is Critical Section Problem?
same time.
Ø Rules for Critical Section This can lead to the inconsistency of shared data. So the
Ø Solutions to the Critical section change made by one process not necessarily reflected when
Ø Synchronization Hardware other processes accessed the same shared data. To avoid this
type of inconsistency of data, the processes need to be
Ø Semaphore
synchronized with each other.
141 142
Lecture - VII BITS Pilani, Pilani Campus Lecture - VII BITS Pilani, Pilani Campus

PROCESS SYNCHRONIZATION PROCESS SYNCHRONIZATION

How Process Synchronization Works? Sections of a Program


For Example, process A changing the data in a memory Here, are four essential elements of the critical section:
location while another process B is trying to read the data from • Entry Section: It is part of the process which decides the
the same memory location. There is a high probability that entry of a particular process.
data read by the second process will be erroneous. • Critical Section: This part allows one process to enter and
modify the shared variable.
Write Read • Exit Section: Exit section allows the other process that are
waiting in the Entry Section, to enter into the Critical
Process 1 Data Process 2
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
MEMORY Remainder Section.
143 144
Lecture - VII BITS Pilani, Pilani Campus Lecture - VII BITS Pilani, Pilani Campus
PROCESS SYNCHRONIZATION PROCESS SYNCHRONIZATION
Do 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
Entry Section
consists of shared data resources that required to be accessed
by other processes.
Critical Section Code Segment
In the critical section, only a single process can be executed.
Exit Section Other processes, waiting to execute their critical section, need
to wait until the current process completes its execution.
Remainder Section Rules for Critical Section
The critical section need to must enforce all three rules:
(i) Mutual Exclusion: Mutual Exclusion is a special type of
} while(true); binary semaphore which is used for controlling access to the
145 shared resource. It includes a priority inheritance mechanism 146
Lecture - VII BITS Pilani, Pilani Campus Lecture - VII BITS Pilani, Pilani Campus

PROCESS SYNCHRONIZATION PROCESS SYNCHRONIZATION

to avoid extended priority inversion problems. Not more than Solutions To The Critical Section
one process can execute in its critical section at one time. In Process Synchronization, critical section plays the main role
(ii) Progress: This solution is used when no one is in the so that the problem must be solved.
Critical section, and someone wants in. Then those processes Here are some widely used methods to solve the critical
not in their reminder section should decide who should go in, section problem.
in a finite time. Solution 1:-
(iii) Bound Waiting: When a process makes a request for We declare an array : boolean flag[2];
getting into critical section, there is a specific limit about If Flag[i] is true then
number of processes can get into their critical section. So, Pi is ready to enter into the critical region.
when the limit is reached, the system must allow request to the The structure is :
process to get into its critical section. do
{
147 Flag[i]=true; 148
Lecture - VII BITS Pilani, Pilani Campus Lecture - VII BITS Pilani, Pilani Campus

PROCESS SYNCHRONIZATION PROCESS SYNCHRONIZATION

while(flag[j]) ; 2.Peterson Solution


Critical Section; Peterson's solution is widely used solution to critical section
flag[i]=false; problems. This algorithm was developed by a computer
remainder section; scientist Peterson that's why it is named as a Peterson's
} While(1); solution.
For Process Pi, sets Flag[i] to be true, signaling that it is ready In this solution, when a process is executing in a critical state,
to enter into its critical section. Then Pi checks whether it can then the other process only executes the rest of the code, and
enter into its critical section. If not, Pi waits till Pj complete its the opposite can happen. This method also helps to make sure
[Flag[j] becomes False]. Then Pi is allowed to enter its critical that only a single process runs in the critical section at a
section. On exiting the critical sectionm Pi sets Flag[i] to be specific time.
false. Example :
Mutual exclusion satisfied but Progress requirement is not
satisfied. 149 150
Lecture - VII BITS Pilani, Pilani Campus Lecture - VII BITS Pilani, Pilani Campus
PROCESS SYNCHRONIZATION PROCESS SYNCHRONIZATION

P1 P2 P3 …… Pn • Assume there are N processes (P1, P2, ... PN) and every
Flag[1]=False Flag[2]=False Flag[3]=False … Flag[n]=False process at some point of time requires to enter the Critical
Do Section
{ • A FLAG[] array of size N is maintained which is by default
false. So, whenever a process requires to enter the critical
Flag[j]=true; section, it has to set its flag as true. For example, If Pi wants
turn=j; to enter it will set FLAG[i]=TRUE.
while (Flag[j] && turn == j) ; • Another variable called TURN indicates the process number
Critical Section which is currently wafting to enter into the CS.
Flag[i]=False; • The process which enters into the critical section while
Remainder Section exiting would change the TURN to another number from the
} While (1); 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.
151 152
Lecture - VII BITS Pilani, Pilani Campus Lecture - VII BITS Pilani, Pilani Campus

PROCESS SYNCHRONIZATION PROCESS SYNCHRONIZATION

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


Some times the problems of the Critical Section are also obtained over the critical resources used inside the critical
resolved by hardware. Some operating system offers a lock section. In the exit section that lock is released.
functionality where a Process acquires a lock when entering SEMAPHORE
the Critical section and releases the lock after leaving it.So Semaphore is simply a variable that is non-negative and
when another process is trying to enter the critical section, it shared between threads. It is another algorithm or solution to
will not be able to enter as it is locked. It can only do so if it is the critical section problem. It is a signaling mechanism and a
free by acquiring the lock itself. thread that is waiting on a semaphore, which can be signaled
Mutex Locks by another thread.
Synchronization hardware not simple method to implement for Semaphore is also used for process synchronization. Assume
everyone, so strict software method known as Mutex Locks was that multiple processes want to enter into critical section.
also introduced. Semaphore Is the synchronization tool with the help of a
153 variable to access the critical section in mutually exclusive 154
Lecture - VII BITS Pilani, Pilani Campus Lecture - VII BITS Pilani, Pilani Campus

PROCESS SYNCHRONIZATION PROCESS SYNCHRONIZATION

way. It ensures that only one process is permitted to enter into Repeat
the critical section. To do this, semaphore uses two atomic Wait(mutex) Entry section
functions :wait ( ) and signal(). Critical Section
The code for the functions are: Signal(mutex) Exit section
Wait(S) : while S ≤ 0 do no – operation [ Not permitted inside Remainder section
the critical section] Until (True);
S=S-1; It specifies that any process is allowed to enter into critical
Signal(S) : S=S+1; section if mutex is 1. Before executing any process, assume
It uses two atomic operations, 1)wait, and 2) signal for the that mutex=1
process synchronization. When P1 wants to enter into the critical section but P0 is
Let mutex is the short form of mutual exclusion variable. Then already in the critical section, the value of mutex is 0. So it can
the critical section code can be written as not enter into the critical section. So it has to wait till mutex is
155 changed to 1. Mutex is changed to 1 only when the running 156
Lecture - VII BITS Pilani, Pilani Campus Lecture - VII BITS Pilani, Pilani Campus
PROCESS SYNCHRONIZATION Learning Objectives
process P0 has exited the critical section and execute the Ø Classical problem 1- Bounded Buffer problem / Producer
Signal() function to make the value 0 mutex as 1 [Signal and Consumer problem
function increments the value of mutex by 1]. - Definition
CLASSICAL SYNCHRONIZATION PROBLEMS - How the problem is synchronized with Semaphore?
The following are the classical examples of for - Structures of Producer and Consumer
synchronization problems. Ø Classical problem 2 – Dining and Philosopher problem
1. Producer and Consumer problem
- Definition
2. Reader and Writer Problem
- Solving the problem with Semaphore
3. Diner – Philosopher Problem
Ø What are monitors?
Ø Solving Dining and Philosopher problem using monitors

157 158
Lecture - VII BITS Pilani, Pilani Campus Lecture - VIII BITS Pilani, Pilani Campus

PROCESS SYNCHRONIZATION-
PROCESS SYNCHRONIZATION
Bounded Buffer Problem
- This problem is also called as producer – consumer problem Solution to the problem is ,
- It is problem based on synchronization - Create two counting semaphores “Full” and “Empty” to track
- There are two enteritis such as Producer and Consumer whether the buffer/ container is full or empty
- Producer produces a product and it should be placed on the - When the container is full, the producer can not place the product
container [buffer] in it
- Consumer consumes the product that is available in the container - When the container is empty, the consumer can not take any
- The container should be used by only one of them , either the product to consume
producer or the consumer The procedure for the problem is written as follows:
1 2 3 4 ……… N Structure of Producer
While (True)
Product Consumer {
wait(empty);
wait(mutex);
Add Item into the buffer
159 160
Lecture - VIII BITS Pilani, Pilani Campus Lecture - VIII BITS Pilani, Pilani Campus

PROCESS SYNCHRONIZATION PROCESS SYNCHRONIZATION


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

161 162
Lecture - VIII BITS Pilani, Pilani Campus Lecture - VIII BITS Pilani, Pilani Campus
PROCESS SYNCHRONIZATION PROCESS SYNCHRONIZATION
while (true ) // Initially true Now if Consumer wants to consume the product, the code is
waif(empty) -> wait(N); Here N is not <=0, so permits the executed as follows:
operation, N = N - 1 while(true) – Condition is true
wait(full) -> wait(1) : Here 1 is not <=0, so permits the
wait(mutext) -> wait(1): Here 1 is not <=0, so permits the operation, full= full-1 =0
operation, mutex = mutex-1 =0 wait(mutex) -> wait(1) : Here 1 is not <=0, so permits
Add Item to the Buffer / Container the operation, mutex= mutex-1 =0
signal(mutex) -> Increments mutex by 1. Now mutex = 1 Removes the Product from the buffer
signal(full) -> Increments full by 1. Full = 1 Signal(mutex) -> mutext = mutex + 1 = 1
Now the Buffer has a single product as Signal(empty) -> empty = empty + 1 = N – 1 + 1= N

P Empty = n - 1
Full = 1

163 164
Lecture - VIII BITS Pilani, Pilani Campus Lecture - VIII BITS Pilani, Pilani Campus

PROCESS SYNCHRONIZATION –
PROCESS SYNCHRONIZATION
Dining Philosopher Problem
What is Dining Philosopher Problem? Semaphore. The structure of the chopstick is
It states that there are 5 philosophers (may be more than 5 also) semaphore Chopstick [5];
sharing a circular table and they eat and think alternatively. Thre is a Initially the elements of the chopstick are initialized to as they are on
bowl of rice for each of the philosophers and 5 chopsticks. A the table and picked by any philosopher.
philosopher needs both their right and left chopstick to eat. The Structure of random philosopher i to eat is given as follows:
A hungry philosopher only eat if there are both the chopsticks While (True)
available. Otherwise the philosopher puts down their chopstick and {
begin thinking again. wait(Chopstick[i] );
It is a classical synchronization problem as it demonstrates a large wait(Chopstick[ (i+1) % 5 );
class of concurrency control problem. Eating Rice ;
Solution: Signal(Chopstick(i]);
The Solution for this problem is to use a semaphore to represent a Signal(Chopstick[( i + 1 ) % 5 );
chopstick. A chopstick can be picked up by executing a wait }
operation on the semaphore and released by executing a signal
165 166
Lecture - VIII BITS Pilani, Pilani Campus Lecture - VIII BITS Pilani, Pilani Campus

PROCESS SYNCHRONIZATION PROCESS SYNCHRONIZATION


Working out the procedure: The Philosopher can eat
Initially, all the chopsticks [i] = 1 , i = 0 to 4 [When chopstick[i]=1, it Signal(Chopstick[i]) – Signal(Chopstick[2])
is free that is un used now]. Chopstick[2]=Chopstick[2]+1 =1
Now Philosopher [2] wants to eat. Then the procedure is executed as Signal (Chopstick[(i+1) % 5 ])- Signal(Chopstick[2])
follows: Chopstick[2]=Chopstick[2]+1 =1
while ( true ) // Initially True Now both the chopsticks are available.
{ Note:- When Philosopher[2] was eating (Chopstick[2]=0 and
wait(chopstick[i])->wait(chopstick [ 2 ] ) – wait[1] Chopstick[3]=0), if philosopher[1] also wants to eat ,
- Here 1 is not <=0, so permits the operation, Executing the same procedure, we have
Chopstick[2]= Chopstick[2]-1=1-1=0 While (True)
wait(chopstick[(i+1)%5])->wait(chopstick [ 3 ] ) – wait[1] {
- Here 1 is not <=0, so permits the operation, wait(chopstick[1]) -> wait(1); No problem Chopstick is available
Chopstick[3]= Chopstick[3]-1=1-1=0 wait(chopstick[(i+1)%5]->wait(chopstick[2]); Stopped as
Chopstick[2]=0;
167 168
Lecture - VIII BITS Pilani, Pilani Campus Lecture - VIII BITS Pilani, Pilani Campus
PROCESS SYNCHRONIZATION PROCESS SYNCHRONIZATION
Monitors monitor monitor_name
- Are also used for synchronization {
- High level abstraction that provides and effective mechanism for Shared variable declaration;
process synchronization procedure P1()
- 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 procedure P2()
- 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 }
169 170
Lecture - VIII BITS Pilani, Pilani Campus Lecture - VIII BITS Pilani, Pilani Campus

PROCESS SYNCHRONIZATION PROCESS SYNCHRONIZATION


procedure P3() 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
initialization Code() 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
} {
} // End of the Monitor enum { THINKING,HUNGRY,EATING } State [5];
condition self[5];
void PickUp(int i)
{
171 172
Lecture - VIII BITS Pilani, Pilani Campus Lecture - VIII BITS Pilani, Pilani Campus

PROCESS SYNCHRONIZATION PROCESS SYNCHRONIZATION


state[i]=HUNGRY; if((state[(i+4)%5] != EATING ) &&
test(i); (state[i]= = HUNGRY ) &&
if (state[i]!=EATING) self[i].wait(); (state[ (i+1) % 5 ] != EATING ) )
} {
void putdown(int i) state[i]=EATING;
{ self[i].signal();
state[i]=THINKING; }
// Test the left and right neighbors }
test((i+4)%5); Initialization_Code()
test((i+1)%5); {
} for (i=0;i<5;i++)
void test(int i) state[i]=THINKING;
{ }
}
173 174
Lecture - VIII BITS Pilani, Pilani Campus Lecture - VIII BITS Pilani, Pilani Campus
Learning Objectives DEADLOCK

Ø What is Deadlock? Deadlock


In an operating system, there are many processes (processes
ØExample for Deadlock are programs that are currently being executed) running
ØNecessary conditions for deadlock continuously. All of these processes are important for the
functioning of the computer. It defines the basic unit of work that
ØDeadlock handling mechanisms has to be implemented by the system. Processes follow
- Deadlock Prevention sequential execution.
These processes require some resources to run or finish their
- Deadlock Avoidance execution. Sequential order is followed here. The process first
- Deadlock Prevention and Recovery 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
175 waiting and releasing, it means that there is a deadlock situation.176
Lecture - IX BITS Pilani, Pilani Campus Lecture - IX BITS Pilani, Pilani Campus

DEADLOCK DEADLOCK

What is Deadlock? able to complete their execution. As the processes are blocked,
A deadlock is a common situation in operating systems where a the system may become unresponsive.
process waiting for a resource can be executed because that So deadlock is the situation in which multiple processes are
resource is currently held by another process and is being waiting infinitely in which there is no progress for waiting
utilised for its execution, therefore, the process does not get processes.
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 177 178
Lecture - IX BITS Pilani, Pilani Campus Lecture - IX BITS Pilani, Pilani Campus

DEADLOCK 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.

179 180
Lecture - IX BITS Pilani, Pilani Campus Lecture - IX BITS Pilani, Pilani Campus
DEADLOCK DEADLOCK
Necessary conditions for deadlock 4. Circular wait
1. Mutual exclusion If the processes are waiting in the circular fashion such as the
The processes are permitted to use the shared resource in following diagram, deadlock may occur.
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.
181 182
Lecture - IX BITS Pilani, Pilani Campus Lecture - IX BITS Pilani, Pilani Campus

DEADLOCK DEADLOCK

If all the above conditions are satisfied, deadlock may occur. Deadlock Prevention procedure removes hold and wait condition
Deadlock Handling Mechanism by not permitting any process to waits for any resource if it holds
Deadlock detection, deadlock prevention and deadlock another process already. It is done in any of the following
avoidance are the main methods for handling deadlocks. procedure:
– Deadlock Prevention • A process must acquire all the necessary resources before
– Deadlock avoidance entire system starts. It reduces the utilization of the resources
– Deadlock Detection & Recovery • A process that holds some resources requests for other
Deadlock Prevention resources is asked to release the acquired resource first.
Starvation is the problem of this method
In this method, we have to include the preventive mechanism to
• Apply wait time out procedure. In this procedure, a process
stop the deadlock to occur. In this procedure, any one of the that holds some resources is asked to release the acquired
necessary condition mentioned above should be made as False. resource for a particular period of time though the resources
Deadlock prevention procedure removes mutual exclusion by may be requested or not requested by another process.
making all the resources sharable 183 184
Lecture - IX BITS Pilani, Pilani Campus Lecture - IX BITS Pilani, Pilani Campus

DEADLOCK DEADLOCK

Deadlock prevention procedure pre-empts by removing a the Resource Rk where [k>j].


process forcefully. It removes the waiting process and not the To request a resource Rj, the process must release all Ri such
running process. When a process requests a resource, any one that i>=j.
of the following cases is selected:
Here P1 holds R2, it can’t hold R1 because R2 > R1.
Case 1 : If the resource is available [not used / not held by
Example:
another process], the resource is allocated to the process
Assume that there are three resources such as Printer, CPU and
Case 2: If the resource is unavailable [used / held by another
Memory. The integer numbers assigned to them are as follows:
process], the resource is released from the holding process
Printer 1
which waits for another resource.
CPU 5
Deadlock prevention procedure removes the circular wait
Memory 6
condition by using following procedure:
Now a process P1 holds Printer and Memory [integer numbers 1
Assign the integer to the resources available.
and 6 respectively]. If it requests for the resource > 6, there will
The process Pi which holds Rj is not permitted to request for
185 be no problem. But if it requests CPU [integer number 5], it has 186
Lecture - IX BITS Pilani, Pilani Campus Lecture - IX BITS Pilani, Pilani Campus
DEADLOCK DEADLOCK

to release Memory and then it can hold CPU. The deadlock Avoidance method is used by the operating
DEADLOCK AVOIDANCE system in order to check whether the system is in a safe state or
In deadlock avoidance, the resource for any process is granted if in an unsafe state and in order to avoid the deadlocks, the
the resulting state of the system does not cause any deadlock in process must need to tell the operating system about the
the system. The state of the system is continuously checked for maximum number of resources a process can request in order to
Safe and Unsafe states. complete its execution.
In order to avoid deadlock, the process must tell OS , the How does Deadlock Avoidance work?
maximum number of resources a process can request to In this method, the request for any resource will be granted only
complete its execution. The deadlock avoidance algorithm if the resulting state of the system doesn't cause any deadlock in
examines the resource allocations so that there can never be a the system. This method checks every step performed by the
circular wait condition operating system. Any process continues its execution until the
system is in a safe state. Once the system enters into an unsafe
187 state, the operating system has to take a step back. 188
Lecture - IX BITS Pilani, Pilani Campus Lecture - IX BITS Pilani, Pilani Campus

DEADLOCK DEADLOCK

With the help of a deadlock-avoidance algorithm, you can deadlocked state and conversely a deadlocked state is an
dynamically assess the resource-allocation state so that there unsafe state.
can never be a circular-wait situation. In an Unsafe state, the operating system cannot prevent
According to the simplest and useful approach, any process processes from requesting resources in such a way that any
should declare the maximum number of resources of each type it deadlock occurs. It is not necessary that all unsafe states are
will need. The algorithms of deadlock avoidance mainly examine deadlocks; an unsafe state may lead to a deadlock
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 The above Figure shows the Safe, unsafe, and deadlocked state
there exists a safe sequence. So a safe state is not a 189
spaces 190
Lecture - IX BITS Pilani, Pilani Campus Lecture - IX BITS Pilani, Pilani Campus

DEADLOCK DEADLOCK
Deadlock Avoidance Example So at time t0, the system is in a safe state. The sequence is
Let us consider a system having 12 magnetic tapes and three <P2,P1,P3> satisfies the safety condition. Process P2 can
processes P1, P2, P3. Process P1 requires 10 magnetic tapes, immediately be allocated all its tape drives and then return them.
process P2 may need as many as 4 tapes, process P3 may After the return the system will have 5 available tapes, then
need up to 9 tapes. Suppose at a time to, process P1 is holding process P1 can get all its tapes and return them ( the system will
5 tapes, process P2 is holding 2 tapes and process P3 is then have 10 tapes); finally, process P3 can get all its tapes and
holding 2 tapes. (There are 3 free magnetic tapes) return them (The system will then have 12 available tapes).
Processes Maximum Needs Current Needs
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.
P1 10 5
The system is no longer in a safe state. At this point, only
P2 4 2 process P2 can be allocated all its tapes. When it returns them
P3 9 2 the system will then have only 4 available tapes. Since P1 is
191 allocated five tapes but has a maximum of ten so it may request 192
Lecture - IX BITS Pilani, Pilani Campus Lecture - IX BITS Pilani, Pilani Campus
DEADLOCK DEADLOCK

5 more tapes. If it does so, it will have to wait because they are Deadlock Avoidance Solution
unavailable. Similarly, process P3 may request its additional 6 Deadlock Avoidance can be solved by two different algorithms:
tapes and have to wait which then results in a deadlock. 1. Resource allocation Graph
The mistake was granting the request from P3 for one more 2. Banker's Algorithm
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.
193 194
Lecture - IX BITS Pilani, Pilani Campus Lecture - IX BITS Pilani, Pilani Campus

Learning Objectives DEADLOCK AVOIDANCE

Ø Dead lock avoidance In deadlock avoidance, the resource for any process is granted if
the resulting state of the system does not cause any deadlock in
- Safe and Unsafe states
the system. The state of the system is continuously checked for
- Resource allocation graph
Safe and Unsafe states.
- Banker’s Algorithm
In order to avoid deadlock, the process must tell OS , the
ØDeadlock Detection and Recovery maximum number of resources a process can request to
- Detection complete its execution. The deadlock avoidance algorithm
(i) Wait for Graph examines the resource allocations so that there can never be a
(ii) Banker’s Algorithm Circular wait condition
- Recovery How does Deadlock avoidance work?
(i) Process Termination Let's understand the working of Deadlock Avoidance with the
(ii) Resource Termination/ Pre-emption help of an intuitive example.
195 196
Lecture - X BITS Pilani, Pilani Campus Lecture - X BITS Pilani, Pilani Campus

DEADLOCK AVOIDANCE DEADLOCK AVOIDANCE

Process Maximum Required current Available • P2 process needs a maximum of 5 resources and is currently
allocated with 2 resources. So it needs 3 more resources to
P1 9 5 complete its execution.
• P3 process needs a maximum of 3 resources and is currently
P2 5 2
allocated with 1 resource. So it needs 2 more resources to
P3 3 1
complete its execution.
Operating System knows that only 2 resources out of the total
Let us assume that there are three processes P1, P2, P3. Some available resources are currently free.
more information on which the processes tells the Operating But only 2 resources are free now. Can P1, P2, and P3 satisfy their
System are : requirements? Let's try to find out.
• P1 process needs a maximum of 9 resources (Resources like As only 2 resources are free for now, then only P3 can satisfy its
tape drive or printer etc..) to complete its execution. P1 is need for 2 resources. If P3 takes 2 resources and completes its
currently allocated with 5 Resources and needs 4 more to execution, then P3 can release its 3 (1+2) resources. Now the
complete its execution.
197 three free resources which P3 released can satisfy the need 198
Lecture - X BITS Pilani, Pilani Campus Lecture - X BITS Pilani, Pilani Campus
DEADLOCK AVOIDANCE DEADLOCK AVOIDANCE

of P2. Now, P2 after taking the three free resources, can Safe State and Unsafe State
complete its execution and then release 5 (2+3) resources. Now Safe State - In the above example, we saw that Operating
five resources are free. P1 can now take 4 out of the 5 free System was able to satisfy the need of all three
resources and complete its execution. So, with 2 free resources processes, P1, P2, and P3, with their resource requirements. So
available initially, all the processes were able to complete their all the processes were able to complete their execution in a
execution leading to Safe State. The order of execution of the certain order like P3->P2->P1.
processes was <P3, P2, P1>. So, If Operating System is able to allocate or satisfy the
What if initially there was only 1 free resource available? None of maximum resource requirements of all the processes in any
the processes would be able to complete its execution. Thus order then the system is said to be in Safe State.
leading to an unsafe state. So safe state does not lead to Deadlock.
We use two words, safe and unsafe states. What are those Unsafe State - If Operating System is not able to prevent
states? Let's understand these concepts. Processes from requesting resources which can also lead to
199 Deadlock, then the System is said to be in an Unsafe State. 200
Lecture - X BITS Pilani, Pilani Campus Lecture - X BITS Pilani, Pilani Campus

DEADLOCK AVOIDANCE DEADLOCK AVOIDANCE

Unsafe State does not necessarily cause deadlock it may or in the System just by observing the Graph, which can not be
may not causes deadlock. done easily by using tables that we use in Banker's algorithm.
Deadlock Avoidance Solution Resource Allocation Graph has a process vertex represented by
Deadlock Avoidance can be solved by two different algorithms: a circle and a resource vertex represented by a box. The
• Resource allocation Graph instance of the resources is represented by a dot inside the box.
• Banker's Algorithm The instance can be single or multiple instances of the resource.
We will discuss both algorithms in detail in their separate article. An example of RAG is shown below.
Resource Allocation Graph Banker's Algorithm
Resource Allocation Graph (RAG) is used to represent the state Banker's algorithm does the same as we explained the Deadlock
of the System in the form of a Graph. The Graph contains all avoidance with the help of an example. The algorithm
processes and resources which are allocated to them and also predetermines whether the System will be in a safe state or not
the requesting resources of every Process. Sometimes if the by simulating the allocation of the resources to the processes
number of processes is less, We can easily identify a deadlock 201 according to the maximum available resources. 202
Lecture - X BITS Pilani, Pilani Campus Lecture - X BITS Pilani, Pilani Campus

DEADLOCK AVOIDANCE DEADLOCK AVOIDANCE

It makes an "s-state" check before actually allocating the A process must specify in the beginning the maximum number of
resources to the Processes. instances of each resource type it may require. It is obvious that
When there are more number of Processes and many this number should not be more than the available. When the
Resources, then Banker's Algorithm is useful. process requests resources, the system decides whether
It is used to avoid deadlocks when multiple instances of each allocation will result in deadlock or not. If not, resources are
resource type are present. This is not possible, using the allocated otherwise process has to wait.
methods like safe state and resource allocation graphs. It is The following are the various data structures that have to be
similar to a banking system where a bank never allocates cash created to implement Banker's algorithm. If 'n' is the number of
in such a way that it could not satisfy the needs of all its processes and 'm' is the number of resources.
customers and also it cannot allocate more than what is Max : A 'n × m' matrix indicating the maximum resources
available. Here, customers are analogous to processes, cash to required by each process.
resources, and bank to the operating system. Allocation : A 'n × m' matrix indicating the number of resources
already allocated to each process.
203 204
Lecture - X BITS Pilani, Pilani Campus Lecture - X BITS Pilani, Pilani Campus
DEADLOCK AVOIDANCE DEADLOCK AVOIDANCE

Need : A 'n × m' matrix indicates the number of resources Step 2 - If Requesti ≤ Available, then proceed to step three,
required by each process. otherwise block Pi because resources are not available.
Available : It is a vector of size 'm' which indicates the resources Step 3 - Allocate resources to Pi as follows,
that are still available (not allocated to any process). Available = Available - Requesti
Request : It is a vector of size 'm' which indicates that process Allocationi = Allocationi + Requesti
Pi has requested some resources. Needi = Needi - Requesti
Each row of matrices "allocation" and "need" can be referred to Safety Algorithm :
as vectors. Then "allocation" indicates the resources currently The job of the banker's algorithm is to perform allocation, it will
allocated to process Pi and "need" refers to resources required not see whether this allocation has resulted in a safe or unsafe
by Pi. The following algorithm is used to determine whether the state. It is the safety algorithm that is called immediately after the
request can be safely granted or not. banker's algorithm to check for the system state after allocation.
Step 1 - If Requesti ≤ Needi, then proceed to step two, otherwise The following is the safety algorithm that requires m x
raise an exception saying the process has exceeded its
maximum claim. 205
n2 operations to find the system state. 206
Lecture - X BITS Pilani, Pilani Campus Lecture - X BITS Pilani, Pilani Campus

DEADLOCK AVOIDANCE DEADLOCK AVOIDANCE

Step 1 - Assume work and finish as vectors of length 'm' and 'n' Example of Banker’s Algorithm
respectively. Consider the following problem:
Work = Available
Allocation Max Available
Finish[i] = 'false' Processes
A B C A B C A B C

Step 2 - Find 'i' such that,


Finish[i] = 'false' P0 1 1 2 4 3 3 2 1 0

Needi ≤ Work
P1 2 1 2 3 2 2
If no such 'i' is found jump to step four.
Step 3 - Work = Work + Allocation P2 4 0 1 9 0 2

Finish[i] = 'true'
Jump to step two. P3 0 2 0 7 5 3

Step 4 - If finish[i] = True for all then the system is in a safe


P4 1 1 2 1 1 2
state.
207 208
Lecture - X BITS Pilani, Pilani Campus Lecture - X BITS Pilani, Pilani Campus

DEADLOCK AVOIDANCE DEADLOCK AVOIDANCE

Do the following: Safe sequence:


(i) Calculate the content of the need matrix? For process P0, Need = (3, 2, 1) and
(ii) Is the system in a safe state? Available = (2, 1, 0)
(iii) Determine the total amount of resources of each type? Need ? Available = False
Solution: So, the system will move for the next process.
1. Content of the need matrix can be calculated by using the 2. For Process P1, Need = (1, 1, 0)
below formula Available = (2, 1, 0)
Need = Max – Allocation Need ? Available = True
Request of P1 is granted.
Available = Available +Allocation
= (2, 1, 0) + (2, 1, 2)
= (4, 2, 2) (New Available)
209 210
Lecture - X BITS Pilani, Pilani Campus Lecture - X BITS Pilani, Pilani Campus
DEADLOCK DETECTION &
DEADLOCK AVOIDANCE
RECOVERY
3. For Process P2, Need = (5, 0, 1) = (4, 2, 2) + (1, 1, 2)
Available = (4, 2, 2) = (5, 3, 4) now, (New Available)
Need ? Available = False 6. Now again check for Process P2, Need = (5, 0, 1)
So, the system will move to the next process. Available = (5, 3, 4)
4. For Process P3, Need = (7, 3, 3) Need ? Available = True
Available = (4, 2, 2) Request of P2 is granted.
Need ? Available = False ? Available = Available + Allocation
So, the system will move to the next process. = (5, 3, 4) + (4, 0, 1)
5. For Process P4, Need = (0, 0, 0) = (9, 3, 5) now, (New
Available = (4, 2, 2) Available)
Need ? Available = True 7. Now again check for Process P3, Need = (7, 3, 3)
Request of P4 is granted. Available = (9, 3, 5)
? Available = Available + Allocation 211
Need ? Available = True 212
Lecture - X BITS Pilani, Pilani Campus Lecture - X BITS Pilani, Pilani Campus

DEADLOCK DETECTION &


DEADLOCK AVOIDANCE RECOVERY
Request of P3 is granted. If a system does not employ either a deadlock-prevention or
Available = Available +Allocation deadlock-avoidance algorithm, then there are chances of
= (9, 3, 5) + (0, 2, 0) = (9, 5, 5) occurrence of a deadlock. In this case, the system may provide
8. Now again check for Process P0, = Need (3, 2, 1) two things: An algorithm is used to examines the state of the
= Available (9, 5, 5) system in order to determine whether a deadlock has
Need ? Available = True occurred. An algorithm that is used to recover from the
So, the request will be granted to P0. deadlock. Thus order to get rid of deadlocks the operating
Safe sequence: < P1, P4, P2, P3, P0> system periodically checks the system for any deadlock. After
The system allocates all the needed resources to each Finding the deadlock the operating system will recover from it
process. So, we can say that system is in a safe state. using recovery techniques.
3. The total amount of resources = sum of columns of allocation Now, the main task of the operating system is to detect the
+ Available = [8 5 7] + [2 1 0] = [10 6 7] deadlocks and this is done with the help of Resource Allocation
213 Graph. 214
Lecture - X BITS Pilani, Pilani Campus Lecture - X BITS Pilani, Pilani Campus

DEADLOCK DETECTION & DEADLOCK DETECTION &


RECOVERY 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.
215 216
Lecture - X BITS Pilani, Pilani Campus Lecture - X BITS Pilani, Pilani Campus
DEADLOCK DETECTION & DEADLOCK DETECTION &
RECOVERY RECOVERY
A deadlock exists in the system if and only if there is a cycle in This algorithm mainly uses several time-varying data structures
the wait-for graph. In order to detect the deadlock, the system that are similar to those used in Banker's Algorithm.
needs to maintain the wait-for graph and periodically system Recovery From Deadlock
invokes an algorithm that searches for the cycle in the wait- When a detection algorithm determines that a deadlock exists
for graph. then there are several available alternatives. There one
The algorithm that is used to detect the cycle in the graph mainly possibility and that is to inform the operator about the deadlock
requires n² operations; where n indicates the number of vertices and let him deal with this problem manually.
in the graph. Another possibility is to let the system recover from the deadlock
Multiple Instances of Each Resource Type automatically. These are two options that are mainly used to
The above scheme that is a wait-for graph is not applicable to break the deadlock.
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. 217 218
Lecture - X BITS Pilani, Pilani Campus Lecture - X BITS Pilani, Pilani Campus

DEADLOCK DETECTION & DEADLOCK DETECTION &


RECOVERY 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
219 deadlock cycle This method can be used but we have to decide220
Lecture - X BITS Pilani, Pilani Campus Lecture - X BITS Pilani, Pilani Campus

DEADLOCK DETECTION & DEADLOCK DETECTION &


RECOVERY RECOVERY
which process to kill and this method incurs considerable The issues in the pre emption are :
overhead. The process that has done the least amount of work is (i) Selecting a victim – Which process is to be selected which
killed by the Operating system firstly. may lead to starvation
Resource Pre-emption (ii) Rollback – We have to rollback to the beginning of the
In order to eliminate the deadlock by using resource preemption, process
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.

221 222
Lecture - X BITS Pilani, Pilani Campus Lecture - X BITS Pilani, Pilani Campus
Learning Objectives MEMORY MANAGEMENT

Ø Swapping SWAPPING
We know that all the processes are executed in the memory. A
Ø Contiguous Memory allocation
process can be swapped temporarily out of memory to a backing
- Fixed Size partition
- Variable size partition
store and then brought back into memory for continued
- Block allocation list execution.
- Bit Map For example, assume that a multiprogramming environment with
a round-robin CPU algorithm. When a quantum expires, the
Ø Non Contiguous Memory allocation
- Segmentation
memory manager will swap out the process that has just
- Paging reached the quantum time and to swap in another process to the
- Segmentation with Paging 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
223 done between swaps. 224
Lecture - XI BITS Pilani, Pilani Campus Lecture - XI BITS Pilani, Pilani Campus

MEMORY MANAGEMENT MEMORY MANAGEMENT

It is depicted in the following diagram: Backing Store 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
Operating priority process so that higher priority process is executed. Once
System Process the higher priority process finishes its execution, the lower
1. Swapped out P1 priority process is swapped back in and continued. This variant
swapping is sometimes is called as Roll out, Roll in.
2. Swapped In Process Normally a process that is swapped out will be swapped back
P2 into the same memory space that was occupied previously. This
restricted is dictated by the method of address binding.
User Space Swapping requires backing store. It is commonly a fast disk. The
disk must have enough space to accommodate swapped out
225 process images for all the users. There are two alternatives to 226
Lecture - XI BITS Pilani, Pilani Campus Lecture - XI BITS Pilani, Pilani Campus

MEMORY MANAGEMENT MEMORY MANAGEMENT

keep the swapped out process images on disk. CONTIGUOUS MEMORY ALLOCATION
The first alternative is to create a separate swap file for each The main memory has to accommodate both the operating
swapped out process. But this method increases the number of system and user space. Now, here the user space has to
files and directory entries. It creates increased overhead that accommodate various user processes. We also want these
requires high search time. several user processes must reside in the main memory at the
The second alternative is to create a common swap file that is same time.
kept in the disk and the location of each swapped out process Contiguous memory allocation is a memory allocation method
image has to be noted in the common swap disk. that allocates a single contiguous section of memory to a
The major part of the swap time is transfer time. The total process or a file.
transfer time is directly proportional to the amount of memory In Contiguous memory allocation, when the process arrives from
swapped. the ready queue to the main memory for execution, the
contiguous memory blocks are allocated to the process
227 according to its requirement. Now, to allocate the contiguous 228
Lecture - XI BITS Pilani, Pilani Campus Lecture - XI BITS Pilani, Pilani Campus
MEMORY MANAGEMENT MEMORY MANAGEMENT

space to user processes, the memory can be divided either in Fixed-Sized Partition: In the fixed-sized partition, the memory is
the fixed-sized partition or in the variable-sized partition. 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
229 processes arrive in the memory, executes, terminates and 230
Lecture - XI BITS Pilani, Pilani Campus Lecture - XI BITS Pilani, Pilani Campus

MEMORY MANAGEMENT MEMORY MANAGEMENT

leaves the memory we will see the set of holes of variable sizes. does not have a memory block (hole) that is large enough to
In the figure above, we can see that when file A and file C hold that process.
release the memory allocated to them, creates the holes in the If the memory block (hole) is too large for the process it
memory of variable size. gets spilt into two parts. One part of the memory block is
In the variable size partition method, the operating system allocated to the arrived process and the other part is returned to
analyses the memory requirement of the process and see the set of holes. When a process terminates and releases the
whether it has a memory block of the required size. memory allocated to it, the released memory is then placed back
If it finds the match, then it allocates that memory block to the to the set of holes. The two holes that are adjacent to each
process. If not, then it searches the ready queue for the process other, in the set of holes, are merged to form one large hole.
that has a smaller memory requirement. The operating system uses either the block allocation list or the
The operating system allocates the memory to the process until bit map to select the hole from the set of holes.
it cannot satisfy the memory requirement of the next process in
the ready queue. It stops allocating memory to the process if it 231 232
Lecture - XI BITS Pilani, Pilani Campus Lecture - XI BITS Pilani, Pilani Campus

MEMORY MANAGEMENT MEMORY MANAGEMENT

1. Block Allocation List Best-fit


Block allocation list maintains two tables. One table contains the This method needs the list of free holes to be sorted according to
entries of the blocks that are allocated to the various files. The their size. Then the smallest hole that is large enough for the
other table contains the entries of the holes that are free and can process to accommodate is selected from the list of free holes.
be allocated to the process in the waiting queue. This strategy reduces the wastage of memory as it does not
Now, as we have entries of free blocks which one must be allocate a hole of larger size which leaves some amount of
chosen can be decided using either of these strategies: first-fit, memory even after the process accommodates the space.
best-fit, worst-fit strategies. Worst-fit
First-fit This method requires the entire list of free holes to be sorted.
Here, the searching starts either at the beginning of the table or Here, again the largest hole among the free holes is selected.
from where the previous first-fit search has ended. While This strategy leaves the largest leftover hole which may be
searching, the first hole that is found to be large enough for a useful for the other process.
process to accommodate is selected. 233 234
Lecture - XI BITS Pilani, Pilani Campus Lecture - XI BITS Pilani, Pilani Campus
MEMORY MANAGEMENT MEMORY MANAGEMENT

2. Bit Map It does not have entries of the files or processes to which the
The bit map method only keeps track of the free or allocated specific blocks are allocated. Normally, implementing the first fit
block. One block is represented by one bit, bit 0 resembles the will search the number of consecutive zeros/free blocks required
free block and bit 1 resembles that the block is allocated to a file by a file of process. Having found that much of consecutive
or a process. 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
235 the non-contiguous memory allocation, a process will 236
Lecture - XI BITS Pilani, Pilani Campus Lecture - XI BITS Pilani, Pilani Campus

MEMORY MANAGEMENT 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.

237 238
Lecture - XI BITS Pilani, Pilani Campus Lecture - XI BITS Pilani, Pilani Campus

MEMORY MANAGEMENT MEMORY MANAGEMENT

Non-contiguous memory allocation is of different types, we have an invalid pointer which means the page is not in main
1. Paging memory or we will get the corresponding frame number. When
2. Segmentation the frame number is combined with instruction of set D than we
3. Segmentation with paging will get the corresponding physical address. Size of a page table
i) Paging is generally very large so cannot be accommodated inside the
A non-contiguous policy with a fixed size partition is called PCB, therefore, PCB contains a register value PTBR( page table
paging. A computer can address more memory than the amount base register) which leads to the page table.
of physically installed on the system. This extra memory is Advantages: It is independent of external fragmentation.
actually called virtual memory. Paging technique is very Disadvantages:
important in implementing virtual memory. Secondary memory is • It makes the translation very slow as main memory access
divided into equal size partition (fixed) called pages. Every two times.
process will have a separate page table. The entries in the page • A page table is a burden over the system which occupies
table are the number of pages a process. At each entry either 239 considerable space.
240
Lecture - XI BITS Pilani, Pilani Campus Lecture - XI BITS Pilani, Pilani Campus
MEMORY MANAGEMENT MEMORY MANAGEMENT

ii) Segmentation partition but here in segmentation with paging, we divide it


Segmentation is a programmer view of the memory where according to segments. All the properties are the same as that of
instead of dividing a process into equal size partition we divided paging because segments are divided into pages
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 241 242
Lecture - XI BITS Pilani, Pilani Campus Lecture - XI 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

243 244
Lecture - XI BITS Pilani, Pilani Campus Lecture - XII BITS Pilani, Pilani Campus

MEMORY MANAGEMENT – MEMORY MANAGEMENT –


SEGMENTATION SEGMENTATION
Segmentation In Operating Systems, Segmentation is a memory management
• It is a memory allocation technique to store parts of a single technique in which the memory is divided into the variable size
process in various locations parts. Each part is known as a segment which can be allocated
• It is a variable size partitioning scheme to a process.
• It is a technique in non contiguous memory allocation like The details about each segment are stored in a table called a
paging segment table. Segment table is stored in one (or many) of the
• In segmentation, process is divided into variable sized segments.
partitions called as segments
Segment table contains mainly two information about segment:
• Secondary and main memory are divided into unequal size
partitions Base: It is the base address of the segment
Limit: It is the length of the segment.

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

MEMORY MANAGEMENT – MEMORY MANAGEMENT –


segmentation segmentation
Translation of Logical address into physical address by the free list maintained by memory manager. Then it tries to
Segment table locate space for other segments. Once adequate space is
CPU generates a logical address which contains two parts: located for all the segments, it loads them into their respective
1. Segment Number areas.
2. Offset The operating system also generates a segment map table for
For Example: each program.
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 249 250
Lecture - XII BITS Pilani, Pilani Campus Lecture - XII BITS Pilani, Pilani Campus

MEMORY MANAGEMENT – MEMORY MANAGEMENT –


segmentation 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.
251 252
Lecture - XII BITS Pilani, Pilani Campus Lecture - XII BITS Pilani, Pilani Campus
MEMORY MANAGEMENT –
STRUCTURE OF PAGE TABLE
segmentation
Advantages of Segmentation The data structure that is used by the virtual memory system in
• No internal fragmentation the operating system of a computer in order to store the mapping
• Average Segment Size is larger than the actual page size. between physical and logical addresses is commonly known
• Less overhead as Page Table.
• It is easier to relocate segments than entire address space. We know that the logical address that is generated by the CPU
• The segment table is of lesser size as compared to the page is translated into the physical address with the help of the page
table in paging. table.
Disadvantages Thus page table mainly provides the corresponding frame
• It can have external fragmentation. number (base address of the frame) where that page is stored in
• It is difficult to allocate contiguous memory to variable sized the main memory.
partition.
• Costly memory management algorithms.

253 254
Lecture - XII BITS Pilani, Pilani Campus Lecture - XII BITS Pilani, Pilani Campus

STRUCTURE OF PAGE TABLE 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
255 256
Lecture - XII BITS Pilani, Pilani Campus Lecture - XII BITS Pilani, Pilani Campus

MEMORY MANAGEMENT – PAGE MEMORY MANAGEMENT – PAGE


TABLE STRUCTURE TABLE STRUCTURE
• Hashed Page Tables Two Level Page Table
• Inverted Page Tables Consider a system having 32-bit logical address space and a
Hierarchical Paging page size of 1 KB and it is further divided into:
Another name for Hierarchical Paging is multilevel paging. • Page Number consisting of 22 bits.
• There might be a case where the page table is too big to fit in • Page Offset consisting of 10 bits.
a contiguous space, so we may have a hierarchy with several As we page the Page table, the page number is further divided
levels. into :
• In this type of Paging the logical address space is broke up Page Number consisting of 12 bits.
into Multiple page tables.
Page Offset consisting of 10 bits.
• Hierarchical Paging is one of the simplest techniques and for
this purpose, a two-level page table and three-level page Thus the Logical address is as follows:
table can be used.

257 258
Lecture - XII BITS Pilani, Pilani Campus Lecture - XII BITS Pilani, Pilani Campus
STRUCTURE OF PAGE TABLE STRUCTURE OF PAGE TABLE

Below given figure below shows the Address Translation scheme


for a two-level 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
259 260
Lecture - XII BITS Pilani, Pilani Campus Lecture - XII BITS Pilani, Pilani Campus

MEMORY MANAGEMENT – PAGE


STRUCTURE OF PAGE TABLE
TABLE STRUCTURE
Three Level Page Table Thus in order to avoid such a large table, there is a solution and
For a system with 64-bit logical address space, a two-level that is to divide the outer page table, and then it will result in
paging scheme is not appropriate. Let us suppose that the page a Three-level page table:
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:

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
261 the same elements. 262
Lecture - XII BITS Pilani, Pilani Campus Lecture - XII BITS Pilani, Pilani Campus

STRUCTURE OF PAGE TABLE STRUCTURE OF PAGE TABLE

Each element mainly consists of : The Virtual Page numbers are compared in this chain searching
1. The virtual page number for a match; if the match is found then the corresponding
2. The value of the mapped page frame. physical frame is extracted.
3.A pointer to the next element in the linked list. In this scheme, a variation for 64-bit address space commonly
The following figure shows the address translation scheme of the uses clustered page tables.
Hashed Page Table: 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
263 frame table into a single data structure. 264
Lecture - XII BITS Pilani, Pilani Campus Lecture - XII BITS Pilani, Pilani Campus
STRUCTURE OF PAGE TABLE 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:

265 266
Lecture - XII BITS Pilani, Pilani Campus Lecture - XII BITS Pilani, Pilani Campus

Learning Objectives PAGE REPLACEMENT


ALGORITHM
ØPage replacement algorithms Page Fault in OS-
ØWhat is a File? •A page fault occurs when a page referenced by the CPU is not
found in the main memory.
ØFile Structure
•The required page has to be brought from the secondary
ØFile types memory into the main memory.
ØFile Access mechanisms •A page has to be replaced if all the frames of main memory are
Ø Space allocation for the files already occupied.
ØFile system structure Page replacement is a process of swapping out an existing page
from the frame of a main memory and replacing it with the req
ØDirectory
•Definition Page replacement is required when-
•Structures - Single level, two level , hierarchical , tree structured and •All the frames of main memory are already occupied.
Acyclic graph directory structures •Thus, a page has to be replaced to create a room for the
required page.
267 268
Lecture - XIII BITS Pilani, Pilani Campus Lecture - XIII BITS Pilani, Pilani Campus

PAGE REPLACEMENT PAGE REPLACEMENT


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

PAGE REPLACEMENT PAGE REPLACEMENT


ALGORITHM ALGORITHM
Solution- = 10 – 6
Total number of references = 10 =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%
From here,Total number of page faults occurred = 6 Alternatively,
Calculating Hit ratio- Miss ratio
Total number of page hits = Total number of references – Total number = 1 – Hit ratio
of page misses or page faults = 1 – 0.4 = 0.6 or 60%
273 274
Lecture - XIII BITS Pilani, Pilani Campus Lecture - XIII BITS Pilani, Pilani Campus

FILE MANAGEMENT – FILE MANAGEMENT –


BASICS BASICS
File File Type
A file is a named collection of information that is recorded on File type refers to the ability of the OS to distinguish different
secondary storage device such as magnetic disks, magnetic types of file such as text files, source files and binary files etc.
tapes and optical disks. MS-DOS and UNIX operating system have the following types of
File Structure files:
• A file has a certain defined structure according to its type. (i) Ordinary files
• A text file is a collection of characters organized • These files contain user information such as text, databases
• A source file is a collection of procedures and functions. or an executable program.
• An object file is a collection of bytes organized into blocks that • Users can perform various operations on such files like add,
are understandable by the computer. modify, delete or even remove the entire file.
• When OS defines different file structures, it also contains the (ii) Directory files
code to support these file structures. These files contain list of file names and other information
related to these files.
275 276
Lecture - XIII BITS Pilani, Pilani Campus Lecture - XIII BITS Pilani, Pilani Campus
FILE MANAGEMENT – FILE MANAGEMENT –
BASICS BASICS
(iii) Special files Sequential access
• These files are also known as device files. A sequential access is that in which the records are accessed
• These files represent physical device like disks, terminals, sequentially, i.e., the information in the file is processed in order,
printers, networks, tape drive etc. one record after the other. This access method is the most
File Access Mechanisms primitive one.
File access mechanism refers to the manner in which the Direct/Random access
records of a file are be accessed. Some of the file mechanisms • Random access file organization provides, accessing the
are: records directly.
1. Sequential access • The records need not be in any sequence within the file and
2. Direct/Random access they need not be in adjacent locations on the storage
3. Indexed sequential access medium.
Indexed sequential access
• This mechanism is built up on base of sequential access.
277 278
Lecture - XIII BITS Pilani, Pilani Campus Lecture - XIII BITS Pilani, Pilani Campus

FILE MANAGEMENT – FILE MANAGEMENT –


BASICS BASICS
• An index is created for each file which contains address of • External fragmentation is a major issue for this technique.
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.
279 280
Lecture - XIII BITS Pilani, Pilani Campus Lecture - XIII BITS Pilani, Pilani Campus

FILE MANAGEMENT – FILE MANAGEMENT –


BASICS 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

281 282
Lecture - XIII BITS Pilani, Pilani Campus Lecture - XIII BITS Pilani, Pilani Campus
FILE MANAGEMENT – FILE MANAGEMENT –
BASICS 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.

283 284
Lecture - XIII BITS Pilani, Pilani Campus Lecture - XIII BITS Pilani, Pilani Campus

FILE MANAGEMENT – FILE MANAGEMENT –


BASICS 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.

285 286
Lecture - XIII BITS Pilani, Pilani Campus Lecture - XIII BITS Pilani, Pilani Campus

FILE MANAGEMENT – FILE MANAGEMENT –


BASICS BASICS
• When an application program asks for a file, the request is information to basic file system. The basic file system is
directed to the logical file system.
responsible for issuing the commands to I/O control to
• The logical file system contains the Meta data of the file and
fetch those blocks.
directory structure. If the application program doesn't have the
required permissions of the file then this layer will throw an •I/O controls contain the codes by using which it can access hard
error. Logical file systems also verify the path to the file. disk. These codes are known as device drivers.
• Generally, files are divided into various logical blocks. Files Directory
are to be stored in the hard disk and to be retrieved from the •Directory can be defined as the listing of the related files on the
hard disk. Hard disk is divided into various tracks and sectors. disk
So, in order to store and retrieve the files, the logical blocks • A directory can be viewed as a file which contains the Meta
need to be mapped to physical blocks. This mapping is done data of the bunch of files.
by File organization module. It is also responsible for free
•Every Directory permits the user to do a number of common
space management.
operations on the file:
• Once File organization module has identified the physical
- File Creation
block of the application program needs, it passes this 287 288
Lecture - XIII BITS Pilani, Pilani Campus Lecture - XIII BITS Pilani, Pilani Campus
FILE MANAGEMENT – FILE MANAGEMENT –
Directory Directory
- Search for the file As we can see in the diagram below all the file F1, F2, F3, F4
- File deletion created by the different users are present at the root directory.
- 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.
289 290
Lecture - XIII BITS Pilani, Pilani Campus Lecture - XIII BITS Pilani, Pilani Campus

FILE MANAGEMENT – FILE MANAGEMENT –


Directory 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.
291 292
Lecture - XIII BITS Pilani, Pilani Campus Lecture - XIII BITS Pilani, Pilani Campus

FILE MANAGEMENT – FILE MANAGEMENT –


Directory 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.

293 294
Lecture - XIII BITS Pilani, Pilani Campus Lecture - XIII BITS Pilani, Pilani Campus
FILE MANAGEMENT – FILE MANAGEMENT –
Directory Directory
Here, the files are accessed by their location using the path. 4. Acyclic-Graph Directory Structure
There are two types of paths to locate the file in this directory This problem can be solved by the acyclic-graph directory
structure structure. As this directory structure allows a directory or a file to
Absolute Path have many parent directories. So, a shared file in a directory can
Here, the path for the desired file is described by considering the be pointed by the other user directories who have access to that
root directory as the base directory. shared file using the links.
Relative Path In the diagram below we can see that the directory having file
Here, either the user’s directory is considered as the base F7 and F8 have two parent directories.
directory or the desired file directory is considered as the base
directory.

295 296
Lecture - XIII BITS Pilani, Pilani Campus Lecture - XIII BITS Pilani, Pilani Campus

FILE MANAGEMENT – Learning Objectives


Directory
ØWhat is Free space management?
Ø Types of free space management methods
- Bit Vector
- Linked List
- Grouping
- Counting
Ø Recovery
Ø What is NFS?
ØBenefits of NFS
297 298
Lecture - XIII BITS Pilani, Pilani Campus Lecture - XIV BITS Pilani, Pilani Campus

FILES AND DIRECTORY FILES AND DIRECTORY


MANAGEMENT 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 -
299 300
Lecture - XIV BITS Pilani, Pilani Campus Lecture - XIV BITS Pilani, Pilani Campus
FILES AND DIRECTORY FILES AND DIRECTORY
MANAGEMENT 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
We can find the free block number from the bit vector using the
block has a bit 0 means that block is allocated to a file, and if a
following method-
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
We will now find the first free block number in the above
allocated to some files. The bit vector for this disk will look like
example.
this- 301 302
Lecture - XIV BITS Pilani, Pilani Campus Lecture - XIV BITS Pilani, Pilani Campus

FILES AND DIRECTORY FILES AND DIRECTORY


MANAGEMENT MANAGEMENT
The first group of 8 bits (00111100) constitutes a non-zero word search the entire bit vector.
since all bits are not 0. After finding the non-zero word, we will Keeping the bit vector in the main memory is possible for smaller
look for the first 1 bit. This is the third character of the non-zero disks but not for larger ones. For example, a 1.3 GB disk with
word. Hence, offset = 3. 512-byte blocks would need a bit vector of over 332 KB to track
Therefore, the first free block number = 8 * 0 + 3 = 3. its free blocks. Giving away 332 KB just to maintain its free block
Advantages space is not so efficient in the long run.
The advantages of the bit vector method are- Linked List
• It is simple to understand. Another method of doing free space management is a linked list.
• It is an efficient method. In this method, all the free blocks existing in the disk are linked
• It occupies less memory. together in a linked list. The address of the first free block is
Disadvantages stored somewhere in the memory. Each free block contains a
The disadvantages of the bit vector method are- pointer that contains the address to the next free block. The last
For finding a free block, the operating system may need to 303 free block points to null, indicating the end of the linked list. 304
Lecture - XIV BITS Pilani, Pilani Campus Lecture - XIV BITS Pilani, Pilani Campus

FILES AND DIRECTORY FILES AND DIRECTORY


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

FILES AND DIRECTORY FILES AND DIRECTORY


MANAGEMENT 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
This method overcomes the disadvantages of the linked list
blocks may be allocated or freed simultaneously. In this method,
method. The addresses of a large number of free blocks can be
a linked list is maintained but in addition to the pointer to the next
found quickly, just by going to the first free block or the nth free
free block, a count of free contiguous blocks that follow the first
block. There is no need to traverse the whole list, which was the
block is also maintained. Thus each free block in the disk will
situation in the linked list method.
309
contain two things- 310
Lecture - XIV BITS Pilani, Pilani Campus Lecture - XIV BITS Pilani, Pilani Campus

FILES AND DIRECTORY FILES AND DIRECTORY


MANAGEMENT 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 This method also overcomes the disadvantages of the linked list
of the blocks, i.e., block numbers 1, 2, 7, 8, 15 and 16 are method since there is no need to traverse the whole list.
allocated to some files. Advantages
If we apply the counting method, Block 3 will point to Block 4 and The advantages of the counting method are-
store the count 4 (since Block 3, 4, 5 and 6 are contiguous). • Fast allocation of a large number of consecutive free blocks.
Similarly, Block 9 will point to Block 10 and keep the count 6 • The overall list is smaller in size.
(since Block 9, 10, 11, 12, 13 and 14 are contiguous). This is Disadvantages
also represented in the following figure- The disadvantages of the counting method is
Each free block requires more space for keeping the count in the
311
disk. 312
Lecture - XIV BITS Pilani, Pilani Campus Lecture - XIV BITS Pilani, Pilani Campus
FILES AND DIRECTORY FILES AND DIRECTORY
MANAGEMENT MANAGEMENT
Recovery lost, and with them any changes in the directories of opened
Files and directories are kept both in main memory and on disk, files. Such an event can leave the file system in an inconsistent
and care must be taken to ensure that system failure does not state: The actual state of some files is not as described in the
Result in loss of data or in data inconsistency. We deal with directory structure. Frequently, a special program is run at reboot
these issues in the following sections. time to check for and correct disk inconsistencies.
Consistency Checking In contrast, the loss of a directory entry on an indexed allocation
Some directory information is kept in main memory to speed up system can be disastrous, because the data blocks have no
access. The directory information in main memory is generally knowledge of one another. For this reason, UNIX caches
more up to date than is the corresponding information on the directory entries for reads; but any data write that results in
disk, because cached directory information is not necessarily space allocation, or other metadata changes, is done
written to disk as soon as the update takes place. Consider, synchronously, before the corresponding data blocks are written.
then, the possible effect of a computer crash. Cache and buffer Of course, problems can still occur if a synchronous write is
contents, as well as I/O operations in progress, can be 313 interrupted by a crash. 314
Lecture - XIV BITS Pilani, Pilani Campus Lecture - XIV BITS Pilani, Pilani Campus

FILES AND DIRECTORY FILES AND DIRECTORY


MANAGEMENT MANAGEMENT
Backup and Restore typical backup schedule may then be as follows:
Magnetic disks sometimes fail, and care must be taken to • Day 1. Copy to a backup medium all files from the disk. This is
ensure that the data lost in such a failure are not lost forever. To called a full backup.
this end, system programs can be used to back up data from • Day 2. Copy to another medium all files changed since day 1.
disk to another storage device, such as a floppy disk, magnetic This is an incremental backup.
tape, optical disk, or other hard disk. • Day 3. Copy to another medium all files changed since day 2.
Recovery from the loss of an individual file, or of an entire disk, • Day N. Copy to another medium all files changed since day
may then be a matter of restoring the data from backup. To N— 1. Then go back to Day 1. The new cycle can have its
minimize the copying needed, we can use information from each backup written over the previous set or onto a new set of backup
file's directory entry. For instance, if the backup program knows media.
when the last backup of a file was done, and the file's last write In this manner, we can restore an entire disk by starting restores
date in the directory indicates that the file has not changed since with the full backup and continuing through each of the
that date, then the file does not need to be copied again. A incremental backups. Of course, the larger the value of N, the
315 316
Lecture - XIV BITS Pilani, Pilani Campus Lecture - XIV BITS Pilani, Pilani Campus

FILES AND DIRECTORY FILES AND DIRECTORY


MANAGEMENT MANAGEMENT
greater the number of tapes or disks that must be read for a backup involves more files and more backup media.
complete restore. An added advantage of this backup cycle is A user may notice that a particular file is missing or corrupted
that we can restore any file accidentally deleted during the cycle long after the damage was done. For this reason, we usually
by retrieving the deleted file from the backup of the previous day. plan to take a full backup from time to time that will be saved
The length of the cycle is a compromise between the amount of "forever." It is a good idea to store these permanent backups far
backup medium needed and the number of days back from away from the regular backups to protect against hazard, such
which a restore can be done. To decrease the number of tapes as a fire that destroys the computer and all the backups too. And
that must be read, to do a restore, an option is to perform a full if the backup cycle reuses media, we must take care not to reuse
backup and then each day back up all files that have changed the media too many times—if the media wear out, it might not
since the full backup. In this way, a restore can be done via the be possible to restore any data from the backups.
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 317 318
Lecture - XIV BITS Pilani, Pilani Campus Lecture - XIV BITS Pilani, Pilani Campus
FILES AND DIRECTORY FILES AND DIRECTORY
MANAGEMENT MANAGEMENT
Network File System (NFS) It is a “distributed” file system protocol, which means that its data
The Network File System (NFS) is an IP-based file-sharing is stored on a server and that it uses a network protocol to share
protocol that is used by NAS systems to allow multiple remote – and restrict – access to the file system, using the same coding
systems to connect to a shared file system. NFS uses file-level that is used for local files. NFS has three components: a client,
data access and the target (or destination) NAS device controls a server, and the protocol that enables the two to communicate.
the storage device. 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
319 remotely. 320
Lecture - XIV BITS Pilani, Pilani Campus Lecture - XIV BITS Pilani, Pilani Campus

FILES AND DIRECTORY FILES AND DIRECTORY


MANAGEMENT MANAGEMENT
And if File A is being modified on Computer 2, those (i.e., between computers in “heterogeneous environments”). For
modifications will be seen on Computer 1. Similarly, if the file is example, you could use a Windows NFS file server in a Linux
moved to a different server, the user of Computer 1 won’t know environment to give Linux client computers access to files on
it. that Windows NFS server. And rather than every user in an
While NFS is OS- (operating system) independent, it is typically organization having their home directory on their individual
used on Unix systems, macOS, and Linux. It is also hardware-, network machine, all the home directories could be set up on an
network architectures, and transport protocol independent, and NFS server and then accessed as appropriate by everyone on
works well in both small and large environments. the network.
Distributed file systems such as NFS offer many benefits. The benefits of the NFS service are
Storing data on servers frees up the resources of client • Multiple clients can use the same files, which allows everyone
machines. Distributing data (e.g., important administrative data) on the network to use the same data, accessing it on remote
to multiple clients is easier, and it is even possible to transfer hosts as if it were accessing local files.
files between computers that use different operating systems 321 322
Lecture - XIV BITS Pilani, Pilani Campus Lecture - XIV BITS Pilani, Pilani Campus

FILES AND DIRECTORY Learning Objectives


MANAGEMENT
• Computers share applications, which eliminates the needs for Ø Storage Structure
local disk space and reduces storage costs.
• All users can read the same files, so data can remain up-to-
Ø Disk structure
date, and it’s consistent and reliable. Ø SAN and NAS
• Mounting the file system is transparent to all users. Ø Disk Scheduling algorithms
• Support for heterogeneous environments allows us to run
mixed technology from multiple vendors and use - First Come First Serve
interoperable components. - Shortest seek time first
• System admin overhead is reduced due to centralization of - SCAN
data.
- LOOK
- C-SCAN
- C-LOOK
323 324
Lecture - XIV BITS Pilani, Pilani Campus Lecture - XV BITS Pilani, Pilani Campus
DISK MANAGEMENT – STORAGE DISK MANAGEMENT – STORAGE
STRUCTURE STRUCTURE
Basically we want the programs and data to reside in main Secondary storage is used as an extension of main memory.
memory permanently. This arrangement is usually not possible Secondary storage devices can hold the data permanently.
for the following two reasons: Storage devices consists of registers, cache,
1. Main memory is usually too small to store all needed Electronic-Disk, Magnetic disk , optical disk , Magnetic Tapes.
programs and data permanently. Each storage system provides the basic system of storing a
2. Main memory is a volatile storage device that loses its datum and of holding the datum until it is retrieved at a later time.
contents when power is turned off All the storage devices differ in speed, cost, size and volatility.
There are two types of storage devices:- The most common Secondary-storage device is a Magnetic-disk,
Volatile Storage Device – which provides storage for both programs and data.
It loses its contents when the power of the device is removed.
The following figure shows the hierarchy of storage
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.
325 326
Lecture - XV BITS Pilani, Pilani Campus Lecture - XV BITS Pilani, Pilani Campus

DISK MANAGEMENT – STORAGE DISK MANAGEMENT – STORAGE


STRUCTURE 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
327 magnetic disk. When external power is restored, the 328
Lecture - XV BITS Pilani, Pilani Campus Lecture - XV BITS Pilani, Pilani Campus

DISK MANAGEMENT – STORAGE DISK MANAGEMENT – STORAGE


STRUCTURE STRUCTURE
controller copies the data back into the RAM. Secondary storage devices are those devices whose storage is
The design of a complete memory system must balance all the non volatile. It means that the stored data will be intact even if
factors. It must use only as much expensive memory as the system is turned off. Here are a few things worth noting
necessary while providing as much inexpensive, Non-Volatile about secondary storage.
memory as possible. Caches can be installed to improve • Secondary storage is also called auxiliary storage.
performance where a large access-time or transfer-rate disparity • Secondary storage is less expensive when compared to
exists between two components. 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.

329 330
Lecture - XV BITS Pilani, Pilani Campus Lecture - XV BITS Pilani, Pilani Campus
DISK MANAGEMENT – DISK MANAGEMENT –
MAGNETIC DISK STRUCTURE 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. 331 332
Lecture - XV BITS Pilani, Pilani Campus Lecture - XV BITS Pilani, Pilani Campus

DISK MANAGEMENT – DISK MANAGEMENT – SAN AND


MAGNETIC DISK STRUCTURE NAS
The speed of the disk is measured as two parts: Storage Area Network (SAN) is used for transferring the data
Transfer rate: This is the rate at which the data moves from disk between the servers and the storage devices’ fiber channels and
to the computer. switches. In SAN (Storage Area Network), data is identified by
Random access time: It is the sum of the seek time and disk block. Protocols that are used in SAN are SCSI (Small
rotational latency. Computer System Interface), SATA (Serial Advanced Technology
Seek time is the time taken by the arm to move to the required Attachment), etc.
track. Rotational latency is defined as the time taken by the Components of Storage Area Network (SAN):
arm to reach the required sector in the track. 1.Node ports
Even though the disk is arranged as sectors and tracks 2. Cables
physically, the data is logically arranged and addressed as an 3. Interconnect devices such as Hubs, switches, directors
array of blocks of fixed size. The size of a block can 4. Storage arrays
be 512 or 1024 bytes. Each logical block is mapped with a sector 5. SAN management Software
on the disk, sequentially. 333 334
Lecture - XV BITS Pilani, Pilani Campus Lecture - XV BITS Pilani, Pilani Campus

DISK MANAGEMENT – SAN AND DISK MANAGEMENT – SAN AND


NAS NAS
Network Attached Storage (NAS), data is identified by file The difference between Storage Area Network (SAN) and
name as well as byte offset. In-Network Attached Storage, the Network Attached Storage (NAS) are as follows:
file system is managed by Head units such as CPU and Memory. SAN NAS
In this for backup and recovery, files are used instead of the
SAN stands for Storage Area Network. NAS stands for Network Attached Storage.
block-by-block copying technique. Data is identified by disk block Data is identified by file name as well as byte offset.
Components of Network Attached Storage (NAS): File system is managed by servers File system is managed by Head unit.
It is more costly. It is less expensive than SAN.
1. Head unit: CPU, Memory 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
2. Network Interface Card (NIC) (Common Internet File System), etc.
3. Optimized operating system For backups and recovery Block by block For backups and recovery in NAS, Files are used.
copying technique is used.
4. Protocols Gives high performance in high-speed for Not suitable for that environment
5. Storage protocols: ATA (Advanced Technology Attachment), high-speed traffic systems.
It has lower latency. Compared to SAN, NAS has higher latency.
SCSI, FC (Fibre Channel) SAN supports virtualization. NAS does not support virtualization.

335 336
Lecture - XV BITS Pilani, Pilani Campus Lecture - XV BITS Pilani, Pilani Campus
DISK MANAGEMENT – DISK DISK MANAGEMENT – DISK
SCHEDULING ALGORITHMS SCHEDULING ALGORITHMS
Disk Scheduling Algorithms First Come First Serve
On a typical multiprogramming system, there will usually be This algorithm performs requests in the same order asked by the
multiple disk access requests at any point of time. So those system. Let's take an example where the queue has the
requests must be scheduled to achieve good efficiency. Disk following requests with cylinder numbers as follows:
scheduling is similar to process scheduling. Some of the disk 98, 183, 37, 122, 14, 124, 65, 67
scheduling algorithms are Assume the head is initially at cylinder 56. The head moves in
1. First Come First Serve the given order in the queue i.e., 56→98→183→...→67.
2. Shortest Seek time first
3. SCAN algorithm
4. LOOK
5. C-SCAN
6. C-LOOK
337 338
Lecture - XV BITS Pilani, Pilani Campus Lecture - XV BITS Pilani, Pilani Campus

DISK MANAGEMENT – DISK DISK MANAGEMENT – DISK


SCHEDULING ALGORITHMS 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.

339 340
Lecture - XV BITS Pilani, Pilani Campus Lecture - XV BITS Pilani, Pilani Campus

DISK MANAGEMENT – DISK DISK MANAGEMENT – DISK


SCHEDULING ALGORITHMS 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.
341 342
Lecture - XV BITS Pilani, Pilani Campus Lecture - XV BITS Pilani, Pilani Campus
DISK MANAGEMENT – DISK DISK MANAGEMENT – DISK
SCHEDULING ALGORITHMS 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.

343 344
Lecture - XV BITS Pilani, Pilani Campus Lecture - XV BITS Pilani, Pilani Campus

DISK MANAGEMENT – DISK DISK MANAGEMENT – DISK


SCHEDULING ALGORITHMS 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,
Seek Time = Distance Moved by the disk arm 160 and the initial position of the Read-Write head is 60. And it is
= (170-60)+(170-25) given that the disk arm should move towards the larger value.
= 235
345 346
Lecture - XV BITS Pilani, Pilani Campus Lecture - XV BITS Pilani, Pilani Campus

DISK MANAGEMENT – DISK DISK MANAGEMENT – DISK


SCHEDULING ALGORITHMS 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.
Seek Time = Distance Moved by the disk arm Eg. Suppose the order of requests are 70, 140, 50, 125, 30, 25,
= (170-60)+(170-0)+(50-0) 160 and the initial position of the Read-Write head is 60. And it is
=330 given that the disk arm should move towards the larger value.

347 348
Lecture - XV BITS Pilani, Pilani Campus Lecture - XV BITS Pilani, Pilani Campus
DISK MANAGEMENT – DISK
SCHEDULING ALGORITHMS Learning Objectives

 What is Swap Space?


 Advantages of Swap Space
 What is swap space management
 RAID
- Need
- RAID 0
Seek Time = Distance Moved by the disk arm - RAID 1
= (160-60)+(160-25)+(50-25) - RAID 3
= 260 - RAID 5
- RAID 6
349 350
Lecture - XV BITS Pilani, Pilani Campus Lecture - XVI BITS Pilani, Pilani Campus

SWAP SPACE MANAGEMENT SWAP SPACE MANAGEMENT

Most of the time, some amount of physical memory in a Virtual memory is a combination of RAM and disk space that
computer is swapped on disk. Swap space is a space on a hard running processes can use. Swap space is the portion of
disk that is a substitute for physical memory. It is used as virtual virtual memory on the hard disk, used when RAM is full. Swap
memory, which contains images of the running process in the space can be useful to computers in the following various ways,
memory. Whenever our computer runs short of physical memory, such as:
it uses its virtual memory and stores information in memory on a • It can be used as a single contiguous memory which reduces
disk. I/O operations to read or write a file.
The process of swapping or interchanging of data between • Applications that are not used or less frequently used can be
virtual memory and real memory is called swapping and the kept in a swap file.
space on disk is known as swap space. Swap space helps the • Having sufficient swap files helps the system keep some
physical memory free all the time.
computer's operating system pretend that it has more RAM than
• The space in physical memory that has been freed due to
it actually has. It is also called a swap file. swap space can be used by OS for other important tasks.
351 352
Lecture - XVI BITS Pilani, Pilani Campus Lecture - XVI BITS Pilani, Pilani Campus

SWAP SPACE MANAGEMENT SWAP SPACE MANAGEMENT

Operating systems such as Windows, Linux, etc. systems system performance. The main goal for the design and
provide a certain amount of swap space by default which users implementation of swap space is to provide the best throughput
can change according to their needs. If we don't want to use for the virtual memory system.
virtual memory, we can easily disable it together. Still, if we run Swap space is used in various ways by different operating
out of memory, then the kernel will kill some of the processes to systems, depending on the memory-management algorithms in
create a sufficient amount of space in physical memory so that it use. For example, systems that implement swapping may use
totally depends upon the user whether he wants to use swap swap space to hold an entire process image, including the code
space or not. and data segments. Paging systems may simply store pages
What is Swap-Space Management? that have been pushed out of the main memory. The amount of
Managing the Swap-space is another low-level task of the swap space needed on a system can vary depending on the
operating system. Virtual memory uses disk space as an amount of physical memory, the amount of virtual memory it is
extension of main memory. Since disk access is much slower backing, and how it is used. It can range from a
than memory access, using swap space significantly decreases 353 few megabytes of disk space to gigabytes. 354
Lecture - XVI BITS Pilani, Pilani Campus Lecture - XVI BITS Pilani, Pilani Campus
SWAP SPACE MANAGEMENT SWAP SPACE MANAGEMENT

Note that it may be safer to overestimate than to underestimate Some operating systems, including Linux, allow multiple swap
the amount of swap space required because if a system runs spaces. These swap spaces are usually put on separate disks
out of swap space, it may be forced to abort processes or may so the load placed on the I/O system by paging and swapping
crash entirely. Overestimation wastes disk space that could can be spread over the system's I/O devices.
otherwise be used for files, but it does no other harm. Some Uses of Swap Space
systems recommend the amount to be set aside for swap space. The different operating system uses Swap-space in various
Solaris, for example, suggests setting swap space equal to the ways. The systems that are implementing swapping may use
amount by which virtual memory exceeds page-able physical swap space to hold the entire process, including image, code,
memory. Previously, Linux suggested setting swap space to and data segments.
double the amount of physical memory, although most Linux Swapping is a memory management technique used in multi-
systems now use considerably less swap space. There is programming to increase the number of processes sharing the
currently much debate in the Linux community about whether to CPU. It is a technique of removing a process from the main
set aside swap space at all. 355 memory, storing it into secondary memory, and then bringing it 356
Lecture - XVI BITS Pilani, Pilani Campus Lecture - XVI BITS Pilani, Pilani Campus

SWAP SPACE MANAGEMENT SWAP SPACE MANAGEMENT

back into the main memory for continued execution. This action files, but it does not harm others. The following table shows
of moving a process out from main memory to secondary different systems using the amount of swap space:
memory is called Swap Out. The action of moving a process out S.No. System Swap Space
from secondary memory to main memory is called Swap In.
1. Solaris Swap space is equal to the amount of
Paging systems may simply store pages that have been pushed physical memory.

out of the main memory. The need for swap space on a system 2. Linux Swap space is double the amount of physical
memory.
can vary from megabytes to gigabytes. Still, it also depends on
the amount of physical memory, the virtual memory it is backing, Solaris setting swap space equal to the amount by which virtual
and how it uses the virtual memory. memory exceeds page-able physical memory.
It is safer to overestimate than to underestimate the amount of Previously, Linux has suggested setting swap space to double
swap space required because if a system runs out of swap the amount of physical memory. Today, this limitation is gone,
space, it may be forced to abort the processes or may crash and most Linux systems use considerably less swap space.
entirely. Overestimation wastes disk space that could be used for357 358
Lecture - XVI BITS Pilani, Pilani Campus Lecture - XVI BITS Pilani, Pilani Campus

SWAP SPACE MANAGEMENT RAID

Including Linux, some operating systems allow the use of RAID stands for ‘Redundant Arrays of Independent Disks’ or
multiple swap spaces, including both files and dedicated swap ‘Redundant Arrays of Inexpensive Disks. This technology
partitions. The swap spaces are placed on the disk, so the load plays a vital role in storing large amounts of data while
on the I/O by the paging and swapping will spread over the preserving Data Integrity. It helps in real-time data recovery
system's bandwidth. 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.

359 360
Lecture - XVI BITS Pilani, Pilani Campus Lecture - XVI BITS Pilani, Pilani Campus
RAID RAID

RAID Configuration Levels configuration offers best ‘Write’ performance and it does not
Different levels represent specific configuration of Disk Arrays. employ redundancy at all. ‘Read’ performance is low in this
Only few configurations are practical for most of the processing configuration.
systems. Hence RAID – 0 ,1,3,5 and 6 are discussed below. If one physical disk in the array fails, the data is lost. This type of
RAID – 0 (Non-Redundant Configuration) configuration is preferred in super-computing environments
RAID – 1 (Mirrored Configuration) where performance and capacity, rather than reliability are the
RAID – 3 (Bit-Interleaved Parity) primary concerns. The following figure shows Non-Redundant
RAID – 5 (Block-Interleaved Distributed-Parity) Configuration where the Data is distributed evenly and striped
RAID – 6 (P+Q Redundancy) across three disks.
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 361 362
Lecture - XVI BITS Pilani, Pilani Campus Lecture - XVI BITS Pilani, Pilani Campus

RAID 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.
Non-Redundant Configuration

363 364
Lecture - XVI BITS Pilani, Pilani Campus Lecture - XVI BITS Pilani, Pilani Campus

RAID 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
Mirrored Configuration of architecture is found in applications that require high
365 bandwidth but not high I/0 rates. 366
Lecture - XVI BITS Pilani, Pilani Campus Lecture - XVI BITS Pilani, Pilani Campus
RAID 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.

RAID – 3 Configuration
367 368
Lecture - XVI BITS Pilani, Pilani Campus Lecture - XVI BITS Pilani, Pilani Campus

RAID 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.

Block-Interleaved Distributed Parity Configuration

369 370
Lecture - XVI BITS Pilani, Pilani Campus Lecture - XVI BITS Pilani, Pilani Campus

RAID 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:
RAID 6 Configuration • In spite of using this technology, backup software is a must.
• Mapping Logic blocks onto physical locations is complex.
371 • Data chunk size affects the performance of disk array. 372
Lecture - XVI BITS Pilani, Pilani Campus Lecture - XVI BITS Pilani, Pilani Campus
RAID - Problems RAID

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

RAID RAID

Transfer time Solution for Problem 2:


= (512 bytes / 40 KB) sec Number of tracks per surface = 200
= 0.0125 sec Disk rotation speed = 2400 RPM
= 12.5 msec Track storage capacity = 62500 bits

Time taken for one full rotation


Average access time
= (60 / 2400) sec
= Average seek time + Average rotational delay + Transfer time
= (1 / 40) sec
+ Controller overhead + Queuing delay
= 0.025 sec
= 5 msec + 5 msec + 12.5 msec + 0.1 msec + 0
= 25 msec
= 22.6 msec Average latency or Average rotational latency
= 1/2 x Time taken for one full rotation
= 1/2 x 25 msec
375
= 12.5 msec 376
Lecture - XVI BITS Pilani, Pilani Campus 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

377
Lecture - XVI BITS Pilani, Pilani Campus

You might also like