0% found this document useful (0 votes)
102 views185 pages

Operating System CIC305 - Dr. Ashish (Complete OS)

Uploaded by

hiyaaank
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)
102 views185 pages

Operating System CIC305 - Dr. Ashish (Complete OS)

Uploaded by

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

Dr.

Ashish

0
Syllabus
Unit-1
UNi

UNIT I

Introduction: What is an Operating System, Simple Batch Systems, Multiprogrammed


Batches systems, Time Sharing Systems, Personal-computer systems, Parallel systems,
Distributed Systems, Real-Time Systems, OS – A Resource Manager.

Processes: Introduction, Process states, process management, Interrupts, Interprocess


Communication

Threads: Introduction, Thread states, Thread Operation, Threading Models.

Processor Scheduling: Scheduling levels, preemptive vs no preemptive scheduling,


priorities, scheduling objective, scheduling criteria, scheduling algorithms, demand
scheduling, real time scheduling.

UNIT-II

Process Synchronization: Mutual exclusion, software solution to Mutual exclusion problem,


hardware solution to Mutual exclusion problem, semaphores, Critical section problems. Case
study on Dining philosopher problem, Barber shop problem etc.

Memory Organization & Management: Memory Organization, Memory Hierarchy,


Memory Management Strategies, Contiguous versus non- Contiguous memory allocation,
Partition Management Techniques, Logical versus Physical Address space, swapping, Paging,
Segmentation, Segmentation with Paging Virtual Memory: Demand Paging, Page
Replacement, Page-replacement Algorithms, Performance of Demand Paging, Thrashing,
Demand Segmentation, and Overlay Concepts.

Dr. Ashish
1
Unit-1
UNi

1 INTRODUCTION
• An Operating System acts as a communication bridge (interface) between the user
and computer hardware.

• The purpose of an operating system is to provide a platform on which a user can execute
programs conveniently and efficiently.

• An operating system is a piece of software that manages the allocation of Computer


Hardware. The coordination of the hardware must be appropriate to ensure the correct
working of the computer system and to prevent user programs from interfering with the
proper working of the system.

• The main goal of the Operating System is to make the computer environment more
convenient to use and the Secondary goal is to use the resources most efficiently.

1.1 Why Use an Operating System?


The operating system helps in improving the computer software as well as hardware. Without
OS, it became very difficult for any application to be user-friendly. Operating System provides
a user with an interface that makes any application attractive and user-friendly. The operating
System comes with a large number of device drivers that makes OS services reachable to the
hardware environment. Each and every application present in the system requires the Operating
System. The operating system works as a communication channel between system hardware
and system software. The operating system helps interact an application with the hardware part
without knowing about the actual hardware configuration. It is one of the most important parts
of the system and hence it is present in every device, whether large or small device.

Dr. Ashish
2
Unit-1
UNi

1.2 Goals of Operating System

There are mainly 2 goals of the operating system:

• Convenience

• Efficiency

The other 3 goals are:

• Portability and Reliability

• Hardware Abstraction

• Security

1. Convenience

An Operating System's primary and first goal is to provide a friendly and convenient
environment to the user. It is optional to use Operating System. Still, things become harder
when the user has to perform all the process scheduling and convert user commands to machine
language so that system can perform tasks. So, we use an Operating System to act as a bridge
between us and the computer hardware. We only have to give commands to the system, and OS
will take the instructions and do the rest of the work. Because of this operating system should
be convenient to use and operate by the user.

2. Efficiency

The second and important goal of an Operating System is efficiency. An operating system
should utilize all the resources efficiently. The management of resources and programs should
be done so that no resource is kept idle and memory is used for no use.

3. Portability and Reliability

The operating system can work/operate on different machines with different processors and
memory configurations. This makes the operating system more reliable.

Also, the operating system can protect itself and the user from accidental damage from the user
program.

4. Hardware Abstraction

Dr. Ashish
3
Unit-1
UNi

The operating system can conceal or can be said to control all functions and resources of the
computer. The user can give commands and access any function or resource of the computer
without facing any difficulties. In this way, the Operating system communicates between the
user and computer hardware.

5. Security

An operating system provides the safety and security of data between the user and the hardware.
OS enables multiple users to securely share a computer(system), including files, processes,
memory, and device separately.

1.3 The Operating system as a resource manager/ Resource Management in Operating


System

• Resource Management in Operating System is the process to manage all the resources
efficiently like CPU, memory, input/output devices, and other hardware resources
among the various programs and processes running in the computer.
• Resource management is an important thing because resources of a computer are limited
and multiple processes or users may require access to the same resources like CPU,
memory etc. at the same time. The operating system has to manage and ensure that all
processes get the resources they need to execute, without any problems like deadlocks.

Resource manager do the following:

o Keep track of the STATUS of resources


o Enforce policy to determine who gets what , when and how much
o Allocate the Resource
o Deallocate /Reclaim the Resource

Here are some Terminologies related to the resource management in OS:

Resource Allocation: This terms defines the process of assigning the available resources to
processes in the operating system. This can be done dynamically or statically.

Resource: Resource can be anything that can be assigned dynamically or statically in the
operating system. Example may include CPU time, memory, disk space, and network
bandwidth etc.

Dr. Ashish
4
Unit-1
UNi

Resource Management: It refers to how to manage resources efficiently between different


processes.

Process: Process refers to any program or application that is being executed in the operating
system and has its own memory space, execution state, and set of system resources.

Scheduling: It is the process of determining from multiple number of processes which process
should be allocated a particular resource at a given time.

Deadlock: When two or more processes are waiting for some resource but resources are busy
somewhere else and resources are also waiting for some process to complete their execution .
In such condition neither resources will be freed nor process would get it and this situation is
called deadlock.

Semaphore: It is the method or tool which is used to prevent race condition. Semaphore is an
integer variable which is used in mutual exclusive manner by various concurrent cooperative
process in order to achieve synchronization.

Mutual Exclusion: It is the technique to prevent multiple number of process to access the same
resources simultaneously.

Memory Management: Memory management is a method used in the operating systems to


manage operations between main memory and disk during process execution.

2 FUNCTIONS OF AN OPERATING SYSTEM


Following are some of important functions of an operating System.

1. Memory Management

2. Processor Management

3. Device Management

4. File Management

5. Network Management

some of the important activities that an Operating System performs

Dr. Ashish
5
Unit-1
UNi

• Security

• Control over system


performance

• Job accounting

• Error detecting aids

• Coordination between other software and users

2.1 Memory Management


The operating system manages the Primary Memory or Main Memory. Main memory is made up
of a large array of bytes or words where each byte or word is assigned a certain address. Main
memory is fast storage and it can be accessed directly by the CPU. For a program to be executed,
it should be first loaded in the main memory. An operating system manages the allocation and
deallocation of memory to various processes and ensures that the other process does not consume
the memory allocated to one process. An Operating System performs the following activities for
Memory Management:

• It keeps track of primary memory, i.e., which bytes of memory are used by which user
program. The memory addresses that have already been allocated and the memory
addresses of the memory that has not yet been used.
• In multiprogramming, the OS decides the order in which processes are granted memory
access, and for how long.
• It Allocates the memory to a process when the process requests it and deallocates the
memory when the process has terminated or is performing an I/O operation.

2.2 Processor Management


• In a multi-programming environment, the OS decides the order in which processes have
access to the processor, and how much processing time each process has. This function of

Dr. Ashish
6
Unit-1
UNi

OS is called Process Scheduling. An Operating System performs the following activities


for Processor Management.
• An operating system manages the processor’s work by allocating various jobs to it and
ensuring that each process receives enough time from the processor to function properly.
• Keeps track of the status of processes. The program which performs this task is known as
a traffic controller. Allocates the CPU that is a processor to a process. De-allocates
processor when a process is no longer required.
• When more than one process runs on the system the OS decides how and when a process
will use the CPU. Hence, the name is
also CPU Scheduling. The OS:
• Allocates and deallocates processor to
the processes.
• Keeps record of CPU status.
• Certain algorithms used for CPU
scheduling are as follows:
o First Come First Serve (FCFS)
o Shortest Job First (SJF)
o Round-Robin Scheduling
o Priority-based scheduling etc.

2.3 Device Management


An OS manages device communication via its respective drivers. It
performs the following activities for device management. Keeps track
of all devices connected to the system. designates a program responsible
for every device known as the Input/Output controller. Decide which
process gets access to a certain device and for how long. Allocates
devices effectively and efficiently. Deallocates devices when they are
no longer required. There are various input and output devices. an OS
controls the working of these input-output devices. It receives the
requests from these devices, performs a specific task, and
communicates back to the requesting process.

2.4 File Management


A file system is organized into directories for efficient or easy navigation and usage. These
directories may contain other directories and other files. An Operating System carries out the

Dr. Ashish
7
Unit-1
UNi

following file management activities. It keeps track of where information is stored, user access
settings, the status of every file, and more. These facilities are collectively known as the file
system. An OS keeps track of information regarding the creation, deletion, transfer, copy, and
storage of files in an organized way. It also maintains the integrity of the data stored in these files,
including the file directory structure, by protecting against unauthorized access.

2.5 Storage Management


• Storage management is a procedure that allows users to maximize the utilization of
storage devices while also protecting data integrity on whatever media on which it lives.
Network virtualization, replication, mirroring, security, compression, deduplication,
traffic analysis, process automation, storage provisioning, and memory management are
some of the features that may be included.

• The operating system is in charge of storing and accessing files. The creation of files, the
creation of directories, the reading and writing of data from files and directories, as well
as the copying of the contents of files and directories from one location to another are all
included in storage management.

• The OS uses storage management for:

• Improving the performance of the data storage resources.

• It optimizes the use of various storage devices.

• Assists businesses in storing more data on existing hardware, speeding up the data
retrieval process, preventing data loss, meeting data retention regulations, and lowering
IT costs

2.6 Following are some of the important activities that an Operating System performs −
• Security – For security, modern operating systems employ a firewall. A firewall is a type
of security system that monitors all computer activity and blocks it if it detects a threat.

• Job Accounting – As the operating system keeps track of all the functions of a computer
system. Hence, it makes a record of all the activities taking place on the system. It has an
account of all the information about the memory, resources, errors, etc. Therefore, this
information can be used as and when required.

Dr. Ashish
8
Unit-1
UNi

• Control over system performance – The operating system will collect consumption
statistics for various resources and monitor performance indicators such as reaction time,
which is the time between requesting a service and receiving a response from the system.

• Error detecting aids – While a computer system is running, a variety of errors might occur.
Error detection guarantees that data is delivered reliably across susceptible networks. The
operating system continuously monitors the system to locate or recognize problems and
protects the system from them.

• Coordination between other software and users – The operating system (OS) allows
hardware components to be coordinated and directs and allocates assemblers, interpreters,
compilers, and other software to different users of the computer system.

• Booting process – The process of starting or restarting a computer is referred to as


Booting. Cold booting occurs when a computer is totally turned off and then turned back
on. Warm booting occurs when the computer is restarted. The operating system (OS) is in
charge of booting the computer.

3 Types of Operating Systems


An operating System performs all the basic tasks like managing files, processes, and memory.
Thus operating system acts as the manager of all the resources, i.e. resource manager. Thus,
the operating system becomes an interface between the user and the machine. It is one of the
most required software that is present in the device.
Operating System is a type of software that works as an interface between the system program
and the hardware. There are several types of Operating Systems in which many of which are
mentioned below. Let’s have a look at them.

3.1 Batch Operating System


• This type of operating system does not interact with the computer directly. There is an
operator which takes similar jobs having the same requirement and groups them into
batches. It is the responsibility of the operator to sort jobs with similar needs.

• In the beginning, computers were extremely large machines that ran from a console. In
general, tape drivers or card readers were used for input, and tape drives, punch
cards, and line printers were used for output. Users had no direct interface with the
system, and job execution was done in a batch system. These systems are known as
batched operating systems, and users have to prepare a job separately to execute it.

Dr. Ashish
9
Unit-1
UNi

• There were no developed operating systems, complex computer architecture, or


secondary memory devices in the 1950s and 1960s. Instead, large mainframe computers
were used to process data, with punched cards or magnetic tapes serving as input and
output. The primary problem at the time was a lack of hard disks.
• In the early 1950s, General Motors Research Laboratories (GMRL) introduced the first
Single-Stream batch processing systems. It only executed one job at a time, and data
was sent in batches or groups. The batch operating system eliminates the setup time
issue.

Types of Batch Operating System

There are mainly two types of the batch operating system. These are as follows:

1. Simple Batched System


2. Multi-programmed batched system

❖ Simple Batched System

• The user did not directly interact with the computer system for job execution in a simple
batch operating system. However, the user was required to prepare a job that included
the program, control information, and data on the nature of the job on control cards. The
job was then submitted to the computer operator, who was usually in the form of a punch
card. The program's output included results and registers and memory dumps in the
event of a program error. The output appeared after some time that could take days,
hours, and minutes.

Dr. Ashish
10
Unit-1
UNi

• Its main role was to transfer control from one job to another. Jobs with similar
requirements were pooled together and processed through the processor to improve
processing speed. The operators were used in the program to create batches with similar
needs. The computer runs the batches one by one when they became available. This
system typically reads a sequence of jobs, each with its control cads and predefined job
tasks.

Dr. Ashish
11
Unit-1
UNi

❖ Multi-programmed batched system

• Spooling deals with many jobs that have already been read and are waiting to run on
disk. A disk containing a pool of jobs allows the operating system to choose which job
to run next to maximize CPU utilization.
• Jobs that come on magnetic tape or cards directly cannot be run in a different order.
Jobs run sequentially because they are executed in a first-come, first-served manner.
When various jobs are stored on a direct access device, job scheduling becomes possible
like a disk. Multi-programming is an important feature of job scheduling. For
overlapped I/O, spooling and offline operations have their limitations. Generally, a
single user could not maintain all of the input/output devices, and CPU buys at all times.

Advantages of Batch Operating System


• It is very difficult to guess or know the time required for any job to complete.
Processors of the batch systems know how long the job would be when it is in the
queue.
• Multiple users can share the batch systems.
• The idle time for the batch system is very less.
• It is easy to manage large work repeatedly in batch systems.
Disadvantages of Batch Operating System
• The computer operators should be well known with batch systems.
• Batch systems are hard to debug.
• It is sometimes costly.
• The other jobs will have to wait for an unknown time if any job fails.
Examples of Batch Operating Systems: Payroll Systems, Bank Statements, etc.

Dr. Ashish
12
Unit-1
UNi

3.2 Multi-Programming Operating System

Multiprogramming Operating Systems can be simply illustrated as more than one program is
present in the main memory and any one of them can be kept in execution. This is basically used
for better execution of resources.

Advantages of Multi-Programming Operating System

• Multi Programming increases the Throughput of the System.

• It helps in reducing the response time.

Dr. Ashish
13
Unit-1
UNi

Disadvantages of Multi-Programming Operating System

• There is not any facility for user interaction of system resources with the system.

Dr. Ashish
14
Unit-1
UNi

Dr. Ashish
15
Unit-1
UNi

3.3 Real-Time Operating System


These types of OSs serve real-time systems. The time interval required to process and respond to
inputs is very small. This time interval is called response time. Real-time systems are used when
there are time requirements that are very strict like missile
systems, air traffic control systems, robots, etc.

Types of Real-Time Operating Systems

Hard Real-Time Systems:

Hard Real-Time OSs are meant for applications where time


constraints are very strict and even the shortest possible delay
is not acceptable. These systems are built for saving life like
automatic parachutes or airbags which are required to be
readily available in case of an accident. Virtual memory is
rarely found in these systems.

Soft Real-Time Systems: These OSs are for applications where time-constraint is less strict.

3.4 Distributed OS
This system is based on autonomous but interconnected computers communicating with each
other via communication lines or a shared network. Each autonomous system has its own
processor that may differ in size and function. These operating systems are often used for tasks
such as telecommunication networks, airline reservation controls and peer-to-peer networks.

A distributed operating system serves multiple applications and multiple users in real time. The
data processing function is then distributed across the processors. Potential advantages and
disadvantages of distributed operating systems are:

Advantages Disadvantages

If the primary network fails, the entire system


They allow remote working.
shuts down.

They allow a faster exchange of data among users. They're expensive to install.

Failure in one site may not cause much disruption to the They require a high level of expertise to
system. maintain.

They reduce delays in data processing.

Dr. Ashish
16
Unit-1
UNi

Advantages Disadvantages

They minimize the load on the host computer.

They enhance scalability since more systems can be


added to the network.

3.5 What is a personal computer (PC)?


• A personal computer (PC) is a microcomputer designed for use by one person at a time.
• The PC is a general-purpose, cost-effective computer that stands for the personal computer. It is
When Ed Roberts introduced the MITS Altair 8800, he coined the term PC. Alternatively, it is
referred to as a single-user computer and a desktop that is designed to be used by a single end-
user. All PCs depend on the microprocessor technology that makes it capable for PC makers to set
the whole CPU (central processing unit) on a single chip.
• Generally, a PC contains a keyboard, mouse, monitor, and system unit. Most of the personal
computers have an Internet or a network connection, including ports for connecting peripheral
devices like external hard drives, scanners, printers, digital cameras, and more. Even if the term
PC can refer to any personal computer, including an Apple Macintosh computer, but it is
commonly used in the computer industry to describe an IBM or IBM-compatible computer.
• Personal computers are used to create spreadsheets, write papers, play games, track our finances,
account, run databases, and many other tasks. If your PC is connected to the Internet, you can use
it for communicating with friends via instant messaging programs, browsing the Web, checking
e-mail, and downloading data or files. It is normal for a personal computer to create a network by
connecting more than one PC together, even though they are designed to use as single-user
systems.
• A personal computer can be a laptop computer, a microcomputer, a handheld PC, a desktop
computer, or a tablet PC. Additionally, without PCs, life can be difficult as it has become such an
integral part of our lives.

3.6 What is a Parallel Operating System?


Parallel operating systems are designed to speed up the execution of programs by dividing them
into multiple segments. It is used for dealing with multiple processors simultaneously by using
computer resources which include a single computer with multiple processors and several
computers connected by a network to form a cluster of parallel processing or a combination of
both.

Dr. Ashish
17
Unit-1
UNi

How do Parallel Operating Systems Work?

• It is an evolution of serial processing wherein tasks are broken down into manageable
tasks that can be executed concurrently.
• Further, each part is broken down into a series of instructions executed simultaneously on
different CPUs. The working of the parallel operating system is such that the CPU and its
components are divided into smaller parts, each having full speed and power.
• In a normal operating system, once an I/O device is identified, it will use the CPU to
transfer the information into the memory before performing any operations on it, like
processing and transmitting.
• By parallel operating systems, however, more data can be transferred and processed
simultaneously, resulting in quicker data transfer.

Dr. Ashish
18
Unit-1
UNi

3.7 Difference Between Distributed System and Parallel System:

S. No Parallel System Distributed System

Parallel systems are the systems that can


In these systems, applications are running on
process the data simultaneously, and
multiple computers linked by communication
increase the computational speed of a
lines.
1. computer system.

The distributed system consists of a number of


Parallel systems work with the
computers that are connected and managed so
simultaneous use of multiple computer
that they share the job processing load among
resources which can include a single
various computers distributed over the
computer with multiple processors.
2. network.

Tasks are performed with a more speedy Tasks are performed with a less speedy
3. process. process.

These systems are multiprocessor In Distributed Systems, each processor has its
4. systems. own memory.

It is also known as a tightly coupled Distributed systems are also known as loosely
5. system. coupled systems.

These systems communicate with one another


These systems have close communication
through various communication lines, such as
with more than one processor.
6. high-speed buses or telephone lines.

These systems share a memory, clock, and These systems do not share memory or clock
7. peripheral devices in contrast to parallel systems.

In this there is no global clock in distributed


In this, all processors share a single master
computing, it uses various synchronization
clock for synchronization.
8. algorithms.

E.g:- Hadoop, MapReduce, Apache E.g:- High-Performance Computing clusters,


9. Cassandra Beowulf clusters

4 Spooling
Spooling is the process of pushing data from various I/O jobs into a buffer, disc, or somewhere
in the memory so that a device can access the data when it is ready.

Dr. Ashish
19
Unit-1
UNi

In order to maintain the spooling buffer, the OS handles I/O device data spooling because the
devices have varying data access rates. Buffer acts as a waiting station for data to rest while
slower devices catch up. Print Spooling is a spooling application.

Dr. Ashish
20
Unit-1
UNi

