0% found this document useful (0 votes)
150 views133 pages

Comprehensive Guide to Operating Systems

Uploaded by

Kelu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
150 views133 pages

Comprehensive Guide to Operating Systems

Uploaded by

Kelu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

OPERATING SYSTEM

SYLLABUS

Objectives

To provide knowledge about the services rendered by operating systems

 To provide a detailed discussion of the various memory management techniques

 To discuss the various file-system design and implementation issues

 To discuss how the protection domains help to achieve security in a system

Module I: (8 Hours)

Operating Systems –Definition- Types- Functions -Abstract view of OS- System Structures –System Calls-
Virtual Machines –Process Concepts –Threads –Multithreading

Module II: (4 Hours)

Process Scheduling- Process Co-ordination –Synchronization –Semaphores –Monitors Hardware


Synchronization –Deadlocks –Methods for Handling Deadlocks

Module III: (12 Hours)

Memory Management Strategies –Contiguous and Non-Contiguous allocation –Virtual memory


Management –Demand Paging- Page Placement and Replacement Policies

Module IV: (6 Hours)

File System –Basic concepts - File System design and Implementation –Case Study: Linux File Systems -
Mass Storage Structure –Disk Scheduling –Disk Management –I/O Systems-System Protection and
Security.

Module V: (10 Hours)

Distributed Systems –Distributed operating systems –Distributed file systems –Distributed


Synchronization

Out comes

 Ability to comprehend the techniques used to implement the process manager

 Ability to comprehend virtual memory abstractions in operating systems

 Ability to design and develop file system interfaces, etc

Page 1
OPERATING SYSTEM

MODULE- I

Computer system:

A computer system is collection of hardware and software components designed to provide an


effective tool for computation.

Hardware
It is generally refers to the electrical, mechanical and electronics parts that make up the computers.

Or

Internal architecture of the computer

Or

Physical computing equipment

Software

It is refers to set of programs written to provide services to the system

Types of software:

1. System software
2. Application software
3. Utility software

System software:

The software which is required by the computer system to run itself is called system software.

Example:

Operating system

Application software:

The software which fulfills one of the specific requirements of a user is called application
software.

Example:

Media player, notepad, Nero, word processor, spread sheet

Page 2
OPERATING SYSTEM

Utility software:

It is used for maintaining trouble shooting or securing computer system.

Example: antivirus, partition magic etc.

Operating system:

 An operating System is a collection of system programs/software that together


control the operations of a computer system
Or
 It is system software which allows other application software to get installed and
run on it. Hence operating system is called as a platform or environment

 Operating system acts as an intermediary between a user of a computer and the


computer hardware.

 The operating system controls and coordinates the execution of the programs. So it
is sometimes called control program.

Goals/Objective of the operating system:

 It provides an environment in which an user can execute programs

 Its primary goal is to provide convenient to the user for executing program(easy to use
commands and GUI) .

 Its secondary goal is that the Operating system allows to computer to Use the computer
hardware (resources)/ computer components in an efficient manner

Example:
Ubuntu, Fedora, Linux, UNIX, MS-DOS, MS-Windows, Windows/NT, MacOS, android, VMS, MVS,
and VM.

Page 3
OPERATING SYSTEM

Function of operating system:

1. Booting:
This is a process that when a computer system is on then operating system is loaded
into the RAM
Booting is a process of starting the computer operating system starts the computer to
work. It checks the computer and makes it ready to work
2. Memory management
It is also an important function of operating system. The memory cannot be managed
without operating system. Different programs and data execute in memory at one time.
if there is no operating system, the programs may mix with each other. The system will
not work properly.
An operating system does the following activities
Keeps track of primary memory using free space management (which partition is used
and which is free)

Allocate the memory to process when it request it

Deallocate the memory when a process terminate and the same memory is allocated
to the other process

3. Loading and execution:


A program is loaded in the memory before it can be executed. Operating system
provides the facility to load programs in memory easily and then execute it.
4. Data security management :
Data is an important part of computer system. There are various virus threats which are
interfere with the normal operation of the computer and can be very harmful and
result in loss of data or system crashes. The operating system protects the data stored
on the computer from illegal use, modification or deletion.
Operating system is also helps to prevent unauthorized access to program and data by
use of password and user login etc.
5. Disk management
Operating system manages the disk space. It manages the stored files and folders in a
proper way.
6. Process management

Page 4
OPERATING SYSTEM

In process management, operating system do management of cpu.


The operating system takes care of the allotment of the cpu to different process.
When the cpu is free operating selects a process from job queue and allocates the cpu
to the process.
When process execution will completed, operating system frees the processor and
again select process for execution.
Selection of process from job queue is done by using various cpu scheduling techniques
like FCFS, SJF, PRIORITY and ROUND ROBIN
In process management function , operating system assigns the processor to a process
and deallocate processor when it is not required.
CPU can perform one task at one time. if there are many tasks, operating system
decides which task should get the CPU.
7. File management:
File : it is a logical related collection of information
File is stored in secondary memory/ storage
A file may contain a report , an executable program or a set of commands to the
operating system.
An os manages the files, folder and directory system on a computer
An operating system does the following activities
Creating and deleting a file
Creating and deleting directory
Os keeps information of files using file allocation table(FAT)
FAT stores general information about file like file name , type (text or binary), size or
access mode (sequential /indexed/direct / relative)
Operating system take care files are opened with proper access rights (read/write)
File manager of operating system helps to create , edit , copy , allocate memory to the
files and also updates the FAT.
8. I/O management
Operating system manages I/O devices and makes the I/O process effective
Operating system accepts the input from the input device(keyboard) stores it in main
memory , ask the cpu to process it and finally provides the result to output
deveces(monitor/printer)
Operating system controls all I/O devices attached to the computer with the help of
small software called device driver.
9. Providing interface

Page 5
OPERATING SYSTEM

It is used in order that user interface acts with a computer mutually. User interface
controls how you input data and instruction and how information is displayed on
screen. The operating system offers two types of the interface to the user:
1. Graphical-line interface: It interacts with of visual environment to communicate
with the computer. It uses windows, icons, menus and other graphical objects to
issues commands.
2. Command-line interface: it provides an interface to communicate with the
computer by typing commands.
Viewpoints of operating system:
There are two types of views of the operating system
1. User view
2. System view
User view:
 The user viewpoint focuses on how the user interacts with the operating system
through the usage of various application programs
 The user view depends on the system interface that is used by the users.

System view:
 From the computer point of view the operating system is the program which is most
intermediate with the hardware.
 An operating system has resources as hardware and software which may be required to
solve a problem like CPU time, memory space, file storage space and I/O devices and so on.
That’s why the operating system acts as manager of these resources.
 Another view of the operating system is it is a control program. A control program
manages the execution of user programs to present the errors in proper use of the
computer. It is especially concerned of the user the operation and controls the I/O devices.

Types of operating system

1. Serial processing system


2. Batch Operating System
3. Multitasking/Time Sharing OS
4. Multiprogramming OS
5. Multiprocessing OS
6. Real Time OS
7. Distributed OS
8. Network OS

Serial processing system:

Page 6
OPERATING SYSTEM

Before 1950’s the programmer directly interact with computer hardware, there is no
operating system at that time. If a programmer wants to execute the program on those days.

Serial steps

1. Type the program on punched card


2. Convert the punched card to card reader
3. Submit to the computing machine , if any error in the program the error condition was
indicated by lights
4. The programmer examines the registers main memory to identify the cause of error
5. Take the output on the printer
6. Then the programmer ready for the next program

Here the programs are submitted to the machine one after one, therefore this method is said
to be serial processing.

Batch/early processing operating system:

 In batch processing operating system similar types of jobs batch together and
execute at a time.
 Early computers were physically large machine. The common input devices were
card readers, tape drivers. The common output devices were line printers, tape
drivers and card punches. In these systems the user did not interact directly with the
computer system.

 Instead the user preparing a job which consists of programming data and some
control information and then submitted it to the computer operator after some

Page 7
OPERATING SYSTEM

time the output is appeared. The output in these early computer was fairly simple is
main task was to transfer control automatically from one job to next.
 The operating system always resides in the memory. To speed up processing
operators batched the jobs with similar needs and ran then together as a group.
 The disadvantages of batch system are that in this execution environment the CPU is
often idle because the speed up of I/O devices is much slower than the CPU.
For exampleFORTRAN programs were grouped together as one batch say batch1, the
PASCAL program into another batch say batch 2, the COBOL programs into another batch
say batch 3 and so on. Now the operator can arrange for the execution of this source
program which has been batched together one by one. After the execution of batch 1 was
over, the operator would load the compiler, assembler and loader etc for the batch 2 and
so on.

The disadvantage is that cpu utilization is very low.

Multiprogrammed operating system ( non preemptive)

 It is a technique to execute number of programs simultaneously by a single


processor.
 In multiprogramming number of processes are reside in main memory at a time
 The operating system picks and begins to execute one of the jobs in the main
memory
 All the jobs that enter the system are stored in the job pool (in disc). The operating
system loads a set of jobs from job pool into main memory and begins to execute.
 During execution, the job may have to wait for some task, such as an I/O operation,
to complete. In a multiprogramming system, the operating system simply switches
to another job and executes.
 When that job needs to wait, the CPU is switched to another job, and so on.
 When the first job finishes waiting and it gets the CPU back.
 As long as at least one job needs to execute, the CPU is never idle.
 Multiprogramming operating systems use the mechanism of job scheduling and CPU
scheduling.

Page 8
OPERATING SYSTEM

In non multiprogramming system the CPU can executes only one program at a time, if the

Running time waiting for any I/O device, the CPU becomes idle so it will affect the
performance of the computer.

Multitasking/Time Sharing OS

 It is the extension of multiprogramming os. The time sharing system is also known
as multi user systems. The CPU executes multiple jobs by switching among them but
the switches occurs so frequently that the user can interact with each program
while it is running.
 An interactive computer system provides direct communication between a user and
system. The user gives instruction to the operating systems or to a program directly
using keyboard or mouse and wait for immediate results. So the response time will
be short.

Page 9
OPERATING SYSTEM

 The time sharing system allows many users to share the computer simultaneously.
Since each action in this system is short, only a little CPU time is needed for each
user.
 The system switches rapidly from one user to the next so each user feels as if the
entire computer system is dedicated to his use, even though it is being shared by
many users.
The disadvantages of time sharing system are:
 It is more complex than multiprogrammed operating system
 The system must have memory management & protection, since several jobs
are kept in memory at the same time
 Time sharing system must also provide a file system, so disk management is
required.
 It provides mechanism for concurrent execution which requires complex CPU
scheduling schemes.

Multiprocessing OS:

Multiprocessor Operating System refers to the use of two or more central processing
units (CPU) within a single computer system. These multiple CPUs are in a close
communication sharing the computer bus, memory and other peripheral devices. These
systems are referred as tightly coupled systems.

Page 10
OPERATING SYSTEM

Real time OS:

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

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.


Hard real time OS:

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.
Soft real time OS:

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.

Distributed Operating System/ loosely coupled system


In contrast to tightly coupled systems, the processors do not share memory or a clock. Instead,
each processor has its own local memory.

The processors communicate with each other by various communication lines such as high speed
buses or telephone lines. Distributed systems depend on networking for their functionalities.

The advantages of distributed system are resource sharing, computation speed up, reliability,
communication.

Network Operating System


Network Operating System runs on a server. It provides the capability to serve to manage
data, user, groups, security, application, and other networking functions.

Page 11
OPERATING SYSTEM

Abstract view of operating system:

Different mode of operating system


User Mode
The User mode is normal mode where the process has limited access. In this mode,
processes do not have direct access to the RAM or other hardware resources and
have to make system calls to the underlying APIs to access these resources.

Page 12
OPERATING SYSTEM

Kernel mode
The kernel mode has direct access to all the underlying hardware resources. In
the kernel mode, all memory addresses are accessible and all CPU instructions
are executable.

The kernel provides System Call Interface (SCI), which are the entry points for kernel
Moving between the user mode and the kernel mode is referred to as context
switching.

System structure:
1. Simple structure: There are several commercial system that don’t have a well-
defined structure such operating systems begins as small, simple & limited
systems and then grow beyond their original scope. MS-DOS is an example of
such system. It was not divided into modules carefully. Another example of
limited structuring is the UNIX operating system.
2. Layered approach: In the layered approach, the OS is broken into a number of
layers (levels) each built on top of lower layers. The bottom layer (layer o ) is the
hardware & top most layer (layer N) is the user interface. The main advantage of
the layered approach is modularity.
The layers are selected such that each users functions (or operations) & services
of only lower layer
This approach simplifies debugging & system verification, i.e. the first layer can
be debugged without concerning the rest of the system. Once the first layer is
debugged, its correct functioning is assumed while the 2nd layer is debugged &
so on.
• If an error is found during the debugging of a particular layer, the error must be
on that layer because the layers below it are already debugged. Thus the design
& implementation of the system are simplified when the system is broken down
into layers.
• Each layer is implemented using only operations provided by lower layers. A
layer doesn’t need to know how these operations are implemented; it only needs
to know what these operations do.
• The layer approach was first used in the operating system. It was defined in six
layers

Page 13
OPERATING SYSTEM

System call
System calls provide the interface between a process & the OS. These are usually
available in the form of assembly language instruction. Some systems allow system
calls to be made directly from a high level language program like C, BCPL and PERL
etc. systems calls occur in different ways depending on the computer in use.

System calls can be roughly grouped into 5 major categories

1. Device Management:
Request device, release device: If there are multiple users of the system, we first
request the device. After we finished with the device, we must release it.
• Read, write, reposition: Once the device has been requested & allocated to us, we
can read, write & reposition the device.
2. File manipulation:
Create file, delete file: We first need to be able to create & delete files. Both the
system calls require the name of the file & some of its attributes.

Page 14
OPERATING SYSTEM

• Open file, close file: Once the file is created, we need to open it & use it. We
close the file when we are no longer using it.
• Read, write, reposition file: After opening, we may also read, write or
reposition the file (rewind or skip to the end of the file).
• Get file attributes, set file attributes: For either files or directories, we need to
be able to determine the values of various attributes & reset them if necessary.
Two system calls get file attribute & set file attributes are required for their
purpose
3. Process Control:
End, abort: A running program needs to be able to has its execution either normally
(end) or abnormally (abort).
• Load, execute: A process or job executing one program may want to load and
executes another program.
• Create Process, terminate process: There is a system call specifying for the purpose
of creating a new process or job (create process or submit job). We may want to
terminate a job or process that we created (terminates process, if we find that it is
incorrect or no longer needed).
• Get process attributes, set process attributes: If we create a new job or process we
should able to control its execution. This control requires the ability to determine &
reset the attributes of a job or processes (get process attributes, set process attributes).
• Wait time: After creating new jobs or processes, we may need to wait for them to
finish their execution (wait time).
• Wait event, signal event: We may wait for a specific event to occur (wait event). The
jobs or processes then signal when that event has occurred (signal event).
4. Information maintenance:
Get time or date, set time or date: Most systems have a system call to return the
current date & time or set the current date & time.
• Get system data, set system data: Other system calls may return information
about the system like number of current users, version number of OS, amount of
free memory etc.
• Get process attributes, set process attributes: The OS keeps information about
all its processes & there are system calls to access this information

5. Communication:

There are two modes of communication such as:

Page 15
OPERATING SYSTEM

Message passing model: Information is exchanged through an inter process


communication facility provided by operating system. Each computer in a network
has a name by which it is known. Similarly, each process has a process name which
is translated to an equivalent identifier by which the OS can refer to it. The get
hostid and get processed systems calls to do this translation. These identifiers are
then passed to the general purpose open & close calls provided by the file system or
to specific open connection system call. The recipient process must give its
permission for communication to take place with an accept connection call. The
source of the communication known as client & receiver known as server exchange
messages by read message & write message system calls. The close connection call
terminates the connection.

• Shared memory model: processes use map memory system calls to access
regions of memory owned by other processes. They exchange information by
reading & writing data in the shared areas. The processes ensure that they are not
writing to the same location simultaneously

Virtual machine:
By using CPU scheduling & virtual memory techniques an operating system can
create the illusion of multiple processes, each executing on its own processors &
own virtual memory. Each processor is provided a virtual copy of the underlying
computer. The resources of the computer are shared to create the virtual
machines. CPU scheduling can be used to create the appearance that users have
their own processor.
Implementation: Although the virtual machine concept is useful, it is difficult to
implement since much effort is required to provide an exact duplicate of the
underlying machine. The CPU is being multiprogrammed among several virtual
machines, which slows down the virtual machines in various ways.
Difficulty: A major difficulty with this approach is regarding the disk system. The
solution is to provide virtual disks, which are identical in all respects except size.
These are known as mini disks in IBM’s VM OS. The sum of sizes of all mini disks
should be less than the actual amount of physical disk space available.
I/O Structure
A general purpose computer system consists of a CPU and multiple device
controller which is connected through a common bus. Each device controller is in
charge of a specific type of device. A device controller maintains some buffer
Page 16
OPERATING SYSTEM

storage and a set of special purpose register. The device controller is responsible
for moving the data between peripheral devices and buffer storage.
I/O Interrupt:
To start an I/O operation the CPU loads the appropriate register within the
device controller. In turn the device controller examines the content of the
register to determine the actions which will be taken. For example, suppose the
device controller finds the read request then, the controller will start the transfer
of data from the device to the buffer. Once the transfer of data is complete the
device controller informs the CPU that the operation has been finished. Once the
I/O is started, two actions are possible such as

Process:
 A process is basically a program in execution. The execution of a process must progress in a
sequential fashion.
 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.

STACK
The process Stack contains the temporary data such as method/function
parameters, return address and local variables.
HEAP
This is dynamically allocated memory to a process during its run time.
TEXT
This includes the current activity represented by the value of Program Counter
and the contents of the processor's registers.
DATA
This section contains the global and static variables

Page 17
OPERATING SYSTEM

Process Life Cycle


When a process executes, it passes through different states. It gives about current
position of the state.

New State:

This is the state when the process is just created. It is the first state of a
process

Ready State:

After the creation of the process, when the process is ready for its execution then
it goes in the ready state. In a ready state, the process is ready for its execution by
the CPU but it is waiting for its turn to come. There can be more than one process
in the ready state.

Ready Suspended State:

There can be more than one process in the ready state but due to memory
constraint, if the memory is full then some process from the ready state gets
placed in the ready suspended state.

Running State:

In this state memory is allocated to the process for execution by using some CPU
scheduling algorithm.

Page 18
OPERATING SYSTEM

Waiting or Blocked State:

During the execution of the process, the process might require some I/O operation
like writing on file or some more priority process might come. In these situations,
the running process will have to go into the waiting or blocked state and the other
process will come for its execution. So, the process is waiting for something in the
waiting state.

Waiting Suspended State:

When the waiting queue of the system becomes full then some of the processes
will be sent to the waiting suspended state.

Terminated State:

After the complete execution of the process, the process comes into the
terminated state and the information related to this process is deleted.
Description of process life cycle:

When a process is created then it goes into the new state. After the new state,
it goes into the ready state. If the ready queue is full, then the process will be
shifted to the ready suspended state. From the ready sate, the CPU will choose
the process and the process will be executed by the CPU and will be in the
running state. During the execution of the process, the process may need some
I/O operation to perform. So, it has to go into the waiting state and if the
waiting state is full then it will be sent to the waiting suspended state. From the
waiting state, the process can go to the ready state after performing I/O
operations. From the waiting suspended state, the process can go to waiting or
ready suspended state. At last, after the complete execution of the process, the
process will go to the terminated state and the information of the process will
be deleted.

Process control block (PCB)/Task control block (TCB)


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

Page 19
OPERATING SYSTEM

1. PROCESS STATE
The current state of the process i.e., whether it is ready, running, waiting, or
whatever.
2. PROCESS PRIVILEGES:
This is required to allow/disallow access to system resources
3. PROCESS ID
Unique identification for each of the process in the operating system.
4. POINTER
A pointer to parent process
5. PROGRAM COUNTER
Program Counter is a pointer to the address of the next instruction to be
executed for this process.
6. CPU register
Various CPU registers where process need to be stored for execution for
running state.
7. CPU scheduling information
Process priority and other scheduling information which is required to
schedule the process.

8. Memory management information


This includes the information of page table, memory limits, Segment table
depending on memory used by the operating system.
9. Accounting information
This includes the amount of CPU used for process execution, time limits,
execution ID etc.

Page 20
OPERATING SYSTEM

10. I/O status information


This includes a list of I/O devices allocated to the process.

Threads:
A thread is a path of execution within a process. A process can contain multiple
threads.
The process can be split down into so many threads. For example, in a browser, many tabs can
be viewed as threads. MS Word uses many threads - formatting text from one thread, processing
input from another thread, etc.
Each thread of the same process makes use of a separate program counter and a stack of
activation records and control blocks. Thread is often referred to as a lightweight process.

