Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Os Notes

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 67

MCA I SEM

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.

Following are some of important functions of an operating System.

 Memory Management
 Processor Management
 Device Management
 File Management
 Security
 Control over system performance
 Job accounting
 Error detecting aids
 Coordination between other software and users

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.

 Allocates the memory when a process requests it to do so.

 De-allocates the memory when a process no longer needs it or has been terminated.

Processor Management
In multiprogramming environment, the OS decides which process gets the processor when and for
how much time. This function is called process scheduling. An Operating System does the
following activities for processor management −

 Keeps tracks of processor and status of process. The program responsible for this task is
known as traffic controller.

 Allocates the processor (CPU) to a process.

 De-allocates processor when a process is no longer required.

Device Management
An Operating System manages device communication via their respective drivers. It does the
following activities for device management −

 Keeps tracks of all devices. Program responsible for this task is known as the I/O
controller.

 Decides which process gets the device when and for how much time.

 Allocates the device in the efficient way.

 De-allocates devices.

File Management
A file system is normally organized into directories for easy navigation and usage. These directories
may contain files and other directions.
An Operating System does the following activities for file management −

 Keeps track of information, location, uses, status etc. The collective facilities are often
known as file system.

 Decides who gets the resources.


 Allocates the resources.

 De-allocates the resources.

* Other Important Activities:


Following are some of the important activities that an Operating System performs −

 Security − By means of password and similar other techniques, it prevents unauthorized


access to programs and data.

 Control over system performance − Recording delays between request for a service and
response from the system.

 Job accounting − Keeping track of time and resources used by various jobs and users.

 Error detecting aids − Production of dumps, traces, error messages, and other debugging
and error detecting aids.

 Coordination between other softwares and users − Coordination and assignment of


compilers, interpreters, assemblers and other software to the various users of the computer
systems.

* Types of Operating System:


Operating systems are there from the very first computer generation and they keep evolving with
time. In this chapter, we will discuss some of the important types of operating systems which are
most commonly used.

1) Batch operating system:-


The users of a batch operating system do not interact with the computer directly. Each user prepares
his job on an off-line device like punch cards and submits it to the computer operator. To speed up
processing, jobs with similar needs are batched together and run as a group. The programmers leave
their programs with the operator and the operator then sorts the programs with similar requirements
into batches.
The problems with Batch Systems are as follows −

 Lack of interaction between the user and the job.


 CPU is often idle, because the speed of the mechanical I/O devices is slower than the CPU.
 Difficult to provide the desired priority.

2)Time-sharing operating systems:-


Time-sharing is a technique which enables many people, located at various terminals, to use a
particular computer system at the same time. Time-sharing or multitasking is a logical extension of
multiprogramming. Processor's time which is shared among multiple users simultaneously is termed
as time-sharing.
The main difference between Multiprogrammed Batch Systems and Time-Sharing Systems is that in
case of Multiprogrammed batch systems, the objective is to maximize processor use, whereas in
Time-Sharing Systems, the objective is to minimize response time.
Multiple jobs are executed by the CPU by switching between them, but the switches occur so
frequently. Thus, the user can receive an immediate response. For example, in a transaction
processing, the processor executes each user program in a short burst or quantum of computation.
That is, if n users are present, then each user can get a time quantum. When the user submits the
command, the response time is in few seconds at most.
The operating system uses CPU scheduling and multiprogramming to provide each user with a
small portion of a time. Computer systems that were designed primarily as batch systems have been
modified to time-sharing systems.
Advantages of Timesharing operating systems are as follows −

 Provides the advantage of quick response.


 Avoids duplication of software.
 Reduces CPU idle time.
Disadvantages of Time-sharing operating systems are as follows −

 Problem of reliability.
 Question of security and integrity of user programs and data.
 Problem of data communication.

3) Distributed operating System :-


Distributed systems use multiple central processors to serve multiple real-time applications and
multiple users. Data processing jobs are distributed among the processors accordingly.
The processors communicate with one another through various communication lines (such as high-
speed buses or telephone lines). These are referred as loosely coupled systems or distributed
systems. Processors in a distributed system may vary in size and function. These processors are
referred as sites, nodes, computers, and so on.
The advantages of distributed systems are as follows −

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