CPU Scheduling

5 Process concept
5.1 Process

A process is basically a program in execution. The execution of a process must progress in a


sequential fashion.

A process is defined as an entity which represents the basic unit


of work to be implemented in the system.

To put it in simple terms, we write our computer programs in a


text file and when we execute this program, it becomes a process
which performs all the tasks mentioned in the program.

When a program is loaded into the memory and it becomes a


process, it can be divided into four sections ─ stack, heap, text and
data. The following image shows a simplified layout of a process
inside main memory −

S.N. Component & Description


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

A program is a piece of code which may be a single line or millions of lines. A computer
program is usually written by a computer programmer in a programming language. For
example, here is a simple program written in C programming language −

#include <stdio.h>

int main() {
printf("Hello, World! \n");
return 0;
}

Dr. Ashish
21
Unit-1
UNi

A computer program is a collection of instructions that performs a specific task when executed
by a computer. When we compare a program with a process, we can conclude that a process is a
dynamic instance of a computer program.

A part of a computer program that performs a well-defined task is known as an algorithm. A


collection of computer programs, libraries and related data are referred to as a software.

5.3 Process Control Block (PCB)

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

S.N. Information & Description


1 Process State
The current state of the process i.e., whether it is ready, running, waiting, or whatever.
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 registers
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.
10 IO status information
This includes a list of I/O devices allocated to the process.

Dr. Ashish
22
Unit-1
UNi

The architecture of a PCB is completely dependent on Operating System and may contain
different information in different operating systems. Here is a simplified diagram of a PCB −

The PCB is maintained for a process throughout its lifetime, and


is deleted once the process terminates.

5.4 Context switching


An operating system uses this technique to switch a process
between states to execute its functions through CPUs. It is a
process of saving the context(state) of the old process(suspend)
and loading it into the new process(resume). It occurs whenever
the CPU switches between one process and another. Basically,
the state of CPU’s registers and program counter at any time
represent a context. Here, the saved state of the currently
executing process means to copy all live registers to PCB
(Process Control Block). Moreover, after that, restore the state
of the process to run or execute next, which means copying live
registers’ values from PCB to registers.

Dr. Ashish
23
Unit-1
UNi

5.5 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. A process is more than just a set of instructions. It contains information
such as the process stack, the program counter, and the contents of the process register, among
other things. When a process runs, it modifies the state of the system. The current activity of a
given process determines the state of the process in general.

5.6 What are the Process States in Operating System?


From start to finish, the process goes through a number of stages. A minimum of five states is
required. Even though the process could be in one of these states during execution, the names of
the states are not standardised. Throughout its life cycle, each process goes through various
stages. They are:

5.6.1 New State


When a program in secondary memory is started for execution, the process is said to be in a new
state.

5.6.2 Ready State


After being loaded into the main memory and ready for execution, a process transitions from a
new to a ready state. The process will now be in the ready state, waiting for the processor to
execute it. Many processes may be in the ready stage in a multiprogramming environment.

Dr. Ashish
24
Unit-1
UNi

5.6.3 Run State


After being allotted the CPU for execution, a process passes from the ready state to the run state.

5.6.4 Terminate State


When a process’s execution is finished, it goes from the run state to the terminate state. The
operating system deletes the process control box (or PCB) after it enters the terminate state.

5.6.5 Block or Wait State


• If a process requires an Input/Output operation or a blocked resource during execution, it
changes from run to block or the wait state.

• The process advances to the ready state after the I/O operation is completed or the resource
becomes available.

5.6.6 Suspend Ready State


If a process with a higher priority needs to be executed while the main memory is full, the process
goes from ready to suspend ready state. Moving a lower-priority process from the ready state to
the suspend ready state frees up space in the ready state for a higher-priority process.

Until the main memory becomes available, the process stays in the suspend-ready state. The
process is brought to its ready state when the main memory becomes accessible.

5.6.7 Suspend Wait State


• If a process with a higher priority needs to be executed while the main memory is full, the
process goes from the wait state to the suspend wait state. Moving a lower-priority process
from the wait state to the suspend wait state frees up space in the ready state for a higher-
priority process.

• The process gets moved to the suspend-ready state once the resource becomes accessible.
The process is shifted to the ready state once the main memory is available.

5.6.8 Important Notes


Note – 01:
A process must pass through at least four states.

• A process must go through a minimum of four states to be considered complete.


• The new state, run state, ready state, and terminate state are the four states.
• However, in case a process also requires I/O, the minimum number of states required is 5

Note – 02:
Only one process can run at a time on a single CPU.

Dr. Ashish
25
Unit-1
UNi

• Any processor can only handle one process at a time.


• When there are n processors in a system, only n processes can run at the same time.
Note – 03:
Present in Memory State

Secondary Memory New state

Main Memory Ready state

Main Memory Run state

Main Memory Wait state

Secondary Memory Suspend wait state

Secondary Memory Suspend ready state

Note – 04:
It is much more preferable to move a given process from its wait state to its suspend wait
state.

• Consider the situation where a high-priority process comes, and the main memory is full.
• Then there are two options for making space for it. They are:

1. Suspending the processes that have lesser priority than the ready state.
2. Transferring the lower-priority processes from wait to the suspend wait state.
Now, out of these: Moving a process from a wait state to a suspend wait state is the superior
option.

• It is because this process is waiting already for a resource that is currently unavailable.

5.7 Type of Process Schedulers


A scheduler is a type of system software that allows you to handle process scheduling.

There are mainly three types of Process Schedulers:

1. Long Term Scheduler


2. Short Term Scheduler
3. Medium Term Scheduler

Dr. Ashish
26
Unit-1
UNi

5.7.1 Long Term Scheduler


Long term scheduler is also known as a job scheduler. This scheduler regulates the program and
select process from the queue and loads them into memory for execution. It also regulates the

degree of multi-programming. However, the main goal of this type of scheduler is to offer a
balanced mix of jobs, like Processor, I/O jobs., that allows managing multiprogramming.

5.7.2 Medium Term Scheduler


Medium-term scheduling is an important part of swapping. It enables you to handle the swapped
out-processes. In this scheduler, a running process can become suspended, which makes an I/O
request.

A running process can become suspended if it makes an I/O request. A suspended processes can’t
make any progress towards completion. In order to remove the process from memory and make
space for other processes, the suspended process should be moved to secondary storage.

5.7.3 Short Term Scheduler


Short term scheduling is also known as CPU scheduler. The main goal of this scheduler is to
boost the system performance according to set criteria. This helps you to select from a group of
processes that are ready to execute and allocates CPU to one of them. The dispatcher gives control
of the CPU to the process selected by the short term scheduler.

Dr. Ashish
27
Unit-1
UNi

6 What is Inter process Communication?


A process can be of two types:

• Independent process.

• Co-operating process.

An independent process is not affected by the execution of other processes while a co-operating
process can be affected by other executing processes. Though one can think that those processes,
which are running independently, will execute very efficiently, in reality, there are many situations
when co-operative nature can be utilized for increasing computational speed, convenience, and
modularity. Inter-process communication (IPC) is a mechanism that allows processes to
communicate with each other and synchronize their actions. The communication between these
processes can be seen as a method of co-operation between them. Inter process communication
(IPC) is a process that allows different processes of a computer system to share information. IPC
lets different programs run in parallel, share data, and communicate with each other. It’s important
for two reasons: First, it speeds up the execution of tasks, and secondly, it ensures that the tasks
run correctly and in the order that they were executed.

Processes can communicate with each other through both:

1. Shared Memory

2. Message passing

• Communication between processes using shared memory requires processes to share some
variable, and it completely depends on how the programmer will implement it.
• Suppose process1 and process2 are executing simultaneously, and they share some resources or use
some information from another process. Process1 generates information about certain computations or
resources being used and keeps it as a record in shared memory.
• When process2 needs to use the shared information, it will check in the record stored in shared memory
and take note of the information generated by process1 and act accordingly. Processes can use shared
memory for extracting information as a record from another process as well as for delivering any
specific information to other processes.

Dr. Ashish
28
Unit-1
UNi

ii) Messaging Passing Method


Now, We will start our discussion of the communication between processes via message passing.
In this method, processes communicate with each other without using any kind of shared memory.
If two processes p1 and p2 want to communicate with each other, they proceed as follows:

• Establish a communication link (if a link already exists, no need to establish it again.)
• Start exchanging messages using basic primitives.
We need at least two primitives:
– send(message, destination) or send(message)
– receive(message, host) or receive(message)

The message size can be of fixed size or of variable size. If it is of fixed size, it is easy for an
OS designer but complicated for a programmer and if it is of variable size then it is easy for a
programmer but complicated for the OS designer. A standard message can have two
parts: header and body.
The header part is used for storing message type, destination id, source id, message length,
and control information. The control information contains information like what to do if runs
out of buffer space, sequence number, priority. Generally, message is sent using FIFO style.

Message Passing through Communication Link. Direct and Indirect Communication link
Direct Communication links are implemented when the processes use a specific process
identifier for the communication, but it is hard to identify the sender ahead of time.
For example the print server.
In-direct Communication is done via a shared mailbox (port), which consists of a queue of
messages. The sender keeps the message in mailbox and the receiver picks them up.
Read More
https://www.geeksforgeeks.org/inter-process-communication-
ipc/#:~:text=Inter%2Dprocess%20communication%20(IPC),Shared%20Memory

Dr. Ashish
29
Unit-1
UNi

7 What is Interrupt in OS?

• An interrupt is a signal emitted by hardware or software when a process or an event needs


immediate attention. It alerts the processor to a high-priority process requiring interruption of
the current working process. In I/O devices, one of the bus control lines is dedicated for this
purpose and is called the Interrupt Service Routine (ISR).
• When a device raises an interrupt at the process, the processor first completes the execution
of an instruction. Then it loads the Program Counter (PC) with the address of the first
instruction of the ISR. Before loading the program counter with the address, the address of
the interrupted instruction is moved to a temporary location. Therefore, after handling the
interrupt, the processor can continue with the process.
• While the processor is handling the interrupts, it must inform the device that its request has
been recognized to stop sending the interrupt request signal. Also, saving the registers so that
the interrupted process can be restored in the future increases the delay between the time an
interrupt is received and the start of the execution of the ISR. This is called Interrupt Latency.
• An operating system usually has some code that is called an interrupt handler. The interrupt
handler prioritizes the interrupts and saves them in a queue if more than one is waiting to be
handled. The operating system has another little program called a scheduler that figures out
which program to control next.

Types of Interrupt
Interrupt signals may be issued in response to
hardware or software events. These are classified
as hardware interrupts or software interrupts,
respectively.
https://www.javatpoint.com/what-is-interrupt-
in-os

Dr. Ashish
30
Unit-1
UNi

8 Thread
A thread is a single sequence stream within a process. Threads
are also called lightweight processes as they possess some of the
properties of processes. Each thread belongs to exactly one
process. In an operating system that supports multithreading, the
process can consist of many threads. But threads can be effective
only if the CPU is more than 1 otherwise two threads have to
context switch for that single CPU.

What is Thread in Operating Systems?

In a process, a thread refers to a single sequential activity being executed. these activities are also
known as thread of execution or thread control. Now, any operating system process can execute
a thread. we can say, that a process can have 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.

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

1. Kernel level thread.


2. User-level thread.

Important: Modern operating systems allow a process to be divided into multiple threads of
execution. The threads within a process share all process management information except for
information directly related to execution. This information is moved out of the PCB into thread
control block (TCBs). Threads in a process can execute different parts of the program code at
the same time. They can also execute the same parts of the code at the same time, but with
different execution state:
• They have independent current instructions; that is, they have (or appear to have)
independent program counters.
• They are working with different data; that is, they are (or appear to be) working with
independent registers.

This is accomplished by moving execution state out of the PCB into thread control blocks (TCBs).
Each thread has its own TCB.

Dr. Ashish
31
Unit-1
UNi

Processes start out with a single main thread.


The main thread can create new threads using a
thread fork system call. The new threads can also
use this system call to create more threads.
Consequently, a thread not only belongs to a
process; it also has a parent thread - the thread
that created it.

8.1 Thread operations


Thread operations in an operating system involve the following key actions:

1. Thread Creation: A thread is started within a process using system calls (e.g.,
pthread_create() or CreateThread()).
2. Thread Execution: Threads execute concurrently, either via time-slicing on a single CPU
or in parallel on multiple cores.
3. Thread Synchronization: Mechanisms like mutexes, semaphores, and condition
variables prevent race conditions when accessing shared resources.
4. Thread Scheduling: The OS determines which thread runs using preemptive or
cooperative scheduling.
5. Thread Termination: A thread can end normally when its task is done, or it can be
forcefully terminated by the OS or other threads.
6. Context Switching: The OS switches between threads, saving and loading their execution
state as needed.

8.2 Thread Models in Operating System

A thread is a light weight process which is similar to a process where every process can have
one or more threads. Each thread contains a Stack and a Thread Control Block. There are four
basic thread models :
1. User Level Single Thread Model :

• Each process contains a single thread.


• Single process is itself a single thread.
• process table contains an entry for every process by maintaining its PCB.

Dr. Ashish
32
Unit-1
UNi

2. User Level Multi Thread Model :

• Each process contains multiple threads.


• All threads of the process are scheduled by a thread library at user level.
• Thread switching can be done faster than process switching.
• Thread switching is independent of operating system which can be done within a process.
• Blocking one thread makes blocking of entire process.
• Thread table maintains Thread Control Block of each thread of a process.
• Thread scheduling happens within a process and not known to Kernel.

3. Kernel Level Single Thread Model :

• Each process contains a single thread.


• Thread used here is kernel level thread.
• Process table works as thread table.

Dr. Ashish
33
Unit-1
UNi

4. Kernel Level Multi Thread Model :

• Thread scheduling is done at kernel level.


• Fine grain scheduling is done on a thread basis.
• If a thread blocks, another thread can be scheduled without blocking the whole process.
• Thread scheduling at Kernel process is slower compared to user level thread scheduling.
• Thread switching involves switch.

9 CPU Scheduling
• Scheduling is the process of allotting the CPU to the processes present in the ready queue.
We also refer to this procedure as process scheduling.

• The operating system schedules the process so that the CPU always has one process to
execute. This reduces the CPU’s idle time and increases its utilization.

• The part of OS that allots the computer resources to the processes is termed as
a scheduler. It uses scheduling algorithms to decide which process it must allot to the
CPU.

Dr. Ashish
34
Unit-1
UNi

9.1 Scheduling Objectives

Be Fair while allocating resources to the processes


Maximize throughput of the system
Maximize number of users receiving acceptable response times.
Be predictable
Balance resource use
Avoid indefinite postponement
Enforce Priorities
Give preference to processes holding key resources
Give better service to processes that have desirable behaviour patterns

CPU and I/O Burst Cycle:


Process execution consists of a cycle of CPU execution and I/O wait.
Processes alternate between these two states.
Process execution begins with a CPU burst, followed by an I/O burst, then another CPU
burst ... etc
The last CPU burst will end with a system request to terminate execution rather than
with another I/O burst.
The duration of these CPU burst have been measured.
An I/O-bound program would typically have many short CPU bursts, A CPU-bound
program might have a few very long CPU bursts.

Dr. Ashish
35
Unit-1
UNi

This can help to select an appropriate CPU-scheduling algorithm.

Preemptive Scheduling:
Preemptive scheduling is used when a process switches from running state to ready
state or from waiting state to ready state.
The resources (mainly CPU cycles) are allocated to the process for the limited amount
of time and then is taken away, and the process is again placed back in the ready queue
if that process still has CPU burst time remaining.
That process stays in ready queue till it gets next chance to execute.

Non-Preemptive Scheduling:
Non-preemptive Scheduling is used when a process terminates, or a process switch
from running to waiting state.
In this scheduling, once the resources (CPU cycles) is allocated to a process, the process
holds the CPU till it gets terminated or it reaches a waiting state.
In case of non-preemptive scheduling does not interrupt a process running CPU in
middle of the execution.
Instead, it waits till the process complete its CPU burst time and then it can allocate the
CPU to another process.

Dr. Ashish
36
Unit-1
UNi

Basis for
Preemptive Scheduling Non Preemptive Scheduling
Comparison
Once resources are allocated to a
Basic The resources are allocated to a process, the process holds it till it
process for a limited time. completes its burst time or switches to
waiting state.
Process can be interrupted in Process can not be interrupted till it
Interrupt
between. terminates or switches to waiting state.
If a high priority process
If a process with long burst time is running
Starvation frequently arrives in the ready
CPU, then another process with less CPU
queue, low priority process may
burst time may starve.
starve.
Preemptive scheduling has
Non-preemptive scheduling does not have
Overhead overheads of scheduling the
overheads.
processes.
Flexibility Preemptive scheduling is flexible. Non-preemptive scheduling is rigid.
Preemptive scheduling is cost Non-preemptive scheduling is not cost
Cost
associated. associative.
9.2 Scheduling Criteria
There are several different criteria to consider when trying to select the "best" scheduling
algorithm for a particular situation and environment,
including:
o CPU utilization - Ideally the CPU would be busy
100% of the time, so as to waste 0 CPU cycles. On a
real system CPU usage should range from 40% (
lightly loaded ) to 90% ( heavily loaded. )
o Throughput - Number of processes completed per
unit time. May range from 10 / second to 1 / hour
depending on the specific processes.

o Turnaround time - Time required for a particular process to complete, from submission
time to completion.
o Waiting time - How much time processes spend in the ready queue waiting their turn to get
on the CPU.

Dr. Ashish
37
Unit-1
UNi

o Response time - The time taken in an interactive program from the issuance of a command
to the commence of a response to that command.

In brief:
1. Arrival Time: Time at which the process arrives in the ready queue.
2. Completion Time: Time at which process completes its execution.
3. Burst Time: Time required by a process for CPU execution.
4. Turn Around Time: Time Difference between completion time and arrival time. Turn
Around Time = Completion Time – Arrival Time
5. Waiting Time(W.T): Time Difference between turnaround time and burst time.
Waiting Time = Turn Around Time – Burst Time
6. Response Time
In an interactive system, turn-around time is not the best criterion. A process may
produce some output fairly early and continue computing new results while previous
results are being output to the user. Thus another criterion is the time taken from
submission of the process of the request until the first response is produced. This
measure is called response time.
Response Time = CPU Allocation Time(when the CPU was allocated for the first) -
Arrival Time

10 Types of Scheduling Algorithm


10.1 First Come First Serve (FCFS)
In FCFS Scheduling
• The process which arrives first in the ready queue is firstly assigned the CPU.
• In case of a tie, process with smaller process id is executed first.
• It is always non-preemptive in nature.
• Jobs are executed on first come, first serve basis.
• It is a non-preemptive, pre-emptive scheduling algorithm.
• Easy to understand and implement.
• Its implementation is based on FIFO queue.
• Poor in performance as average wait time is high.

Advantages-
• It is simple and easy to understand.

Dr. Ashish
38
Unit-1
UNi

• It can be easily implemented using queue data structure.


• It does not lead to
starvation.
Disadvantages-
• It does not consider the priority or burst time of the processes.
• It suffers from convoy effect i.e. processes with higher burst time arrived before the
processes with smaller burst time.

Example 1:

Dr. Ashish
39
Unit-1
UNi

Example 2:
Consider the processes P1, P2, P3 given in the below table, arrives for execution in the same
order, with Arrival Time 0, and given Burst Time,
PROCESS ARRIVAL TIME BURST TIME
P1 0 24
P2 0 3
P3 0 3
Gantt chart

P1 P2 P3
0 24 27 30

PROCESS WAIT TIME TURN AROUND


TIME
P1 0 24
P2 24 27
P3 27 30

Total Wait Time = 0 + 24 + 27 = 51 ms

Average Waiting Time = (Total Wait Time) / (Total number of processes) = 51/3 = 17 ms
Total Turn Around Time: 24 + 27 + 30 = 81 ms
Average Turn Around time = (Total Turn Around Time) / (Total number of processes)
= 81 / 3 = 27 ms
Throughput = 3 jobs/30 sec = 0.1 jobs/sec
Example 3:
1. Consider the processes P1, P2, P3, P4 given in the below table, arrives for execution in
the same order, with given Arrival Time and Burst Time.
PROCESS ARRIVAL TIME BURST TIME
P1 0 8
P2 1 4
P3 2 9
P4 3 5

Dr. Ashish
40
Unit-1
UNi

Gantt chart
P1 P2 P3 P4
0 8 12 21 26

