Osa
Osa
ComponentsofComputerSystem ComponentsofComputerSystem
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 systems
functional operating system. Because an operating system is
large and complex, it must be created piece by piece. Each of System and Application Programs
these pieces should be a well-delineated portion of the
system, with carefully defined inputs, outputs, and functions Operating System
A computer system has four components. They are
1. Hardware 3. Users Computer Hardware
2. Application Programs 4. Operating System
3 4
Lecture I BITS Pilani, Pilani Campus Lecture I BITS Pilani, Pilani Campus
ComponentsofOS ComponentsofComputerSystem
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
within which other programs can do useful work.
5 6
Lecture I BITS Pilani, Pilani Campus Lecture I BITS Pilani, Pilani Campus
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 MAINFRAMESYSTEMS
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
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
MAINFRAMESYSTEMS OPERATINGSYSTEM
(i) Client Server Model
MAINFRAMESYSTEMS MAINFRAMESYSTEMS
(ii) Peer – to - Peer Model
15 16
Lecture I BITS Pilani, Pilani Campus Lecture I BITS Pilani, Pilani Campus
MAINFRAME SYSTEMS MAINFRAME 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
MAINFRAMESYSTEMS ComputerSystemoperation
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
ComputerSystemoperation ComputerSystemoperation
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
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
ComputerSystemoperation ComputerSystemoperation
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
ComputerSystemoperation ComputerSystemoperation
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
ComputerSystemoperation ComputerSystemoperation
Registers
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
SingleandMultiprocessorSystems SingleandMultiprocessorSystems
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 processors is not however; rather, it is
begun to dominate the landscape of computing. Such systems less than 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, programmers working closely
desktop and laptop systems. Recently, multiple processors together do not produce 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
ComputerSystemArchitecture ComponentsofOperatingSystem
-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
ComponentsofOperatingSystem ComponentsofOperatingSystem
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
• 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 or Device Management
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 program much time.
responsible for this task is known as traffic controller. • Allocates the device in the efficient way.
• Allocates the processor (CPU) to a process. • De-allocates devices.
39 40
Lecture I BITS Pilani, Pilani Campus Lecture I BITS Pilani, Pilani Campus
ComponentsofOperatingSystem ComponentsofOperatingSystem
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 systemperformance − 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
ComponentsofOperatingSystem LearningObjectives
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
OPERATINGSYSTEMSERVICES OPERATINGSYSTEMSERVICES
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
SYSTEMCALLS
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
OPERATINGSYSTEMSERVICES OPERATINGSYSTEMSERVICES
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
OPERATINGSYSTEMSERVICES OPERATINGSYSTEMSERVICES
• 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
OPERATINGSYSTEMSERVICES OPERATINGSYSTEMSERVICES
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
SYSTEMCALLS SYSTEMCALLS
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:
- 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
SYSTEMCALLS SYSTEMCALLS
Functions: Functions:
55 56
Lecture - 2 BITS Pilani, Pilani Campus Lecture - 2 BITS Pilani, Pilani Campus
SYSTEMCALLS SYSTEMCALLS
CloseHandle() close() Protection SetFileSecurity() chmod()
Device SetConsoleMode() ioctl() InitlializeSecurityDescriptor() umask()
Manipulation ReadConsole() read() SetSecurityDescriptorGroup() chown()
WriteConsole() write()
57 58
Lecture - 2 BITS Pilani, Pilani Campus Lecture - 2 BITS Pilani, Pilani Campus
LearningObjectives PROCESSCONCEPT
Ø 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
PROCESSCONCEPT PROCESSCONCEPT
• 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
PROCESSSCHEDULING PROCESSSCHEDULINGQUEUES
PROCESSSCHEDULINGQUEUES PROCESSSCHEDULING
PROCESSSCHEDULING PROCESSSCHEDULING
CONTEXTSWITCHING CONTEXTSWITCHING
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
CONTEXTSWITCHING LearningObjectives
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
PROCESSCONCEPT PROCESSCONCEPT
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
PROCESSCONCEPT PROCESSCONCEPT
PROCESSCONCEPT PROCESSCONCEPT
Kernel M
83 84
Lecture - IV BITS Pilani, Pilani Campus Lecture - IV BITS Pilani, Pilani Campus
THREAD THREAD
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
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
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
K K K Kernel Thread
99 100
Lecture - IV BITS Pilani, Pilani Campus Lecture - IV BITS Pilani, Pilani Campus
LearningObjectives
Ø 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
THREADLIBARARIES THREADLIBARARIES
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
THREADLIBARARIES PROCESSSHEDULING
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
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- preemptive.
specific process. The process that keeps the CPU busy will release All other scheduling are preemptive.
the CPU either by switching context or terminating. It is the only Important CPU scheduling Terminologies
method that can be used for various hardware platforms. That’ s Burst Time/Execution Time: It is a time required by the process to
because it doesn’ t need special hardware (for example, a timer) like complete execution. It is also called running time.
preemptive scheduling.
107 108
Lecture - V BITS Pilani, Pilani Campus Lecture - V BITS Pilani, Pilani Campus
109 110
Lecture - V BITS Pilani, Pilani Campus Lecture - V BITS Pilani, Pilani Campus
Response time: Itis 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
111 112
Lecture - V BITS Pilani, Pilani Campus Lecture - V BITS Pilani, Pilani Campus
PROCESS SHEDULING PROCESS SHEDULING
113 114
Lecture - V BITS Pilani, Pilani Campus Lecture - V BITS Pilani, Pilani Campus
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
• Processes with same priority are executed on first come first served P1 1 3 2
basis. P2 2 8 1
P0 P3 P1 P2
0 5 11 14 22
117 118
Lecture - V BITS Pilani, Pilani Campus Lecture - V BITS Pilani, Pilani Campus
119 120
Lecture - V BITS Pilani, Pilani Campus Lecture - V BITS Pilani, Pilani Campus
PROCESS SHEDULING PROCESS SHEDULING
• Context switching is used to save states of preempted processes. Time(AT) Time(BT) Time(CT) Time
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
PROCESSSHEDULING PROCESSSHEDULING
123 124
Lecture - V BITS Pilani, Pilani Campus Lecture - V BITS Pilani, Pilani Campus
LearningObjectives MULTIPLE-LEVELQUEUESSCHEDULING
MULTIPLE-LEVELQUEUESSCHEDULING MULTIPLE-LEVELQUEUESSCHEDULING
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
P4 10 5 1 robin fashion and completes after 7 units then process in
queue 2 (P3) starts running (as there is no process in queue 1)
but while it is running P4 comes in queue 1 and interrupts P3
127 128
Lecture - VI BITS Pilani, Pilani Campus Lecture - VI BITS Pilani, Pilani Campus
MULTIPLE-LEVELQUEUESSCHEDULING MULTIPLEPROCESSORSCHEDULING
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
MULTIPLEPROCESSORSCHEDULING ALGORITHMEVALUATION
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
scheduler for each processor examine the ready queue and select a • Maximize CPU utilization under the constraint that the
process to execute. maximum response time is 1 second.
• Maximize throughput such that turnaround time is ( on
average ) linearly proportional to total execution time
Once the selection criteria is defined, we want to evaluate the
various algorithms under consideration.
There are three types of algorithm evaluation methods. They
are
131 132
Lecture - VI BITS Pilani, Pilani Campus : Lecture - VI BITS Pilani, Pilani Campus
ALGORITHMEVALUATION ALGORITHMEVALUATION
ALGORITHMEVALUATION ALGORITHMEVALUATION
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
ALGORITHMEVALUATION THREADSCHEDULING
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 . The Java
conditions over time.
runtime environment supports a very simple, deterministic
scheduling algorithm called . 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
THREADSCHEDULING THREADSCHEDULING
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 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
LearningObjectives PROCESSSYNCHRONIZATION
PROCESSSYNCHRONIZATION PROCESSSYNCHRONIZATION
PROCESSSYNCHRONIZATION PROCESSSYNCHRONIZATION
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
PROCESSSYNCHRONIZATION PROCESSSYNCHRONIZATION
PROCESSSYNCHRONIZATION PROCESSSYNCHRONIZATION
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 section, it
Flag[j]=true; has to set its flag as true. For example, If Pi wants to enter it
turn=j; 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 exiting
Remainder Section would change the TURN to another number from the list of
} While (1); 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
PROCESSSYNCHRONIZATION PROCESSSYNCHRONIZATION
PROCESSSYNCHRONIZATION PROCESSSYNCHRONIZATION
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
PROCESSSYNCHRONIZATION LearningObjectives
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
PROCESSSYNCHRONIZATION-Bounded
PROCESSSYNCHRONIZATION
BufferProblem
- 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 in
container [buffer] it
- Consumer consumes the product that is available in the container - When the container is empty, the consumer can not take any product
- The container should be used by only one of them , either the 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
PROCESSSYNCHRONIZATION PROCESSSYNCHRONIZATION
161 162
Lecture - VIII BITS Pilani, Pilani Campus Lecture - VIII BITS Pilani, Pilani Campus
PROCESSSYNCHRONIZATION PROCESSSYNCHRONIZATION
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
PROCESSSYNCHRONIZATION – Dining
PROCESSSYNCHRONIZATION
PhilosopherProblem
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
PROCESSSYNCHRONIZATION PROCESSSYNCHRONIZATION
169 170
Lecture - VIII BITS Pilani, Pilani Campus Lecture - VIII BITS Pilani, Pilani Campus
PROCESSSYNCHRONIZATION PROCESSSYNCHRONIZATION
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
PROCESSSYNCHRONIZATION PROCESSSYNCHRONIZATION
LearningObjectives 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
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 process the Resource Rk where [k>j].
forcefully. It removes the waiting process and not the running To request a resource Rj, the process must release all Ri such
process. When a process requests a resource, any one of the that i>=j.
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 spaces
189 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).
A system can go from a safe state to an unsafe state. Suppose
Processes Maximum Needs Current Needs
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
LearningObjectives DEADLOCKAVOIDANCE
Ø 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
DEADLOCKAVOIDANCE DEADLOCKAVOIDANCE
DEADLOCKAVOIDANCE DEADLOCKAVOIDANCE
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
DEADLOCKAVOIDANCE DEADLOCKAVOIDANCE
It makes an "s-state" check before actually allocating the resources to A process must specify in the beginning the maximum number of
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 required
available. Here, customers are analogous to processes, cash to 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
DEADLOCKAVOIDANCE DEADLOCKAVOIDANCE
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
DEADLOCKAVOIDANCE DEADLOCKAVOIDANCE
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
Finish[i] = 'false' Processes
Allocation
A B C
Max
A B C
Available
A B C
Needi ≤ Work
If no such 'i' is found jump to step four. P1 2 1 2 3 2 2
Step 4 - If finish[i] = True for all then the system is in a safe state.
P4 1 1 2 1 1 2
207 208
Lecture - X BITS Pilani, Pilani Campus Lecture - X BITS Pilani, Pilani Campus
DEADLOCKAVOIDANCE DEADLOCKAVOIDANCE
DEADLOCKDETECTION&RECOVERY DEADLOCKAVOIDANCE
DEADLOCKAVOIDANCE DEADLOCKDETECTION&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 process. So, we Finding the deadlock the operating system will recover from it
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
DEADLOCKDETECTION&RECOVERY DEADLOCKDETECTION&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
DEADLOCKDETECTION&RECOVERY DEADLOCKDETECTION&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
DEADLOCKDETECTION&RECOVERY DEADLOCKDETECTION&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 decide 220
Lecture - X BITS Pilani, Pilani Campus Lecture - X BITS Pilani, Pilani Campus
DEADLOCKDETECTION&RECOVERY DEADLOCKDETECTION&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
LearningObjectives MEMORYMANAGEMENT
Ø 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
Ø Non Contiguous Memory allocation a round-robin CPU algorithm. When a quantum expires, the
- 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
MEMORYMANAGEMENT MEMORYMANAGEMENT
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
MEMORYMANAGEMENT MEMORYMANAGEMENT
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
MEMORYMANAGEMENT MEMORYMANAGEMENT
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
MEMORYMANAGEMENT MEMORYMANAGEMENT
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
MEMORYMANAGEMENT MEMORYMANAGEMENT
MEMORYMANAGEMENT MEMORYMANAGEMENT
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
MEMORYMANAGEMENT MEMORYMANAGEMENT
237 238
Lecture - XI BITS Pilani, Pilani Campus Lecture - XI BITS Pilani, Pilani Campus
MEMORYMANAGEMENT MEMORYMANAGEMENT
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 two
divided into equal size partition (fixed) called pages. Every 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 considerable space.
239 240
Lecture - XI BITS Pilani, Pilani Campus Lecture - XI BITS Pilani, Pilani Campus
MEMORYMANAGEMENT MEMORYMANAGEMENT
MEMORYMANAGEMENT LearningObjectives
Ø 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
MEMORYMANAGEMENT – MEMORYMANAGEMENT –
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
MEMORYMANAGEMENT – MEMORYMANAGEMENT –
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
MEMORYMANAGEMENT – MEMORYMANAGEMENT –
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
MEMORYMANAGEMENT – MEMORYMANAGEMENT –
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
MEMORYMANAGEMENT –
STRUCTUREOFPAGETABLE
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
STRUCTUREOFPAGETABLE STRUCTUREOFPAGETABLE
255 256
Lecture - XII BITS Pilani, Pilani Campus Lecture - XII BITS Pilani, Pilani Campus
MEMORYMANAGEMENT – PAGE MEMORYMANAGEMENT – PAGE
TABLESTRUCTURE TABLESTRUCTURE
• 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 a • Page Offset consisting of 10 bits.
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 into Page Number consisting of 12 bits.
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 table Thus the Logical address is as follows:
can be used.
257 258
Lecture - XII BITS Pilani, Pilani Campus Lecture - XII BITS Pilani, Pilani Campus
STRUCTUREOFPAGETABLE STRUCTUREOFPAGETABLE
MEMORYMANAGEMENT – PAGE
STRUCTUREOFPAGETABLE
TABLESTRUCTURE
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:
STRUCTUREOFPAGETABLE STRUCTUREOFPAGETABLE
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
STRUCTUREOFPAGETABLE STRUCTUREOFPAGETABLE
• 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
LearningObjectives PAGEREPLACEMENTALGORITHM
Ø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
ØDirectory from the frame of a main memory and replacing it with the req
• 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
PAGEREPLACEMENTALGORITHM PAGEREPLACEMENTALGORITHM
PAGEREPLACEMENTALGORITHM PAGEREPLACEMENTALGORITHM
LRU Page Replacement Algorithm Hence, it is used as a performance measure criterion for other
algorithms.
• As the name suggests, this algorithm works on the principle of
Random Page Replacement Algorithm
“ Least Recently Used“ .
• As the name suggests, this algorithm randomly replaces any page.
• It replaces the page that has not been referred by the CPU for
• So, this algorithm may behave like any other algorithm like FIFO, LIFO,
the longest time.
LRU, Optimal etc.
Optimal Page Replacement Algorithm
PROBLEMS BASED ON PAGE REPLACEMENT ALGORITHMS
• This algorithm replaces the page that will not be referred by the Problem-01: A system uses 3 page frames for storing process
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
empty. What is the total number of page faults that will occur
longest time can not be predicted.
while processing the page reference string given below-
• However, it is the best known algorithm and gives the least
number of page faults. 4 , 7, 6, 1, 7, 6, 1, 2, 7, 2. Also calculate the hit ratio and
miss ratio.
271 272
Lecture - XIII BITS Pilani, Pilani Campus Lecture - XIII BITS Pilani, Pilani Campus
PAGEREPLACEMENTALGORITHM PAGEREPLACEMENTALGORITHM
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,
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 or
• A source file is a collection of procedures and functions. 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
277 278
Lecture - XIII BITS Pilani, Pilani Campus Lecture - XIII BITS Pilani, Pilani Campus
• 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
FILEMANAGEMENT – BASICS FILEMANAGEMENT – 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
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
285 286
Lecture - XIII BITS Pilani, Pilani Campus Lecture - XIII BITS Pilani, Pilani Campus
• 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
error. Logical file systems also verify the path to the file. hard disk. These codes are known as device drivers.
Directory
• Generally, files are divided into various logical blocks. Files are
to be stored in the hard disk and to be retrieved from the hard • Directory can be defined as the listing of the related files on the
disk. Hard disk is divided into various tracks and sectors. So, in disk
order to store and retrieve the files, the logical blocks need to • A directory can be viewed as a file which contains the Meta
be mapped to physical blocks. This mapping is done by File data of the bunch of files.
organization module. It is also responsible for free space
• Every Directory permits the user to do a number of common
management.
operations on the file:
• Once File organization module has identified the physical block
- File Creation
of the application program needs, it passes this 287 288
Lecture - XIII BITS Pilani, Pilani Campus Lecture - XIII BITS Pilani, Pilani Campus
FILEMANAGEMENT – Directory FILEMANAGEMENT – 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
293 294
Lecture - XIII BITS Pilani, Pilani Campus Lecture - XIII BITS Pilani, Pilani Campus
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
FILEMANAGEMENT – Directory LearningObjectives
FILESANDDIRECTORY FILESANDDIRECTORY
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
FILESANDDIRECTORY FILESANDDIRECTORY
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
FILESANDDIRECTORY FILESANDDIRECTORY
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
FILESANDDIRECTORY FILESANDDIRECTORY
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
FILESANDDIRECTORY FILESANDDIRECTORY
MANAGEMENT MANAGEMENT
Advantages these free blocks contains the addresses of the next 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 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 free figure-
blocks. The first of these blocks are free. The last block in
307 308
Lecture - XIV BITS Pilani, Pilani Campus Lecture - XIV BITS Pilani, Pilani Campus
FILESANDDIRECTORY FILESANDDIRECTORY
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
This method overcomes the disadvantages of the linked list method takes advantage of the fact that several contiguous
method. The addresses of a large number of free blocks can be blocks may be allocated or freed simultaneously. In this method,
found quickly, just by going to the first free block or the nth free a linked list is maintained but in addition to the pointer to the next
block. There is no need to traverse the whole list, which was the free block, a count of free contiguous blocks that follow the first
situation in the linked list method. block is also maintained. Thus each free block in the disk will
contain two things-
309 310
Lecture - XIV BITS Pilani, Pilani Campus Lecture - XIV BITS Pilani, Pilani Campus
FILESANDDIRECTORY FILESANDDIRECTORY
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.
Advantages
allocated to some files.
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.
Disadvantages
(since Block 9, 10, 11, 12, 13 and 14 are contiguous). This is
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
disk.
311 312
Lecture - XIV BITS Pilani, Pilani Campus Lecture - XIV BITS Pilani, Pilani Campus
FILESANDDIRECTORY FILESANDDIRECTORY
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
FILESANDDIRECTORY FILESANDDIRECTORY
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
FILESANDDIRECTORY FILESANDDIRECTORY
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
FILESANDDIRECTORY FILESANDDIRECTORY
MANAGEMENT MANAGEMENT
Network File System (NFS) It is a “ distributed” file system protocol, which means that its
The Network File System (NFS) is an IP-based file-sharing data
protocol that is used by NAS systems to allow multiple remote is stored on a server and that it uses a network protocol to share
systems to connect to a shared file system. NFS uses file-level – and restrict – access to the file system, using the same coding
data access and the target (or destination) NAS device controls that is used for local files. NFS has three components: a client,
the storage device. a server, and the protocol that enables the two to communicate.
It allows a user on Computer 1 (the “ client” ) to access File A on
Computer 2 over a network in much the same way they would
access a file on Computer 1’ s own hard drive. To Computer 1,
NFS is ‘ invisible’ ; while NFS is keeping track of where files are
located and transporting data, Computer 1 ‘ sees’ a file system
that looks local. The naming convention used by NFS makes it
impossible to tell from File A’ s name that it’ s being accessed
319 320
Lecture - XIV BITS Pilani, Pilani Campus remotely. Lecture - XIV BITS Pilani, Pilani Campus
FILESANDDIRECTORY FILESANDDIRECTORY
MANAGEMENT MANAGEMENT
And if File A is being modified on Computer 2, those (i.e., between computers in “ heterogeneous environments” ).
modifications will be seen on Computer 1. Similarly, if the file is For
moved to a different server, the user of Computer 1 won’ t know example, you could use a Windows NFS file server in a Linux
it. environment to give Linux client computers access to files on
While NFS is OS- (operating system) independent, it is typically that Windows NFS server. And rather than every user in an
used on Unix systems, macOS, and Linux. It is also hardware-, organization having their home directory on their individual
network architectures, and transport protocol independent, and network machine, all the home directories could be set up on an
works well in both small and large environments. NFS server and then accessed as appropriate by everyone on
Distributed file systems such as NFS offer many benefits. the network.
Storing data on servers frees up the resources of client The benefits of the NFS service are
machines. Distributing data (e.g., important administrative data) • Multiple clients can use the same files, which allows everyone
to multiple clients is easier, and it is even possible to transfer on the network to use the same data, accessing it on remote
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
FILESANDDIRECTORY
LearningObjectives
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
Ø Disk structure
up-to-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 interoperable - First Come First Serve
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
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 programs Electronic-Disk, Magnetic disk , optical disk , Magnetic Tapes.
and data permanently. Each storage system provides the basic system of storing a
2. Main memory is a volatile storage device that loses its contents datum and of holding the datum until it is retrieved at a later time.
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
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
DISKMANAGEMENT – MAGNETICDISK
STRUCTURE
DISKMANAGEMENT – SANANDNAS
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):
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.
File system is managed by servers File system is managed by Head unit.
Components of Network Attached Storage (NAS):
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
DISKMANAGEMENT – DISK DISKMANAGEMENT – DISK
SCHEDULINGALGORITHMS SCHEDULINGALGORITHMS
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
339 340
Lecture - XV BITS Pilani, Pilani Campus Lecture - XV BITS Pilani, Pilani Campus
SCHEDULINGALGORITHMS SCHEDULINGALGORITHMS
SCAN algorithm
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
DISKMANAGEMENT – DISK DISKMANAGEMENT – DISK
SCHEDULINGALGORITHMS SCHEDULINGALGORITHMS
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
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
DISKMANAGEMENT – DISK
SCHEDULINGALGORITHMS Learning Objectives
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. 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 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 swap
it actually has. It is also called a . space can be used by OS for other important tasks.
351 352
Lecture - XVI BITS Pilani, Pilani Campus Lecture - XVI BITS Pilani, Pilani Campus
SWAPSPACE MANAGEMENT SWAPSPACE 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. 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 of disk space to . 354
Lecture - XVI BITS Pilani, Pilani Campus Lecture - XVI BITS Pilani, Pilani Campus
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 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
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 . The action of moving a process out S.No. System Swap Space
out of the main memory. The need for swap space on a system 2. Linux Swap space is double the amount of
can vary from megabytes to gigabytes. Still, it also depends on physical memory.
the amount of physical memory, the virtual memory it is backing, 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, 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 for 357 358
Lecture - XVI BITS Pilani, Pilani Campus Lecture - XVI BITS Pilani, Pilani Campus
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 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
363 364
Lecture - XVI BITS Pilani, Pilani Campus Lecture - XVI BITS Pilani, Pilani Campus
RAID RAID
RAID RAID
RAID – 3 Configuration
367 368
Lecture - XVI BITS Pilani, Pilani Campus Lecture - XVI BITS Pilani, Pilani Campus
RAID RAID
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
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
Problem 2:A certain moving arm disk storage with one head has the Time taken for one full rotation
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?
373 = 1/2 x 10 msec = 5 msec 374
Lecture - XVI BITS Pilani, Pilani Campus Lecture - XVI BITS Pilani, Pilani Campus
RAID RAID
377
Lecture - XVI BITS Pilani, Pilani Campus