4)Network operating System:-


A Network Operating System runs on a server and provides the server the capability to manage
data, users, groups, security, applications, and other networking functions. The primary purpose of
the network operating system is to allow shared file and printer access among multiple computers in
a network, typically a local area network (LAN), a private network or to other networks.
Examples of network operating systems include Microsoft Windows Server 2003, Microsoft
Windows Server 2008, UNIX, Linux, Mac OS X, Novell NetWare, and BSD.
The advantages of network operating systems are as follows −

 Centralized servers are highly stable.


 Security is server managed.
 Upgrades to new technologies and hardware can be easily integrated into the system.
 Remote access to servers is possible from different locations and types of systems.
The disadvantages of network operating systems are as follows −

 High cost of buying and running a server.


 Dependency on a central location for most operations.
 Regular maintenance and updates are required.

5)Real Time operating System:-


A real-time system is defined as a data processing system in which the time interval required to
process and respond to inputs is so small that it controls the environment. The time taken by the
system to respond to an input and display of required updated information is termed as the response
time. So in this method, the response time is very less as compared to online processing.
Real-time systems are used when there are rigid time requirements on the operation of a processor
or the flow of data and real-time systems can be used as a control device in a dedicated application.
A real-time operating system must have well-defined, fixed time constraints, otherwise the system
will fail. For example, Scientific experiments, medical imaging systems, industrial control systems,
weapon systems, robots, air traffic control systems, etc.
There are two types of real-time operating systems.

A) Hard real-time systems


Hard real-time systems guarantee that critical tasks complete on time. In hard real-time systems,
secondary storage is limited or missing and the data is stored in ROM. In these systems, virtual
memory is almost never found.
B) Soft real-time systems
Soft real-time systems are less restrictive. A critical real-time task gets priority over other tasks and
retains the priority until it completes. Soft real-time systems have limited utility than hard real-time
systems. For example, multimedia, virtual reality, Advanced Scientific Projects like undersea
exploration and planetary rovers, etc

* Logical and user View of OS:-


An operating system is a construct that allows the user application programs to interact with the
system hardware. Operating system by itself does not provide any function but it provides an
atmosphere in which different applications and programs can do useful work.
The operating system can be observed from the point of view of the user or the system. This is
known as the user view and the system view respectively. More details about these are given as
follows:

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.

2 ) Logical or System View:


According to the computer system, the operating system is the bridge between applications and
hardware. It is most intimate with the hardware and is used to control it as required.
The different types of system view for operating system can be explained as follows:

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

* Types of System Calls


There are mainly five types of system calls. These are explained in detail as follows:

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:

Types of System Calls Windows Linux


CreateProcess()
fork()
ExitProcess()
Process Control exit()
WaitForSingleObject()
wait()

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 −

S.N. Component & Description


Stack

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.

Process Life Cycle


When a process executes, it passes through different states. These stages may differ in different
operating systems, and the names of these states are also not standardized.
In general, a process can have one of the following five states at a time.

S.N. State & Description


1 Start
This is the initial state when a process is first started/created.

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 Control Block (PCB)


A Process Control Block is a data structure maintained by the Operating System for every process.
The PCB is identified by an integer process ID (PID). A PCB keeps all the information needed to
keep track of a process as listed below in the table −

S.N. Information & Description


1 Process State
The current state of the process i.e., whether it is ready, running, waiting, or whatever.

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.

CPU Scheduling Information


7
Process priority and other scheduling information which is required to schedule the process.

Memory management information

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

Long Term Scheduler


It is also called a job scheduler. A long-term scheduler determines which programs are admitted to
the system for processing. It selects processes from the queue and loads them into memory for
execution. Process loads into the memory for CPU scheduling.
The primary objective of the job scheduler is to provide a balanced mix of jobs, such as I/O bound
and processor bound. It also controls the degree of multiprogramming. If the degree of
multiprogramming is stable, then the average rate of process creation must be equal to the average
departure rate of processes leaving the system.
On some systems, the long-term scheduler may not be available or minimal. Time-sharing operating
systems have no long term scheduler. When a process changes the state from new to ready, then
there is use of long-term scheduler.