PROCESS WAIT TIME TURN AROUND TIME


P1 0 8–0=8
P2 8–1=7 12 – 1 = 11
P3 12 – 2 = 10 21 – 2 = 19
P4 21 – 3 = 18 26 – 3 = 23

Total Wait Time:= 0 + 7 + 10 + 18 = 35 ms

Average Waiting Time = (Total Wait Time) / (Total number of processes)= 35/4 = 8.75 ms
Total Turn Around Time: 8 + 11 + 19 + 23 = 61 ms
Average Turn Around time = (Total Turn Around Time) / (Total number of processes)
61/4 = 15.25 ms

Throughput: 4 jobs/26 sec = 0.15385 jobs/sec

Dr. Ashish
41
Unit-1
UNi

Dr. Ashish
42
Unit-1
UNi

10.2 Shortest Job First (SJF) or (Shortest Job Next)


Process which has the shortest burst time are scheduled first.
If two processes have the same bust time, then FCFS is used to break the tie.
This is a non-pre-emptive, pre-emptive scheduling algorithm.
Best approach to minimize waiting time.
Easy to implement in Batch systems where required CPU time is known in advance.
Impossible to implement in interactive systems where required CPU time is not
known.
The processer should know in advance how much time process will take.
Pre-emptive mode of Shortest Job First is called as Shortest Remaining Time First
(SRTF).

Advantages-
SRTF is optimal and guarantees the minimum average waiting time.
It provides a standard for other algorithms since no other algorithm performs better
than it.

Disadvantages-
It can not be implemented practically since burst time of the processes can not be
known in advance.
It leads to starvation for processes with larger burst time.
Priorities can not be set for the processes.
Processes with larger burst time have poor response time.

Example-01:
Consider the set of 5 processes whose arrival time and burst time are given below-
Process Id Arrival time Burst time
P1 3 1
P2 1 4
P3 4 2
P4 0 6
P5 2 3
Solution-
If the CPU scheduling policy is SJF non-preemptive, calculate the average waiting time and

Dr. Ashish
43
Unit-1
UNi

average turnaround time.


Gantt Chart-

Now, we know-
Turn Around time = Exit time – Arrival time
Waiting time = Turn Around time – Burst time

Process Id Exit time Turn Around time Waiting time


P1 7 7–3=4 4–1=3
P2 16 16 – 1 = 15 15 – 4 = 11
P3 9 9–4=5 5–2=3
P4 6 6–0=6 6–6=0
P5 12 12 – 2 = 10 10 – 3 = 7
Now,
Average Turn Around time = (4 + 15 + 5 + 6 + 10) / 5 = 40 / 5 = 8 unit
Average waiting time = (3 + 11 + 3 + 0 + 7) / 5 = 24 / 5 = 4.8 unit

Example-02:
Consider the set of 5 processes whose arrival time and burst time are given below-
Process Id Arrival time Burst time
P1 3 1
P2 1 4
P3 4 2
P4 0 6
P5 2 3
If the CPU scheduling policy is SJF pre-emptive, calculate the average waiting time and average
turnaround time.
Solution-

Dr. Ashish
44
Unit-1
UNi

Gantt Chart-

Process Id Exit time Turn Around time Waiting time


P1 4 4–3=1 1–1=0
P2 6 6–1=5 5–4=1
P3 8 8–4=4 4–2=2
P4 16 16 – 0 = 16 16 – 6 = 10
P5 11 11 – 2 = 9 9–3=6

Now,

Average Turn Around time = (1 + 5 + 4 + 16 + 9) / 5 = 35 / 5 = 7 unit


Average waiting time = (0 + 1 + 2 + 10 + 6) / 5 = 19 / 5 = 3.8 unit

Dr. Ashish
45
Unit-1
UNi

10.3 SRTF

Dr. Ashish
46
Unit-1
UNi

Dr. Ashish
47
Unit-1
UNi

Example-03:

Consider the set of 6 processes whose arrival time and burst time are given below-

Process Id Arrival time Burst time


P1 0 7
P2 1 5
P3 2 3
P4 3 1
P5 4 2
P6 5 1

If the CPU scheduling policy is shortest remaining time first, calculate the average
waiting time and average turnaround time.
Solution-
Gantt Chart-

Now, we know-
Turn Around time = Exit time – Arrival time
Waiting time = Turn Around time – Burst time

Process Id Exit time Turn Around time Waiting time


P1 19 19 – 0 = 19 19 – 7 = 12
P2 13 13 – 1 = 12 12 – 5 = 7
P3 6 6–2=4 4–3=1
P4 4 4–3=1 1–1=0
P5 9 9–4=5 5–2=3
P6 7 7–5=2 2–1=1
Now,
Average Turn Around time = (19 + 12 + 4 + 1 + 5 + 2) / 6 = 43 / 6 = 7.17 unit
Average waiting time = (12 + 7 + 1 + 0 + 3 + 1) / 6 = 24 / 6 = 4 unit

Dr. Ashish
48
Unit-1
UNi

Example -04:

Consider the set of 3 processes whose arrival time and burst time are given below-

Process Id Arrival time Burst time


P1 0 9
P2 1 4
P3 2 9

If the CPU scheduling policy is SRTF, calculate the average waiting time and average turn
around time.

Solution- Gantt
Chart-

Now, we know-
Turn Around time = Exit time – Arrival time
Waiting time = Turn Around time – Burst time

Process Id Exit time Turn Around time Waiting time


P1 13 13 – 0 = 13 13 – 9 = 4
P2 5 5–1=4 4–4=0
P3 22 22- 2 = 20 20 – 9 = 11

Now,
Average Turn Around time = (13 + 4 + 20) / 3 = 37 / 3 = 12.33 unit
Average waiting time = (4 + 0 + 11) / 3 = 15 / 3 = 5 unit

Dr. Ashish
49
Unit-1
UNi

Example-05:

Consider the set of 4 processes whose arrival time and burst time are given below-

Process Id Arrival time Burst time


P1 0 20
P2 15 25
P3 30 10
P4 45 15

If the CPU scheduling policy is SRTF, calculate the waiting time of process P2.

Solution-

Gantt Chart-

Now, we know-
Turn Around time = Exit time – Arrival time
Waiting time = Turn Around time – Burst time

Thus,
Turn Around Time of process P2 = 55 – 15 = 40 unit
Waiting time of process P2 = 40 – 25 = 15 unit

Dr. Ashish
50
Unit-1
UNi

10.4 Round Robin Scheduling


CPU is assigned to the process on the basis of FCFS for a fixed amount of time.
This fixed amount of time is called as time quantum or time slice.
After the time quantum expires, the running process is preempted and sent to the ready
queue.
Then, the processor is assigned to the next arrived process.
It is always preemptive in nature.

Advantages-

It gives the best performance in terms of average response time.


It is best suited for time sharing system, client server architecture and
interactive system.

Disadvantages-

• It leads to starvation for processes with larger burst time as they have to repeat the cycle
many times.
• Its performance heavily depends on time quantum.
• Priorities can not be set for the processes.
With decreasing value of time quantum,
• Number of context switch increases
• Response time decreases

Dr. Ashish
51
Unit-1
UNi

• Chances of starvation decreases


Thus, smaller value of time quantum is better in terms of response time.
With increasing value of time quantum,
• Number of context switch decreases
• Response time increases
• Chances of starvation increases
Thus, higher value of time quantum is better in terms of number of context switch.
• With increasing value of time quantum, Round Robin Scheduling tends to become
FCFS Scheduling.
• When time quantum tends to infinity, Round Robin Scheduling becomes FCFS Scheduling.
• The performance of Round Robin scheduling heavily depends on the value of time
quantum.
• The value of time quantum should be such that it is neither too big nor too small.

Example-01:
Consider the set of 5 processes whose arrival time and burst time are given below-

Process Id Arrival time Burst time


P1 0 5
P2 1 3
P3 2 1
P4 3 2
P5 4 3

If the CPU scheduling policy is Round Robin with time quantum = 2 unit, calculate the
average waiting time and average turnaround time.
Solution-
Ready Queue- P5, P1, P2, P5, P4, P1, P3, P2, P1
Gantt Chart-

Now, we know-
Turn Around time = Exit time – Arrival time
Waiting time = Turn Around time – Burst time

Dr. Ashish
52
Unit-1
UNi

Process Id Exit time Turn Around time Waiting time


P1 13 13 – 0 = 13 13 – 5 = 8
P2 12 12 – 1 = 11 11 – 3 = 8
P3 5 5–2=3 3–1=2
P4 9 9–3=6 6–2=4
P5 14 14 – 4 = 10 10 – 3 = 7
Now,
Average Turn Around time = (13 + 11 + 3 + 6 + 10) / 5 = 43 / 5 = 8.6 unit
Average waiting time = (8 + 8 + 2 + 4 + 7) / 5 = 29 / 5 = 5.8 unit

Problem-02:
Consider the set of 6 processes whose arrival time and burst time are given below-
Process Id Arrival time Burst time

P1 0 4

P2 1 5

P3 2 2

P4 3 1

P5 4 6

P6 6 3

If the CPU scheduling policy is Round Robin with time quantum = 2, calculate the average
waiting time and average turnaround time.
Solution-
Ready Queue- P5, P6, P2, P5, P6, P2, P5, P4, P1, P3, P2, P1
Gantt chart-

Now, we know-
Turn Around time = Exit time – Arrival time
Waiting time = Turn Around time – Burst time

Dr. Ashish
53
Unit-1
UNi

Process Id Exit time Turn Around time Waiting time


P1 8 8–0=8 8–4=4
P2 18 18 – 1 = 17 17 – 5 = 12
P3 6 6–2=4 4–2=2
P4 9 9–3=6 6–1=5
P5 21 21 – 4 = 17 17 – 6 = 11
P6 19 19 – 6 = 13 13 – 3 = 10
Now,
Average Turn Around time = (8 + 17 + 4 + 6 + 17 + 13) / 6 = 65 / 6 = 10.84 unit
Average waiting time = (4 + 12 + 2 + 5 + 11 + 10) / 6 = 44 / 6 = 7.33 unit

54
Dr. Ashish
Unit-1
UNi

Problem-03: Consider the set of 6 processes whose arrival time and burst time are given
below-

Process Id Arrival time Burst time


P1 5 5
P2 4 6
P3 3 7
P4 1 9
P5 2 2
P6 6 3
If the CPU scheduling policy is Round Robin with time quantum = 3, calculate the average

55
Dr. Ashish
Unit-1
UNi

waiting time and average turnaround time.


Solution-
Ready Queue- P3, P1, P4, P2, P3, P6, P1, P4, P2, P3, P5, P4
Gantt chart-

Now, we know-
Turn Around time = Exit time – Arrival time
Waiting time = Turn Around time – Burst time
Process Id Exit time Turn Around time Waiting time
P1 32 32 – 5 = 27 27 – 5 = 22
P2 27 27 – 4 = 23 23 – 6 = 17
P3 33 33 – 3 = 30 30 – 7 = 23
P4 30 30 – 1 = 29 29 – 9 = 20
P5 6 6–2=4 4–2=2
P6 21 21 – 6 = 15 15 – 3 = 12

Now,

Average Turn Around time = (27 + 23 + 30 + 29 + 4 + 15) / 6 = 128 / 6 = 21.33 unit


Average waiting time = (22 + 17 + 23 + 20 + 2 + 12) / 6 = 96 / 6 = 16 unit

56
Dr. Ashish
Unit-1
UNi

10.5 Priority Scheduling


• Out of all the available processes, CPU is assigned to the process having the

highest priority.
• In case of a tie, it is broken by FCFS Scheduling.
• Priority Scheduling can be used in both preemptive and non-preemptive mode.

• The waiting time for the process having the highest priority will always be zero
in preemptive mode.
• The waiting time for the process having the highest priority may not be zero in
non- preemptive mode.
Priority scheduling in preemptive and non-preemptive mode behaves exactly same under
following conditions-
• The arrival time of all the processes is same
• All the processes become available
Advantages-
• It considers the priority of the processes and allows the important processes to run
first.
• Priority scheduling in pre-emptive mode is best suited for real time operating
system.
Disadvantages-
• Processes with lesser priority may starve for CPU.
• There is no idea of response time and waiting time.

57
Dr. Ashish
Unit-1
UNi

58
Dr. Ashish
Unit-1
UNi

59
Dr. Ashish
Unit-1
UNi

Problem-01:
Consider the set of 5 processes whose arrival time and burst time are given below-
Process Id Arrival time Burst time Priority

P1 0 4 2

P2 1 3 3

P3 2 1 4

P4 3 5 5

P5 4 2 5

If the CPU scheduling policy is priority non-preemptive, calculate the average waiting time
and average turnaround time. (Higher number represents higher priority)

Solution-

Gantt
Chart-
Now, we know-
• Turn Around time = Exit time – Arrival time
• Waiting time = Turn Around time – Burst time

Process Id Exit time Turn Around time Waiting time


P1 4 4–0=4 4–4=0
P2 15 15 – 1 = 14 14 – 3 = 11
P3 12 12 – 2 = 10 10 – 1 = 9
P4 9 9–3=6 6–5=1
P5 11 11 – 4 = 7 7–2=5
Now,
• Average Turn Around time = (4 + 14 + 10 + 6 + 7) / 5 = 41 / 5 = 8.2 unit
• Average waiting time = (0 + 11 + 9 + 1 + 5) / 5 = 26 / 5 = 5.2 unit

Problem-02: Consider the set of 5 processes whose arrival time and burst time are given
below-

60
Dr. Ashish
Unit-1
UNi

Process Id Arrival time Burst time Priority


P1 0 4 2
P2 1 3 3
P3 2 1 4
P4 3 5 5
P5 4 2 5
If the CPU scheduling policy is priority preemptive, calculate the average waiting time
and average turn around time. (Higher number represents higher priority). Solution-
Gantt Chart-

Now, we know-
• Turn Around time = Exit time – Arrival time
• Waiting time = Turn Around time – Burst time
Process Id Exit time Turn Around time Waiting time
P1 15 15 – 0 = 15 15 – 4 = 11
P2 12 12 – 1 = 11 11 – 3 = 8
P3 3 3–2=1 1–1=0
P4 8 8–3=5 5–5=0
P5 10 10 – 4 = 6 6–2=4

Now,
• Average Turn Around time = (15 + 11 + 1 + 5 + 6) / 5 = 38 / 5 = 7.6 unit
• Average waiting time = (11 + 8 + 0 + 0 + 4) / 5 = 23 / 5 = 4.6 unit

10.6 Multilevel Queue Scheduling


A multi-level queue scheduling algorithm partitions the ready queue into several separate
queues. The processes are permanently assigned to one queue, generally based on some
property of the process, such as memory size, process priority, or process type. Each queue has
its own scheduling algorithm.
Let us consider an example of a multilevel queue-scheduling algorithm with five queues:
1. System Processes
2. Interactive Processes

61
Dr. Ashish
Unit-1
UNi

3. Interactive Editing Processes


4. Batch Processes
5. Student Processes
Each queue has absolute priority over lower-priority queues. No process in the batch queue,
for example, could run unless the queues for system processes, interactive processes, and
interactive editing processes were all empty. If an interactive editing process entered the ready
queue while a batch process was running, the batch process will be pre-empted.

References

• https://byjus.com/gate/process-state-in-operating-system-notes/
• https://www.tutorialspoint.com/operating_system/os_process_scheduling.htm
• https://www.geeksforgeeks.org/context-switch-in-operating-system/
• https://www.scaler.com/topics/scheduling-criteria-in-os/

62
Dr. Ashish
Unit-1
UNi

UNIT-II
Process Synchronization
and
Memory Organization & Management

UNIT-II

Process Synchronization: Mutual exclusion, software solution to Mutual exclusion problem,


hardware solution to Mutual exclusion problem, semaphores, Critical section problems. Case
study on Dining philosopher problem, Barber shop problem etc.

Memory Organization & Management: Memory Organization, Memory Hierarchy,


Memory Management Strategies, Contiguous versus non- Contiguous memory allocation,
Partition Management Techniques, Logical versus Physical Address space, swapping, Paging,
Segmentation, Segmentation with Paging Virtual Memory: Demand Paging, Page
Replacement, Page-replacement Algorithms, Performance of Demand Paging, Thrashing,
Demand Segmentation, and Overlay Concepts.

63
Dr. Ashish
• Mutual Exclusion
Mutual Exclusion is a property of process synchronization that states that “no two processes
can exist in the critical section at any given point of time“. The term was first coined
by Dijkstra. Any process synchronization technique being used must satisfy the property of
mutual exclusion, without which it would not be possible to get rid of a race condition.

The need for mutual exclusion comes with concurrency. There are several kinds of concurrent
execution:

• Interrupt handlers

• Interleaved, preemptively scheduled processes/threads

• Multiprocessor clusters, with shared memory

• Distributed systems

o Conditions Required for Mutual Exclusion


According to the following four criteria, mutual exclusion is applicable:

• When using shared resources, it is important to ensure mutual exclusion between various
processes. There cannot be two processes running simultaneously in either of their critical
sections.

• It is not advisable to make assumptions about the relative speeds of the unstable processes.

• For access to the critical section, a process that is outside of it must not obstruct another
process.

• Its critical section must be accessible by multiple processes in a finite amount of time;
multiple processes should never be kept waiting in an infinite loop.

Approaches To Implementing Mutual Exclusion


o
• Software Method: Leave the responsibility to the processes themselves. These methods
are usually highly error-prone and carry high overheads.

• Hardware Method: Special-purpose machine instructions are used for accessing shared
resources. This method is faster but cannot provide a complete solution. Hardware
solutions cannot give guarantee the absence of deadlock and starvation.

• Programming Language Method: Provide support through the operating system or


through the programming language.

64
Unit-2
UNi

o Requirements of Mutual Exclusion


• At any time, only one process is allowed
to enter its critical section.
• The solution is implemented purely in
software on a machine.
• A process remains inside its critical
section for a bounded time only.
• No assumption can be made about the
relative speeds of asynchronous
concurrent processes.
• A process cannot prevent any other process from entering into a critical section.
• A process must not be indefinitely postponed from entering its 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. Although there are some properties that should be followed if any code
in the critical section

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

65
Dr. Ashish
Unit-2
UNi

o 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. If no process is executing in its critical section
and some processes wish to enter their critical sections, then only those processes that are not
executing in their remainder sections can participate in deciding which will enter its critical
section next, and this selection cannot be postponed indefinitely.

o Bounded Waiting
There exists a bound, or limit, on the number of times that other processes are allowed to
enter their critical sections after a process has made a request to enter its critical section and
before that request is granted.
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.

66
Dr. Ashish
Unit-2
UNi

Software based solution


Critical Section Problem: Algorithm 1 (Strict Alteration or Deckers algo)

Algorithm 1
• Satisfies mutual exclusion
The turn is equal to either i or j and hence one of Pi
and Pj can enter the critical section
• Does not satisfy progress
Example: Pi finishes the critical section and then gets
stuck indefinitely in its remainder section. Then Pj
enters the critical section, finishes, and then finishes
its remainder section. Pj then tries to enter the critical
section again, but it cannot since turn was set to i by
Pj in the previous iteration. Since Pi is stuck in the
remainder section, turn will be equal to i indefinitely
and Pj can’t enter although it wants to. Hence no
process is in the critical section and hence no
progress.

• We don’t need to discuss/consider bounded wait when progress is not satisfied

67
Dr. Ashish
Unit-2
UNi

Critical Section Problem: Algorithm 2

68
Dr. Ashish
Unit-2
UNi
3. Peterson’s Solution
While(1)
{
flag[0] = true;
turn = 1;
while (flag[1] ==True && turn == 1);
P0
Critical Section
flag[0] = false;
}

While(1)
{
flag[1] = true;
turn = 0; /* Change to Both waiting, turn=0 */
P1
while (flag[0]==True && turn == 0);

Critical Section

flag[1] = false;

}
Explanation of Peterson's Algorithm

The algorithm utilizes two main variables, which are a turn and a flag. turn is an integer variable that indicates whose turn it
is to enter the critical section. flag is an array of Boolean variables. Each element in the array represents the intention of a
process to enter the critical section. Let us look at the explanation of how Peterson's algorithm in OS works:

• Firstly, both processes set their flag variables to indicate that they don't currently want to enter the critical section. By
default, the turn variable is set to the ID of one of the processes, it can be 0 or 1. This will indicate that it is initially the
turn of that process to enter the critical section.
• Both processes set their flag variable to indicate their intention to enter the critical section.
• Then the process, which is having the next turn, sets the turn variable to its own ID. This will indicate that it is its turn to
enter the critical section. For example, if the current value of turn is 1, and it is now the turn of Process 0 to enter,
Process 0 sets turn = 0.
• Both processes enter a loop where they will check the flag of the other process and wait if necessary. The loop continues
as long as the following two conditions are met:
i. The other process has expressed its intention to enter the critical section (flag[1 - processID] == true for Process
processID).
ii. It is currently the turn of the other process (turn == 1 - processID).
• If both conditions are satisfied, the process waits and yields the CPU to the other process. This ensures that the other
process has an opportunity to enter the critical section.
• Once a process successfully exits the waiting loop, then it can enter the critical section. It can also access the shared
resource without interference from the other process. It can perform any necessary operations or modifications within
this section.
• After completing its work in the critical section, the process resets its flag variable. Resetting is required to indicate that
this process no longer wants to enter the critical section (flag[processID] = false). This step ensures that the process can
enter the waiting loop again correctly if needed.

69
Dr. Ashish
Unit-2
UNi

• Hardware Synchronization Algorithms: Unlock and Lock, Test and Set,


Swap
Process Synchronization problems occur when two processes running concurrently share the
same data or same variable. The value of that variable may not be updated correctly before its
being used by a second process. Such a condition is known as Race Around Condition. There
are a software as well as hardware solutions to this problem. There are three algorithms in the
hardware approach of solving Process Synchronization problem:
1. Test and Set
2. Swap
3. Unlock and Lock
Hardware instructions in many operating systems help in the effective solution of critical section
problems.

1. Test and Set:

• Here, the shared variable is lock which is initialized to false.


• TestAndSet(lock) algorithm works in this way – it always returns whatever value is sent
to it and sets lock to true. The first process will enter the critical section at once as
TestAndSet(lock) will return false and it’ll break out of the while loop.
• The other processes cannot enter now as lock is set to true and so the while loop
continues to be true. Mutual exclusion is ensured. Once the first process gets out of the
critical section, lock is changed to false. So, now the other processes can enter one by one.
Progress is also ensured. However, after the first process, any process can go in. There is
no queue maintained, so any new process that finds the lock to be false again can enter.
So bounded waiting is not ensured.

70
Dr. Ashish
Unit-2
UNi

2. Swap:
Swap algorithm is a lot like the TestAndSet algorithm. Instead of directly setting lock to true in
the swap function, key is set to true and then swapped with lock. First process will be executed,
and in while(key), since key=true , swap will take place and hence lock=true and key=false. Again
next iteration takes place while(key) but key=false , so while loop breaks and first process will
enter in critical section. Now another process will try to enter in Critical section, so again key=true
and hence while(key) loop will run and swap takes place so, lock=true and key=true (since
lock=true in first process). Again on next iteration while(key) is true so this will keep on executing
and another process will not be able to enter in critical section. Therefore Mutual exclusion is
ensured. Again, out of the critical section, lock is changed to false, so any process finding it gets
t enter the critical section. Progress is ensured. However, again bounded waiting is not ensured
for the very same reason.

Swap Pseudocode –
// Shared variable lock initialized to false
// and individual key initialized to false;

boolean lock;
Individual key;

71
Dr. Ashish
Unit-2
UNi

void swap(boolean &a, boolean &b){


boolean temp = a;
a = b;
b = temp;
}

while (1){
key = true;
while(key)
swap(lock,key);
critical section
lock = false;
remainder section
}

3. Unlock and Lock :


Unlock and Lock Algorithm uses TestAndSet to regulate the value of lock but it adds another
value, waiting[i], for each process which checks whether or not a process has been waiting. A
ready queue is maintained with respect to the process in the critical section. All the processes
coming in next are added to the ready queue with respect to their process number, not necessarily
sequentially. Once the ith process gets out of the critical section, it does not turn lock to false so
that any process can avail the critical section now, which was the problem with the previous
algorithms. Instead, it checks if there is any process waiting in the queue. The queue is taken to
be a circular queue. j is considered to be the next process in line and the while loop checks from
jth process to the last process and again from 0 to (i-1)th process if there is any process waiting
to access the critical section. If there is no process waiting then the lock value is changed to false
and any process which comes next can enter the critical section. If there is, then that process’
waiting value is turned to false, so that the first while loop becomes false and it can enter the
critical section. This ensures bounded waiting. So the problem of process synchronization can be
solved through this algorithm.

Unlock and Lock Pseudocode –


// Shared variable lock initialized to false
// and individual key initialized to false

boolean lock;

72
Dr. Ashish
Unit-2
UNi

Individual key;
Individual waiting[i];

while(1){
waiting[i] = true;
key = true;
while(waiting[i] && key)
key = TestAndSet(lock);
waiting[i] = false;
critical section
j = (i+1) % n;
while(j != i && !waiting[j])
j = (j+1) % n;
if(j == i)
lock = false;
else
waiting[j] = false;
remainder section
}

• Semaphore

Semaphores are integer variables that are used to solve the critical section problem by using two
atomic operations, wait and signal that are used for process synchronization.

The definitions of wait and signal are as follows −

• Wait –P( )
The wait operation decrements the value of its argument S, if it is positive. If S is negative
or zero, then no operation is performed.
wait(S)
{
while (S<=0);

S--;
}


Signal----V( )
The signal operation increments the value of its argument S.
signal(S)
{
S++;
}
73
Dr. Ashish
Unit-2
UNi

o Types of Semaphores

There are two main types of semaphores i.e. counting semaphores and binary semaphores. Details
about these are given as follows −

• Counting Semaphores
These are integer value semaphores and have an unrestricted value domain. These
semaphores are used to coordinate the resource access, where the semaphore count is the
number of available resources. If the resources are added, semaphore count automatically
incremented and if the resources are removed, the count is decremented.
• Binary Semaphores
The binary semaphores are like counting semaphores but their value is restricted to 0 and
1. The wait operation only works when the semaphore is 1 and the signal operation
succeeds when semaphore is 0. It is sometimes easier to implement binary semaphores
than counting semaphores.

Some points regarding P and V operation:

1. P operation is also called wait, sleep, or down operation, and V operation is also called signal,
wake-up, or up operation.

2. Both operations are atomic and semaphore(s) is always initialized to one. Here atomic means
that variable on which read, modify and update happens at the same time/moment with no
pre-emption i.e. in-between read, modify and update no other operation is performed that may
change the variable.

A critical section is surrounded by both operations to implement process synchronization. See the
below image. The critical section of Process P is in between P and V operation

74
Dr. Ashish
Unit-2
UNi

• Now, let us see how it implements mutual exclusion. Let


there be two processes P1 and P2 and a semaphore s is
initialized as 1.
• Now if suppose P1 enters in its critical section then the value
of semaphore s becomes 0.
• Now if P2 wants to enter its critical section then it will wait
until s > 0, this can only happen when P1 finishes its critical
section and calls V operation on semaphore s.
• This way mutual exclusion is achieved. Look at the below
image for details which is a Binary semaphore

75
Dr. Ashish
Unit-2
UNi

• Classical problems of synchronization


classical problems of synchronization as examples of a large class of concurrency-control
problems. In our solutions to the problems, we use semaphores for synchronization, since that
is the traditional way to present such solutions. However, actual implementations of these
solutions could use mutex locks in plac of binary semaphores.
These problems are used for testing nearly every newly proposed synchronization scheme. The
following problems of synchronization are considered as classical problems:
1. Bounded-buffer (or Producer-Consumer) Problem,
2. Dining-Philosophers Problem,
3. Readers and Writers Problem,
These are summarized, for detailed explanation, you can view the linked articles for each.
o Bounded-buffer (or Producer-Consumer) Problem:
Bounded Buffer problem is also called producer consumer problem. This problem is generalized
in terms of the Producer-Consumer problem. Solution to this problem is, creating two counting
semaphores “full” and “empty” to keep track of the current number of full and empty buffers
respectively. Producers produce a product and consumers consume the product, but both use of
one of the containers each time.
o Dining-Philosophers Problem:
The Dining Philosopher Problem states that K philosophers
seated around a circular table with one chopstick between each
pair of philosophers. There is one chopstick between each
philosopher. A philosopher may eat if he can pickup the two
chopsticks adjacent to him. One chopstick may be picked up
by any one of its adjacent followers but not both. This problem
involves the allocation of limited resources to a group of
processes in a deadlock-free and starvation-free manner.
o Readers and Writers Problem:
Suppose that a database is to be shared among several
concurrent processes. Some of these processes may want only
to read the database, whereas others may want to update (that is, to read and write) the database.
We distinguish between these two types of processes by referring to the former as readers and to
the latter as writers. Precisely in OS we call this situation as the readers-writers problem. Problem
parameters:

• One set of data is shared among a number of processes.


• Once a writer is ready, it performs its write. Only one writer may write at a time.
• If a process is writing, no other process can read it.
• If at least one reader is reading, no other process can write.
• Readers may not write and only read.

76
Dr. Ashish
Unit-2
UNi

• Reader’s writer problem is another example of a classic synchronization


problem

Solution

Variables used –

1. Mutex – mutex (used for mutual exclusion, when readcount is changed)


1. initialised as 1
2. Semaphore – wrt (used by both readers and writers)
1. initialised as 1
3. readers_count – Counter of number of people reading the file
1. initialised as 0

Readers Problem
while(TRUE)
77
Dr. Ashish
Unit-2
UNi

{
//a reader wishes to enter critical section
//this mutex is used for mutual exclusion before readcount is
changed
wait(mutex);

//now since he has entered increase the readers_count by 1


readers_count++;

// readers_count value now should be greater than or equal to


1

// used ==1 not >=1 as we want to perform this only once.


if(readers_count == 1)
// decrementing w value so no writer can enter writer
section
// as readers are reading
// MCQ Question Fact -
// Readers have more priority then writers.
wait(wrt);

//this will ensure that now, other readers can enter critical
section
signal(mutex);

/* perform the reading operation */

// a reader wants to leave after reading process


wait(mutex);
readers_count--;

if(readers_count == 0)
// if readers_count is 0 we must restore w value to 1 so
writers can write
signal(wrt);

// reader has now left


signal(mutex);
}

78
Dr. Ashish
Unit-2
UNi

• Dining philosopher's problem


• The dining philosopher's problem is the classical problem of synchronization which says that
Five philosophers are sitting around a circular table and their job is to think and eat
alternatively.
• A bowl of noodles is placed at the center of the table along with five chopsticks for each of
the philosophers. To eat a philosopher needs both their right and a left chopstick.

• A philosopher can only eat if both immediate left and right chopsticks of the philosopher is
available. In case if both immediate left and right chopsticks of the philosopher are not
available then the philosopher puts down their (either left or right) chopstick and starts
thinking again.
• The dining philosopher demonstrates a large class of concurrency control problems hence it's
a classic synchronization problem.
• When a philosopher thinks, he does not interact with others. When he gets hungry, he tries to
pick up the two chopsticks that are near to him.
• For example, philosopher 1 will try to pick chopsticks 1 and 2. But the philosopher can pickup
only one chopstick at a time. He cannot take a chopstick that is already in the hands of his
neighbour.
• The philosopher stars to eat when he has both his chopsticks in his hand. After eating the
philosopher puts down both the chopsticks and starts to think again.
o Approach to Solution
Here is the simple approach to solving it:

1. We convert each fork as a binary semaphore. That implies there exists an array of
semaphore data types of forks having size = 5. It initially contains 1 at all positions.
2. Now when a philosopher picks up a fork he calls wait() on fork[i] which means that i'th
philosopher has acquired the fork.
3. If a philosopher has done eating he calls release (). That implies fork[i] is released and
any other philosopher can pick up this fork and can start eating.

79
Dr. Ashish
Unit-2
UNi

Here is the code for this approach:

do{
wait(fork[i]);
wait(fork[(i + 1) % 5]);
//eat noodles

signal(fork[i]);
signal(fork[(i + 1) % 5]);
//think

}while(1);

o The drawback of the above solution of the dining philosopher problem

From the above solution of the dining philosopher problem, we have proved that no two
neighboring philosophers can eat at the same point in time. The drawback of the above solution
is that this solution can lead to a deadlock condition. This situation happens if all the philosophers
pick their left chopstick at the same time, which leads to the condition of deadlock and none of
the philosophers can eat.

To avoid deadlock, some of the solutions are as follows -

o Maximum number of philosophers on the table should not be more than four, in this case,
chopstick C4 will be available for philosopher P3, so P3 will start eating and after the finish
of his eating procedure, he will put down his both the chopstick C3 and C4, i.e. semaphore
C3 and C4 will now be incremented to 1. Now philosopher P2 which was holding chopstick
C2 will also have chopstick C3 available, hence similarly, he will put down his chopstick
after eating and enable other philosophers to eat.
o A philosopher at an even position should pick the right chopstick and then the left chopstick
while a philosopher at an odd position should pick the left chopstick and then the right
chopstick.
o Only in case if both the chopsticks ( left and right ) are available at the same time, only then
a philosopher should be allowed to pick their chopsticks
o All the four starting philosophers ( P0, P1, P2, and P3) should pick the left chopstick and then
the right chopstick, whereas the last philosopher P4 should pick the right chopstick and then
the left chopstick. This will force P4 to hold his right chopstick first since the right chopstick
of P4 is C0, which is already held by philosopher P0 and its value is set to 0, i.e C0 is already
0, because of which P4 will get trapped into an infinite loop and chopstick C4 remains vacant.
Hence philosopher P3 has both left C3 and right C4 chopstick available, therefore it will start
eating and will put down its both chopsticks once finishes and let others eat which removes
the problem of deadlock.

80
Dr. Ashish
Unit-2
UNi

• Producer consumer problem solution using semaphore


In the producer-consumer problem, we use three semaphore variables:

1. Semaphore S: This semaphore variable is used to achieve mutual exclusion between


processes. By using this variable, either Producer or Consumer will be allowed to use
or access the shared buffer at a particular time. This variable is set to 1 initially.
2. Semaphore E: This semaphore variable is used to define the empty space in the buffer.
Initially, it is set to the whole space of the buffer i.e. "n" because the buffer is initially
empty.
3. Semaphore F: This semaphore variable is used to define the space that is filled by the
producer. Initially, it is set to "0" because there is no space filled by the producer
initially.

void producer() { void consumer() {


while(T) { while(T) {
produce() wait(F)
wait(E) wait(S)
wait(S) take()
append() signal(S)
signal(S) signal(E)
signal(F) use()
} }
} }

81
Dr. Ashish
Unit-2
UNi

• What is Inter process Communication?


A process can be of two types:

• Independent process.

• Co-operating process.

An independent process is not affected by the execution of other processes while a co-operating
process can be affected by other executing processes. Though one can think that those processes,
which are running independently, will execute very efficiently, in reality, there are many situations
when co-operative nature can be utilized for increasing computational speed, convenience, and
modularity. Inter-process communication (IPC) is a mechanism that allows processes to
communicate with each other and synchronize their actions. The communication between these
processes can be seen as a method of co-operation between them. Inter process communication
(IPC) is a process that allows different processes of a computer system to share information. IPC
lets different programs run in parallel, share data, and communicate with each other. It’s important
for two reasons: First, it speeds up the execution of tasks, and secondly, it ensures that the tasks
run correctly and in the order that they were executed.

Processes can communicate with each other through both:

3. Shared Memory

4. Message passing

• Communication between processes using shared memory requires processes to share some
variable, and it completely depends on how the programmer will implement it.
• Suppose process1 and process2 are executing simultaneously, and they share some resources or use
some information from another process. Process1 generates information about certain computations or
resources being used and keeps it as a record in shared memory.
• When process2 needs to use the shared information, it will check in the record stored in shared memory
and take note of the information generated by process1 and act accordingly. Processes can use shared
memory for extracting information as a record from another process as well as for delivering any
specific information to other processes.

82
Dr. Ashish
Unit-2
UNi

ii) Messaging Passing Method


Now, We will start our discussion of the communication between processes via message passing.
In this method, processes communicate with each other without using any kind of shared memory.
If two processes p1 and p2 want to communicate with each other, they proceed as follows:

• Establish a communication link (if a link already exists, no need to establish it again.)
• Start exchanging messages using basic primitives.
We need at least two primitives:
– send(message, destination) or send(message)
– receive(message, host) or receive(message)

The message size can be of fixed size or of variable size. If it is of fixed size, it is easy for an
OS designer but complicated for a programmer and if it is of variable size then it is easy for a
programmer but complicated for the OS designer. A standard message can have two
parts: header and body.
The header part is used for storing message type, destination id, source id, message length,
and control information. The control information contains information like what to do if runs
out of buffer space, sequence number, priority. Generally, message is sent using FIFO style.

Message Passing through Communication Link. Direct and Indirect Communication link
Direct Communication links are implemented when the processes use a specific process
identifier for the communication, but it is hard to identify the sender ahead of time.
For example the print server.
In-direct Communication is done via a shared mailbox (port), which consists of a queue of
messages. The sender keeps the message in mailbox and the receiver picks them up.

83
Dr. Ashish
Unit-2
UNi

• Sleeping Barber Problem in OS

• The Sleeping Barber problem is a classic


illustration of synchronization challenges
in concurrent systems, often used to
demonstrate process synchronization in
operating systems.
• Dijkstra introduced the sleeping barber
problem in 1965. This problem is based on a hypothetical scenario where there is a
barbershop with one barber.
• The barbershop is divided into two rooms, the waiting room, and the workroom. The
waiting room has n chairs for waiting customers, and the workroom only has a barber
chair.
• Now, if there is no customer, then the barber sleeps in his own chair(barber chair).
Whenever a customer arrives, he has to wake up the barber to get his haircut. If there
are multiple customers and the barber is cutting a customer's hair, then the remaining
customers wait in the waiting room with "n" chairs(if there are empty chairs), or they
leave if there are no empty chairs in the waiting room.
• The sleeping barber problem may lead to a race condition. This problem has occurred
because of the actions of both barber and customer.

Problem: The analogy is based upon a hypothetical


barber shop with one barber. There is a barber shop
which has one barber, one barber chair, and n chairs for
waiting for customers if there are any to sit on the chair.
• If there is no customer, then the barber sleeps in
his own chair.
• When a customer arrives, he has to wake up the
barber.
• If there are many customers and the barber is cutting a customer’s hair, then the
remaining customers either wait if there are empty chairs in the waiting room or they
leave if no chairs are empty.

84
Dr. Ashish
Unit-2
UNi