Types of Threads

In the operating system, there are two types of threads.

1. Kernel level thread.


2. User-level thread

User level thread (User managed threads)

The operating system does not recognize the user-level thread. User threads can be easily
implemented and it is implemented by the user. If a user performs a user-level thread blocking
operation, the whole process is blocked. The kernel level thread does not know nothing about the user
level thread. The kernel-level thread manages user-level threads as if they are single-threaded processes?

Examples: Java thread, POSIX threads, etc.

Kernel level thread (Operating System managed threads acting on kernel, an operating
system core.)
The kernel thread recognizes the operating system. There is a thread control block and process
control block in the system for each thread and process in the kernel-level thread. The kernel-level
thread is implemented by the operating system. The kernel knows about all the threads and
manages them. The kernel-level thread offers a system call to create and manage the threads from
user-space. The implementation of kernel threads is more difficult than the user thread. Context
switch time is longer in the kernel thread. If a kernel thread performs a blocking operation, the
Banky thread execution can continue. Example: Window Solaris.

Page 21
OPERATING SYSTEM

Page 22
OPERATING SYSTEM

MODULE –II

CPU Scheduling in Operating Systems

Scheduling of processes/work is done to finish the work on time. CPU Scheduling is a process
that allows one process to use the CPU while another process is delayed (in standby) due to
unavailability of any resources such as I / O etc, thus making full use of the CPU. The purpose of
CPU Scheduling is to make the system more efficient, faster, and fairer.

Whenever the CPU becomes idle, the operating system must select one of the processes in the
line ready for launch. The selection process is done by a temporary (CPU) scheduler. The
Scheduler selects between memory processes ready to launch and assigns the CPU to one of
them.

What is a process?

In computing, a process is the instance of a computer program that is being executed by one
or many threads. It contains the program code and its activity. Depending on the operating
system (OS), a process may be made up of multiple threads of execution that execute instructions
concurrently.

How is process memory used for efficient operation?

The process memory is divided into four sections for efficient operation:

 The text category is composed of integrated program code, which is read from fixed
storage when the program is launched.
 The data class is made up of global and static variables, distributed and executed before
the main action.
 Heap is used for flexible, or dynamic memory allocation and is managed by calls to new,
delete, malloc, free, etc.
 The stack is used for local variables. The space in the stack is reserved for local variables
when it is announced.

Page 23
OPERATING SYSTEM

What is Process Scheduling?

Process Scheduling is the process of the process manager handling the removal of an active
process from the CPU and selecting another process based on a specific strategy.

Process Scheduling is an integral part of Multi-programming applications. Such operating


systems allow more than one process to be loaded into usable memory at a time and the loaded
shared CPU process uses repetition time.

There are three types of process schedulers:

 Long term or Job Scheduler


 Short term or CPU Scheduler
 Medium-term Scheduler

Why do we need to schedule processes?

 Scheduling is important in many different computer environments. One of the most


important areas is scheduling which programs will work on the CPU. This task is handled
by the Operating System (OS) of the computer and there are many different ways in which
we can choose to configure programs.
 Process Scheduling allows the OS to allocate CPU time for each process. Another
important reason to use a process scheduling system is that it keeps the CPU busy at all
times. This allows you to get less response time for programs.
 Considering that there may be hundreds of programs that need to work, the OS must
launch the program, stop it, switch to another program, etc. The way the OS configures the

Page 24
OPERATING SYSTEM

system to run another in the CPU is called “context switching”. If the OS keeps context-
switching programs in and out of the provided CPUs, it can give the user a tricky idea that
he or she can run any programs he or she wants to run, all at once.
 So now that we know we can run 1 program at a given CPU, and we know we can change
the operating system and remove another one using the context switch, how do we
choose which programs we need. run, and with what program?
 That’s where scheduling comes in! First, you determine the metrics, saying something like
“the amount of time until the end”. We will define this metric as “the time interval between
which a function enters the system until it is completed”. Second, you decide on a metrics
that reduces metrics. We want our tasks to end as soon as possible.

What is the need for CPU scheduling algorithm?

CPU scheduling is the process of deciding which process will own the CPU to use while another
process is suspended. The main function of the CPU scheduling is to ensure that whenever the
CPU remains idle, the OS has at least selected one of the processes available in the ready-to-use
line.

In Multiprogramming, if the long-term scheduler selects multiple I / O binding processes then


most of the time, the CPU remains an idle. The function of an effective program is to improve
resource utilization.

If most operating systems change their status from performance to waiting then there may
always be a chance of failure in the system. So in order to minimize this excess, the OS needs to
schedule tasks in order to make full use of the CPU and avoid the possibility of deadlock.

Objectives of Process Scheduling Algorithm:

 Utilization of CPU at maximum level. Keep CPU as busy as possible.


 Allocation of CPU should be fair.
 Throughput should be Maximum. i.e. Number of processes that complete their
execution per time unit should be maximized.
 Minimum turnaround time, i.e. time taken by a process to finish execution should be the
least.
 There should be a minimum waiting time and the process should not starve in the ready
queue.
 Minimumresponse time. It means that the time when a process produces the first
response should be as less as possible.

What are the different terminologies to take care of in any CPU Scheduling algorithm?

 Arrival Time: Time at which the process arrives in the ready queue.
 Completion Time: Time at which process completes its execution.
 Burst Time: Time required by a process for CPU execution.

Page 25
OPERATING SYSTEM

 Turn Around Time: Time Difference between completion time and arrival time.

Turn Around Time = Completion Time – Arrival Time

 Waiting Time(W.T): Time Difference between turn around time and burst time.

Waiting Time = Turn Around Time – Burst Time

Things to take care while designing a CPU Scheduling algorithm?

Different CPU Scheduling algorithms have different structures and the choice of a particular
algorithm depends on a variety of factors. Many conditions have been raised to compare CPU
scheduling algorithms.

The criteria include the following:

 CPU utilization: The main purpose of any CPU algorithm is to keep the CPU as busy as
possible. Theoretically, CPU usage can range from 0 to 100 but in a real-time system, it
varies from 40 to 90 percent depending on the system load.
 Throughput: The average CPU performance is the number of processes performed and
completed during each unit. This is called throughput. The output may vary depending on
the length or duration of the processes.
 Turn round Time: For a particular process, the important conditions are how long it takes
to perform that process. The time elapsed from the time of process delivery to the time of
completion is known as the conversion time. Conversion time is the amount of time spent
waiting for memory access, waiting in line, using CPU, and waiting for I / O.
 Waiting Time: The Scheduling algorithm does not affect the time required to complete
the process once it has started performing. It only affects the waiting time of the process
i.e. the time spent in the waiting process in the ready queue.
 Response Time: In a collaborative system, turn around time is not the best option. The
process may produce something early and continue to computing the new results while
the previous results are released to the user. Therefore another method is the time taken in
the submission of the application process until the first response is issued. This measure is
called response time.

What are the different types of CPU Scheduling Algorithms?

There are mainly two types of scheduling methods:

 Preemptive Scheduling: Preemptive scheduling is used when a process switches from


running state to ready state or from the waiting state to the ready state.
 Non-Preemptive Scheduling: Non-Preemptive scheduling is used when a process
terminates , or when a process switches from running state to waiting state.

Page 26
OPERATING SYSTEM

Different types of CPU Scheduling Algorithms

Let us now learn about these CPU scheduling algorithms in operating systems one by one:

1. First Come First Serve:

FCFS considered to be the simplest of all operating system scheduling algorithms. First come first
serve scheduling algorithm states that the process that requests the CPU first is allocated the CPU
first and is implemented by using FIFO queue.

Characteristics of FCFS:

 FCFS supports non-preemptive and preemptive CPU scheduling algorithms.


 Tasks are always executed on a First-come, First-serve concept.
 FCFS is easy to implement and use.
 This algorithm is not much efficient in performance, and the wait time is quite high.

Advantages of FCFS:

 Easy to implement
 First come, first serve method

Disadvantages of FCFS:

Page 27
OPERATING SYSTEM

 FCFS suffers from Convoy effect.


 The average waiting time is much higher than the other algorithms.
 FCFS is very simple and easy to implement and hence not much efficient.

Example

Let's take an example of The FCFS scheduling algorithm. In the Following schedule, there are 5
processes with process ID P0, P1, P2, P3 and P4. P0 arrives at time 0, P1 at time 1, P2 at time 2,
P3 arrives at time 3 and Process P4 arrives at time 4 in the ready queue. The processes and their
respective Arrival and Burst time are given in the following table.

The Turnaround time and the waiting time are calculated by using the following formula.

1. Turn Around Time = Completion Time - Arrival Time


2. Waiting Time = Turnaround time - Burst Time

The average waiting Time is determined by summing the respective waiting time of all the
processes and divided the sum by the total number of processes.

Process ID Arrival Time Burst Time Completion Time Turn Around Time Waiting Time

0 0 2 2 2 0

1 1 6 8 7 1

2 2 4 12 10 6

3 3 9 21 18 9

4 6 12 33 29 17

Avg Waiting Time=33/5

(Gantt chart)

Page 28
OPERATING SYSTEM

Convoy Effect in FCFS

FCFS may suffer from the convoy effect if the burst time of the first job is the highest among all.
As in the real life, if a convoy is passing through the road then the other persons may get blocked
until it passes completely. This can be simulated in the Operating System also.

If the CPU gets the processes of the higher burst time at the front end of the ready queue then
the processes of lower burst time may get blocked which means they may never get the CPU if
the job in the execution has a very high burst time. This is called convoy effect or starvation.

Example

In the Example, We have 3 processes named as P1, P2 and P3. The Burt Time of process P1 is
highest.

The Turnaround time and the waiting time in the following table, are calculated by the formula,

1. Turn Around Time = Completion Time - Arrival Time


2. Waiting Time = Turn Around Time - Burst Time

In the First scenario, The Process P1 arrives at the first in the queue although; the burst time of
the process is the highest among all. Since, the Scheduling algorithm, we are following is FCFS
hence the CPU will execute the Process P1 first.

In this schedule, the average waiting time of the system will be very high. That is because of the
convoy effect. The other processes P2, P3 have to wait for their turn for 40 units of time although
their burst time is very low. This schedule suffers from starvation.

Process ID Arrival Time Burst Time Completion Time Turn Around Time Waiting Time

1 0 40 40 40 0

2 1 3 43 42 39

Page 29
OPERATING SYSTEM

3 1 1 44 43 42

Avg waiting Time = 81/3

In the Second scenario, If Process P1 would have arrived at the last of the queue and the other
processes P2 and P3 at earlier then the problem of starvation would not be there.

Following example shows the deviation in the waiting times of both the scenarios. Although the
length of the schedule is same that is 44 units but the waiting time will be lesser in this schedule.

Process ID Arrival Time Burst Time Completion Time Turn Around Time Waiting Time

1 1 40 44 43 3

2 0 3 3 3 0

3 0 1 4 4 3

Avg Waiting Time=6/3

Shortest Job First (SJF) Scheduling

Till now, we were scheduling the processes according to their arrival time (in FCFS scheduling).
However, SJF scheduling algorithm, schedules the processes according to their burst time.

In SJF scheduling, the process with the lowest burst time, among the list of available processes in
the ready queue, is going to be scheduled next.

However, it is very difficult to predict the burst time needed for a process hence this algorithm is
very difficult to implement in the system.

Page 30
OPERATING SYSTEM

Advantages of SJF

1. Maximum throughput
2. Minimum average waiting and turnaround time

Disadvantages of SJF

1. May suffer with the problem of starvation


2. It is not implementable because the exact Burst time for a process can't be known in
advance.

There are different techniques available by which, the CPU burst time of the process can be
determined. We will discuss them later in detail.

Example

In the following example, there are five jobs named as P1, P2, P3, P4 and P5. Their arrival time and
burst time are given in the table below.

Waiting
PID Arrival Time Burst Time Completion Time Turn Around Time
Time

1 1 7 8 7 0

2 3 3 13 10 7

3 6 2 10 4 2

4 7 10 31 24 14

5 9 8 21 12 4

Since, No Process arrives at time 0 hence; there will be an empty slot in the Gantt chart from
time 0 to 1 (the time at which the first process arrives).

According to the algorithm, the OS schedules the process which is having the lowest burst time
among the available processes in the ready queue.

Till now, we have only one process in the ready queue hence the scheduler will schedule this to
the processor no matter what is its burst time.

Page 31
OPERATING SYSTEM

This will be executed till 8 units of time. Till then we have three more processes arrived in the
ready queue hence the scheduler will choose the process with the lowest burst time.

Among the processes given in the table, P3 will be executed next since it is having the lowest
burst time among all the available processes.

So that's how the procedure will go on in shortest job first (SJF) scheduling algorithm.

Avg Waiting Time = 27/5

Shortest Remaining Time First (SRTF) Scheduling Algorithm

This Algorithm is the preemptive version of SJF scheduling. In SRTF, the execution of the
process can be stopped after certain amount of time. At the arrival of every process, the short
term scheduler schedules the process with the least remaining burst time among the list of
available processes and the running process.

Once all the processes are available in the ready queue, No preemption will be done and the
algorithm will work as SJF scheduling. The context of the process is saved in the Process Control
Block when the process is removed from the execution and the next process is scheduled. This
PCB is accessed on the next execution of this process.

Example

In this Example, there are five jobs P1, P2, P3, P4, P5 and P6. Their arrival time and burst time are
given below in the table.

Process Arrival Burst Completion Turn Around Waiting Response


ID Time Time Time Time Time Time
1 0 8 20 20 12 0
2 1 4 10 9 5 1
3 2 2 4 2 0 2
4 3 1 5 2 1 4
5 4 3 13 9 6 10
6 5 2 7 2 0 5

Page 32
OPERATING SYSTEM

Avg Waiting Time = 24/6

The Gantt chart is prepared according to the arrival and burst time given in the table.

1. Since, at time 0, the only available process is P1 with CPU burst time 8. This is the only
available process in the list therefore it is scheduled.
2. The next process arrives at time unit 1. Since the algorithm we are using is SRTF which is a
preemptive one, the current execution is stopped and the scheduler checks for the process
with the least burst time.
Till now, there are two processes available in the ready queue. The OS has executed P1 for
one unit of time till now; the remaining burst time of P1 is 7 units. The burst time of
Process P2 is 4 units. Hence Process P2 is scheduled on the CPU according to the
algorithm.
3. The next process P3 arrives at time unit 2. At this time, the execution of process P3 is
stopped and the process with the least remaining burst time is searched. Since the process
P3 has 2 unit of burst time hence it will be given priority over others.
4. The Next Process P4 arrives at time unit 3. At this arrival, the scheduler will stop the
execution of P4 and check which process is having least burst time among the available
processes (P1, P2, P3 and P4). P1 and P2 are having the remaining burst time 7 units and 3
units respectively.

P3 and P4 are having the remaining burst time 1 unit each. Since, both are equal hence the
scheduling will be done according to their arrival time. P3 arrives earlier than P4 and
therefore it will be scheduled again.

5. The Next Process P5 arrives at time unit 4. Till this time, the Process P3 has completed its
execution and it is no more in the list. The scheduler will compare the remaining burst time
of all the available processes. Since the burst time of process P4 is 1 which is least among
all hence this will be scheduled.
6. The Next Process P6 arrives at time unit 5, till this time, the Process P4 has completed its
execution. We have 4 available processes till now, that are P1 (7), P2 (3), P5 (3) and P6 (2).
The Burst time of P6 is the least among all hence P6 is scheduled. Since, now, all the

Page 33
OPERATING SYSTEM

processes are available hence the algorithm will now work same as SJF. P6 will be executed
till its completion and then the process with the least remaining time will be scheduled.

Once all the processes arrive, No preemption is done and the algorithm will work as SJF.

Q. Given the arrival time and burst time of 3 jobs in the table below. Calculate the Average
waiting time of the system.

Process Arrival Burst Completion Turn Around Waiting


ID Time Time Time Time Time

1 0 9 13 13 4

2 1 4 5 4 0

3 2 9 22 20 11

There are three jobs P1, P2 and P3. P1 arrives at time unit 0; it will be scheduled first for the time
until the next process arrives. P2 arrives at 1 unit of time. Its burst time is 4 units which is least
among the jobs in the queue. Hence it will be scheduled next.

At time 2, P3 will arrive with burst time 9. Since remaining burst time of P2 is 3 units which are
least among the available jobs. Hence the processor will continue its execution till its completion.
Because all the jobs have been arrived so no preemption will be done now and all the jobs will be
executed till the completion according to SJF.

Avg Waiting Time = (4+0+11)/3 = 5 units

Round Robin Scheduling Algorithm

Round Robin scheduling algorithm is one of the most popular scheduling algorithm which can
actually be implemented in most of the operating systems. This is the preemptive version of first

Page 34
OPERATING SYSTEM

come first serve scheduling. The Algorithm focuses on Time Sharing. In this algorithm, every
process gets executed in a cyclic way. A certain time slice is defined in the system which is called
time quantum. Each process present in the ready queue is assigned the CPU for that time
quantum, if the execution of the process is completed during that time then the process will
terminate else the process will go back to the ready queue and waits for the next turn to
complete the execution.

Advantages

1. It can be actually implementable in the system because it is not depending on the burst
time.
2. It doesn't suffer from the problem of starvation or convoy effect.
3. All the jobs get a fare allocation of CPU.

Disadvantages

1. The higher the time quantum, the higher the response time in the system.
2. The lower the time quantum, the higher the context switching overhead in the system.
3. Deciding a perfect time quantum is really a very difficult task in the system.

RR Scheduling Example

In the following example, there are six processes named as P1, P2, P3, P4, P5 and P6. Their arrival
time and burst time are given below in the table. The time quantum of the system is 4 units.

Process ID Arrival Time Burst Time

1 0 5

2 1 6

3 2 3

Page 35
OPERATING SYSTEM

4 3 1

5 4 5

6 6 4

According to the algorithm, we have to maintain the ready queue and the Gantt chart. The
structure of both the data structures will be changed after every scheduling.

Ready Queue:

Initially, at time 0, process P1 arrives which will be scheduled for the time slice 4 units. Hence in
the ready queue, there will be only one process P1 at starting with CPU burst time 5 units.

P1

GANTT chart

The P1 will be executed for 4 units first.

Ready Queue

Meanwhile the execution of P1, four more processes P2, P3, P4 and P5 arrives in the ready queue.
P1 has not completed yet, it needs another 1 unit of time hence it will also be added back to the
ready queue.

P2 P3 P4 P5 P1

6 3 1 5 1

GANTT chart

After P1, P2 will be executed for 4 units of time which is shown in the Gantt chart.

Page 36
OPERATING SYSTEM

Ready Queue

During the execution of P2, one more process P6 is arrived in the ready queue. Since P2 has not
completed yet hence, P2 will also be added back to the ready queue with the remaining burst
time 2 units.

P3 P4 P5 P1 P6 P2

3 1 5 1 4 2

GANTT chart

After P1 and P2, P3 will get executed for 3 units of time since its CPU burst time is only 3 seconds.

Ready Queue

Since P3 has been completed, hence it will be terminated and not be added to the ready queue.
The next process will be executed is P4.

P4 P5 P1 P6 P2

1 5 1 4 2

Page 37
OPERATING SYSTEM

GANTT chart

After, P1, P2 and P3, P4 will get executed. Its burst time is only 1 unit which is lesser then the time
quantum hence it will be completed.

Ready Queue

The next process in the ready queue is P5 with 5 units of burst time. Since P4 is completed hence
it will not be added back to the queue.

P5 P1 P6 P2

5 1 4 2

GANTT chart

P5 will be executed for the whole time slice because it requires 5 units of burst time which is
higher than the time slice.

Ready Queue

P5 has not been completed yet; it will be added back to the queue with the remaining burst time
of 1 unit.

P1 P6 P2 P5

1 4 2 1

Page 38
OPERATING SYSTEM

GANTT Chart

The process P1 will be given the next turn to complete its execution. Since it only requires 1 unit
of burst time hence it will be completed.

Ready Queue

P1 is completed and will not be added back to the ready queue. The next process P6 requires
only 4 units of burst time and it will be executed next.

P6 P2 P5

4 2 1

GANTT chart

P6 will be executed for 4 units of time till completion.

Ready Queue

Since P6 is completed, hence it will not be added again to the queue. There are only two
processes present in the ready queue. The Next process P2 requires only 2 units of time.

P2 P5

2 1

Page 39
OPERATING SYSTEM

GANTT Chart

P2 will get executed again, since it only requires only 2 units of time hence this will be completed.

Ready Queue