Short Term Scheduler


It is also called as CPU scheduler. Its main objective is to increase system performance in
accordance with the chosen set of criteria. It is the change of ready state to running state of the
process. CPU scheduler selects a process among the processes that are ready to execute and
allocates CPU to one of them.
Short-term schedulers, also known as dispatchers, make the decision of which process to execute
next. Short-term schedulers are faster than long-term schedulers.

Medium Term Scheduler


Medium-term scheduling is a part of swapping. It removes the processes from the memory. It
reduces the degree of multiprogramming. The medium-term scheduler is in-charge of handling the
swapped out-processes.
A running process may become suspended if it makes an I/O request. A suspended processes cannot
make any progress towards completion. In this condition, to remove the process from memory and
make space for other processes, the suspended process is moved to the secondary storage. This
process is called swapping, and the process is said to be swapped out or rolled out. Swapping may
be necessary to improve the process mix.

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

 User logs on.

 User starts a program.

 Operating systems creates process to provide service, e.g., to manage printer.

 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).

Inter-process communication (IPC) :


Inter-process communication (IPC) is a mechanism that allows the exchange of data between
processes. By providing a user with a set of programming interfaces, IPC helps a programmer
organize the activities among different processes. IPC allows one application to control another
application, thereby enabling data sharing without interference.
IPC enables data communication by allowing processes to use segments, semaphores, and other
methods to share memory and information. IPC facilitates efficient message transfer between
processes. The idea of IPC is based on Task Control Architecture (TCA). It is a flexible technique
that can send and receive variable length arrays, data structures, and lists. It has the capability of
using publish/subscribe and client/server data-transfer paradigms while supporting a wide range of
operating systems and languages.
Communication in Client Server :

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:

2)Remote Procedure Calls


These are interprocess communication techniques that are used for client-server based applications.
A remote procedure call is also known as a subroutine call or a function call.
A client has a request that the RPC translates and sends to the server. This request may be a
procedure or a function call to a remote server. When the server receives the request, it sends the
required response back to the client.
A diagram that illustrates remote procedure calls is given as follows:
3)Pipes
These are interprocess communication methods that contain two end points. Data is entered from
one end of the pipe by a process and consumed from the other end by the other process.
The two different types of pipes are ordinary pipes and named pipes. Ordinary pipes only allow one
way communication. For two way communication, two pipes are required. Ordinary pipes have a
parent child relationship between the processes as the pipes can only be accessed by processes that
created or inherited them.
Named pipes are more powerful than ordinary pipes and allow two way communication. These
pipes exist even after the processes using them have terminated. They need to be explicitly deleted
when not required anymore.
A diagram that demonstrates pipes are given 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.

Types of CPU Scheduling


CPU scheduling decisions may take place under the following four circumstances:
1. When a process switches from the running state to the waiting state(for I/O request or
invocation of wait for the termination of one of the child processes).
2. When a process switches from the running state to the ready state (for example, when an
interrupt occurs).
3. When a process switches from the waiting state to the ready state(for example, completion
of I/O).
4. When a process terminates.
In circumstances 1 and 4, there is no choice in terms of scheduling. A new process(if one exists in
the ready queue) must be selected for execution. There is a choice, however in circumstances 2 and
3.
When Scheduling takes place only under circumstances 1 and 4, we say the scheduling scheme is
non-preemptive; otherwise the scheduling scheme is preemptive.

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 −

 First-Come, First-Served (FCFS) Scheduling


 Shortest-Job-Next (SJN) Scheduling
 Priority Scheduling
 Shortest Remaining Time
 Round Robin(RR) Scheduling
 Multiple-Level Queues Scheduling
These algorithms are either non-preemptive or preemptive. Non-preemptive algorithms are
designed so that once a process enters the running state, it cannot be preempted until it completes its
allotted time, whereas the preemptive scheduling is based on priority where a scheduler may
preempt a low priority running process anytime when a high priority process enters into a ready
state.