Semaphore Customers = 0;
Semaphore Barber = 0;
Mutex Seats = 1;
int FreeSeats = N;
Barber { Customer {
while(true) { while(true) {
/* waits for a customer (sleeps). * /* protects seats so only 1 cu
/ stomer tries to sit
down(Customers); in a chair if that's the case.*/

/* mutex to protect the number down(Seats); //This line sh


of available seats.*/ ould not be here.
down(Seats); if(FreeSeats > 0)
{
/* a chair gets free.*/
FreeSeats++; /* sitting down.*/
FreeSeats--;
/* bring customer for hairc
ut.*/ /* notify the barber. */
up(Barber); up(Customers);

/* release the mutex on th /* release the lock */


e chair.*/ up(Seats);
up(Seats);
/* barber is cutting hair.*/ /* wait in the waiting roo
} m if barber is busy. */
} down(Barber);
// customer is having hair
cut
} else
{
/* release the lock */
up(Seats);
// customer leaves
}
}
}

Read More
https://www.geeksforgeeks.org/inter-process-communication-
ipc/#:~:text=Inter%2Dprocess%20communication%20(IPC),Shared%20Memory

85
Dr. Ashish
Unit-2
UNi

Memory
Management
Memory Organization & Management: Memory Organization, Memory
Hierarchy, Memory Management Strategies, Contiguous versus non- Contiguous
memory allocation, Partition Management Techniques, Logical versus Physical
Address space, swapping, Paging, Segmentation, Segmentation with Paging
Virtual Memory: Demand Paging, Page Replacement, Page-replacement
Algorithms, Performance of Demand Paging, Thrashing, Demand Segmentation,
and Overlay Concepts.

86
Dr. Ashish
Unit-2
UNi

11 INTRODUCTION
• The term memory can be defined as a collection of data in a specific format. It is used to
store instructions and process data. The memory comprises a large array or group of words
or bytes, each with its own location. The primary purpose of a computer system is to
execute programs.
• These programs, along with the information they access, should be in the main memory
during execution. The CPU fetches instructions from memory according to the value of
the program counter.
• To achieve a degree of multiprogramming and proper utilization of memory, memory
management is important. Many memory management methods exist, reflecting various
approaches, and the effectiveness of each algorithm depends on the situation.
A memory unit is the collection of storage units or devices together. The memory unit stores the
binary information in the form of bits. Generally, memory/storage is classified into 2 categories:

• Volatile Memory: This loses its data, when power is switched off.

• Non-Volatile Memory: This is a permanent storage and does not lose any data when
power is switched off.

87
Dr. Ashish
Unit-2
UNi

11.1 Memory Organization

1. Registers

Registers are small, high-speed memory units located in the CPU. They are used to store the most
frequently used data and instructions. Registers have the fastest access time and the smallest
storage capacity, typically ranging from 16 to 64 bits.

2. Cache Memory

Cache memory is a small, fast memory unit located close to the CPU. It stores frequently used
data and instructions that have been recently accessed from the main memory. Cache memory is
designed to minimize the time it takes to access data by providing the CPU with quick access to
frequently used data.

3. Main Memory

Main memory, also known as RAM (Random Access Memory), is the primary memory of a
computer system. It has a larger storage capacity than cache memory, but it is slower. Main
memory is used to store data and instructions that are currently in use by the CPU.

4. Secondary Storage

Secondary storage, such as hard disk drives (HDD) and solid-state drives (SSD), is a non-volatile
memory unit that has a larger storage capacity than main memory. It is used to store data and
instructions that are not currently in use by the CPU. Secondary storage has the slowest access
time and is typically the least expensive type of memory in the memory hierarchy.

5. Magnetic Disk

Magnetic Disks are simply circular plates that are fabricated with either a metal or a plastic or a
magnetized material. The Magnetic disks work at a high speed inside the computer and these are
frequently used.

6. Magnetic Tape

Magnetic Tape is simply a magnetic recording device that is covered with a plastic film. It is generally
used for the backup of data. In the case of a magnetic tape, the access time for a computer is a little slower
and therefore, it requires some amount of time for accessing the strip.

88
Dr. Ashish
Unit-2
UNi

• What is Memory Management?


In a multiprogramming computer, the Operating System resides in a part of memory, and the rest
is used by multiple processes. The task of subdividing the memory among different processes is
called Memory Management. Memory management is a method in the operating system to
manage operations between main memory and disk during process execution. The main aim of
memory management is to achieve efficient utilization of memory.

• Why Memory Management is Required?


• Allocate and de-allocate memory before and after process execution.
• To keep track of used memory space by processes.
• To minimize fragmentation issues.
• To proper utilization of main memory.
• To maintain data integrity while executing of process.
11.2 Memory management has three goals:
• Protection – Protect users from each other and Operating System from users.
• Sharing – Share data among co-operating processes
• Efficiency – We always desire efficient use of a computer.

To understand memory management, the difference between a physical (or absolute) address
and a logical (or virtual) address must be identified.

Physical Address – An address loaded into the memory address register. That is, the memory
placed on the physical address bus to retrieve or store memory.

Logical Address – An address generated by the CPU (before being placed in the memory address
register during execution).
11.3 Concept of Logical Address Space and Physical Address Space

• Logical Address Space: An address generated by the CPU is known as a “Logical


Address”. It is also known as a Virtual address. Logical address space can be defined as
the size of the process. A logical address can be changed.

• Physical Address Space: An address seen by the memory unit (i.e the one loaded into
the memory address register of the memory) is commonly known as a “Physical Address”.
A Physical address is also known as a Real address. The set of all physical addresses
corresponding to these logical addresses is known as Physical address space. A physical

89
Dr. Ashish
Unit-2
UNi

address is computed by MMU. The run-time mapping from virtual to physical addresses
is done by a hardware device Memory Management Unit (MMU). The physical address
always remains constant.

The MMU (Memory Management Unit) is the hardware which changes logical addresses to
physical addresses.

90
Dr. Ashish
Unit-2
UNi

The actual binding of an address, that is,


the translation of a logical address to the
physical (absolute) address can take place
various stages between a program’s
creation and its execution.

11.4 Address Binding in Operating


systems

In operating systems, there are three


primary types of address binding in OS:
compile-time binding, load-time binding,
and runtime binding. Each type differs in
when and how the association between
symbolic addresses and physical memory
addresses occurs. Let’s explore each type
in detail:

11.4.1 Compile-Time Binding (Static


Binding):
Compile-time binding, also known as static binding, associates symbolic addresses with physical
memory addresses during the compilation phase of a program. The addresses are determined and
fixed before the program is executed. This type of binding is commonly used for global variables
and functions that have a fixed memory location throughout the program’s execution. The
advantage of compile-time binding is its simplicity and efficiency since the addresses are known
in advance. However, it lacks flexibility as the addresses remain constant throughout program
execution, which limits adaptability to runtime changes.

11.4.2 Load-Time Binding:


Load-time binding defers the address binding process until the program is loaded into memory
for execution. During the loading phase, the linker and loader allocate memory addresses for
variables and functions based on their requirements and the availability of memory. The linker
resolves external references and updates the symbolic addresses with actual physical addresses.
Load-time binding provides more flexibility compared to compile-time binding since the
addresses can be adjusted based on the specific runtime conditions. It allows the program to adapt
to changes in memory availability and supports dynamic libraries.

91
Dr. Ashish
Unit-2
UNi

11.4.3 Runtime Binding (Dynamic Binding):


Runtime binding, also known as dynamic binding, performs the address binding process during
program execution. This type of binding allows for greater flexibility as memory addresses can
be dynamically allocated and deallocated as needed. Runtime binding is often used in dynamic
and object-oriented programming languages where the memory layout can change during
program execution. In runtime binding, the program resolves symbolic addresses at runtime based
on the current state of the program. This enables dynamic memory allocation, polymorphism, and
late binding of functions, enhancing the flexibility and adaptability of the program.

The choice of address binding type depends on factors such as program complexity, memory
requirements, and the desired level of flexibility. While compile-time binding offers simplicity
and efficiency, load-time binding and runtime binding provide more dynamic memory
management capabilities. The appropriate type of address binding is determined based on the
specific needs of the program and the operating system environment.

92
Dr. Ashish
Unit-2
UNi

• Memory Management Techniques:

The memory management techniques can be classified into following main


categories:

o Contiguous memory management schemes


o Non-Contiguous memory management schemes

11.5 Contiguous memory management schemes:


In a Contiguous memory management scheme, each program occupies a single contiguous block
of storage locations, i.e., a set of memory locations with consecutive addresses.

Single contiguous memory management schemes:



The Single contiguous memory management scheme is the simplest memory management
scheme used in the earliest generation of computer systems. In this scheme, the main memory is
divided into two contiguous areas or partitions. The operating systems reside permanently in one
partition, generally at the lower memory, and the user process is loaded into the other partition.

Advantages of Single contiguous memory management schemes:

o Simple to implement.

o Easy to manage and design.

o In a Single contiguous memory management scheme, once a process is loaded, it is given


full processor's time, and no other processor will interrupt it.

93
Dr. Ashish
Unit-2
UNi

Disadvantages of Single contiguous memory management schemes:

o Wastage of memory space due to unused memory as the process is unlikely to use all the
available memory space.

o The CPU remains idle, waiting for the disk to load the binary image into the main memory.

o It cannot be executed if the program is too large to fit the entire available main memory
space.

o It does not support multiprogramming, i.e., it cannot handle multiple programs


simultaneously.

11.6 Multiple Partitioning

• The single Contiguous memory management scheme is inefficient as it limits computers


to execute only one program at a time resulting in wastage in memory space and CPU
time.
• The problem of inefficient CPU use can be overcome using multiprogramming that allows
more than one program to run concurrently. To switch between two processes, the
operating systems need to load both processes into the main memory.
• The operating system needs to divide the available main memory into multiple parts to
load multiple processes into the main memory.
• Thus multiple processes can reside in the main memory simultaneously.

94
Dr. Ashish
Unit-2
UNi

1. FIXED PARTITIONING

In this technique, the main memory is divided into partitions of equal or different sizes. The operating
system always resides in the first partition while the other partitions can be used tostore user processes.
The memory is assigned to the processes in contiguous way.

In fixed partitioning,

1. The partitions cannot overlap.


2. A process must be contiguously present in a partition for the execution.

There are various cons of using this technique.

1. Internal Fragmentation

If the size of the process is lesser then the total size of the partition then some size of the partitionget
wasted and remain unused. This is wastage of the memory and called internal fragmentation.

As shown in the image below, the 4 MB partition is used to load only 3 MB process and theremaining
1 MB got wasted.

2. External Fragmentation

The total unused space of various partitions cannot be used to load the processes even though
there is space available but not in the contiguous form.

As shown in the image below, the remaining 1 MB space of each partition cannot be used as a unit
to store a 4 MB process. Despite of the fact that the sufficient space is available to load the process,
process will not be loaded.

3. Limitation on the size of the process

If the process size is larger than the size of maximum sized partition then that process cannot be
loaded into the memory. Therefore, a limitation can be imposed on the process size that is it cannot
be larger than the size of the largest partition.

4. Degree of multiprogramming is less

By Degree of multi programming, we simply mean the maximum number of processes that canbe
loaded into the memory at the same time. In fixed partitioning, the degree of multiprogramming is
fixed and very less due to the fact that the size of the partition cannot be varied according to the size
of processes.

95
Dr. Ashish
Unit-2
UNi

2. DYNAMIC PARTITIONING/VARIABLE PARTITIONING

Dynamic partitioning tries to overcome the problems caused by fixed partitioning. In this technique,
the partition size is not declared initially. It is declared at the time of process loading.

The first partition is reserved for the operating system. The remaining space is divided into parts. The
size of each partition will be equal to the size of the process. The partition size varies according to the
need of the process so that the internal fragmentation can be avoided.

96
Dr. Ashish
Unit-2
UNi

3. Advantages of Dynamic Partitioning over fixed partitioning

1. No Internal Fragmentation

Given the fact that the partitions in dynamic partitioning are created according to the need of the
process, It is clear that there will not be any internal fragmentation because there will not be any unused
remaining space in the partition.

2. No Limitation on the size of the process

In Fixed partitioning, the process with the size greater than the size of the largest partition could not
be executed due to the lack of sufficient contiguous memory. Here, In Dynamic partitioning,the
process size can't be restricted since the partition size is decided according to the process size.

3. Degree of multiprogramming is dynamic

Due to the absence of internal fragmentation, there will not be any unused space in the partitionhence
more processes can be loaded in the memory at the same time.

Disadvantages of dynamic partitioning

External Fragmentation

Absence of internal fragmentation doesn't mean that there will not be external fragmentation.

Let's consider three processes P1 (1 MB) and P2 (3 MB) and P3 (1 MB) are being loaded in the
respective partitions of the main memory.

After some time P1 and P3 got completed and their assigned space is freed. Now there are twounused
partitions (1 MB and 1 MB) available in the main memory but they cannot be used to load a 2 MB
process in the memory since they are not contiguously located.

The rule says that the process must be contiguously present in the main memory to get executed.We
need to change this rule to avoid external fragmentation.

97
Dr. Ashish
Unit-2
UNi

4. Complex Memory Allocation

In Fixed partitioning, the list of partitions is made once and will never change but in dynamic
partitioning, the allocation and deallocation is very complex since the partition size will be varied
every time when it is assigned to a new process. OS has to keep track of all the partitions.

Due to the fact that the allocation and deallocation are done very frequently in dynamic memory
allocation and the partition size will be changed at each time, it is going to be very difficult for OS to
manage everything.

98
Dr. Ashish
Unit-2
UNi

12 Compaction
• Compaction is a technique to collect all the free memory present in the form of fragments into one large
chunk of free memory, which can be used to run other processes.
• It does that by moving all the processes towards one end of the memory and all the available free space
towards the other end of the memory so that it becomes contiguous.
• It is not always easy to do compaction. Compaction can be done only when the relocation is dynamic and
done at execution time. Compaction can not be done when relocation is static and is performed at load
time or assembly time.
12.1 Purpose of Compaction in Operating System
While allocating memory to process, the operating system often faces a problem when there’s a
sufficient amount of free space within the memory to satisfy the memory demand of a process.
however the process’s memory request can’t be fulfilled because the free memory available is in
a non-contiguous manner, this problem is referred to as external fragmentation. To solve such kinds
of problems compaction technique is used.
12.2 Issues with Compaction
Although the compaction technique is very useful in making memory utilization efficient and reduces
external fragmentation of memory, the problem with it is that a large amount of time is wasted in the
process and during that time the CPU sits idle hence reducing the efficiency of the system.

99
Dr. Ashish
Unit-2
UNi

13 Partition Allocation Methods


A. First Fit
B. Best Fit
C. Worst Fit
D. Next Fit
1. First Fit: In the first fit, the partition is allocated which is the first sufficient block from the top of Main
Memory. It scans memory from the beginning and chooses the first available block that is large enough. Thus it
allocates the first hole that is large enough.

2. Best Fit Allocate the process to the partition which is the first smallest sufficient partition among the free
available partition. It searches the entire list of holes to find the smallest hole whose size is greater than or equal
to the size of the process.

3. Worst Fit Allocate the process to the partition which is the largest sufficient among the freely available
partitions available in the main memory. It is opposite to the best-fit algorithm. It searches the entire list of holes
to find the largest hole and allocate it to process.

100
Dr. Ashish
Unit-2
UNi

4. Next Fit: Next fit is similar to the first fit but it will search for the first sufficient partition from the last
allocation point.
Is Best-Fit really best?
Although best fit minimizes the wastage space, it consumes a lot of processor time for searching the block which
is close to the required size. Also, Best-fit may perform poorer than other algorithms in some cases. For example,
see the below exercise.
Exercise: Consider the requests from processes in given order 300K, 25K, 125K, and 50K. Let there be two
blocks of memory available of size 150K followed by a block size 350K.
Which of the following partition allocation schemes can satisfy the above requests?
A) Best fit but not first fit.
B) First fit but not best fit.
C) Both First fit & Best fit.
D) neither first fit nor best fit.
Solution: Let us try all options.
Best Fit:
300K is allocated from a block of size 350K. 50 is left in the block.
25K is allocated from the remaining 50K block. 25K is left in the block.
125K is allocated from 150 K block. 25K is left in this block also.
50K can’t be allocated even if there is 25K + 25K space available.
First Fit:
300K request is allocated from 350K block, 50K is left out.
25K is allocated from the 150K block, 125K is left out.
Then 125K and 50K are allocated to the remaining left out partitions.
So, the first fit can handle requests.
So option B is the correct choice.

101
Dr. Ashish
Unit-2
UNi

Q. Process requests are given as;

5. 25 K , 50 K , 100 K , 75 K

Determine the algorithm which can optimally satisfy this requirement.

1. First Fit algorithm


2. Best Fit Algorithm
3. Neither of the two
4. Both of them

In the question, there are five partitions in the memory. 3 partitions are having processes inside
them and two partitions are holes.

Our task is to check the algorithm which can satisfy the request optimally.

Using First Fit algorithm

Let's see, how first fit algorithm works on this problem.

1. 25 K requirement

The algorithm scans the list until it gets first hole which should be big enough to satisfy the
request of 25 K. it gets the space in the second partition which is free hence it allocates 25 K out
of 75 K to the process and the remaining 50 K is produced as hole.

102
Dr. Ashish
Unit-2
UNi

2. 50 K requirement

The 50 K requirement can be fulfilled by allocating the third partition which is 50 K in size to
the process. No free space is produced as free space.

3. 100 K requirement

100 K requirement can be fulfilled by using the fifth partition of 175 K size. Out of 175 K, 100 K
will be allocated and remaining 75 K will be there as a hole.

4. 75 K requirement

Since we are having a 75 K free partition hence we can allocate that much space to the process
which is demanding just 75 K space.

103
Dr. Ashish
Unit-2
UNi

Using first fit algorithm, we have fulfilled the entire request optimally and no useless space is
remaining.

Let's see, How Best Fit algorithm performs for the problem.

Using Best Fit Algorithm

6. 25 K requirement

To allocate 25 K space using best fit approach, need to scan the whole list and then we find that a
75 K partition is free and the smallest among all, which can accommodate the need of the
process.

Therefore 25 K out of those 75 K free partition is allocated to the process and the remaining 5o
K is produced as a hole.

7. 50 K requirement

To satisfy this need, we will again scan the whole list and then find the 50 K space is free which
the exact match of the need is. Therefore, it will be allocated for the process.

8. 100 K requirement

100 K need is close enough to the 175 K space. The algorithm scans the whole list and then
allocates 100 K out of 175 K from the 5th free partition.

104
Dr. Ashish
Unit-2
UNi

9. 75 K requirement

75 K requirement will get the space of 75 K from the 6th free partition but the algorithm will
scan the whole list in the process of taking this decision.

By following both of the algorithms, we have noticed that both the algorithms perform similar to
most of the extant in this case.

Both can satisfy the need of the processes but however, the best fit algorithm scans the list again
and again which takes lot of time.

Therefore, if you ask me that which algorithm performs in more optimal way then it will be First
Fit algorithm for sure.

Therefore, the answer in this case is A.

105
Dr. Ashish
Unit-2
UNi

14 Need for Paging


• Let’s consider a process P1 of size 2 MB and the
main memory which is divided into three
partitions. Out of the three partitions, two partitions
are holes of size 1 MB each.
• P1 needs 2 MB space in the main memory to be
loaded. We have two holes of 1 MB each but they
are not contiguous.
• Although, there is 2 MB space available in the
main memory in the form of those holes but that
remains useless until it become contiguous. This is a serious problem to address.
• We need to have some kind of mechanism which can store one process at different locations of the
memory.
• The Idea behind paging is to divide the process in pages so that, we can store them in the memory at
different holes. We will discuss paging with the examples in the next sections.

14.1 Paging with Example

• In Operating Systems, Paging is a storage mechanism used to retrieve processes from the secondary
storage into the main memory in the form of pages.
• The main idea behind the paging is to divide each process in the form of pages. The main memory will
also be divided in the form of frames.
• One page of the process is to be stored in one of the frames of the memory. The pages can be stored at the
different locations of the memory but the priority is always to find the contiguous frames or holes.
• Pages of the process are brought into the main memory only when they are required otherwise they reside
in the secondary storage.
• Different operating system defines different frame sizes. The sizes of each frame must be equal.
Considering the fact that the pages are mapped to the frames in Paging, page size needs to be as same as
frame size.

106
Dr. Ashish
Unit-2
UNi

• Let us consider the main memory size 16 Kb and Frame size is 1 KB therefore the main
memory will be divided into the collection of 16 frames of 1 KB each.
• There are 4 processes in the system that is P1, P2, P3 and P4 of 4 KB each. Each process is
divided into pages of 1 KB each so that one page can be stored in one frame.
• Initially, all the frames are empty therefore pages of the processes will get stored in the
contiguous way.
• Frames, pages and the mapping between the two is shown in the image below.

107
Dr. Ashish
Unit-2
UNi

Memory Management Unit


The purpose of Memory Management Unit (MMU) is to convert the logical address into the physical address. The
logical address is the address generated by the CPU for every page while the physical address is the actual address
of the frame where each page will be stored.
When a page is to be accessed by the CPU by using the logical address, the operating system needs to obtain the
physical address to access that page physically.

The logical address has two parts.


1. Page Number
2. Offset
Memory management unit of OS needs to convert the page number to the frame number.

14.2 PHYSICAL ADDRESS SPACE:-

Physical address space in a system can be defined as the size of the main memory. It is really important to
compare the process size with the physical address space. The process size must be less than the physical address
space.

Physical Address Space = Size of the Main Memory


If, physical address space = 64 KB = 2 ^ 6 KB = 2 ^ 6 X 2 ^ 10 Bytes = 2 ^ 16 bytes Let us consider,
word size = 8 Bytes = 2 ^ 3 Bytes
Hence,
Physical address space (in words) = (2 ^ 16) / (2 ^ 3) = 2 ^ 13 Words

Therefore,
Physical Address = 13 bits
In General,
If, Physical Address Space = N Words
then, Physical Address = log2 N Logical Address Space

Logical address space can be defined as the size of the process. The size of the process should be less enough so
that it can reside in the main memory.
108
Dr. Ashish
Unit-2
UNi

Let's say,