Now, the only available process in the queue is P5 which requires 1 unit of burst time. Since the
time slice is of 4 units hence it will be completed in the next burst.

P5

GANTT chart

P5 will get executed till completion.

The completion time, Turnaround time and waiting time will be calculated as shown in the table
below.

1. Turn Around Time = Completion Time - Arrival Time


2. Waiting Time = Turn Around Time - Burst Time

Process ID Arrival Time Burst Time Completion Time Turn Around Time Waiting Time

1 0 5 17 17 12

Page 40
OPERATING SYSTEM

2 1 6 23 22 16

3 2 3 11 9 6

4 3 1 12 9 8

5 4 5 24 20 15

6 6 4 21 15 11

Avg Waiting Time = (12+16+6+8+15+11)/6 = 76/6 units

Priority Scheduling Algorithm in OS (Operating System)

In Priority scheduling, there is a priority number assigned to each process. In some systems, the
lower the number, the higher the priority. While, in the others, the higher the number, the higher
will be the priority. The Process with the higher priority among the available processes is given
the CPU. There are two types of priority scheduling algorithm exists. One is Preemptive priority
scheduling while the other is Non Preemptive Priority scheduling.

The priority number assigned to each of the process may or may not vary. If the priority number
doesn't change itself throughout the process, it is called static priority, while if it keeps changing
itself at the regular intervals, it is called dynamic priority.

Page 41
OPERATING SYSTEM

Process Synchronization in OS (Operating System)

When two or more process cooperates with each other, their order of execution must be
preserved otherwise there can be conflicts in their execution and inappropriate outputs can be
produced.

A cooperative process is the one which can affect the execution of other process or can be
affected by the execution of other process. Such processes need to be synchronized so that their
order of execution can be guaranteed.

The procedure involved in preserving the appropriate order of execution of cooperative processes
is known as Process Synchronization. There are various synchronization mechanisms that are used
to synchronize the processes.

Race Condition

A Race Condition typically occurs when two or more threads try to read, write and possibly make
the decisions based on the memory that they are accessing concurrently.

Critical Section

The regions of a program that try to access shared resources and may cause race conditions are
called critical section. To avoid race condition among the processes, we need to assure that only
one process at a time can execute within the critical section.

Critical Section Problem in OS (Operating System)

Critical Section is the part of a program which tries to access shared resources. That resource may
be any resource in a computer like a memory location, Data structure, CPU or any IO device.

The critical section cannot be executed by more than one process at the same time; operating
system faces the difficulties in allowing and disallowing the processes from entering the critical
section.

The critical section problem is used to design a set of protocols which can ensure that the Race
condition among the processes will never arise.

In order to synchronize the cooperative processes, our main task is to solve the critical section
problem. We need to provide a solution in such a way that the following conditions can be
satisfied.

Page 42
OPERATING SYSTEM

Requirements of Synchronization mechanisms

Primary

1. Mutual Exclusion

Our solution must provide mutual exclusion. By Mutual Exclusion, we mean that if one
process is executing inside critical section then the other process must not enter in the
critical section.

Page 43
2. Progress

Progress means that if one process doesn't need to execute into critical section then it
should not stop other processes to get into the critical section.

Secondary

1. Bounded Waiting

We should be able to predict the waiting time for every process to get into the critical
section. The process must not be endlessly waiting for getting into the critical section.

2. Architectural Neutrality

Our mechanism must be architectural natural. It means that if our solution is working fine
on one architecture then it should also run on the other ones as well.

Semaphores in OS (Operating System)

To get rid of the problem of wasting the wake-up signals, Dijkstra proposed an approach which
involves storing all the wake-up calls. Dijkstra states that, instead of giving the wake-up calls
directly to the consumer, producer can store the wake-up call in a variable. Any of the consumers
can read it whenever it needs to do so.

Semaphore is the variables which storesthe entire wake up calls that are being transferred from
producer to consumer. It is a variable on which read, modify and update happens automatically in
kernel mode.

Semaphore cannot be implemented in the user mode because race condition may always arise
when two or more processes try to access the variable simultaneously. It always needs support
from the operating system to be implemented.

According to the demand of the situation, Semaphore can be divided into two categories.

1. Counting Semaphore
2. Binary Semaphore or Mutex

Counting Semaphore

There are the scenarios in which more than one processes need to execute in critical section
simultaneously. However, counting semaphore can be used when we need to have more than one
process in the critical section at the same time.

44
OPERATING SYSTEMLECTURE NOTE

The programming code of semaphore implementation is shown below which includes the
structure of semaphore and the logic using which the entry and the exit can be performed in the
critical section.

1. struct Semaphore
2. {
3. int value; // processes that can enter in the critical section simultaneously.
4. queue type L; // L contains set of processes which get blocked
5. }
6. Down (Semaphore S)
7. {
8. SS.value = S.value - 1; //semaphore's value will get decreased when a new
9. //process enter in the critical section
10. if (S.value< 0)
11. {
12. put_process(PCB) in L; //if the value is negative then
13. //the process will get into the blocked state.
14. Sleep();
15. }
16. else
17. return;
18. }
19. up (Semaphore s)
20. {
21. SS.value = S.value+1; //semaphore value will get increased when
22. //it makes an exit from the critical section.
23. if(S.value<=0)
24. {
25. select a process from L; //if the value of semaphore is positive
26. //then wake one of the processes in the blocked queue.
27. wake-up();
28. }
29. }
30. }

In this mechanism, the entry and exit in the critical section are performed on the basis of the
value of counting semaphore. The value of counting semaphore at any point of time indicates the
maximum number of processes that can enter in the critical section at the same time.

P a r a m a n a n d a S a h u , D E P T . o f C S E 45 Page 45
OPERATING SYSTEMLECTURE NOTE

A process which wants to enter in the critical section first decrease the semaphore value by 1 and
then check whether it gets negative or not. If it gets negative then the process is pushed in the
list of blocked processes (i.e. q) otherwise it gets enter in the critical section.

When a process exits from the critical section, it increases the counting semaphore by 1 and then
checks whether it is negative or zero. If it is negative then that means that at least one process is
waiting in the blocked state hence, to ensure bounded waiting, the first process among the list of
blocked processes will wake up and gets enter in the critical section.

The processes in the blocked list will get waked in the order in which they slept. If the value of
counting semaphore is negative then it states the number of processes in the blocked state while
if it is positive then it states the number of slots available in the critical section.

Problem on Counting Semaphore

1. Wait → Decre → Down → P


2. Signal → Inc → Up → V

A Counting Semaphore was initialized to 12. then 10P (wait) and 4V (Signal) operations
were computed on this semaphore. What is the result?

1. S = 12 (initial)
2. 10 p (wait) :
3. SS = S -10 = 12 - 10 = 2
4. then 4 V :
5. SS = S + 4 =2 + 4 = 6

Hence, the final value of counting semaphore is 6.

Binary Semaphore or Mutex

In counting semaphore, Mutual exclusion was not provided because we has the set of processes
which required to execute in the critical section simultaneously.

However, Binary Semaphore strictly provides mutual exclusion. Here, instead of having more than
1 slots available in the critical section, we can only have at most 1 process in the critical section.
The semaphore can have only two values, 0 or 1.

Let's see the programming implementation of Binary Semaphore.

1. StructBsemaphore
2. {

P a r a m a n a n d a S a h u , D E P T . o f C S E 46 Page 46
OPERATING SYSTEMLECTURE NOTE

3. enum Value(0,1); //value is enumerated data type which can only have two values 0 or 1.

4. Queue type L;
5. }
6. /* L contains all PCBs corresponding to process
7. Blocked while processing down operation unsuccessfully.
8. */
9. Down (Bsemaphore S)
10. {
11. if (s.value == 1) // if a slot is available in the
12. //critical section then let the process enter in the queue.
13. {
14. S.value = 0; // initialize the value to 0 so that no other process can read it as 1.
15. }
16. else
17. {
18. put the process (PCB) in S.L; //if no slot is available
19. //then let the process wait in the blocked queue.
20. sleep();
21. }
22. }
23. Up (Bsemaphore S)
24. {
25. if (S.L is empty) //an empty blocked processes list implies that no process
26. //has ever tried to get enter in the critical section.
27. {
28. S.Value =1;
29. }
30. else
31. {
32. Select a process from S.L;
33. Wakeup(); // if it is not empty then wake the first process of the blocked queue.
34. }
35. }

What is Deadlock in Operating System (OS)?

Every process needs some resources to complete its execution. However, the resource is granted
in a sequential order.

1. The process requests for some resource.


2. OS grant the resource if it is available otherwise let the process waits.

P a r a m a n a n d a S a h u , D E P T . o f C S E 47 Page 47
OPERATING SYSTEMLECTURE NOTE

3. The process uses it and release on the completion.

A Deadlock is a situation where each of the computer process waits for a resource which is being
assigned to some another process. In this situation, none of the process gets executed since the
resource it needs, is held by some other process which is also waiting for some other resource to
be released.

Let us assume that there are three processes P1, P2 and P3. There are three different resources R1,
R2 and R3. R1 is assigned to P1, R2 is assigned to P2 and R3 is assigned to P3.

After some time, P1 demands for R1 which is being used by P2. P1 halts its execution since it can't
complete without R2. P2 also demands for R3 which is being used by P3. P2 also stops its
execution because it can't continue without R3. P3 also demands for R1 which is being used by P1
therefore P3 also stops its execution.

In this scenario, a cycle is being formed among the three processes. None of the process is
progressing and they are all waiting. The computer becomes unresponsive since all the processes
got blocked.

Difference between Starvation and Deadlock

Sr. Deadlock Starvation

Starvation is a situation where the low


Deadlock is a situation where no process got
1 priority process got blocked and the high
blocked and no process proceeds
priority processes proceed.

2 Deadlock is an infinite waiting. Starvation is a long waiting but not infinite.

3 Every Deadlock is always a starvation. Every starvation need not be deadlock.

4 The requested resource is blocked by the other The requested resource is continuously be

P a r a m a n a n d a S a h u , D E P T . o f C S E 48 Page 48
OPERATING SYSTEMLECTURE NOTE

process. used by the higher priority processes.

Deadlock happens when Mutual exclusion, hold


It occurs due to the uncontrolled priority and
5 and wait, No preemption and circular wait
resource management.
occurs simultaneously.

Necessary conditions for Deadlocks

1. Mutual Exclusion

A resource can only be shared in mutually exclusive manner. It implies, if two process
cannot use the same resource at the same time.

2. Hold and Wait

A process waits for some resources while holding another resource at the same time.

3. No preemption

The process which once scheduled will be executed till the completion. No other process
can be scheduled by the scheduler meanwhile.

4. Circular Wait

All the processes must be waiting for the resources in a cyclic manner so that the last
process is waiting for the resource which is being held by the first process.

P a r a m a n a n d a S a h u , D E P T . o f C S E 49 Page 49
OPERATING SYSTEMLECTURE NOTE

MODULE- III

Memory Management in Operating System

Computer Memory is basically known as a collection of data that is represented in binary format.
Main Memory refers to a physical memory that is the internal memory of 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 the 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 memory for execution. Sometimes the complete
program is loaded into the memory, but sometimes 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, which enhances the performance.

Also, at times one program is dependent on some other program. In such a case, rather than
loading all the dependent programs, the CPU links the dependent programs to the main
executing program when required. This mechanism is known as Dynamic Linking.

Let us understand how the data is being stored in the computer system.

Memory Management in Operating System

Memory Management is the process of coordinating and controlling the memory in a computer,
Blocks are assigning portions that are assigned to various running programs in order to optimize
the overall performance of the system. This technique helps in keeping the track of each and
every memory location, whether the memory location is allocated to some process or it is free.

This technique decides which process will get memory at what time. It also keeps the count of
how much memory can be allocated to a process. As it keeps the tracks of everything so
whenever some memory gets freed or unallocated it updates the status correspondingly.

Need for Memory Management in OS

Memory management technique is needed for the following reasons:

This technique helps in placing the programs in memory in such a way so that memory is utilized
at its fullest extent.

This technique helps to protect different processes from each other so that they do not
interfere with each other's operations.

P a r a m a n a n d a S a h u , D E P T . o f C S E 50 Page 50
OPERATING SYSTEMLECTURE NOTE

It helps to allocate space to different application routines.

This technique allows you to check how much memory needs to be allocated to processes
that decide which processor should get memory at what time.

It keeps the track of each memory location whether it is free or allocated.

This technique keeps the track of inventory whenever memory gets freed or unallocated
and it will update the status accordingly.

Methods Involved in Memory Management

There are various methods and with their help Memory Management can be done intelligently by
the Operating System:

Swapping in OS

A process needs to be in memory for execution. But sometimes there is not enough main
memory to hold all the currently active processes in a timesharing system. So, the excess process
is kept on disk and brought in to run dynamically. Swapping is the process of bringing in each
process in the main memory, running it for a while, and then putting it back to the disk.

Contiguous Memory Allocation

In contiguous memory allocation, each process is contained in a single contiguous block of


memory. Memory is divided into several fixed-size partitions. Each partition contains exactly one
process. When a partition is free, a process is selected from the input queue and loaded into it.
The free blocks of memory are known as holes. The set of holes is searched to determine which
hole is best to allocate.

Memory Protection

Memory protection is a phenomenon by which we control memory access rights on a computer.


The main aim of it is to prevent a process from accessing memory that has not been allocated to

P a r a m a n a n d a S a h u , D E P T . o f C S E 51 Page 51
OPERATING SYSTEMLECTURE NOTE

it. Hence prevents a bug within a process from affecting other processes, or the operating system
itself, and instead results in a segmentation fault or storage violation exception being sent to the
disturbing process, generally killing of process.

Memory Allocation in OS

Memory allocation is a process by which computer programs are assigned memory or space. It is
of three types :

1. First Fit Allocation

The first hole that is big enough is allocated to the program.

2. Best Fit Allocation

The smallest hole that is big enough is allocated to the program.

3. Worst Fit Allocation

The largest hole that is big enough is allocated to the program.

Fragmentation in OS

Fragmentation occurs in a dynamic memory allocation system when most of the free blocks are
too small to satisfy any request. It is generally termed as the inability to use the available memory.

In such a situation, processes are loaded and removed from the memory. As a result of this, free
holes exist to satisfy a request but are non-contiguous i.e. the memory is fragmented into large
no. Of small holes. This phenomenon is known as External Fragmentation.

Also, at times the physical memory is broken into fixed-size blocks, and memory is allocated in
the unit of block sizes. The memory allocated to space may be slightly larger than the requested
memory. The difference between allocated and required memory is known as Internal
fragmentation i.e. the memory that is internal to a partition but is of no use.

Paging

A solution to the fragmentation problem is Paging. Paging is a memory management mechanism


that allows the physical address space of a process to be non-contagious. Here physical memory
is divided into blocks of equal size called Pages. The pages belonging to a certain process are
loaded into available memory frames.

P a r a m a n a n d a S a h u , D E P T . o f C S E 52 Page 52
OPERATING SYSTEMLECTURE NOTE

Page Table

A Page Table is the data structure used by a virtual memory system in a computer operating
system to store the mapping between the virtual address and physical addresses.

The virtual address is also known as the Logical address and is generated by the CPU. While
Physical address is the address that actually exists on memory.

Segmentation in OS

Segmentation is another memory management scheme that supports the user-view of memory.
Segmentation allows the breaking of the virtual address space of a single process into segments
that may be placed in non-contiguous areas of physical memory.

Segmentation with Paging

Both paging and segmentation have their advantages and disadvantages, it is better to combine
these two schemes to improve on each. The combined scheme is known as 'Page the Elements'.
Each segment in this scheme is divided into pages and each segment is maintained in a page
table. So the logical address is divided into the following 3 parts :

Segment numbers(S)

Page number (P)

The displacement or offset number (D)

Logical and Physical Address Space in Operating Systems

In this tutorial, we will be covering the concept of logical and physical address space.

The address that is generated by the CPU is commonly referred to as the Logical Address. It is
basically a virtual address. The logical address is basically the address of an instruction or data
used by any program.

The set of all logical addresses that are generated by any program is referred to as Logical
Address Space.

The address that is loaded into the memory-address register of the memory is commonly
referred to as a Physical address. A physical address cannot be accessed by the user directly but
the user can calculate the physical address with the help of a Logical address.

P a r a m a n a n d a S a h u , D E P T . o f C S E 53 Page 53
OPERATING SYSTEMLECTURE NOTE

The user's program mainly generates the logical address and the user thinks that the program is
running in this logical address but the program mainly needs physical memory in order to
complete its execution.

The set of all physical addresses corresponding to the Logical addresses is commonly known as
Physical Address Space.

Memory Management Unit(MMU) in OS

It is a hardware device that does the run-time mapping from the virtual address to the physical
address. It is located within the Central Processing Unit.

Let us understand the concept of mapping with the help of a simple MMU scheme and that is a
base-register scheme.

In the above diagram, the base register is termed the Relocation register. The relocation register
is a special register in the CPU and is used for the mapping of logical addresses used by a
program to physical addresses of the system's main memory.

The value in the relocation register is added to every address that is generated by the user
process at the time when the address is sent to the memory.

MMU Example

Suppose the base is at 14000, then an attempt by the user to address location 0 is relocated
dynamically to 14000; thus access to location 356 is mapped to 14356.

It is important to note that the user program never sees the real physical addresses. The Program
can create a pointer to location 356 and store it in the memory and then manipulate it after that
compare it with other addresses as number 356.

User program always deals with the logical addresses. The Memory Mapping unit mainly
converts the logical addresses into the physical addresses. The final location of the referenced
memory address is not determined until the reference is made.

P a r a m a n a n d a S a h u , D E P T . o f C S E 54 Page 54
OPERATING SYSTEMLECTURE NOTE

There are two types of addresses that we have:

logical addresses(lies in the range 0 to max).

physical addresses(lies in the range R+0 to R+max for the base value R)

As we have told you above the user generates only a logical address and then thinks that the
process runs in locations 0 to max. However, these logical addresses must be mapped to physical
addresses before they are used.

Difference Between Logical Address and Physical Address

Let us now cover the differences between the Logical addresses and Physical addresses in the
Operating System

S.No Logical Address Physical Address

Users can access the logical User can never access the physical address
1.
address of the Program. of the Program

The logical address is generated by The physical address is located in the


2.
the CPU. memory unit.

The user can access the physical


A physical address can be accessed by a
3. address with the help of a logical
user indirectly b ut not directly.
address.

On the other hand, the physical address is


The logical address does not exist
4. a location in the memory. Thus it can be
physically in the memory and thus
termed as a Virtual address. accessed physically.

The set of all logical addresses that The set of all physical addresses
are generated by any program is corresponding to the Logical addresses is
5.
referred to as Logical Address commonly known as Physical Address
Space. Space.

P a r a m a n a n d a S a h u , D E P T . o f C S E 55 Page 55
OPERATING SYSTEMLECTURE NOTE

S.No Logical Address Physical Address

This address is generated by the It is computed by the Memory


6.
CPU. Management Unit(MMU).

Swapping in Operating System

In this tutorial, we will be covering the concept of swapping in the Operating system.

Swapping is a memory management technique and is used to temporarily remove the inactive
programs from the main memory of the computer system. Any process must be in the memory
for its execution, but can be swapped temporarily out of memory to a backing store and then
again brought back into the memory to complete its execution. Swapping is done so that other
processes get memory for their execution.

Due to the swapping technique performance usually gets affected, but it also helps in running
multiple and big processes in parallel. The swapping process is also known as a technique for
memory compaction. Basically, low priority processes may be swapped out so that processes
with a higher priority may be loaded and executed.

Let us understand this technique with the help of a figure given below:

The above diagram shows swapping of two processes where the disk is used as a Backing store.

In the above diagram, suppose there is a multiprogramming environment with a round-robin


scheduling algorithm; whenever the time quantum expires then the memory manager starts to
swap out those processes that are just finished and swap another process into the memory that
has been freed. And in the meantime, the CPU scheduler allocates the time slice to some other
processes in the memory.

P a r a m a n a n d a S a h u , D E P T . o f C S E 56 Page 56
OPERATING SYSTEMLECTURE NOTE

The swapping of processes by the memory manager is fast enough that some processes will be in
memory, ready to execute, when the CPU scheduler wants to reschedule the CPU.

A variant of the swapping technique is the priority-based scheduling algorithm. If any higher-
priority process arrives and wants service, then the memory manager swaps out lower priority
processes and then load the higher priority processes and then execute them. When the process
with higher priority finishes .then the process with lower priority swapped back in and continues
its execution. This variant is sometimes known as roll in and roll out.

There are two more concepts that come in the swapping technique and these are: swap in and
swap out.

Swap In and Swap Out in OS