First Come First Serve (FCFS)


 Jobs are executed on first come, first serve basis.
 It is a non-preemptive, pre-emptive scheduling algorithm.
 Easy to understand and implement.
 Its implementation is based on FIFO queue.
 Poor in performance as average wait time is high.

Wait time of each process is as follows −

Process Wait Time : Service Time - Arrival Time


P0 0-0=0
P1 5-1=4
P2 8-2=6
P3 16 - 3 = 13
Average Wait Time: (0+4+6+13) / 4 = 5.75

Shortest Job Next (SJN)


 This is also known as shortest job first, or SJF

 This is a non-preemptive, pre-emptive scheduling algorithm.


 Best approach to minimize waiting time.

 Easy to implement in Batch systems where required CPU time is known in advance.

 Impossible to implement in interactive systems where required CPU time is not known.

 The processer should know in advance how much time process will take.

Wait time of each process is as follows −

Process Wait Time : Service Time - Arrival Time


P0 3-0=3
P1 0-0=0
P2 16 - 2 = 14
P3 8-3=5
Average Wait Time: (3+0+14+5) / 4 = 5.50

Priority Based Scheduling


 Priority scheduling is a non-preemptive algorithm and one of the most common scheduling
algorithms in batch systems.

 Each process is assigned a priority. Process with highest priority is to be executed first and
so on.

 Processes with same priority are executed on first come first served basis.

 Priority can be decided based on memory requirements, time requirements or any other
resource requirement.
Wait time of each process is as follows −

Process Wait Time : Service Time - Arrival Time


P0 9-0=9
P1 6-1=5
P2 14 - 2 = 12
P3 0-0=0
Average Wait Time: (9+5+12+0) / 4 = 6.5

Shortest Remaining Time


 Shortest remaining time (SRT) is the preemptive version of the SJN algorithm.

 The processor is allocated to the job closest to completion but it can be preempted by a
newer ready job with shorter time to completion.

 Impossible to implement in interactive systems where required CPU time is not known.

 It is often used in batch environments where short jobs need to give preference.

Round Robin Scheduling


 Round Robin is the preemptive process scheduling algorithm.

 Each process is provided a fix time to execute, it is called a quantum.

 Once a process is executed for a given time period, it is preempted and other process
executes for a given time period.

 Context switching is used to save states of preempted processes.


Wait time of each process is as follows −

Process Wait Time : Service Time - Arrival Time


P0 (0 - 0) + (12 - 3) = 9
P1 (3 - 1) = 2
P2 (6 - 2) + (14 - 9) + (20 - 17) = 12
P3 (9 - 3) + (17 - 12) = 11
Average Wait Time: (9+2+12+11) / 4 = 8.5

Multiple-Level Queues Scheduling


Multiple-level queues are not an independent scheduling algorithm. They make use of other existing
algorithms to group and schedule jobs with common characteristics.

 Multiple queues are maintained for processes with common characteristics.


 Each queue can have its own scheduling algorithms.
 Priorities are assigned to each queue.
For example, CPU-bound jobs can be scheduled in one queue and all I/O-bound jobs in another
queue. The Process Scheduler then alternately selects jobs from each queue and assigns them to the
CPU based on the algorithm assigned to the queue.

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.

Critical Section Problem


A Critical Section is a code segment that accesses shared variables and has to be executed as an
atomic action. It means that in a group of cooperating processes, at a given point of time, only one
process must be executing its critical section. If any other process also wants to execute its critical
section, it must wait until the first one finishes.

Solution to Critical Section Problem


A solution to the critical section problem must satisfy the following three conditions:
1. Mutual Exclusion
Out of a group of cooperating processes, only one process can be in its critical section at a given
point of time.
2. Progress
If no process is in its critical section, and if one or more threads want to execute their critical section
then any one of these threads must be allowed to get into its critical section.
3. Bounded Waiting
After a process makes a request for getting into its critical section, there is a limit for how many
other processes can get into their critical section, before this process's request is granted. So after
the limit is reached, system must grant the process permission to get into its critical section.

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.

