Os Notes
Os Notes
Os Notes
Advanced OS
MANC404
Unit 1: Introdution
An Operating System (OS) is an interface between a computer user and computer hardware. An
operating system is a software which performs all the basic tasks like file management, memory
management, process management, handling input and output, and controlling peripheral devices
such as disk drives and printers.
Some popular Operating Systems include Linux Operating System, Windows Operating System,
VMS, OS/400, AIX, z/OS, etc.
Definition
An operating system is a program that acts as an interface between the user and the computer
hardware and controls the execution of all kinds of programs.
Memory Management
Processor Management
Device Management
File Management
Security
Control over system performance
Job accounting
Error detecting aids
Coordination between other software and users
Memory Management
Memory management refers to management of Primary Memory or Main Memory. Main memory is
a large array of words or bytes where each word or byte has its own address.
Main memory provides a fast storage that can be accessed directly by the CPU. For a program to be
executed, it must in the main memory. An Operating System does the following activities for
memory management −
Keeps tracks of primary memory, i.e., what part of it are in use by whom, what part are not
in use.
In multiprogramming, the OS decides which process will get memory when and how much.
De-allocates the memory when a process no longer needs it or has been terminated.
Processor Management
In multiprogramming environment, the OS decides which process gets the processor when and for
how much time. This function is called process scheduling. An Operating System does the
following activities for processor management −
Keeps tracks of processor and status of process. The program responsible for this task is
known as traffic controller.
Device Management
An Operating System manages device communication via their respective drivers. It does the
following activities for device management −
Keeps tracks of all devices. Program responsible for this task is known as the I/O
controller.
Decides which process gets the device when and for how much time.
De-allocates devices.
File Management
A file system is normally organized into directories for easy navigation and usage. These directories
may contain files and other directions.
An Operating System does the following activities for file management −
Keeps track of information, location, uses, status etc. The collective facilities are often
known as file system.
Control over system performance − Recording delays between request for a service and
response from the system.
Job accounting − Keeping track of time and resources used by various jobs and users.
Error detecting aids − Production of dumps, traces, error messages, and other debugging
and error detecting aids.
Problem of reliability.
Question of security and integrity of user programs and data.
Problem of data communication.
With resource sharing facility, a user at one site may be able to use the resources available at
another.
Speedup the exchange of data with one another via electronic mail.
If one site fails in a distributed system, the remaining sites can potentially continue
operating.
Better service to the customers.
Reduction of the load on the host computer.
Reduction of delays in data processing.
1) User View:-
The user view depends on the system interface that is used by the users. The different types of user
view experiences can be explained as follows:
If the user is using a personal computer, the operating system is largely designed to make the
interaction easy. Some attention is also paid to the performance of the system, but there is no
need for the operating system to worry about resource utilization. This is because the
personal computer uses all the resources available and there is no sharing.
If the user is using a system connected to a mainframe or a minicomputer, the operating
system is largely concerned with resource utilization. This is because there may be multiple
terminals connected to the mainframe and the operating system makes sure that all the
resources such as CPU,memory, I/O devices etc. are divided uniformly between them.
If the user is sitting on a workstation connected to other workstations through networks, then
the operating system needs to focus on both individual usage of resources and sharing
though the network. This happens because the workstation exclusively uses its own
resources but it also needs to share files etc. with other workstations across the network.
If the user is using a handheld computer such as a mobile, then the operating system handles
the usability of the device including a few remote operations. The battery level of the device
is also taken into account.
There are some devices that contain very less or no user view because there is no interaction with
the users. Examples are embedded computers in home devices, automobiles etc.
The system views the operating system as a resource allocator. There are many resources
such as CPU time, memory space, file storage space, I/O devices etc. that are required by
processes for execution. It is the duty of the operating system to allocate these resources
judiciously to the processes so that the computer system can run as smoothly as possible.
The operating system can also work as a control program. It manages all the processes and
I/O devices so that the computer system works smoothly and there are no errors. It makes
sure that the I/O devices work in a proper manner without creating problems.
Operating systems can also be viewed as a way to make using hardware easier.
Computers were required to easily solve user problems. However it is not easy to work
directly with the computer hardware. So, operating systems were developed to easily
communicate with the hardware.
An operating system can also be considered as a program running at all times in the
background of a computer system (known as the kernel) and handling all the application
programs. This is the definition of the operating system that is generally followed.
* System Calls:-
The interface between a process and an operating system is provided by system calls. In general,
system calls are available as assembly language instructions. They are also included in the manuals
used by the assembly level programmers. System calls are usually made when a process in user
mode requires access to a resource. Then it requests the kernel to provide the resource via a system
call.
A figure representing the execution of the system call is given as follows:
As can be seen from this diagram, the processes execute normally in the user mode until a system
call interrupts this. Then the system call is executed on a priority basis in the kernel mode. After the
execution of the system call, the control returns to the user mode and execution of user processes
can be resumed.
In general, system calls are required in the following situations:
If a file system requires the creation or deletion of files. Reading and writing from files also
require a system call.
Creation and management of new processes.
Network connections also require system calls. This includes sending and receiving packets.
Access to a hardware devices such as a printer, scanner etc. requires a system call.
1) Process Control
These system calls deal with processes such as process creation, process termination etc.
2)File Management
These system calls are responsible for file manipulation such as creating a file, reading a file,
writing into a file etc.
3)Device Management
These system calls are responsible for device manipulation such as reading from device buffers,
writing into device buffers etc.
4)Information Maintenance
These system calls handle information and its transfer between the operating system and the user
program.
5)Communication
These system calls are useful for interprocess communication. They also deal with creating and
deleting a communication connection.
Some of the examples of all the above types of system calls in Windows and Unix are given as
follows:
CreateFile() open()
ReadFile() read()
File Management
WriteFile() write()
CloseHandle() close()
SetConsoleMode() ioctl()
ReadConsole() read()
Device Management
WriteConsole() write()
GetCurrentProcessID() getpid()
SetTimer() alarm()
Information Maintenance
Sleep() sleep()
CreatePipe() pipe()
CreateFileMapping() shmget()
Communication
MapViewOfFile() mmap()
* Concept of Virtual Machine:-
A virtual machine (VM) is a software program or operating system that not only exhibits the
behavior of a separate computer, but is also capable of performing tasks such as running
applications and programs like a separate computer. A virtual machine, usually known as a guest is
created within another computing environment referred as a "host." Multiple virtual machines can
exist within a single host at one time.
A virtual machine is also known as a guest.
Virtual machines are becoming more common with the evolution of virtualization technology.
Virtual machines are often created to perform certain tasks that are different than tasks performed in
a host environment.
Virtual machines are implemented by software emulation methods or hardware virtualization
techniques. Depending on their use and level of correspondence to any physical computer, virtual
machines can be divided into two categories:
1. System Virtual Machines: A system platform that supports the sharing of the host computer's
physical resources between multiple virtual machines, each running with its own copy of the
operating system. The virtualization technique is provided by a software layer known as a
hypervisor, which can run either on bare hardware or on top of an operating system.
2. Process Virtual Machine: Designed to provide a platform-independent programming
environment that masks the information of the underlying hardware or operating system and
allows program execution to take place in the same way on any given platform.
Some of the advantages of a virtual machine include:
Allows multiple operating system environments on a single physical computer without any
intervention
Virtual machines are widely available and are easy to manage and maintain.
Offers application provisioning and disaster recovery options
Some of the drawbacks of virtual machines include:
They are not as efficient as a physical computer because the hardware resources are
distributed in an indirect way.
Multiple VMs running on a single physical machine can deliver unstable performance
*Interrupt concept:-
When a Process is executed by the CPU and when a user Request for another Process then this will
create disturbance for the Running Process. This is also called as the Interrupt.
Interrupts can be generated by User, Some Error Conditions and also by Software’s and the
hardware’s. But CPU will handle all the Interrupts very carefully because when Interrupts are
generated then the CPU must handle all the Interrupts Very carefully means the CPU will also
Provides Response to the Various Interrupts those are generated. So that When an interrupt has
Occurred then the CPU will handle by using the Fetch, decode and Execute Operations.
nterrupt is a signal which has highest priority from hardware or software which processor should
process its signal immediately.
Types of Interrupts:
Although interrupts have highest priority than other signals, there are many type of interrupts but
basic type of interrupts are
1. Hardware Interrupts: If the signal for the processor is from external device or hardware is
called hardware interrupts. Example: from keyboard we will press the key to do some action
this pressing of key in keyboard will generate a signal which is given to the processor to do
action, such interrupts are called hardware interrupts. Hardware interrupts can be classified
into two types they are
Maskable Interrupt: The hardware interrupts which can be delayed when a much
highest priority interrupt has occurred to the processor.
Non Maskable Interrupt: The hardware which cannot be delayed and should
process by the processor immediately.
2. Software Interrupts: Software interrupt can also divided in to two types. They are
Normal Interrupts: the interrupts which are caused by the software instructions are
called software instructions.
Exception: unplanned interrupts while executing a program is called Exception. For
example: while executing a program if we got a value which should be divided by
zero is called a exception.
Classification of Interrupts According to Periodicity of Occurrence:
1. Periodic Interrupt: If the interrupts occurred at fixed interval in timeline then that
interrupts are called periodic interrupts
2. Aperiodic Interrupt: If the occurrence of interrupt cannot be predicted then that interrupt is
called aperiodic interrupt.
Classification of Interrupts According to the Temporal Relationship with System Clock:
1. Synchronous Interrupt: The source of interrupt is in phase to the system clock is called
synchronous interrupt. In other words interrupts which are dependent on the system clock.
Example: timer service that uses the system clock.
2. Asynchronous Interrupts: If the interrupts are independent or not in phase to the system
clock is called asynchronous interrupt.
Interrupt Handling:
We know that instruction cycle consists of fetch, decode, execute and read/write functions. After
every instruction cycle the processor will check for interrupts to be processed if there is no interrupt
is present in the system it will go for the next instruction cycle which is given by the instruction
register.
If there is an interrupt present then it will trigger the interrupt handler, the handler will stop the
present instruction which is processing and save its configuration in a register and load the program
counter of the interrupt from a location which is given by the interrupt vector table. After processing
the interrupt by the processor interrupt handler will load the instruction and its configuration from
the saved register, process will start its processing where it’s left. This saving the old instruction
processing configuration and loading the new interrupt configuration is also called as context
switching.
The interrupt handler is also called as Interrupt service routine (ISR). There are different types of
interrupt handler which will handle different interrupts. For example for the clock in a system will
have its interrupt handler, keyboard it will have its interrupt handler for every device it will have its
interrupt handler.
The main features of the ISR are
Interrupts can occur at any time they are asynchronous. ISR’s can call for asynchronous
interrupts.
Interrupt service mechanism can call the ISR’s from multiple sources.
ISR’s can handle both maskable and non maskable interrupts. An instruction in a program
can disable or enable an interrupt handler call.
ISR on beginning of execution it will disable other devices interrupt services. After
completion of the ISR execution it will re initialize the interrupt services.
The nested interrupts are allowed in ISR for diversion to other ISR.
Unit 2. Process Management:
* Process Concept:-
A process is basically a program in execution. The execution of a process must progress in a
sequential fashion.
A process is defined as an entity which represents the basic unit of work to be
implemented in the system.
To put it in simple terms, we write our computer programs in a text file and when we execute this
program, it becomes a process which performs all the tasks mentioned in the program.
When a program is loaded into the memory and it becomes a process, it can be divided into four
sections ─ stack, heap, text and data. The following image shows a simplified layout of a process
inside main memory −
1 The process Stack contains the temporary data such as method/function parameters, return
address and local variables.
Heap
2
This is dynamically allocated memory to a process during its run time.
Text
3 This includes the current activity represented by the value of Program Counter and the
contents of the processor's registers.
Data
4
This section contains the global and static variables.
Ready
The process is waiting to be assigned to a processor. Ready processes are waiting to have the
2 processor allocated to them by the operating system so that they can run. Process may come
into this state after Start state or while running it by but interrupted by the scheduler to assign
CPU to some other process.
Running
3 Once the process has been assigned to a processor by the OS scheduler, the process state is
set to running and the processor executes its instructions.
Waiting
4 Process moves into the waiting state if it needs to wait for a resource, such as waiting for user
input, or waiting for a file to become available.
Terminated or Exit
5 Once the process finishes its execution, or it is terminated by the operating system, it is
moved to the terminated state where it waits to be removed from main memory.
Process privileges
2
This is required to allow/disallow access to system resources.
Process ID
3
Unique identification for each of the process in the operating system.
Pointer
4
A pointer to parent process.
Program Counter
5 Program Counter is a pointer to the address of the next instruction to be executed for this
process.
CPU registers
6
Various CPU registers where process need to be stored for execution for running state.
8 This includes the information of page table, memory limits, Segment table depending on
memory used by the operating system.
Accounting information
9
This includes the amount of CPU used for process execution, time limits, execution ID etc.
IO status information
10
This includes a list of I/O devices allocated to the process.
The architecture of a PCB is completely dependent on Operating System and may contain different
information in different operating systems. Here is a simplified diagram of a PCB −
The PCB is maintained for a process throughout its lifetime, and is deleted once the process
terminates.
The process scheduling is the activity of the process manager that handles the removal of the
running process from the CPU and the selection of another process on the basis of a particular
strategy.
Process scheduling is an essential part of a Multiprogramming operating systems. Such operating
systems allow more than one process to be loaded into the executable memory at a time and the
loaded process shares the CPU using time multiplexing.
Process Scheduling
The OS maintains all PCBs in Process Scheduling Queues. The OS maintains a separate queue for
each of the process states and PCBs of all processes in the same execution state are placed in the
same queue. When the state of a process is changed, its PCB is unlinked from its current queue and
moved to its new state queue.
The Operating System maintains the following important process scheduling queues −
Job queue − This queue keeps all the processes in the system.
Ready queue − This queue keeps a set of all processes residing in main memory, ready and
waiting to execute. A new process is always put in this queue.
Device queues − The processes which are blocked due to unavailability of an I/O device
constitute this queue.
The OS can use different policies to manage each queue (FIFO, Round Robin, Priority, etc.). The
OS scheduler determines how to move processes between the ready and run queues which can only
have one entry per processor core on the system; in the above diagram, it has been merged with the
CPU.
Schedulers
Schedulers are special system software which handle process scheduling in various ways. Their
main task is to select the jobs to be submitted into the system and to decide which process to run.
Schedulers are of three types −
Long-Term Scheduler
Short-Term Scheduler
Medium-Term Scheduler
Process Operations
1)Process Creation
In general-purpose systems, some way is needed to create processes as needed during operation.
There are four principal events led to processes creation.
System initialization.
Execution of a process Creation System calls by a running process.
A user request to create a new process.
Initialization of a batch job.
Foreground processes interact with users. Background processes that stay in background
sleeping but suddenly springing to life to handle activity such as email, webpage, printing,
and so on. Background processes are called daemons. This call creates an exact clone of the
calling process.
A process may create a new process by some create process such as 'fork'. It choose to does
so, creating process is called parent process and the created one is called the child processes.
Only one parent is needed to create a child process. Note that unlike plants and animals that
use sexual representation, a process has only one parent. This creation of process (processes)
yields a hierarchical structure of processes like one in the figure. Notice that each child has
only one parent but each parent may have many children. After the fork, the two processes,
the parent and the child, have the same memory image, the same environment strings and
the same open files. After a process is created, both the parent and child have their own
distinct address space. If either process changes a word in its address space, the change is
not visible to the other process.
<Figure 3.2 pp.55 From Dietel>
Following are some reasons for creation of a process
Some program starts another process, e.g., Netscape calls xv to display a picture.
2) Process Termination
A process terminates when it finishes executing its last statement. Its resources are returned to the
system, it is purged from any system lists or tables, and its process control block (PCB) is erased
i.e., the PCB's memory space is returned to a free memory pool. The new process terminates the
existing process, usually due to following reasons:
Normal Exist Most processes terminates because they have done their job. This call is
exist in UNIX.
Error Exist When process discovers a fatal error. For example, a user tries to compile a
program that does not exist.
Fatal Error An error caused by process due to a bug in program for example, executing
an illegal instruction, referring non-existing memory or dividing by zero.
Killed by another Process A process executes a system call telling the Operating Systems
to terminate some other process. In UNIX, this call is kill. In some systems when a process
kills all processes it created are killed as well (UNIX does not work this way).
Client/Server communication involves two components, namely a client and a server. They are
usually multiple clients in communication with a single server. The clients send requests to the
server and the server responds to the client requests.
There are three main methods to client/server communication. These are given as follows:
1)Sockets
Sockets facilitate communication between two processes on the same machine or different
machines. They are used in a client/server framework and consist of the IP address and port number.
Many application protocols use sockets for data connection and data transfer between a client and a
server.
Socket communication is quite low-level as sockets only transfer an unstructured byte stream across
processes. The structure on the byte stream is imposed by the client and server applications.
A diagram that illustrates sockets is as follows:
CPU Scheduling:
Scheduling Concept:
CPU scheduling is a process which allows one process to use the CPU while the execution of
another process is on hold(in waiting state) due to unavailability of any resource like I/O etc,
thereby making full use of CPU. The aim of CPU scheduling is to make the system efficient, fast
and fair.
Whenever the CPU becomes idle, the operating system must select one of the processes in the
ready queue to be executed. The selection process is carried out by the short-term scheduler (or
CPU scheduler). The scheduler selects from among the processes in memory that are ready to
execute, and allocates the CPU to one of them.
Non-Preemptive Scheduling
Under non-preemptive scheduling, once the CPU has been allocated to a process, the process keeps
the CPU until it releases the CPU either by terminating or by switching to the waiting state.
This scheduling method is used by the Microsoft Windows 3.1 and by the Apple Macintosh
operating systems.
It is the only method that can be used on certain hardware platforms, because It does not require the
special hardware(for example: a timer) needed for preemptive scheduling.
Preemptive Scheduling
In this type of Scheduling, the tasks are usually assigned with priorities. At times it is necessary to
run a certain task that has a higher priority before another task although it is running. Therefore, the
running task is interrupted for some time and resumed later when the priority task has finished its
execution.
Scheduling Criteria:
There are many different criterias to check when considering the "best" scheduling algorithm, they
are:
CPU Utilization
To make out the best use of CPU and not to waste any CPU cycle, CPU would be working most of
the time(Ideally 100% of the time). Considering a real system, CPU usage should range from 40%
(lightly loaded) to 90% (heavily loaded.)
Throughput
It is the total number of processes completed per unit time or rather say total amount of work done
in a unit of time. This may range from 10/second to 1/hour depending on the specific processes.
Turnaround Time
It is the amount of time taken to execute a particular process, i.e. The interval from time of
submission of the process to the time of completion of the process(Wall clock time).
Waiting Time
The sum of the periods spent waiting in the ready queue amount of time a process has been waiting
in the ready queue to acquire get control on the CPU.
Load Average
It is the average number of processes residing in the ready queue waiting for their turn to get into
the CPU.
Response Time
Amount of time it takes from when a request was submitted until the first response is produced.
Remember, it is the time till the first response and not the completion of process execution(final
response).
In general CPU utilization and Throughput are maximized and other factors are reduced for proper
optimization
Scheduling Algorithms:
A Process Scheduler schedules different processes to be assigned to the CPU based on particular
scheduling algorithms. There are six popular process scheduling algorithms which we are going to
discuss in this chapter −
Easy to implement in Batch systems where required CPU time is known in advance.
Impossible to implement in interactive systems where required CPU time is not known.
The processer should know in advance how much time process will take.
Each process is assigned a priority. Process with highest priority is to be executed first and
so on.
Processes with same priority are executed on first come first served basis.
Priority can be decided based on memory requirements, time requirements or any other
resource requirement.
Wait time of each process is as follows −
The processor is allocated to the job closest to completion but it can be preempted by a
newer ready job with shorter time to completion.
Impossible to implement in interactive systems where required CPU time is not known.
It is often used in batch environments where short jobs need to give preference.
Once a process is executed for a given time period, it is preempted and other process
executes for a given time period.
Scheduling Evaluation:
The first thing we need to decide is how we will evaluate the algorithms. To do this we need to
decide on the relative importance of the factors we listed above (Fairness, Efficiency, Response
Times, Turnaround and Throughput). Only once we have decided on our evaluation method can we
carry out the evaluation.
1)Deterministic Modeling
This evaluation method takes a predetermined workload and evaluates each algorithm using that
workload.
The advantages of deterministic modeling is that it is exact and fast to compute. The disadvantage is
that it is only applicable to the workload that you use to test. As an example, use the above
workload but assume P1 only has a burst time of 8 milliseconds. What does this do to the average
waiting time?
Of course, the workload might be typical and scale up but generally deterministic modeling is too
specific and requires too much knowledge about the workload.
Queuing Models
Another method of evaluating scheduling algorithms is to use queuing theory. Using data from real
processes we can arrive at a probability distribution for the length of a burst time and the I/O times
for a process. We can now generate these times with a certain distribution.
We can also generate arrival times for processes (arrival time distribution).
If we define a queue for the CPU and a queue for each I/O device we can test the various scheduling
algorithms using queuing theory.
The main disadvantage of using queuing models is that it is not always easy to define realistic
distribution times and we have to make assumptions. This results in the model only being an
approximation of what actually happens.
Simulations
Rather than using queuing models we simulate a computer. A Variable, representing a clock is
incremented. At each increment the state of the simulation is updated.
Statistics are gathered at each clock tick so that the system performance can be analysed.
The data to drive the simulation can be generated in the same way as the queuing model, although
this leads to similar problems.
Alternatively, we can use trace data. This is data collected from real processes on real machines and
is fed into the simulation. This can often provide good results and good comparisons over a range of
scheduling algorithms.
However, simulations can take a long time to run, can take a long time to implement and the trace
data may be difficult to collect and require large amounts of storage.
Unit III: Process Synchronization & Deadlocks:
Process Synchronization
Process Synchronization means sharing system resources by processes in a such a way that,
Concurrent access to shared data is handled thereby minimizing the chance of inconsistent data.
Maintaining data consistency demands mechanisms to ensure synchronized execution of
cooperating processes.
Process Synchronization was introduced to handle problems that arose while multiple process
executions. Some of the problems are discussed below.
Monitors:
Monitor is one of the ways to achieve Process synchronization. Monitor is supported by
programming languages to achieve mutual exclusion between processes. For example Java
Synchronized methods. Java provides wait() and notify() constructs.
1. It is the collection of condition variables and procedures combined together in a special kind of
module or a package.
2. The processes running outside the monitor can’t access the internal variable of monitor but can
call procedures of the monitor.
3. Only one process at a time can execute code inside monitors.
Syntax of Monitor
Condition Variables
Two different operations are performed on the condition variables of the monitor.
Wait.
signal.
Deadlock Concept:
A process in operating systems uses different resources and uses resources in following way.
1) Requests a resource
2) Use the resource
2) Releases the resource
Deadlock is a situation where a set of processes are blocked because each process is holding a
resource and waiting for another resource acquired by some other process.
Consider an example when two trains are coming toward each other on same track and there is only
one track, none of the trains can move once they are in front of each other. Similar situation occurs
in operating systems when there are two or more processes hold some resources and wait for
resources held by other(s). For example, in the below diagram, Process 1 is holding Resource 1 and
waiting for resource 2 which is acquired by process 2, and process 2 is waiting for resource 1.
Deadlock can arise if following four conditions hold simultaneously (Necessary Conditions)
1) Mutual Exclusion: One or more than one resource are non-sharable (Only one process can use
at a time)
2) Hold and Wait: A process is holding at least one resource and waiting for resources.
3) No Preemption: A resource cannot be taken from a process unless the process releases the
resource.
4) Circular Wait: A set of processes are waiting for each other in circular form.
Methods for handling deadlock
There are three ways to handle deadlock
1) Deadlock prevention or avoidance: The idea is to not let the system into deadlock state.
2) Deadlock detection and recovery: Let deadlock occur, then do preemption to handle it once
occurred.
3) Ignore the problem all together: If deadlock is very rare, then let it happen and reboot the system.
This is the approach that both Windows and UNIX take.
Deadlock Prevention:
We have gone through what is deadlock in os, necessary conditions for deadlock to occur. Now, we
will see how to handle deadlock if occurred. At first, we will see how to handle deadlock by
deadlock prevention technique.
Deadlock can be prevented by preventing at least one of the four required conditions. Either try to
remove all four conditions or one of the four conditions. So, we can easily prevent deadlock from
the system.
Let’s see how we can avoid either one or all conditions from the system –
No Mutual Exclusion: Make the resources are shareable. It will be helpful to eliminate mutual
exclusion. Unfortunately, some resources can be never be shareable such as printers and tape drives.
It needs exclusive access by a single process.
No Hold and wait: When process starts up the execution, allocate all the required resources. Then,
hold and wait condition won’t be existing. But, practically it is less possible. For example, if a
process requires a printer at a later time and we have allocated printer before the start of the its
execution. So, it can be wasteful for system resources.
It must be mandatory for the processes holding resources to release them before requesting new
resources. Then, re-acquire the released resources along with a new one. It can lead to starvation if
process requires one or more resources.
No Preemption: If a process holding some resources and requests another resource that can’t be
immediately allocated to it (that is, the process must wait), then, all resources currently being held
are preempted.
Circular Wait: A total ordering of all resource types should be imposed. Each process requests any
resource in an increasing order of enumeration.
Let R = {R1, R2……Rn} be the set of resource types. Assign to each resource type a unique integer
number.
For example, R = {tape drives, printers, disk drives}, then the function F might be as follows:
F(tape drives) = 2
F(printers) = 5
F(disk drives) = 18
If the below protocols are used, then the circular wait condition cannot hold.
Each process can request only in an increasing order of enumeration i.e. A process, holding
resource type Ri, can request instance of resource type Rj if and only if F(Rj) > F(Ri).
A process requests resource type Rj if it has released any resource Ri.
Deadlock avoidance
As you saw already, most prevention algorithms have poor resource utilization, and hence result in
reduced through puts. Instead, we can try to avoid deadlocks by making use prior knowledge about
the usage of resources by processes including resources available, resources allocated, future
requests and future releases by processes. Most deadlock avoidance algorithms need every process
to tell in advance the maximum number of resources of each type that it may need. Based on all
these info we may decide if a process should wait for a resource or not, and thus avoid chances for
circular wait.
If a system is already in a safe state, we can try to stay away from an unsafe state and avoid
deadlock. Deadlocks cannot be avoided in an unsafe state. A system can be considered to be in safe
state if it is not in a state of deadlock and can allocate resources upto the maximum available. A safe
sequence of processes and allocation of resources ensures a safe state. Deadlock avoidance
algorithms try not to allocate resources to a process if it will make the system in an unsafe state.
Since resource allocation is not done right away in some cases, deadlock avoidance algorithms also
suffer from low resource utilization problem.
A resource allocation graph is generally used to avoid deadlocks. If there are no cycles in the
resource allocation graph, then there are no deadlocks. If there are cycles, there may be a deadlock.
If there is only one instance of every resource, then a cycle implies a deadlock. Vertices of the
resource allocation graph are resources and processes. The resource allocation graph has request
edges and assignment edges. An edge from a process to resource is a request edge and an edge from
a resource to process is an allocation edge. A calm edge denotes that a request may be made in
future and is represented as a dashed line. Based on calm edges we can see if there is a chance for a
cycle and then grant requests if the system will again be in a safe state.
Consider the image with calm edges as below:
If R2 is allocated to p2 and if P1 request for R2, there will be a deadlock.
The resource allocation graph is not much useful if there are multiple instances for a resource. In
such a case, we can use Banker’s algorithm. In this algorithm, every process must tell upfront the
maximum resource of each type it need, subject to the maximum available instances for each type.
Allocation of resources is made only, if the allocation ensures a safe state; else the processes need to
wait. The Banker’s algorithm can be divided into two parts: Safety algorithm if a system is in a safe
state or not. The resource request algorithm make an assumption of allocation and see if the system
will be in a safe state. If the new state is unsafe, the resources are not allocated and the data
structures are restored to their previous state; in this case the processes must wait for the resource.
You can refer to any operating system text books for details of these algorithms.
Deadlock Detection
If deadlock prevention and avoidance are not done properly, as deadlock may occur and only things
left to do is to detect the recover from the deadlock.
If all resource types has only single instance, then we can use a graph called wait-for-graph, which
is a variant of resource allocation graph. Here, vertices represent processes and a directed edge from
P1 to P2 indicate that P1 is waiting for a resource held by P2. Like in the case of resource
allocation graph, a cycle in a wait-for-graph indicate a deadlock. So the system can maintain a wait-
for-graph and check for cycles periodically to detect any deadlocks.
The wait-for-graph is not much useful if there are multiple instances for a resource, as a cycle may
not imply a deadlock. In such a case, we can use an algorithm similar to Banker’s algorithm to
detect deadlock. We can see if further allocations can be made on not based on current allocations.
You can refer to any operating system text books for details of these algorithms.
Deadlock Recovery
Once a deadlock is detected, you will have to break the deadlock. It can be done through different
ways, including, aborting one or more processes to break the circular wait condition causing the
deadlock and preempting resources from one or more processes which are deadlocked.
When deadlock detected, then our system stops working, and after the recovery of the deadlock, our
system start working again.
Therefore, after the detection of deadlock, a method/way must require to recover that deadlock to
run the system again. The method/way is called as deadlock recovery.
Here are various ways of deadlock recovery that we will discuss briefly in this tutorial.
Memory management is the functionality of an operating system which handles or manages primary
memory and moves processes back and forth between main memory and disk during execution.
Memory management keeps track of each and every memory location, regardless of either it is
allocated to some process or it is free. It checks how much memory is to be allocated to processes.
It decides which process will get memory at what time. It tracks whenever some memory gets freed
or unallocated and correspondingly it updates the status.
Main Memory refers to a physical memory that is the internal memory to the computer. The word
main is used to distinguish it from external mass storage devices such as disk drives. Main memory
is also known as RAM. The computer is able to change only data that is in main memory.
Therefore, every program we execute and every file we access must be copied from a storage
device into main memory.
All the programs are loaded in the main memeory for execution. Sometimes complete program is
loaded into the memory, but some times a certain part or routine of the program is loaded into the
main memory only when it is called by the program, this mechanism is called Dynamic Loading,
this enhance the performance.
Also, at times one program is dependent on some other program. In such a case, rather than loading
all the dependent programs, CPU links the dependent programs to the main executing program
when its required. This mechanism is known as Dynamic Linking.
Basis for
Compariso Logical Address Physical Address
n
It is the virtual address The physical address is a
Basic
generated by CPU location in a memory unit.
Set of all logical addresses
Set of all physical addresses
generated by CPU in
Address mapped to the corresponding
reference to a program is
Space logical addresses is referred as
referred as Logical Address
Physical Address.
Space.
The user can view the logical The user can never view
Visibility
address of a program. physical address of program
The user uses the logical
The user can not directly access
Access address to access the physical
physical address.
address.
The Logical Address is Physical Address is Computed
Generation
generated by the CPU by MMU
The set of all logical addresses generated by a program is referred to as a logical address space.
The set of all physical addresses corresponding to these logical addresses is referred to as a physical
address space.
The runtime mapping from virtual to physical address is done by the memory management unit
(MMU) which is a hardware device. MMU uses following mechanism to convert virtual address to
physical address.
The value in the base register is added to every address generated by a user process, which is
treated as offset at the time it is sent to memory. For example, if the base register value is
10000, then an attempt by the user to use address location 100 will be dynamically
reallocated to location 10100.
The user program deals with virtual addresses; it never sees the real physical addresses.
Fragmentation
As processes are loaded and removed from memory, the free memory space is broken into little
pieces. It happens after sometimes that processes cannot be allocated to memory blocks considering
their small size and memory blocks remains unused. This problem is known as Fragmentation.
Fragmentation is of two types −
1 Total memory space is enough to satisfy a request or to reside a process in it, but it is not
contiguous, so it cannot be used.
Internal fragmentation
2 Memory block assigned to process is bigger. Some portion of memory is left unused, as it
cannot be used by another process.
The following diagram shows how fragmentation can cause waste of memory and a compaction
technique can be used to create more free memory out of fragmented memory −
External fragmentation can be reduced by compaction or shuffle memory contents to place all free
memory together in one large block. To make compaction feasible, relocation should be dynamic.
The internal fragmentation can be reduced by effectively assigning the smallest partition but large
enough for the process.
Paging
A computer can address more memory than the amount physically installed on the system. This
extra memory is actually called virtual memory and it is a section of a hard that's set up to emulate
the computer's RAM. Paging technique plays an important role in implementing virtual memory.
Paging is a memory management technique in which process address space is broken into blocks of
the same size called pages (size is power of 2, between 512 bytes and 8192 bytes). The size of the
process is measured in the number of pages.
Similarly, main memory is divided into small fixed-sized blocks of (physical) memory called
frames and the size of a frame is kept the same as that of a page to have optimum utilization of the
main memory and to avoid external fragmentation.
Address Translation
Page address is called logical address and represented by page number and the offset.
Logical Address = Page number + page offset
Frame address is called physical address and represented by a frame number and the offset.
Physical Address = Frame number + page offset
A data structure called page map table is used to keep track of the relation between a page of a
process to a frame in physical memory.
When the system allocates a frame to any page, it translates this logical address into a physical
address and create entry into the page table to be used throughout execution of the program.
When a process is to be executed, its corresponding pages are loaded into any available memory
frames. Suppose you have a program of 8Kb but your memory can accommodate only 5Kb at a
given point in time, then the paging concept will come into picture. When a computer runs out of
RAM, the operating system (OS) will move idle or unwanted pages of memory to secondary
memory to free up RAM for other processes and brings them back when needed by the program.
This process continues during the whole execution of the program where the OS keeps removing
idle pages from the main memory and write them onto the secondary memory and bring them back
when required by the program.
Advantages and Disadvantages of Paging
Here is a list of advantages and disadvantages of paging −
Paging reduces external fragmentation, but still suffer from internal fragmentation.
Due to equal size of the pages and frames, swapping becomes very easy.
Page table requires extra memory space, so may not be good for a system having small
RAM.
Segmentation
Segmentation is a memory management technique in which each job is divided into several
segments of different sizes, one for each module that contains pieces that perform related functions.
Each segment is actually a different logical address space of the program.
When a process is to be executed, its corresponding segmentation are loaded into non-contiguous
memory though every segment is loaded into a contiguous block of available memory.
Segmentation memory management works very similar to paging but here segments are of variable-
length where as in paging pages are of fixed size.
A program segment contains the program's main function, utility functions, data structures, and so
on. The operating system maintains a segment map table for every process and a list of free
memory blocks along with segment numbers, their size and corresponding memory locations in
main memory. For each segment, the table stores the starting address of the segment and the length
of the segment. A reference to a memory location includes a value that identifies a segment and an
offset.
Advantages of Segmentation
1. No internal fragmentation
2. Average Segment Size is larger than the actual page size.
3. Less overhead
4. It is easier to relocate segments than entire address space.
5. The segment table is of lesser size as compare to the page table in paging.
Disadvantages
1. It can have external fragmentation.
2. it is difficult to allocate contiguous memory to variable sized partition.
3. Costly memory management algorithms.
Paging VS Segmentation
Sr
Paging Segmentation
No.
1 Non-Contiguous memory allocation Non-contiguous memory allocation
Paging divides program into fixed size Segmentation divides program into variable
2
pages. size segments.
3 OS is responsible Compiler is responsible.
4 Paging is faster than segmentation Segmentation is slower than paging
5 Paging is closer to Operating System Segmentation is closer to User
6 It suffers from internal fragmentation It suffers from external fragmentation
7 There is no external fragmentation There is no external fragmentation
Logical address is divided into page number Logical address is divided into segment number
8
and page offset and segment offset
Page table is used to maintain the page Segment Table maintains the segment
9
information. information
Page table entry has the frame number and Segment table entry has the base address of the
10 some flag bits to represent details about segment and some protection bits for the
pages. segments.
Error handling code is not needed unless that specific error occurs, some of which are quite
rare.
Arrays are often over-sized for worst-case scenarios, and only a small fraction of the arrays
are actually used in practice.
Certain features of certain programs are rarely used.
Demand Paging:
The basic idea behind demand paging is that when a process is swapped in, its pages are not
swapped in all at once. Rather they are swapped in only when the process needs them(On demand).
This is termed as lazy swapper, although a pager is a more accurate term.
Initially only those pages are loaded which will be required the process immediately.
A demand paging system is quite similar to a paging system with swapping where processes reside
in secondary memory and pages are loaded only on demand, not in advance. When a context switch
occurs, the operating system does not copy any of the old program’s pages out to the disk or any of
the new program’s pages into the main memory Instead, it just begins executing the new program
after loading the first page and fetches that program’s pages as they are referenced.
While executing a program, if the program references a page which is not available in the main
memory because it was swapped out a little ago, the processor treats this invalid memory reference
as a page fault and transfers control from the program to the operating system to demand the page
back into the memory.
Advantages
Following are the advantages of Demand Paging −
Disadvantages
Number of tables and the amount of processor overhead for handling page interrupts are
greater than in the case of the simple paged management techniques.
The pages that are not moved into the memory, are marked as invalid in the page table. For an
invalid entry the rest of the table is empty. In case of pages that are loaded in the memory, they are
marked as valid along with the information about where to find the swapped out page.
When the process requires any of the page that is not loaded into the memory, a page fault trap is
triggered and following steps are followed,
1. The memory address which is requested by the process is first checked, to verify the request
made by the process.
2. If its found to be invalid, the process is terminated.
3. In case the request by the process is valid, a free frame is located, possibly from a free-frame
list, where the required page will be moved.
4. A new operation is scheduled to move the necessary page from disk to the specified memory
location. ( This will usually block the process on an I/O wait, allowing some other process to
use the CPU in the meantime. )
5. When the I/O operation is complete, the process's page table is updated with the new frame
number, and the invalid bit is changed to valid.
6. The instruction that caused the page fault must now be restarted from the beginning.
There are cases when no pages are loaded into the memory initially, pages are only loaded when
demanded by the process by generating page faults. This is called Pure Demand Paging.
The only major issue with Demand Paging is, after a new page is loaded, the process starts
execution from the beginning. Its is not a big issue for small programs, but for larger programs it
affects performance drastically.
Page Replacement
As studied in Demand Paging, only certain pages of a process are loaded initially into the memory.
This allows us to get more number of processes into the memory at the same time. but what
happens when a process requests for more pages and no free memory is available to bring them in.
Following steps can be taken to deal with this problem :
1. Put the process in the wait queue, until any other process finishes its execution thereby
freeing frames.
2. Or, remove some other process completely from the memory to free frames.
3. Or, find some pages that are not being used right now, move them to the disk to get free
frames. This technique is called Page replacement and is most commonly used. We have
some great algorithms to carry on page replacement efficiently.
Reference String
The string of memory references is called reference string. Reference strings are generated
artificially or by tracing a given system and recording the address of each memory reference. The
latter choice produces a large number of data, where we note two things.
For a given page size, we need to consider only the page number, not the entire address.
If we have a reference to a page p, then any immediately following references to page p will
never cause a page fault. Page p will be in memory after the first reference; the immediately
following references will not fault.
Oldest page in main memory is the one which will be selected for replacement.
Easy to implement, keep a list, replace pages from the tail and add new pages at the head.
this algorithms replaces the page which will not be referred for so long in future. Although it
can not be practically implementable but it can be used as a benchmark. Other algorithms
are compared to this in terms of optimality.
Easy to implement, keep a list, replace pages by looking back into time.
this algorithm replaces the page which has not been referred for a long time. This algorithm
is just opposite to the optimal page replacement algorithm. In this, we look at the past
instead of staring at future.
Page Fault:-
When the page (data) requested by a program is not available in the memory, it is called as a page
fault. This usually results in the application being shut down.
A page is a fixed length memory block used as a transferring unit between physical memory and an
external storage. A page fault occurs when a program accesses a page that has been mapped in
address space, but has not been loaded in the physical memory.
A page fault occurs when a program attempts to access a block of memory that is not stored in the
physical memory, or RAM. The fault notifies the operating system that it must locate the data in
virtual memory, then transfer it from the storage device, such as an HDD or the system RAM.
Though the term "page fault" sounds like an error, page faults are common and are part of the
normal way computers handle virtual memory. In programming terms, a page fault generates an
exception, which notifies the operating system that it must retrieve the memory blocks or "pages"
from virtual memory in order for the program to continue. Once the data is moved into physical
memory, the program continues as normal. This process takes place in the background and usually
goes unnoticed by the user.
File Management:
A file can be defined as a data structure which stores the sequence of records. Files are stored in a
file system, which may exist on a disk or in the main memory. Files can be simple (plain text) or
complex (specially-formatted).
The collection of files is known as Directory. The collection of directories at the different levels, is
known as File System.
Attributes of the File
1.Name
Every file carries a name by which the file is recognized in the file system. One directory cannot
have two files with the same name.
2.Identifier
Along with the name, Each File has its own extension which identifies the type of the file. For
example, a text file has the extension .txt, A video file can have the extension .mp4.
3.Type
In a File System, the Files are classified in different types such as video files, audio files, text files,
executable files, etc.
4.Location
In the File System, there are several locations on which, the files can be stored. Each file carries its
location as its attribute.
5.Size
The Size of the File is one of its most important attribute. By size of the file, we mean the number
of bytes acquired by the file in the memory.
6.Protection
The Admin of the computer may want the different protections for the different files. Therefore each
file carries its own set of permissions to the different group of Users.
7.Time and Date
Every file carries a time stamp which contains the time and date on which the file is last modified
File Structure:
A File Structure should be according to a required format that the operating system can understand.
An object file is a sequence of bytes organized into blocks that are understandable by the
machine.
When operating system defines different file structures, it also contains the code to support
these file structure. Unix, MS-DOS support minimum number of file structure.
File Type
File type refers to the ability of the operating system to distinguish different types of file such as
text files source files and binary files etc. Many operating systems support many types of files.
Operating system like MS-DOS and UNIX have the following types of files −
Ordinary files
These are the files that contain user information.
These may have text, databases or executable program.
The user can apply various operations on such files like add, modify, delete or even remove
the entire file.
Directory files
These files contain list of file names and other information related to these files.
Special files
These files are also known as device files.
These files represent physical device like disks, terminals, printers, networks, tape drive etc.
These files are of two types −
Block special files − data is handled in blocks as in the case of disks and tapes.
Protection
When information is kept in a computer system, a major concern is its protection from physical
damage (reliability) as well as improper access.
Types of access: In case of systems that don’t permit access to the files of other users. Protection
is not needed. So, one extreme is to provide protection by prohibiting access. The other extreme is
to provide free access with no protection. Both these approaches are too extreme for general use.
So, we need controlled access. It is provided by limiting the types of file access. Access is permitted
depending on several factors. One major factor is type of access requested. The different type of
operations that can be controlled are:
• Read
• Write
• Execute
• Append
• Delete
• List
Access lists and groups:
Various users may need different types of access to a file or directory. So, we can associate an
access
lists with each file and directory to implement identity dependent access. When a user access
requests access to a particular file, the OS checks the access list associated with that file. If that user
is granted the requested access, then the access is allowed. Otherwise, a protection violation occurs
& the user is denied access to the file. But the main problem with access lists is their length. It is
82 | P a g every tedious to construct such a list. So, we use a condensed version of the access list by
classifying
the users into 3 categories:
• Owners: The user who created the file.
• Group: A set of users who are sharing the files.
• Others: All other users in the system.
Here only 3 fields are required to define protection. Each field is a collection of bits each of which
either allows or prevents the access. E.g. The UNIX file system defines 3 fields of 3 bits each: rwx
• r( read access)
• w(write access)
• x(execute access)
Separate fields are kept for file owners, group & other users. So, a bit is needed to record protection
information for each file.
Directory Structure
Directory can be defined as the listing of the related files on the disk. The directory may store some
or the entire file attributes.
To get the benefit of different file systems on the different operating systems, A hard disk can be
divided into the number of partitions of different sizes. The partitions are also called volumes or
mini disks.
Each partition must have at least one directory in which, all the files of the partition can be listed. A
directory entry is maintained for each file in the directory which stores all the information related to
that file.
A directory can be viewed as a file which contains the Meta data of the bunch of files.
Every Directory supports a number of common operations on the file:
1. File Creation
2. Search for the file
3. File deletion
4. Renaming the file
5. Traversing Files
6. Listing of files
Single Level Directory
The simplest method is to have one big list of all the files on the disk. The entire system will
contain only one directory which is supposed to mention all the files present in the file system. The
directory contains one entry per each file present on the file system.
Advantages
1. Implementation is very simple.
2. If the sizes of the files are very small then the searching becomes faster.
3. File creation, searching, deletion is very simple since we have only one directory.
Disadvantages
1. We cannot have two files with the same name.
2. The directory may be very big therefore searching for a file may take so much time.
3. Protection cannot be implemented for multiple users.
4. There are no ways to group same kind of files.
5. Choosing the unique name for every file is a bit complex and limits the number of files in
the system because most of the Operating System limits the number of characters used to
construct the file name.
1. Bit Vector
In this approach, the free space list is implemented as a bit map vector. It contains the number of
bits where each bit represents each block.
If the block is empty then the bit is 1 otherwise it is 0. Initially all the blocks are empty therefore
each bit in the bit map vector contains 1.
LAs the space allocation proceeds, the file system starts allocating blocks to the files and setting the
respective bit to 0.
2. Linked List
It is another approach for free space management. This approach suggests linking together all the
free blocks and keeping a pointer in the cache which points to the first free block.
Therefore, all the free blocks on the disks will be linked together with a pointer. Whenever a block
gets allocated, its previous free block will be linked to its next free block.
File Allocation:-
Files are allocated disk spaces by operating system. Operating systems deploy following three main
ways to allocate disk space to files.
Contiguous Allocation
Linked Allocation
Indexed Allocation
Contiguous Allocation
Each file occupies a contiguous address space on disk.
Assigned disk address is in linear order.
Easy to implement.
External fragmentation is a major issue with this type of allocation technique.
Linked Allocation
Each file carries a list of links to disk blocks.
Directory contains link / pointer to first block of a file.
No external fragmentation
Effectively used in sequential access file.
Inefficient in case of direct access file.
Indexed Allocation
Provides solutions to problems of contiguous and linked allocation.
A 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.
The disk controller then transfers any sector requests to the operating system. Once blocks make it
from the disk controller into main memory, the operating system may cache the blocks there. Some
systems maintain a separate section of main memory for a buffer cache, where blocks are kept
under the assumption that they will be used again shortly. Other systems cache file data using a
page cache.
The page cache uses virtual memory techniques to cache file data as pages rather than as file-
system-oriented blocks. Caching file data using virtual addresses is far more efficient than caching
through physical disk blocks, as accesses interface with virtual memory rather than the file system.
Several systems—including Solaris, Linux, and Windows NT, 2000, and XP—use page caching to
cache both process pages and file data. This is known as unified virtual memory. Some versions of
UNIX and Linux provide a unified buffer cache. To illustrate the benefits of the unified buffer
cache, consider the two alternatives for opening and accessing a file. One approach is to use
memory mapping (Section 9.7); the second is to use the standard system calls readO and write 0 .
Without a unified buffer cache, we have a situation similar to Figure 11.11.
Here, the read() and write () system calls go through the buffer cache. The memory-mapping call,
however, requires using two caches—the page cache and the buffer cache. A memory mapping
proceeds by reading in disk blocks from the file system and storing them in the buffer cache.
Because the virtual memory system does not interface with the buffer cache, the contents of the file
in the buffer cache must be copied into the page cache. This situation is known as double caching
and requires caching file-system data twice. Not only does it waste memory but it also wastes
significant CPU and I/O cycles due to the extra data movement within, system memory.
In add ition, inconsistencies between the two caches can result in corrupt files. In contrast, when a
unifiedthe disk data structures and algorithms in MS-DOS had to be modified to allow larger file
systems. (Each FAT entry was expanded to 16 bits and later to 32 bits.) The initial file-system
decisions were made for efficiency reasons; however, with the advent of MS-DOS version 4,
millions of computer users were inconvenienced when they had to switch to the new, larger file
system. Sun's ZFS file system uses 128-bit pointers, which theoretically should never need to be
extended. (The minimum mass of a device capable of storing 2'2S bytes using atomic-level storage
would be about 272 trillion kilograms.) As another example, consider the evolution of Sun's Solaris
operating system.
Originally, many data structures were of fixed length, allocated at system startup. These structures
included the process table and the open-file table. When the process table became full, no more
processes could be created. When the file table became full, no more files could be opened. The
system would fail to provide services to users. Table sizes could be increased only by recompiling
the kernel and rebooting the system. Since the release of Solaris 2, almost all kernel structures have
been allocated dynamically, eliminating these artificial limits on system performance. Of course, the
algorithms that manipulate these tables are more complicated, and the operating system is a little
slower because it must dynamically allocate and deallocate table entries; but that price is the usual
one for more general, functionality.
Performance
Once a seek is performed, the track is read into the disk cache starting at the sector under the disk
head (reducing latency time). The disk controller then transfers any sector requests to the operating
system. Once blocks make it from the disk controller into main memory, the operating system may
cache the blocks there. Some systems maintain a separate section of main memory for a buffer
cache, where blocks are kept under the assumption that they will be used again shortly. Other
systems cache file data using a page cache.
The page cache uses virtual memory techniques to cache file data as pages rather than as file-
system-oriented blocks. Caching file data using virtual addresses is far more efficient than caching
through physical disk blocks, as accesses interface with virtual memory rather than the file system.
Several systems—including Solaris, Linux, and Windows NT, 2000, and XP—use page caching to
cache both process pages and file data. This is known as unified virtual memory. Some versions of
UNIX and Linux provide a unified buffer cache.
This situation is known as double caching and requires caching file-system data twice. Not only
does it waste memory but it also wastes significant CPU and I/O cycles due to the extra data
movement within, system memory. In add ition, inconsistencies between the two caches can result
in corrupt files. In contrast, when a unified buffer cache is provided, both memory mapping and the
read () and write () system calls use the same page cache. This has the benefit of avoiding double
caching, and it allows the virtual memory system to manage file-system data. The unified buffer
cache is shown in Figure 11.12. Regardless of whether we are caching disk blocks or pages (or
both), LEU (Section 9.4.4) seems a reasonable general-purpose algorithm for block or page
replacement. However, the evolution of the Solaris page-caching algorithms reveals the difficulty in
choosing an algorithm. Solaris allows processes and the page cache to share unused inemory.
There are other issvies that can affect the performance of I/O such as whether writes to the file
system occur synchronously or asynchronously. Synchronous writes occur in the order in which the
disk subsystem receives them, and the writes are not buffered. Thus, the calling routine must wait
for the data to reach the disk drive before it can proceed. Asynchronous writes are done the majority
of the time. In an asynchronous write, the data are stored in the cache, and control returns to the
caller. Metadata writes, among others, can be synchronous.
Operating systems frequently include a flag in the open system call to allow a process to request
that writes be performed synchronously. For example, databases use this feature for atomic
transactions, to assure that data reach stable storage in the required order. Some systems optimize
their page cache by using different replacement algorithms, depending on the access type of the file.
A file being read or written sequentially should not have its pages replaced in LRU order, because
the most 11.7 Recovery 435 recently used page will be used last, or perhaps never again. Instead,
sequential access can be optimized by techniques known as free-behind and read-ahead. Free-
behind removes a page from the buffer as soon as the next page is requested. The previous pages are
not likely to be used again and waste buffer space. With read-ahead, a requested page and several
subsequent pages are read and cached. These pages are likely to be requested after the current page
is processed.
Retrieving these data from the disk in one transfer and caching them saves a considerable amount of
time. One might think a track cache on the controller eliminates the need for read-ahead on a
multiprogrammed system. However, because of the high latency and overhead involved in making
many small transfers from the track cache to main memory, performing a read-ahead remains
beneficial. The page cache, the file system, and the disk drivers have some interesting interactions.
When data are written to a disk file, the pages are buffered in the cache, and the disk driver sorts its
output queue according to disk address. These two actions allow the disk driver to minimize disk-
head seeks and to write data at times optimized for disk rotation.
Unless synchronous writes are required, a process writing to disk simply writes into the cache, and
the system asynchronously writes the data to disk when convenient. The user process sees very fast
writes. When data are read from a disk file, the block I/O system does some read-ahead; however,
writes are much more nearly asynchronous than are reads. Thus, output to the disk through the file
system is often faster than is input for large transfers, counter to intuition
Recovery:
Files and directories are kept both in main memory and on disk, and care must taken to ensure that
system failure does not result in loss of data or in data inconsistency. We deal with these issues in
the following sections.
Consistency Checking
some directory information is kept in main memory (or cache) to speed up access. The directory
information in main memory is generally more up to date than is the corresponding information on
the disk, because cached directory information is not necessarily written to disk as soon as the
update takes place.
Consider, then, the possible effect of a computer crash. Cache and buffetcontents, as well as I/O
operations in progress, can be lost, and with them any changes in the directories of opened files.
Such an event can leave the file system in an inconsistent state: The actual state of some files is not
as described in the directory structure. Frequently, a special program is run at reboot time to check
for and correct disk inconsistencies.
The consistency checker—a systems program such as f sck in UNIX or chkdsk in MS-DOS—
compares the data in the directory structure with the data blocks on disk and tries to fix any
inconsistencies it finds. The allocation and free-space-management algorithms dictate what types of
problems the checker can find and how successful it will be in fixing them. For instance, if linked
allocation is used and there is a link from any block to its next block, then the entire file can be
reconstructed from the data blocks, and the directory structure can be recreated.
In contrast, the loss of a directory entry on an indexed allocation system can be disastrous, because
the data blocks have no knowledge of one another. For this reason, UNIX caches directory entries
for reads; but any data write that results in space allocation, or other metadata changes, is done
synchronously, before the corresponding data blocks are written. Of course, problems can still occur
if a synchronous write is interrupted by a crash.
Backup and Restore
Magnetic disks sometimes fail, and care must be taken to ensure that the data lost in such a failure
are not lost forever. To this end, system programs can be used to back up data from disk to another
storage device, such as a floppy disk, magnetic tape, optical disk, or other hard disk.
Recovery from the loss of an individual file, or of an entire disk, may then be a matter of restoring
the data from backup. To minimize the copying needed, we can use information from each file's
directory entry. For instance, if the backup program knows when the last backup of a file was done,
and the file's last write date in the directory indicates that the file has not changed since that date,
then the file does not need to be copied again.