The procedure by which any process gets removed from the hard disk and placed in the main
memory or RAM commonly known as Swap In.

On the other hand, Swap Out is the method of removing a process from the main memory or
RAM and then adding it to the Hard Disk.

Advantages of Swapping

The advantages/benefits of the Swapping technique are as follows:

1. The swapping technique mainly helps the CPU to manage multiple processes within a single
main memory.
2. This technique helps to create and use virtual memory.
3. With the help of this technique, the CPU can perform several tasks simultaneously. Thus,
processes need not wait too long before their execution.

4.This technique is economical.

5.This technique can be easily applied to priority-based scheduling in order to improve its
performance.

Disadvantages of Swapping

The drawbacks of the swapping technique are as follows:

1. There may occur inefficiency in the case if a resource or a variable is commonly used by
those processes that are participating in the swapping process.
2. If the algorithm used for swapping is not good then the overall method can increase the
number of page faults and thus decline the overall performance of processing.

P a r a m a n a n d a S a h u , D E P T . o f C S E 57 Page 57
OPERATING SYSTEMLECTURE NOTE

3. If the computer system loses power at the time of high swapping activity then the user
might lose all the information related to the program.

Contiguous Memory Allocation in Operating System

In this tutorial, we will cover the concept of Contiguous Memory Allocation in an Operating
System.

In the Contiguous Memory Allocation, each process is contained in a single contiguous section of
memory. In this memory allocation, all the available memory space remains together in one place
which implies that the freely available memory partitions are not spread over here and there
across the whole memory space.

In Contiguous memory allocation which is a memory management technique, whenever there is


a request by the user process for the memory then a single section of the contiguous memory
block is given to that process according to its requirement. Contiguous Memory allocation is
achieved just by dividing the memory into the fixed-sized partition.

The memory can be divided either in the fixed-sized partition or in the variable-sized
partition in order to allocate contiguous space to user processes.

We will cover the concept of different Contiguous Memory allocation techniques one by one.

Fixed-size Partition Scheme

This technique is also known as Static partitioning. In this scheme, the system divides the
memory into fixed-size partitions. The partitions may or may not be the same size. The size of
each partition is fixed as indicated by the name of the technique and it cannot be changed.

In this partition scheme, each partition may contain exactly one process. There is a problem that
this technique will limit the degree of multiprogramming because the number of partitions will
basically decide the number of processes.

Whenever any process terminates then the partition becomes available for another process.

P a r a m a n a n d a S a h u , D E P T . o f C S E 58 Page 58
OPERATING SYSTEMLECTURE NOTE

Example

Let's take an example of fixed size partitioning scheme, we will divide a memory size of 15 KB into
fixed-size partitions:

It is important to note that these partitions are allocated to the processes as they arrive and the
partition that is allocated to the arrived process basically depends on the algorithm followed.

If there is some wastage inside the partition then it is termed Internal Fragmentation.

Advantages of Fixed-size Partition Scheme

 This scheme is simple and is easy to implement


 It supports multiprogramming as multiple processes can be stored inside the main memory.
 Management is easy using this scheme

Disadvantages of Fixed-size Partition Scheme

Some disadvantages of using this scheme are as follows:

1. Internal Fragmentation

Suppose the size of the process is lesser than the size of the partition in that case some size of
the partition gets wasted and remains unused. This wastage inside the memory is generally
termed as Internal fragmentation

As we have shown in the above diagram the 70 KB partition is used to load a process of 50 KB so
the remaining 20 KB got wasted.

P a r a m a n a n d a S a h u , D E P T . o f C S E 59 Page 59
OPERATING SYSTEMLECTURE NOTE

2. Limitation on the size of the process

If in a case size of a process is more than that of a maximum-sized partition then that process
cannot be loaded into the memory. Due to this, a condition is imposed on the size of the process
and it is: the size of the process cannot be larger than the size of the largest partition.

3. External Fragmentation

It is another drawback of the fixed-size partition scheme as total unused space by various
partitions cannot be used in order to load the processes even though there is the availability of
space but it is not in the contiguous fashion.

4. Degree of multiprogramming is less

In this partition scheme, as the size of the partition cannot change according to the size of the
process. Thus the degree of multiprogramming is very less and is fixed.

Variable-size Partition Scheme

This scheme is also known as Dynamic partitioning and is came into existence to overcome the
drawback i.e internal fragmentation that is caused by Static partitioning. In this partitioning,
scheme allocation is done dynamically.

The size of the partition is not declared initially. Whenever any process arrives, a partition of size
equal to the size of the process is created and then allocated to the process. Thus the size of each
partition is equal to the size of the process.

As partition size varies according to the need of the process so in this partition scheme there is
no internal fragmentation.

Advantages of Variable-size Partition Scheme

Some Advantages of using this partition scheme are as follows:

P a r a m a n a n d a S a h u , D E P T . o f C S E 60 Page 60
OPERATING SYSTEMLECTURE NOTE

1. No Internal Fragmentation As in this partition scheme space in the main memory is


allocated strictly according to the requirement of the process thus there is no chance of
internal fragmentation. Also, there will be no unused space left in the partition.
2. Degree of Multiprogramming is Dynamic As there is no internal fragmentation in this
partition scheme due to which there is no unused space in the memory. Thus more
processes can be loaded into the memory at the same time.
3. No Limitation on the Size of Process In this partition scheme as the partition is allocated
to the process dynamically thus the size of the process cannot be restricted because the
partition size is decided according to the process size.

Disadvantages of Variable-size Partition Scheme

Some Disadvantages of using this partition scheme are as follows:

1. External Fragmentation As there is no internal fragmentation which is an advantage of


using this partition scheme does not mean there will no external fragmentation. Let us
understand this with the help of an example: In the above diagram- process P1(3MB) and
process P3(8MB) completed their execution. Hence there are two spaces left i.e. 3MB and
8MB. Let’s there is a Process P4 of size 15 MB comes. But the empty space in memory
cannot be allocated as no spanning is allowed in contiguous allocation. Because the rule
says that process must be continuously present in the main memory in order to get
executed. Thus it results in External Fragmentation.
2. Difficult Implementation The implementation of this partition scheme is difficult as
compared to the Fixed Partitioning scheme as it involves the allocation of memory at run-
time rather than during the system configuration. As we know that OS keeps the track of
all the partitions but here allocation and deallocation are done very frequently and
partition size will be changed at each time so it will be difficult for the operating system to
manage everything.

Paging in Operating Systems

In this tutorial, we will be covering the concept of Paging in the Operating System.

Paging permits the physical address space of a process to be non-contiguous. It is a fixed-size


partitioning scheme. In the Paging technique, the secondary memory and main memory are
divided into equal fixed-size partitions.

Paging solves the problem of fitting memory chunks of varying sizes onto the backing store and
this problem is suffered by many memory management schemes.

Paging helps to avoid external fragmentation and the need for compaction.

P a r a m a n a n d a S a h u , D E P T . o f C S E 61 Page 61
OPERATING SYSTEMLECTURE NOTE

Basic Method of Paging

The paging technique divides the physical memory(main memory) into fixed-size blocks that are
known as Frames and also divide the logical memory(secondary memory) into blocks of the
same size that are known as Pages.

This technique keeps the track of all the free frames.

The Frame has the same size as that of a Page. A frame is basically a place where a (logical) page
can be (physically) placed.

Each process is mainly divided into parts where the size of each part is the same as the page size.

There is a possibility that the size of the last part may be less than the page size.

 Pages of a process are brought into the main memory only when there is a requirement
otherwise they reside in the secondary storage.
 One page of a process is mainly stored in one of the frames of the memory. Also, the pages
can be stored at different locations of the memory but always the main priority is to find
contiguous frames.

Let us now cover the concept of translating a logical address into the physical address:

Translation of Logical Address into Physical Address

Before moving on further there are some important points to note:

P a r a m a n a n d a S a h u , D E P T . o f C S E 62 Page 62
OPERATING SYSTEMLECTURE NOTE

 The CPU always generates a logical address.


 In order to access the main memory always a physical address is needed.

The logical address generated by CPU always consists of two parts:

1. Page Number(p)
2. Page Offset (d)

Page Number is used to specify the specific page of the process from which the CPU wants to
read the data. and it is also used as an index to the page table.

and Page offset is mainly used to specify the specific word on the page that the CPU wants to
read.

Page Table in OS

The Page table mainly contains the base address of each page in the Physical memory. The
base address is then combined with the page offset in order to define the physical memory
address which is then sent to the memory unit.

Thus page table mainly provides the corresponding frame number (base address of the frame)
where that page is stored in the main memory.

As we have told you above that the frame number is combined with the page offset and forms
the required physical address.

So, The physical address consists of two parts:

1. Page offset(d)
2. Frame Number(f)

The Frame number is used to indicate the specific frame where the required page is stored.

and Page Offset indicates the specific word that has to be read from that page.

The Page size (like the frame size) is defined with the help of hardware. It is important to note
here that the size of the page is typically the power of 2 that varies between 512 bytes and 16 MB
per page and it mainly depends on the architecture of the computer.

If the size of logical address space is 2 raised to the power m and page size is2 raised to the
power naddressing units then the high order m-n bits of logical address designates the page
number and the n low-order bits designate the page offset.

The logical address is as follows:

P a r a m a n a n d a S a h u , D E P T . o f C S E 63 Page 63
OPERATING SYSTEMLECTURE NOTE

where p indicates the index into the page table, and d indicates the displacement within the page.

The above diagram indicates the translation of the Logical address into the Physical address. The
PTBR in the above diagram means page table base register and it basically holds the base
address for the page table of the current process.

The PTBR is mainly a processor register and is managed by the operating system. Commonly,
each process running on a processor needs its own logical address space.

But there is a problem with this approach and that is with the time required to access a user
memory location. Suppose if we want to find the location i, we must first find the index into the
page table by using the value in the PTBR offset by the page number for I. And this task requires
memory access. It then provides us the frame number which is combined with the page offset in
order to produce the actual address. After that, we can then access the desired place in the
memory.

With the above scheme, two memory accesses are needed in order to access a byte( one for the
page-table entry and one for byte). Thus memory access is slower by a factor of 2 and in most
cases, this scheme slowed by a factor of 2.

Translation of look-aside buffer(TLB)

There is the standard solution for the above problem that is to use a special, small, and fast-
lookup hardware cache that is commonly known as Translation of look-aside buffer(TLB).

P a r a m a n a n d a S a h u , D E P T . o f C S E 64 Page 64
OPERATING SYSTEMLECTURE NOTE

 TLB is associative and high-speed memory.


 Each entry in the TLB mainly consists of two parts: a key(that is the tag) and a value.
 When associative memory is presented with an item, then the item is compared
with all keys simultaneously. In case if the item is found then the corresponding
value is returned.
 The search with TLB is fast though the hardware is expensive.
 The number of entries in the TLB is small and generally lies in between 64 and 1024.

TLB is used with Page Tables in the following ways:

The TLB contains only a few of the page-table entries. Whenever the logical address is generated
by the CPU then its page number is presented to the TLB.

 If the page number is found, then its frame number is immediately available and is
used in order to access the memory. The above whole task may take less than 10
percent longer than would if an unmapped memory reference were used.
 In case if the page number is not in the TLB (which is known as TLB miss), then a
memory reference to the Page table must be made.
 When the frame number is obtained it can be used to access the memory.
Additionally, page number and frame number is added to the TLB so that they will
be found quickly on the next reference.
 In case if the TLB is already full of entries then the Operating system must select
one for replacement.
 TLB allows some entries to be wired down, which means they cannot be removed
from the TLB. Typically TLB entries for the kernel code are wired down.

Paging Hardware With TLB

Advantages of Paging

Given below are some advantages of the Paging technique in the operating system:

P a r a m a n a n d a S a h u , D E P T . o f C S E 65 Page 65
OPERATING SYSTEMLECTURE NOTE

 Paging mainly allows to storage of parts of a single process in a non-contiguous


fashion.
 With the help of Paging, the problem of external fragmentation is solved.
 Paging is one of the simplest algorithms for memory management.

Disadvantages of Paging

Disadvantages of the Paging technique are as follows:

 In Paging, sometimes the page table consumes more memory.


 Internal fragmentation is caused by this technique.
 There is an increase in time taken to fetch the instruction since now two memory
accesses are required.

Paging Hardware

Every address generated by CPU mainly consists of two parts:

1. Page Number(p)
2. Page Offset (d)

Page Number is used as an index into the page table that generally contains the base address
of each page in the physical memory.

Page offset is combined with base address in order to define the physical memory address which
is then sent to the memory unit.

If the size of logical address space is 2 raised to the power m and page size is2 raised to the
power n addressing units then the high order m-n bits of logical address designates the page
number and the n low-order bits designate the page offset.

The logical address is as follows:

where p indicates the index into the page table, and d indicates the displacement within the page.
The Page size is usually defined by the hardware. The size of the page is typically the power of 2
that varies between 512 bytes and 16 MB per page.

P a r a m a n a n d a S a h u , D E P T . o f C S E 66 Page 66
OPERATING SYSTEMLECTURE NOTE

Now its time to cover an example of Paging:

Paging Example

In order to implement paging, one of the simplest methods is to implement the page table as a
set of registers. As the size of registers is limited but the size of the page table is usually large
thus page table is kept in the main memory.

There is no External fragmentation caused due to this scheme; Any frame that is free can be
allocated to any process that needs it. But the internal fragmentation is still there.

 If any process requires n pages then at least n frames are required.


 The first page of the process is loaded into the first frame that is listed on the
free-frame list, and then the frame number is put into the page table.

The frame table is a data structure that keeps the information of which frames are allocated or
which frames are available and many more things. This table mainly has one entry for each
physical page frame.

P a r a m a n a n d a S a h u , D E P T . o f C S E 67 Page 67
OPERATING SYSTEMLECTURE NOTE

The Operating system maintains a copy of the page table for each process in the same way as it
maintains the copy of the instruction counter and register contents. Also, this copy is used to
translate logical addresses to physical addresses whenever the operating system maps a logical
address to a physical address manually.

This copy is also used by the CPU dispatcher in order to define the hardware page table whenever
a process is to be allocated to the CPU.

Structure of Page Table

In this tutorial, we will cover some of the most common techniques used for structuring the Page
table.

The data structure that is used by the virtual memory system in the operating system of a
computer in order to store the mapping between physical and logical addresses is commonly
known as Page Table.

As we had already told you that the logical address that is generated by the CPU is translated into
the physical address with the help of the page table.

 Thus page table mainly provides the corresponding frame number (base address of the frame)
where that page is stored in the main memory.

P a r a m a n a n d a S a h u , D E P T . o f C S E 68 Page 68
OPERATING SYSTEMLECTURE NOTE

The above diagram shows the paging model of Physical and logical memory.

Characteristics of the Page Table

Some of the characteristics of the Page Table are as follows:

 It is stored in the main memory.


 Generally; the Number of entries in the page table = the Number of Pages in which
the process is divided.
 PTBR means page table base register and it is basically used to hold the base
address for the page table of the current process.
 Each process has its own independent page table.

Techniques used for Structuring the Page Table

Some of the common techniques that are used for structuring the Page table are as follows:

1. Hierarchical Paging
2. Hashed Page Tables
3. Inverted Page Tables

Let us cover these techniques one by one;

Hierarchical Paging

Another name for Hierarchical Paging is multilevel paging.

 There might be a case where the page table is too big to fit in a contiguous space,
so we may have a hierarchy with several levels.
 In this type of Paging the logical address space is broke up into Multiple page
tables.
 Hierarchical Paging is one of the simplest techniques and for this purpose, a two-
level page table and three-level page table can be used.
P a r a m a n a n d a S a h u , D E P T . o f C S E 69 Page 69
OPERATING SYSTEMLECTURE NOTE

Two Level Page Table

Consider a system having 32-bit logical address space and a page size of 1 KB and it is further
divided into:

 Page Number consisting of 22 bits.


 Page Offset consisting of 10 bits.

As we page the Page table, the page number is further divided into :

 Page Number consisting of 12 bits.


 Page Offset consisting of 10 bits.

Thus the Logical address is as follows:

In the above diagram,

P1 is an index into the Outer Page table.

P2 indicates the displacement within the page of the Inner page Table.

As address translation works from outer page table inward so is known as forward-mapped
Page Table.

Below given figure below shows the Address Translation scheme for a two-level page table

Three Level Page Table

For a system with 64-bit logical address space, a two-level paging scheme is not appropriate. Let
us suppose that the page size, in this case, is 4KB.If in this case, we will use the two-page level
scheme then the addresses will look like this:

P a r a m a n a n d a S a h u , D E P T . o f C S E 70 Page 70
OPERATING SYSTEMLECTURE NOTE

Thus in order to avoid such a large table, there is a solution and that is to divide the outer page
table, and then it will result in a Three-level page table:

Hashed Page Tables

This approach is used to handle address spaces that are larger than 32 bits.

 In this virtual page, the number is hashed into a page table.


 This Page table mainly contains a chain of elements hashing to the same elements.

Each element mainly consists of :

1. The virtual page number


2. The value of the mapped page frame.
3. A pointer to the next element in the linked list.

Given below figure shows the address translation scheme of the Hashed Page Table:

The above Figure shows Hashed Page Table

P a r a m a n a n d a S a h u , D E P T . o f C S E 71 Page 71
OPERATING SYSTEMLECTURE NOTE

The Virtual Page numbers are compared in this chain searching for a match; if the match is found
then the corresponding physical frame is extracted.

In this scheme, a variation for 64-bit address space commonly uses clustered page tables.

Clustered Page Tables

 These are similar to hashed tables but here each entry refers to several pages (that
is 16) rather than 1.
 Mainly used for sparse address spaces where memory references are non-
contiguous and scattered

Inverted Page Tables

The Inverted Page table basically combines A page table and A frame table into a single data
structure.

 There is one entry for each virtual page number and a real page of memory
 And the entry mainly consists of the virtual address of the page stored in that real
memory location along with the information about the process that owns the page.
 Though this technique decreases the memory that is needed to store each page
table; but it also increases the time that is needed to search the table whenever a
page reference occurs.

Given below figure shows the address translation scheme of the Inverted Page Table:

In this, we need to keep the track of process id of each entry, because many processes may have
the same logical addresses.

Also, many entries can map into the same index in the page table after going through the hash
function. Thus chaining is used in order to handle this.

P a r a m a n a n d a S a h u , D E P T . o f C S E 72 Page 72
OPERATING SYSTEMLECTURE NOTE

Segmentation in Operating Systems

In this tutorial, we will be covering segmentation in the Operating System.

Segmentation is another way of dividing the addressable memory. It is another scheme of


memory management and it generally supports the user view of memory. The Logical address
space is basically the collection of segments. Each segment has a name and a length.

Basically, a process is divided into segments. Like paging, segmentation divides or segments the
memory. But there is a difference and that is while the paging divides the memory into a fixed
size and on the other hand, segmentation divides the memory into variable segments these are
then loaded into logical memory space.

A Program is basically a collection of segments. And a segment is a logical unit such as:

 main program
 procedure
 function
 method
 object
 local variable and global variables.
 symbol table
 common block
 stack
 arrays

Types of Segmentation

Given below are the types of Segmentation:

 Virtual Memory Segmentation With this type of segmentation, each process is


segmented into n divisions and the most important thing is they are not segmented
all at once.
 Simple Segmentation With the help of this type, each processis segmented into n
divisions and they are all together segmented at once exactly but at the runtime
and can be non-contiguous (that is they may be scattered in the memory).

Characteristics of Segmentation

Some characteristics of the segmentation technique are as follows:

 The Segmentation partitioning scheme is variable-size.


 Partitions of the secondary memory are commonly known as segments.

P a r a m a n a n d a S a h u , D E P T . o f C S E 73 Page 73
OPERATING SYSTEMLECTURE NOTE

 Partition size mainly depends upon the length of modules.


 Thus with the help of this technique, secondary memory and main memory are
divided into unequal-sized partitions.

Need of Segmentation

One of the important drawbacks of memory management in the Operating system is the
separation of the user's view of memory and the actual physical memory. and Paging is a
technique that provides the separation of these two memories.

User's view is basically mapped onto physical storage. And this mapping allows differentiation
between Physical and logical memory.

It is possible that the operating system divides the same function into different pages and those
pages may or may not be loaded into the memory at the same time also Operating system does
not care about the User's view of the process. Due to this technique system's efficiency decreases.

Segmentation is a better technique because it divides the process into segments.

User's View of a Program

Given below figure shows the user's view of segmentation:

logical address

Basic Method

A computer system that is using segmentation has a logical address space that can be viewed as
multiple segments. And the size of the segment is of the variable that is it may grow or shrink. As
we had already told you that during the execution each segment has a name and length. And the
address mainly specifies both thing name of the segment and the displacement within the
segment.