let say we have 2 condition variables


condition x, y; //Declaring variable
 
Wait operation
x.wait() : Process performing wait operation on any condition variable are suspended. The
suspended processes are placed in block queue of that condition variable.
Note: Each condition variable has its unique block queue.
 
Signal operation
x.signal(): When a process performs signal operation on condition variable, one of the blocked
processes is given chance.
If (x block queue empty)
// Ignore signal
else
// Resume a process from block queue.

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.

 Deadlock recovery through preemption


 Deadlock recovery through rollback
 Deadlock recovery through killing processes
Let's discuss about all the above three ways of deadlock recovery one by one.

1)Deadlock Recovery through Preemption:


The ability to take a resource away from a process, have another process use it, and then give it
back without the process noticing. It is highly dependent on the nature of the resource.
Deadlock recovery through preemption is too difficult or sometime impossible.
2)Deadlock Recovery through RollBack:
In this case of deadlock recovery through rollback, whenever a deadlock is detected, it is easy to see
which resources are needed.
To do the recovery of deadlock, a process that owns a needed resource is rolled back to a point in
time before it acquired some other resource just by starting one of its earlier checkpoints.

3)Deadlock Recovery through Killing Processes:


This method of deadlock recovery through killing processes is the simplest way of deadlock
recovery.
Sometime it is best to kill a process that can be return from the beginning with no ill effects.
Unit IV: Memory Management

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.

Contiguous memory allocation:


The operating system and the user’s processes both must be accommodated in the main memory.
Hence the main memory is divided into two partitions: at one partition the operating system resides
and at other the user processes reside. In usual conditions, the several user processes must reside in
the memory at the same time, and therefore, it is important to consider the allocation of memory to
the processes.
The Contiguous memory allocation is one of the methods of memory allocation. In contiguous
memory allocation, when a process requests for the memory, a single contiguous section of
memory blocks is assigned to the process according to its requirement.
The contiguous memory allocation can be achieved by dividing the memory into the fixed-sized
partition and allocate each partition to a single process only. But this will cause the degree of
multiprogramming, bounding to the number of fixed partition done in the memory. The contiguous
memory allocation also leads to the internal fragmentation. Like, if a fixed sized memory block
allocated to a process is slightly larger than its requirement then the left over memory space in the
block is called internal fragmentation. When the process residing in the partition terminates the
partition becomes available for the another process.
In the variable partitioning scheme, the operating system maintains a table which indicates, which
partition of the memory is free and which occupied by the processes. The contiguous memory
allocation fastens the execution of a process by reducing the overheads of address translation.

Non-Contiguous Memory Allocation


The Non-contiguous memory allocation allows a process to acquire the several memory blocks at
the different location in the memory according to its requirement. The noncontiguous memory
allocation also reduces the memory wastage caused due to internal and external fragmentation. As
it utilizes the memory holes, created during internal and external fragmentation.
non-contiguous. In non-contiguous memory allocation, the process is divided into blocks (pages or
segments) which are placed into the different area of memory space according to the availability of
the memory.
The noncontiguous memory allocation has an advantage of reducing memory wastage but, but it
increases the overheads of address translation. As the parts of the process are placed in a different
location in memory, it slows the execution of the memory because time is consumed in address
translation.
Here, the operating system needs to maintain the table for each process which contains the base
address of the each block which is acquired by the process in memory space.
Paging and segmentation are the two ways which allow a process’s physical address space to be

Key Differences Between Contiguous and Noncontiguous Memory Allocation


