OS
OS
1
2. System and application programs
3. Operating system
4. Hardware
Every general-purpose computer consists of hardware, an operating system(s),
system programs, and application programs. The hardware consists of memory,
CPU, ALU, I/O devices, peripheral devices, and storage devices. The system
program consists of compilers, loaders, editors, OS, etc. The application program
consists of business programs and database programs.
2
• A memory Management component that includes buffering caching and
spooling.
• A general device driver interface.
Drivers for specific hardware devices.
Assembler: The input to an assembler is an assembly language program. The
output is an object program plus information that enables the loader to prepare
the object program for execution. At one time, the computer programmer had at
his disposal a basic machine that interpreted, through hardware, certain
fundamental instructions. He would program this computer by writing a series
of ones and Zeros (Machine language), and place them into the memory of the
machine. Examples of assembly languages include
Compiler and Interpreter: The High-level languages- examples are C, C++, Java,
Python etc (around 300+ famous high level languages) are processed by
compilers and interpreters. A compiler is a program that accepts a source
program in a “high-level language “and produces machine code in one go. Some
of the compiled languages are FORTRAN, COBOL, C, C++ , Rust and Go. An
interpreter is a program that does the same thing but converts high-level code
to machine code line-by-line and not all at once. Example of interpreted
languages are Python, Perl and Ruby.
Loader: A Loader is a routine that loads an object program and prepares it for
execution. There are various loading schemes: absolute, relocating, and direct-
linking. In general, the loader must load, relocate and link the object program.
The loader is a program that places programs into memory and prepares them
for execution. In a simple loading scheme, the assembler outputs the machine
language translation of a program on a secondary device and a loader places it
in the core. The loader places into memory the machine language version of the
user’s program and transfers control to it. Since the loader program is much
smaller than the assembler, those make more core available to the user’s
program.
History of Operating system: The operating system has been evolving through
the years. The following table shows the history of OS.
3
Features of operating systems:
4
A technician might be asked to choose and install an operating system for a
customer. The type of OS selected depends on the customer's requirements for
the computer. There are two distinct types of operating systems: desktop
operating systems and network operating systems. A desktop operating system is
intended for use in a Small Office/Home Office (SOHO) with a limited number of
users. A Network Operating System (NOS) is designed for a corporate environment
serving multiple users with a wide range of needs.
A network OS has the following characteristics:
• Supports multiple users
• Runs multi-user applications
• Is robust and redundant
• Provides increased security compared to desktop operating systems
These are the most common network operating systems:
• Microsoft Windows – Network operating systems offered by Microsoft are
Windows 2000 Server, Windows Server 2003, and Windows Server 2008. Windows
Server operating systems use a central database called Active Directory to manage
network resources.
• Novell NetWare – Novell NetWare was the first OS to meet network OS
requirements and enjoy widespread deployment in PC-based Local Area Networks
(LANs) back in the 1980s.
• Linux – Linux operating systems include Red Hat, Caldera, SUSE, Debian, Fedora,
Ubuntu, and Slackware.
• UNIX – Various corporations offer proprietary operating systems based on UNIX.
Why OS is essential :-
The operating system (OS) acts as a manager for all the I/O device, memory, CPU,
file storage resources and allocates them to specific programs and users,
whenever necessary to perform a particular task. Therefore, the operating system
is the resource manager that means it can manage the resources of a computer
system internally.
The operating systems are important and should be correctly used when writing
the user applications. Large and complex systems have high economic impact and
this result in interesting problems of management.
Few systems are involved in the design and implementation of OS but,
nevertheless many general techniques have to be learnt and applied.
Operating systems combine concepts from many other areas of Computer
Science: Architecture, Languages, Data Structures, Algorithms, etc.
So finally, we can say that an operating system is the most important software
that runs on any system. It manages a computer's resources like memory,
processes involved, software and hardware.
Operating systems provide an interface to communicate with the computer
without learning the computer's language i.e. machine language. Basically, an
operating system provides a layer between the program and hardware to enable
a program to use a standard interface irrespective of what hardware is used.
5
Operating System does the following
An operating system provides a user interface by which we can run our
applications on computer hardware. It depends on the point of view.
Users always think about convenience, ease of use and good performance and do
not care about resource utilization.
OS shared computers like mainframe or minicomputer making all users happy.
OS dedicated system users such as workstations have dedicated resources but
frequently use shared resources from servers.
There is little or no user interface for some computer systems, such as embedded
computers in devices and automobiles.
Functions of operating system
The functions of an operating system are as follows−
•Provide a user interface
•Resource Management − An OS manages a computer's resources like
memory, processes involved, etc very efficiently.
• Handles I/O operations
• Security − Operating system protects the data and resources of
computer
• Helps in coordination between software and users.
Operating System Goals
The operating system has mainly three goals which are convenience, efficiency,
and capability of evolution. A more complete explanation is as follows −
• Convenience makes computer use more comfortable.
• We can efficiently use the resources of a computer system with the
help of an operating system.
• There is a capability of evolution, in building an operating system it is
possible to make it easier to develop, test, and use new systems.
Operating Systems, has evolved in past years. It went through several changes
before getting its original form. These changes in the operating system are
known as the evolution of operating systems. The evolution of the operating
system went through four generations. Let us see these generations in detail:
First Generation:
6
and the tendency to commit an error by a person often lead to problems.
Thus electronic machines were introduced to do the work. Though these
machines did not use an OS at all still this phase is regarded as the
beginning of the Operating Systems era.
• The electronic machines supplied instructions that were to be executed
by the machine immediately because if an error occurred, the whole
process was to be restarted again. The calculation speed in these
machines was limited and still errors could occur if there was any error
in the instruction provided to the machine. These systems were referred
to as serial processing systems.
Second Generation:
Third Generation:
Fourth Generation:
7
• The operating systems in this generation saw the use of a Graphical User
Interface (GUI) which made it very easy to interact with the operating
system and in turn with the hardware.
• The fourth generation of operating systems saw the invention of time-
shared operating systems and the Macintosh operating systems. Let us
see about the time-shared operating systems and the Macintosh
operating systems in brief.
In other operating systems, many times users felt that their work was not being
done as some tasks took too long to get completed. This lead to starvation of
processes or jobs. Time-shared operating systems solved this problem by
ensuring that no user feels that his work is not being done. These operating
systems made use of scheduling algorithms such as Round Robin in which the
work of a user was done for a specific time and then the OS moved to the next
job. Thus in actuality, these OS shared the time between multiple users and
hence got the name Time Shared Operating Systems.
These types of operating systems paved the way for Mac and Windows OS. The
use of personal computers grew as the OS evolved more and more and the
computers became more user-centric instead of being shared by multiple users.
Personal computers became increasingly cheap as the hardware costs decreased.
As the hardware evolved more and more, the color Macs were developed and
Microsoft implemented Windows based on the Graphical User Interface operating
system.
8
The Third Generation ( 1965 - 1980 ): Integrated Circuits and
Multiprogramming
Until the 1960’s, there were two types of computer systems i.e the scientific and
the commercial computers. These were combined by IBM in the System/360. This
used integrated circuits and provided a major price and performance advantage
over the second generation systems.
The third generation operating systems also introduced multiprogramming. This
meant that the processor was not idle while a job was completing its I/O
operation. Another job was scheduled on the processor so that its time would not
be wasted.
The Fourth Generation ( 1980 - Present ): Personal Computers
Personal Computers were easy to create with the development of large-scale
integrated circuits. These were chips containing thousands of transistors on a
square centimeter of silicon. Because of these, microcomputers were much
cheaper than minicomputers and that made it possible for a single individual to
own one of them.
The advent of personal computers also led to the growth of networks. This
created network operating systems and distributed operating systems. The users
were aware of a network while using a network operating system and could log in
to remote machines and copy files from one machine to another.
9
It is better that operating systems have a modular structure, unlike MS-DOS. That
would lead to greater control over the computer system and its various
applications. The modular structure would also allow the programmers to hide
information as required and implement internal routines as they see fit without
changing the outer specifications.
Layered Structure
One way to achieve modularity in the operating system is the layered approach.
In this, the bottom layer is the hardware and the topmost layer is the user
interface.
An image demonstrating the layered approach is as follows −
As seen from the image, each upper layer is built on the bottom layer. All the
layers hide some structures, operations etc from their upper layers.
One problem with the layered structure is that each layer needs to be carefully
defined. This is necessary because the upper layers can only use the
functionalities of the layers below them.
Operating System – Processes:-
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 −
10
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.
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;
}
11
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.
Process Life Cycle
When a process executes, it passes through different states. These stages may
differ in different operating systems, and the names of these states are also not
standardized.
In general, a process can have one of the following five states at a time.
S.N. State & Description
1 Start
This is the initial state when a process is first started/created.
2 Ready
The process is waiting to be assigned to a processor. Ready
processes are waiting to have the processor allocated to them by
the operating system so that they can run. Process may come into
this state after Start state or while running it by but interrupted
by the scheduler to assign CPU to some other process.
3
Running
Once the process has been assigned to a processor by the OS
scheduler, the process state is set to running and the processor
executes its instructions.
4 Waiting
Process moves into the waiting state if it needs to wait for a
resource, such as waiting for user input, or waiting for a file to
become available.
5
Terminated or Exit
Once the process finishes its execution, or it is terminated by the
operating system, it is moved to the terminated state where it
waits to be removed from main memory.
12
Components of Operating System:-
There are various components of an Operating System to perform well defined
tasks. Though most of the Operating Systems differ in structure but logically they
have similar components. Each component must be a well-defined portion of a
system that appropriately describes the functions, inputs, and outputs.
There are following 8-components of an Operating System:
1. Process Management
2. I/O Device Management
3. File Management
4. Network Management
5. Main Memory Management
6. Secondary Storage Management
7. Security Management
8. Command Interpreter System
13
File Management
File management is one of the most visible services of an operating system.
Computers can store information in several different physical forms; magnetic
tape, disk, and drum are the most common forms.
A file is defined as a set of correlated information and it is defined by the creator
of the file. Mostly files represent data, source and object forms, and programs.
Data files can be of any type like alphabetic, numeric, and alphanumeric.
A files is a sequence of bits, bytes, lines or records whose meaning is defined by
its creator and user.
The operating system implements the abstract concept of the file by managing
mass storage device, such as types and disks. Also files are normally organized
into directories to ease their use. These directories may contain files and other
directories and so on.
The operating system is responsible for the following activities in connection with
file management:
Network Management
The definition of network management is often broad, as network management
involves several different components. Network management is the process of
managing and administering a computer network. A computer network is a
collection of various types of computers connected with each other.
Network management comprises fault analysis, maintaining the quality of service,
provisioning of networks, and performance management.
Network management is the process of keeping your network healthy for an
efficient communication between different computers.
Following are the features of network management:
• Network administration
• Network maintenance
• Network operation
• Network provisioning
• Network security
14
The operating system is responsible for the following activities in connections
with memory management:
• Keep track of which parts of memory are currently being used and by whom.
• Decide which processes to load when memory space becomes available.
• Allocate and deallocate memory space as needed.
Disk scheduling
Security Management
The operating system is primarily responsible for all task and activities happen in
the computer system. The various processes in an operating system must be
protected from each other’s activities. For that purpose, various mechanisms
which can be used to ensure that the files, memory segment, cpu and other
resources can be operated on only by those processes that have gained proper
authorization from the operating system.
Security Management refers to a mechanism for controlling the access of
programs, processes, or users to the resources defined by a computer controls to
be imposed, together with some means of enforcement.
For example, memory addressing hardware ensure that a process can only execute
within its own address space. The timer ensure that no process can gain control
of the CPU without relinquishing it. Finally, no process is allowed to do it’s own
I/O, to protect the integrity of the various peripheral devices.
Command Interpreter System
One of the most important component of an operating system is its command
interpreter. The command interpreter is the primary interface between the user
and the rest of the system.
Command Interpreter System executes a user command by calling one or more
number of underlying system programs or system calls.
Command Interpreter System allows human users to interact with the Operating
System and provides convenient programming environment to the users.
15
Many commands are given to the operating system by control statements. A
program which reads and interprets control statements is automatically executed.
This program is called the shell and few examples are Windows DOS command
window, Bash of Unix/Linux or C-Shell of Unix/Linux.
Structures of Operating Systems:-
Operating system can be implemented with the help of various structures. The
structure of the OS depends mainly on how the various common components of
the operating system are interconnected and melded into the kernel. Depending
on this we have following structures of the operating system:
Simple structure:
Such operating systems do not have well defined structure and are small, simple
and limited systems. The interfaces and levels of functionality are not well
separated. MS-DOS is an example of such operating system. In MS-DOS
application programs are able to access the basic I/O routines. These types of
operating system cause the entire system to crash if one of the user programs
fails.
Diagram of the structure of MS-DOS is shown below.
16
The main disadvantage of this structure is that at each layer, the data needs to
be modified and passed on which adds overhead to the system. Moreover careful
planning of the layers is necessary as a layer can use only lower level layers.
UNIX is an example of this structure.
17
module can call any other module.
For example Solaris OS is organized as shown in the figure.
• Program execution
• I/O operations
• File System manipulation
• Communication
• Error Detection
• Resource Allocation
• Protection
Program execution
Operating systems handle many kinds of activities from user programs to system
programs like printer spooler, name servers, file server, etc. Each of these
activities is encapsulated as a process.
A process includes the complete execution context (code to execute, data to
manipulate, registers, OS resources in use). Following are the major activities of
an operating system with respect to program management −
18
I/O Operation
An I/O subsystem comprises of I/O devices and their corresponding driver
software. Drivers hide the peculiarities of specific hardware devices from the
users.
An Operating System manages the communication between user and device
drivers.
• I/O operation means read or write operation with any file or any
specific I/O device.
• Operating system provides the access to the required I/O device when
required.
File system manipulation
A file represents a collection of related information. Computers can store files on
the disk (secondary storage), for long-term storage purpose. Examples of storage
media include magnetic tape, magnetic disk and optical disk drives like CD, DVD.
Each of these media has its own properties like speed, capacity, data transfer rate
and data access methods.
A file system is normally organized into directories for easy navigation and usage.
These directories may contain files and other directions. Following are the major
activities of an operating system with respect to file management −
19
Error handling
Errors can occur anytime and anywhere. An error may occur in CPU, in I/O devices
or in the memory hardware. Following are the major activities of an operating
system with respect to error handling −
20
5. Communication
System Programs:-
System Programming can be defined as the act of building Systems Software
using System Programming Languages. According to Computer Hierarchy, one
which comes at last is Hardware. Then it is Operating System, System Programs,
and finally Application Programs. Program Development and Execution can be
done conveniently in System Programs. Some of the System Programs are simply
user interfaces, others are complex. It traditionally lies between the user interface
and system calls.
21
So here, the user can only view up-to-the System Programs he can’t see System
Calls.
System Programs can be divided into these categories :
22
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 −
Process States:-
• New (Create) – In this step, the process is about to be created but not
yet created, it is the program which is present in secondary memory
that will be picked up by OS to create the process.
• Ready – New -> Ready to run. After the creation of a process, the process
enters the ready state i.e. the process is loaded into the main memory.
The process here is ready to run and is waiting to get the CPU time for
its execution. Processes that are ready for execution by the CPU are
maintained in a queue for ready processes.
• Run – The process is chosen by CPU for execution and the instructions
within the process are executed by any one of the available CPU cores.
• Blocked or wait – Whenever the process requests access to I/O or needs
input from the user or needs access to a critical region(the lock for
which is already acquired) it enters the blocked or wait state. The
process continues to wait in the main memory and does not require CPU.
Once the I/O operation is completed the process goes to the ready state.
• Terminated or completed – Process is killed as well as PCB is deleted.
• Suspend ready – Process that was initially in the ready state but was
swapped out of main memory(refer Virtual Memory topic) and placed
onto external storage by scheduler is said to be in suspend ready state.
The process will transition back to ready state whenever the process is
again brought onto the main memory.
• Suspend wait or suspend blocked – Similar to suspend ready but uses
the process which was performing I/O operation and lack of main
memory caused them to move to secondary memory. When work is
finished it may go to suspend ready.
23
Here are the different states of a process in an operating system:
• New: When a process is created, it enters a new state. In this state, the
operating system allocates the necessary resources to the process, such
as memory and CPU time.
• Ready: When the process has been allocated all the necessary resources
and is waiting for the CPU to execute it, it enters the ready state. In this
state, the process is waiting to be scheduled for execution.
• Running: When the process is selected by the operating system to
execute on the CPU, it enters the running state. In this state, the process
is executing its instructions on the CPU.
• Waiting: When a process is waiting for an event to occur, such as
input/output operations, it enters the waiting state. In this state, the
process is waiting for the event to occur before it can continue
executing.
• Terminated: When a process has finished executing, or if it is
terminated by the operating system, it enters the terminated state. In
this state, the operating system frees the resources that were allocated
to the process.
• A process can move from the running state to the waiting state if it
needs to wait for a resource to become available.
• A process can move from the waiting state to the ready state when the
resource it was waiting for becomes available.
• A process can move from the ready state to the running state when it is
selected by the operating system for execution.
• The scheduling algorithm used by the operating system determines
which process is selected to execute from the ready state.
• The operating system may also move a process from the running state
to the ready state to allow other processes to execute
Process Control :-
Process Control Block is a data structure that contains information of the
process related to it. The process control block is also known as a task control
block, entry of the process table, etc.
It is very important for process management as the data structuring for processes
is done in terms of the PCB. It also defines the current state of the operating
system.
Structure of the Process Control Block
The process control stores many data items that are needed for efficient process
management. Some of these data items are explained with the help of the given
diagram −
24
Operating System services for Process Management :-
There are different processes in operating system.So there is need to manage them
properly.This article will tell you how an operating system manages different
processes.
This post will discuss the different process states, different process operations,
context switching, and Process management by modern operating systems. So in
this article we will learn what is process management in os.
25
1. System processes
System processes are started by the operating system, and they usually have
something to do with running the OS itself.
2. User processes
User processes, as you might guess, are those that the user starts. The operating
system though handles various system processes, but it mainly handles the user
code execution.
Each process can run independently of other processes and have its own memory
space. This makes process management tricky, as the OS needs to ensure that each
process gets its fair share of resources (memory, CPU time, etc.) while ensuring
that the system remains stable.
Process management
It is an important part of the operating system. It allows you to control the way
your computer runs by managing the currently active processes. This includes
ending processes that are no longer needed, setting process priorities, and more.
You can do it on your computer also.
There are a few ways to manage your processes. The first is through the use of
Task Manager. This allows you to see all of the processes currently running on
your computer and their current status and CPU/memory usage. You can end any
process that you no longer need, set a process priority, or start or stop a service.
Process Scheduling:-
The process manager’s activity is process scheduling, which involves removing
the running process from the CPU and selecting another process based on a
specific strategy.
Multiprogramming OS’s process scheduling is critical. Multiple processes could
be loaded into the executable memory at the same time in such operating systems,
and the loaded processes share the CPU utilising temporal multiplexing.
All PCBs (Process Scheduling Blocks) are kept in Process Scheduling Queues by the
OS. Each processing state has its own queue in the OS, and PCBs from all processes
in the same execution state are put in the very same queue. A process’s PCB is
unlinked from its present queue and then moved to its next state queue when its
status changes.
The following major process scheduling queues are maintained by the Operating
System:
Job queue – It contains all of the system’s processes.
Ready queue – This queue maintains a list of all processes in the main memory
that are ready to run. This queue is always filled with new processes.
Device queue – This queue is made up of processes that are stalled owing to the
lack of an I/O device.
26
Each queue can be managed by the OS using distinct policies (FIFO, Priority, Round
Robin, etc.). The OS scheduler governs how tasks are moved between the ready
and run queues, each of which can only have one item per processor core on a
system; it has been integrated with the CPU in the preceding figure.
• The Job Queue stores all processes that are entered into the system.
• The Ready Queue holds processes in the ready state.
• Device Queues hold processes that are waiting for any device to become
available. For each I/O device, there are separate device queues.
The ready queue is where a new process is initially placed. It sits in the ready
queue, waiting to be chosen for execution or dispatched. One of the following
occurrences can happen once the process has been assigned to the CPU and is
running:
• The process could send out an I/O request before being placed in the I/O
queue.
• The procedure could start a new one and then wait for it to finish.
• As a result of an interrupt, the process could be forcibly removed from the
CPU and returned to the ready queue.
process creation:-
A process can create several new processes through creating process system calls
during the process execution. Creating a process we call it the parent process and
the new process is a child process.
Every new process creates another process forming a tree-like structure. It can be
identified with a unique process identifier that usually represents it as pid which
is typically an integer number. Every process needs some resources like CPU time,
memory, file, I/O devices to accomplish.
Whenever a process creates a sub process, and may be each sub process is able
to obtain its resources directly from the operating system or from the resources
of the parent process. The parent process needs to partition its resources among
all its children or it may be able to share some resources to several children.
Restricting a child process to a subset of the parent’s resources prevents any
process from overloading the system by creating too many sub-processes. A
process is going to obtain its resources whenever it is created.
Let us consider a tree of process on a typical Solaris system as follows −
27
Whenever a process creates a new process, there are two possibilities in terms of
execution, which are as follows −
• The parent continues to execute concurrently with its children.
• The parent waits till some or all its children have terminated.
There are two more possibilities in terms of address space of the new process,
which are as follows −
• The child process is a duplicate of the parent process.
• The child process has a new program loaded into it.
process termination:-
Whenever the process finishes executing its final statement and asks the
operating system to delete it by using exit() system call.
At that point of time the process may return the status value to its parent process
with the help of wait() system call.
All the resources of the process including physical and virtual memory, open
files, I/O buffers are deallocated by the operating system.
Reasons for process termination
The reasons that the process may terminate the execution of one of its children
are as follows −
• The child exceeds its usage of resources that it has been allocated.
• The task that is assigned to the child is no longer required.
• The parent is exiting and the operating system does not allow a child
to continue if its parent terminates.
28
Some systems, including VMS, do not allow a child to exist if its parent has
terminated. In such systems, if a process terminates either normally or
abnormally, then all its children have to be terminated. This concept is referred
to as cascading termination.
Causes for termination
The other causes for termination are as follows −
• Time slot expired − When the process execution does not complete
within the time quantum, then the process gets terminated from the
running state. The CPU picks the next job in the ready queue to
execute.
• Memory bound violation − If a process needs more memory than the
available memory.
• I/O failure − When the operating system does not provide an I/O
device, the process enters a waiting state.
• Process request − If the parent process requests the child process
about termination.
• Invalid instruction
Thread:-
Within a program, a thread is a separate execution path. It is a lightweight
process that the operating system can schedule and run concurrently with
other threads. The operating system creates and manages threads, and they
share the same memory and resources as the program that created them.
This enables multiple threads to collaborate and work efficiently within a
single program.
29
3. Effective utilization of multiprocessor system: If we have multiple
threads in a single process, then we can schedule multiple threads on
multiple processor. This will make process execution faster.
4. Resource sharing: Resources like code, data, and files can be shared
among all threads within a process. Note: stack and registers can’t be
shared among the threads. Each thread has its own stack and registers.
5. Communication: Communication between multiple threads is easier, as
the threads shares common address space. while in process we have to
follow some specific communication technique for communication
between two process.
6. Enhanced throughput of the system: If a process is divided into
multiple threads, and each thread function is considered as one job,
then the number of jobs completed per unit of time is increased, thus
increasing the throughput of the system.
Types of Threads
There are two types of threads:
• User Level Thread
• Kernel Level Thread
Multithreading Model:-
Multithreading allows the application to divide its task into individual threads. In
multi-threads, the same process or task can be done by the number of threads, or
we can say that there is more than one thread to perform the task in
multithreading. With the use of multithreading, multitasking can be achieved.
The main drawback of single threading systems is that only one task can be
performed at a time, so to overcome the drawback of this single threading, there
is multithreading that allows multiple tasks to be performed.
For example:
55M
995
Prime Ministers of India | List of Prime Minister of India (1947-2020)
30
In the above example, client1, client2, and client3 are accessing the web server
without any waiting. In multithreading, several tasks can run at the same time.
In an operating system, threads are divided into the user-level thread and the
Kernel-level thread. User-level threads handled independent form above the
kernel and thereby managed without any kernel support. On the opposite hand,
the operating system directly manages the kernel-level threads. Nevertheless,
there must be a form of relationship between user-level and kernel-level threads.
The many to one model maps many user levels threads to one kernel thread. This
type of relationship facilitates an effective context-switching environment, easily
implemented even on the simple kernel with no thread support.
The disadvantage of this model is that since there is only one kernel-level thread
schedule at any given time, this model cannot take advantage of the hardware
acceleration offered by multithreaded processes or multi-processor systems. In
this, all the thread management is done in the userspace. If blocking comes, this
model blocks the whole system.
31
In the above figure, the many to one model associates all user-level threads to
single kernel-level threads.
32
In the above figure, one model associates that one user-level thread to a single
kernel-level thread.
In this type of model, there are several user-level threads and several kernel-level
threads. The number of kernel threads created depends upon a particular
application. The developer can create as many threads at both levels but may not
be the same. The many to many model is a compromise between the other two
models. In this model, if any thread makes a blocking system call, the kernel can
schedule another thread for execution. Also, with the introduction of multiple
threads, complexity is not present as in the previous models. Though this model
allows the creation of multiple kernel threads, true concurrency cannot be
achieved by this model. This is because the kernel can schedule only one process
at a time.
Types of Threads:
1. User Level thread (ULT) – Is implemented in the user level library, they
are not created using the system calls. Thread switching does not need
to call OS and to cause interrupt to Kernel. Kernel doesn’t know about
the user level thread and manages them as if they were single-threaded
processes.
• Advantages of ULT –
33
• Can be implemented on an OS that doesn’t support
multithreading.
• Simple representation since thread has only program
counter, register set, stack space.
• Simple to create since no intervention of kernel.
• Thread switching is fast since no OS calls need to be
made.
• Limitations of ULT –
• No or less co-ordination among the threads and
Kernel.
• If one thread causes a page fault, the entire process
blocks.
2. Kernel Level Thread (KLT) – Kernel knows and manages the threads.
Instead of thread table in each process, the kernel itself has thread table
(a master one) that keeps track of all the threads in the system. In
addition kernel also maintains the traditional process table to keep track
of the processes. OS kernel provides system call to create and manage
threads.
• Advantages of KLT –
• Since kernel has full knowledge about the threads in
the system, scheduler may decide to give more time to
processes having large number of threads.
• Good for applications that frequently block.
• Limitations of KLT –
• Slow and inefficient.
• It requires thread control block so it is an overhead.
Process Scheduler:-
The process manager’s activity is process scheduling, which involves removing
the running process from the CPU and selecting another process based on a
specific strategy. The scheduler’s purpose is to implement the virtual machine so
that each process appears to be running on its own computer to the user.
34
Multiprogramming OS’s process scheduling is critical. Multiple processes could
be loaded into executable memory at the same time in such an OS, and the loaded
processes share the CPU utilising temporal multiplexing.
CPU scheduler is another name for Short-Term scheduler. It chooses one job from
the ready queue and then sends it to the CPU for processing.
To determine which work will be dispatched for execution, a scheduling method
is utilised. The Short-Term scheduler’s task can be essential in the sense that if it
chooses a job with a long CPU burst time, all subsequent jobs will have to wait in
a ready queue for a long period. This is known as hunger, and it can occur if the
Short-Term scheduler makes a mistake when selecting the work.
35
3. Medium-Term Scheduler
36
Wait time of each process is as follows −
P0 0-0=0
P1 5-1=4
P2 8-2=6
P3 16 - 3 = 13
P0 0 5 0
P1 1 3 5
P2 2 8 14
37
P3 3 6 8
P0 0-0=0
P1 5-1=4
P2 14 - 2 = 12
P3 8-3=5
P0 0 5 1 0
P1 1 3 2 11
38
P2 2 8 1 14
P3 3 6 3 5
P0 0-0=0
P1 11 - 1 = 10
P2 14 - 2 = 12
P3 5-3=2
39
Wait time of each process is as follows −
P0 (0 - 0) + (12 - 3) = 9
P1 (3 - 1) = 2
P3 (9 - 3) + (17 - 12) = 11
40
6. Minimum response time
There are the following algorithms which can be used to schedule the jobs.
It is the simplest algorithm to implement. The process with the minimal arrival
time will get the CPU first. The lesser the arrival time, the sooner will the process
gets the CPU. It is the non-preemptive type of scheduling.
2. Round Robin
In the Round Robin scheduling algorithm, the OS defines a time quantum (slice).
All the processes will get executed in the cyclic way. Each of the process will get
the CPU for a small amount of time (called time quantum) and then get back to the
ready queue to wait for its next turn. It is a preemptive type of scheduling.
PlayNext
Unmute
Duration 18:10
Loaded: 0.37%
Â
Fullscreen
Backward Skip 10sPlay VideoForward Skip 10s
3. Shortest Job First
The job with the shortest burst time will get the CPU first. The lesser the burst
time, the sooner will the process get the CPU. It is the non-preemptive type of
scheduling.
It is the preemptive form of SJF. In this algorithm, the OS schedules the Job
according to the remaining time of the execution.
In this algorithm, the priority will be assigned to each of the processes. The higher
the priority, the sooner will the process get the CPU. If the priority of the two
processes is same then they will be scheduled according to their arrival time.
In this scheduling Algorithm, the process with highest response ratio will be
scheduled next. This reduces the starvation in the system.
Process Synchronization:-
Process Synchronization is the coordination of execution of multiple processes
in a multi-process system to ensure that they access shared resources in a
41
controlled and predictable manner. It aims to resolve the problem of race
conditions and other synchronization issues in a concurrent system.
The main objective of process synchronization is to ensure that multiple
processes access shared resources without interfering with each other, and to
prevent the possibility of inconsistent data due to concurrent access. To achieve
this, various synchronization techniques such as semaphores, monitors, and
critical sections are used.
In a multi-process system, synchronization is necessary to ensure data
consistency and integrity, and to avoid the risk of deadlocks and other
synchronization problems. Process synchronization is an important aspect of
modern operating systems, and it plays a crucial role in ensuring the correct and
efficient functioning of multi-process systems.
On the basis of synchronization, processes are categorized as one of the
following two types:
• Independent Process: The execution of one process does not affect the
execution of other processes.
• Cooperative Process: A process that can affect or be affected by other
processes executing in the system.
Process synchronization problem arises in the case of Cooperative process also
because resources are shared in Cooperative processes.
Race Condition:
When more than one process is executing the same code or accessing the same
memory or any shared variable in that condition there is a possibility that the
output or the value of the shared variable is wrong so for that all the processes
doing the race to say that my output is correct this condition known as a race
condition. Several processes access and process the manipulations over the same
data concurrently, then the outcome depends on the particular order in which
the access takes place. A race condition is a situation that may occur inside a
critical section. This happens when the result of multiple thread execution in the
critical section differs according to the order in which the threads execute. Race
conditions in critical sections can be avoided if the critical section is treated as
an atomic instruction. Also, proper thread synchronization using locks or atomic
variables can prevent race conditions.
A critical section is a code segment that can be accessed by only one process at
a time. The critical section contains shared variables that need to be
synchronized to maintain the consistency of data variables. So the critical
section problem means designing a way for cooperative processes to access
shared resources without creating data inconsistencies.
42
In the entry section, the process requests for entry in the Critical Section.
Any solution to the critical section problem must satisfy three requirements:
• Mutual Exclusion: If a process is executing in its critical section, then
no other process is allowed to execute in the critical section.
• Progress: If no process is executing in the critical section and other
processes are waiting outside the critical section, then only those
processes that are not executing in their remainder section can
participate in deciding which will enter in the critical section next, and
the selection can not be postponed indefinitely.
• Bounded Waiting: A bound must exist 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.
Peterson’s Solution:
43
Peterson’s Solution preserves all three conditions:
• Mutual Exclusion is assured as only one process can access the critical
section at any time.
• Progress is also assured, as a process outside the critical section does
not block other processes from entering the critical section.
• Bounded Waiting is preserved as every process gets a fair chance.
Disadvantages of Peterson’s solution:
• It involves busy waiting.(In the Peterson’s solution, the code statement-
“while(flag[j] && turn == j);” is responsible for this. Busy waiting is not
favored because it wastes CPU cycles that could be used to perform
other tasks.)
• It is limited to 2 processes.
• Peterson’s solution cannot be used in modern CPU architectures.
Semaphores:
44
processes can share the same mutex semaphore that is initialized to 1.
Then, a process has to wait until the lock becomes 0. Then, the process
can make the mutex semaphore 1 and start its critical section. When it
completes its critical section, it can reset the value of the mutex
semaphore to 0 and some other process can enter its critical section.
• Counting Semaphores: They can have any value and are not restricted
over a certain domain. They can be used to control access to a resource
that has a limitation on the number of simultaneous accesses. The
semaphore can be initialized to the number of instances of the resource.
Whenever a process wants to use that resource, it checks if the number
of remaining instances is more than zero, i.e., the process has an
instance available. Then, the process can enter its critical section
thereby decreasing the value of the counting semaphore by 1. After the
process is over with the use of the instance of the resource, it can leave
the critical section thereby adding 1 to the number of available
instances of the resource.
45
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. Processes can communicate with each other through both:
1. Shared Memory
2. Message passing
Figure 1 below shows a basic structure of communication between processes via
the shared memory method and via the message passing method.
46
items and the Consumer consumes that item. The two processes share a common
space or memory location known as a buffer where the item produced by the
Producer is stored and from which the Consumer consumes the item if needed.
There are two versions of this problem: the first one is known as the unbounded
buffer problem in which the Producer can keep on producing items and there is
no limit on the size of the buffer, the second one is known as the bounded buffer
problem in which the Producer can produce up to a certain number of items
before it starts waiting for Consumer to consume it. We will discuss the bounded
buffer problem. First, the Producer and the Consumer will share some common
memory, then the producer will start producing items. If the total produced item
is equal to the size of the buffer, the producer will wait to get it consumed by
the Consumer. Similarly, the consumer will first check for the availability of the
item. If no item is available, the Consumer will wait for the Producer to produce
it. If there are items available, Consumer will consume them. The pseudo-code
to demonstrate is provided below:
Shared Data between the two Processes
#define buff_max 25
#define mod %
struct item{
item nextProduced;
while(1){
// check if there is no space
// for production.
// if so keep waiting.
while((free_index+1) mod buff_max == full_index)
shared_buff[free_index] = nextProduced;
free_index = (free_index + 1) mod buff_max;
}
item nextConsumed;
47
while(1){
nextConsumed = shared_buff[full_index];
full_index = (full_index + 1) mod buff_max;
}
In the above code, the Producer will start producing again when the
(free_index+1) mod buff max will be free because if it is not free, this implies
that there are still items that can be consumed by the Consumer so there is no
need to produce more. Similarly, if free index and full index point to the same
index, this implies that there are no items to consume.
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
48
information like what to do if runs out of buffer space, sequence number,
priority. Generally, message is sent using FIFO style
Advantages of IPC:
Disadvantages of IPC:
49
Examples Of Deadlock
1. The system has 2 tape drives. P1 and P2 each hold one tape drive and
each needs another one.
2. Semaphores A and B, initialized to 1, P0, and P1 are in deadlock as
follows:
• P0 executes wait(A) and preempts.
• P1 executes wait(B).
• Now P0 and P1 enter in deadlock.
P0 P1
wait(A); wait(B)
wait(B); wait(A)
3. Assume the space is available for allocation of 200K bytes, and the
following sequence of events occurs.
P0 P1
Request Request
80KB; 70KB;
Request 60KB; Request 80KB;
Deadlock occurs if both processes progress to their second request.
Deadlock can arise if the following four conditions hold simultaneously
(Necessary Conditions)
Mutual Exclusion: Two or more resources are non-shareable (Only one process
can use at a time)
Hold and Wait: A process is holding at least one resource and waiting for
resources.
No Preemption: A resource cannot be taken from a process unless the process
releases the resource.
Circular Wait: A set of processes waiting for each other in circular form.
Methods for handling deadlock
There are three ways to handle deadlock
1) Deadlock prevention or avoidance:
Prevention:
The idea is to not let the system into a deadlock state. This system will make
sure that above mentioned four conditions will not arise. These techniques are
very costly so we use this in cases where our priority is making a system
deadlock-free.
One can zoom into each category individually, Prevention is done by negating
one of the above-mentioned necessary conditions for deadlock. Prevention can
be done in four different ways:
50
1. Eliminate mutual exclusion 3. Allow preemption
2. Solve hold and Wait 4. Circular wait
Solution
Avoidance:
Avoidance is kind of futuristic. By using the strategy of “Avoidance”, we have to
make an assumption. We need to ensure that all information about resources that
the process will need is known to us before the execution of the process. We use
Banker’s algorithm (Which is in turn a gift from Dijkstra) to avoid deadlock.
In prevention and avoidance, we get the correctness of data but performance
decreases.
2) Deadlock detection and recovery: If Deadlock prevention or avoidance is not
applied to the software then we can handle this by deadlock detection and
recovery. which consist of two phases:
1. In the first phase, we examine the state of the process and check
whether there is a deadlock or not in the system.
2. If found deadlock in the first phase then we apply the algorithm for
recovery of the deadlock.
In Deadlock detection and recovery, we get the correctness of data but
performance decreases.
3) Deadlock ignorance: If a deadlock is very rare, then let it happen and reboot
the system. This is the approach that both Windows and UNIX take. we use the
ostrich algorithm for deadlock ignorance.
In Deadlock, ignorance performance is better than the above two methods but
the correctness of data
data Access:-
When a file is used, information is read and accessed into computer memory and
there are several ways to access this information of the file. Some systems
provide only one access method for files. Other systems, such as those of IBM,
support many access methods, and choosing the right one for a particular
application is a major design problem.
There are three ways to access a file into a computer system: Sequential-Access,
Direct Access, Index sequential Method.
1. Sequential Access –
It is the simplest access method. Information in the file is processed in
order, one record after the other. This mode of access is by far the most
common; for example, editor and compiler usually access the file in this
fashion.
Read and write make up the bulk of the operation on a file. A read
operation -read next- read the next position of the file and automatically
advance a file pointer, which keeps track I/O location. Similarly, for the
-write next- append to the end of the file and advance to the newly
written material.
Key points:
• Data is accessed one record right after another record in an
order.
• When we use read command, it move ahead pointer by one
• When we use write command, it will allocate memory and move
the pointer to the end of the file
• Such a method is reasonable for tape.
51
• It is simple to implement this file access mechanism.
• It uses lexicographic order to quickly access the next entry.
Disadvantages of Sequential Access Method :
• If the file record that needs to be accessed next is not present next to
the current record, this type of file access method is slow.
• Moving a sizable chunk of the file may be necessary to insert a new
record.
2.Direct Access –
Another method is direct access method also known as relative access method. A
fixed-length logical record that allows the program to read and write record
rapidly. in no particular order. The direct access is based on the disk model of a
file since disk allows random access to any file block. For direct access, the file
is viewed as a numbered sequence of block or record. Thus, we may read block
14 then block 59, and then we can write block 17. There is no restriction on the
order of reading and writing for a direct access file.
A block number provided by the user to the operating system is normally
a relative block number, the first relative block of the file is 0 and then 1 and so
on.
52
We will study what is process synchronization in OS, what is synchronization in
OS, and what is the need for synchronization and we will explain in detail
process synchronization in this blog. And if you want in-depth knowledge about
operating systems to pursue a career, you can go for the Web Development
Course to understand the basics of operating systems and learn how to
use advanced tools to design and create dynamic websites.
The total balance remaining in your account after the transaction will be less
than x because you are taking money out of it. However, since the transaction
takes some time, the person interprets x as your account balance, resulting in
inconsistent data. We could guarantee consistent data if we could somehow
make sure that only one process was running at once.
Source – Scalar.com
In the above image, if Process1 and Process2 occur simultaneously, User 2 will
receive the incorrect account balance as Y as a result of Process1 being executed
when the balance is X. When multiple processes share a single resource in a
53
system, inconsistent data can result, which is why process synchronization is
required in the operating system.
Critical Section:-
In computer science, a critical section refers to a segment of code that is
executed by multiple concurrent threads or processes, and which accesses
shared resources. These resources may include shared memory, files, or other
system resources that can only be accessed by one thread or process at a time
to avoid data inconsistency or race conditions.
1. The critical section must be executed as an atomic operation, which
means that once one thread or process has entered the critical section,
all other threads or processes must wait until the executing thread or
process exits the critical section. The purpose of synchronization
mechanisms is to ensure that only one thread or process can execute
the critical section at a time.
2. The concept of a critical section is central to synchronization in
computer systems, as it is necessary to ensure that multiple threads or
processes can execute concurrently without interfering with each other.
Various synchronization mechanisms such as semaphores, mutexes,
monitors, and condition variables are used to implement critical
sections and ensure that shared resources are accessed in a mutually
exclusive manner.
The use of critical sections in synchronization can be advantageous in improving
the performance of concurrent systems, as it allows multiple threads or
processes to work together without interfering with each other. However, care
must be taken in designing and implementing critical sections, as incorrect
synchronization can lead to race conditions, deadlocks.
Critical Section:_
When more than one processes try to access the same code segment that
segment is known as the critical section. The critical section contains shared
variables or resources which are needed to be synchronized to maintain the
consistency of data variables.
In simple terms, a critical section is a group of instructions/statements or
regions of code that need to be executed atomically (read this post for atomicity),
such as accessing a resource (file, input or output port, global data, etc.)
In concurrent programming, if one thread tries to change the value of shared
data at the same time as another thread tries to read the value (i.e, data race
across threads), the result is unpredictable. The access to such shared variables
(shared memory, shared files, shared port, etc.) is to be synchronized.
Few programming languages have built-in support for synchronization. It is
critical to understand the importance of race conditions while writing kernel-
mode programming (a device driver, kernel thread, etc.) since the programmer
can directly access and modify kernel data structures
race condition:-
54
Race conditions are most commonly associated with computer science and
programming. They occur when two computer program processes, or threads,
attempt to access the same resource at the same time and cause problems in the
system.
There are a few types of race conditions. Two categories that define the impact of
the race condition on a system are referred to as critical and noncritical:
• A critical race condition will cause the end state of the device, system or
program to change. For example, if flipping two light switches connected
to a common light at the same time blows the circuit, it is considered a
critical race condition. In software, a critical race condition is when a
situation results in a bug with unpredictable or undefined behavior.
• A noncritical race condition does not directly affect the end state of the
system, device or program. In the light example, if the light is off and
flipping both switches simultaneously turns the light on and has the
same effect as flipping one switch, then it is a noncritical race condition.
In software, a noncritical race condition does not result in a bug.
55
account to process check A first and then look again to see if there are
enough funds to process check B after processing check A. However, if
the two checks are processed at the same time, the system may read the
same account balance value for both processes and give an incorrect
account balance value, causing the account to be overdrawn.
Classical problems:-
in this article, we will see number of 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 place 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,
4. Sleeping Barber Problem
These are summarized, for detailed explanation, you can view the linked articles
for each.
• 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.
• 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
56
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.
57
1. Bounded-buffer (or Producer-Consumer) Problem,
2. Dining-Philosophers Problem,
3. Readers and Writers Problem,
4. Sleeping Barber Problem
These are summarized, for detailed explanation, you can view the linked articles
for each.
• 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.
• 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.
58
• If a process is writing, no other process can read it.
Semaphores :-
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
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
The signal operation increments the value of its argument S.
signal(S)
{
S++;
}
59
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.
Advantages of Semaphores
Some of the advantages of semaphores are as follows −
• Semaphores allow only one process into the critical section. They
follow the mutual exclusion principle strictly and are much more
efficient than some other methods of synchronization.
• There is no resource wastage because of busy waiting in semaphores
as processor time is not wasted unnecessarily to check if a condition
is fulfilled to allow a process to access the critical section.
• Semaphores are implemented in the machine independent code of the
microkernel. So they are machine independent.
Disadvantages of Semaphores
Some of the disadvantages of semaphores are as follows −
60
Step 2 − For fixed size messages the system level implementation is straight
forward. It makes the task of programming more difficult.
Step 3 − The variable sized messages require a more system level implementation
but the programming task becomes simpler.
Step 4 − If process P1 and P2 want to communicate they need to send a message
to and receive a message from each other that means here a communication link
exists between them.
Step 5 − Methods for logically implementing a link and the send() and receive()
operations.
Given below is the structure of message passing technique −
Characteristics
The characteristics of Message passing model are as follows −
• Mainly the message passing is used for communication.
• It is used in distributed environments where the communicating
processes are present on remote machines which are connected with
the help of a network.
• Here no code is required because the message passing facility provides
a mechanism for communication and synchronization of actions that
are performed by the communicating processes.
• Message passing is a time consuming process because it is
implemented through kernel (system calls).
• It is useful for sharing small amounts of data so that conflicts need
not occur.
• In message passing the communication is slower when compared to
shared memory technique.
Deadlock:-
A process in operating system uses resources in the following way.
1. Requests a resource
2. Use the resource
3. Releases the resource
61
A deadlock is a situation where a set of processes are blocked because each
process is holding a resource and waiting for another resource acquired by some
other process.
Consider an example when two trains are coming toward each other on the same
track and there is only one track, none of the trains can move once they are in
front of each other. A similar situation occurs in operating systems when there
are two or more processes that hold some resources and wait for resources held
by other(s). For example, in the below diagram, Process 1 is holding Resource 1
and waiting for resource 2 which is acquired by process 2, and process 2 is
waiting for resource 1.
Examples Of Deadlock
1. The system has 2 tape drives. P1 and P2 each hold one tape drive and
each needs another one.
2. Semaphores A and B, initialized to 1, P0, and P1 are in deadlock as
follows:
• P0 executes wait(A) and preempts.
• P1 executes wait(B).
• Now P0 and P1 enter in deadlock.
P0 P1
wait(A); wait(B)
wait(B); wait(A)
3. Assume the space is available for allocation of 200K bytes, and the
following sequence of events occurs.
P0 P1
62
Request Request
80KB; 70KB;
63
Conditions for Deadlock in Operating System:-
Deadlock is a situation which involves the interaction of more than one resources
and processes with each other. We can visualise the occurrence of deadlock as a
situation where there are two people on a staircase. One is ascending the
staircase while the other is descending. The staircase is so narrow that it can
only fit one person at a time. As a result, one has to retreat while the other moves
on and uses the staircase. Once that person has finished, the other one can use
that staircase. But here, neither person is willing to retreat and waits for the other
to retreat. None of them are able to use the staircase. The people here are the
processes, and the staircase is the resource. When a process requests for the
resource that is been held another process which needs another resource to
continue, but is been held by the first process, then it is called
a deadlock. There
are four conditions necessary for the occurrence of a deadlock. They can be
understood with the help of the above illustrated example of staircase:
1. Mutual Exclusion: When two people meet in the landings, they can’t just
walk through because there is space only for one person. This condition
allows only one person (or process) to use the step between them (or
the resource) is the first condition necessary for the occurrence of the
deadlock.
2. Hold and Wait: When the two people refuse to retreat and hold their
ground, it is called holding. This is the next necessary condition for
deadlock.
3. No Preemption: For resolving the deadlock one can simply cancel one
of the processes for other to continue. But the Operating System doesn’t
do so. It allocates the resources to the processors for as much time as
is needed until the task is completed. Hence, there is no temporary
reallocation of the resources. It is the third condition for deadlock.
4. Circular Wait: When the two people refuse to retreat and wait for each
other to retreat so that they can complete their task, it is called circular
wait. It is the last condition for deadlock to occur.
64
As Banker’s algorithm using some kind of table like allocation, request, available
all that thing to understand what is the state of the system. Similarly, if you want
to understand the state of the system instead of using those table, actually tables
are very easy to represent and understand it, but then still you could even
represent the same information in the graph. That graph is called Resource
Allocation Graph (RAG).
So, resource allocation graph is explained to us what is the state of the system
in terms of processes and resources. Like how many resources are available,
how many are allocated and what is the request of each process. Everything can
be represented in terms of the diagram. One of the advantages of having a
diagram is, sometimes it is possible to see a deadlock directly by using RAG, but
then you might not be able to know that by looking at the table. But the tables
are better if the system contains lots of process and resource and Graph is better
if the system contains less number of process and resource.
We know that any graph contains vertices and edges. So RAG also contains
vertices and edges. In RAG vertices are two type –
1. Process vertex – Every process will be represented as a process
vertex.Generally, the process will be represented with a circle.
2. Resource vertex – Every resource will be represented as a resource vertex. It
is also two type –
• Single instance type resource – It represents as a box, inside the box,
there will be one dot.So the number of dots indicate how many instances
are present of each resource type.
• Multi-resource instance type resource – It also represents as a box,
inside the box, there will be many dots present.
Now coming to the edges of RAG.There are two types of edges in RAG –
1. Assign Edge – If you already assign a resource to a process then it is called
Assign edge.
2. Request Edge – It means in future the process might want some resource to
complete the execution, that is called request edge.
65
So, if a process is using a resource, an arrow is drawn from the resource node to
the process node. If a process is requesting a resource, an arrow is drawn from
the process node to the resource node.
Example 1 (Single instances RAG) –
If there is a cycle in the Resource Allocation Graph and each resource in the cycle
provides only one instance, then the processes will be in deadlock. For example,
if process P1 holds resource R1, process P2 holds resource R2 and process P1 is
waiting for R2 and process P2 is waiting for R1, then process P1 and process P2
will be in deadlock.
66
Here’s another example, that shows Processes P1 and P2 acquiring resources R1
and R2 while process P3 is waiting to acquire both resources. In this example,
there is no deadlock because there is no circular dependency.
So cycle in single-instance resource type is the sufficient condition for deadlock.
Example 2 (Multi-instances RAG) –
From the above example, it is not possible to say the RAG is in a safe state or in
an unsafe state.So to see the state of this RAG, let’s construct the allocation
matrix and request matrix.
67
• The total number of processes are three; P1, P2 & P3 and the total
number of resources are two; R1 & R2.
Allocation matrix –
• For constructing the allocation matrix, just go to the resources and see
to which process it is allocated.
• R1 is allocated to P1, therefore write 1 in allocation matrix and similarly,
R2 is allocated to P2 as well as P3 and for the remaining element just
write 0.
Request matrix –
• In order to find out the request matrix, you have to go to the process
and see the outgoing edges.
• P1 is requesting resource R2, so write 1 in the matrix and similarly, P2
requesting R1 and for the remaining element write 0.
So now available resource is = (0, 0).
Checking deadlock (safe or not) –
So, there is no deadlock in this RAG.Even though there is a cycle, still there
is no deadlock.Therefore in multi-instance resource cycle is not sufficient
condition for deadlock.
68
Above example is the same as the previous example except that, the
process P3 requesting for resource R1.
So the table becomes as shown in below.
So,the Available resource is = (0, 0), but requirement are (0, 1), (1, 0) and
(1, 0).So you can’t fulfill any one requirement.Therefore, it is in deadlock.
Therefore, every cycle in a multi-instance resource type graph is not a
deadlock, if there has to be a deadlock, there has to be a cycle.So, in case
of RAG with multi-instance resource type, the cycle is a necessary
condition for deadlock, but not sufficient.
Handling Deadlocks:-
Deadlock is a situation where a process or a set of processes is blocked, waiting
for some other resource that is held by some other waiting process. It is an
undesirable state of the system. The following are the four conditions that must
hold simultaneously for a deadlock to occur.
69
1. Mutual Exclusion – A resource can be used by only one process at a
time. If another process requests for that resource then the requesting
process must be delayed until the resource has been released.
2. Hold and wait – Some processes must be holding some resources in the
non-shareable mode and at the same time must be waiting to acquire
some more resources, which are currently held by other processes in
the non-shareable mode.
3. No pre-emption – Resources granted to a process can be released back
to the system only as a result of voluntary action of that process after
the process has completed its task.
4. Circular wait – Deadlocked processes are involved in a circular chain
such that each process holds one or more resources being requested by
the next process in the chain.
Methods of handling deadlocks: There are four approaches to dealing with
deadlocks.
1. Deadlock Prevention
2. Deadlock avoidance (Banker's Algorithm)
3. Deadlock detection & recovery
4. Deadlock Ignorance (Ostrich Method)
These are explained below.
1. Deadlock Prevention: The strategy of deadlock prevention is to design the
system in such a way that the possibility of deadlock is excluded. The indirect
methods prevent the occurrence of one of three necessary conditions of
deadlock i.e., mutual exclusion, no pre-emption, and hold and wait. The direct
method prevents the occurrence of circular wait. Prevention techniques
– Mutual exclusion – are supported by the OS. Hold and Wait – the condition can
be prevented by requiring that a process requests all its required resources at
one time and blocking the process until all of its requests can be granted at the
same time simultaneously. But this prevention does not yield good results
because:
• long waiting time required
• inefficient use of allocated resource
• A process may not know all the required resources in advance
No pre-emption – techniques for ‘no pre-emption are’
• If a process that is holding some resource, requests another resource
that can not be immediately allocated to it, all resources currently being
held are released and if necessary, request again together with the
additional resource.
• If a process requests a resource that is currently held by another
process, the OS may pre-empt the second process and require it to
release its resources. This works only if both processes do not have the
same priority.
Circular wait One way to ensure that this condition never holds is to impose a
total ordering of all resource types and to require that each process requests
resources in increasing order of enumeration, i.e., if a process has been allocated
resources of type R, then it may subsequently request only those resources of
types following R in ordering.
2. Deadlock Avoidance: The deadlock avoidance Algorithm works by
proactively looking for potential deadlock situations before they occur. It does
this by tracking the resource usage of each process and identifying conflicts that
could potentially lead to a deadlock. If a potential deadlock is identified, the
algorithm will take steps to resolve the conflict, such as rolling back one of the
processes or pre-emptively allocating resources to other processes. The
70
Deadlock Avoidance Algorithm is designed to minimize the chances of a
deadlock occurring, although it cannot guarantee that a deadlock will never
occur. This approach allows the three necessary conditions of deadlock but
makes judicious choices to assure that the deadlock point is never reached. It
allows more concurrency than avoidance detection A decision is made
dynamically whether the current resource allocation request will, if granted,
potentially lead to deadlock. It requires knowledge of future process requests.
Two techniques to avoid deadlock :
1. Process initiation denial
2. Resource allocation denial
Advantages of deadlock avoidance techniques:
• Not necessary to pre-empt and rollback processes
• Less restrictive than deadlock prevention
Disadvantages :
• Future resource requirements must be known in advance
• Processes can be blocked for long periods
• Exists a fixed number of resources for allocation
Banker’s Algorithm:
The Banker’s Algorithm is based on the concept of resource allocation graphs. A
resource allocation graph is a directed graph where each node represents a
process, and each edge represents a resource. The state of the system is
represented by the current allocation of resources between processes. For
example, if the system has three processes, each of which is using two resources,
the resource allocation graph would look like this:
Processes A, B, and C would be the nodes, and the resources they are using would
be the edges connecting them. The Banker’s Algorithm works by analyzing the
state of the system and determining if it is in a safe state or at risk of entering a
deadlock.
To determine if a system is in a safe state, the Banker’s Algorithm uses two
matrices: the available matrix and the need matrix. The available matrix contains
the amount of each resource currently available. The need matrix contains the
amount of each resource required by each process.
The Banker’s Algorithm then checks to see if a process can be completed without
overloading the system. It does this by subtracting the amount of each resource
used by the process from the available matrix and adding it to the need matrix.
If the result is in a safe state, the process is allowed to proceed, otherwise, it is
blocked until more resources become available.
The Banker’s Algorithm is an effective way to prevent deadlocks in
multiprogramming systems. It is used in many operating systems, including
Windows and Linux. In addition, it is used in many other types of systems, such
as manufacturing systems and banking systems.
The Banker’s Algorithm is a powerful tool for resource allocation problems, but
it is not foolproof. It can be fooled by processes that consume more resources
than they need, or by processes that produce more resources than they need.
Also, it can be fooled by processes that consume resources in an unpredictable
manner. To prevent these types of problems, it is important to carefully monitor
the system to ensure that it is in a safe state.
3. Deadlock Detection: Deadlock detection is used by employing an algorithm
that tracks the circular waiting and kills one or more processes so that the
deadlock is removed. The system state is examined periodically to determine if
71
a set of processes is deadlocked. A deadlock is resolved by aborting and
restarting a process, relinquishing all the resources that the process held.
• This technique does not limit resource access or restrict process action.
• Requested resources are granted to processes whenever possible.
• It never delays the process initiation and facilitates online handling.
• The disadvantage is the inherent pre-emption losses.
4. Deadlock Ignorance: In the Deadlock ignorance method the OS acts like the
deadlock never occurs and completely ignores it even if the deadlock occurs.
This method only applies if the deadlock occurs very rarely. The algorithm is
very simple. It says ” if the deadlock occurs, simply reboot the system and act
like the deadlock never occurred.” That’s why the algorithm is called the Ostrich
Algorithm.
Advantages:
• Ostrich Algorithm is relatively easy to implement and is effective in
most cases.
• It helps in avoiding the deadlock situation by ignoring the presence of
deadlocks.
Disadvantages:
• Ostrich Algorithm does not provide any information about the deadlock
situation.
• It can lead to reduced performance of the system as the system may be
blocked for a long time.
• It can lead to a resource leak, as resources are not released when the
system is blocked due to deadlock.
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.
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.
PlayNext
Unmute
72
Duration 18:10
Loaded: 0.37%
Â
Fullscreen
Backward Skip 10sPlay VideoForward Skip 10s
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.
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.
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
73
than one process which are holding one resource and waiting for other in the
cyclic order.
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.
!(Hold and wait) = !hold or !wait (negation of hold and wait is, either you don't
hold or you don't wait)
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.
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.
4. Circular Wait
To violate circular wait, we can assign a priority number to each of the resource.
A process can't request for a lesser priority resource. This ensures that not a single
process can request a resource which is being utilized by some other process and
no cycle will be formed.
74
Among all the methods, violating Circular wait is the only approach that can be
implemented practically.
Deadlock avoidance:-
In deadlock avoidance, the request for any resource will be granted if the resulting
state of the system doesn't cause deadlock in the system. The state of the system
will continuously be checked for safe and unsafe states.
In order to avoid deadlocks, the process must tell OS, the maximum number of
resources a process can request to complete its execution.
The simplest and most useful approach states that the process should declare the
maximum number of resources of each type it may ever need. The Deadlock
avoidance algorithm examines the resource allocations so that there can never be
a circular wait condition.
PlayNext
Unmute
Duration 18:10
Loaded: 0.37%
Â
Fullscreen
75
Backward Skip 10sPlay VideoForward Skip 10s
Resources Assigned
A 3 0 2 2
B 0 0 1 1
C 1 1 1 0
D 2 1 4 0
A 1 1 0 0
B 0 1 1 2
C 1 2 1 0
D 2 1 1 2
1. E = (7 6 8 4)
2. P = (6 2 8 3)
3. A = (1 4 0 1)
Above tables and vector E, P and A describes the resource allocation state of a
system. There are 4 processes and 4 types of the resources in a system. Table 1
shows the instances of each resource assigned to each process.
Table 2 shows the instances of the resources, each process still needs. Vector E is
the representation of total instances of each resource in the system.
76
A state of the system is called safe if the system can allocate all the resources
requested by all the processes without entering into deadlock.
If the system cannot fulfill the request of all processes then the state of the system
is called unsafe.
The key of Deadlock avoidance approach is when the request is made for
resources then the request must only be approved in the case if the resulting state
is also a safe state.
Deadlock Detection :
1. If resources have a single instance –
In this case for Deadlock detection, we can run an algorithm to check for the
cycle in the Resource Allocation Graph. The presence of a cycle in the graph is a
sufficient condition for deadlock.
In the above diagram, resource 1 and resource 2 have single instances. There is
a cycle R1 → P1 → R2 → P2. So, Deadlock is Confirmed.
. Process Termination:
To eliminate the deadlock, we can simply kill one or more processes. For this,
we use two methods:
77
• (a). Abort all the Deadlocked Processes:
Aborting all the processes will certainly break the deadlock, but with a
great expense. The deadlocked processes may have computed for a long
time and the result of those partial computations must be discarded and
there is a probability to recalculate them later.
2. Resource Preemption:
To eliminate deadlocks using resource preemption, we preempt some resources
from processes and give those resources to other processes. This method will
raise three issues –
• (a). Selecting a victim:
We must determine which resources and which processes are to be
preempted and also the order to minimize the cost.
• (b). Rollback:
We must determine what should be done with the process from which
resources are preempted. One simple idea is total rollback. That means
abort the process and restart it.
• (c). Starvation:
In a system, it may happen that same process is always picked as a
victim. As a result, that process will never complete its designated task.
This situation is called Starvation and must be avoided. One solution is
that a process must be picked as a victim only a finite number of times.
Memory Management :-
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 motive 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.
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.
78
Why Memory Management is required:
Virtual Memory :-
Virtual Memory is a storage allocation scheme in which secondary memory can
be addressed as though it were part of the main memory. The addresses a
program may use to reference memory are distinguished from the addresses the
memory system uses to identify physical storage sites, and program-generated
addresses are translated automatically to the corresponding machine addresses.
The size of virtual storage is limited by the addressing scheme of the computer
system and the amount of secondary memory is available not by the actual
number of the main storage locations.
It is a technique that is implemented using both hardware and software. It maps
memory addresses used by a program, called virtual addresses, into physical
addresses in computer memory.
1. All memory references within a process are logical addresses that are
dynamically translated into physical addresses at run time. This means
that a process can be swapped in and out of the main memory such that
it occupies different places in the main memory at different times
during the course of execution.
2. A process may be broken into a number of pieces and these pieces need
not be continuously located in the main memory during execution. The
combination of dynamic run-time address translation and use of page
or segment table permits this.
If these characteristics are present then, it is not necessary that all the pages or
segments are present in the main memory during execution. This means that the
required pages need to be loaded into memory whenever required. Virtual
memory is implemented using Demand Paging or Demand Segmentation
Memory management scheme:-
Memory management is the functionality of an operating system which
handles or manages primary memory and moves processes back and forth
between main memory and disk during execution. Memory management keeps
track of each and every memory location, regardless of either it is allocated
to some process or it is free. It checks how much memory is to be allocated to
processes. It decides which process will get memory at what time. It tracks
whenever some memory gets freed or unallocated and correspondingly it
updates the status.
Swapping
Swapping is a mechanism in which a process can be swapped temporarily out of
main memory (or move) to secondary storage (disk) and make that memory
available to other processes. At some later time, the system swaps back the
process from the secondary storage to main memory.
Though performance is usually affected by swapping process but it helps in
running multiple and big processes in parallel and that's the reason Swapping is
also known as a technique for memory compaction.
79
The total time taken by swapping process includes the time it takes to move the
entire process to a secondary disk and then to copy the process back to memory,
as well as the time the process takes to regain main memory.
Let us assume that the user process is of size 2048KB and on a standard hard disk
where swapping will take place has a data transfer rate around 1 MB per second.
The actual transfer of the 1000K process to or from memory will take
2048KB / 1024KB per second
= 2 seconds
= 2000 milliseconds
Now considering in and out time, it will take complete 4000 milliseconds plus
other overhead where the process competes to regain main memory.
Memory Allocation
Main memory usually has two partitions −
• Low Memory − Operating system resides in this memory.
• High Memory − User processes are held in high memory.
Operating system uses the following memory allocation mechanism.
80
S.N. Memory Allocation & Description
1 Single-partition allocation
In this type of allocation, relocation-register scheme is used to
protect user processes from each other, and from changing
operating-system code and data. Relocation register contains
value of smallest physical address whereas limit register contains
range of logical addresses. Each logical address must be less than
the limit register.
2 Multiple-partition allocation
In this type of allocation, main memory is divided into a number
of fixed-sized partitions where each partition should contain only
one process. When a partition is free, a process is selected from
the input queue and is loaded into the free partition. When the
process terminates, the partition becomes available for another
process.
Fragmentation
As processes are loaded and removed from memory, the free memory space is
broken into little pieces. It happens after sometimes that processes cannot be
allocated to memory blocks considering their small size and memory blocks
remains unused. This problem is known as Fragmentation.
Fragmentation is of two types −
1 External fragmentation
Total memory space is enough to satisfy a request or to reside a
process in it, but it is not contiguous, so it cannot be used.
2 Internal fragmentation
Memory block assigned to process is bigger. Some portion of
memory is left unused, as it cannot be used by another process.
The following diagram shows how fragmentation can cause waste of memory and
a compaction technique can be used to create more free memory out of
fragmented memory −
81
External fragmentation can be reduced by compaction or shuffle memory contents
to place all free memory together in one large block. To make compaction feasible,
relocation should be dynamic.
The internal fragmentation can be reduced by effectively assigning the smallest
partition but large enough for the process.
Paging
A computer can address more memory than the amount physically installed on
the system. This extra memory is actually called virtual memory and it is a section
of a hard that's set up to emulate the computer's RAM. Paging technique plays an
important role in implementing virtual memory.
Paging is a memory management technique in which process address space is
broken into blocks of the same size called pages (size is power of 2, between 512
bytes and 8192 bytes). The size of the process is measured in the number of pages.
Similarly, main memory is divided into small fixed-sized blocks of (physical)
memory called frames and the size of a frame is kept the same as that of a page
to have optimum utilization of the main memory and to avoid external
fragmentation.
82
Address Translation
Page address is called logical address and represented by page number and
the offset.
Logical Address = Page number + page offset
Frame address is called physical address and represented by a frame
number and the offset.
Physical Address = Frame number + page offset
A data structure called page map table is used to keep track of the relation
between a page of a process to a frame in physical memory.
83
When the system allocates a frame to any page, it translates this logical address
into a physical address and create entry into the page table to be used throughout
execution of the program.
When a process is to be executed, its corresponding pages are loaded into any
available memory frames. Suppose you have a program of 8Kb but your memory
can accommodate only 5Kb at a given point in time, then the paging concept will
come into picture. When a computer runs out of RAM, the operating system (OS)
will move idle or unwanted pages of memory to secondary memory to free up RAM
for other processes and brings them back when needed by the program.
This process continues during the whole execution of the program where the OS
keeps removing idle pages from the main memory and write them onto the
secondary memory and bring them back when required by the program.
Advantages and Disadvantages of Paging
Here is a list of advantages and disadvantages of paging −
• Paging reduces external fragmentation, but still suffer from internal
fragmentation.
• Paging is simple to implement and assumed as an efficient memory
management technique.
• Due to equal size of the pages and frames, swapping becomes very
easy.
• Page table requires extra memory space, so may not be good for a
system having small RAM.
Segmentation
Segmentation is a memory management technique in which each job is divided
into several segments of different sizes, one for each module that contains pieces
that perform related functions. Each segment is actually a different logical address
space of the program.
When a process is to be executed, its corresponding segmentation are loaded into
non-contiguous memory though every segment is loaded into a contiguous block
of available memory.
84
Segmentation memory management works very similar to paging but here
segments are of variable-length where as in paging pages are of fixed size.
A program segment contains the program's main function, utility functions, data
structures, and so on. The operating system maintains a segment map table for
every process and a list of free memory blocks along with segment numbers, their
size and corresponding memory locations in main memory. For each segment, the
table stores the starting address of the segment and the length of the segment. A
reference to a memory location includes a value that identifies a segment and an
offset.
The paging system also has a problem with sharing as one of its design issues.
Now, simply share the pages in order to prevent having two distinct copies of the
same page stored in your memory at the same time. The fact that not all of the
pages can be shared presents a challenge, however.
Pages that can only be read are generally shareable, such as the text of a program;
however, data pages are not.
85
When two or more than two processes (referred to collectively as multiple
processes) share some code, it can result in a problem with shared pages.
Let's say that process X and process Y are both running the editor and sharing its
pages. What would happen?
If the scheduler makes the decision to remove process X from memory, evicting
all of its pages and filling the empty page frames with the other program will cause
process Y to generate a large number of page faults in order to restore them. If
the scheduler makes the decision to remove process Y from memory, evicting all
of its pages and filling the empty page frames with the other program.
86
• If Physical Address Space = 16 M words = 24 * 220 words, then Physical
Address = log2 224 = 24 bits
The mapping from virtual to physical address is done by the memory management
unit (MMU) which is a hardware device and this mapping is known as paging
technique.
• The Physical Address Space is conceptually divided into a number of
fixed-size blocks, called frames.
• The Logical address Space is also splitted into fixed-size blocks,
called pages.
• Page Size = Frame Size
Let us consider an example:
• Physical Address = 12 bits, then Physical Address Space = 4 K words
• Logical Address = 13 bits, then Logical Address Space = 8 K words
• Page size = frame size = 1 K words (assumption)
Addr
ess generated by CPU is divided into
• Page number(p): Number of bits required to represent the pages in
Logical Address Space or Page number
• Page offset(d): Number of bits required to represent particular word in a
page or page size of Logical Address Space or word number of a page or
page offset.
Physical Address is divided into
• Frame number(f): Number of bits required to represent the frame of
Physical Address Space or Frame number.
• Frame offset(d): Number of bits required to represent particular word in
a frame or frame size of Physical Address Space or word number of a
frame or frame offset.
The hardware implementation of page table can be done by using dedicated
registers. But the usage of register for the page table is satisfactory only if page
table is small. If page table contain large number of entries then we can use
TLB(translation Look-aside buffer), a special, small, fast look up hardware cache.
• The TLB is associative, high speed memory.
• Each entry in TLB consists of two parts: a tag and a value.
87
• When this memory is used, then an item is compared with all tags
simultaneously.If the item is found, then corresponding value is
returned.
88
Addre
ss generated by the CPU is divided into:
• Segment number (s): Number of bits required to represent the segment.
• Segment offset (d): Number of bits required to represent the size of the
segment.
Advantages of Segmentation –
• No Internal fragmentation.
Segment Table consumes less space in comparison to Page table in paging.
• As a complete module is loaded all at once, segmentation improves CPU
utilization.
• The user’s perception of physical memory is quite similar to
segmentation. Users can divide user programs into modules via
segmentation. These modules are nothing more than the separate
processes’ codes.
• The user specifies the segment size, whereas in paging, the hardware
determines the page size.
• Segmentation is a method that can be used to segregate data from
security operations.
• Flexibility: Segmentation provides a higher degree of flexibility than
paging. Segments can be of variable size, and processes can be designed
to have multiple segments, allowing for more fine-grained memory
allocation.
• Sharing: Segmentation allows for sharing of memory segments between
processes. This can be useful for inter-process communication or for
sharing code libraries.
• Protection: Segmentation provides a level of protection between
segments, preventing one process from accessing or modifying another
process’s memory segment. This can help increase the security and
stability of the system.
Disadvantage of Segmentation –
• As processes are loaded and removed from the memory, the free
memory space is broken into little pieces, causing External
fragmentation.
• Overhead is associated with keeping a segment table for each activity.
• Due to the need for two memory accesses, one for the segment table
and the other for main memory, access time to retrieve the instruction
increases.
89
• Fragmentation: As mentioned, segmentation can lead to external
fragmentation as memory becomes divided into smaller segments. This
can lead to wasted memory and decreased performance.
• Overhead: The use of a segment table can increase overhead and reduce
performance. Each segment table entry requires additional memory, and
accessing the table to retrieve memory locations can increase the time
needed for memory operations.
• Complexity: Segmentation can be more complex to implement and
manage than paging. In particular, managing multiple segments per
process can be challenging, and the potential for segmentation faults
can increase as a result
Virtual Memory:-
virtual Memory is a storage allocation scheme in which secondary memory can
be addressed as though it were part of the main memory. The addresses a
program may use to reference memory are distinguished from the addresses the
memory system uses to identify physical storage sites, and program-generated
addresses are translated automatically to the corresponding machine addresses.
The size of virtual storage is limited by the addressing scheme of the computer
system and the amount of secondary memory is available not by the actual
number of the main storage locations.
It is a technique that is implemented using both hardware and software. It maps
memory addresses used by a program, called virtual addresses, into physical
addresses in computer memory.
1. All memory references within a process are logical addresses that are
dynamically translated into physical addresses at run time. This means
that a process can be swapped in and out of the main memory such that
it occupies different places in the main memory at different times
during the course of execution.
2. A process may be broken into a number of pieces and these pieces need
not be continuously located in the main memory during execution. The
combination of dynamic run-time address translation and use of page
or segment table permits this.
If these characteristics are present then, it is not necessary that all the pages or
segments are present in the main memory during execution. This means that the
required pages need to be loaded into memory whenever required. Virtual
memory is implemented using Demand Paging or Demand Segmentation.
Demand Paging :
The process of loading the page into memory on demand (whenever page fault
occurs) is known as demand paging.
The process includes the following steps :
90
1. If the CPU tries to refer to a page that is currently not available in the
main memory, it generates an interrupt indicating a memory access
fault.
2. The OS puts the interrupted process in a blocking state. For the
execution to proceed the OS must bring the required page into the
memory.
3. The OS will search for the required page in the logical address space.
4. The required page will be brought from logical address space to physical
address space. The page replacement algorithms are used for the
decision-making of replacing the page in physical address space.
5. The page table will be updated accordingly.
6. The signal will be sent to the CPU to continue the program execution
and it will place the process back into the ready state.
Hence whenever a page fault occurs these steps are followed by the operating
system and the required page is brought into memory.
Advantages :
• More processes may be maintained in the main memory: Because we are
going to load only some of the pages of any particular process, there is
room for more processes. This leads to more efficient utilization of the
processor because it is more likely that at least one of the more
numerous processes will be in the ready state at any particular time.
• A process may be larger than all of the main memory: One of the most
fundamental restrictions in programming is lifted. A process larger than
the main memory can be executed because of demand paging. The OS
itself loads pages of a process in the main memory as required.
• It allows greater multiprogramming levels by using less of the available
(primary) memory for each process.
Page Fault Service Time :
The time taken to service the page fault is called page fault service time. The
page fault service time includes the time taken to perform all the above six
steps.
Let Main memory access time is: m
Page fault service time is: s
91
Page fault rate is : p
Then, Effective memory access time = (p*s) + (1-p)*m
Swapping:
Swapping a process out means removing all of its pages from memory, or
marking them so that they will be removed by the normal page replacement
process. Suspending a process ensures that it is not runnable while it is swapped
out. At some later time, the system swaps back the process from the secondary
storage to the main memory. When a process is busy swapping pages in and out
then this situation is called thrashing.
Thrashing :
92
At any given time, only a few pages of any process are in the main memory and
therefore more processes can be maintained in memory. Furthermore, time is
saved because unused pages are not swapped in and out of memory. However,
the OS must be clever about how it manages this scheme. In the steady-state
practically, all of the main memory will be occupied with process pages, so that
the processor and OS have direct access to as many processes as possible. Thus
when the OS brings one page in, it must throw another out. If it throws out a
page just before it is used, then it will just have to get that page again almost
immediately. Too much of this leads to a condition called Thrashing. The system
spends most of its time swapping pages rather than executing instructions. So a
good page replacement algorithm is required.
In the given diagram, the initial degree of multiprogramming up to some extent
of point(lambda), the CPU utilization is very high and the system resources are
utilized 100%. But if we further increase the degree of multiprogramming the
CPU utilization will drastically fall down and the system will spend more time
only on the page replacement and the time is taken to complete the execution of
the process will increase. This situation in the system is called thrashing.
Causes of Thrashing :
1. High degree of multiprogramming : If the number of processes keeps
on increasing in the memory then the number of frames allocated to
each process will be decreased. So, fewer frames will be available for
each process. Due to this, a page fault will occur more frequently and
more CPU time will be wasted in just swapping in and out of pages and
the utilization will keep on decreasing.
For example:
Let free frames = 400
Case 1: Number of process = 100
Then, each process will get 4 frames.
Case 2: Number of processes = 400
Each process will get 1 frame.
Case 2 is a condition of thrashing, as the number of processes is
increased, frames per process are decreased. Hence CPU time will be
consumed in just swapping pages.
2. Lacks of Frames: If a process has fewer frames then fewer pages of that
process will be able to reside in memory and hence more frequent
swapping in and out will be required. This may lead to thrashing. Hence
sufficient amount of frames must be allocated to each process in order
to prevent thrashing.
Recovery of Thrashing :
• Do not allow the system to go into thrashing by instructing the long-
term scheduler not to bring the processes into memory after the
threshold.
• If the system is already thrashing then instruct the mid-term scheduler
to suspend some of the processes so that we can recover the system
from thrashing.
Performance in Virtual Memory:
• Let p be the page fault rate( 0 <= p <= 1).
• if p = 0 no page faults
• if p =1, every reference is a fault.
Effective access time (EAT) = (1 -p)* Memory Access Time + p * Page fault time.
Note: Page fault time = page fault overhead + swap out + swap in +restart
overhead
93
The performance of a virtual memory management system depends on the total
number of page faults, which depend on “paging policies” and “frame allocation”
Frame Allocation:
Number of frames allocating to each process in either static or dynamic.
• Static Allocation: The number of frame allocation to a process is fixed.
• Dynamic Allocation: The number of frames allocated to a process
changes.
Paging Policies:
• Fetch Policy: It decides when a page should be loaded into memory.
• Replacement Policy: It decides which page in memory should be
replaced.
• Placement Policy: It decides where in memory should a page be loaded.
Demand Paging:-
However, deciding, which pages need to be kept in the main memory and which
need to be kept in the secondary memory, is going to be difficult because we
cannot say in advance that a process will require a particular page at particular
time.
Whenever any page is referred for the first time in the main memory, then that
page will be found in the secondary memory.
After that, it may or may not be present in the main memory depending upon the
page replacement algorithm which will be covered later in this tutorial.
1. First In First Out (FIFO): This is the simplest page replacement algorithm. In
this algorithm, the operating system keeps track of all pages in the memory in a
94
queue, the oldest page is in the front of the queue. When a page needs to be
replaced page in the front of the queue is selected for removal.
Example 1: Consider page reference string 1, 3, 0, 3, 5, 6, 3 with 3 page
frames.Find the number of page faults.
Initially, all slots are empty, so when 1, 3, 0 came they are allocated to the empty
slots —> 3 Page Faults.
when 3 comes, it is already in memory so —> 0 Page Faults. Then 5 comes, it is
not available in memory so it replaces the oldest page slot i.e 1. —>1 Page
Fault. 6 comes, it is also not available in memory so it replaces the oldest page
slot i.e 3 —>1 Page Fault. Finally, when 3 come it is not available so it replaces
0 1 page fault.
Belady’s anomaly proves that it is possible to have more page faults when
increasing the number of page frames while using the First in First Out (FIFO)
page replacement algorithm. For example, if we consider reference strings 3, 2,
1, 0, 3, 2, 4, 3, 2, 1, 0, 4, and 3 slots, we get 9 total page faults, but if we increase
slots to 4, we get 10-page faults.
2. Optimal Page replacement: In this algorithm, pages are replaced which would
not be used for the longest duration of time in the future.
Example-2: Consider the page references 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 3 with
4 page frame. Find number of page fault.
95
Initially, all slots are empty, so when 7 0 1 2 are allocated to the empty slots —
>4 Page faults
0 is already there so —> 0 Page fault. when 3 came it will take the place of 7
because it is not used for the longest duration of time in the future.—>1 Page
fault. 0 is already there so —> 0 Page fault. 4 will takes place of 1 —> 1 Page
Fault.
Now for the further page reference string —> 0 Page fault because they are
already available in the memory.
Optimal page replacement is perfect, but not possible in practice as the operating
system cannot know future requests. The use of Optimal Page replacement is to
set up a benchmark so that other replacement algorithms can be analyzed against
it.
3. Least Recently Used: In this algorithm, page will be replaced which is least
recently used.
Example-3: Consider the page reference string 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2,
3 with 4 page frames. Find number of page faults.
Initially, all slots are empty, so when 7 0 1 2 are allocated to the empty slots —
>4 Page faults
0 is already their so —> 0 Page fault. when 3 came it will take the place of 7
because it is least recently used —>1 Page fault
0 is already in memory so —> 0 Page fault.
4 will takes place of 1 —> 1 Page Fault
96
Now for the further page reference string —> 0 Page fault because they are
already available in the memory.
4. Most Recently Used (MRU): In this algorithm, page will be replaced which has
been used recently. Belady’s anomaly can occur in this algorithm.
Trashing:-
To know more clearly about thrashing, first, we need to know about page fault
and swapping.
o Page fault: We know every program is divided into some pages. A page fault
occurs when a program attempts to access data or code in its address space
but is not currently located in the system RAM.
o Swapping: Whenever a page fault happens, the operating system will try to
fetch that page from secondary memory and try to swap it with one of the
pages in RAM. This process is called swapping.
Thrashing is when the page fault and swapping happens very frequently at a
higher rate, and then the operating system has to spend more time swapping these
pages. This state in the operating system is known as thrashing. Because of
thrashing, the CPU utilization is going to be reduced or negligible.s
The basic concept involved is that if a process is allocated too few frames, then
there will be too many and too frequent page faults. As a result, no valuable work
would be done by the CPU, and the CPU utilization would fall drastically.
97
The long-term scheduler would then try to improve the CPU utilization by loading
some more processes into the memory, thereby increasing the degree of
multiprogramming. Unfortunately, this would result in a further decrease in the
CPU utilization, triggering a chained reaction of higher page faults followed by an
increase in the degree of multiprogramming, called thrashing.
98
The basic concept involved is that if a process is allocated to few frames, then
there will be too many and too frequent page faults. As a result, no useful work
would be done by the CPU and the CPU utilization would fall drastically. The long-
term scheduler would then try to improve the CPU utilization by loading some
more processes into the memory thereby increasing the degree of multi
programming. This would result in a further decrease in the CPU utilization
triggering a chained reaction of higher page faults followed by an increase in the
degree of multi programming, called Thrashing.
Locality Model –
A locality is a set of pages that are actively used together. The locality model states
that as a process executes, it moves from one locality to another. A program is
generally composed of several different localities which may overlap.
For example, when a function is called, it defines a new locality where memory
references are made to the instructions of the function call, it’s local and global
variables, etc. Similarly, when the function is exited, the process leaves this
locality.
Techniques to handle:
1. Working Set Model –
This model is based on the above-stated concept of the Locality Model.
The basic principle states that if we allocate enough frames to a process to
accommodate its current locality, it will only fault whenever it moves to some new
locality. But if the allocated frames are lesser than the size of the current locality,
the process is bound to thrash.
According to this model, based on parameter A, the working set is defined as the
set of pages in the most recent ‘A’ page references. Hence, all the actively used
pages would always end up being a part of the working set.
The accuracy of the working set is dependent on the value of parameter A. If A is
too large, then working sets may overlap. On the other hand, for smaller values of
A, the locality might not be covered entirely.
If D is the total demand for frames and is the working set size for process
i,
Now, if ‘m’ is the number of frames available in the memory, there are 2
possibilities:
• (i) D>m i.e. total demand exceeds the number of frames, then thrashing
will occur as some processes would not get enough frames.
• (ii) D<=m, then there would be no thrashing.
2. Page Fault Frequency –
A more direct approach to handling thrashing is the one that uses the Page-Fault
Frequency concept.
99
The problem associated with Thrashing is the high page fault rate and thus, the
concept here is to control the page fault rate.
If the page fault rate is too high, it indicates that the process has too few frames
allocated to it. On the contrary, a low page fault rate indicates that the process
has too many frames.
Upper and lower limits can be established on the desired page fault rate as shown
in the diagram.
If the page fault rate falls below the lower limit, frames can be removed from the
process. Similarly, if the page fault rate exceeds the upper limit, more frames can
be allocated to the process.
In other words, the graphical state of the system should be kept limited to the
rectangular region formed in the given diagram.
Here too, if the page fault rate is high with no free frames, then some of the
processes can be suspended and frames allocated to them can be reallocated to
other processes. The suspended processes can then be restarted later.
62
100