Therefore the user specifies each address with the help of two quantities: segment name and
offset.

P a r a m a n a n d a S a h u , D E P T . o f C S E 74 Page 74
OPERATING SYSTEMLECTURE NOTE

For simplified Implementation segments are numbered; thus referred to as segment number
rather than segment name.

Thus the logical address consists of two tuples:

<segment-number,offset>

Segment Number(s): Segment Number is used to represent the number of bits that are required
to represent the segment.

Offset(d) Segment offset is used to represent the number of bits that are required to represent
the size of the segment.

Segmentation Architecture

Segment Table

A Table that is used to store the information of all segments of the process is commonly known
as Segment Table. Generally, there is no simple relationship between logical addresses and
physical addresses in this scheme.

 The mapping of a two-dimensional Logical address into a one-dimensional Physical


address is done using the segment table.
 This table is mainly stored as a separate segment in the main memory.
 The table that stores the base address of the segment table is commonly known as
the Segment table base register (STBR)

In the segment table each entry has :

1. Segment Base/base address: The segment base mainly contains the starting
physical address where the segments reside in the memory.
2. Segment Limit: The segment limit is mainly used to specify the length of the
segment.

Segment Table Base Register(STBR) The STBR register is used to point the segment table's
location in the memory.

Segment Table Length Register(STLR) This register indicates the number of segments used by
a program. The segment number s is legal if s<STLR

P a r a m a n a n d a S a h u , D E P T . o f C S E 75 Page 75
OPERATING SYSTEMLECTURE NOTE

Segmentation Hardware

Given below figure shows the segmentation hardware :

The logical address generated by CPU consist of two parts:

Segment Number(s): It is used as an index into the segment table.

Offset(d): It must lie in between '0' and 'segment limit'.In this case, if the Offset exceeds the
segment limit then the trap is generated.

Thus; correct offset+segment base= address in Physical memory

and segment table is basically an array of base-limit register pair.

Advantages of Segmentation

The Advantages of the Segmentation technique are as follows:

 In the Segmentation technique, the segment table is mainly used to keep the record
of segments. Also, the segment table occupies less space as compared to the
paging table.
 There is no Internal Fragmentation.
 Segmentation generally allows us to divide the program into modules that provide
better visualization.
 Segments are of variable size.
P a r a m a n a n d a S a h u , D E P T . o f C S E 76 Page 76
OPERATING SYSTEMLECTURE NOTE

Disadvantages of Segmentation

Some disadvantages of this technique are as follows:

 Maintaining a segment table for each process leads to overhead


 This technique is expensive.
 The time is taken in order to fetch the instruction increases since now two memory
accesses are required.
 Segments are of unequal size in segmentation and thus are not suitable for
swapping.
 This technique leads to external fragmentation as the free space gets broken down
into smaller pieces along with the processes being loaded and removed from the
main memory then this will result in a lot of memory waste.

Example of Segmentation

Given below is the example of the segmentation, There are five segments numbered from 0 to 4.
These segments will be stored in Physical memory as shown. There is a separate entry for each
segment in the segment table which contains the beginning entry address of the segment in the
physical memory( denoted as the base) and also contains the length of the segment(denoted as
limit).

Segment 2 is 400 bytes long and begins at location 4300. Thus in this case a reference to byte 53
of segment 2 is mapped onto the location 4300 (4300+53=4353). A reference to segment 3, byte
85 is mapped to 3200(the base of segment 3)+852=4052.

A reference to byte 1222 of segment 0 would result in the trap to the OS, as the length of this
segment is 1000 bytes.

P a r a m a n a n d a S a h u , D E P T . o f C S E 77 Page 77
OPERATING SYSTEMLECTURE NOTE

Virtual Memory in Operating Systems

In this tutorial, we will be covering the concept of Virtual Memory in an Operating System.

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 a large virtual memory is provided for user programs when a
very small physical memory is there. Thus Virtual memory is a technique that allows the execution
of processes that are not in the physical memory completely.

Virtual Memory mainly gives the illusion of more physical memory than there really is with the
help of Demand Paging.

In real scenarios, most processes never need all their pages at once, for the 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.

In an Operating system, the memory is usually stored in the form of units that are known as
pages. Basically, these are atomic units used to store large programs.

Virtual memory can be implemented with the help of:-

1. Demand Paging
2. Demand Segmentation

The Need for Virtual Memory

Following are the reasons due to which there is a need for Virtual Memory:

 In case, if a computer running the Windows operating system needs more memory
or RAM than the memory installed in the system then it uses a small portion of the
hard drive for this purpose.
 Suppose there is a situation when your computer does not have space in the
physical memory, then it writes things that it needs to remember into the hard disk
in a swap file and that as virtual memory.

P a r a m a n a n d a S a h u , D E P T . o f C S E 78 Page 78
OPERATING SYSTEMLECTURE NOTE

Benefits of having Virtual Memory

1. Large programs can be written, as the 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 little space on actual physical memory.
4. Therefore, the Logical address space can be much larger than that of physical
address space.
5. Virtual memory allows address spaces to be shared by several processes.
6. During the process of creation, virtual memory allows: copy-on-write and
Memory-mapped files

Execution of the Program in the Virtual memory

With the help of the Operating system few pieces of the program are brought into the main
memory:

 A portion of the process that is brought in the main memory is known as Resident Set.

Whenever an address is needed that is not in the main memory, then it generates an interrupt.
The process is placed in the blocked state by the Operating system. Those pieces of the process
that contains the logical address are brought into the main 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). Initially, only those pages are loaded which will be required by the process immediately.

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

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 processes into 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.

P a r a m a n a n d a S a h u , D E P T . o f C S E 79 Page 79
OPERATING SYSTEMLECTURE NOTE

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.

Thrashing

A process that is spending more time paging than executing is said to be thrashing. In other
words, it means, that the process doesn't have enough frames to hold all the pages for its
execution, so it is swapping pages in and out very frequently to keep executing. Sometimes, the
pages which will be required in the near future have to be swapped out.

Initially, when the CPU utilization is low, the process scheduling mechanism, to increase the level
of multiprogramming loads multiple processes into the memory at the same time, allocating a
limited amount of frames to each process. As the memory fills up, the process starts to spend a
lot of time for the required pages to be swapped in, again leading to low CPU utilization because
most of the processes are waiting for pages. Hence the scheduler loads more processes to
increase CPU utilization, as this continues at a point in time the complete system comes to a stop.

Advantages of Virtual Memory

Given below are the advantages of using Virtual Memory:

 Virtual Memory allows you to run more applications at a time.


 With the help of virtual memory, you can easily fit many large programs into smaller
programs.
 With the help of Virtual memory, a multiprogramming environment can be easily
implemented.
 As more processes should be maintained in the main memory which leads to the
effective utilization of the CPU.
 Data should be read from the disk at the time when required.
 Common data can be shared easily between memory.
 With the help of virtual memory, speed is gained when only a particular segment of
the program is required for the execution of the program.
 The process may even become larger than all of the physical memory.

Disadvantages of Virtual Memory

Given below are the drawbacks of using Virtual Memory:

 Virtual memory reduces the stability of the system.


 The performance of Virtual memory is not as good as that of RAM.
 If a system is using virtual memory then applications may run slower.

P a r a m a n a n d a S a h u , D E P T . o f C S E 80 Page 80
OPERATING SYSTEMLECTURE NOTE

 Virtual memory negatively affects the overall performance of a system.


 Virtual memory occupies the storage space, which might be otherwise used for
long-term data storage.
 This memory takes more time in order to switch between applications.

Demand Paging in Operating Systems

Every process in the virtual memory contains lots of pages and in some cases, it might not be
efficient to swap all the pages for the process at once. Because it might be possible that the
program may need only a certain page for the application to run. Let us take an example here,
suppose there is a 500 MB application and it may need as little as 100MB pages to be swapped,
so in this case, there is no need to swap all pages at once.

The demand paging system is somehow similar to the paging system with swapping where
processes mainly reside in the main memory(usually in the hard disk). Thus demand paging is the
process that solves the above problem only by swapping the pages on Demand. This is also
known as lazy swapper( It never swaps the page into the memory unless it is needed).

Swapper that deals with the individual pages of a process are referred to as Pager.

Demand Paging is a technique in which a page is usually brought into the main memory only
when it is needed or demanded by the CPU. Initially, only those pages are loaded that are
required by the process immediately. Those pages that are never accessed are thus never loaded
into the physical memory.

Figure: Transfer of a Paged Memory to the contiguous disk space.

Whenever a page is needed? make a reference to it;

P a r a m a n a n d a S a h u , D E P T . o f C S E 81 Page 81
OPERATING SYSTEMLECTURE NOTE

 If the reference is invalid then abort it.


 If the page is Not-in-memory then bring it to memory.

Valid-Invalid Bit

Some form of hardware support is used to distinguish between the pages that are in the memory
and the pages that are on the disk. Thus for this purpose Valid-Invalid scheme is used:

 With each page table entry, a valid-invalid bit is associated( where 1 indicates in the
memory and 0 indicates not in the memory)
 Initially, the valid-invalid bit is set to 0 for all table entries.

1. If the bit is set to "valid", then the associated page is both legal and is in memory.
2. If the bit is set to "invalid" then it indicates that the page is either not valid or the page is
valid but is currently not on the disk.

 For the pages that are brought into the memory, the page table is set as usual.
 But for the pages that are not currently in the memory, the page table is either
simply marked as invalid or it contains the address of the page on the disk.

During the translation of address, if the valid-invalid bit in the page table entry is 0 then it leads
to page fault.

The above figure is to indicates the page table when some pages are not in the main memory.

Demand Paging Works

First of all the components that are involved in the Demand paging process are as follows:

 Main Memory
 CPU
 Secondary Memory
 Interrupt
P a r a m a n a n d a S a h u , D E P T . o f C S E 82 Page 82
OPERATING SYSTEMLECTURE NOTE

 Physical Address space


 Logical Address space
 Operating System
 Page Table

1. If a page is not available in the main memory in its active state; then a request may
be made to the CPU for that page. Thus for this purpose, it has to generate an interrupt.
2. After that, the Operating system moves the process to the blocked state as an
interrupt has occurred.
3. Then after this, the Operating system searches the given page in the Logical address
space.
4. And Finally with the help of the page replacement algorithms, replacements are
made in the physical address space. Page tables are updated simultaneously.
5. After that, the CPU is informed about that update and then asked to go ahead with
the execution and the process gets back into its ready state.

When the process requires any of the pages that are not loaded into the memory, a page fault
trap is triggered and the 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 it is 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 the 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.

Advantages of Demand Paging

The benefits of using the Demand Paging technique are as follows:

 With the help of Demand Paging, memory is utilized efficiently.


 Demand paging avoids External Fragmentation.
 Less Input/Output is needed for Demand Paging.
 This process is not constrained by the size of physical memory.
 With Demand Paging it becomes easier to share the pages.
 With this technique, portions of the process that are never called are never loaded.
 No compaction is required in demand Paging.

P a r a m a n a n d a S a h u , D E P T . o f C S E 83 Page 83
OPERATING SYSTEMLECTURE NOTE

Disadvantages of Demand paging

Drawbacks of Demand Paging are as follows:

 There is an increase in overheads due to interrupts and page tables.


 Memory access time in demand paging is longer.

Pure Demand Paging

In some cases when initially no pages are loaded into the memory, pages in such cases are only
loaded when are demanded by the process by generating page faults. It is then referred to as
Pure Demand Paging.

 In the case of pure demand paging, there is not even a single page that is loaded
into the memory initially. Thus pure demand paging causes the page fault.
 When the execution of the process starts with no pages in the memory, then the
operating system sets the instruction pointer to the first instruction of the process
and that is on a non-memory resident page and then in this case the process
immediately faults for the page.
 After that when this page is brought into the memory then the process continues its
execution, page fault is necessary until every page that it needs is in the memory.
 And at this point, it can execute with no more faults.
 This scheme is referred to as Pure Demand Paging: means never bring a page into
the memory until it is required.

Copy-on-Write in Operating System

In this tutorial, we will be covering the Copy on Write which is one of the resource management
techniques.

Copy-on-Write(CoW) is mainly a resource management technique that allows the parent and
child process to share the same pages of the memory initially. If any process either parent or
child modifies the shared page, only then the page is copied.

The CoW is basically a technique of efficiently copying the data resources in the computer
system. In this case, if a unit of data is copied but is not modified then "copy" can mainly exist as
a reference to the original data.

But when the copied data is modified, then at that time its copy is created(where new bytes are
actually written )as suggested from the name of the technique.

P a r a m a n a n d a S a h u , D E P T . o f C S E 84 Page 84
OPERATING SYSTEMLECTURE NOTE

The main use of this technique is in the implementation of the fork system call in which it shares
the virtual memory/pages of the Operating system.

Recall in the UNIX(OS), the fork() system call is used to create a duplicate process of the parent
process which is known as the child process.

 The CoW technique is used by several Operating systems like Linux, Solaris, and
Windows XP.
 The CoW technique is an efficient process creation technique as only the pages that
are modified are copied.

Free pages in this technique are allocated from a pool of zeroed-out pages.

The Copy on Write(CoW) Technique

The main intention behind the CoW technique is that whenever a parent process creates a child
process both parent and child process initially will share the same pages in the memory.

These shared pages between parent and child process will be marked as copy-on-write
which means that if the parent or child process will attempt to modify the shared pages
then a copy of these pages will be created and the modifications will be done only on the
copy of pages by that process and it will not affect other processes.

Now its time to take a look at the basic example of this technique:

Let us take an example where Process A creates a new process that is Process B, initially both
these processes will share the same pages of the memory.

Figure: Above figure indicates parent and child process sharing the same pages

Now, let us assume that process A wants to modify a page in the memory. When the Copy-on-
write(CoW) technique is used, only those pages that are modified by either process are copied;
all the unmodified pages can be easily shared by the parent and child process.
P a r a m a n a n d a S a h u , D E P T . o f C S E 85 Page 85
OPERATING SYSTEMLECTURE NOTE

Figure: After Page Z is modified by Process A

Whenever it is determined that a page is going to be duplicated using the copy-on-write


technique, then it is important to note the location from where the free pages will be allocated.
There is a pool of free pages for such requests; provided by many operating systems.

And these free pages are allocated typically when the stack/heap for a process must expand or
when there are copy-on-write pages to manage.

These pages are typically allocated using the technique that is known as Zero-fill-on-demand.
And the Zero-fill-on-demand pages are zeroed-out before being allocated and thus erasing the
previous content.

Page Fault in Operating System

Page fault dominates more like an error. It mainly occurs when any program tries to access the
data or the code that is in the address space of the program, but that data is not currently located
in the RAM of the system.

 So basically when the page referenced by the CPU is not found in the main memory
then the situation is termed as Page Fault.
 Whenever any page fault occurs, then the required page has to be fetched from the
secondary memory into the main memory.

In case if the required page is not loaded into the memory, then a page fault trap arises

The page fault mainly generates an exception, which is used to notify the operating system that it
must have to retrieve the "pages" from the virtual memory in order to continue the execution.
Once all the data is moved into the physical memory the program continues its execution
normally. The Page fault process takes place in the background and thus goes unnoticed by the
user.

P a r a m a n a n d a S a h u , D E P T . o f C S E 86 Page 86
OPERATING SYSTEMLECTURE NOTE

 The hardware of the computer tracks to the kernel and the program counter (PC) is
generally saved on the stack.CPU registers store the information of the current state
of instruction.
 An assembly program is started that usually saves the general registers and also
saves the other volatile information to prevent the OS from destroying it.

Handling the Page Fault

Given below is the simple procedure to handle the page fault:

Figure: Steps to Handle the Page fault

If you will access a page that is marked as invalid then it also causes a Page Fault. Then the
Paging hardware during translating the address through the page table will notice that the invalid
bit is set that will cause a trap to the Operating system.

This trap is mainly the result of the failure of the Operating system in order to bring the desired
page into memory.

Let us understand the procedure to handle the page fault as shown with the help of the above
diagram:

1. First of all, internal table(that is usually the process control block) for this process in order
to determine whether the reference was valid or invalid memory access.
2. If the reference is invalid, then we will terminate the process. If the reference is valid, but
we have not bought in that page so now we just page it in.
3. Then we locate the free frame list in order to find the free frame.
4. Now a disk operation is scheduled in order to read the desired page into the newly
allocated frame.

P a r a m a n a n d a S a h u , D E P T . o f C S E 87 Page 87
OPERATING SYSTEMLECTURE NOTE

5. When the disk is completely read, then the internal table is modified that is kept with the
process, and the page table that mainly indicates the page is now in memory.
6. Now we will restart the instruction that was interrupted due to the trap. Now the process
can access the page as though it had always been in memory.

Page Replacement Algorithms

In this tutorial, we will be covering the concept of Page replacement and its algorithms in the
Operating system.

As studied in Demand Paging, only certain pages of a process are loaded initially into the
memory. This allows us to get more processes into 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.

In this case, if a process requests a new page and supposes there are no free frames, then the
Operating system needs to decide which page to replace. The operating system must use any
page replacement algorithm in order to select the victim frame. The Operating system must then
write the victim frame to the disk then read the desired page into the frame and then update the
page tables. And all these require double the disk access time.

 Page replacement prevents the over-allocation of the memory by modifying the


page-fault service routine.
 To reduce the overhead of page replacement a modify bit (dirty bit) is used in
order to indicate whether each page is modified.
 This technique provides complete separation between logical memory and physical
memory.

Page Replacement

In Virtual Memory Management, Page Replacement Algorithms play an important role. The main
objective of all the Page replacement policies is to decrease the maximum number of page
faults.

P a r a m a n a n d a S a h u , D E P T . o f C S E 88 Page 88
OPERATING SYSTEMLECTURE NOTE

Page Fault – It is basically a memory error, and it occurs when the current programs attempt to
access the memory page for mapping into virtual address space, but it is unable to load into the
physical memory then this is referred to as Page fault.

Basic Page Replacement Algorithm

Page Replacement technique uses the following approach. If there is no free frame, then we will
find the one that is not currently being used and then free it. A-frame can be freed by writing its
content to swap space and then change the page table in order to indicate that the page is no
longer in the memory.

1. First of all, find the location of the desired page on the disk.
2. Find a free Frame: a) If there is a free frame, then use it. b) If there is no free frame
then make use of the page-replacement algorithm in order to select the victim
frame. c) Then after that write the victim frame to the disk and then make the
changes in the page table and frame table accordingly.
3. After that read the desired page into the newly freed frame and then change the
page and frame tables.
4. Restart the process.

Figure: Page Replacement

Page Replacement Algorithms

This algorithm helps to decide which pages must be swapped out from the main memory in order
to create a room for the incoming page. This Algorithm wants the lowest page-fault rate.

Various Page Replacement algorithms used in the Operating system are as follows;

P a r a m a n a n d a S a h u , D E P T . o f C S E 89 Page 89
OPERATING SYSTEMLECTURE NOTE

Let us discuss all algorithms one by one in the upcoming sections:

1. FIFO Page Replacement Algorithm

It is a very simple way of Page replacement and is referred to as First in First Out. This algorithm
mainly replaces the oldest page that has been present in the main memory for the longest time.

 This algorithm is implemented by keeping the track of all the pages in the queue.

 As new pages are requested and are swapped in, they are added to the tail of a
queue and the page which is at the head becomes the victim.
 This is not an effective way of page replacement but it can be used for small
systems.

Advantages

 This algorithm is simple and easy to use.


 FIFO does not cause more overhead.

Disadvantage

 This algorithm does not make the use of the frequency of last used time rather it
just replaces the Oldest Page.
 There is an increase in page faults as page frames increases.
 The performance of this algorithm is the worst.

Q) Consider a main memory with five page frames and the following sequence of
page references: 3, 8, 2, 3, 9, 1, 6, 3, 8, 9, 3, 6, 2, 1, 3. which one of the following
is true with respect to page replacement policies First-In-First-out (FIFO) and
Least Recently Used (LRU)?

P a r a m a n a n d a S a h u , D E P T . o f C S E 90 Page 90
OPERATING SYSTEMLECTURE NOTE

A. Both incur the same number of page faults


B. FIFO incurs 2 more page faults than LRU
C. LRU incurs 2 more page faults than FIFO
D. FIFO incurs 1 more page faults than LRU

Solution:

Number of frames = 5

FIFO

According to FIFO, the page which first comes in the memory will first goes out.

Number of Page Faults = 9


Number of hits = 6

LRU

According to LRU, the page which has not been requested for a long time will get replaced with
the new one.

P a r a m a n a n d a S a h u , D E P T . o f C S E 91 Page 91
OPERATING SYSTEMLECTURE NOTE