1. The basic difference between contiguous and noncontiguous memory allocation is that
contiguous allocation allocates one single contiguous block of memory to the process
whereas, the noncontiguous allocation divides the process into several blocks and place
them in the different address space of the memory i.e. in a noncontiguous manner.
2. In contiguous memory allocation, the process is stored in contiguous memory space; so there
is no overhead of address translation during execution. But in noncontiguous memory
allocation, there is an overhead of address translation while the process execution, as the
process blocks are spread in the memory space.
3. Process stored in contiguous memory executes faster in comparison to process stored in
noncontiguous memory space.
4. The solution for contiguous memory allocation is to divide the memory space into the
fixed-sized partition and allocate a partition to a single process only. On the other hands, in
noncontigous memory allocation, a process is divided into several blocks and each block is
placed at different places in memory according to the availability of the memory.
5. In contiguous memory allocation, operating system has to maintain a table which indicates
which partition is available for the process and which is occupied by the process. In
noncontiguous memory allocation, a table is maintained for each process which indicates
the base address of each block of the process placed in the memory space.
Logical and Physical Memory:
Logical Memory: Logical memory enables the user to use large amount of memory to store
data. It defines way to organize the physical memory such as RAM and cache. This enables
the Operating System to arrange memory into a logical manner such as assigning a logical
address.
Physical Memory: Physical memory is the Primary memory RAM available in your
system. It is the only memory that is directly accessible to the CPU. CPU reads the
instructions stored in the physical memory and executes them continuously. The data that is
operated will also be stored in physical memory in uniform manner.

Logical and Physical Address:


Address uniquely identifies a location in the memory. We have two types of addresses that
are logical address and physical address. The logical address is a virtual address and can be
viewed by the user. The user can’t view the physical address directly. The logical address is
used like a reference, to access the physical address. The fundamental difference between
logical and physical address is that logical address is generated by CPU during a program
execution whereas, the physical address refers to a location in the memory unit.
There are some other differences between the logical and physical address. Let us discuss
them with the help of comparison chart shown below.

Definition of Logical Address


Address generated by CPU while a program is running is referred as Logical Address. The
logical address is virtual as it does not exist physically. Hence, it is also called as Virtual
Address. This address is used as a reference to access the physical memory location. The set
of all logical addresses generated by a programs perspective is called Logical Address
Space.
The logical address is mapped to its corresponding physical address by a hardware device
called Memory-Management Unit. The address-binding methods used by MMU generates
identical logical and physical address during compile time and load time. However, while
run-time the address-binding methods generate different logical and physical address.

Definition of Physical Address


Physical Address identifies a physical location in a memory. MMU (Memory-
Management Unit) computes the physical address for the corresponding logical address.
MMU also uses logical address computing physical address. The user never deals with the
physical address. Instead, the physical address is accessed by its corresponding logical
address by the user. The user program generates the logical address and thinks that the
program is running in this logical address. But the program needs physical memory for its
execution. Hence, the logical address must be mapped to the physical address before they
are used.
The logical address is mapped to the physical address using a hardware called Memory-
Management Unit. The set of all physical addresses corresponding to the logical addresses
in a Logical address space is called Physical Address Space.

Key Differences Between Logical and Physical Address in OS


1. The basic difference between Logical and physical address is that Logical address is
generated by CPU in perspective of a program. On the other hand, the physical address is a
location that exists in the memory unit.
2. The set of all logical addresses generated by CPU for a program is called Logical Address
Space. However, the set of all physical address mapped to corresponding logical addresses is
referred as Physical Address Space. The logical address is also called virtual address as the
logical address does not exist physically in the memory unit.  The physical address is a
location in the memory unit that can be accessed physically.
3.Identical logical address and physical address are generated by Compile-time and Load
time address binding methods.
4.The logical and physical address generated while run-time address binding method differs
from each other.
5.The logical address is generated by the CPU while program is running whereas, the
physical addres is computed by the MMU (Memory Management Unit).
Comparison Chart
6.

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 −

S.N. Fragmentation & Description


External fragmentation

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.

 Paging is simple to implement and assumed as an efficient memory management technique.

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

Segmentation with paging:


Pure segmentation is not very popular and not being used in many of the operating systems.
However, Segmentation can be combined with Paging to get the best features out of both the
techniques.
In Segmented Paging, the main memory is divided into variable size segments which are further
divided into fixed size pages.
1. Pages are smaller than segments.
2. Each Segment has a page table which means every program has multiple page tables.
3. The logical address is represented as Segment Number (base address), Page number and
page offset.
Segment Number → It points to the appropriate Segment Number.
Page Number → It Points to the exact page within the segment
Page Offset → Used as an offset within the page frame
Each Page table contains the various information about every page of the segment. The Segment
Table contains the information about every segment. Each segment table entry points to a page table
entry and every page table entry is mapped to one of the page within a segment.