Logical Address Space = 128 MB = (2 ^ 7 X 2 ^ 20) Bytes = 2 ^ 27 Bytes Word size = 4 Bytes = 2 ^ 2 Bytes

Logical Address Space (in words) = (2 ^ 27) / (2 ^ 2) = 2 ^ 25 Words Logical Address = 25 Bits

In general,
If, logical address space = L words Then, Logical Address = Log2L bits

14.3 PAGE TABLE:-

• Page Table is a data structure used by the virtual memory system to store the mapping between logical
addresses and physical addresses.
• Logical addresses are generated by the CPU for the pages of the processes therefore they are generally
used by the processes.
• Physical addresses are the actual frame address of the memory. They are generally used by the hardware
or more specifically by RAM subsystems.

109
Dr. Ashish
Unit-2
UNi

• The CPU always accesses the processes through their logical addresses. However, the main memory
recognizes physical address only.
• In this situation, a unit named as Memory Management Unit comes into the picture. It converts the
page number of the logical address to the frame number of the physical address. The offset remains
same in both the addresses.
• To perform this task, Memory Management unit needs a special kind of mapping which is done by
page table. The page table stores all the Frame numbers corresponding to the page numbers of the page
table.
• In other words, the page table maps the page number to its actual location (frame number) in the
memory.
• In the image given below shows, how the required word of the frame is accessed with the help of offset.

14.4 Mapping from page table to main memory

In operating systems, there is always a requirement of mapping from logical address to the physical address.
However, this process involves various steps which are defined as follows.
14.4.1 Generation of logical address
CPU generates logical address for each page of the process. This contains two parts: page number and offset.
14.4.2 Scaling
To determine the actual page number of the process, CPU stores the page table base in a special register. Each
time the address is generated, the value of the page table base is added to the page number to get the actual
location of the page entry in the table. This process is called scaling.
14.4.3 Generation of physical Address
The frame number of the desired page is determined by its entry in the page table. A physical address is generated
which also contains two parts : frame number and offset. The Offset will be similar to the offset of the logical
address therefore it will be copied from the logical address.
14.4.4 Getting Actual Frame Number
The frame number and the offset from the physical address is mapped to the main memory in order to get the
actual word address.

110
Dr. Ashish
Unit-2
UNi

o Performance of Paging :
Evaluating of paging performance is one of the important tasks. Consider the main memory access time is M and
the page table is stored in the main memory then the evaluating expression for effective memory access time is as
follows.

Effective Memory Access Time (E.M.A.T) = 2M


14.5 Page Table Entry

Along with page frame number, the page table also contains some of the bits representing the extra information
regarding the page.
Let's see what the each bit represents about the page.
1. Caching Disabled
Sometimes, there are differences between the information closest to the CPU and the information closest to the
user. Operating system always wants CPU to access user's data as soon as possible. CPU accesses cache which
can be inaccurate in some of the cases, therefore, OS can disable the cache for the required pages. This bit is set to
1 if the cache is disabled.
2. Referenced
There are variouspage replacement algorithms which will be covered later in this tutorial. This bit is set to 1 if the
page is referred in the last clock cycle otherwise it remains 0.
3. Modified

111
Dr. Ashish
Unit-2
UNi

This bit will be set if the page has been modified otherwise it remains 0.
4. Protection
The protection field represents the protection level which is applied on the page. It can be read only or read &
write or execute. We need to remember that it is not a bit rather it is a field which contains many bits.
5. Present/Absent
In the concept of demand paging, all the pages doesn't need to be present in the main memory Therefore, for all
the pages that are present in the main memory, this bit will be set to 1 and the bit will be 0 for all the pages which
are absent.
If some page is not present in the main memory then it is called page fault.

14.6 Numerical - Size of the page table

However, the part of the process which is being executed by the CPU must be present in the main memory during
that time period. The page table must also be present in the main memory all the time because it has the entry for
all the pages.
1. Logical Address = 24 bits
2. Logical Address space = 2 ^ 24 bytes
3. Let's say, Page size = 4 KB = 2 ^ 12 Bytes
4. Page offset = 12
5. Number of bits in a page = Logical Address - Page Offset = 24 - 12 = 12 bits
6. Number of pages = 2 ^ 12 = 2 X 2 X 10 ^ 10 = 4 KB
7. Let's say, Page table entry = 1 Byte
8. Therefore, the size of the page table = 4 KB X 1 Byte = 4 KB

112
Dr. Ashish
Unit-2
UNi

15 TRANSLATION LOOK ASIDE BUFFER with Paging

In translation look aside buffers, there are tags and keys with the help of which, the mapping is done.

TLB hit is a condition where the desired entry is found in translation look aside buffer. If this happens then the CPU simply
access the actual location in the main memory.

However, if the entry is not found in TLB (TLB miss) then CPU has to access page table in the main memory and then access
the actual frame in the main memory.

113
Dr. Ashish
Unit-2
UNi

• How Virtual Memory Works?


• In modern word, virtual memory has become quite common these days. In this scheme, whenever some
pages needs to be loaded in the main memory for the execution and the memory is not available for those
many pages, then in that case, instead of stopping the pages from entering in the main memory, the OS
search for the RAM area that are least used in the recent times or that are not referenced and copy that into
the secondary memory to make the space for the new pages in the main memory.
• Since all this procedure happens automatically, therefore it makes the computer feel like it is having the
unlimited RAM.

15.1 Demand Paging


Demand Paging is a popular method of virtual memory management. In demand paging, the pages of a process
which are least used, get stored in the secondary memory.
A page is copied to the main memory when its demand is made or page fault occurs. There are various page
replacement algorithms which are used to determine the pages which will be replaced. We will discuss each one
of them later in detail.

• Let us assume 2 processes, P1 and P2, contains 4 pages each. Each page size is 1 KB. The main
memory contains 8 frame of 1 KB each. The OS resides in the first two partitions. In the third
partition, 1st page of P1 is stored and the other frames are also shown as filled with the different pages
of processes in the main memory.
• The page tables of both the pages are 1 KB size each and therefore they can be fit in one frame each.
The page tables of both the processes contain various information that is also shown in the image.
• The CPU contains a register which contains the base address of page table that is 5 in the case of P1
and 7 in the case of P2. This page table base address will be added to the page number of the Logical
address when it comes to accessing the actual corresponding entry.

114
Dr. Ashish
Unit-2
UNi

15.2 Advantages of Virtual Memory

1. The degree of Multiprogramming will be increased.


2. User can run large application with less real RAM.
3. There is no need to buy more memory RAMs.
15.3 Disadvantages of Virtual Memory

1. The system becomes slower since swapping takes time.


2. It takes more time in switching between applications.
3. The user will have the lesser hard disk space for its use.

115
Dr. Ashish
Unit-2
UNi

16 Demand Paging
How does Demand Paging Work?

The demand paging system depends on the page table implementation because the page table helps map logical
memory to physical memory. Bitwise operators are implemented in the page table to indicate that pages are ok or
not (valid or invalid). All valid pages exist in primary memory, and invalid pages exist in secondary memory.
Now all processes go-ahead to access all pages, and then the following things will be happened, such as:
1. Attempt to currently access page.
2. If the page is ok (Valid), then all processing instructions work as normal.
3. If anyone's page is found invalid, then a page fault issue arises.
4. Now memory reference is determined whether a valid reference exists on the auxiliary memory or not. If
not exist, then the process is terminated, otherwise needed pages are paged in.
5. Now disk operations are implemented to fetch the required page into primary memory.

116
Dr. Ashish
Unit-2
UNi

Examples of Demand Paging


Suppose we have to execute a process P having four pages P0, P1, P2, and P3. Currently, in the page table, we
have pages P1 and P3.

1. If the CPU wants to access page P2 of a process P, it will first search the page in the page table.
2. As the page table does not contain this page so it will be a trapor page fault. The control goes to the
operating system as soon as the trap is generated and context switching happens.
3. The OS system will put the process in a waiting/ blocked state. The OS system will now search that page
in the backing store or secondary memory.
4. The OS will then read the page from the backing store and load it to the main memory.
5. Next, the OS system will update the page table entry accordingly.
6. Finally, the control is taken back from the OS, and the execution of the process is resumed.
Hence, the operating system follows these steps whenever a page fault occurs and the required page is brought
into memory.
So whenever a page fault occurs, as shown in all the above steps 2 to 6. This time taken to service the page fault is
called the Page fault service time.
Effective Memory Access Time: When the page fault rate is 'p' while executing any process, then the effective
memory access time is calculated as follows:

Effective Memory Access time = (p)*(s) + (1-p)*(m)

117
Dr. Ashish
Unit-2
UNi

17 Page Replacement Algorithms


The page replacement algorithm decides which memory page is to be replaced. The process of replacement is
sometimes called swap out or write to disk. Page replacement is done when the requested page is not found in the
main memory (page fault).

There are two main aspects of virtual memory, Frame allocation and Page Replacement. It is very important to
have the optimal frame allocation and page replacement algorithm. Frame allocation is all about how many
frames are to be allocated to the process while the page replacement is all about determining the page number
which needs to be replaced in order to make space for the requested page.
Types of Page Replacement Algorithms

There are various page replacement algorithms. Each algorithm has a different method by which the pages can be
replaced.

1. Optimal Page Replacement algorithm → this algorithms replaces the page which will not be
referred for so long in future. Although it can not be practically implementable but it can be used as a
benchmark. Other algorithms are compared to this in terms of optimality.
2. Least recent used (LRU) page replacement algorithm → this algorithm replaces the page which
has not been referred for a long time. This algorithm is just opposite to the optimal page replacement
algorithm. In this, we look at the past instead of staring at future.
3. FIFO → in this algorithm, a queue is maintained. The page which is assigned the frame first will be
replaced first. In other words, the page which resides at the rare end of the queue will be replaced on
the every page fault.

10. Numerical on Optimal, LRU and FIFO

118
Dr. Ashish
Unit-2
UNi

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


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

Number of Page Faults in LRU = 6

FIFO Page Replacement Algorithm

119
Dr. Ashish
Number of Page Faults in FIFO = 6

17.1 BELADY'SANOMALY

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

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

[email protected]
Case 2: Number of frames = 4

Req uest
0 1 5 3 0 1 4 0 1 5 3 4

Frame 4 3 3 3 3 3 3 5 5 5

Fra me 3 5 5 5 5 5 5 1 1 1 1

Fra me 2 1 1 1 1 1 1 0 0 0 0 4
Fra me 1
0 0 0 0 0 0 4 4 4 4 3 3

Miss Mi s s Mi ss Mis s Mis s Hi t Hi t Mis s Mis s Mis s Mis s Mis s Mis s


/Hit

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.

[email protected]
• SEGMENTATION
In Operating Systems, Segmentation is a memory management technique in which,
the memory is divided into the variable size parts. Each part is known as segment
which can be allocated to a process.
The details about each segment are stored in a table called as segment table. Segment
table is stored in one (or many) of the segments.

Segment table contains mainly two information about segment:


1. Base: It is the base address of the segment
2. Limit: It is the length of the segment.
Why Segmentation is required?
• Till now, we were using Paging as our main memory management technique. Paging
is more close to Operating system rather than the User. It divides all the process into
the form of pages regardless of the fact that a process can have some relative parts
of functions which needs to be loaded in the same page.
• Operating system doesn't care about the User's view of the process. It may divide
the same function into different pages and those pages may or may not be loaded at
the same time into the memory. It decreases the efficiency of the system.
• It is better to have segmentation which divides the process into the segments. Each
segment contain same type of functions such as main function can be included in
one segment and the library functions can be included in the other segment,
• Translation of Logical address into physical address by segment table CPU
generates a logical address which contains two parts:
1. Segment Number
2. Offset
The Segment number is mapped to the segment table. The limit of the respective
segment is compared with the offset. If the offset is less than the limit then the address
is valid otherwise it throws an error as the address is invalid.
In the case of valid address, the base address of the segment is added to the offset to
get the physical address of actual word in the main memory.

[email protected]
[email protected]
Advantages of Segmentation
1. No internal fragmentation
2. Average Segment Size is larger than the actual page size.
3. Less overhead
4. It is easier to relocate segments than entire address space.
5. The segment table is of lesser size as compare to the page table in paging.
Disadvantages
1. It can have external fragmentation.
2. it is difficult to allocate contiguous memory to variable sized partition.
3. Costly memory management algorithms.
Paging VS Segmentation
Sr No Paging Segmentation

1 Non-Contiguous memory allocation Non-contiguous memory allocation

2 Paging divides program into fixed size Segmentation divides program into
pages. variable size segments.
3 OS is responsible Compiler is responsible.

4 Paging is faster than segmentation Segmentation is slower than paging

5 Paging is closer to Operating System Segmentation is closer to User

6 It suffers from internal fragmentation It suffers from external fragmentation

7 There is no external fragmentation There is no external fragmentation


8 Logical address is divided into page Logical address is divided into segment
number and page offset number and segment offset

9 Page table is used to maintain the page Segment Table maintains the segment
information. information
10 Page table entry has the frame number Segment table entry has the base address of
and some flag bits to represent details the segment and some protection bits for the
about pages. segments.

[email protected]
18 Paging with segmentation
INTRODUCTION:
• Paged Segmentation and Segmented Paging are two different memory management
techniques that combine the benefits of paging and segmentation.
1. Paged Segmentation is a memory management technique that divides a process’s
address space into segments and then divides each segment into pages. This allows
for a flexible allocation of memory, where each segment can have a different size,
and each page can have a different size within a segment.
2. Segmented Paging, on the other hand, is a memory management technique that
divides the physical memory into pages, and then maps each logical address used
by a process to a physical page. In this approach, segments are used to map virtual
memory addresses to physical memory addresses, rather than dividing the virtual
memory into pages.
3. Both Paged Segmentation and Segmented Paging provide the benefits of paging,
such as improved memory utilization, reduced fragmentation, and increased
performance. They also provide the benefits of segmentation, such as increased
flexibility in memory allocation, improved protection and security, and reduced
overhead in memory management.
o Segmented Paging

A solution to the problem is to use segmentation along with paging


to reduce the size of page table. Traditionally, a program is divided
into four segments, namely code segment, data segment, stack
segment and heap segment.

The memory management unit (MMU) will use the segment table
which will contain the address of page table(base) and limit. The page
table will point to the page frames of the segments in main memory.

[email protected]
Advantages of Segmented Paging
1. The page table size is reduced as pages are present only for data of segments,
hence reducing the memory requirements.
2. Gives a programmers view along with the advantages of paging.
3. Reduces external fragmentation in comparison with segmentation.
4. Since the entire segment need not be swapped out, the swapping out into
virtual memory becomes easier .
Disadvantages of Segmented Paging
1. Internal fragmentation still exists in pages.
2. Extra hardware is required
3. Translation becomes more sequential increasing the memory access time.

[email protected]
4. External fragmentation occurs because of varying sizes of page tables and
varying sizes of segment tables in today’s systems.

o Paged Segmentation
1. In segmented paging, not every process has the same number of segments and the
segment tables can be large in size which will cause external fragmentation due to
the varying segment table sizes. To solve this problem, we use paged
segmentation which requires the segment table to be paged. The logical address
generated by the CPU will now consist of page no #1, segment no, page no #2 and
offset.
2. The page table even with segmented paging can have a lot of invalid pages. Instead
of using multi level paging along with segmented paging, the problem of larger page
table can be solved by directly applying multi level paging instead of segmented
paging.

[email protected]
19 Overlays in Memory Management

One of the main limitations imposed on programmers in the early days of computers
was the size of the computer's memory. If the program was larger than the available
memory, it could not be loaded, severely restricting program size. The main
problem with fixed partitioning is that the size of a process has to be limited by the
maximum size of the partition, which means a process can never span over another.
The obvious solution would be to increase the amount of memory available, but this
would significantly increase the cost of the computer system. To solve this problem,
earlier people have used some solution called Overlays.
The concept of overlays is that it will not use the complete program simultaneously
whenever a process is running. It will use only some part of it. Then overlays
concept says that whatever part you require, you load it, and once the part is
done, you just unload it, which means pull it back and get the new part you
required and run it.
Formally, "The process of transferring a block of program code or other data into
internal memory, replacing what is already stored".
Sometimes it happens that compared to the size of the biggest partition, the size
of the program will be even more. Then, in that case, you should go with

overlays.

[email protected]
So overlay is a technique to run a program that is bigger than the size of the physical
memory by keeping only those instructions and data that are needed at any given time.
Divide the program into modules so that not all modules need to be in the memory
simultaneously. In memory management, overlays work in the following steps, such as:

1. The programmer divided the program into many logical sections.


2. A small portion of the program had to remain in memory at all times, but the
remaining sections (or overlays) were loaded only when needed.
3. The use of overlays allowed programmers to write programs much larger than
physical memory, although memory usage depends on the programmer rather
than the operating system.

19.1 Example of Overlays

The best example of overlays is assembler. Consider the assembler has 2 passes, 2 pass
means at any time it will be doing only one thing, either the 1st pass or the 2nd pass.
This means it will finish 1st pass first and then 2nd pass. Let's assume that the available
main memory size is 150KB and the total code size is 200KB.

1. Pass 1.......................70KB
2. Pass 2.......................80KB
3. Symbol table............30KB
4. Common routine......20KB

As the total code size is 200KB and the main memory size is 150KB, it is impossible
to use 2 passes together. So, in this case, we should go with the overlays technique.

20 Thrashing
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
[email protected]
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.
During thrashing, the CPU spends less time on some actual productive work spend
more time swapping.
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.
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

[email protected]
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:
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.

[email protected]
Question on TLB

GATE | GATE-CS-2014-(Set-3)

Consider a paging hardware with a TLB. Assume that the entire page
table and all the pages are in the physical memory. It takes 10
milliseconds to search the TLB and 80 milliseconds to accessthe
physical memory. If the TLB hit ratio is 0.6, the effective memory
access time (in milliseconds) is .

A. 120
B. 122
C. 124
D. 118

Given,

1. TLB hit ratio = 0.6


2. Therefore, TLB miss ratio = 0.4
3. Time taken to access TLB (t) = 10 ms
4. Time taken to access main memory (m) = 80 ms

Effective Access Time (EAT) = 0.6 ( 10 + 80 ) + 0.4 ( 10 + 80 + 80 ) = 90 X 0.6 +


0.4 X 170 =
122

Hence, the right answer is option B.

GATE 2015 question on LRU and FIFO

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 respectto page replacement
policies First-In-First-out (FIFO) and Least Recently Used (LRU)?

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