Number of Page Faults = 9


Number of Hits = 6

The Number of page faults in both the cases is equal therefore the Answer is option (A).

2. LIFO Page Replacement Algorithm

This Page Replacement algorithm stands for "Last In First Out".This algorithm works in a similar
way to the LIFO principle.

 In this, the newest page is replaced which is arrived at last in the primary memory
 This algorithm makes use of the stack for monitoring all the pages.

3. LRU Page Replacement Algorithm in OS

This algorithm stands for "Least recent used" and this algorithm helps the Operating system to
search those pages that are used over a short duration of time frame.

 The page that has not been used for the longest time in the main memory will be
selected for replacement.
 This algorithm is easy to implement.
 This algorithm makes use of the counter along with the even-page.

Advantages of LRU

 It is an efficient technique.
 With this algorithm, it becomes easy to identify the faulty pages that are not
needed for a long time.
 It helps in Full analysis.

Disadvantages of LRU

 It is expensive and has more complexity.

 There is a need for an additional data structure.

4. Optimal Page Replacement Algorithm

This algorithm mainly replaces the page that will not be used for the longest time in the future.
The practical implementation of this algorithm is not possible.

 Practical implementation is not possible because we cannot predict in advance


those pages that will not be used for the longest time in the future.

P a r a m a n a n d a S a h u , D E P T . o f C S E 92 Page 92
OPERATING SYSTEMLECTURE NOTE

 This algorithm leads to less number of page faults and thus is the best-known
algorithm

Also, this algorithm can be used to measure the performance of other algorithms.

Advantages of OPR

 This algorithm is easy to use.


 This algorithm provides excellent efficiency and is less complex.
 For the best result, the implementation of data structures is very easy

Disadvantages of OPR

 In this algorithm future awareness of the program is needed.


 Practical Implementation is not possible because the operating system is unable to
track the future request

5. Random Page Replacement Algorithm

As indicated from the name this algorithm replaces the page randomly. This Algorithm can work
like any other page replacement algorithm that is LIFO, FIFO, Optimal, and LRU.

Q. Consider a reference string: 4, 7, 6, 1, 7, 6, 1, 2, 7, 2. the number of frames in


the memory is 3. Find out the number of page faults respective to:

1. Optimal Page Replacement Algorithm


2. FIFO Page Replacement Algorithm
3. LRU Page Replacement Algorithm

Optimal Page Replacement Algorithm

Number of Page Faults in Optimal Page Replacement Algorithm = 5

LRU Page Replacement Algorithm

P a r a m a n a n d a S a h u , D E P T . o f C S E 93 Page 93
OPERATING SYSTEMLECTURE NOTE

Number of Page Faults in LRU = 6

FIFO Page Replacement Algorithm

Number of Page Faults in FIFO = 6

Belady’s Anomaly

In the case of LRU and optimal page replacement algorithms, it is seen that the number of page
faults will be reduced if we increase the number of frames. However, Balady found that, In FIFO
page replacement algorithm, the number of page faults will get increased with the increment in
number of frames.

This is the strange behavior shown by FIFO algorithm in some of the cases. This is an Anomaly
called as Belady'sAnomaly.

Let's examine such example :

The reference String is given as 0 1 5 3 0 1 4 0 1 5 3 4. Let's analyze the behavior of FIFO


algorithm in two cases.

Case 1: Number of frames = 3

P a r a m a n a n d a S a h u , D E P T . o f C S E 94 Page 94
OPERATING SYSTEMLECTURE NOTE

Request 0 1 5 3 0 1 4 0 1 5 3 4
Frame 3 5 5 5 1 1 1 1 1 3 3
Frame 2 1 1 1 0 0 0 0 0 5 5 5
Frame 1 0 0 0 3 3 3 4 4 4 4 4 4
Miss/Hit Miss Miss Miss Miss Miss Miss Miss Hit Hit Miss Miss Hit

Number of Page Faults = 9

Case 2: Number of frames = 4

Request 0 1 5 3 0 1 4 0 1 5 3 4
Frame 4 3 3 3 3 3 3 5 5 5
Frame 3 5 5 5 5 5 5 1 1 1 1
Frame 2 1 1 1 1 1 1 0 0 0 0 4
Frame 1 0 0 0 0 0 0 4 4 4 4 3 3
Miss/Hit Miss Miss Miss Miss Hit Hit Miss Miss Miss Miss Miss Miss

Number of Page Faults = 10

Therefore, in this example, the number of page faults is increasing by increasing the number of
frames hence this suffers from Belady's Anomaly.

Thrashing in Operating System

In this tutorial, we will be covering the concept of thrashing in the Operating system.

In case, if the page fault and swapping happens very frequently at a higher rate, then the
operating system has to spend more time swapping these pages. This state in the operating
system is termed thrashing. Because of thrashing the CPU utilization is going to be reduced.

Let's understand by an example, if any process does not have the number of frames that it needs
to support pages in active use then it will quickly page fault. And at this point, the process must
replace some pages. As all the pages of the process are actively in use, it must replace a page that
will be needed again right away. Consequently, the process will quickly fault again, and again, and
again, replacing pages that it must bring back in immediately. This high paging activity by a
process is called thrashing.

P a r a m a n a n d a S a h u , D E P T . o f C S E 95 Page 95
OPERATING SYSTEMLECTURE NOTE

During thrashing, the CPU spends less time on some actual productive work spend more time
swapping.

Figure: Thrashing

Causes of Thrashing

Thrashing affects the performance of execution in the Operating system. Also, thrashing results in
severe performance problems in the Operating system.

When the utilization of CPU is low, then the process scheduling mechanism tries to load many
processes into the memory at the same time due to which degree of Multiprogramming can be
increased. Now in this situation, there are more processes in the memory as compared to the
available number of frames in the memory. Allocation of the limited amount of frames to each
process.

Whenever any process with high priority arrives in the memory and if the frame is not freely
available at that time then the other process that has occupied the frame is residing in the frame
will move to secondary storage and after that this free frame will be allocated to higher priority
process.

We can also say that as soon as the memory fills up, the process starts spending a lot of time for
the required pages to be swapped in. Again the utilization of the CPU becomes low because most
of the processes are waiting for pages.

Thus a high degree of multiprogramming and lack of frames are two main causes of thrashing in
the Operating system.

Effect of Thrashing

At the time, when thrashing starts then the operating system tries to apply either the Global
page replacement Algorithm or the Local page replacement algorithm.

P a r a m a n a n d a S a h u , D E P T . o f C S E 96 Page 96
OPERATING SYSTEMLECTURE NOTE

Global Page Replacement

The Global Page replacement has access to bring any page, whenever thrashing found it tries to
bring more pages. Actually, due to this, no process can get enough frames and as a result, the
thrashing will increase more and more. Thus the global page replacement algorithm is not
suitable whenever thrashing happens.

Local Page Replacement

Unlike the Global Page replacement, the local page replacement will select pages which only
belongs to that process. Due to this, there is a chance of a reduction in the thrashing. As it is also
proved that there are many disadvantages of Local Page replacement. Thus local page
replacement is simply an alternative to Global Page replacement.

Techniques used to handle the thrashing

As we have already told you the Local Page replacement is better than the Global Page
replacement but local page replacement has many disadvantages too, so it is not suggestible.
Thus given below are some other techniques that are used:

Working-Set Model

This model is based on the assumption of the locality. It makes the use of the parameter ? in
order to define the working-set window. The main idea is to examine the most recent? page
reference. What locality is saying, the recently used page can be used again, and also the pages
that are nearby this page will also be used?

1. Working Set

The set of the pages in the most recent? page reference is known as the working set. If a page is
in active use, then it will be in the working set. In case if the page is no longer being used then it
will drop from the working set ? times after its last reference.

The working set mainly gives the approximation of the locality of the program.

The accuracy of the working set mainly depends on? what is chosen?

This working set model avoids thrashing while keeping the degree of multiprogramming as high
as possible.

P a r a m a n a n d a S a h u , D E P T . o f C S E 97 Page 97
OPERATING SYSTEMLECTURE NOTE

2. Page Fault Frequency

Figure: Page fault frequency

The working-set model is successful and its knowledge can be useful in preparing but it is a very
clumpy approach in order to avoid thrashing. There is another technique that is used to avoid
thrashing and it is Page Fault Frequency(PFF) and it is a more direct approach.

The main problem is how to prevent thrashing. As thrashing has a high page fault rate and also
we want to control the page fault rate.

When the Page fault is too high, then we know that the process needs more frames. Conversely, if
the page fault-rate is too low then the process may have too many frames.

We can establish upper and lower bounds on the desired page faults. If the actual page-fault rate
exceeds the upper limit then we will allocate the process to another frame. And if the page fault
rate falls below the lower limit then we can remove the frame from the process.

Thus with this, we can directly measure and control the page fault rate in order to prevent
thrashing.

=================================**********===============================

P a r a m a n a n d a S a h u , D E P T . o f C S E 98 Page 98
OPERATING SYSTEMLECTURE NOTE

MODULE- IV
File Systems in Operating System
A file system is a method an operating system uses to store, organize, and manage files and
directories on a storage device. Some common types of file systems include:
• FAT (File Allocation Table): An older file system used by older versions of Windows and other
operating systems.
• NTFS (New Technology File System): A modern file system used by Windows. It supports features
such as file and folder permissions, compression, and encryption.
• ext (Extended File System): A file system commonly used on Linux and Unix-based operating
systems.
• HFS (Hierarchical File System): A file system used by macOS.
• APFS (Apple File System): A new file system introduced by Apple for their Macs and iOS devices.
The advantages of using a file system include the:
• Organization: A file system allows files to be organized into directories and subdirectories, making
it easier to manage and locate files.
• Data protection: File systems often include features such as file and folder permissions, backup
and restore, and error detection and correction, to protect data from loss or corruption.
• Improved performance: A well-designed file system can improve the performance of reading and
writing data by organizing it efficiently on disk.
Disadvantages of using a file system include:
• Compatibility issues: Different file systems may not be compatible with each other, making it
difficult to transfer data between different operating systems.
• Disk space overhead: File systems may use some disk space to store metadata and other
overhead information, reducing the amount of space available for user data.
• Vulnerability: File systems can be vulnerable to data corruption, malware, and other security
threats, which can compromise the stability and security of the system.
A file is a collection of related information that is recorded on secondary storage. Or file is a
collection of logically related entities. From the user’s perspective, a file is the smallest allotment
of logical secondary storage.
The name of the file is divided into two parts as shown below:
• name
• extension, separated by a period.

Files attributes and its operations:

Attributes Types Operations

P a r a m a n a n d a S a h u , D E P T . o f C S E 99 Page 99
OPERATING SYSTEMLECTURE NOTE

Name Doc Create

Type Exe Open

Size Jpg Read

Creation
Xis Write
Data

Author C Append

Last
Java Truncate
Modified

protection class Delete

Close

File type Usual extension Function

Executable exe, com, bin Read to run machine language program

Object obj, o Compiled, machine language not linked

C, java, pas, asm,


Source Code Source code in various languages
a

Batch bat, sh Commands to the command interpreter

Text txt, doc Textual data, documents

Word
wp, tex, rrf, doc Various word processor formats
Processor

Archive arc, zip, tar Related files grouped into one compressed file

Multimedia mpeg, mov, rm For containing audio/video information

Markup xml, html, tex It is the textual data and documents

Library lib, a ,so, dll It contains libraries of routines for programmers

It is a format for printing or viewing a ASCII or binary


Print or View gif, pdf, jpg
file.

P a r a m a n a n d a S a h u , D E P T . o f C S E 100 Page 100


OPERATING SYSTEMLECTURE NOTE

FILE DIRECTORIES:
Collection of files is a file directory. The directory contains information about the files, including
attributes, location and ownership. Much of this information, especially that is concerned with
storage, is managed by the operating system. The directory is itself a file, accessible by various file
management routines.
Information contained in a device directory are:
• Name
• Type
• Address
• Current length
• Maximum length
• Date last accessed
• Date last updated
• Owner id
• Protection information
Operation performed on directory are:
• Search for a file
• Create a file
• Delete a file
• List a directory
• Rename a file
• Traverse the file system
Advantages of maintaining directories are:
• Efficiency: A file can be located more quickly.
• Naming: It becomes convenient for users as two users can have same name for different files or
may have different name for same file.
• Grouping: Logical grouping of files can be done by properties e.g. all java programs, all games etc.
SINGLE-LEVEL DIRECTORY
In this a single directory is maintained for all the users.
• Naming problem: Users cannot have same name for two files.
• Grouping problem: Users cannot group files according to their need.
FILE ALLOCATION METHODS :
1. Continuous Allocation –
A single continuous set of blocks is allocated to a file at the time of file creation. Thus, this is a
pre-allocation strategy, using variable size portions. The file allocation table needs just a single
entry for each file, showing the starting block and the length of the file. This method is best from

P a r a m a n a n d a S a h u , D E P T . o f C S E 101 Page 101


OPERATING SYSTEMLECTURE NOTE

the point of view of the individual sequential file. Multiple blocks can be read in at a time to
improve I/O performance for sequential processing. It is also easy to retrieve a single block. For
example, if a file starts at block b, and the ith block of the file is wanted, its location on secondary
storage is simply b+i-1.

Disadvantage –
• External fragmentation will occur, making it difficult to find contiguous blocks of space of
sufficient length. Compaction algorithm will be necessary to free up additional space on
disk.
• Also, with pre-allocation, it is necessary to declare the size of the file at the time of creation.
2. Linked Allocation(Non-contiguous allocation) –
Allocation is on an individual block basis. Each block contains a pointer to the next block in the
chain. Again the file table needs just a single entry for each file, showing the starting block and
the length of the file. Although pre-allocation is possible, it is more common simply to allocate
blocks as needed. Any free block can be added to the chain. The blocks need not be continuous.
Increase in file size is always possible if free disk block is available. There is no external
fragmentation because only one block at a time is needed but there can be internal
fragmentation but it exists only in the last disk block of file.
Disadvantage –
• Internal fragmentation exists in last disk block of file.
• There is an overhead of maintaining the pointer in every disk block.
• If the pointer of any disk block is lost, the file will be truncated.
• It supports only the sequential access of files.

P a r a m a n a n d a S a h u , D E P T . o f C S E 102 Page 102


OPERATING SYSTEMLECTURE NOTE

3. Indexed Allocation –
It addresses many of the problems of contiguous and chained allocation. In this case, the file
allocation table contains a separate one-level index for each file: The index has one entry for each
block allocated to the file. Allocation may be on the basis of fixed-size blocks or variable-sized
blocks. Allocation by blocks eliminates external fragmentation, whereas allocation by variable-size
blocks improves locality. This allocation technique supports both sequential and direct access to
the file and thus is the most popular form of file allocation.

Disk Free Space Management :


Just as the space that is allocated to files must be managed ,so the space that is not currently
allocated to any file must be managed. To perform any of the file allocation techniques,it is
necessary to know what blocks on the disk are available. Thus we need a disk allocation table in
addition to a file allocation table.The following are the approaches used for free space
management.

• Bit Tables : This method uses a vector containing one bit for each block on the disk. Each
entry for a 0 corresponds to a free block and each 1 corresponds to a block in use.
For example: 00011010111100110001

P a r a m a n a n d a S a h u , D E P T . o f C S E 103 Page 103


OPERATING SYSTEMLECTURE NOTE

In this vector every bit correspond to a particular block and 0 implies that, that particular
block is free and 1 implies that the block is already occupied. A bit table has the advantage
that it is relatively easy to find one or a contiguous group of free blocks. Thus, a bit table
works well with any of the file allocation methods. Another advantage is that it is as small
as possible.
• Free Block List : In this method, each block is assigned a number sequentially and the list
of the numbers of all free blocks is maintained in a reserved block of the disk.

Linux File System


A Linux file system is a structured collection of files on a disk drive or a partition. A partition is a
segment of memory and contains some specific data. In our machine, there can be various
partitions of the memory. Generally, every partition contains a file system.
The general-purpose computer system needs to store data systematically so that we can easily
access the files in less time. It stores the data on hard disks (HDD) or some equivalent storage
type. There may be below reasons for maintaining the file system:
• Primarily the computer saves data to the RAM storage; it may lose the data if it gets turned
off. However, there is non-volatile RAM (Flash RAM and SSD) that is available to maintain
the data after the power interruption.
• Data storage is preferred on hard drives as compared to standard RAM as RAM costs more
than disk space. The hard disks costs are dropping gradually comparatively the RAM.
The Linux file system contains the following sections:
• The root directory (/)
• A specific data storage format (EXT3, EXT4, BTRFS, XFS and so on)
• A partition or logical volume having a particular file system.

P a r a m a n a n d a S a h u , D E P T . o f C S E 104 Page 104


OPERATING SYSTEMLECTURE NOTE

the Linux File System


Linux file system is generally a built-in layer of a Linux operating system used to handle the data
management of the storage. It helps to arrange the file on the disk storage. It manages the file
name, file size, creation date, and much more information about a file.

Linux File System Structure


Linux file system has a hierarchal file structure as it contains a root directory and its subdirectories.
All other directories can be accessed from the root directory. A partition usually has only one file
system, but it may have more than one file system.
A file system is designed in a way so that it can manage and provide space for non-volatile
storage data. All file systems required a namespace that is a naming and organizational
methodology. The namespace defines the naming process, length of the file name, or a subset of
characters that can be used for the file name. It also defines the logical structure of files on a
memory segment, such as the use of directories for organizing the specific files. Once a
namespace is described, a Metadata description must be defined for that particular file.
The data structure needs to support a hierarchical directory structure; this structure is used to
describe the available and used disk space for a particular block. It also has the other details
about the files such as file size, date & time of creation, update, and last modified.
Also, it stores advanced information about the section of the disk, such as partitions and volumes.
The advanced data and the structures that it represents contain the information about the file
system stored on the drive; it is distinct and independent of the file system metadata.
Linux file system contains two-part file system software implementation architecture. Consider the
below image:

The file system requires an API (Application programming interface) to access the function calls to interact
with file system components like files and directories. API facilitates tasks such as creating, deleting, and
copying the files. It facilitates an algorithm that defines the arrangement of files on a file system.
The first two parts of the given file system together called a Linux virtual file system. It provides
a single set of commands for the kernel and developers to access the file system. This virtual file
system requires the specific system driver to give an interface to the file system.

Linux File System Features


P a r a m a n a n d a S a h u , D E P T . o f C S E 105 Page 105
OPERATING SYSTEMLECTURE NOTE

In Linux, the file system creates a tree structure. All the files are arranged as a tree and its
branches. The topmost directory called the root (/) directory. All other directories in Linux can be
accessed from the root directory.
Some key features of Linux file system are as following:
• Specifying paths: Linux does not use the backslash (\) to separate the components; it uses
forward slash (/) as an alternative. For example, as in Windows, the data may be stored in
C:\ My Documents\ Work, whereas, in Linux, it would be stored in /home/ My Document/
Work.
• Partition, Directories, and Drives: Linux does not use drive letters to organize the drive
as Windows does. In Linux, we cannot tell whether we are addressing a partition, a network
device, or an "ordinary" directory and a Drive.
• Case Sensitivity: Linux file system is case sensitive. It distinguishes between lowercase and
uppercase file names. Such as, there is a difference between test.txt and Test.txt in Linux.
This rule is also applied for directories and Linux commands.
• File Extensions: In Linux, a file may have the extension '.txt,' but it is not necessary that a
file should have a file extension. While working with Shell, it creates some problems for the
beginners to differentiate between files and directories. If we use the graphical file
manager, it symbolizes the files and folders.
• Hidden files: Linux distinguishes between standard files and hidden files, mostly the
configuration files are hidden in Linux OS. Usually, we don't need to access or read the
hidden files. The hidden files in Linux are represented by a dot (.) before the file name
(e.g., .ignore). To access the files, we need to change the view in the file manager or need
to use a specific command in the shell.

Types of Linux File System


When we install the Linux operating system, Linux offers many file systems such as Ext, Ext2,
Ext3, Ext4, JFS, ReiserFS, XFS, btrfs, and swap.

Let's understand each of these file systems in detail:

P a r a m a n a n d a S a h u , D E P T . o f C S E 106 Page 106


OPERATING SYSTEMLECTURE NOTE

1. Ext, Ext2, Ext3 and Ext4 file system

The file system Ext stands for Extended File System. It was primarily developed for MINIX OS.
The Ext file system is an older version, and is no longer used due to some limitations.
Ext2 is the first Linux file system that allows managing two terabytes of data. Ext3 is developed
through Ext2; it is an upgraded version of Ext2 and contains backward compatibility. The major
drawback of Ext3 is that it does not support servers because this file system does not support file
recovery and disk snapshot.
Ext4 file system is the faster file system among all the Ext file systems. It is a very compatible
option for the SSD (solid-state drive) disks, and it is the default file system in Linux distribution.