Advantages of Segmented Paging


1. It reduces memory usage.
2. Page table size is limited by the segment size.
3. Segment table has only one entry corresponding to one actual segment.
4. External Fragmentation is not there.
5. It simplifies memory allocation.

Disadvantages of Segmented Paging


1. Internal Fragmentation will be there.
2. The complexity level will be much higher as compare to paging.
3. Page Tables need to be contiguously stored in the memory.
Virtual Memory Concept:
A computer can address more memory than the amount physically installed on the system. This
extra memory is actually called virtual memory and it is a section of a hard disk that's set up to
emulate the computer's RAM.
The main visible advantage of this scheme is that programs can be larger than physical memory.
Virtual memory serves two purposes. First, it allows us to extend the use of physical memory by
using disk. Second, it allows us to have memory protection, because each virtual address is
translated to a physical address.
Virtual Memory is a space where large programs can store themselves in form of pages while their
execution and only the required pages or portions of processes are loaded into the main memory.
This technique is useful as large virtual memory is provided for user programs when a very small
physical memory is there.
In real scenarios, most processes never need all their pages at once, for following reasons :

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

Benefits of having Virtual Memory


1. Large programs can be written, as virtual space available is huge compared to physical
memory.
2. Less I/O required, leads to faster and easy swapping of processes.
3. More physical memory available, as programs are stored on virtual memory, so they occupy
very less space on actual physical memory.

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 −

 Large virtual memory.


 More efficient use of memory.
 There is no limit on degree of multiprogramming.

Disadvantages
 Number of tables and the amount of processor overhead for handling page interrupts are
greater than in the case of the simple paged management techniques.
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.

Basic Page Replacement


 Find the location of the page requested by ongoing process on the disk.
 Find a free frame. If there is a free frame, use it. If there is no free frame, use a page-
replacement algorithm to select any existing frame to be replaced, such frame is known as
victim frame.
 Write the victim frame to disk. Change all related page tables to indicate that this page is no
longer in memory.
 Move the required page and store it in the frame. Adjust all related page and frame tables to
indicate the change.
 Restart the process that was waiting for this page.
Page Replaccement Algorithms:
Page replacement algorithms are the techniques using which an Operating System decides which
memory pages to swap out, write to disk when a page of memory needs to be allocated. Paging
happens whenever a page fault occurs and a free page cannot be used for allocation purpose
accounting to reason that pages are not available or the number of free pages is lower than required
pages.
When the page that was selected for replacement and was paged out, is referenced again, it has to
read in from disk, and this requires for I/O completion. This process determines the quality of the
page replacement algorithm: the lesser the time waiting for page-ins, the better is the algorithm.
A page replacement algorithm looks at the limited information about accessing the pages provided
by hardware, and tries to select which pages should be replaced to minimize the total number of
page misses, while balancing it with the costs of primary storage and processor time of the
algorithm itself. There are many different page replacement algorithms. We evaluate an algorithm
by running it on a particular string of memory reference and computing the number of page faults,

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.

 For example, consider the following sequence of addresses − 123,215,600,1234,76,96

1) FIFO Page Replacement:-


 A very simple way of Page replacement is FIFO (First in First Out)
 As new pages are requested and are swapped in, they are added to tail of a queue and the
page which is at the head becomes the victim.
 Its not an effective way of page replacement but can be used for small systems.

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

2) Optimal Page algorithm:-


 An optimal page-replacement algorithm has the lowest page-fault rate of all algorithms. An
optimal page-replacement algorithm exists, and has been called OPT or MIN.
 Replace the page that will not be used for the longest period of time. Use the time when a
page is to be used.

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

3)Least Recently Used (LRU) algorithm:-


 Page which has not been used for the longest time in main memory is the one which will be