[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
Syllabus

UNIT-III

Deadlocks: examples of deadlock, resource concepts, necessary conditions for

deadlock, deadlock solution, deadlock prevention, deadlock avoidance with Bankers

algorithms, deadlock detection, deadlock recovery.

Device Management: Disk Scheduling Strategies, Rotational Optimization, System

Consideration, Caching and Buffering.

UNIT – IV

File System: Introduction, File Organization, Logical File System, Physical File

System, File Allocation strategy, Free Space Management, File Access Control, Data

Access Techniques, Data Integrity Protection, Case study on file system viz FAT32,

NTFS, Ext2/Ext3 etc.

[email protected]
21 Deadlock
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.

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.

21.1 Necessary conditions for Deadlocks


1. Mutual Exclusion

[email protected]
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.
• Hold and wait is when a process is holding a resource and waiting to acquire another
resource that it needs but cannot proceed because another process is keeping the first
resource. Each of these processes must have a hold on at least one of the resources it’s
requesting.

3. No preemption

• The process which once scheduled will be executed till the completion. No other
process can be scheduled by the scheduler meanwhile.
• Preemption means temporarily interrupting a task or process to execute another task or
process. Preemption can occur due to an external event or internally within the system.
If we take away the resource from the process that is causing deadlock, we can avoid
deadlock. But is it a good approach? The answer is NO because that will lead to an
inconsistent state.
• For example, if we take away memory from any process(whose data was in the process
of getting stored) and assign it to some other process. Then will lead to an inconsistent
state.

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.

[email protected]
21.2 Methods For Handling Deadlocks
Methods that are used in order to handle the problem of deadlocks are as follows:

21.2.1 Deadlock Prevention


As we have discussed in the above section, that all four conditions: Mutual Exclusion,
Hold and Wait, No preemption, and circular wait if held by a system then causes
deadlock to occur. The main aim of the deadlock prevention method is to violate any
one condition among the four; because if any of one condition is violated then the
problem of deadlock will never occur. As the idea behind this method is simple but
the difficulty can occur during the physical implementation of this method in the
system.

21.2.2 Avoiding the Deadlock


This method is used by the operating system in order to check whether the system is in
a safe state or in an unsafe state. This method checks every step performed by the
operating system. Any process continues its execution until the system is in a safe state.
Once the system enters into an unsafe state, the operating system has to take a step back.

Basically, with the help of this method, the operating system keeps an eye on each
allocation, and make sure that allocation does not cause any deadlock in the system.

21.2.3 Deadlock detection and recovery


With this method, the deadlock is detected first by using some algorithms of the
resource-allocation graph. This graph is mainly used to represent the allocations of
various resources to different processes. After the detection of deadlock, a number of
methods can be used in order to recover from that deadlock.

21.2.4 Ignoring the Deadlock


According to this method, it is assumed that deadlock would never occur.This approach
is used by many operating systems where they assume that deadlock will never occur
which means operating systems simply ignores the deadlock. This approach can be
beneficial for those systems that are only used for browsing and for normal tasks. Thus
ignoring the deadlock method can be useful in many cases but it is not perfect in order
to remove the deadlock from the operating system.

[email protected]
22 Deadlock Prevention
If we simulate deadlock with a table which is standing on its four legs then we can also
simulate four legs with the four conditions which when occurs simultaneously, cause the
deadlock.
However, if we break one of the legs of the table then the table will fall definitely. The same
happens with deadlock, if we can be able to violate one of the four necessary conditions and
don't let them occur together then we can prevent the deadlock.

Let's see how we can prevent each of the conditions.

22.1 1. Mutual Exclusion


Mutual section from the resource point of view is the fact that a resource can never be used by
more than one process simultaneously which is fair enough but that is the main reason behind
the deadlock. If a resource could have been used by more than one process at the same time
then the process would have never been waiting for any resource.

22.1.1 Spooling
For a device like printer, spooling can work. There is a memory associated with the printer
which stores jobs from each of the process into it. Later, Printer
collects all the jobs and print each one of them according
to FCFS. By using this mechanism, the process doesn't
have to wait for the printer and it can continue whatever it
was doing. Later, it collects the output when it is produced.

Although, Spooling can be an effective approach to violate


mutual exclusion but it suffers from two kinds of problems.

1. This cannot be applied to every resource.


2. After some point of time, there may arise a race condition between the processes to get
space in that spool.

We cannot force a resource to be used by more than one process at the same time since it will
not be fair enough and some serious problems may arise in the performance. Therefore, we
cannot violate mutual exclusion for a process practically.

22.2 Hold and Wait

Hold and wait condition lies when a process holds a resource and waiting for some other
resource to complete its task. Deadlock occurs because there can be more than one process
which are holding one resource and waiting for other in the cyclic order.

[email protected]
• However, we have to find out some mechanism by which a process either doesn't hold any
resource or doesn't wait. That means, a process must be assigned all the necessary resources
before the execution starts. A process must not wait for any resource once the execution
has been started.
• This can be implemented practically if a process declares all the resources initially.
However, this sounds very practical but can't be done in the computer system because a
process can't determine necessary resources initially.
• Process is the set of instructions which are executed by the CPU. Each of the instruction
may demand multiple resources at the multiple times. The need cannot be fixed by the OS.

The problem with the approach is:


1. Practically not possible.
2. Possibility of getting starved will be increases due to the fact that some process
may hold a resource for a very long time.

22.3 No Preemption

• Deadlock arises due to the fact that a process can't be stopped once it starts. However, if
we take the resource away from the process which is causing deadlock then we can prevent
deadlock.
• This is not a good approach at all since if we take a resource away which is being used by
the process then all the work which it has done till now can become inconsistent.
• Consider a printer is being used by any process. If we take the printer away from that
process and assign it to some other process then all the data which has been printed can
become inconsistent and ineffective and also the fact that the process can't start printing
again from where it has left which causes performance inefficiency.

22.4 Eliminate Circular Wait


• Each resource will be assigned a numerical number.
• To eliminate circular wait, we assign a priority to each resource. A
process can only request resources in increasing order of priority.
• In the example above, process P3 is requesting resource R1, which
has a number lower than resource R3 which is already allocated to
process P3. So this request is invalid and cannot be made, as R1 is
already allocated to process P1.
• Challenges:

o It is difficult to assign a relative priority to resources, as one resource can be prioritized


differently by different processes. For Example: A media player will give a lesser priority
to a printer while a document processor might give it a higher priority. The priority of
resources is different according to the situation and use case.

[email protected]
23 Deadlock Ignorance or Ostrich Algorithm
• The simplest approach is the ostrich algorithm: stick your head in the sand and pretend
there is no problem at all.
• Different people react to this strategy in different ways. Mathematicians find it totally
unacceptable and say that deadlocks must be prevented at all costs.
• Engineers ask how often the problem is expected, how often the system crashes for other
reasons, and how serious a deadlock is.
• If deadlocks occur on average once every five years, but system crashes due to hardware
failures, compiler errors, and operating system bugs occur once a week, most engineers
would not be willing to pay a large penalty in performance or convenience to eliminate
deadlocks.
• Most operating systems, including UNIX and Windows, just ignore the problem on the
assumption that most users would prefer an occasional deadlock to a rule restricting all
users to one process, one open file, and one of everything.
• If deadlocks could be eliminated for free, there would not be much discussion. The problem
is that the price is high, mostly in terms of putting inconvenient restrictions on processes,
as we will see shortly. Thus we are faced with an unpleasant trade-off between convenience
and correctness, and a great deal of discussion about which is more important, and to whom.
Under these conditions, general solutions are hard to find.

24 Deadlock Avoidance in OS
• The operating system avoids Deadlock by knowing the maximum resource requirements
of the processes initially, and also, the Operating System knows the free resources available
at that time.
• The operating system tries to allocate the resources according to the process requirements
and checks if the allocation can lead to a safe state or an unsafe state. If the resource
allocation leads to an unsafe state, then the Operating System does not proceed further with
the allocation sequence.
Safe State and Unsafe State
Safe State – If Operating System is able to satisfy the needs of all
processes with their resource requirements. So all the processes are able
to complete their execution in a certain order. So, If the Operating System
is able to allocate or satisfy the maximum resource requirements of all
the processes in any order then the system is said to be in Safe State. So
safe state does not lead to Deadlock.
Unsafe State - If the Operating System is not able to prevent Processes
from requesting resources which can also lead to a Deadlock, then the
System is said to be in an Unsafe State.
Unsafe State does not necessarily cause deadlock it may or may not cause deadlock.

[email protected]
24.1 Banker’s Algorithm
Example:
Considering a system with five processes P0 through P4 and three resources of type A, B, C. Resource
type A has 10 instances, B has 5 instances and type C has 7 instances. Suppose at time t0 following
snapshot of the system has been taken:

[email protected]
[email protected]
25 Deadlock Detection and Recovery
25.1 Deadlock Detection
• When a deadlock is detected, the system can initiate recovery procedures to break the
deadlock and restore the system to a functional state.
• It is essential to monitor and detect deadlocks as early as possible to prevent any negative
impact on the system's performance and stability. A delay in detecting deadlocks can result
in significant wait times for processes and unresponsive systems, leading to user frustration
and potential business losses. There are two primary methods for detecting deadlocks:
resource allocation graph (RAG) and wait-for graph (WFG).

25.1.1 Resource Allocation Graph (RAG)


• The resource allocation graph (RAG) is a widely used method for deadlock detection
in computer systems. The RAG is a graphical representation of the current allocation
state of resources and the processes that are holding them. The nodes of the graph
represent the resources and the processes, and the edges represent the allocation
relationship between them.
• In the RAG method, a cycle in the graph indicates the presence of a deadlock. The RAG
method is highly efficient and can quickly detect deadlocks, making it an essential
technique in modern operating systems.

Can solve using Banker’s algorithm

[email protected]
25.1.2 Wait-For Graph (WFG)
The wait-for graph (WFG) is a common method used in
deadlock detection in computer systems. The WFG is a
graphical representation of the dependencies between the
processes and the resources that they are waiting for. In the
WFG, the nodes represent processes, and the resources are
represented as edges. Each edge points from the process
that is waiting for a resource to the process that currently
holds that resource. The WFG method can efficiently
detect deadlocks by analysing the graph for cycles. If a
cycle is found, it indicates that a set of processes is waiting
for resources that are being held by other processes in the same set, resulting in a deadlock.
The system can then take appropriate actions to break the deadlock, such as rolling back the
transactions or aborting some of the processes.

25.2 Deadlock Recovery

A traditional operating system such as Windows doesn’t deal with deadlock recovery as it is
a time and space-consuming process. Real-time operating systems use Deadlock recovery.
1. Killing the process –Killing all the processes involved in the deadlock. Killing process one by one.
After killing each process check for deadlock again and keep repeating the process till the system
recovers from deadlock. Killing all the processes one by one helps a system to break circular wait
conditions.
2. Resource Preemption –Resources are preempted from the processes involved in the deadlock, and
preempted resources are allocated to other processes so that there is a possibility of recovering the
system from the deadlock. In this case, the system goes into starvation.
3. Concurrency Control – Concurrency control mechanisms are used to prevent data inconsistencies
in systems with multiple concurrent processes. These mechanisms ensure that concurrent processes
do not access the same data at the same time, which can lead to inconsistencies and errors.
Deadlocks can occur in concurrent systems when two or more processes are blocked, waiting for

[email protected]
each other to release the resources they need. This can result in a system-wide stall, where no
process can make progress. Concurrency control mechanisms can help prevent deadlocks by
managing access to shared resources and ensuring that concurrent processes do not interfere with
each other.

• Since process P3 does not need any


resource, so it executes.
• After execution, process P3 release its
resources.

Then,

Available

=[00]+[01]

=[01]

Step-02:

• With the instances available currently, only


the requirement of the process P1 can be
satisfied.
• So, process P1 is allocated the requested
resources.
• It completes its execution and then free up
the instances of resources held by it.

Then-Available

=[01]+[10]

=[11]

Step-03:

• With the instances available currently, the


requirement of the process P2 can be
satisfied.
• So, process P2 is allocated the requested
resources.
• It completes its execution and then free up the
instances of resources held by it.
Then-

Available

=[11]+[01]

=[12]

Thus,

• There exists a safe sequence P3, P1, P2 in


which all the processes can be executed.
• So, the system is in a safe state.

[email protected]
References
1. https://www.geeksforgeeks.org/deadlock-
prevention/#:~:text=Deadlock%20avoidance%20is%20another%20technique,that%20cou
ld%20lead%20to%20deadlocks.
2. https://www.javatpoint.com/os-deadlock-avoidance
3. https://www.scaler.com/topics/operating-system/deadlock-avoidance-in-os/
4. https://www.tutorialspoint.com/deadlock-avoidance
5. https://www.studytonight.com/operating-system/deadlock-avoidance-in-operating-system

[email protected]
What is Disk Scheduling Algorithm?

A Process makes the I/O requests to the operating


system to access the disk. Disk Scheduling Algorithm
manages those requests and decides the order of the
disk access given to the requests.

Why Disk Scheduling Algorithm is needed?

Disk Scheduling Algorithms are needed because a


process can make multiple I/O requests and multiple
processes run at the same time. The requests made by
a process may be located at different sectors on
different tracks. Due to this, the seek time may increase
more. These algorithms help in minimizing the seek
time by ordering the requests made by the processes.

Important Terms related to Disk Scheduling Algorithms

Seek Time - It is the time taken by the disk arm to locate the desired track.

Rotational Latency - The time taken by a desired sector of the disk to rotate itself to the
position where it can access the Read/Write heads is called Rotational Latency.

Transfer Time - It is the time taken to transfer the data requested by the processes.

Disk Access Time - Disk Access time is the sum of the Seek Time, Rotational Latency, and
Transfer Time.

[email protected]
[email protected]
[email protected]
[email protected]
(199-0); Calculate Two
Answers, Ans 1. Add
199 in Total 2. Without
adding 199

Ans 1. Without adding


199

Ans 2. Add |199-0|; =187+199= 386 (Main Answer)

[email protected]
[email protected]
2.4 Operating System Considerations
• The operating system performs several functions that can have a significant impact on database application
performance.
• The operating system schedules threads of control (called processes in some operating systems), the
fundamental units of execution in a computer system. Issues here include the amount of time needed to
schedule a new thread (the less the better); the time slice of a database thread (should be long); and whether
different threads should have different priorities (database threads should all run at the same priority for most
applications). Some database systems such as Sybase implement threads within operating system processes
so the overhead of thread switching is potentially lower.
• The operating system manages virtual and physical memory mappings. The issue here is how big to make the
portion of virtual memory that is shared by all database threads, the database buffer, and how much random
access memory to devote to that buffer.
• The operating system controls the number of user threads that can access the database concurrently. The goal
is to have enough to accommodate the available users while avoiding thrashing.

• The operating system manages files. Issues here include whether the files can span devices (necessary for
large databases); whether the files can be built from contiguous portions of the disk (helpful for scan
performance); whether file reads can perform lookahead (also helpful for scan performance)

[email protected]
File-System
1 File Concepts, Attributes, Operations
1.1 File
• A file is a named collection of related information that is recorded on secondary storage such as magnetic
disks, magnetic tapes and optical disks.
• In general, a file is a sequence of bits, bytes, lines or records whose meaning is defined by the files creator
and user.

1.2 File concept


• The operating system can provide a logical view of the information stored in the disks, this logical unit is
known as a file.
• The information stored in files is not lost during power failures. A file helps to write data on the computer.
It is a sequence of bits, bytes, or records, the structure of which is defined by the owner and depends on
the type of the file.

Different types of files are:


• Executable file
In an executable file, the binary code that is loaded in the memory for execution is stored. It is stored in an exe
type file.
• Source file
The source file has subroutines and functions that are compiled later.
• Object file
An object file is a sequence of bytes used by the linker.
• Text file
A text file is a sequence of characters.

• Image file
An image file is a sequence of visual information, for example, vector art.

1.3 File Attributes


Some of the common file attributes are:

• Different OSes keep track of different file attributes, including:


o Name - Some systems give special significance to names, and particularly extensions ( .exe, .txt, etc),
and some do not. Some extensions may be of significance to the OS ( .exe), and others only to certain
applications ( .jpg )

[email protected]
o Identifier ( e.g. inode number )
o Type - Text, executable, other binary, etc.
o Location - on the hard drive.
o Size
o Protection
o Time & Date
o User ID
1.4 File Operations

• The file ADT (Abstract Data Type) supports many common operations:
o Creating a file
o Writing a file
o Reading a file
o Repositioning within a file
o Deleting a file
o Truncating a file.
• Most OSes require that files be opened before access and closed after all access is complete. Normally
the programmer must open and close files explicitly, but some rare systems open the file automatically
at first access. Information about currently open files is stored in an open file table, containing for
example:
o File pointer - records the current position in the file, for the next read or write access.
o File-open count - How many times has the current file been opened ( simultaneously by
different processes ) and not yet closed? When this counter reaches zero the file can be removed from
the table.
o Disk location of the file.
o Access rights
• Some systems provide support for file locking.
o A shared lock is for reading only.
o A exclusive lock is for writing as well as reading.

o An advisory lock is informational only, and not enforced. ( A "Keep Out" sign, which may be ignored.
)
o A mandatory lock is enforced. ( A truly locked door. )
o UNIX used advisory locks, and Windows uses mandatory locks.

[email protected]
1.5 File Types
Windows ( and some other systems ) use special file extensions to indicate the type of each file:

1.6 File Structure

• Some files contain an internal structure, which may or may not be known to the OS.
• For the OS to support particular file formats increases the size and complexity of the OS.
• UNIX treats all files as sequences of bytes, with no further consideration of the internal structure. (With
the exception of executable binary programs, which it must know how to load and find the first
executable statement, etc )
• Macintosh files have two forks - a resource fork, and a data fork. The resource fork contains information
relating to the UI, such as icons and button images, and can be modified independently of the data fork,
which contains the code or data as appropriate.
[email protected]
Internal File Structure

• Disk files are accessed in units of physical blocks, typically 512 bytes or some power-of-two multiple
thereof. (Larger physical disks use larger block sizes, to keep the range of block numbers within the
range of a 32-bit integer. )
• Internally files are organized in units of logical units, which may be as small as a single byte, or may be
a larger size corresponding to some data record or structure size.
• The number of logical units which fit into one physical block determines its packing, and has
an impact on the amount of internal fragmentation (wasted space) that occurs.
• As a general rule, half a physical block is wasted for each file, and the larger the block sizes the more
space is lost to internal fragmentation.

[email protected]
2 File System Structure
• Hard disks have two important properties that make them suitable for secondary storage of files in file
systems: (1) Blocks of data can be rewritten in place, and (2) they are direct access, allowing any block
of data to be accessed with only ( relatively ) minor movements of the disk heads and rotational latency.
• Disks are usually accessed in physical blocks, rather than a byte at a time. Block sizes may range from
512 bytes to 4K or larger.
• File systems organize storage on disk drives, and can be viewed as a layered design:
o At the lowest layer are the physical devices, consisting of the magnetic media, motors & controls, and
the electronics connected to them and controlling them. Modern disk put more and more of the electronic
controls directly on the disk drive itself, leaving relatively little work for the disk controller card to
perform.
o I/O Control consists of device drivers, special software programs ( often written in assembly ) which
communicate with the devices by reading and writing special codes directly to and from memory
addresses corresponding to the controller card's registers. Each controller card ( device ) on a system
has a different set of addresses ( registers, a.k.a. ports ) that it listens to, and a unique set of command
codes and results codes that it understands.
o The basic file system level works directly with the device drivers in terms of retrieving and storing raw
blocks of data, without any consideration for what is in each block. Depending on the system, blocks
may be referred to with a single block number, ( e.g. block # 234234 ), or with head-sector-cylinder
combinations.
o The file organization module knows about files and their logical blocks, and how they map to physical
blocks on the disk. In addition to translating from logical to

physical blocks, the file organization module also maintains the list of free blocks, and allocates free
blocks to files as needed.
o The logical file system deals with all of the meta data associated with a file ( UID, GID, mode, dates, etc
), i.e. everything about the file except the data itself. This level manages the directory structure and the
mapping of file names to file control blocks, FCBs, which contain all of the meta data as well as block
number information for finding the data on the disk.
• The layered approach to file systems means that much of the code can be used uniformly for a wide
variety of different file systems, and only certain layers need to be filesystem specific. Common file
systems in use include the UNIX file system, UFS, the Berkeley Fast File System, FFS, Windows
systems FAT, FAT32, NTFS, CD-ROM systems ISO 9660, and for Linux the extended file systems ext2
and ext3 ( among 40 others supported)
[email protected]
• Layered file system.

3 Logical and Physical File system


1. Physical files: Physical files contain the actual data that is stored on an iSeries system, and a description of
how data is to be presented to or received from a program. They contain only one record format and one or more
members. Records in database files can be described using either a field-level description or a record-level
description. A field-level description describes the fields in the record to the system. Database files that are
created with field-level descriptions are referred to as externally described files. A record-level description
describes only the length of the record, and not the contents of the record. Database files that are created with
record-level descriptions are referred to as program-described files. This means that your ILE C/C++ program
must describe the fields in the record.

An ILE C/C++ program can use either externally described or program-described files. If it uses an externally
described file, the ILE C/C++ compiler can extract information from the externally described file, and
automatically include field information in your program. Your program does not need to define the field
information. For further information see “Using Externally Described Files in Your Programs”. A physical file
can have a keyed sequence access path. This means that data is presented to an ILE C/C++ program in a sequence
that is based on one or more key fields in the file.

2. Logical files: Logical files do not contain data. They contain a description of records that are found in one or
more physical files. A logical file is a view or representation of one or more physical files. Logical files that
contain more than one format are referred to as multi-format logical files. If your program processes a logical
file that contains more than one record format, you can use the _Rformat() function to set the format you wish
to use. Some operations cannot be performed on logical files. If you open a logical file for stream file processing
[email protected]
with open modes W, W+, WB, or WB+, the file is opened but not cleared. If you open a logical file for record
file processing with open modes WR or WR+, the file is opened but not cleared. Records in iSeries database
files can be described using either a field-level description or a record-level description. The field-level
description of the record includes a description of all fields and their arrangement in this record. Since the
description of the fields and their arrangement is kept within a database file and not in your ILE C/C++ program,
database files created with a field-level description are referred to as externally described files.

Physical versus Logical Files :

Physical File System Logical File System

The logical file represents or perspective of one or


Physical files have the real data saved on an iSeries
multiple physical files. Logical files don't have data.
system and describe how the data is to be displayed to
They have a description of the records found in one or
or retrieved from a program.
multiple physical files.

It takes up space in memory, and it includes the original It doesn't take up any memory space. It does not include
data. data.

It exists without a Logical file. A logical file cannot exist without a physical file.

The CRTPF command is utilized to make such an The CRTLF command is utilized to make such an
object in a physical file. object in a logical file system.

If there is a logical file for a physical file, the physical If a physical file has a logical file, the logical file can be
file may not be erased until the logical file is deleted. removed without deleting the actual file.

The logical file system may include up to 32 record


The physical file system has one record format.
formats.

[email protected]
4 File/Disk Space Allocation Methods
The allocation methods define how the files are stored in the disk blocks. There
are three main disk space or file allocation methods.
4.1 Contiguous Allocation

• Contiguous Allocation requires that all blocks of a file be kept together contiguously.
• Performance is very fast, because reading successive blocks of the same file generally requires no
movement of the disk heads, or at most one small step to the next adjacent cylinder.
• Storage allocation involves the same issues discussed earlier for the allocation of contiguous blocks of
memory ( first fit, best fit, fragmentation problems, etc. ) The distinction is that the high time penalty
required for moving the disk heads from spot to spot may now justify the benefits of keeping files
contiguously when possible.
• ( Even file systems that do not by default store files contiguously can benefit from certain utilities that
compact the disk and make all files contiguous in the process. )
• Problems can arise when files grow, or if the exact size of a file is unknown at creation time:
o Over-estimation of the file's final size increases external fragmentation and wastes disk space.
o Under-estimation may require that a file be moved or a process aborted if the file grows beyond its
originally allocated space.
o If a file grows slowly over a long time period and the total final space must be allocated initially, then a
lot of space becomes unusable before the file fills the space.
• A variation is to allocate file space in large contiguous chunks, called extents.
When a file outgrows its original extent, then an additional one is allocated. ( For example an extent may
be the size of a complete track or even cylinder, aligned on an appropriate track or cylinder boundary. )
The high- performance files system Veritas uses extents to optimize performance.

[email protected]
Contiguous allocation of disk space.
4.2 Linked Allocation

• Disk files can be stored as linked lists, with the expense of the storage space consumed by each link. (
E.g. a block may be 508 bytes instead of 512. )
• Linked allocation involves no external fragmentation, does not require pre-known file sizes, and allows
files to grow dynamically at any time.
• Unfortunately linked allocation is only efficient for sequential access files, as random access requires
starting at the beginning of the list for each new location access.
• Allocating clusters of blocks reduces the space wasted by pointers, at the cost of internal fragmentation.
• Another big problem with linked allocation is reliability if a pointer is lost or damaged. Doubly linked
lists provide some protection, at the cost of additional overhead and wasted space.

[email protected]
Linked allocation of disk space.

• The File Allocation Table, FAT, used by DOS is a variation of linked allocation, where all the links are
stored in a separate table at the beginning of the disk. The benefit of this approach is that the FAT table
can be cached in memory, greatly improving random access speeds.

• File-allocation table.
4.3 Indexed Allocation

• Indexed Allocation combines all of the indexes for accessing each file into a common
block ( for that file ), as opposed to spreading them all over the disk or storing them in a
FAT table.
[email protected]
• Indexed allocation of disk space.

• Some disk space is wasted ( relative to linked lists or FAT tables ) because an entire index block must
be allocated for each file, regardless of how many data blocks the file contains. This leads to questions
of how big the index block should be, and how it should be implemented. There are several approaches:
o Linked Scheme - An index block is one disk block, which can be read and written in a single disk
operation. The first index block contains some header information, the first N block addresses, and if
necessary a pointer to additional linked index blocks.
o Multi-Level Index - The first index block contains a set of pointers to secondary index blocks, which in
turn contain pointers to the actual data blocks.
o Combined Scheme - This is the scheme used in UNIX inodes, in which the first 12 or so data block
pointers are stored directly in the inode, and then

singly, doubly, and triply indirect pointers provide access to more data blocks as needed.
( See below. ) The advantage of this scheme is that for small files ( which many are ), the
data blocks are readily accessible ( up to 48K with 4K block sizes ); files up to about
4144K ( using 4K blocks ) are accessible with only a single indirect block ( which can be
[email protected]
cached ), and huge files are still accessible using a relatively small number of disk
accesses ( larger in theory than can be addressed by a 32-bit address, which is why some
systems have moved to 64-bit file pointers. )

The UNIX inode.


4.4 Performance

• The optimal allocation method is different for sequential access files than for random
access files, and is also different for small files than for large files.
• Some systems support more than one allocation method, which may require specifying
how the file is to be used ( sequential or random access ) at the time it is allocated. Such
systems also provide conversion utilities.
• Some systems have been known to use contiguous access for small files, and
automatically switch to an indexed scheme when file sizes surpass a certain threshold.
• And of course some systems adjust their allocation schemes ( e.g. block sizes ) to best
match the characteristics of the hardware for optimum performance.

5 Free-Space Management
Free space management is a critical aspect of operating systems as it involves managing the
available storage space on the hard disk or other secondary storage devices. The operating system
uses various techniques to manage free space and optimize the use of storage devices. Here are
some of the commonly used free space management techniques. The operating system allocates
memory space to the files in the hard disk. After allocating memory to the files in the hard disk,
there is free space or void space left for other files. Moreover, different files get stored and deleted
in the memory simultaneously.

5.1 Bit Vector


In this method, each block in the hard disk is represented by a bit (either 0 or 1). If a block has a
bit 0 means that the block is allocated to a file, and if a block has a bit 1 means that the block is
not allocated to any file, i.e., the block is free.
For example, consider a disk having 16 blocks where block numbers 2, 3, 4, 5, 8, 9, 10, 11, 12,
and 13 are free, and the rest of the blocks, i.e., block numbers 0, 1, 6, 7, 14 and 15 are allocated to
some files. The bit vector for this disk will look like this-

We can find the free block number from the bit vector using the following method-

Block number = (Number of bits per word )* (number of 0-value words) + (offset of first bit)
We will now find the first free block number in the above example.
The first group of 8 bits (00111100) constitutes a non-zero word since all bits are not 0. After
finding the non-zero word, we will look for the first 1 bit. This is the third character of the non-
zero word. Hence, offset = 3.
Therefore, the first free block number = 8 * 0 + 3 = 3.
• Advantages

• It is simple to understand.
• It is an efficient method.
• It occupies less memory.
• Disadvantages
• For finding a free block, the operating system may need to search the entire bit vector.
• To detect the first 1 in a word that is not 0 using this method, special hardware support
is needed.
• Keeping the bit vector in the main memory is possible for smaller disks but not for
larger ones. For example, a 1.3 GB disk with 512-byte blocks would need a bit vector
of over 332 KB to track its free blocks. Giving away 332 KB just to maintain its free
block space is not so efficient in the long run.
5.2 Linked List

▪ In this method, all the free blocks


existing in the disk are linked together in a
linked list.
▪ The address of the first free block is
stored somewhere in the memory. Each free
block contains a pointer that contains the
address to the next free block. The last free
block points to null, indicating the end of the
linked list.
▪ For example, consider a disk having
16 blocks where block numbers 3, 4, 5, 6, 9,
10, 11, 12, 13, and 14 are free, and the rest of
the blocks, i.e., block numbers 1, 2, 7, 8, 15
and 16 are allocated to some files. If we
maintain a linked list, then Block 3 will contain a pointer to Block 4, and Block 4 will
contain a pointer to Block 5.
▪ Similarly, Block 5 will point to Block 6, Block 6 will point to Block 9, Block 9 will point
to Block 10, Block 10 will point to Block 11, Block 11 will point to Block 12, Block 12
will point to Block 13 and Block 13 will point to Block 14. Block 14 will point to null.
▪ The address of the first free block, i.e., Block 3, will be stored somewhere in the memory.
This is also represented in the following figure-
• Advantages
o External fragmentation is prevented by linked list allocation. As opposed to contiguous
allocation, this prevents the wasting of memory blocks.
o It is also quite simple to make our file bigger. All we have to do is link a new file block to
our linked list. The file can so expand as long as memory blocks are available.
• Since the directory only needs to hold the starting and ending pointers of the file, linked
list allocation places less strain on it.

• Disadvantages
• This method is inefficient since we need to read each block to traverse the list, which
takes more I/O time.

• There is an overhead in maintaining the pointer.


• There is no provision for random or direct memory access in linked list allocation. We
must search through the full linked list to locate the correct block if we wish to access
a certain file block.
You can also read about the Multilevel Queue Scheduling.
5.3 Grouping
• A variation on linked list free lists is to use links of blocks of indices of free blocks. If a
block holds up to N addresses, then the first block in the linked-list contains up to N-1
addresses of free blocks and a pointer to the next block of free addresses.
• For example, consider a disk having 16 blocks where block numbers 3, 4, 5, 6, 9, 10, 11,
12, 13, and 14 are free, and the rest of the blocks, i.e., block numbers 1, 2, 7, 8, 15 and 16
are allocated to some files.
• If we apply the Grouping method considering n to be 3, Block 3 will store the addresses of
Block 4, Block 5, and Block 6. Similarly, Block 6 will store the addresses of Block 9, Block
10, and Block 11. Block 11 will store the
addresses of Block 12, Block 13, and Block 14.
This is also represented in the following figure-
• This method overcomes the disadvantages of the
linked list method. The addresses of a large
number of free blocks can be found quickly, just
by going to the first free block or the nth free
block. There is no need to traverse the whole list,
which was the situation in the linked list method.

5.4 Counting
• When there are multiple contiguous blocks of free space then the system can keep track of
the starting address of the group and the number of contiguous free blocks. As long as the
average length of a contiguous group of free blocks is greater than two this offers a savings
in space needed for the free list. ( Similar to compression techniques used for graphics
images when a group of pixels all the same color is encountered.)

• This method takes advantage of the fact that several contiguous blocks may be allocated or
freed simultaneously. In this method, a linked list is maintained but in addition to the
pointer to the next free block, a count of free contiguous
blocks that follow the first block is also maintained. Thus
each free block in the disk will contain two things-
1. A pointer to the next free block.

2. The number of free contiguous blocks following it.

For example, consider a disk having 16 blocks where block numbers 3, 4, 5, 6, 9, 10, 11, 12, 13,
and 14 are free, and the rest of the blocks, i.e., block numbers 1, 2, 7, 8, 15 and 16 are allocated to
some files.
If we apply the counting method, Block 3 will point to Block 4 and store the count 4 (since Block
3, 4, 5, and 6 are contiguous). Similarly, Block 9 will point to Block 10 and keep the count of 6
(since Block 9, 10, 11, 12, 13, and 14 are contiguous). This is also represented in the following
figure-

This method also overcomes the disadvantages of the linked list method since there is no need to
traverse the whole list.

5.5 Space Maps


• Sun's ZFS file system was designed for HUGE numbers and sizes of files, directories, and
even file systems.

• The resulting data structures could be VERY inefficient if not implemented carefully. For
example, freeing up a 1 GB file on a 1 TB file system could involve updating thousands of
blocks of free list bit maps if the file was spread across the disk.

• ZFS uses a combination of techniques, starting with dividing the disk up into ( hundreds
of ) metaslabs of a manageable size, each having their own space map.

• Free blocks are managed using the counting technique, but rather than write the
information to a table, it is recorded in a log-structured transaction record. Adjacent free
blocks are also coalesced into a larger single free block.

• An in-memory space map is constructed using a balanced tree data structure, constructed
from the log data.
• The combination of the in-memory tree and the on-disk log provide for very fast and
efficient management of these very large files and free blocks.

6 Access Methods (Important) (File Access Mechanisms)


6.1 Sequential Access
• A sequential access file emulates magnetic tape operation, and generally
supports a few operations:
o read next - read a record and advance the tape to the next position.
o write next - write a record and advance the tape to the next position.
o rewind
o skip n records - May or may not be supported. N may be limited to positive
numbers, or may be limited to +/- 1.

• Sequential-access file.
6.2 Direct Access

• Jump to any record and read that record. Operations supported include:
o read n - read record number n. ( Note an argument is now required. )
o write n - write record number n. ( Note an argument is now required. )
o jump to record n - could be 0 or the end of file.
o Query current record - used to return back to this record later.
o Sequential access can be easily emulated using direct access. The inverse is complicated
and inefficient.
• Simulation of sequential access on a direct-access file.

For the Better Classification of the files, the Files will be stored in Directory
Directory == Folder

Other Access Methods

• An indexed access scheme can be easily built on top of a direct access system. Very large
files may require a multi-tiered indexing scheme, i.e. indexes of indexes.

• Example of index and relative files.


6.3 Directory Structure
6.3.1 Storage Structure
• A disk can be used in its entirety for a file system.

• Alternatively a physical disk can be broken up into multiple partitions, slices, or mini-disks, each of
which becomes a virtual disk and can have its own filesystem. ( or be used for raw storage, swap space,
etc. )
• Or, multiple physical disks can be combined into one volume, i.e. a larger virtual disk, with its own
filesystem spanning the physical disks.
• A typical file-system organization.
Directory Overview

• Directory operations to be supported include:


o Search for a file
o Create a file - add to the directory
o Delete a file - erase from the directory
o List a directory - possibly ordered in different ways.
o Rename a file - may change sorting order
o Traverse the file system.
6.3.2 Single-Level Directory

• Simple to implement, but each file must have a unique name.

• Single-level directory.
6.3.3 Two-Level Directory

• Each user gets their own directory space.


• File names only need to be unique within a given user's directory.
• A master file directory is used to keep track of each users directory, and must be
maintained when users are added to or removed from the system.
• A separate directory is generally needed for system ( executable ) files.
• Systems may or may not allow users to access other directories besides their own
o If access to other directories is allowed, then provision must be made to specify the
directory being accessed.
o If access is denied, then special consideration must be made for users to run programs
located in system directories. A search path is the list of directories in which to search
for executable programs, and can be set uniquely for each user.

• Two-level directory structure.


6.3.4 Tree-Structured Directories

• An obvious extension to the two-tiered directory structure, and the one with which we
are all most familiar.
• Each user / process has the concept of a current directory from which all (
relative ) searches take place.
• Files may be accessed using either absolute pathnames ( relative to the root of the tree )
or relative pathnames ( relative to the current directory. )
• Directories are stored the same as any other file in the system, except there is a bit that
identifies them as directories, and they have some special structure that the OS
understands.
• One question for consideration is whether or not to allow the removal of directories that
are not empty - Windows requires that directories be emptied first, and UNIX provides
an option for deleting entire sub-trees.
• Tree-structured directory structure.
6.3.5 Acyclic-Graph Directories

• When the same files need to be accessed in more than one place in the directory structure ( e.g. because
they are being shared by more than one user / process ), it can be useful to provide an acyclic-graph
structure. ( Note the directed arcs from parent to child. )
• UNIX provides two types of links for implementing the acyclic-graph structure. ( See "man ln" for more
details. )
o A hard link ( usually just called a link ) involves multiple directory entries that both refer to the same
file. Hard links are only valid for ordinary files in the same filesystem.
o A symbolic link, that involves a special file, containing information about where to find the linked file.
Symbolic links may be used to link directories and/or files in other filesystems, as well as ordinary files
in the current filesystem.

• Windows only supports symbolic links, termed shortcuts.


• Hard links require a reference count, or link count for each file, keeping track of how many directory
entries are currently referring to this file. Whenever one of the references is removed the link count is
reduced, and when it reaches zero, the disk space can be reclaimed.
• For symbolic links there is some question as to what to do with the symbolic links when the original file
is moved or deleted:
o One option is to find all the symbolic links and adjust them also.
o Another is to leave the symbolic links dangling, and discover that they are no longer valid the next time
they are used.
o What if the original file is removed, and replaced with another file having the same name before the
symbolic link is next used?

• Acyclic-graph directory structure.


6.3.6 General Graph Directory

• If cycles are allowed in the graphs, then several problems can arise:
o Search algorithms can go into infinite loops. One solution is to not follow links in search algorithms. (
Or not to follow symbolic links, and to only allow symbolic links to refer to directories. )
o Sub-trees can become disconnected from the rest of the tree and still not have their reference counts
reduced to zero. Periodic garbage collection is
required to detect and resolve this problem. ( chkdsk in DOS and fsck in UNIX search
for these problems, among others, even though cycles are not supposed to be allowed
in either system. Disconnected disk blocks that are not marked as free are added back
to the file systems with made-up file names, and can usually be safely deleted. )

General graph directory.


7 Data Integrity Protection
In the context of an operating system, "data integrity protection" refers to the
mechanisms implemented to ensure that data stored on a system remains accurate,
complete, and consistent, preventing unauthorized modifications or corruption, and
is typically achieved through access controls, error detection (like checksums), data
validation, and logging, all aimed at maintaining the reliability of information
throughout its lifecycle; as described in Galvin's "Operating System Concepts.".

Key aspects of data integrity protection in an operating system:


• Access Controls:
Limiting who can access and modify data based on user roles and permissions, preventing
unauthorized alterations.
• Checksums:
A mathematical calculation applied to data to generate a unique "fingerprint" that can be
used to verify if the data has been altered during storage or transmission.
• Data Validation:
Implementing checks to ensure data adheres to defined formats and constraints during
input and processing.
• File System Consistency Checks:
Regularly scanning the file system to identify and repair inconsistencies or corrupted
data.
• Error Detection and Handling:
Mechanisms to identify errors during data operations (like read/write errors) and take
corrective actions, potentially including logging the error for further analysis.
• Backup and Recovery Systems:
Regularly backing up data to enable restoration in case of accidental corruption or system
failure.
• Protection Mechanisms:
Galvin extensively discusses the concept of protection within an operating system, which
includes access controls, memory protection, and file system protection, all contributing
to data integrity.
• File System Design:
The file system plays a crucial role in maintaining data integrity by managing file
attributes, implementing journaling techniques to track changes, and performing
consistency checks.

8 Case study on file system viz FAT32, NTFS, Ext2/Ext3 etc.

Characteristics NTFS FAT32


Release date First introduced in 1993 for It was introduced in 1996
Windows NT 3.1. The current
version was unveiled in 2012
alongside Windows 8
File structure An advanced, high-performance file A simple file system that
system with a customized journaling stores files as a linked list of
system blocks in a table.
Ideal use Ideal for external hard-disk drives Ideal for smaller storage
with large storage capacities. devices with less than 4GB
such as SD cards, microSD
cards, flash drives, SSDs,
digital cameras, and
smartphones.
Max Volume It supports large volume sizes up to It supports volume sizes of up
size 256TB. to 2TB.
File size Supports very large files—16EB Individual files on a FAT32
(Exabytes) theoretically. drive can’t be over 4GB in
size—that’s the maximum.
And, 8TB maximum partition
size.
Fault tolerance NTFS automatically repairs FAT32 maintains two different
files/folders in the case of power copies of the FAT in the case
failures or errors. of damage.
Compatibility NTFS is supported by Windows It works well
NT/2000, XP, 7, and Vista. MAC on all versions of Windows.
OS X can only read from NTFS It’s compatible with other
formatted drives, it can not write to operating systems—Mac and
them. Linux(has both read/write
access).
Compression NTFS allows automatic file FAT32 lacks the compression
compression allowing you to save feature.
disk space and archive folders.
Encryption It offers file-level encryption which No native encryption.
protects your file from unauthorized
access.
Speed NTFS can transfer files at 25 MB/s. It’s low—it has a maximum
speed of 5 MB/s.
Security Users and user groups can be given FAT32 provides very minimal
specific permissions to read, write or security, every user on the
execute drives, folders, or files. same computer has access to
data on that drive.

A. Ext2-
The ext2 or second extended file system is a file system for the Linux kernel. It was initially
designed by Remy Card as a replacement for the extended file system (ext). It was the first
commercial-grade file system for Linux. Maximum file size can be from 16 GB to 2 TB.
Overall ext2 file system size can be from 2 TB to 32 TB.

B. Ext3
Ext3 the third extended file system is the commonly used filesystem on Linux. It is extension
of ext2 . Ext3 is introduced in 2001 which is Developed by Stephen Tweedie. Starting from
Linux Kernel 2.4.15 ext3 was available. Maximum file size can be from 16GB to 2TB. Over
all ext3 file system size can be from 2TB to 32TB. There are three types of journaling available
in ext3 file system.

You might also like