2. JFS File System

JFS stands for Journaled File System, and it is developed by IBM for AIX Unix. It is an
alternative to the Ext file system. It can also be used in place of Ext4, where stability is needed
with few resources. It is a handy file system when CPU power is limited.

3. ReiserFS File System

ReiserFS is an alternative to the Ext3 file system. It has improved performance and advanced
features. In the earlier time, the ReiserFS was used as the default file system in SUSE Linux, but
later it has changed some policies, so SUSE returned to Ext3. This file system dynamically supports
the file extension, but it has some drawbacks in performance.

4. XFS File System

XFS file system was considered as high-speed JFS, which is developed for parallel I/O processing.
NASA still using this file system with its high storage server (300+ Terabyte server).

5. Btrfs File System

Btrfs stands for the B tree file system. It is used for fault tolerance, repair system, fun
administration, extensive storage configuration, and more. It is not a good suit for the production
system.

6. Swap File System

The swap file system is used for memory paging in Linux operating system during the system
hibernation. A system that never goes in hibernate state is required to have swap space equal to
its RAM size.

P a r a m a n a n d a S a h u , D E P T . o f C S E 107 Page 107


OPERATING SYSTEMLECTURE NOTE

Mass Storage Structure


Primary Memory

A processor or computer initially or directly accesses primary memory while using a computer. It
enables a processor to access programs and services that are now in use and temporarily stored
in a particular area of memory.

Primary storage and main memory are the other terms which can be used as a substitute to the
term primary memory.

The volatile storage component of a computer system is primary memory. Although there may be
data buses, cache memory, or Random Access Memory (RAM), RAM is the most common
example for primary memory.

An operating system (OS), user interface, and any user-installed and running software utilities are
all loaded into main memory as soon as a computer turns on. When a program or application is
launched from main memory, it communicates with the system processor to carry out all of its
unique functions.

Secondary memory is said to be slower than primary memory.

Secondary Memory

Secondary memory is non-volatile, permanent computer memory that is not directly accessible by
a computer or processor. Data that can be quickly and easily retrieved, transmitted, and utilized
by apps and services can be stored by the user and then used in this manner.

Secondary storage is another name which can be used as a substitute to the word secondary
memory.

Read-only memory (ROM), flash drives, hard disk drives (HDD), magnetic tapes, and other forms
of internal and external storage media are all considered secondary memory. Only the major or
main memory may access secondary memory during computations, and only then is it sent to the
processor.

Even if the computer is not powered on, data can be stored and retained in secondary memory,
which is slower than primary memory. Additionally, it has large storage capacity, with each
memory being capable of holding anywhere from a few megabytes (MB) to many terabytes (TB).

Secondary memory is said to be slower than primary memory.

All the mass storage media belongs to the secondary memory.

P a r a m a n a n d a S a h u , D E P T . o f C S E 108 Page 108


OPERATING SYSTEMLECTURE NOTE

Difference between Primary Memory and Secondary Memory


S.
Primary Memory Secondary Memory
No

Primary Memory is also primarily known as main Secondary Memory is also primarily
1.)
memory or primary storage memory known as secondary memory storage

They are also known as Auxiliary Memory


2.) They are also known as Internal Memory or Backup Memory or Additional
Memory.

Primary Memory is more costlier than Secondary Secondary Memory is less costlier than
3.)
Memory Secondary Memory

Primary memory is said to be faster than Secondary Secondary memory is said to be slower
4.)
memory. than primary memory.

It has a substantial amount of


It stores information or data that the processing unit is
information and data storage. Typically,
5.) currently using. Usually, capacity ranges from sixteen
capacity ranges from 200 GB to
Giga Bytes (16 GB) to Thirty Two Giga Bytes (32 GB).
terabytes.

The Primary Memory can be divided as Volatile and The Secondary Memory can only be
6.)
Non Volatile Memories classified as Non Volatile Memory only.

Because it has a non-volatile memory,


Data cannot be preserved in the event of a power
7.) the information may be kept even in the
outage since it is a volatile memory.
event of a power outage.

Now, let us learn about the types of the Mass Storage Media which are present in the market.

Mass Storage Structure

Systems designed to store enormous volumes of data are referred to as mass storage devices.
Massive storage devices are sometimes used interchangeably with peripheral storage, which is
the management of bigger volumes of data that are larger than the native storage capability of a
computer or device.

The basic idea of Mass Storage is to create a Data Backup or Data Recovery System.

P a r a m a n a n d a S a h u , D E P T . o f C S E 109 Page 109


OPERATING SYSTEMLECTURE NOTE

Along with computer systems, definitions of mass storage technologies and tactics have changed.
The earliest and most basic mass storage techniques date back to the era of main frame
supercomputers, according to experts. Punch cards, Hollerith cards, and other relatively similar
manual storage medium are examples of this Mass Storage Media these days. Today, mass
storage may include several kinds of hard disks or solid-state storage devices, as well as tape
drives and other physical data storage devices.

The concepts of data backup and data recovery are frequently linked to mass storage media. The
biggest Business Companies will make plans for recording, storing, and backing up all accessible
data, which calls for a lot more mass storage media than what factory-direct gear can provide.
This suggests a method for handling continuous mass storage that uses tape or other media.
Other kinds of mass storage could function well as a data storage plan for a big network or a
bunch of mobile distant devices. To backup data on a portable tablet that doesn't have a lot of
internal capacity, for instance, mass storage for a tablet might require the usage of flash or
Universal Serial Bus media (USB Media).

The Mass Storage Structure Devices are:

1. Magnetic Disks
2. Solid State Disks
3. Magnetic Tapes

Magnetic Disks

Now, we are going to know about all whereabouts of the Magnetic Disk Mass Storage Structure
Devices.

The process of magnetization is used to write, rewrite, and access data on a magnetic disk, a
storage device. This process is known as Magnetic Disk. It is coated magnetically and has tracks,
spots, and sectors for storing data.

In 1956, IBM created the first magnetic hard drive, a substantial device with 50 21-inch (53-cm)
platters. Despite being large, it could only hold 5 megabytes of information. Since then, magnetic
disks' storage capabilities have multiplied dramatically while simultaneously shrinking in size.

Basic Common Examples of Magnetic Disks are:

1. Floppy Disks
2. Hard Disks
3. Zip Disks

The Magnetic Disk basically looks like:

P a r a m a n a n d a S a h u , D E P T . o f C S E 110 Page 110


OPERATING SYSTEMLECTURE NOTE

Structure and Working of Magnetic Disks:

The basic structure of Magnetic Disks is:

A mechanical arm that travels across a revolving magnetic surface, known as the platter, makes
up the majority of a magnetic disk. They come together to make a "comb." Both reading from
and writing to the disk are done using the mechanical arm. A magnetization process is used to
read and write data on magnetic disks.

One or more disk-shaped platters with magnetic material covering them. Unlike "floppy" disks,
which are composed of more flexible plastic, hard disk platters are built of stiff metal.

There are two work areas on each plate. The very top and bottom surfaces of a stack of platters
were occasionally avoided by older hard disk drives because they are more prone to damage or
even breaking in some cases.

Disk scheduling :
Disk scheduling is done by operating systems to schedule I/O requests arriving for the disk. Disk
scheduling is also known as I/O scheduling.
Disk scheduling is important because:

• Multiple I/O requests may arrive by different processes and only one I/O request can be
served at a time by the disk controller. Thus other I/O requests need to wait in the waiting
queue and need to be scheduled.
• Two or more request may be far from each other so can result in greater disk arm
movement.
• Hard drives are one of the slowest parts of the computer system and thus need to be
accessed in an efficient manner.
Goals of disk scheduling algorithm:
• high throughput.
• minimal travelling head time.
• fairness.

P a r a m a n a n d a S a h u , D E P T . o f C S E 111 Page 111


OPERATING SYSTEMLECTURE NOTE

Seek Time:Seek time is the time taken to locate the disk arm to a specified track where the data
is to be read or write. So the disk scheduling algorithm that gives minimum average seek time is
better.
Rotational Latency: Rotational Latency is the time taken by the desired sector of disk to rotate
into a position so that it can access the read/write heads. So the disk scheduling algorithm that
gives minimum rotational latency is better.
Transfer Time: Transfer time is the time to transfer the data. It depends on the rotating speed of
the disk and number of bytes to be transferred.
Disk Access Time: Disk Access Time is:
Disk Access Time = Seek Time +
Rotational Latency +
Transfer Time
Disk Response Time: Response Time is the average of time spent by a request waiting to perform its I/O
operation. Average Response time is the response time of the all requests. Variance Response Time is
measure of how individual request are serviced with respect to average response time. So the disk
scheduling algorithm that gives minimum variance response time is better.
Disk Scheduling Algorithms

FCFS: FCFS is the simplest of all the Disk Scheduling Algorithms. In FCFS, the requests are addressed
in the order they arrive in the disk queue.Let us understand this with the help of an example.

Example:
• Suppose the order of request is- (82,170,43,140,24,16,190)
And current position of Read/Write head is : 50

P a r a m a n a n d a S a h u , D E P T . o f C S E 112 Page 112


OPERATING SYSTEMLECTURE NOTE

• So, total seek time:


=(82-50)+(170-82)+(170-43)+(140-43)+(140-24)+(24-16)+(190-16)
=642

Advantages:

• Every request gets a fair chance


• No indefinite postponement
Disadvantages:

• Does not try to optimize seek time


• May not provide the best possible service

SSTF: In SSTF (Shortest Seek Time First), requests having shortest seek time are executed first. So, the
seek time of every request is calculated in advance in the queue and then they are scheduled according to
their calculated seek time. As a result, the request near the disk arm will get executed first. SSTF is certainly
an improvement over FCFS as it decreases the average response time and increases the throughput of
system.Let us understand this with the help of an example.

Example:
• Suppose the order of request is- (82,170,43,140,24,16,190)
And current position of Read/Write head is : 50

So, total seek time:


P a r a m a n a n d a S a h u , D E P T . o f C S E 113 Page 113
OPERATING SYSTEMLECTURE NOTE

=(50-43)+(43-24)+(24-16)+(82-16)+(140-82)+(170-140)+(190-170)
=208

Advantages:
• The average Response Time decreases
• Throughput increases
Disadvantages:
• Overhead to calculate seek time in advance
• Can cause Starvation for a request if it has a higher seek time as compared to incoming requests
• The high variance of response time as SSTF favors only some requests

SCAN:In the SCAN algorithm the disk arm moves in a particular direction and services the
requests coming in its path and after reaching the end of the disk, it reverses its direction and again
services the request arriving in its path. So, this algorithm works as an elevator and is hence also
known as an elevator algorithm. As a result, the requests at the midrange are serviced more and
those arriving behind the disk arm will have to wait.

Example:
• Suppose the requests to be addressed are-82,170,43,140,24,16,190. And the Read/Write arm is at
50, and it is also given that the disk arm should move “towards the larger value”.

Therefore, the seek time is calculated as:


=(199-50)+(199-16)
=332
Advantages:
• High throughput
P a r a m a n a n d a S a h u , D E P T . o f C S E 114 Page 114
OPERATING SYSTEMLECTURE NOTE

• Low variance of response time


• Average response time
Disadvantages:
• Long waiting time for requests for locations just visited by disk arm

CSCAN: In the SCAN algorithm, the disk arm again scans the path that has been scanned, after
reversing its direction. So, it may be possible that too many requests are waiting at the other end or there
may be zero or few requests pending at the scanned area.
These situations are avoided in the CSCAN algorithm in which the disk arm instead of reversing
its direction goes to the other end of the disk and starts servicing the requests from there. So, the
disk arm moves in a circular fashion and this algorithm is also similar to SCAN algorithm and
hence it is known as C-SCAN (Circular SCAN).

Example:

Suppose the requests to be addressed are-82,170,43,140,24,16,190. And the Read/Write arm is at


50, and it is also given that the disk arm should move “towards the larger value”.

Seek time is calculated as:


=(199-50)+(199-0)+(43-0)
=391
Advantages:

P a r a m a n a n d a S a h u , D E P T . o f C S E 115 Page 115


OPERATING SYSTEMLECTURE NOTE

• Provides more uniform wait time compared to SCAN

LOOK: It is similar to the SCAN disk scheduling algorithm except for the difference that the disk arm
in spite of going to the end of the disk goes only to the last request to be serviced in front of the head and
then reverses its direction from there only. Thus it prevents the extra delay which occurred due to
unnecessary traversal to the end of the disk.

Example:
• Suppose the requests to be addressed are-82,170,43,140,24,16,190. And the Read/Write arm is at
50, and it is also given that the disk arm should move “towards the larger value”.

So, the seek time is calculated as:


=(190-50)+(190-16)
=314

CLOOK: As LOOK is similar to the SCAN algorithm, in a similar way, CLOOK is similar to the CSCAN
disk scheduling algorithm. In CLOOK, the disk arm in spite of going to the end goes only to the last
request to be serviced in front of the head and then from there goes to the other end’s last request. Thus,
it also prevents the extra delay which occurred due to unnecessary traversal to the end of the disk.

Example:
• Suppose the requests to be addressed are-82,170,43,140,24,16,190. And the Read/Write arm is at
50, and it is also given that the disk arm should move “towards the larger value”.

P a r a m a n a n d a S a h u , D E P T . o f C S E 116 Page 116


OPERATING SYSTEMLECTURE NOTE

So, the seek time is calculated as:


=(190-50)+(190-16)+(43-16)
=341

Disk Management
The range of services and add-ons provided by modern operating systems is constantly
expanding, and four basic operating system management functions are implemented by all
operating systems. These management functions are briefly described below and given the
following overall context. The four main operating system management functions (each of which
are dealt with in more detail in different places) are:

 Process Management
 Memory Management
 File and Disk Management
 I/O System Management

Most computer systems employ secondary storage devices (magnetic disks). It provides low-cost,
non-volatile storage for programs and data (tape, optical media, flash drives, etc.). Programs and
the user data they use are kept on separate storage devices called files. The operating system is
responsible for allocating space for files on secondary storage media as needed.

P a r a m a n a n d a S a h u , D E P T . o f C S E 117 Page 117


OPERATING SYSTEMLECTURE NOTE

There is no guarantee that files will be stored in contiguous locations on physical disk drives,
especially large files. It depends greatly on the amount of space available. When the disc is full,
new files are more likely to be recorded in multiple locations. However, as far as the user is
concerned, the example file provided by the operating system hides the fact that the file is
fragmented into multiple parts.

The operating system needs to track the location of the disk for every part of every file on the
disk. In some cases, this means tracking hundreds of thousands of files and file fragments on a
single physical disk. Additionally, the operating system must be able to locate each file and
perform read and write operations on it whenever it needs to. Therefore, the operating system is
responsible for configuring the file system, ensuring the safety and reliability of reading and write
operations to secondary storage, and maintains access times (the time required to write data to
or read data from secondary storage).

Disk management of the operating system includes:

 Disk Format
 Booting from disk
 Bad block recovery

The low-level format or physical format:

Divides the disk into sectors before storing data so that the disk controller can read and write
Each sector can be:

The header retains information, data, and error correction code (ECC) sectors of data, typically
512 bytes of data, but optional disks use the operating system’s own data structures to preserve
files using disks.

It is conducted in two stages:

1. Divide the disc into multiple cylinder groups. Each is treated as a logical disk.

2. Logical format or “Create File System”. The OS stores the data structure of the first file system
on the disk. Contains free space and allocated space.

For efficiency, most file systems group blocks into clusters. Disk I / O runs in blocks. File I / O runs
in a cluster.

Boot block:

 When the computer is turned on or restarted, the program stored in the initial bootstrap ROM
finds the location of the OS kernel from the disk, loads the kernel into memory, and runs the OS.
start.

P a r a m a n a n d a S a h u , D E P T . o f C S E 118 Page 118


OPERATING SYSTEMLECTURE NOTE

 To change the bootstrap code, you need to change the ROM and hardware chip. Only a small
bootstrap loader program is stored in ROM instead.
 The full bootstrap code is stored in the “boot block” of the disk.
 A disk with a boot partition is called a boot disk or system disk.

Bad Blocks:

 Disks are error-prone because moving parts have small tolerances.


 Most disks are even stuffed from the factory with bad blocks and are handled in a variety of ways.
 The controller maintains a list of bad blocks.
 The controller can instruct each bad sector to be logically replaced with one of the spare sectors.
This scheme is known as sector sparing or transfer.
 A soft error triggers the data recovery process.
 However, unrecoverable hard errors may result in data loss and require manual intervention.

I/O Systems:-
when you browse a web page or edit any file, our immediate attention is to read or enter some
information, not for computing an answer. The primary role of the operating system in computer Input /
Output is to manage and organize I/O operations and all I/O devices

Overview ofI/O Systems


The controlling of various devices that are connected to the computer is a key concern of
operating-system designers. This is because I/O devices vary so widely in their functionality and
speed (for example a mouse, a hard disk and a CD-ROM), varied methods are required for
controlling them. These methods form the I/O sub-system of the kernel of OS that separates the
rest of the kernel from the complications of managing I/O devices.

Computers operate many huge kinds of devices. The general categories of storage devices are
like disks, tapes, transmission devices (like network interface cards, modems) and human interface
devices (like screen, keyboard, etc.).

A device communicates with the operating system of a computer by transferring signals over
cable or even through the air. The peripheral devices communicate with the machine through a
connection point also called ports— (one example is a serial port). When devices use a set of
wires or cables, that connecting cables are called a "bus." A bus is a collection of wires and a
firmly defined protocol which specifies a set of messages that can be sent on the wires.

Operating System using I/O PORT


An I/O port usually consists of four different registers. These are (1) status, (2) control, (3) data-in,
and (4) data-out registers.

 The data-in register is read by the host for getting input.

P a r a m a n a n d a S a h u , D E P T . o f C S E 119 Page 119


OPERATING SYSTEMLECTURE NOTE

 The data-out register is written by the host for sending output.


 The status register holds bits which can be read by the host.
 The control register is written by the host for starting a command or for changing the
mode of any device.
 The data registers are usually 1 to 4 bytes in size. Some of the controllers have FIFO chips
which hold several bytes of input or output data for expanding the capacity of the
controller beyond the size of the data register.

POLLING:-
The complete protocol used to interact with the host and a controller can be difficult for the OS,
but the necessary handshaking notion is simple. You can express handshaking with an example.
You assume that 2 bits have been used for coordinating the producer - consumer relationship
among the controller and the host. The controller points to its state using the busy bit in the
status register.

The host marks output through a port, with the controller using handshaking like:

1. The host frequently reads the busy bit till that bit gets clear.
2. The host then sets the write, bit in the command register and then writes a byte to the
data-out register.
3. The host then puts the command-ready bit.
4. As the controller notices that the command - ready bit has been set, it sets the busy bit.
5. The controller then reads the command register. It reads the data-out register for getting
the byte and performs the I/O to the device.
6. The controller clears the command - ready bit then clears the error bit within the status
register for indicating that the device I/O succeeded.

System Protection
Protection refers to a mechanism which controls the access of programs, processes, or users to
the resources defined by a computer system. We can take protection as a helper to multi
programming operating system, so that many users might safely share a common logical name
space such as directory or files.

Need of Protection:

 To prevent the access of unauthorized users and


 To ensure that each active programs or processes in the system uses resources only as the
stated policy,
 To improve reliability by detecting latent errors.

P a r a m a n a n d a S a h u , D E P T . o f C S E 120 Page 120


OPERATING SYSTEMLECTURE NOTE

Role of Protection:
The role of protection is to provide a mechanism that implement policies which defines the uses
of resources in the computer system.Some policies are defined at the time of design of the
system, some are designed by management of the system and some are defined by the users of
the system to protect their own files and programs.

Every application has different policies for use of the resources and they may change over time so
protection of the system is not only concern of the designer of the operating system. Application
programmer should also design the protection mechanism to protect their system against misuse.

Policy is different from mechanism. Mechanisms determine how something will be done and
policies determine what will be done.Policies are changed over time and place to place.
Separation of mechanism and policy is important for the flexibility of the system.

System Security
Prerequisite – Basic Network Attacks, Types of Viruses
The security of a computer system is a crucial task. It is a process of ensuring the confidentiality
and integrity of the OS. Security is one of most important as well as the major task in order to
keep all the threats or other malicious tasks or attacks or program away from the computer’s
software system.
A system is said to be secure if its resources are used and accessed as intended under all the
circumstances, but no system can guarantee absolute security from several of various malicious
threats and unauthorized access.

The security of a system can be threatened via two violations:

 Threat: A program that has the potential to cause serious damage to the system.
 Attack: An attempt to break security and make unauthorized use of an asset.