selected for replacement.

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

 A file has a certain defined structure according to its type.

 A text file is a sequence of characters organized into lines.

 A source file is a sequence of procedures and functions.

 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 −

 Character special files − data is handled character by character as in case of terminals or


printers.

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

File System implementation:


File systems store several important data structures on the disk:
 A boot-control block, ( per volume ) a.k.a. the boot block in UNIX or the partition boot
sector in Windows contains information about how to boot the system off of this disk. This
will generally be the first sector of the volume if there is a bootable system loaded on that
volume, or the block will be left vacant otherwise.
 A volume control block, ( per volume ) a.k.a. the master file table in UNIX or the
superblock in Windows, which contains information such as the partition table, number of
blocks on each filesystem, and pointers to free blocks and free FCB blocks.
 A directory structure ( per file system ), containing file names and pointers to corresponding
FCBs. UNIX uses inode numbers, and NTFS uses a master file table.
 The File Control Block, FCB, ( per file ) containing details about ownership, size,
permissions, dates, etc. UNIX stores this information in inodes, and NTFS in the master file
table as a relational database structure. Fig.FCB

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.

This type of directories can be used for a simple 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.

Two Level Directory


In two level directory systems, we can create a separate directory for each user. There is one master
directory which contains separate directories dedicated to each user. For each user, there is a
different directory present at the second level, containing group of user's file. The system doesn't let
a user to enter in the other user's directory without permission.

Characteristics of two level directory system


1. Each files has a path name as /User-name/directory-name/
2. Different users can have the same file name.
3. Searching becomes more efficient as only one user's list needs to be traversed.
4. The same kind of files cannot be grouped into a single directory for a particular user.
Every Operating System maintains a variable as PWD which contains the present directory name
(present user name) so that the searching can be done appropriately.

Tree Structured Directory


In Tree structured directory system, any directory entry can either be a file or sub directory. Tree
structured directory system overcomes the drawbacks of two level directory system. The similar
kind of files can now be grouped in one directory.
Each user has its own directory and it cannot enter in the other user's directory. However, the user
has the permission to read the root's data but he cannot write or modify this. Only administrator of
the system has the complete access of root directory.
Searching is more efficient in this directory structure. The concept of current working directory is
used. A file can be accessed by two types of path, either relative or absolute.
Absolute path is the path of the file with respect to the root directory of the system while relative
path is the path with respect to the current working directory of the system. In tree structured
directory systems, the user is given the privilege to create the files as well as directories.
Free Space Management:
A file system is responsible to allocate the free blocks to the file therefore it has to keep track of all
the free blocks present in the disk. There are mainly two approaches by using which, the free blocks
in the disk are managed.

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.

Efficiency and Performance:-


Efficiency:
The efficient use of disk space depends heavily on the disk allocation and directory algorithms in
use. For instance, UNIX inodes are preallocated on a volume. Even an "empty" disk has a
percentage of its space lost to inodes. However, by preallocating the inodes and. spreading them
across the volume, we improve the file system's performance. This improved performance results
from the UNIX allocation and free-space algorithms, which try to keep a file's data blocks near that
file's inode block to reduce seek time. As another example, let's reconsider the clustering scheme
discussed in Section 11.4, which aids in file-seek and file-transfer performance at the cost of
internal fragmentation.
To reduce this fragmentation, BSD UNIX varies the cluster size as a file grows. Large clusters are
used where they can be filled, and small clusters are used for small files and the last cluster of a file.
This system is described in Appendix A. The types of data normally kept in a file's directory (or
inode) entry also require consideration. Commonly, a 'last write date" is recorded to supply
information to the user and, to determine whether the file needs to be backed up. Some systems also
keep a "last access date," so that a user can determine when the file was last read.
The result of keeping this information is that, whenever the file is read, a field in the directory
structure must be written to. That means the block must be read into memory, a section changed,
and the block written back out to disk, because operations on disks occur only in block (or cluster)
chunks. So any time a file is opened for reading, its directory entry must be read and written as well.
This requirement can be inefficient for frequently accessed files, so we must weigh its benefit
against its performance cost when designing a file system. Generally, every data item associated

 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.

You might also like