Security violations affecting the system can be categorized as malicious and accidental threats.
Malicious threats, as the name suggests are a kind of harmful computer code or web script
designed to create system vulnerabilities leading to back doors and security breaches. Accidental
Threats, on the other hand, are comparatively easier to be protected against. Example: Denial of
Service DDoS attack.

Security can be compromised via any of the breaches mentioned:

 Breach of confidentiality: This type of violation involves the unauthorized reading of data.
 Breach of integrity: This violation involves unauthorized modification of data.
 Breach of availability: It involves unauthorized destruction of data.
 Theft of service: It involves the unauthorized use of resources.

P a r a m a n a n d a S a h u , D E P T . o f C S E 121 Page 121


OPERATING SYSTEMLECTURE NOTE

 Denial of service: It involves preventing legitimate use of the system. As mentioned


before, such attacks can be accidental in nature.

Security System Goal:


Henceforth, based on the above breaches, the following security goals are aimed:

1. Integrity:
The objects in the system mustn’t be accessed by any unauthorized user & any user not
having sufficient rights should not be allowed to modify the important system files and
resources.
2. Secrecy:
The objects of the system must be accessible only to a limited number of authorized users.
Not everyone should be able to view the system files.
3. Availability:
All the resources of the system must be accessible to all the authorized users i.e. only one
user/process should not have the right to hog all the system resources. If such kind of
situation occurs, denial of service could happen. In this kind of situation, malware might
hog the resources for itself & thus preventing the legitimate processes from accessing the
system resources.

Threats can be classified into the following two categories:

1. Program Threats:
A program was written by a cracker to hijack the security or to change the behavior of a
normal process. In other words, if a user program is altered and further made to perform
some malicious unwanted tasks, then it is known as Program Threats.
2. System Threats:
These threats involve the abuse of system services. They strive to create a situation in
which operating-system resources and user files are misused. They are also used as a
medium to launch program threats.

Types of Program Threats:

1. Virus:
An infamous threat, known most widely. It is a self-replicating and malicious thread that
attaches itself to a system file and then rapidly replicates itself, modifying and destroying
essential files leading to a system breakdown.

Further, Types of computer viruses can be described briefly as follows:


– file/parasitic – appends itself to a file
– boot/memory – infects the boot sector
– macro – written in a high-level language like VB and affects MS Office files

P a r a m a n a n d a S a h u , D E P T . o f C S E 122 Page 122


OPERATING SYSTEMLECTURE NOTE

– source code – searches and modifies source codes


– polymorphic – changes in copying each time
– encrypted – encrypted virus + decrypting code
– stealth – avoids detection by modifying parts of the system that can be used to detect it,
like the read system
call
– tunneling – installs itself in the interrupt service routines and device drivers
– multipartite – infects multiple parts of the system

2. Trojan Horse:
A code segment that misuses its environment is called a Trojan Horse. They seem to be
attractive and harmless cover programs but are really harmful hidden programs that can
be used as the virus carrier. In one of the versions of Trojan, the User is fooled to enter
confidential login details on an application. Those details are stolen by a login emulator
and can be further used as a way of information breaches. One of the major as well as a
serious threat or consequences of the Trojan horse is that it will actually perform proper
damage once installed or run on the computer’s system but at first, a glance will appear to
be useful software and later turns out to be maliciously unwanted one.

Another variance is Spyware, Spyware accompanies a program that the user has chosen to
install and download ads to display on the user’s system, thereby creating pop-up browser
windows and when certain sites are visited by the user, it captures essential information
and sends it over to the remote server. Such attacks are also known as Convert Channels.

3. Trap Door:
The designer of a program or system might leave a hole in the software that only he is
capable of using, the Trap Door works on similar principles. Trap Doors are quite difficult
to detect as to analyze them, one needs to go through the source code of all the
components of the system. In other words, if we may have to define a trap door then it
would be like, a trap door is actually a kind of a secret entry point into a running or static
program that actually allows anyone to gain access to any system without going through
the usual security access procedures.

4. Logic Bomb:
A program that initiates a security attack only under a specific situation. To be very precise,
a logic bomb is actually the most malicious program which is inserted intentionally into the
computer system and that is triggered or functions when specific conditions have been
met for it to work.
5. Worm:
A computer worm is a type of malware that replicates itself
and infects other computers while remaining active on affected systems. A computer worm

P a r a m a n a n d a S a h u , D E P T . o f C S E 123 Page 123


OPERATING SYSTEMLECTURE NOTE

replicates itself in order to infect machines that aren’t already infested. It frequently
accomplishes this by taking advantage of components of an operating system that are
automatic and unnoticed by the user. Worms are frequently overlooked until their
uncontrolled replication depletes system resources, slowing or stopping other activities.

Types of System Threats –


Aside from the program threats, various system threats are also endangering the security of our
system:

1. Worm:
An infection program that spreads through networks. Unlike a virus, they target mainly LANs. A
computer affected by a worm attacks the target system and writes a small program “hook” on it.
This hook is further used to copy the worm to the target computer. This process repeats
recursively, and soon enough all the systems of the LAN are affected. It uses the spawn
mechanism to duplicate itself. The worm spawns copies of itself, using up a majority of system
resources and also locking out all other processes.

The basic functionality of the worm can be represented as:

2. Port Scanning:
It is a means by which the cracker identifies the vulnerabilities of the system to attack. It is an
automated process that involves creating a TCP/IP connection to a specific port. To protect the
identity of the attacker, port scanning attacks are launched from Zombie Systems, that is systems
that were previously independent systems that are also serving their owners while being used for
such notorious purposes.

3. Denial of Service:
Such attacks aren’t aimed for the purpose of collecting information or destroying system files.
Rather, they are used for disrupting the legitimate use of a system or facility.
These attacks are generally network-based. They fall into two categories:

P a r a m a n a n d a S a h u , D E P T . o f C S E 124 Page 124


OPERATING SYSTEMLECTURE NOTE

– Attacks in this first category use so many system resources that no useful work can be
performed.

For example, downloading a file from a website that proceeds to use all available CPU time.
– Attacks in the second category involve disrupting the network of the facility. These attacks are a
result of the abuse of some fundamental TCP/IP principles.
the fundamental functionality of TCP/IP.

Security Measures Taken –


To protect the system, Security measures can be taken at the following levels:

 Physical:
The sites containing computer systems must be physically secured against armed and
malicious intruders. The workstations must be carefully protected.
 Human:
Only appropriate users must have the authorization to access the system.
Phishing(collecting confidential information) and Dumpster Diving(collecting basic
information so as to gain unauthorized access) must be avoided.
 Operating system:
The system must protect itself from accidental or purposeful security breaches.
 Networking System:
Almost all of the information is shared between different systems via a network.
Intercepting these data could be just as harmful as breaking into a computer. Henceforth,
Network should be properly secured against such attacks.

Usually, Anti Malware programs are used to periodically detect and remove such viruses and
threats. Additionally, to protect the system from Network Threats, a Firewall is also be used.

=================================************======================
===

P a r a m a n a n d a S a h u , D E P T . o f C S E 125 Page 125


OPERATING SYSTEMLECTURE NOTE

MODULE – V

Distributed System
Distributed System is a collection of autonomous computer systems that are physically separated
but are connected by a centralized computer network that is equipped with distributed system
software. The autonomous computers will communicate among each system by sharing resources
and files and performing the tasks assigned to them.

Example of Distributed System:

Any Social Media can have its Centralized Computer Network as its Headquarters and computer
systems that can be accessed by any user and using their services will be the Autonomous
Systems in the Distributed System Architecture.

 Distributed System Software: This Software enables computers to coordinate their activities and
to share the resources such as Hardware, Software, Data, etc.
 Database: It is used to store the processed data that are processed by each Node/System of the
Distributed systems that are connected to the Centralized network.

P a r a m a n a n d a S a h u , D E P T . o f C S E 126 Page 126


OPERATING SYSTEMLECTURE NOTE

 As we can see that each Autonomous System has a common Application that can have its own
data that is shared by the Centralized Database System.
 To Transfer the Data to Autonomous Systems, Centralized System should be having a Middleware
Service and should be connected to a Network.
 Middleware Services enables some services which are not present in the local systems or
centralized system default by acting as an interface between the Centralized System and the local
systems. By using components of Middleware Services systems communicate and manage data.
 The Data which is been transferred through the database will be divided into segments or modules
and shared with Autonomous systems for processing.
 The Data will be processed and then will be transferred to the Centralized system through the
network and will be stored in the database.

Characteristics of Distributed System:

 Resource Sharing: It is the ability to use any Hardware, Software, or Data anywhere in the System.
 Openness: It is concerned with Extensions and improvements in the system (i.e., How openly the
software is developed and shared with others)
 Concurrency: It is naturally present in the Distributed Systems, that deal with the same activity or
functionality that can be performed by separate users who are in remote locations. Every local
system has its independent Operating Systems and Resources.
 Scalability: It increases the scale of the system as a number of processors communicate with more
users by accommodating to improve the responsiveness of the system.
 Fault tolerance: It cares about the reliability of the system if there is a failure in Hardware or
Software, the system continues to operate properly without degrading the performance the system.
 Transparency: It hides the complexity of the Distributed Systems to the Users and Application
programs as there should be privacy in every system.
 Heterogeneity: Networks, computer hardware, operating systems, programming languages, and
developer implementations can all vary and differ among dispersed system components.

Advantages of Distributed System:

P a r a m a n a n d a S a h u , D E P T . o f C S E 127 Page 127


OPERATING SYSTEMLECTURE NOTE

 Applications in Distributed Systems are Inherently Distributed Applications.


 Information in Distributed Systems is shared among geographically distributed users.
 Resource Sharing (Autonomous systems can share resources from remote locations).
 It has a better price performance ratio and flexibility.
 It has shorter response time and higher throughput.
 It has higher reliability and availability against component failure.
 It has extensibility so that systems can be extended in more remote locations and also incremental
growth.

Disadvantages of Distributed System:

 Relevant Software for Distributed systems does not exist currently.


 Security possess a problem due to easy access to data as the resources are shared to multiple
systems.
 Networking Saturation may cause a hurdle in data transfer i.e., if there is a lag in the network then
the user will face a problem accessing data.
 In comparison to a single user system, the database associated with distributed systems is much
more complex and challenging to manage.
 If every node in a distributed system tries to send data at once, the network may become
overloaded.

Applications Area of Distributed System:

 Finance and Commerce: Amazon, eBay, Online Banking, E-Commerce websites.


 Information Society: Search Engines, Wikipedia, Social Networking, Cloud Computing.
 Cloud Technologies: AWS, Salesforce, Microsoft Azure, SAP.
 Entertainment: Online Gaming, Music, youtube.
 Healthcare: Online patient records, Health Informatics.
 Education: E-learning.
 Transport and logistics: GPS, Google Maps.
 Environment Management: Sensor technologies.

Distributed File System( DFS)


A Distributed File System (DFS) as the name suggests, is a file system that is distributed on
multiple file servers or multiple locations. It allows programs to access or store isolated files as
they do with the local ones, allowing programmers to access files from any network or computer.

The main purpose of the Distributed File System (DFS) is to allows users of physically distributed
systems to share their data and resources by using a Common File System. A collection of
workstations and mainframes connected by a Local Area Network (LAN) is a configuration on
Distributed File System. A DFS is executed as a part of the operating system. In DFS, a namespace
is created and this process is transparent for the clients.

P a r a m a n a n d a S a h u , D E P T . o f C S E 128 Page 128


OPERATING SYSTEMLECTURE NOTE

DFS has two components:

 Location Transparency –
Location Transparency achieves through the namespace component.
 Redundancy –
Redundancy is done through a file replication component.

In the case of failure and heavy load, these components together improve data availability by
allowing the sharing of data in different locations to be logically grouped under one folder, which
is known as the “DFS root”.

It is not necessary to use both the two components of DFS together, it is possible to use the
namespace component without using the file replication component and it is perfectly possible to
use the file replication component without using the namespace component between servers.

File system replication:

Early iterations of DFS made use of Microsoft’s File Replication Service (FRS), which allowed for
straightforward file replication between servers. The most recent iterations of the whole file are
distributed to all servers by FRS, which recognises new or updated files.

“DFS Replication” was developed by Windows Server 2003 R2 (DFSR). By only copying the
portions of files that have changed and minimising network traffic with data compression, it helps
to improve FRS. Additionally, it provides users with flexible configuration options to manage
network traffic on a configurable schedule.

Features of DFS :

 Transparency :
o Structure transparency –
There is no need for the client to know about the number or locations of file servers and
the storage devices. Multiple file servers should be provided for performance, adaptability,
and dependability.
o Access transparency –
Both local and remote files should be accessible in the same manner. The file system should
be automatically located on the accessed file and send it to the client’s side.
o Naming transparency –
There should not be any hint in the name of the file to the location of the file. Once a name
is given to the file, it should not be changed during transferring from one node to another.
o Replication transparency –
If a file is copied on multiple nodes, both the copies of the file and their locations should be
hidden from one node to another.
 User mobility :
It will automatically bring the user’s home directory to the node where the user logs in.

P a r a m a n a n d a S a h u , D E P T . o f C S E 129 Page 129


OPERATING SYSTEMLECTURE NOTE

 Performance :
Performance is based on the average amount of time needed to convince the client requests. This
time covers the CPU time + time taken to access secondary storage + network access time. It is
advisable that the performance of the Distributed File System be similar to that of a centralized file
system.
 Simplicity and ease of use :
The user interface of a file system should be simple and the number of commands in the file
should be small.
 High availability :
A Distributed File System should be able to continue in case of any partial failures like a link failure,
a node failure, or a storage drive crash.
A high authentic and adaptable distributed file system should have different and independent file
servers for controlling different and independent storage devices.
 Scalability :
Since growing the network by adding new machines or joining two networks together is routine,
the distributed system will inevitably grow over time. As a result, a good distributed file system
should be built to scale quickly as the number of nodes and users in the system grows. Service
should not be substantially disrupted as the number of nodes and users grows.
 High reliability :
The likelihood of data loss should be minimized as much as feasible in a suitable distributed file
system. That is, because of the system’s unreliability, users should not feel forced to make backup
copies of their files. Rather, a file system should create backup copies of key files that can be used
if the originals are lost. Many file systems employ stable storage as a high-reliability strategy.
 Data integrity :
Multiple users frequently share a file system. The integrity of data saved in a shared file must be
guaranteed by the file system. That is, concurrent access requests from many users who are
competing for access to the same file must be correctly synchronized using a concurrency control
method. Atomic transactions are a high-level concurrency management mechanism for data
integrity that is frequently offered to users by a file system.
 Security :
A distributed file system should be secure so that its users may trust that their data will be kept
private. To safeguard the information contained in the file system from unwanted & unauthorized
access, security mechanisms must be implemented.
 Heterogeneity :
Heterogeneity in distributed systems is unavoidable as a result of huge scale. Users of
heterogeneous distributed systems have the option of using multiple computer platforms for
different purposes.

History :

The server component of the Distributed File System was initially introduced as an add-on feature.
It was added to Windows NT 4.0 Server and was known as “DFS 4.1”. Then later on it was included
as a standard component for all editions of Windows 2000 Server. Client-side support has been
included in Windows NT 4.0 and also in later on version of Windows.

P a r a m a n a n d a S a h u , D E P T . o f C S E 130 Page 130


OPERATING SYSTEMLECTURE NOTE

Linux kernels 2.6.14 and versions after it come with an SMB client VFS known as “cifs” which
supports DFS. Mac OS X 10.7 (lion) and onwards supports Mac OS X DFS.

Properties:

 File transparency: users can access files without knowing where they are physically stored on the
network.
 Load balancing: the file system can distribute file access requests across multiple computers to
improve performance and reliability.
 Data replication: the file system can store copies of files on multiple computers to ensure that the
files are available even if one of the computers fails.
 Security: the file system can enforce access control policies to ensure that only authorized users can
access files.
 Scalability: the file system can support a large number of users and a large number of files.
 Concurrent access: multiple users can access and modify the same file at the same time.
 Fault tolerance: the file system can continue to operate even if one or more of its components fail.
 Data integrity: the file system can ensure that the data stored in the files is accurate and has not
been corrupted.
 File migration: the file system can move files from one location to another without interrupting
access to the files.
 Data consistency: changes made to a file by one user are immediately visible to all other users.
Support for different file types: the file system can support a wide range of file types, including text
files, image files, and video files.

Applications :

 NFS –
NFS stands for Network File System. It is a client-server architecture that allows a computer user to
view, store, and update files remotely. The protocol of NFS is one of the several distributed file
system standards for Network-Attached Storage (NAS).
 CIFS –
CIFS stands for Common Internet File System. CIFS is an accent of SMB. That is, CIFS is an
application of SIMB protocol, designed by Microsoft.
 SMB –
SMB stands for Server Message Block. It is a protocol for sharing a file and was invented by IMB.
The SMB protocol was created to allow computers to perform read and write operations on files to
a remote host over a Local Area Network (LAN). The directories present in the remote host can be
accessed via SMB and are called as “shares”.
 Hadoop –
Hadoop is a group of open-source software services. It gives a software framework for distributed
storage and operating of big data using the MapReduce programming model. The core of Hadoop
contains a storage part, known as Hadoop Distributed File System (HDFS), and an operating part
which is a MapReduce programming model.
 NetWare –
NetWare is an abandon computer network operating system developed by Novell, Inc. It primarily

P a r a m a n a n d a S a h u , D E P T . o f C S E 131 Page 131


OPERATING SYSTEMLECTURE NOTE

used combined multitasking to run different services on a personal computer, using the IPX
network protocol.

Working of DFS :

There are two ways in which DFS can be implemented:

 Standalone DFS name space –


It allows only for those DFS roots that exist on the local computer and are not using Active
Directory. A Standalone DFS can only be acquired on those computers on which it is created. It
does not provide any fault liberation and cannot be linked to any other DFS. Standalone DFS roots
are rarely come across because of their limited advantage.
 Domain-based DFS name space –
It stores the configuration of DFS in Active Directory, creating the DFS namespace root accessible
at \\<domainname>\<dfsroot> or \\<FQDN>\<dfsroot>

Advantages :

 DFS allows multiple user to access or store the data.


 It allows the data to be share remotely.
 It improved the availability of file, access time, and network efficiency.
 Improved the capacity to change the size of the data and also improves the ability to exchange the
data.
 Distributed File System provides transparency of data even if server or disk fails.

Disadvantages :

 In Distributed File System nodes and connections needs to be secured therefore we can say that
security is at stake.
 There is a possibility of lose of messages and data in the network while movement from one node
to another.
 Database connection in case of Distributed File System is complicated.
P a r a m a n a n d a S a h u , D E P T . o f C S E 132 Page 132
OPERATING SYSTEMLECTURE NOTE

 Also handling of the database is not easy in Distributed File System as compared to a single user
system.
 There are chances that overloading will take place if all nodes tries to send data at once.

Synchronization in Distributed Systems


Distributed System is a collection of computers connected via the high speed communication
network. In the distributed system, the hardware and software components communicate and
coordinate their actions by message passing. Each node in distributed systems can share their
resources with other nodes. So, there is need of proper allocation of resources to preserve the
state of resources and help coordinate between the several processes. To resolve such conflicts,
synchronization is used. Synchronization in distributed systems is achieved via clocks.

The physical clocks are used to adjust the time of nodes.Each node in the system can share its
local time with other nodes in the system. The time is set based on UTC (Universal Time
Coordination). UTC is used as a reference time clock for the nodes in the system.

The clock synchronization can be achieved by 2 ways: External and Internal Clock Synchronization.

1. External clock synchronization is the one in which an external reference clock is present.
It is used as a reference and the nodes in the system can set and adjust their time
accordingly.
2. Internal clock synchronization is the one in which each node shares its time with other
nodes and all the nodes set and adjust their times accordingly.

There are 2 types of clock synchronization algorithms: Centralized and Distributed.

1. Centralized is the one in which a time server is used as a reference. The single time server
propagates its time to the nodes and all the nodes adjust the time accordingly. It is
dependent on single time server so if that node fails, the whole system will lose
synchronization. Examples of centralized are- Berkeley Algorithm, Passive Time Server,
Active Time Server etc.
2. Distributed is the one in which there is no centralized time server present. Instead the
nodes adjust their time by using their local time and then, taking the average of the
differences of time with other nodes. Distributed algorithms overcome the issue of
centralized algorithms like the scalability and single point failure. Examples of Distributed
algorithms are – Global Averaging Algorithm, Localized Averaging Algorithm, NTP
(Network time protocol) etc.

***********************************END *********************************************

====================================================================================

P a r a m a n a n d a S a h u , D E P T . o f C S E 133 Page 133

You might also like