0% found this document useful (0 votes)
34 views136 pages

Overview of Computer Systems and OS

The document provides an overview of computer systems, focusing on the definitions and functions of software, particularly operating systems (OS). It explains the roles of application and system software, details the functionalities of an OS such as memory, device, and file management, and outlines the history and types of operating systems. Additionally, it discusses the advantages and disadvantages of various OS types including batch, time-sharing, and distributed systems.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views136 pages

Overview of Computer Systems and OS

The document provides an overview of computer systems, focusing on the definitions and functions of software, particularly operating systems (OS). It explains the roles of application and system software, details the functionalities of an OS such as memory, device, and file management, and outlines the history and types of operating systems. Additionally, it discusses the advantages and disadvantages of various OS types including batch, time-sharing, and distributed systems.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

UNIT 1 - INTRODUCTION

Computer System - A computer system is a set of integrated devices the input, output, process, and store data and
information. Basically, computer system mainly consists of hardware, software and data.

Software - Computer software, or only software, is a kind of program that enables a user to perform some specific task or
used to operate a computer. It directs all the peripheral devices on the computer system – what to do and how to perform a
task. PC software plays the role of mediator between the user and computer hardware.

Software can be further divided into mainly two parts:

1. APPLICATION SOFTWARE - Application software also called end-user programs or merely an application. The end-user
uses application software for a specific purpose. It is programmed for simple as well as complex tasks.

It can either be installed or accessed online. It can be a single program or a group of small programs that referred to as an
application suite. Application software can be used by the user to complete specific tasks, such as creating word processors
documents, spreadsheets, presentations, graphics, sending the emails, etc.

2. SYSTEM SOFTWARE - System software is a collection of programs designed to operate, control, and extend the
processing capabilities of the computer itself. These software products comprise of programs written in low-level languages,
which interact with the hardware at a very basic level.

System software serves as the interface between the hardware and the end users. The best-known example of system
software is an operating system. Some examples of system software are Operating System, Compilers, Interpreter,
Assemblers, etc.

OPERATING SYSTEM (OS)

 An operating system is a program that acts as an interface between the user and the computer hardware and controls
the execution of all kinds of programs.

 An OS performs all the basic tasks like file management,


memory management, process management, handling input
and output, and controlling peripheral devices such as disk
drives and printers.

 It is not possible for the user to use any computer or mobile


device without having an operating system.

 The Software is the non-touchable parts of the computer, and


software‟s are those which are used for performing an
operation so that software‟s are just used for making an
application but hardware‟s are those which are used for
performing an operation.

 Operating system is software that is required in order to run


application programs and utilities.

 It works as a bridge to perform better interaction between


application programs and hardware of the computer.

 Examples of operating system are UNIX, MS-DOS, MS-


Windows –98/XP/Vista, Windows-NT/2000, OS/2 and Mac OS.

HOW OPERATING SYSTEM WORK?

 When you turn on the computer, the operating system program is loaded into the main memory. This program is called
the kernel.
 Once initialized, the system program is prepared to run the user programs and permits them to use the hardware
efficiently. For example, if we want to perform some paintings on the screen, then we must use the application software
as paint and hardware as a mouse for drawing an object.

 But how the system knows what to do when mouse moves on the screen and when the mouse draws a line on the
system so that operating system is necessary which interact between or which communicates with the hardware and the
software.

 Although it‟s easy to feel like a director in front of your desktop or laptop, there‟s a lot going on inside, and the real man
behind the curtain handling the necessary tasks is the operating system.

Two views of the operating system:

OS AS AN EXTENDED MACHINE or VIRTUAL MACHINE

 Operating systems perform two basically unrelated functions:

1. Providing a clean abstract set of resources instead of


the messy hardware to application programmers.
2. Managing these hardware resources.

 The architecture (instruction set, memory, I/O, and bus


structure) of most computers at the machine level
language is primitive and awkward to program,
especially for input/output operations.

 Users do not want to be involved in programming of


storage devices.

 Operating System provides a simple, high level


abstraction such that these devices contain a collection
of named files.
 Such files consist of useful piece of information like a digital photo, e mail messages, or web page.
 Operating System provides a set of basic commands or instructions to perform various operations such as read, write,
modify, save or close.
 Dealing with them is easier than directly dealing with hardware.
 Thus, Operating System hides the complexity of hardware and presents a beautiful interface to the users.
 Just as the operating system shields (protect from an unpleasant experience) the programmer from the disk hardware
presents a simple file-oriented interface.
 It also conceals a lot of unpleasant business concerning interrupts, timers, memory management, and other low level
features.
 In each case, the abstraction offered by the operating system is simpler and easier to use than that offered by the
underlying hardware.
 In this view, the function of the operating system is to present the user with the equivalent of an extended machine or
virtual machine that is easier to work with than the underlying hardware.
 The operating system provides a variety of services that programs can obtain using special instructions called system
calls.

SYSTEM CALLS - System calls provide an interface to the services made available by an operating system. Types of system
calls:

Process control(e.g., create and destroy processes)


File management(e.g., open and close files)
Device management(e.g., read and write operations)
Information maintenance(e.g., get time or date)
Communication(e.g., send and receive messages)

OS AS A RESOURCE MANAGER
 A computer has many resources (Hardware and Software), which may be required to complete a task.
 The commonly required resources are Input/output devices, Memory file storage space, and CPU (Central Processing
Unit) time and so on.
 The operating system acts as the manager of these resources and allocates them to specific programs and users as
necessary for their tasks.
 Therefore we can say an operating system is a resource allocator. This is the main features of an operating system.
 When a number of computers connected through a network more than one computers trying for a computer print or a
common resource, then the operating system follow same order and manage the resources in an efficient manner.

 Generally, resources sharing in two ways "in time" and "in space".
 When a resource is a time-sharing resource, first one of the tasks gets the resource for some time, then another and so
on.
 For example, a CPU in the time-sharing system. In the time-sharing system, OS fixes the time slot for the CPU. First one
of the processes gets the CPU, when the time slot expires, the CPU switch to the next process in the ready queue. In this
example, the CPU is a time resource.
 The other kind of sharing is the space sharing. In this method, the users share the space of resource.
 For example, the main memory consisting of several processes at a time, so the process of sharing the resources.
 The main difference between "in time" sharing resources and "in space" sharing resources is "in time" resources are not
divided into units, whereas "in space" sharing resources are divided into units.

FUNCTIONS OF OPERATING SYSTEM

1. It boots the computer.


2. It performs basic computer tasks e.g. managing the various peripheral devices e.g. mouse, keyboard.
3. It provides a user interface, e.g. command line, graphical user interface (GUI).
4. It handles system resources such as the computer's memory and sharing of the central processing unit (CPU) time by
various applications or peripheral devices.
5. It provides file management which refers to the way that the operating system manipulates, stores, retrieves, and saves
data.
6. Error Handling is done by the operating system. It takes preventive measures whenever required to avoid errors.

FUNCTIONALITIES OF AN OS

MEMORY MANAGEMENT

Memory management refers to management of Primary Memory or Main Memory.


Main memory is a large array of words or bytes where each word or byte has its own address.
Main memory provides a fast storage that can be accessed directly by the CPU.
For a program to be executed, it must be in the main memory.
An Operating System does the following activities for memory management:

1. Keeps tracks of primary memory, i.e., what part of it are in use by whom, what parts are not in use.
2. In multiprogramming, the OS decides which process will get memory when and how much.
3. Allocates the memory when a process requests it to do so.
4. De-allocates the memory when a process no longer needs it or has been terminated.

DEVICE MANAGEMENT

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

1. Keeps tracks of all devices. Program responsible for this task is known as the I/O controller.
2. Decides which process gets the device when and for how much time.
3. Allocates the device in the efficient way.
4. De-allocates devices.

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

1. Keeps tracks of processor and status of process. The program responsible for this task is known as traffic controller.
2. Allocates the processor (CPU) to a process.
3. De-allocates processor when a process is no longer required.

FILE MANAGEMENT

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

1. Keeps track of information, location, uses, status etc. The collective facilities are often known as file system.
2. Decides who gets the resources.
3. Allocates the resources.
4. De-allocates the resources.

OTHER IMPORTANT ACTIVITIES - Following are some of the important activities that an Operating System performs:

1. Security: By means of password and similar other techniques, it prevents unauthorized access to programs and data.
2. Control over system performance: Recording delays between request for a service and response from the system.
3. Job accounting: Keeping track of time and resources used by various jobs and users.
4. Error detecting aids: Production of dumps, traces, error messages, and other debugging and error detecting aids.
5. Coordination between other software and users: Coordination and assignment of compilers, interpreters, assemblers
and other software to the various users of the computer systems.

HISTORY OF THE OPERATING SYSTEM

The First Generation (1940 to early 1950s)

When the first electronic computer was developed in 1940, it was created without any operating system. In early times, users
have full access to the computer machine and write a program for each task in absolute machine language. The programmer
can perform and solve only simple mathematical calculations during the computer generation, and this calculation does not
require an operating system.

The Second Generation (1955 - 1965)

The first operating system (OS) was created in the early 1950s and was known as GMOS. General Motors has developed OS
for the IBM computer. The second-generation operating system was based on a single stream batch processing system
because it collects all similar jobs in groups or batches and then submits the jobs to the operating system using a punch card
to complete all jobs in a machine. At each completion of jobs (either normally or abnormally), control transfer to the
operating system that is cleaned after completing one job and then continues to read and initiates the next job in a punch
card. After that, new machines were called mainframes, which were very big and used by professional operators.

The Third Generation (1965 - 1980)

During the late 1960s, operating system designers were very capable of developing a new operating system that could
simultaneously perform multiple tasks in a single computer program called multiprogramming. The introduction
of multiprogramming plays a very important role in developing operating systems that allow a CPU to be busy every time
by performing different tasks on a computer at the same time. During the third generation, there was a new development of
minicomputer's phenomenal growth starting in 1961 with the DEC PDP-1. These PDP's leads to the creation of personal
computers in the fourth generation.

The Fourth Generation (1980 - Present Day)

The fourth generation of operating systems is related to the development of the personal computer. However, the personal
computer is very similar to the minicomputers that were developed in the third generation. The cost of a personal computer
was very high at that time; there were small fractions of minicomputers costs. A major factor related to creating personal
computers was the birth of Microsoft and the Windows operating system. Microsoft created the first window operating
system in 1975. After introducing the Microsoft Windows OS, Bill Gates and Paul Allen had the vision to take personal
computers to the next level. Therefore, they introduced the MS-DOS in 1981; however, it was very difficult for the person to
understand its cryptic commands. Today, Windows has become the most popular and most commonly used operating
system technology. And then, Windows released various operating systems such as Windows 95, Windows 98, Windows XP
and the latest operating system, Windows 7. Currently, most Windows users use the Windows 10 operating system. Besides
the Windows operating system, Apple is another popular operating system built in the 1980s, and this operating system was
developed by Steve Jobs, a co-founder of Apple. They named the operating system Macintosh OS or Mac OS.

BRIEF HISTORY OF OS DEVELOPMENT

LIST OF COMMON OPERATING SYSTEMS

Given below is a list of commonly used Operating systems along with their year of release.

Name of the OS Release Date


Android 2008
iOS 2007
Windows 1985
Mac OS 2001
MS-DOS 1981
Chrome OS 2011
Windows Phone 2010
Blackberry OS 1999
Firefox OS 2013
UNIX 1969

TYPES OF OPERATING SYSTEM

Given below are the different types of Operating System along with brief information about each of them:

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

 It is very difficult to guess or know the time required for any job to complete. Processors of the batch systems know how
long the job would be when it is in queue
 Multiple users can share the batch systems
 The idle time for the batch system is very less
 It is easy to manage large work repeatedly in batch systems

Disadvantages of Batch Operating System:

 The computer operators should be well known with batch systems


 Batch systems are hard to debug
 It is sometimes costly
 The other jobs will have to wait for an unknown time if any job fails

2. Time-sharing Operating System - Each task is given some time to execute so that all the tasks work smoothly. Each user
gets the time of CPU as they use a single system. These systems are also known as Multitasking Systems. The task can be
from a single user or different users also. The time that each task gets to execute is called quantum. After this time interval is
over OS switches over to the next task.

Advantages of Time-Sharing OS:

 Each task gets an equal opportunity


 Fewer chances of duplication of software
 CPU idle time can be reduced

Disadvantages of Time-Sharing OS:

 Reliability problem
 One must have to take care of the security and integrity of user programs and data
 Data communication problem

3. Distributed Operating System – These types of the operating system is a recent advancement in the world of computer
technology and are being widely accepted all over the world and, that too, with a great pace. Various autonomous
interconnected computers communicate with each other using a shared communication network. Independent systems
possess their own memory unit and CPU. These are referred to as loosely coupled systems or distributed systems. These
system‟s processors differ in size and function. The major benefit of working with these types of the operating system is that
it is always possible that one user can access the files or software which are not actually present on his system but some
other system connected within this network i.e., remote access is enabled within the devices connected in that network.

Advantages of Distributed Operating System:

 Failure of one will not affect the other network communication, as all systems are independent from each other
 Electronic mail increases the data exchange speed
 Since resources are being shared, computation is highly fast and durable
 Load on host computer reduces
 These systems are easily scalable as many systems can be easily added to the network
 Delay in data processing reduces

Disadvantages of Distributed Operating System:

 Failure of the main network will stop the entire communication


 To establish distributed systems the language which is used are not well defined yet
 These types of systems are not readily available as they are very expensive. Not only that the underlying software is
highly complex and not understood well yet

Examples of Distributed Operating System are - LOCUS, etc.

4. Network Operating System – These systems run on a server and provide the capability to manage data, users, groups,
security, applications, and other networking functions. These types of operating systems allow shared access of files, printers,
security, applications, and other networking functions over a small private network. One more important aspect of Network
Operating Systems is that all the users are well aware of the underlying configuration, of all other users within the network,
their individual connections, etc. and that‟s why these computers are popularly known as tightly coupled systems.
Advantages of Network Operating System:

 Highly stable centralized servers


 Security concerns are handled through servers
 New technologies and hardware up-gradation are easily integrated into the system
 Server access is possible remotely from different locations and types of systems

Disadvantages of Network Operating System:

 Servers are costly


 User has to depend on a central location for most operations
 Maintenance and updates are required regularly

Examples of Network Operating System are: Microsoft Windows Server 2003, Microsoft Windows Server 2008, UNIX,
Linux, Mac OS X, Novell NetWare, and BSD, etc.

5. Real-Time Operating System – These types of OSs serve real-time systems. The time interval required to process and
respond to inputs is very small. This time interval is called response time.

Real-time systems are used when there are time requirements that are very strict like missile systems, air traffic control
systems, robots, etc.

Two types of Real-Time Operating System which are as follows:

 Hard Real-Time Systems: These OSs are meant for applications where time constraints are very strict and even the
shortest possible delay is not acceptable. These systems are built for saving life like automatic parachutes or airbags
which are required to be readily available in case of any accident. Virtual memory is rarely found in these systems.

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

 Maximum Consumption: Maximum utilization of devices and system, thus more output from all the resources

 Task Shifting: The time assigned for shifting tasks in these systems are very less. For example, in older systems, it takes
about 10 microseconds in shifting one task to another, and in the latest systems, it takes 3 microseconds.

 Focus on Application: Focus on running applications and less importance to applications which are in the queue.

 Real-time operating system in the embedded system: Since the sizes of programs are small, RTOS can also be used in
embedded systems like in transport and others.

 Error Free: These types of systems are error-free.

 Memory Allocation: Memory allocation is best managed in these types of systems.

Disadvantages of RTOS:

 Limited Tasks: Very few tasks run at the same time and their concentration is very less on few applications to avoid
errors.

 Use heavy system resources: Sometimes the system resources are not so good and they are expensive as well.

 Complex Algorithms: The algorithms are very complex and difficult for the designer to write on.

 Device driver and interrupt signals: It needs specific device drivers and interrupts signals to respond earliest to
interrupts.

 Thread Priority: It is not good to set thread priority as these systems are very less prone to switching tasks.

Examples of Real-Time Operating Systems are: Scientific experiments, medical imaging systems, industrial control
systems, weapon systems, robots, air traffic control systems, etc.

ADVANTAGES OF AN OPERATING SYSTEM

Given below are some advantages of the Operating System:

1. The operating system helps to improve the efficiency of the work and helps to save a lot of time by reducing the
complexity.
2. An operating system is mainly used to hide the complexity of the hardware.
3. Users can easily access the hardware without writing large programs.
4. With the help of an operating system, sharing data becomes easier with a large number of users.
5. We can easily install any game or application on the Operating system easily and can run them
6. An operating system can be refreshed easily from time to time without having any problems.
7. There are various operating systems that are accessible in open source. Example: Unix/Linux.
8. With the help of an operating system, multitasking becomes easier.

DISADVANTAGES OF AN OPERATING SYSTEM

Given below are the drawbacks of using an operating system:

1. Expensive - There are some open-source platforms like Linux. But some operating systems are expensive. Also, users can
use free operating systems but generally, there is a bit difficulty to run them than others. On the other hand, operating
systems like Microsoft Windows having GUI functionality and other in-built features are very expensive.

2. Virus Threat - Operating Systems are open to virus attacks and sometimes it happens that many users download the
malicious software packages on their system which pauses the functioning of the Operating system and also slows it down.

3. Complexity - Some operating systems are complex in nature because the language used to establish them is not clear
and well defined. If there occurs an issue in the operating system then the user becomes unable to resolve that issue.

4. System Failure - An operating system is the heart of the computer system if due to any reason it will stop functioning
then the whole system will crashes.

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:

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

These types of operating system cause the entire system to crash if one of the
user programs fails. MS-DOS is an example of such operating system. In MS-
DOS, application programs are able to access the basic I/O routines.

Advantages of Simple structure:

 It delivers better application performance because of the few interfaces


between the application program and the hardware.
 Easy for kernel developers to develop such an operating system.

Disadvantages of Simple structure:

 The structure is very complicated as no clear boundaries exist between modules.


 It does not enforce data hiding in the operating system.

2. LAYERED STRUCTURE - An OS can be broken into pieces and retain


much more control on system.

In this structure the OS is broken into number of layers (levels). The


bottom layer (layer 0) is the hardware and the topmost layer (layer N) is
the user interface. These layers are so designed that each layer uses the
functions of the lower level layers only.

This simplifies the debugging process as if lower level layers are


debugged and an error occurs during debugging then the error must be
on that layer only as the lower level layers have already been debugged.
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.
Advantages of Layered structure:

 Layering makes it easier to enhance the operating system as implementation of a layer can be changed easily without
affecting the other layers.
 It is very easy to perform debugging and system verification.

Disadvantages of Layered structure:

 In this structure the application performance is degraded as compared to simple structure.


 It requires careful planning for designing the layers as higher layers use the functionalities of only the lower layers.

3. MICRO-KERNEL - This structure designs the operating system by removing all non-
essential components from the kernel and implementing them as system and user
programs. This result in a smaller kernel called the micro-kernel.

 The main function of the microkernel is to provide communication facility between


the client program and various services.
 Advantages of this structure are that all new services need to be added to user
space and does not require the kernel to be modified.
 Thus it is more secure and reliable as if a service fails then rest of the operating
system remains untouched.
 Mac OS is an example of this type of OS.

Advantages of Micro-kernel structure:

 It makes the operating system portable to various platforms.


 As microkernels are small so these can be tested effectively.

Disadvantages of Micro-kernel structure:

 Increased level of inter module communication degrades system performance.

4. MODULAR STRUCTURE OR APPROACH - It is considered as the best approach for an OS. It involves designing of a
modular kernel. The kernel has only set of core components and other services are added as dynamically loadable modules
to the kernel either during run time or boot time.

It resembles layered structure due to the fact that each kernel has defined and protected interfaces. But it is more flexible
than the layered structure as a module can call any other module. For example Solaris OS is organized as shown in the figure.
UNIT 2 - PROCESS AND THREADS

Process

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


progress in a sequential fashion.

For example, when we write a program in C or C++ and compile it, the compiler
creates binary code. The original code and binary code are both programs. When
we actually run the binary code, it becomes a process.

A process is an „active‟ entity, as opposed to a program, which is considered to be


a „passive‟ entity. A single program can create many processes when run multiple
times; for example, when we open a .exe or binary file multiple times, multiple
instances begin (multiple processes are created).

When a program is loaded into the memory and it becomes a process, it can be
divided into four sections ─ stack, heap, text and data. The following image shows
a simplified layout of a process inside main memory:

S.N. Component & Description


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

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;
}

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

PROCESS VS PROGRAM

Let us take a look at the differences between Process and Program:

Process Program
The process is basically an instance of the computer A Program is basically a collection of instructions that mainly
program that is being executed. performs a specific task when executed by the computer.
A process has a shorter lifetime. A Program has a longer lifetime.
A Process requires resources such as memory, CPU, Input- A Program is stored by hard-disk and does not require any
Output devices. resources.
A process has a dynamic instance of code and data A Program has static code and static data.
Basically, a process is the running instance of the code. On the other hand, the program is the executable code.

Process Control Block (PCB)

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

S.N. Information & Description

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

2 Process privileges - This is required to allow/disallow access to system resources.

3 Process ID - Unique identification for each of the process in the operating system.

4 Pointer - A pointer to parent process.

5 Program Counter - Program Counter is a pointer to the address of the next instruction to be executed for this
process.

6 CPU registers - Various CPU registers where process need to be stored for execution for running state.

7 CPU Scheduling Information - Process priority and other scheduling information which is required to schedule the
process.

8 Memory management information - This includes the information of page table, memory limits, Segment table
depending on memory used by the operating system.

9 Accounting information - This includes the amount of CPU used for process execution, time limits, execution ID etc.

10 IO status information - This includes a list of I/O devices allocated to the process.

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

The PCB is maintained for a process throughout its lifetime, and is deleted once the
process terminates.

Process States / 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.

State Diagram
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.

Process State Models in Operating System

When a process executes, it goes through a number of states. The current state of the process tells us about the current
activity of the process. The state of a process may change due to events like I/O requests; interrupt routines, synchronization
of processes, process scheduling algorithms, etc.

Two State Process Model

Two State Process Model consists of two states:

1. Not-running State: Process waiting for execution.


2. Running State: Process currently executing.

When a process is first created by the OS, it initializes the program control block for the process and the new process enters
the system in Not-running state. After some time, the currently running process will be interrupted by some events, and the
OS will move the currently running process from Running state to Not-running state. The dispatcher then selects one process
from Not-running processes and moves the process to the Running state for execution.

For example, if we implement round-robin scheduling then the running process moves to not-running state after the time
quantum. When a process finishes the execution, the process exits the system and the dispatcher again selects a new process
and moves it to Running state.

All the processes in the Not-running state are maintained in a queue.

Three State Process Model


There is one major drawback of two state process model. When dispatcher brings a new process from not-running state to
running state, the process might still be waiting for some event or I/O request. So, the dispatcher must traverse the queue
and find a not-running process that is ready for execution. It can degrade performance.

To overcome this problem, we split the not-running state into two states: Ready State and Waiting (Blocked) State.

1. Ready State: The process in the main memory that is ready for execution.
2. Waiting or Blocked State: The process in the main memory that is waiting for some event.

The OS maintains a separate queue for both Ready State and Waiting State. A process moves from Waiting State to Ready
State once the event it‟s been waiting for completes.

Five State Process Model

In the five state model, we introduce two new states: new state and terminated state.

Reason for New State - In the previous models, we assumed that the main memory is large enough to accommodate all
programs but this is not true. Modern programs are very large. Loading all processes in the main memory is not possible.

When a new process is made, its program is not loaded in the main memory. The OS only stores some information about the
process in the main memory. The long term scheduler moves the program to the main memory when sufficient space is
available. Such a process is said to be in new state.

Reason for Terminated State In the previous models, when a process finishes execution, its resources are immediately
freed. But there might be some other process that may need its data in the future.

For example, when a child process finishes execution, the OS preserves its data until the parent call wait(). The child process is
still in memory but not available for execution. The child process is said to be in terminated state.

States

1. Running: The currently executing process.

2. Waiting/Blocked: Process waiting for some event such as completion of I/O operation, waiting for other processes,
synchronization signal, etc.

3. Ready: A process that is waiting to be executed.

4. New: The process that is just being created. The Program Control Block is already being made but the program is not
yet loaded in the main memory. The program remains in the new state until the long term scheduler moves the process
to the ready state (main memory).

5. Terminated/Exit: A process that is finished or aborted due to some reason.


Process Schedulers

1. Long term scheduler - Long term scheduler is also known as job scheduler. It chooses the processes from the pool
(secondary memory) and keeps them in the ready queue maintained in the primary memory.

Long Term scheduler mainly controls the degree of Multiprogramming. The purpose of long term scheduler is to choose a
perfect mix of IO bound and CPU bound processes among the jobs present in the pool.

If the job scheduler chooses more IO bound processes then all of the jobs may reside in the blocked state all the time and
the CPU will remain idle most of the time. This will reduce the degree of Multiprogramming. Therefore, the Job of long term
scheduler is very critical and may affect the system for a very long time.

2. Short term scheduler - Short term scheduler is also known as CPU scheduler. It selects one of the Jobs from the ready
queue and dispatch to the CPU for the execution.

A scheduling algorithm is used to select which job is going to be dispatched for the execution. The Job of the short term
scheduler can be very critical in the sense that if it selects job whose CPU burst time is very high then all the jobs after that,
will have to wait in the ready queue for a very long time.

This problem is called starvation which may arise if the short term scheduler makes some mistakes while selecting the job.

3. Medium term scheduler - Medium term scheduler takes care of the swapped out processes. If the running state
processes needs some IO time for the completion then there is a need to change its state from running to waiting.

Medium term scheduler is used for this purpose. It removes the process from the running state to make room for the other
processes. Such processes are the swapped out processes and this procedure is called swapping. The medium term scheduler
is responsible for suspending and resuming the processes.

It reduces the degree of multiprogramming. The swapping is necessary to have a perfect mix of processes in the ready
queue.

S.N. Long-Term Scheduler Short-Term Scheduler Medium-Term Scheduler

1 It is a job scheduler It is a CPU scheduler It is a process swapping scheduler.

2 Speed is lesser than short term Speed is fastest among other two Speed is in between both short and
scheduler long term scheduler.

3 It controls the degree of It provides lesser control over It reduces the degree of
multiprogramming degree of multiprogramming multiprogramming.

4 It is almost absent or minimal in time It is also minimal in time sharing It is a part of Time sharing systems.
sharing system system

5 It selects processes from pool and loads It selects those processes which It can re-introduce the process into
them into memory for execution are ready to execute memory and execution can be
continued.

Context Switch

When CPU switches to another process, the system must save the state of the old process and load the saved state for the
new process. This is known as context switch. In other words, the process of saving the context of one process and loading
the context of another process is known as Context Switching.

 Context-switch time is overhead; the system does no useful work while switching.

 Switching speed varies from machine to machine, depending on the memory speed, the number of registers that must
be copied, and the existence of special instructions. A typical speed is a few milliseconds.
 Context switch times are highly dependent on hardware
support.

 In simple terms, it is like loading and unloading the


process from the running state to the ready state.

When does context switching happen?

 When a high-priority process comes to a ready state (i.e.


with higher priority than the running process).
 An Interrupt occurs.
 User and kernel-mode switch (It is not necessary though).
 Preemptive CPU scheduling used.

When the scheduler switches the CPU from executing one


process to execute another, the state from the current
running process is stored into the process control block. After
this, the state for the process to run next is loaded from its
own PCB and used to set the PC, registers, etc. At that point,
the second process can start executing.

Context switches are computationally intensive since register


and memory state must be saved and restored. To avoid the
amount of context switching time, some hardware systems
employ two or more sets of processor registers. When the
process is switched, the following information is stored for
later use.

 Program Counter
 Scheduling information
 Base and limit register value
 Currently used register
 Changed State
 I/O State information
 Accounting information

Various Times related to the Process


1. Arrival Time - The time at which the process enters into the ready queue is called the arrival time.

2. Burst Time - The total amount of time required by the CPU to execute the whole process is called the Burst Time. This
does not include the waiting time. It is confusing to calculate the execution time for a process even before executing it hence
the scheduling problems based on the burst time cannot be implemented in reality.

3. Completion Time - The Time at which the process enters into the completion state or the time at which the process
completes its execution, is called completion time.

4. Turnaround time - The total amount of time spent by the process from its arrival to its completion, is called Turnaround
time.

5. Waiting Time - The Total amount of time for which the process waits for the CPU to be assigned is called waiting time.

6. Response Time - The difference between the arrival time and the time at which the process first gets the CPU is called
Response Time.

What is Dispatcher?

It is a module that provides control of the CPU to the process. The Dispatcher should be fast so that it can run on every
context switch. Dispatch latency is the amount of time needed by the CPU scheduler to stop one process and start another.

Functions performed by Dispatcher:

 Context Switching
 Switching to user mode
 Moving to the correct location in the newly loaded program.

CPU Scheduling

In the uniprogrammming systems like MS DOS, when a process waits for any I/O operation to be done, the CPU remains
idle. This is an overhead since it wastes the time and causes the problem of starvation. However, In Multiprogramming
systems, the CPU doesn't remain idle during the waiting time of the Process and it starts executing other processes.
Operating System has to define which process the CPU will be given.

In Multiprogramming systems, the Operating system schedules the processes on the CPU to have the maximum utilization
of it and this procedure is called CPU scheduling. The Operating System uses various scheduling algorithm to schedule the
processes.

This is a task of the short term scheduler to schedule the CPU for the number of processes present in the Job Pool. Whenever
the running process requests some IO operation then the short term scheduler saves the current context of the process (also
called PCB) and changes its state from running to waiting. During the time, process is in waiting state; the Short term
scheduler picks another process from the ready queue and assigns the CPU to this process. This procedure is called context
switching.

Why do we need Scheduling?

In Multiprogramming, if the long term scheduler picks more I/O bound processes then most of the time, the CPU remains
idle. The task of Operating system is to optimize the utilization of resources.

If most of the running processes change their state from running to waiting then there may always be a possibility of
deadlock in the system. Hence to reduce this overhead, the OS needs to schedule the jobs to get the optimal utilization of
CPU and to avoid the possibility to deadlock.

PROCESS SCHEDULING

 The act of determining which process is in the ready state, and should be moved to the running state is known as
Process Scheduling.
 When there are two or more runnable processes then it is decided by the operating system which one to run first then it
is referred to as Process Scheduling.

 The prime aim of the process scheduling system is to keep the CPU busy all the time and to deliver minimum response
time for all programs.

 A scheduler is used to make decisions by using some scheduling algorithm.

Good Scheduling Algorithm - Given below are the properties of a Good Scheduling Algorithm:

1. Response time should be minimum for the users.


2. The number of jobs processed per hour should be maximum i.e. Good scheduling algorithm should give maximum
throughput.
3. The utilization of the CPU should be 100%.
4. Each process should get a fair share of the CPU.

Preemptive and Non-Preemptive Scheduling

Preemptive Scheduling

Preemptive scheduling is used when a process switches from running


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

Algorithms based on preemptive scheduling are: Round Robin (RR),


Shortest Remaining Time First (SRTF), Priority (preemptive version),
etc.

Non-Preemptive Scheduling:

Non-preemptive Scheduling is used when a process terminates, or a


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

Algorithms based on non-preemptive scheduling are: Shortest Job First


(SJF basically non preemptive) and Priority (non-preemptive version),
etc.

Comparison Chart:

Parameter PREEMPTIVE SCHEDULING NON-PREEMPTIVE SCHEDULING


Basic In this resources (CPU Cycle) are allocated to a Once resources (CPU Cycle) are allocated to a process, the
process for a limited time. process holds it till it completes its burst time or switches
to waiting state.
Interrupt Process can be interrupted in between. Process cannot be interrupted until it terminates itself or
its time is up.
Starvation If a process having high priority frequently If a process with long burst time is running CPU, then later
arrives in the ready queue, low priority process coming process with less CPU burst time may starve.
may starve.
Overhead It has overheads of scheduling the processes. It does not have overheads.
Flexibility flexible rigid
Cost cost associated no cost associated
CPU In preemptive scheduling, CPU utilization is It is low in non-preemptive scheduling.
Utilization high.
Examples Examples of preemptive scheduling are Round Examples of non-preemptive scheduling are First Come
Robin and Shortest Remaining Time First. First Serve and Shortest Job First.

Types of CPU scheduling Algorithm

FCFS Scheduling

First come first serve (FCFS) scheduling algorithm simply schedules the jobs according to their arrival time. The job which
comes first in the ready queue will get the CPU first. The lesser the arrival time of the job, the sooner will the job get the CPU.
FCFS scheduling may cause the problem of starvation if the burst time of the first process is the longest among all the jobs.

Characteristics of FCFS method

 It supports non-preemptive and pre-emptive scheduling algorithm.


 Jobs are always executed on a first-come first-serve basis.
 It is easy to implement and use.
 This method is poor in performance, and the general wait time is quite high.

Example of FCFS scheduling - A perfect real life example of FCFS scheduling is buying movie tickets at ticket counter.

In this scheduling algorithm, a person is served according to the queue manner. The person who arrives first in the queue
first buys the ticket and then the next one. This will continue until the last person in the queue purchases the ticket. Using
this algorithm, the CPU process works in a similar manner.

Advantages of FCFS

 Simple
 Easy
 First come, First serve

Disadvantages of FCFS

 The scheduling method is non-preemptive; the process will run to the completion.
 Due to the non-preemptive nature of the algorithm, the problem of starvation may occur.
 Although it is easy to implement, but it is poor in performance since the average waiting time is higher as compare to
other scheduling algorithms.

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

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

Turn Around Time = Completion Time - Arrival Time


Waiting Time = Turnaround time - Burst Time

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

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

0 0 2 2 2 0

1 1 6 8 7 1

2 2 4 12 10 6
3 3 9 21 18 9

4 4 12 33 29 17

Average Waiting Time=33/5 = 6.60

(Gantt chart)

Example 2: Calculating Average Waiting Time - For every scheduling algorithm, Average waiting time is a crucial
parameter to judge its performance. AWT or Average waiting time is the average of the waiting times of the processes in the
queue, waiting for the scheduler to pick them for execution. Lower the Average Waiting Time, better the scheduling algorithm.

Consider the processes P1, P2, P3, P4 given in the below table, arrives for execution in the same order, with Arrival Time 0,
and given Burst Time, let's find the average waiting time using the FCFS scheduling algorithm.

The average waiting time will be 18.75 ms

For the above given processes, first P1 will be provided with the CPU resources,

 Hence, waiting time for P1 will be 0


 P1 requires 21 ms for completion, hence waiting time for P2 will be 21 ms
 Similarly, waiting time for process P3 will be execution time of P1 + execution time for P2, which will be (21 + 3)
ms = 24 ms.
 For process P4 it will be the sum of execution times of P1, P2 and P3.

The Gantt chart above perfectly represents the waiting time for each process.

Convoy Effect in FCFS

FCFS may suffer from the convoy effect if the burst time of the first job is the highest among all. As in the real life, if a
convoy is passing through the road then the other persons may get blocked until it passes completely. This can be simulated
in the Operating System also.
If the CPU gets the processes of the higher burst time at the front end of the ready queue then the processes of lower burst
time may get blocked which means they may never get the CPU if the job in the execution has a very high burst time. This is
called convoy effect or starvation.

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

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

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

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

1 0 40 40 40 0

2 1 3 43 42 39

3 1 1 44 43 42

Avg waiting Time = 81/3

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

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

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

1 1 40 44 43 3

2 0 3 3 3 0

3 0 1 4 4 3

Avg Waiting Time=6/3


Shortest Job First (SJF) Scheduling

Till now, we were scheduling the processes according to their arrival time (in FCFS scheduling). However, SJF scheduling
algorithm, schedules the processes according to their burst time. Shortest job first (SJF) or shortest job next, is a scheduling
policy that selects the waiting process with the smallest execution time to execute next.

 Shortest Job first has the advantage of having a minimum average waiting time among all scheduling algorithms.
 It is a Greedy Algorithm.
 It may cause starvation if shorter processes keep coming. This problem can be solved using the concept of ageing.
 It is practically infeasible as Operating System may not know burst time and therefore it may not sort them. While it is
not possible to predict execution time, several methods can be used to estimate the execution time for a job, such as a
weighted average of previous execution times. SJF can be used in specialized environments where accurate estimates of
running time are available.

Advantages of SJF

 Maximum throughput
 Minimum average waiting and turnaround time

Disadvantages of SJF

 May suffer with the problem of starvation


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

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

Algorithm:

1. Sort all the process according to the arrival time.


2. Then select that process which has minimum arrival time and minimum Burst time.
3. After completion of process make a pool of process which after till the completion of previous process and select that
process among the pool which is having minimum Burst time.

There are basically two types of SJF methods:

Non-Preemptive SJF
In non-preemptive scheduling, once the CPU cycle is allocated to process, the process holds it till it reaches a waiting state or
terminated. Consider the following five processes each having its own unique burst time and arrival time.

Process Queue Burst time Arrival time

P1 6 2

P2 2 5

P3 8 1

P4 3 0

P5 4 4

Step 0) At time=0, P4 arrives and starts execution.

Step 1) At time= 1, Process P3 arrives. But, P4 still needs 2 execution units to complete. It will continue execution.

Step 2) At time =2, process P1 arrives and is added to the waiting queue. P4 will continue execution.

Step 3) At time = 3, process P4 will finish its execution. The burst time of P3 and P1 is compared. Process P1 is executed
because its burst time is less compared to P3.

Step 4) At time = 4, process P5 arrives and is added to the waiting queue. P1 will continue execution.

Step 5) At time = 5, process P2 arrives and is added to the waiting queue. P1 will continue execution.

Step 6) At time = 9, process P1 will finish its execution. The burst time of P3, P5, and P2 is compared. Process P2 is executed
because its burst time is the lowest.

Step 7) At time=10, P2 is executing and P3 and P5 are in the waiting queue.

Step 8) At time = 11, process P2 will finish its execution. The burst time of P3 and P5 is compared. Process P5 is executed
because its burst time is lower.

Step 9) At time = 15, process P5 will finish its execution.

Step 10) At time = 23, process P3 will finish its execution.

Step 11) Let's calculate the average waiting time for above example.

Wait time

P4 = 0-0=0
P1 = 3-2=1
P2 = 9-5=4
P5 = 11-4=7
P3 = 15-1=14
Average Waiting Time= 0+1+4+7+14/5 = 26/5 = 5.2

Preemptive SJF
In Preemptive SJF Scheduling, jobs are put into the ready queue as they come. A process with shortest burst time begins
execution. If a process with even a shorter burst time arrives, the current process is removed or preempted from execution,
and the shorter job is allocated CPU cycle.

Consider the following five processes:

Process Queue Burst time Arrival time

P1 6 2

P2 2 5

P3 8 1

P4 3 0

P5 4 4

Step 0) At time=0, P4 arrives and starts execution.

Process Queue Burst time Arrival time

P1 6 2

P2 2 5

P3 8 1

P4 3 0

P5 4 4

Step 1) At time= 1, Process P3 arrives. But, P4 has a shorter burst time. It will continue execution.

Step 2) At time = 2, process P1 arrives with burst time = 6. The burst time is more than that of P4. Hence, P4 will continue
execution.
Step 3) At time = 3, process P4 will finish its execution. The burst time of P3 and P1 is compared. Process P1 is executed
because its burst time is lower.

Step 4) At time = 4, process P5 will arrive. The burst time of P3, P5, and P1 is compared. Process P5 is executed because its
burst time is lowest. Process P1 is preempted.

Process Queue Burst time Arrival time

P1 5 out of 6 is remaining 2
P2 2 5
P3 8 1
P4 3 0
P5 4 4

Step 5) At time = 5, process P2 will arrive. The burst time of P1, P2, P3, and P5 is compared. Process P2 is executed because
its burst time is least. Process P5 is preempted.

Process Queue Burst time Arrival time


P1 5 out of 6 is remaining 2
P2 2 5
P3 8 1
P4 3 0
P5 3 out of 4 is remaining 4
Step 6) At time =6, P2 is executing.

Step 7) At time =7, P2 finishes its execution. The burst time of P1, P3, and P5 is compared. Process P5 is executed because its
burst time is lesser.

Process Queue Burst time Arrival time


P1 5 out of 6 is remaining 2
P2 2 5
P3 8 1
P4 3 0
P5 3 out of 4 is remaining 4

Step 8) At time =10, P5 will finish its execution. The burst time of P1 and P3 is compared. Process P1 is executed because its
burst time is less.

Step 9) At time =15, P1 finishes its execution. P3 is the only process left. It will start execution.
Step 10) At time =23, P3 finishes its execution.

Step 11) Let's calculate the average waiting time for above example.

Wait time

P4 = 0-0=0
P1 = (3-2) + 6 =7
P2 = 5-5 = 0
P5 = 4-4+2 =2
P3 = 15-1 = 14
Average Waiting Time = 0+7+0+2+14/5 = 23/5 =4.6

Q.1: Consider the set of 5 processes whose arrival time and burst time are given below:

If the CPU scheduling policy is SJF non-preemptive, calculate the average waiting time and average turnaround time.

Solution:

Now, we know,

Turn Around time = Exit time –Arrival time


Waiting time = Turn Around time –Burst time
Now,
Average Turn Around time = (4+15+5+6+10)/5 = 40/5 = 8 unit
Average waiting time = (3+11+3+0+7)/5 = 24/5 = 4.8 unit

Q.2: Consider the set of 5 processes whose arrival time and burst time are given below:

If the CPU scheduling policy is SJF preemptive, calculate the average waiting time and average turnaround time.

Solution:

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

Now,
Average Turn Around time = (1+5+4+16+9)/5 = 35/5 = 7 unit
Average waiting time = (0+1+2+10+6)/5 = 19/5 = 3.8 unit

Priority Scheduling

In Priority scheduling, there is a priority number assigned to each process. In some systems, the lower the number, the higher
the priority. While, in the others, the higher the number, the higher will be the priority. The Process with the higher priority
among the available processes is given the CPU. There are two types of priority scheduling algorithm exists. One
is Preemptive priority scheduling while the other is Non Preemptive Priority scheduling.
Example of Priority Scheduling Algorithm - Consider the below table of processes with their respective CPU burst times
and the priorities.

As you can see in the Gantt chart that the processes are given CPU time just on the basis of the priorities.

Preemptive Priority Scheduling

In Preemptive Priority Scheduling, at the time of arrival of a process in the ready queue, its Priority is compared with the
priority of the other processes present in the ready queue as well as with the one which is being executed by the CPU at that
point of time. The One with the highest priority among all the available processes will be given the CPU next.

The difference between preemptive priority scheduling and non-preemptive priority scheduling is that, in the preemptive
priority scheduling, the job which is being executed can be stopped at the arrival of a higher priority job.

Once all the jobs get available in the ready queue, the algorithm will behave as non-preemptive priority scheduling, which
means the job scheduled will run till the completion and no preemption will be done.

Example: There are 7 processes P1, P2, P3, P4, P5, P6 and P7 given. Their respective priorities, Arrival Times and Burst times
are given in the table below.

Process Id Priority Arrival Time Burst Time


1 2(L) 0 1
2 6 1 7
3 3 2 3
4 5 3 6
5 4 4 5
6 10(H) 5 15
7 9 15 8

GANTT chart Preparation

At time 0, P1 arrives with the burst time of 1 units and priority 2. Since no other process is available hence this will be
scheduled till next job arrives or its completion (whichever is lesser).
At time 1, P2 arrives. P1 has completed its execution and no other process is available at this time hence the Operating
system has to schedule it regardless of the priority assigned to it.

The Next process P3 arrives at time unit 2, the priority of P3 is higher to P2. Hence the execution of P2 will be stopped and P3
will be scheduled on the CPU.

During the execution of P3, three more processes P4, P5 and P6 becomes available. Since, all these three have the priority
lower to the process in execution so PS can't preempt the process. P3 will complete its execution and then P5 will be
scheduled with the priority highest among the available processes.

Meanwhile the execution of P5, all the processes got available in the ready queue. At this point, the algorithm will start
behaving as Non Preemptive Priority Scheduling. Hence now, once all the processes get available in the ready queue, the OS
just took the process with the highest priority and execute that process till completion. In this case, P4 will be scheduled and
will be executed till the completion.

Since P4 is completed, the other process with the highest priority available in the ready queue is P2. Hence P2 will be
scheduled next.

P2 is given the CPU till the completion. Since its remaining burst time is 6 units hence P7 will be scheduled after this.

The only remaining process is P6 with the least priority, the Operating System has no choice unless of executing it. This will
be executed at the last.

The Completion Time of each process is determined with the help of GANTT chart. The turnaround time and the waiting time
can be calculated by the following formula.
Turnaround Time = Completion Time - Arrival Time
Waiting Time = Turn Around Time - Burst Time

Process Id Priority Arrival Time Burst Time Completion Time Turnaround Time Waiting Time
1 2 0 1 1 1 0
2 6 1 7 22 21 14
3 3 2 3 5 3 0
4 5 3 6 16 13 7
5 4 4 5 10 6 1
6 10 5 15 45 40 25
7 9 6 8 30 24 16

Avg Waiting Time = (0+14+0+7+1+25+16)/7 = 63/7 = 9 units

Non Preemptive Priority Scheduling

In the Non Preemptive Priority scheduling, The Processes are scheduled according to the priority number assigned to them.
Once the process gets scheduled, it will run till the completion. Generally, the lower the priority number, the higher is the
priority of the process. The people might get confused with the priority numbers, hence in the GATE, there clearly mention
which one is the highest priority and which one is the lowest one.

Example: In the Example, there are 7 processes P1, P2, P3, P4, P5, P6 and P7. Their priorities, Arrival Time and burst time are
given in the table.

Process ID Priority Arrival Time Burst Time


1 2 0 3
2 6 2 5
3 3 1 4
4 5 4 2
5 7 6 9
6 4 5 4
7 10 7 10

We can prepare the Gantt chart according to the Non Preemptive priority scheduling.

The Process P1 arrives at time 0 with the burst time of 3 units and the priority number 2. Since No other process has arrived
till now hence the OS will schedule it immediately.

Meanwhile the execution of P1, two more Processes P2 and P3 are arrived. Since the priority of P3 is 3 hence the CPU will
execute P3 over P2.

Meanwhile the execution of P3, All the processes get available in the ready queue. The Process with the lowest priority
number will be given the priority. Since P6 has priority number assigned as 4 hence it will be executed just after P3.

After P6, P4 has the least priority number among the available processes; it will get executed for the whole burst time.

Since all the jobs are available in the ready queue hence All the Jobs will get executed according to their priorities. If two jobs
have similar priority number assigned to them, the one with the least arrival time will be executed.

From the Gantt chart prepared, we can determine the completion time of every process. The turnaround time, waiting time
and response time will be determined.

Turn Around Time = Completion Time - Arrival Time


Waiting Time = Turn Around Time - Burst Time

Process Priority Arrival Burst Completion Turnaround Waiting Response


Id Time Time Time Time Time Time
1 2 0 3 3 3 0 0
2 6 2 5 18 16 11 13
3 3 1 4 7 6 2 3
4 5 4 2 13 9 7 11
5 7 6 9 27 21 12 18
6 4 5 4 11 6 2 7
7 10 7 10 37 30 18 27

Avg Waiting Time = (0+11+2+7+12+2+18)/7 = 52/7 units

ROUND ROBIN SCHEDULING ALGORITHM

 This algorithm is similar to FCFS scheduling, but in Round Robin (RR) scheduling, preemption is added which enables the
system to switch between processes.

 Round Robin scheduling algorithm is one of the most popular scheduling algorithms which can actually be implemented
in most of the operating systems.

 This algorithm is mainly designed for time-sharing systems; every process gets executed in a cyclic way.

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

 Each process present in the ready queue is assigned the CPU for that time quantum, if the execution of the process is
completed during that time then the process will terminate else the process will go back to the ready queue and waits
for the next turn to complete the execution.

 This algorithm is starvation-free as all processes get a fair share of CPU.

Characteristics of the Round Robin Algorithm

1. Round Robin Scheduling algorithm resides under the category of Preemptive Algorithms.
2. This algorithm is one of the oldest, easiest, and fairest algorithms.
3. This Algorithm is a real-time algorithm because it responds to the event within a specific time limit.
4. In this algorithm, the time slice should be the minimum that is assigned to a specific task that needs to be processed.
Though it may vary for different operating systems.
5. This is a hybrid model and is clock-driven in nature.
6. This is a widely used scheduling method in the traditional operating system.

Advantages

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

Disadvantages

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

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

Process ID Arrival Time Burst Time


1 0 5
2 1 6
3 2 3
4 3 1
5 4 5
6 6 4
According to the algorithm, we have to maintain the ready queue and the Gantt chart. The structure of both the data
structures will be changed after every scheduling.

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

P1
5
The P1 will be executed for 4 units first.

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

P2 P3 P4 P5 P1
6 3 1 5 1
After P1, P2 will be executed for 4 units of time which is shown in the Gantt chart.

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

P3 P4 P5 P1 P6 P2
3 1 5 1 4 2
After P1 and P2, P3 will get executed for 3 units of time since its CPU burst time is only 3 seconds.

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

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

Ready Queue: The next process in the ready queue is P5 with 5 units of burst time. Since P4 is completed hence it will not be
added back to the queue.
P5 P1 P6 P2
5 1 4 2
P5 will be executed for the whole time slice because it requires 5 units of burst time which is higher than the time slice.

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

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

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

P6 P2 P5
4 2 1
P6 will be executed for 4 units of time till completion.

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

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

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

P5
1
P5 will get executed till completion.

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

As, we know,
Turn Around Time = Completion Time - Arrival Time
Waiting Time = Turn Around Time - Burst Time

Process ID Arrival Time Burst Time Completion Time Turn Around Time Waiting Time
1 0 5 17 17 12
2 1 6 23 22 16
3 2 3 11 9 6
4 3 1 12 9 8
5 4 5 24 20 15
6 6 4 21 15 11

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

Thread

 A thread is the subset of a process and is also known as the lightweight process. A process can have more than one
thread, and these threads are managed independently by the scheduler. All the threads within one process are
interrelated to each other. Threads have some common information, such as data segment, code segment, files,
etc., that is shared to their peer threads. But contains its own registers, stack, and counter.

 A thread is a flow of execution through the process code, with its own program counter that keeps track of which
instruction to execute next, system registers which hold its current working variables, and a stack which contains the
execution history.

 A thread shares with its peer threads little information like code segment, data segment and open files. When one
thread alters a code segment memory item, all other threads see that.

What is a Thread? A thread is a path of execution within a process. A process can contain multiple threads.

Process vs. Thread - The primary difference is that threads within the same process run in a shared memory space, while
processes run in separate memory spaces.

Threads are not independent of one another like processes are, and as a result threads share with other threads their code
section, data section, and OS resources (like open files and signals). But, like process, a thread has its own program counter
(PC), register set, and stack space.

Each thread belongs to exactly one process and no thread can exist outside a process. Each thread represents a separate flow
of control. Threads have been successfully used in implementing network servers and web server. They also provide a
suitable foundation for parallel execution of applications on shared memory multiprocessors. The following figure shows the
working of a single-threaded and a multithreaded process.
How does thread work? As we have discussed that a thread is a sub process or an execution unit within a process. A
process can contain a single thread to multiple threads. A thread works as follows:

o When a process starts, OS assigns the memory and resources to it. Each thread within a process shares the memory and
resources of that process only.

o Threads are mainly used to improve the processing of an application. In reality, only a single thread is executed at a time,
but due to fast context switching between threads gives an illusion that threads are running parallelly.

If a single thread executes in a process, it is known as a single-threaded and if multiple threads execute simultaneously, then
it is known as multithreading.

S.N. Process Thread


1 Process simply means any program in execution. Thread simply means a segment of a process.
2 Process consumes more resources. Thread consumes fewer resources.
3 Process requires more time for creation. Thread requires comparatively less time for creation that
process.
4 Process is a heavyweight process. Thread is known as a lightweight process.
5 The process takes more time to terminate. The thread takes less time to terminate.
6 Processes have independent data and code segments. Thread mainly shares the data segment, code segment,
files etc. with its peer threads.
7 Process takes more time for context switching. Thread takes less time for context switching.
8 Communication between processes needs more time as Communication between threads needs less time as
compared to thread. compared to processes.
9 For some reason, if a process gets blocked the In case if a user-level thread gets blocked all of its peer
remaining processes can continue their execution. threads also gets blocked.
10 Process switching needs interaction with operating Thread switching does not need to interact with operating
system. system.

Why Multithreading? You can imagine multitasking as something that allows processes to run concurrently, while
multithreading allows sub-processes to run concurrently.

 It is the ability of a CPU to manage the use of operating system by executing multiple threads concurrently.
 The main idea of multithreading is to achieve parallelism by dividing a process into multiple threads.
 A thread is also known as lightweight process.
 For example, in a browser, multiple tabs can be different threads.
 MS Word uses multiple threads: one thread to format the text, another thread to process inputs, etc.
 Threads have been successfully used in implementing network servers and web server.

Types of Thread

1. User Level Thread

 As the name suggests, the user-level threads are only managed by users, and the kernel does not have its information.
 These are faster, easy to create and manage.
 The application starts with a single thread.
 The kernel takes all these threads as a single process and handles them as one process only.
 The user-level threads are implemented by user-level libraries, not by the system calls.
 The kernel knows nothing about user level threads and manages them as if they were single threaded processes.
 When threads are managed in user space, each process needs its own private thread table to keep track of the threads
in that process.
 This table keeps track only of the per-thread properties, such as each thread‟s program counter, stack pointer, register,
state, and so forth.

Advantages of ULT

 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.
 Thread switching does not require Kernel mode privileges.
 User level thread can run on any operating system.
 Scheduling can be application specific in the user level thread.
 User level threads are fast to create and manage.

Disadvantages of ULT

 No or less co-ordination among the threads and Kernel.


 If one thread causes a page fault, the entire process blocks.
 In a typical operating system, most system calls are blocking.
 Multithreaded application cannot take advantage of multiprocessing.

2. Kernel Level Thread

 In this case, thread management is done by the Kernel.


 There is no thread management code in the application area.
 Kernel threads are supported directly by the operating system.
 Any application can be programmed to be multithreaded.
 All of the threads within an application are supported within a single process.
 The Kernel maintains context information for the process as a whole and for individual threads within the process.
 Scheduling by the Kernel is done on a thread basis.
 The Kernel performs thread creation, scheduling and management in Kernel space.
 Kernel threads are generally slower to create and manage than the user threads.
 OS kernel provides system call to create and manage threads.

Advantages of KLT

 Kernel can simultaneously schedule multiple threads from the same process on multiple processes.
 If one thread in a process is blocked; the Kernel can schedule another thread of the same process.
 Kernel routines themselves can be multithreaded.
 Good for applications that frequently block.

Disadvantages of KLT

 Kernel threads are generally slower to create and manage than the user threads.
 It requires thread control block so it is an overhead.
 Transfer of control from one thread to another within the same process requires a mode switch to the Kernel.

User level thread Kernel level thread


User threads are implemented by users. Kernel threads are implemented by OS.
OS doesn‟t recognized user level threads. Kernel threads are recognized by OS.
Implementation of User threads is easy. Implementation of Kernel thread is complicated.
Context switch time is less. Context switch time is more.
Context switch requires no hardware support. Hardware support is needed.
If one user level thread performs blocking operation then If one kernel thread performs blocking operation then
entire process will be blocked. another thread can continue execution.
User level threads are designed as dependent threads. Kernel level threads are designed as independent threads.
This thread is generic in nature and can run on any This is specific to the operating system.
operating system.
Example: Java thread, POSIX threads. Example: Window Solaris.
KERNEL IN OPERATING SYSTEM

 Kernel is central component of an operating system that manages operations of


computer and hardware. It is also a system program.
 A kernel is the core component of an operating system which coverts user
command into machine language.
 Kernel loads first into memory when an operating system is loaded and remains
into memory until operating system is shut down again.
 The major aim of kernel is to manage communication between software i.e. user-
level applications and hardware i.e., CPU and disk memory.
 It is responsible for various tasks such as disk management, task management,
and memory management.
 It decides which process should be allocated to processor to execute and which process should be kept in main memory
to execute.

Objectives of Kernel:

 To establish communication between user level application and hardware.


 To decide state of incoming processes.
 To control disk management.
 To control memory management.
 To control task management.

Operating System Kernel


Operating System is a system software. Kernel is system software which is part of operating system.
Operating System provides interface b/w user and Kernel provides interface b/w application and hardware.
hardware.
It also provides protection and security. Its main purpose is memory management, disk management,
process management and task management.
All system needs operating system to run. All operating system needs kernel to run.
Type of operating system includes single and multiuser Type of kernel includes Monolithic and Micro kernel.
OS, multiprocessor OS, real-time OS, and Distributed OS.
It is the first program to load when computer boots up. It is the first program to load when operating system loads.

INTER PROCESS COMMUNICATION (IPC)

Inter process communication (IPC) is a set of programming interfaces that allow a programmer to coordinate activities
among different program processes that can run concurrently in an operating system. In other words, Inter process
communication (IPC) is used for exchanging data between multiple threads in one or more processes or programs.

 The processes can be running on the same computer or on different computers connected through a network. This
allows a program to handle many user requests at the same time.

 IPC enables one application to control another application, and for several applications to share the same data without
interfering with one another.

 IPC is required in all multiprocessing systems, but it is not generally supported by single-process operating systems such
as DOS. A diagram that illustrates inter-process communication is as follows:

WHY IPC? Here, are the reasons for using the inter-process communication protocol for information sharing:

 It helps to speedup modularity.


 Computational.
 Privilege separation.
 Convenience.
 Helps operating system to communicate with each other and synchronize their actions.

RACE CONDITIONS

A race condition is a situation where two or more control threads are working with the same memory elements, and
simultaneously decide to write different values in the same place.

When more than one processes are 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.

Suppose that two processes, P1 and P2, share the global variable a. At some point in its execution, P1 updates a to the value
1, and at some point in its execution, P2 updates a to the value 2. Thus, the two tasks are in a race to write variable a.

In this example the "loser" of the race (the process that updates last) determines the final value of a.

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.

 This condition can be avoided using the technique called Synchronization or Process Synchronization, in which we allow
only one process to enter and manipulates the shared data in Critical Section.

Producer-Consumer problem

The Producer-Consumer problem is a classical multi-process synchronization problem that is we are trying to achieve
synchronization between more than one processes.

There is one Producer in the producer-consumer problem; Producer is producing some items, whereas there is one
Consumer that is consuming the items produced by the Producer. The same memory buffer is shared by both producers and
consumers which is of fixed-size.

The task of the Producer is to produce the item, put it into the memory buffer, and again start producing items. Whereas the
task of the Consumer is to consume the item from the memory buffer.

Let's understand what is the problem? Below are a few points that considered as the problems occur in Producer-Consumer:

o The producer should produce data only when the buffer is not full. In case it is found that the buffer is full, the producer
is not allowed to store any data into the memory buffer.

o Data can only be consumed by the consumer if and only if the memory buffer is not empty. In case it is found that the
buffer is empty, the consumer is not allowed to use any data from the memory buffer.

o Accessing memory buffer should not be allowed to producer and consumer at the same time.

SECTIONS OF A PROGRAM

Here, are four essential elements of the critical section:

1. Entry Section: It is part of the process which decides the entry of a particular process. It is part of the process which
decide the entry of a particular process in the Critical Section, out of many other processes.

2. Critical Section: This part allows one process to enter and modify the shared variable. It is the part in which only one
process is allowed to enter and modify the shared variable. This part of the process ensures that only no other process can
access the resource of shared data.

3. Exit Section: Exit section allows the other processes that are waiting in the Entry Section, to enter into the Critical Sections.
It also checks that a process that finished its execution should be removed through this Section. This process allows the other
processes that are waiting in the Entry Section, to enter into the Critical Sections. It checks that a process that after a process
has finished execution in Critical Section can be removed through this Exit Section.

4. Remainder Section: All other parts of the Code, which is not in Critical, Entry, and Exit Section, are known as the
Remainder Section. The other parts of the Code other than Entry Section, Critical Section and Exit Section are known as
Remainder Section.

CRITICAL SECTION PROBLEM

 Critical section is a code segment that can be accessed by only one process at a time.
 Critical section contains shared variables which need to be synchronized to maintain consistency of data variables.
 If any other process also wants to execute its critical section, it must wait until the first one finishes.
 It means that in a group of cooperating processes, at a given point of time, only one process must be executing its
critical section.
 The entry to the critical section is mainly handled by wait()function while the exit from the critical section is controlled by
the signal()function.
 In the entry section, the process requests for entry in the Critical Section.

In the above diagram, the entry section handles the entry into the critical section. It acquires the resources needed for
execution by the process.

 The exit section handles the exit from the critical section. It releases the resources and also informs the other processes
that critical section is free.
 The critical section problem needs a solution to synchronize the different processes.

SOLUTION TO CRITICAL SECTION PROBLEM

The solution to the critical section problem must satisfy the following conditions:

1. Mutual Exclusion: If a process is executing in its critical section, then no other process is allowed to execute in the critical
section. Mutual exclusion implies that only one process can be inside the critical section at any time. If any other processes
require the critical section, they must wait until it is free.

2. Progress: Progress means that if a process is not using the critical section, then it should not stop any other process from
accessing it. In other words, any process can enter a critical section if it is free. When no process is in the critical section, then
any process from outside that request for execution can enter in the critical section without any delay. Only those processes
can enter that have requested and have finite time to enter the process.

3. Bounded Waiting: Bounded waiting means that each process must have a limited waiting time. It should not wait
endlessly to access the critical section. 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.
Mutual Exclusion in Synchronization

During concurrent execution of processes, processes need to enter the critical section (or the section of the program shared
across processes) at times for execution. It might so happen that because of the execution of multiple processes at once, the
values stored in the critical section become inconsistent. In other words, the values depend on the sequence of execution of
instructions – also known as a race condition. The primary task of process synchronization is to get rid of race conditions
while executing the critical section. This is primarily achieved through mutual exclusion.

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

To understand mutual exclusion, let‟s take an example.

Example: In the clothes section of a supermarket, two people are shopping for clothes.

Boy A decides upon some clothes to buy and heads to the changing room to try them out. Now, while boy A is inside the
changing room, there is an „occupied‟ sign on it – indicating that no one else can come in. Girl B has to use the changing
room too, so she has to wait till boy A is done using the changing room.

Once boy A comes out of the changing room, the sign on it changes from „occupied‟ to „vacant‟ – indicating that another
person can use it. Hence, girl B proceeds to use the changing room, while the sign displays „occupied‟ again.
The changing room is nothing but the critical section, boy A and girl B are two different processes, while the sign outside the
changing room indicates the process synchronization mechanism being used.

Solution for critical section problem:

1. Software type: Lock variables, Strict alternation, Peterson‟s algorithm


2. Hardware type: Test and Set
3. OS type: Semaphore
4. Compiler type: Monitors

MUTUAL EXCLUSION WITH BUSY WAITING

For achieving mutual exclusion, while one process is busy updating shared memory in its critical region, no other process will
enter its critical region and cause trouble.

In computer science, mutual exclusion refers to the requirement of ensuring that no two concurrent processes are in their
critical section at the same time; it is a basic requirement in concurrency control, to prevent race conditions.

Requirements for Mutual Exclusion:

1. Mutual exclusion must be enforced only one process at a time is allowed into its critical section, among all processes that
have critical sections for the same resource or shared object.

2. A process that halts in its non-critical section must do so without interfering with other processes.

3. It must not be possible for a process requiring access to a critical section to be delayed indefinitely: no deadlock or
starvation.

4. When no process is in a critical section, any process that requests entry to its critical section must be permitted to enter
without delay.

5. No assumptions are made about relative process speeds or number of processors.

6. A process remains inside its critical section for a finite time only.

1. PETERSON’S SOLUTION

Peterson‟s Solution is a classical software based solution to the critical section problem. Peterson's algorithm is a concurrent
programming algorithm developed by Gary L. Peterson in a 1981 paper. It is known as a simple algorithm when compared to
others. Peterson proved the algorithm using both the 2-process case and the N-process case.
Peterson's algorithm is used for mutual exclusion and allows two processes to share a single-use resource without conflict. It
uses only shared memory for communication. Peterson's formula originally worked only with two processes, but has since
been generalized for more than two.

In Peterson‟s solution, we have two shared variables:

1. boolean flag[i]: Initialized to FALSE, initially no one is interested in entering the critical section
2. int turn: The process whose turn is to enter the critical section.

Problem: The producer consumer problem (or bounded buffer problem) describes two processes, the producer and the
consumer, which share a common, fixed-size buffer used as a queue. Producer produce an item and put it into buffer. If
buffer is already full then producer will have to wait for an empty block in buffer. Consumer consume an item from buffer. If
buffer is already empty then consumer will have to wait for an item in buffer. Implement Peterson‟s Algorithm for the two
processes using shared memory such that there is mutual exclusion between them. The solution should have free from
synchronization problems.

Peterson’s algorithm

Code for producer (j)

flag[j] = true; // producer j is ready to produce an item


turn = i; // but consumer (i) can consume an item

// if consumer is ready to consume an item and if its consumer's turn


while (flag[i] == true && turn == i) { } // // then producer will wait

// otherwise producer will produce an item and put it into buffer (critical Section)
flag[j] = false; // Now, producer is out of critical section
// end of code for producer

Code for consumer i

flag[i] = true; // consumer i is ready to consume an item


turn = j; // but producer (j) can produce an item

// if producer is ready to produce an item and if its producer's turn


while (flag[j] == true && turn == j){ } // then consumer will wait

// otherwise consumer will consume an item from buffer (critical Section)


flag[i] = false; // Now, consumer is out of critical section
// end of code for consumer

Explanation of Peterson’s algorithm - Peterson‟s Algorithm is used to synchronize two processes. It uses two variables, a
bool array flag of size 2 and an int variable turn to accomplish it.

In the solution i represent the Consumer and j represents the Producer. Initially the flags are false. When a process wants to
execute its critical section, it sets its flag to true and turn as the index of the other process. This means that the process wants
to execute but it will allow the other process to run first. The process performs busy waiting until the other process has
finished its own critical section.
After this the current process enters its critical section and adds or removes a random number from the shared buffer. After
completing the critical section, it sets its own flag to false, indication it does not wish to execute anymore.

Peterson‟s Solution preserves all three conditions:

1. Mutual Exclusion is assured as only one process can access the critical section at any time.
2. Progress is also assured, as a process outside the critical section does not block other processes from entering the critical
section.
3. Bounded Waiting is preserved as every process gets a fair chance.

Disadvantages of Peterson’s Solution:

 It involves Busy waiting


 It is limited to 2 processes.

2. SEMAPHORES IN PROCESS SYNCHRONIZATION

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.

Semaphore was proposed by Dijkstra in 1965 which is a very significant technique to manage concurrent processes by using
a simple integer value, which is known as a semaphore. Semaphore is simply an integer variable that is shared between
threads. This variable is used to solve the critical section problem and to achieve process synchronization in the
multiprocessing environment. So it is basically a synchronizing tool and is accessed only through two low standard atomic
operations, wait and signal designated by P(S) and V(S) respectively.

In very simple words, the semaphore is a variable that can hold only a non-negative Integer value, shared between all the
threads, with operations wait and signal, which work as follow:

P(S): if S >= 1 then S := S - 1


else <block and enqueue the process>;

V(S): if <some process is blocked on the queue>


then <unblock a process>
else S := S + 1;

The classical definitions of wait and signal are:

Wait: This operation decrements the value of its argument S, as soon as it would become non-negative(greater than or equal
to 1). This Operation mainly helps you to control the entry of a task into the critical section. In the case of the negative or
zero value, no operation is executed. wait() operation was originally termed as P; so it is also known as P(S) operation. The
definition of wait operation is as follows:

wait(S)
{
while (S<=0);//no operation
S--;
}

Note: When one process modifies the value of a semaphore then, no other process can simultaneously modify that same
semaphore's value. In the above case the integer value of S(S<=0) as well as the possible modification that is S-- must be
executed without any interruption.

Signal: Increments the value of its argument S, as there is no more process blocked on the queue. This Operation is mainly
used to control the exit of a task from the critical section. signal() operation was originally termed as V; so it is also known
as V(S) operation. The definition of signal operation is as follows:

signal(S)
{
S++;
}
Also, note that all the modifications to the integer value of semaphore in the wait() and signal() operations must be executed
indivisibly.

Properties of Semaphores

1. It's simple and always has a non-negative integer value.


2. Works with many processes.
3. Can have many different critical sections with different semaphores.
4. Each critical section has unique access semaphores.
5. Can permit multiple processes into the critical section at once, if desirable.

TYPES OF SEMAPHORE

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

They are also known as mutex locks, as the locks can provide mutual exclusion. All the processes can share the same mutex
semaphore that is initialized to 1.

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

Advantages of Semaphore

1. In the Semaphore, only one process is allowed to enter into the critical section. In this, the principle of mutual exclusion
is to be followed strictly. And the semaphore mechanism is a more efficient mechanism than other methods which we
use in process synchronization.
2. It is machine-independent because it is implemented in the microkernel‟s machine-independent code.
3. With the help of the semaphore, the resources are managed flexibly.
4. In semaphore, there is a busy waiting, so there is no wastage of resources and process time.
5. In the semaphore, more threads are permitted to enter into the critical section.

Disadvantages of Semaphore

1. There is a priority inversion in the semaphore, and this is the biggest drawback of the semaphore.
2. In the semaphore, due to program error violation of mutual exclusion, deadlock can be happened.
3. In the semaphore, there are more chances of programmer errors.
4. For the purpose of large-scale use, the semaphore is not a practical method, and due to this, there may be loss of
modularity.
5. The Programming of the semaphore is tough, so there may be a possibility of not achieving mutual exclusion.
6. In the semaphore, the OS has to preserve all calls to wait and signal semaphore.

IMP: Producer Consumer Problem using Semaphores

Prerequisite – Semaphores in operating system, Inter Process Communication Producer consumer problem is a classical
synchronization problem. We can solve this problem by using semaphores.

A semaphore S is an integer variable that can be accessed only through two standard operations : wait() and signal().
The wait() operation reduces the value of semaphore by 1 and the signal() operation increases its value by 1.

wait(S){
while(S<=0); // busy waiting
S--;
}

signal(S){
S++;
}

Problem Statement – We have a buffer of fixed size. A producer can produce an item and can place in the buffer. A
consumer can pick items and can consume them. We need to ensure that when a producer is placing an item in the buffer,
then at the same time consumer should not consume any item. In this problem, buffer is the critical section.

To solve this problem, we need two counting semaphores – Full and Empty. “Full” keeps track of number of items in the
buffer at any given time and “Empty” keeps track of number of unoccupied slots.

Initialization of semaphores –
mutex = 1
Full = 0 // Initially, all slots are empty. Thus full slots are 0
Empty = n // All slots are empty initially

Solution for Producer

do{
//produce an item
wait(empty);
wait(mutex);

//place in buffer
signal(mutex);
signal(full);

}while(true)

When producer produces an item then the value of “empty” is reduced by 1 because one slot will be filled now. The value of
mutex is also reduced to prevent consumer to access the buffer. Now, the producer has placed the item and thus the value of
“full” is increased by 1. The value of mutex is also increased by 1 because the task of producer has been completed and
consumer can access the buffer.

Solution for Consumer

do{
wait(full);
wait(mutex);

// remove item from buffer


signal(mutex);
signal(empty);
// consumes item

}while(true)

As the consumer is removing an item from buffer, therefore the value of “full” is reduced by 1 and the value is mutex is also
reduced so that the producer cannot access the buffer at this moment. Now, the consumer has consumed the item, thus
increasing the value of “empty” by 1. The value of mutex is also increased so that producer can access the buffer now.

Monitors in process synchronization

 Monitor is a compiler type solution to the synchronization problem.

 A monitor is a collection of procedures, variables, and data structures that are all grouped together in a special kind of
module or package.

 Processes may call the procedures in a monitor whenever they want to, but they cannot directly access the monitor‟s
internal data structures from procedures declared outside the monitor.

 Monitor simply defines a lock and a condition variable for managing concurrent access to the shared data.
 Lock insures that only one single thread is active in the monitor at a time by providing mutual exclusion.

 Condition variable enable thread to go to sleep inside of critical section by releasing their lock at the same time it puts
the thread to go to sleep.

To overcome the timing errors that occurs while using semaphore for process synchronization, the researchers have
introduced a high-level synchronization construct i.e. the monitor type. A monitor type is an abstract data type that is used
for process synchronization.

Being an abstract data type monitor type contains the shared data variables that are to be shared by all the processes and
some programmer-defined operations that allow processes to execute in mutual exclusion within the monitor. A process
can not directly access the shared data variable in the monitor; the process has to access it through procedures defined in
the monitor which allow only one process to access the shared variables in a monitor at a time.

The syntax of monitor is as follow:

monitor monitor_name
{
//shared variable declarations
procedure P1 ( . . . ) {
}
procedure P2 ( . . . ) {
}
procedure Pn ( . . . ) {
}
initialization code ( . . . ) {
}
}

A monitor is a construct such as only one process is active at a time within the monitor. If other process tries to access the
shared variable in monitor, it gets blocked and is lined up in the queue to get the access to shared data when previously
accessing process releases it.

Conditional variables were introduced for additional synchronization mechanism. The conditional variable allows a process
to wait inside the monitor and allows a waiting process to resume immediately when the other process releases the
resources.

The conditional variable can invoke only two operation wait() and signal(). Where if a process P invokes a wait() operation
it gets suspended in the monitor till other process Q invoke signal() operation i.e. a signal() operation invoked by a process
resumes the suspended process.

Drawback of Monitors

1. Major weakness of monitors is the absence of concurrency if a monitor encapsulates the resources, since only one
process can be active within a monitor at a time.
2. There is the possibility of deadlock in the case of nested monitor calls
3. Monitor concept is its lack of implementation most commonly used programming language.
4. Monitors cannot easily be added if they are not natively supported by the language.

Similarities and difference between Semaphore and Monitors

 Even though both the semaphores and monitors are used to achieve mutual exclusion in parallel programming
environments, they differ in the techniques used to achieve this task.
 In monitors, the code that is used to achieve mutual exclusion is in a single place and is more structured, while code for
semaphores are distributed as wait and signal function calls.
 Also, it is very easy to make mistakes when implementing semaphores, while there is a very little chance to make
mistakes when implementing monitors.
 Further, monitors use condition variable, while semaphores do not.
UNIT 3 – MEMORY MANAGEMENT

MEMORY

 Computer Memory is basically known as a collection of data that is represented in binary format.
 Main Memory refers to a physical memory that is the internal memory of the computer.
 The word main is used to distinguish it from external mass storage devices such as disk drives.
 Main memory is also known as RAM.
 The computer is able to change only data that is in the main memory.
 Therefore, every program we execute and every file we access must be copied from a storage device into main memory.

This Memory Hierarchy Design is divided into 2 main types:

1. External Memory or Secondary Memory: Comprising of Magnetic Disk, Optical Disk, and Magnetic Tape i.e. peripheral
storage devices which are accessible by the processor via I/O module.

2. Internal Memory or Primary Memory:

 Comprising of Main Memory, Cache Memory & CPU registers. This is directly accessible by the processor.

a. Cache Memory: A small size of memory that is incredible fast, expensive, and volatile.
b. Random Access Memory: A medium-sized memory of a few gigabytes, medium-priced, medium-fast, and volatile.
c. HDD or SSD: A large-sized memory of up to a terabyte of storage, cheap, slow, and nonvolatile.

MEMORY MANAGEMENT IN OS

Memory Management is the process of controlling and coordinating computer memory, assigning portions known as blocks
to various running programs to optimize the overall performance of the system. It is the most important function 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 also keeps the count of how much memory can be allocated to a process.
 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.

NEED FOR MEMORY MANAGEMENT IN OS - Memory management technique is needed for the following reasons:

 This technique helps in placing the programs in memory in such a way so that memory is utilized at its fullest extent.
 This technique helps to protect different processes from each other so that they do not interfere with each other's
operations.
 It helps to allocate space to different application routines.
 This technique allows you to check how much memory needs to be allocated to processes that decide which processor
should get memory at what time.
 It keeps the track of each memory location whether it is free or allocated.
 This technique keeps the track of inventory whenever memory gets freed or unallocated and it will update the status
accordingly.

Variable (or dynamic) Partitioning in Operating System

In operating systems, Memory Management is the function responsible for allocating and managing computer‟s main
memory. Memory Management function keeps track of the status of each memory location, either allocated or free to ensure
effective and efficient use of Primary Memory.

There are two Memory Management Techniques: Contiguous, and Non-Contiguous. In Contiguous Technique, executing
process must be loaded entirely in main-memory. Contiguous Technique can be divided into:
CONTIGUOUS MEMORY ALLOCATION

 In contiguous memory allocation, whenever any user process request for the memory then a single section of the
contiguous memory block is allocated to that process according to the requirements of the process.

 In this, all the available memory space remains together at one place and freely available memory partitions are not
distributed here and there across the whole memory space.

 The process of dividing the memory into sections is called memory partitioning.

 For better utilization of memory and flow of execution, the memory is partitioned into blocks of different sizes to
accommodate the program.

 The partitioned allocation technique was introduced to solve the problem of wasted time and memory.

There are two ways in which memory can be partitioned:

1. Fixed-Size Partition Allocation

In partition allocation, main memory is divided into a number of partitions, whereas in fixed size of partitioning allocation,
partition can be of equal or different sizes. This technique is also known as static partitioning.

 In this scheme, the system divides the memory into fixed-size partitions. The partitions may or may not be the same size.
 The size of each partition is fixed as indicated by the name of the technique and it cannot be changed.
 In this partition scheme, each partition may contain exactly one process.
 There is a problem that this technique will limit the degree of multiprogramming because the number of partitions will
basically decide the number of processes.
 Whenever any process terminates then the partition becomes available for another process.
 As the allocation of memory is contiguous, no spanning is allowed.
 In fixed partitioning, the partitions are made either before execution or during system configuration.

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

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

 If there is some wastage inside the partition then it is termed Internal Fragmentation.
WHAT IS FRAGMENTATION?

 Processes are stored and removed from memory, which creates free memory space, which are too small to use by other
processes.

 After sometimes, that processes not able to allocate to memory blocks because its small size and memory blocks always
remain unused is called fragmentation.

 This type of problem happens during a dynamic memory allocation system when free blocks are quite small, so it is not
able to fulfill any request.

 Two types of Fragmentation methods are: External fragmentation and Internal fragmentation

External fragmentation can be reduced by rearranging memory contents to place all free memory together in a single block.

The internal fragmentation can be reduced by assigning the smallest partition, which is still good enough to carry the entire
process.

Internal Fragmentation External Fragmentation

Internal Fragmentation occurs when the memory blocks of External Fragmentation occurs when the memory blocks of
fixed-size are allocated to the processes. variable-size are allocated to the processes dynamically.

This type of fragmentation mainly occurs when the fixed When the memory space in the system can easily satisfy the
size partition is assigned to a process whose size is less requirement of the processes, but this available memory space
than the size of the partition due to which the rest of the is non-contiguous, So it can‟t be utilized further.
space in the partition becomes unusable.
The difference between allocated memory and memory Unused memory spaces between the non-contiguous memory
required by a process is called Internal fragmentation. fragments that are too small to serve a new process are called
External fragmentation.

It mainly refers to the unused space in the partition that It mainly refers to the unused blocks of the memory that are
resides in the allocated region; as suggested by the name. not contiguous and hence are unable to satisfy the
requirements of the process.

Best-fit block can be used to overcome the problem of Compaction, segmentation, and paging can be used to
Internal fragmentation.
overcome the problem of External fragmentation.

Paging suffers from Internal fragmentation. First-fit and best-fit suffer from external fragmentation.

ADVANTAGES OF FIXED SIZE PARTITION SCHEME

This scheme is simple and is easy to implement.

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

DISADVANTAGES OF FIXED SIZE PARTITION SCHEME

1. Internal Fragmentation: Suppose the size of the process is lesser than the size of the partition in that case some size of
the partition gets wasted and remains unused. This wastage inside the memory is generally termed as Internal fragmentation.
As we have shown in the above diagram the 70 KB partition is used to load a process of 50 KB so the remaining 20 KB got
wasted.

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

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

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

2. Variable-Size Partition Allocation

This scheme is also known as dynamic partitioning and came into existence to overcome the drawback i.e. internal
fragmentation that is caused by static partitioning.

 In this partitioning, scheme allocation is done dynamically.


 In dynamic partitioning, the primary memory is emptied, and partitions are made during the run time according to the
needs of the different processes.
 The size of the partition is not declared initially.
 The partition size varies according to the need of the
processes.
 Whenever any process arrives, a partition of size equal to
the size of the process is created and then allocated to the
process.
 Thus the size of each partition is equal to the size of the
process.
 As partition size varies according to the need of the
process so in this partition scheme there is no internal
fragmentation.

EXAMPLE

 It is important to note that these partitions are allocated to


the processes at run-time.
 There is no wastage inside the partition then it is termed
as no internal fragmentation.

ADVANTAGES OF VARIABLE-SIZE PARTITION SCHEME


1. No Internal Fragmentation: As in this partition scheme space in the main memory is allocated strictly according to the
requirement of the process thus there is no chance of internal fragmentation. Also, there will be no unused space left in the
partition.

2. Degree of Multiprogramming is Dynamic: As there is no internal fragmentation in this partition scheme due to which
there is no unused space in the memory. Thus more processes can be loaded into the memory at the same time.

3. No Limitation on the Size of Process: In this partition scheme as the partition is allocated to the process dynamically
thus the size of the process cannot be restricted because the partition size is decided according to the process size.

DISADVANTAGES OF VARIABLE-SIZE PARTITION SCHEME

1. External Fragmentation: As there is no internal fragmentation which is an advantage of using this partition scheme does
not mean there will no external fragmentation. Let us understand this with the help of an example: In the above diagram
process P1(3MB) and process P3(8MB) completed their execution. Hence there are two spaces left i.e. 3MB and 8MB. Let‟s
there is a Process P4 of size 15 MB comes.

But the empty space in memory cannot be allocated as no spanning is allowed in contiguous allocation. Because the rule
says that process must be continuously present in the main memory in order to get executed. Thus it results in External
Fragmentation.

2. Difficult Implementation (Complex Memory Allocation): The implementation of this partition scheme is difficult as
compared to the Fixed Partitioning scheme as it involves the allocation of memory at run time rather than during the system
configuration.

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

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

NON-CONTIGUOUS ALLOCATION

Paging and Segmentation are the two ways which allow a process‟s physical address space to be non-contiguous. It
has advantage of reducing memory wastage but it increases the overheads due to address translation. It slows the execution
of the memory because time is consumed in address translation.

In non-contiguous allocation, Operating system needs to maintain the table which is called Page Table for each process
which contains the base address of the each block which is acquired by the process in memory space. In non-contiguous
memory allocation, different parts of a process are allocated different places in Main Memory. Spanning is allowed which is
not possible in other techniques like Dynamic or Static Contiguous memory allocation. That‟s why paging is needed to
ensure effective memory allocation. Paging is done to remove External Fragmentation.

Working: Here a process can be spanned across different spaces in main memory in non-consecutive manner. Suppose
process P of size 4KB. Consider main memory have two empty slots each of size 2KB. Hence total free space is, 2*2= 4 KB. In
contiguous memory allocation, process P cannot be accommodated as spanning is not allowed.

In contiguous allocation, space in memory should be allocated to whole process. If not, then that space remains unallocated.
But in Non-Contiguous allocation, process can be divided into different parts and hence filling the space in main memory. In
this example, process P can be divided into two parts of equal size – 2KB. Hence one part of process P can be allocated to
first 2KB space of main memory and other part of process P can be allocated to second 2KB space of main memory. Below
diagram will explain in better way:
But, in what manner we divide a process to allocate them into main memory is very important to understand. Process is
divided after analyzing the number of empty spaces and their size in main memory. Then only we divide our process. It is
very time consuming process. Their number as well as their sizes changing every time due to execution of already present
processes in main memory.

In order to avoid this time consuming process, we divide our process in secondary memory in advance before reaching the
main memory for its execution. Every process is divided into various parts of equal size called Pages. We also divide our main
memory into different parts of equal size called Frames. It is important to understand that:

Size of page in process = Size of frame in memory

Although their numbers can be different. Below diagram will make you understand in better way: consider empty main
memory having size of each frame is 2 KB, and two processes P1 and P2 are 2 KB each.

Resolvent main memory,


In conclusion we can say that, Paging allows memory address space of a process to be non-contiguous. Paging is more
flexible as only pages of a process are moved. It allows more processes to reside in main memory than Contiguous memory
allocation.

Address Binding

 Programs are stored on the secondary storage disks as binary executable files.
 When the programs are to be executed they are brought into the main memory and placed within a process.
 The collection of processes on the disk waiting to enter the main memory forms the input queue.
 One of the processes which are to be executed is fetched from the queue and placed in the main memory.
 During the execution it fetches instruction and data from main memory.
 After the process terminates it returns back the memory space.
 During execution the process will go through different steps and in each step the address is represented in different
ways.
 In source program the address is symbolic.
 The compiler converts the symbolic address to re-locatable address.
 The loader will convert this re-locatable address to absolute address.

Process Address Space

The process address space is the set of logical addresses that a process references in its code. The operating system takes
care of mapping the logical addresses to physical addresses at the time of memory allocation to the program.

There are three types of addresses used in a program before and after memory is allocated:

S.N. Memory Addresses & Description


1 Symbolic addresses – The addresses used in a source code The variable names, constants, and instruction labels are
the basic elements of the symbolic address space
2 Relative addresses – At the time of compilation, a compiler converts symbolic addresses into relative addresses
3 Physical addresses – The loader generates these addresses at the time when a program is loaded into main memory

LOGICAL VS. PHYSICAL ADDRESS SPACE

The concept of a logical address space that is bound to a separate physical address space is central to proper memory
management.

Logical Address is generated by CPU while a program is running. The logical address is virtual address as it does not exist
physically, therefore, it is also known as Virtual Address. This address is used as a reference to access the physical memory
location by CPU. The term Logical Address Space is used for the set of all logical addresses generated by a program‟s
perspective.
The hardware device called Memory-Management Unit is used for mapping logical address to its corresponding physical
address.

Physical Address identifies a physical location of required data in a memory. The user never directly deals with the physical
address but can access by its corresponding logical address. The user program generates the logical address and thinks that
the program is running in this logical address but the program needs physical memory for its execution, therefore, the logical
address must be mapped to the physical address by MMU before they are used. The term Physical Address Space is used for
all physical addresses corresponding to the logical addresses in a Logical address space.

S. Logical Address Physical Address


N.
1. Users can access the logical address of the User can never access the physical address of the Program
Program.
2. The logical address is generated by the CPU. The physical address is located in the memory unit.
3. The user can access the physical address with A physical address can be accessed by a user indirectly but not
the help of a logical address. directly.
4. The logical address does not exist physically The physical address is a location in the memory. Thus it can be
in the memory and thus termed as a Virtual accessed physically.
address.
5. The set of all logical addresses that are The set of all physical addresses corresponding to the Logical
generated by any program is referred to as addresses is commonly known as Physical Address Space.
Logical Address Space.
6. This address is generated by the CPU. It is computed by the Memory Management Unit (MMU).

MEMORY MANAGEMENT UNIT (MMU)

The runtime mapping from virtual (logical) to physical address is done by the memory management unit (which is a hardware
device. MMU uses following mechanism to convert virtual address to physical address

MMU Example

 The value in the base register is added to every address generated by a user process, which is treated as offset at the
time it is sent to memory.

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

 It is important to note that the user program never sees the real physical addresses.

 The Program can create a pointer to location 356 and store it in the memory and then manipulate it after that compare
it with other addresses as number 356.
METHODS INVOLVED IN MEMORY MANAGEMENT

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

MEMORY MANAGEMENT WITHOUT SWAPPING OR PAGING

Paging is a technique in which the main memory of computer system is organized in the form of equal sized blocks
called pages. In this technique, the address of occupied pages of physical memory is stored in a table, which is known as
page table.

Paging enables the operating system to obtain data from the physical memory location without specifying lengthy memory
address in the instruction. In this technique, the virtual address is used to map the physical address of the data. The length of
virtual address is specified in the instruction and is smaller than physical address of the data. It consists of two different
numbers, first number is the address of page called virtual page in the page table and the second number is the offset value
of the actual data in the page.

 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 storage mechanism that allows OS to retrieve processes from the secondary storage into the main memory
in the form of pages.
 In other words, paging is a memory management technique in which process address space is broken into blocks of the
same size called pages.
 Similarly, the main memory is divided into small fixed-sized blocks of (physical) memory called frames.
 The size of a frame should be kept the same as that of a page to have maximum utilization of the main memory.
 Paging helps to avoid external fragmentation and the need for compaction.
 Paging is used for faster access to data, and it is a logical concept.
 Paging permits the physical address space of a process to be non-contiguous. It is a fixed-size partitioning scheme.

In paging, the logical address space is divided into fixed-sized blocks known as Pages. The physical address space is also
divided into fixed-size blocks known as Frames.

A page is mapped into a frame. The individual pages and frames are recognized by unique numbers known as the page
number and frame number respectively.

The size of the page is equal to the size of a frame. A page number forms a part of the logical address and a frame number
forms a part of a physical address.
ADDRESS TRANSLATION

Translation of Logical Address into Physical Address

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

a. The CPU always generates a logical address.


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

 Following steps are followed to translate logical address into physical address:

Step-1: Page address is called logical address and represented by page number and the offset.

Logical Address = Page Number (p) + Page Offset (d)


Where,

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

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

Step-2: For the page number generated by the CPU,

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

Step-3: Frame address is called physical address and represented by a frame number and the offset.

Physical Address = Frame Number (f) + Page Offset (d)

Where,
Frame Number specifies the specific frame where the required page is stored.
Page Offset specifies the specific word that has to be read from that page.

PAGE TABLE IN OS

 Page table is a data structure.


 It maps the page number referenced by the CPU to the frame number where that page is stored.

Characteristics:

 Page table is stored in the main memory.


 Number of entries in a page table = Number of pages in which the process is divided.
 Page Table Base Register (PTBR) contains the base address of page table.
 Each process has its own independent page table.
 Thus page table mainly provides the corresponding frame number (base address of the frame) where that page is stored
in the main memory.

Diagram: The following diagram illustrates the above steps of translating logical address into physical address:

The above diagram indicates the translation of the Logical address into the Physical address.

 The PTBR in the above diagram means page table base register and it basically holds the base address for the page table
of the current process.

 The PTBR is mainly a processor register and is managed by the operating system.

 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

 Commonly, each process running on a processor needs its own logical address space.

 But there is a problem with this approach and that is with the time required to access a user memory location.

 Suppose if we want to find the location i, we must first find the index into the page table by using the value in the PTBR
offset by the page number for i. And this task requires memory access.

 It then provides us the frame number which is combined with the page offset in order to produce the actual address.

 After that, we can then access the desired place in the memory.

EXAMPLE

 For example, if the main memory size is 16 KB and Frame size is 1 KB.
 Here, the main memory will be divided into the collection of 16 frames of 1 KB each.
 There are 4 separate processes in the system that is A1, A2, A3, and A4 of 4 KB each.
 Here, all the processes are divided into pages of 1 KB each so that operating system can store one page in one frame.
 At the beginning of the process, all the frames remain empty so that all the pages of the processes will get stored in a
contiguous way.
In this example you can see that A2 and A4 are moved to the waiting state after some time.

 Therefore, eight frames become empty, and so other pages can be loaded in that empty blocks.
 The process A5 of size 8 pages (8 KB) are waiting in the ready queue.

 In this example, you can see that there are eight non-contiguous frames which is available in the memory, and paging
offers the flexibility of storing the process at the different places.

 This allows us to load the pages of process A5 instead of A2 and A4.

Advantages of Paging

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


 With the help of paging, the problem of external fragmentation is solved.
 Simplest methods is to implement the page table as a set of registers Paging is assumed as an efficient memory
management technique.
 Due to equal size of the pages and frames, swapping becomes very easy.

Disadvantages of Paging

 Page table requires extra memory space, so may not be good for a system having small RAM.
 May cause Internal fragmentation by this technique.
 There is an increase in time taken to fetch the instruction since now two memory accesses are required.
 Complex memory management algorithm.
 Multi-level paging may lead to memory reference overhead.

SEGMENTATION

Segmentation refers to the technique of dividing the physical memory space into multiple blocks. Each block has
specific length and is known as a segment. Each
segment has a starting address called the base
address. The length of the segment determines the
availability memory space in the segment.

The above figure shows the organization of


segment in memory unit.

The location of data values stored in the segment


can be determined by the distance of actual
position of data value from base address of the
segment. The distance between the actual position
of data and the base address of segment is known
as displacement or offset value. In other words,
when there is a need to obtain data from required
segmented memory then the actual address of data
is calculated by adding the base address of the
segment with offset value.

There are types of segmentation:

1. Virtual memory segmentation - Each process is divided into a number of segments, not all of which are resident at any
one point in time.

2. Simple segmentation - Each process is divided into a number of segments, all of which are loaded into memory at run
time, though not necessarily contiguously.

There is no simple relationship between logical addresses and physical addresses in segmentation. A table stores the
information about all such segments and is called Segment Table.

Segment Table – It maps two-dimensional Logical address into one-dimensional Physical address. It‟s each table entry has:

 Base Address: It contains the starting physical address where the segments reside in memory.

 Limit: It specifies the length of the segment.


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

Why Segmentation is needed?

Initially, we used the Paging technique for memory management. But instead of the user, it is closer to the operating system
more probably. In paging, all the processes are split into the pages. Sometimes there may be a situation where a process
having some relevant sections of the function requires being loaded on the same page.

The operating system does not mind the process from the user‟s point of view. The operating system split the same function
into the number of pages, and at the same time, the pages may be loaded or not loaded into the memory. Due to this
reason, system efficiency could be decreased.

So, it is better to use the segmentation technique for memory management. In segmentation, the process is split into the
segments. In this, every segment consists of a similar kind of function as a library function contained in one segment, and the
main function contained in the other segment.

Advantages of Segmentation

 No internal fragmentation
 Average Segment Size is larger than the actual page size
 Less overhead
 It is easier to relocate segments than entire address space
 The segment table is of lesser size as compared to the page table in paging
Disadvantages of Segmentation

 It can have external fragmentation


 it is difficult to allocate contiguous memory to variable sized partition
 Costly memory management algorithms

PAGING VS. SEGMENTATION

S.N. Paging Segmentation


1 Non-Contiguous memory allocation Non-contiguous memory allocation
2 Paging divides program into fixed size pages. Segmentation divides program into variable size segments.
3 OS is responsible Compiler is responsible.
4 Paging is faster than segmentation Segmentation is slower than paging
5 Paging is closer to Operating System Segmentation is closer to User
6 It suffers from internal fragmentation It suffers from external fragmentation
7 There is no external fragmentation There is no external fragmentation
8 Logical address is divided into page number and Logical address is divided into segment number and segment
page offset offset
9 Page table is used to maintain the page Segment Table maintains the segment information
information.
10 Page table entry has the frame number and some Segment table entry has the base address of the segment
flag bits to represent details about pages. and some protection bits for the segments.

Paged Segmentation and Segmented Paging

Major Limitation of Single Level Paging - A big challenge with single level paging is that if the logical address space is
large, then the page table may take up a lot of space in main memory. For instance, consider that logical address is 32 bit
and each page is 4 KB, the number of pages will be 2^20 pages. The page table without additional bits will be of the size 20
20
bits * 2 or 2.5 MB. Since each process has its own page table, a lot of memory will be consumed when single level paging is
used. For a system with 64-bit logical address even a page table of single process will not fit in main memory. For a process
with a large logical address space, a lot of its page table entries are invalid as a lot of the logical address space goes unused.

Segmented Paging

A solution to the problem is to use segmentation along with paging to reduce the size of page table. Traditionally, a program
is divided into four segments, namely code segment, data segment, stack segment and heap segment.

Segments of a process

The size of the page table can be reduced by creating a page table for each segment. To accomplish this hardware support is
required. The address provided by CPU will now be partitioned into segment no., page no. and offset.

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

1. The page table size is reduced as pages are present only for data of segments, hence reducing the memory
requirements.
2. Gives a programmers view along with the advantages of paging.
3. Reduces external fragmentation in comparison with segmentation.
4. Since the entire segment need not be swapped out, the swapping out into virtual memory becomes easier .

Disadvantages of Segmented Paging

1. Internal fragmentation still exists in pages.


2. Extra hardware is required
3. Translation becomes more sequential increasing the memory access time.
4. External fragmentation occurs because of varying sizes of page tables and varying sizes of segment tables in today‟s
systems.

Paged Segmentation

1. In segmented paging, not every process has the same number of segments and the segment tables can be large in size
which will cause external fragmentation due to the varying segment table sizes. To solve this problem, we use paged
segmentation which requires the segment table to be paged. The logical address generated by the CPU will now consist
of page no #1, segment no, page no #2 and offset.

2. The page table even with segmented paging can have a lot of invalid pages. Instead of using multi-level paging along
with segmented paging, the problem of larger page table can be solved by directly applying multi-level paging instead
of segmented paging.
Advantages of Paged Segmentation

1. No external fragmentation
2. Reduced memory requirements as no. of pages limited to segment size.
3. Page table size is smaller just like segmented paging,
4. Similar to segmented paging, the entire segment need not be swapped out.

Disadvantages of Paged Segmentation

1. Internal fragmentation remains a problem.


2. Hardware is complexer than segmented paging.
3. Extra level of paging at first stage adds to the delay in memory access.

SWAPPING
Swapping is the technique used by an operating system for efficient management of memory space of a computer
system. Swapping involves performing two tasks called swapping in and swapping out. The task of placing the pages
or blocks of data from the hard disk to the main memory is called swapping in. On the other hand, the task of removing
pages or blocks of data from main memory to the hard disk is called swapping out. The swapping technique is useful when
larger program is to be executed or some operations have to be performed on a large file.

 Swapping is a mechanism in which a process (inactive programs) can be swapped temporarily out of main memory (or
move) to secondary storage (disk) and make that memory available to other processes. It will be later brought back into
the memory for continue execution.

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

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

 In the above diagram, suppose there is a multiprogramming environment with a round-robin scheduling algorithm;
whenever the time quantum expires then the memory manager starts to swap out those processes that are just finished
and swap another process into the memory that has been freed.

 And in the meantime, the CPU scheduler allocates the time slice to some other processes in the memory.

 A variant of the swapping technique is the priority-based scheduling algorithm.


 If any higher-priority process arrives and wants service, then the memory manager swaps out lower priority processes
and then load the higher priority processes and then execute them.

 When the process with higher priority finishes then the process with lower priority swapped back in and continues its
execution.

 This variant is sometimes known as roll in and roll out.

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

Swap In and Swap Out in OS - The concept of swapping consists of two more concepts, swap in and swap out.

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

 On the other hand, Swap In is the procedure by which any process gets removed from the hard disk and placed it back
into the main memory or RAM.

TIME TAKEN FOR SWAPPING

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

ADVANTAGES OF SWAPPING

 It offers a higher degree of multiprogramming.

 Allows dynamic relocation. For example, if address binding at execution time is being used, then processes can be swap
in different locations. Else in case of compile and load time bindings, processes should be moved to the same location.

 It helps to get better utilization of memory.

 Minimum wastage of CPU time on completion so it can easily be applied to a priority-based scheduling method to
improve its performance.

 This technique helps to create and use virtual memory.

 The method is economical.

 Swapping makes a CPU perform several tasks simultaneously. Hence, processes do not have to wait for too long before
they are executed.

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

DISADVANTAGES OF SWAPPING

 There may occur inefficiency in the case if a resource or a variable is commonly used by those processes that are
participating in the swapping process.

 If the algorithm used for swapping is not good then the overall method can increase the number of page faults and thus
decline the overall performance of processing.

 If the computer system loses power at the time of high swapping activity then the user might lose all the information
related to the program.
 Although the process has no significant or effective disadvantage, the overall method depends heavily on the virtual
memory, such dependability results in a significant performance drop.

S.N. PAGING SWAPPING


1 Paging is a memory management technique in Swapping is a technique that is used to temporarily remove
which computer stores and retrieves data from the the inactive programs from the main memory of the
secondary storage for use in the main memory. computer system
2 This technique allows more processes to reside in Swapping allows fewer processes to reside in the main
the main memory memory.
3 Paging follows non-contiguous memory Swapping can be done without any memory management
management. technique.
4 Paging is more flexible because in this there is the Swapping is less flexible as the entire process moves back
movement of pages of a process. and forth in the main memory and back store.
5 Paging occurs when some part of the process is While Swapping occurs when the whole process is transferred
transferred to the disk. to the disk.
6 For medium workloads paging technique is For heavy workloads swapping technique is suitable.
suitable.
7 This technique allows the memory address space of With the help of Swapping multiple processes can run in
a process to be noncontiguous. parallel in the Operating System
8 This technique helps to implement virtual memory. Swapping helps the CPU to access processes faster.

VIRTUAL MEMORY IN OS

 The primary memory (such as RAM) is expensive and limited in the system; however, the secondary memory is cheaper
and large and can be extended easily.

 Virtual memory concept offers unique solution for apparent primary memory expansion.

 Virtual memory is a storage mechanism which offers user an illusion of having a very big main memory. It is done by
treating a part of secondary memory as the main memory.

 It essentially with the help of OS, and the software and hardware installed on the system helps to access more memory
than what actually is installed on the system, by transferring data from the RAM to the disk.

 In Virtual memory, the user can store processes with a bigger size than the available main memory.

 Instead of loading one long process in the main memory, the OS loads the various parts of more than one process in the
main memory.

 In an operating system, the memory is usually stored in the form of units that are known as pages.

 Basically, these are atomic units used to store large programs.

 Virtual memory is mostly implemented with demand paging and segmentation.

DEMAND PAGING IN OPERATING SYSTEM

 This is also known as lazy swapper because page is not swapped into the memory unless it is required.

 A demand paging mechanism is very much similar to a paging system with swapping where processes stored in the
secondary memory and pages are loaded only on demand, not in advance.

 When a context switch occurs, the operating system does not copy any of the old program‟s pages out to the disk or
any of the new program‟s pages into the main memory.

 Instead, it will start executing the new program after loading the first page and fetches the program's pages, which are
referenced.
 During the program execution, if the program references a page that may not be available in the main memory because
it was swapped, then the processor considers it as an invalid memory reference as a page fault and transfers control
back from the program to the OS, which demands to store page back into the memory.

PAGE FAULT

 In this technique we need some hardware support to distinct between the pages that are in memory and those that are
on the disk. A valid and invalid bit is used for this purpose.

 When this bit is set to valid it indicates that the associate page is in memory.

 If the bit is set to invalid it indicates that the page is either not valid or is valid but currently not in the disk.

 It mainly occurs when any program tries to access the data or the code that is in the address space of the program, but
that data is not currently located in the RAM of the system.

 So basically when the page referenced by the CPU is not found in the main memory then the situation is termed as Page
Fault.

 Whenever any page fault occurs, then the required page has to be fetched from the secondary memory into the main
memory.

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

 The page fault mainly generates an exception, which is used to notify the operating system that it must have to retrieve
the "pages" from the virtual memory in order to continue the execution.

 Once all the data is moved into the physical memory the program continues its execution normally.

PROCEDURE TO HANDLE PAGE FAULT


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

 The main steps involved in demand paging that is in between the page is requested and it is loaded into main memory
are as follows:

1. CPU refers to the page it needs. The referred page is checked in page table whether that is present in main memory or
not. If not an interrupt page fault is generated.

2. OS puts the interrupts process in blocking state and starts the process of fetching the page from memory so that
process can be executed.

3. OS will search for it in logical address space.

4. 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. Page table will be updated.

6. CPU is informed about the update and asked to go ahead with the execution and process gets back into the ready state
i.e. interrupted process will be restarted.

Advantages of Demand Paging

 Large virtual memory.


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

Disadvantages of Demand Paging

 Number of tables and amount of processor overhead for handling page interrupts are greater than in the case of the
simple paged management techniques.
 Due to the lack of explicit constraints on jobs address space size.

PAGE REPLACEMENT

 When all the frames are in main memory, it is necessary to bring a new page to satisfy the page fault, replacement policy
is concerned with selecting a page currently in memory to be replaced.

 Page replacement is required when:

1. All the frames of main memory are already occupied.


2. Thus, a page has to be replaced to create a room for the required page.

PAGE REPLACEMENT ALGORITHM

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

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

 Page replacement algorithms are the techniques using which an OS decides which memory pages to swap out, write to
disk when a page of memory needs to be allocated.

 A page replacement algorithm looks at the limited information about accessing the pages provided by hardware, and
tries to select which pages should be replaced to minimize the total number of page misses, while balancing it with the
costs of primary storage and processor time of the algorithm itself.
TYPES OF PAGE REPLACEMENT ALGORITHM

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

Page Fault: The CPU demanded page is not present in memory.


Page Hit: The CPU demanded page is present in memory.

A good page replacement algorithm is one that minimizes the number of page faults.

1. FIFO (FIRST IN FIRST OUT) ALGORITHM

 This is the simplest page replacement algorithm.


 As the name suggests, this algorithm works on the principle of “First in First out“.
 It replaces the oldest page that has been present in the main memory for the longest time.
 It is implemented by keeping track of all the pages in a queue.
 This is not an effective way of page replacement but it can be used for small systems.

Example: Consider the following references string with frames initially empty.

 The first three references (7,0,1) cases page faults and are brought into the empty frames.
 The next references 2 replaces page 7 because the page 7 was brought in first.
 Since 0 is the next references and 0 is already in memory i.e. no page faults.
 The next references 3 results in page 0 being replaced so that the next references to 0 causer page fault. This will
continue till the end of string.

Analysis:

 Number of Page references = 20


 Number of Page faults = 15
 Number of Page Hits = 5
 Page Hit Ratio = Number of Page Hits / Total Number of Page references
= (5 / 20)*100
= 25%

 Page Fault Ratio = Number of Page Faults / Total Number of Page references
= (15 / 20)*100
= 75%

Advantages

 This algorithm is simple and easy to use.


 FIFO does not cause more overhead.

Disadvantages

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

BELADY S A NOMALY

 Belady found that, in FIFO page replacement algorithm, the number of page faults will get increased with the increment
in number of frames.

 FIFO replacement algorithm may face this problem.

 This is the strange behavior shown by FIFO algorithm in some of the cases. This is an Anomaly called as Belady‟s
Anomaly.

 However, in the case of LRU and optimal page replacement algorithms, it is seen that the number of page faults will be
reduced if we increase the number of frames.

EXAMPLE OF BELADY’S ANOMALY

 The reference String is given as 0 1 5 3 0 1 4 0 1 5 3 4.


 Let's analyze the behavior of FIFO algorithm in two cases.
2. LEAST RECENTLY USED (LRU) ALGORITHM

 As the name suggests, this algorithm works on the principle of “Least Recently Used“.
 The page that has not been used for the longest time in the main memory will be selected for replacement.
 This algorithm is easy to implement.
 This algorithm makes use of the counter along with the even page.

Example:

 Consider the following references string with frames initially empty.

 The first 5 faults are similar to optimal replacement.


 When reference to page 4 occurs, LRU sees that of the three frames, page 2 as used least recently.
 The most recently used page is page 0 and just before page 3 was used.
 The LRU policy is often used as a page replacement algorithm and considered to be good.

Analysis:

 Number of Page references = 20


 Number of Page faults = 12
 Number of Page Hits = 08
 Page Hit Ratio = Number of Page Hits / Total Number of Page references
= (8 / 20)*100
= 40%
 Page Fault Ratio = Number of Page Faults / Total Number of Page references
= (12 / 20)*100
= 60%

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

Disadvantages of LRU

 It is expensive and has more complexity.


 There is a need for an additional data structure.

3. OPTIMAL ALGORITHM

 Optimal page replacement algorithm is mainly to solve the problem of Belady‟s Anomaly.
 This algorithm mainly replaces the page that will not be used for the longest time in the future.
 The practical implementation of this algorithm is not possible.
 This is because we cannot predict in advance those pages that will not be used for the longest time in the future.
 However, it is the best known algorithm and gives the least number of page faults.
 Hence, this algorithm can be used to measure the performance of other algorithms.

Example: Consider the following references string with frames initially empty.

 The first three references cause faults that fill the three empty frames.
 The references to page 2 replaces page 7, because 7 will not be used until reference 18.
 The page 0 will be used at 5 and page 1 at 14.
 With only 9 page faults, optimal replacement is much better than a FIFO, which had 15 faults.
 This algorithm is difficult t implement because it requires future knowledge of reference strings.

Analysis:

 Number of Page references = 20


 Number of Page faults = 09
 Number of Page Hits = 11
 Page Hit Ratio = Number of Page Hits / Total Number of Page references
= (11 / 20)*100
= 55%
 Page Fault Ratio = Number of Page Faults / Total Number of Page references
= (9 / 20)*100
= 45%

Advantages of OPR

 This algorithm is easy to use.


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

Disadvantages of OPR

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


 Practical Implementation is not possible because the operating system is unable to track the future request
4. NRU ALGORITHM

It is a page replacement algorithm. This algorithm removes a page at random from the lowest numbered non-empty class.
Implicit in this algorithm is that it is better to remove a modified page that has not been referenced in at least one clock tick
than a clean page that is in heavy use.

It is easy to understand, moderately efficient to implement and gives a performance that while certainly not optimal, may be
adequate. When page is modified, a modified bit is set. When a page needs to be replaced, the Operating System divides
pages into 4 classes.

 0:- Not Referenced, Not Modified


 1:- Not Referenced, Modified
 2:- Referenced, Not Modified
 3:- Referenced, Modified

Out of above 4 categories, NRU will replace a Not Referenced, Not Modified page, if such page exists. Note that this
algorithm implies that a Modified but Not Referenced is less important than a Not Modified and Referenced.

Example

Page Referenced Modified


0 1 0
1 0 1
2 0 0
3 1 1

 Case-1 implies to Not Referenced and Modified.


 Case-2 implies to Not Referenced and Not Modified.
 Case-3 implies to Referenced and Modified.
 Case-0 implies to Referenced and Not Modified.

Algorithm: From the given reference string NRU will remove a page at random from the lowest numbered nonempty class.
Implicit in this algorithm is that it is better to remove a modified page that has not been referenced in at least one clock tick
(typically 20 msec) than a clean page that is in heavy use.

5. RANDOM PAGE REPLACEMENT ALGORITHM

 As the name suggests, this algorithm randomly replaces any page.

 So, this algorithm may behave like any other algorithm like FIFO, LIFO, LRU, Optimal etc.
 In page replacement using a random algorithm (random) every time a page fault occurs, the replaced page is randomly
selected.

 This technique does not use any information in determining which pages to replace, all pages in main memory have the
same weight to be selected.

 How to select random or arbitrary pages, including the page that is currently referenced (pages that should not be
replaced).

 This random technique is very bad, the experiment shows the random algorithm causes a very high frequency of page
faults.

6. SECOND CHANCE ALGORITHM

 The second chance algorithm (which some people call the clock algorithm) works just like FIFO, but it skips over any
pages with the use bit set (and clears the use bit).

 Second chance page replacement algorithm is a modified version of FIFO algorithm that avoids the problem of heavily
used page.

 It inspects the R bit if it is 0, the page is both old and unused, so it is replaced immediately.

 If the R bit is 1, the bit is cleared, the page is put onto the end of the list of pages, and its load time is updated as though
it had just arrived in memory. Then the search continues.

 The operation of this algorithm is called second chance.

 As the name suggests, the pages are given a second chance. The pages that arrived are stored in a linked list. If the
oldest page in the linked list gets referenced, a page fault occurs and the R bit is cleared. The oldest page is now cleared
from memory and is pushed to the latest clock interval.

 When the time arrives to replace a page, the operating system replaces the oldest page that has also not been
referenced.

 In a rare situation where all the pages have been referenced, the algorithm goes back to its own roots and performs the
simple FIFO algorithm.

7. CLOCK ALGORITHM

 The Second Chance Page Replacement algorithm has a drawback that it constantly moves the pages around on its list.
 To solve this, the OS keeps all the page frames on a circular list in the form of a clock.
 The hand of the clock (pointer) points to the oldest page.
 When a page fault takes place, the page being pointed is inspected.
 If its R bit is 0, the page is evicted and the new page is inserted into its place. The pointer moves one position ahead.
 On the other hand, if R is 1, then it‟s cleared and the hand is advanced to the next page until a page with a false R (i.e.
R=0) bit is found.
 Since this works the way a clock works, it is called Clock Page Replacement Algorithm.
 It differs from second chance only in the implementation.

UNIT 4 - FILE SYSTEM

WHAT IS A FILE?

 File is a collection of similar records or a file is a container for a


collection of information
 The file is treated as a single entity by users and applications and
may be referred by name.
 Files have unique file names and may be created and deleted.
 The file manager provides a protection mechanism to allow user‟s
administrator how processes executing on behalf of different users
can access the information in a file.
 File protection is a fundamental property of files because it allows different people to store their information on a shared
computer.
 Files are stored in a file system, which may exist on a disk or in the main memory.
 Files can be simple (plain text) or complex (specially formatted).
 The collection of files is known as Directory.
 The collection of directories at the different levels, is known as File System.

FILE ATTRIBUTES

 Every file has a name and its data.


 In addition, all operating systems associate other information with each file, for example, the date and
time the file was created and the file‟s size, these extra items are the file‟s attributes.
 File attributes vary from one operating system to another.
 The common file attributes are:

1. Name:

 Only information kept in human-readable form (i.e. the name which is displayed to the user).
 Every file carries a name by which the file is recognized in the file system.
 One directory cannot have two files with the same name.
2. Identifier:

 Unique tag (number) identifies file within file system.


 Along with the name, each file has its own extension which identifies the type of the file.
 For example, a text file has the extension .txt; a video file can have the extension .mp4.

3. Type:

 Needed for systems that support different types.


 In a file system, the files are classified in different types such as video files, audio files, text files, executable files, etc.

4. Location:

 Pointer to file location on device.


 In the file system, there are several locations on which, the files can be stored.
 Each file carries its location as its attribute.

5. Size:

 Current file size.


 The size of the file is one of its most important attribute.
 By size of the file, we mean the number of bytes acquired by the file in the memory.

6. Protection:

 Controls who can do reading, writing, executing, deleting.


 The admin of the computer may want the different protections for the different files.
 Therefore each file carries its own set of permissions to the different group of users.

7. Time and Date:

 Data for protection, security, and usage monitoring.


 Every file carries a time stamp which contains the time and date on which the file is last modified.

FILE TYPE

File type refers to the ability of the operating system to distinguish different types of file such as text files source files and
binary files etc. Many operating systems support many types of files. Operating system like MS DOS and UNIX have the
following types of files

1. Ordinary Files

 These are the files that contain user information.


 These may have text, databases or executable program.
 The user can apply various operations on such files like add, modify, delete or even remove the entire file.

2. Directory Files

 These files contain list of file names and other information related to these files.

3. Special Files

 These files are also known as device files.


 These files represent physical device like disks, terminals, printers, networks, tape drive etc.
 These files are of two types

1. Character Special Files: data is handled character by character as in case of terminals or printers
2. Block Special Files: data is handled in blocks as in the case of disks and tapes

TYPES OF FILE
File System in OS General Extension Function
Archive arc, zip, tar Related files grouped into one compressed file
Batch bat, sh Commands to the command interpreter
Executable exe, com, bin Read to run machine language program
Multimedia mpeg, mov, rm For containing audio/video information
Object obj, o Compiled, machine language not linked
Source Code C, java, pas, asm, a Source code in various languages
Text txt, doc Textual data, documents
Word Processor wp, tex, rrf, doc Various word processor formats

OPERATIONS ON THE FILE

 A file is a collection of logically related data that is recorded on the secondary storage in the form of sequence of
operations
 The content of the files are defined by its creator who is creating the file
 The various operations which can be implemented on a file such as read, write, open and close etc. are called file
operations
 These operations are performed by the user by using the commands provided by the operating system Some common
operations are as follows:

1. Create Operation

 This operation is used to create a file in the file system


 It is the most widely used operation performed on the file system
 To create a new file of a particular type the associated application program calls the file system
 This file system allocates space to the file As the file system knows the format of directory structure, so entry of this new
file is made into the appropriate directory

2. Open Operation

 This operation is the common operation performed on the file


 Once the file is created, it must be opened before performing the file processing operations
 When the user wants to open a file, it provides a file name to open the particular file in the file system
 It tells the operating system to invoke the open system call and passes the file name to the file system

3. Write Operation

 This operation is used to write the information into a file


 A system call write is issued that specifies the name of the file and the length of the data has to be written to the file
 Whenever the file length is increased by specified value and the file pointer is repositioned after the last byte written

4. Read Operation

 This operation reads the contents from a file


 A Read pointer is maintained by the OS, pointing to the position up to which the data has been read

5. Re Position or Seek Operation

 The seek system call re positions the file pointers from the current position to a specific place in the file i.e. forward or
backward depending upon the user's requirement
 This operation is generally performed with those file management systems that support direct access files

6. Delete Operation

 Deleting the file will not only delete all the data stored inside the file it is also used so that disk space occupied by it is
freed
 In order to delete the specified file the directory is searched
 When the directory entry is located, all the associated file space and the directory entry is released
7. Truncate Operation

 Truncating is simply deleting the file except deleting attributes


 The file is not completely deleted although the information stored inside the file gets replaced

8. Close Operation

 When the processing of the file is complete, it should be closed so that all the changes made permanent and all the
resources occupied should be released
 On closing it deallocates all the internal descriptors that were created when the file was opened

9. Append Operation

 This operation adds data to the end of the file

10. Rename Operation

 This operation is used to rename the existing file.

FILE ACCESS METHODS

 File access mechanism refers to the manner in which the records of a file may be accessed
 Let's look at various ways to access files stored in secondary memory

1. Sequential Access

 This access method is the most primitive one Example


Compilers usually access files in this fashion
 Most of the operating systems access the file sequentially
 In other words, we can say that most of the files need to be
accessed sequentially by the operating system
 In sequential access, the OS read the file word by word A
pointer is maintained which initially points to the base
address of the file

 If the user wants to read first word of the file then the
pointer provides that word to the user and increases its
value by 1 word This process continues till the end of the file

 Modern word systems do provide the concept of direct access and indexed access but the most used method is
sequential access due to the fact that most of the files such as text files, audio files, video files, etc. need to be
sequentially accessed

2. Direct/Random Access

 Random access file organization provides, accessing the records directly


 Each record has its own address on the file with by the help of which it can be
directly accessed for reading or writing
 The Direct Access is mostly required in the case of database systems
 In most of the cases, we need filtered information from the database
 The sequential access can be very slow and inefficient in such cases

 Suppose every block of the storage stores 4 records and we know that the record
we needed is stored in 10 th block

 In that case, the sequential access will not be implemented because it will traverse
all the blocks in order to access the needed record

 Direct access will give the required result despite of the fact that the operating
system has to perform some complex tasks such as determining the desired block
number.

 However, that is generally implemented in database applications.

3. Indexed Access

 This mechanism is built up on base of sequential access.


 An index is created for each file which contains pointers to various blocks.
 Index is searched sequentially and its pointer is used to access the file directly.
 If a file can be sorted on any of the filed then an index can be assigned to a group of certain records
 However, a particular record can be accessed by its index
 The index is nothing but the address of a record in the file
 In index accessing, searching in a large database became very quick and easy but we need to have some extra space in
the memory to store the index value
 In other words, index is searched sequentially and its pointer is used to access the file directly

WHAT IS A DIRECTORY?

 Directory can be defined as the listing of the related files on the disk. In other words, group of files combined is known
as directory.
 A single directory may or may not contain multiple files.
 Information about files is maintained by Directories. In Windows OS, it is called folders.
 The directory may store some or the entire file attributes i.e. a directory contains all information about file, its attributes.

 To get the benefit of different file systems on the different operating systems, a hard disk can be divided into the
number of partitions of different sizes. The partitions are also called volumes or mini disks.
 Each partition must have at least one directory in which, all the files of the partition can be listed.
 Directory itself can be organized in many ways.
 The organization must allow us to insert entries, to delete entries, to search for a named entry, and to list all the entries
in the directory.

Advantages of maintaining directories are:

Efficiency: A file can be located more quickly.

Naming: It becomes convenient for users as two users can have same name for different files or may have different name for
same file.

Grouping: Logical grouping of files can be done by properties e.g. all java programs, all games etc.

DIRECTORY OPERATIONS
The allowed system calls for managing directories exhibit more variation from system to system than system calls for files.

1. Create: A directory is created. It is empty except for dot and dotdot, which are put there automatically by the system (or in
a few cases, by the mkdir program).

2. Delete: A directory is deleted. Only an empty directory can be deleted. A directory containing only dot and dotdot is
considered empty as these cannot usually be deleted.

3. Opendir: Directories can be read. For example, to list all the files in a directory, a listing program opens the directory to
read out the names of all the files it contains. Before a directory can be read, it must be opened, analogous to opening and
reading a file.

4. Closedir: When a directory has been read, it should be closed to free up internal table space.

5. Readdir: This call returns the next entry in an open directory.

Formerly, it was possible to read directories using the usual read system call, but that approach has the disadvantage of
forcing the programmer to know and deal with the internal structure of directories.

In contrast, readdir always returns one entry in a standard format, no matter which of the possible directory structures are
being used.

6. Rename: In many respects, directories are just like files and can be renamed the same way files can be.

7. Link: Linking is a technique that allows a file to appear in more than one directory.

This system call specifies an existing file and a path name, and creates a link from the existing file to the name specified by
the path. In this way, the same file may appear in multiple directories.

A link of this kind, which increments the counter in the file‟s i-node (to keep track of the number of directory entries
containing the file), is sometimes called a hard link.

8. Unlink: A directory entry is removed. If the file being unlinked is only present in one directory (the normal case), it is
removed from the file system.

If it is present in multiple directories, only the path name specified is removed.

STRUCTURES OF DIRECTORY IN OS

A directory is a container that is used to contain folders and files. It organizes files and folders in a hierarchical manner.

Types of File Directories in OS:

1. Single Level Directory


2. Two Level Directory
3. Tree Structured Directory
1. SINGLE LEVEL DIRECTORY

 The single level directory is the simplest directory structure (one big list of all the files on the disk).
 The entire system will contain only one directory which is supposed to mention all the files present in the file system.
 A single level directory has a significant limitation, however, when the number of files increases or when the system has
more than one user.
 Since all the files are in the same directory, they must have a unique name, if two users call their dataset test, then the
unique name rule violated.
 The directory contains one entry per each file present on the file system
 This type of directories can be used for a simple system

o Naming problem: Users cannot have same name for two files.
o Grouping problem: Users cannot group files according to their need.

Advantages

1. Since it is a single directory, so its implementation is very easy


2. If the sizes of the files are very small then the searching becomes faster
3. The operations like file creation, searching, deletion, updating are very simple since we have only one directory
4. Reduced Redundancy

Disadvantages

1. There may chance of name collision because two files cannot have the same name
2. The directory may be very big therefore searching for a file may take so much time
3. There are no ways to group same kind of files

2. TWO LEVEL DIRECTORY

 As we have seen, a single level directory often leads to confusion of files names among different users, the solution to
this problem is each user has its own user file directory (UFD) in two level directory systems
 The system doesn't let a user to enter in the other user's directory without permission
 There is one master file directory (MFD) which contains separate directories dedicated to each user
 The UFDs have similar structures, but each lists only the files of a single user
 When a user job starts or a user logs in, the system‟s master file directory (MFD) is searched. The MFD is indexed by user
name or account number, and each entry points to the UFD for that user.
 Path Name: Due to two levels there is a path name for every file to locate that file.

Advantages

1. Searching of files becomes easier due to pathname and user-grouping.


2. Sharing Directory by users problem is solved.
3. More secure than 1 level directory.
4. Different users can have the same directory as well as the file name.

Disadvantages

1. Grouping problem is still not solved, for example if user wants to create a directory group for all files that are movies or
music, he can‟t do that.
2. A user is not allowed to share files with other users.
3. Still, it not very scalable, two files of the same type cannot be grouped together in the same user.

3. TREE STRUCTURED DIRECTORY

 A tree structure is the most common directory structure. The tree has a root directory, and every file in the system has a
unique path.
 Tree structured directory system overcomes the drawbacks of two level directory system. The similar kind of files can
now be grouped in one directory
 Each user has its own directory and it cannot enter in the other user's directory. However, the user has the permission to
read the root's data but he cannot write or modify this. Only administrator of the system has the complete access of root
directory
 A file can be accessed by two types of path, either relative or absolute
 Absolute path is the path of the file with respect to the root directory of the system while relative path is the path with
respect to the current working directory of the system
Advantages

1. Very general, since full pathname can be given.


2. Very scalable, the probability of name collision is less.
3. Searching becomes very easy, we can use both absolute paths as well as relative.
4. Two files can have same names now, if they are in different sub directories.
5. Grouping is also possible now.

Disadvantages

1. Every file does not fit into the hierarchical model; files may be saved into multiple directories.
2. We cannot share files.
3. It is inefficient, because accessing a file may go under multiple directories.

4. ACYCLIC-GRAPH STRUCTURED DIRECTORIES

 The tree structured directory system doesn't allow the same file to exist in multiple directories therefore sharing is major
concern in tree structured directory system. We can provide sharing by making the directory an acyclic graph.
 In this system, two or more directory entry can point to the same file or sub directory. That file or sub directory is shared
between the two directory entries
 These kinds of directory graphs can be made using links or aliases
 We can have multiple paths for a same file Links can either be symbolic (logical) or hard link (physical).
 If a file gets deleted in acyclic graph structured directory system then

1. In the case of soft link, the file just gets deleted and we are left with a dangling pointer
2. In the case of hard link, the actual file will be deleted only if all the references to it get deleted.

 It is used in the situation like when two programmers are working on a joint project and they need to access files.
 It is the point to note that the shared file is not the same as the copy file.
 If any programmer makes some changes in the subdirectory it will reflect in both subdirectories.

Advantages

1. We can share files.


2. Searching is easy due to different-different paths.

Disadvantages

1. We share the files via linking, in case deleting it may create the problem,
2. If the link is a soft link then after deleting the file we left with a dangling pointer.
3. In the case of a hard link, to delete a file we have to delete all the references associated with it.

5. GENERAL GRAPH DIRECTORY STRUCTURE

 In general graph directory structure, cycles are allowed within a directory structure where multiple directories can be
derived from more than one parent directory.
 The main problem with this kind of directory structure is to calculate the total size or space that has been taken by the
files and directories.

Advantages
1. It allows cycles.
2. It is more flexible than other directories structure.

Disadvantages

1. It is more costly than others.


2. It needs garbage collection.

FILE SYSTEM IN OPERATING SYSTEM

 A set of files and directories contained on a single drive


 File system is the part of the operating system which is responsible for file
management
 It provides a mechanism to store the data and access to the file contents
including data and programs
 Some operating systems treats everything as a file for example Ubuntu

The file manager administers the file system by:

1. Storing the information on a device.


2. Mapping the block storage to a logical view.
3. Allocating/deallocating storage.
4. Providing directories.

FILE SYSTEM STRUCTURE

 Most of the Operating Systems use layering approach for every task including file systems
 Every layer of the file system is responsible for some activities
 The figure shown below, elaborates how the file system is divided in different layers, and also the functionality of each
layer

Fig. Layered file system

1. Application Programs & Logical File System:

 When an application program asks for a file, the first request is directed to the logical file system
 The logical file system contains the metadata i.e. includes all details about a file except the actual contents of the file and
directory structure
 If the application program doesn't have the required permissions of the file then this layer will throw an error
 Logical file systems also verify the path to the file
 It also maintains via file control blocks (FCB).
2. File Organization Module:

 Generally, files are divided into various logical blocks


 Files are to be stored in the hard disk and to be retrieved from the hard disk Hard disk is divided into various tracks and
sectors
 Therefore, in order to store and retrieve the files, the logical blocks need to be mapped to physical blocks This mapping
is done by file organization module
 It is also responsible for free space management

3. Basic File

 Once file organization module decided which physical block the application program needs, it passes this information to
basic file system
 The basic file system is responsible for issuing the commands to I/O control in order to fetch those blocks
 It manages the memory buffers and caches

4. I/O Control Level &

 I/O controls contain the codes by using which it can access hard disk These codes are known as device drivers
 I/O controls are also responsible for handling interrupts
 Device drivers acts as interface between devices and OS, they help to transfer data between disk and main memory

FILE SYSTEM IMPLEMENTATION

We can implement file system by using two types of data structures:

ON-DISK STRUCTURES

 Generally they contain information about total number of disk blocks, free disk blocks, location of them and etc.

 There are various on-disk data structures that are used to implement a file system. This structure may vary depending
upon the operating system.

 Below given are different on-disk structures:

1. Boot Control Block – It is usually the first block of volume and it contains information needed to boot an operating
system. In UNIX it is called boot block and in NTFS it is called as partition boot sector.

2. Volume Control Block – It has information about a particular partition ex:- free block count, block size and block
pointers etc. In UNIX it is called super block and in NTFS it is stored in master file table.

3. Directory Structure – They store file names and associated inode numbers. In UNIX, includes file names and associated
file names and in NTFS, it is stored in master file table.

4. Per-File FCB – It contains details about files and it has a unique identifier number to allow association with directory
entry. In NTFS it is stored in master file table.

IN MEMORY STRUCTURE

They are maintained in main-memory and these are helpful for file system management for caching. Several in-memory
structures given below:

1. Mount Table – It contains information about each mounted volume.

2. Directory-Structure cache – This cache holds the directory information of recently accessed directories.

3. System wide open-file table – It contains the copy of FCB of each open file.

4. Per-process open-file table – It contains information opened by that particular process and it maps with appropriate
system wide open-file.
DIRECTORY IMPLEMENTATION

 There is the number of algorithms by using which, the directories can be implemented

 However, the selection of an appropriate directory implementation algorithm may significantly affect the performance of
the system

 The directory implementation algorithms are classified according to the data structure they are using

 There are mainly two algorithms which are used in these days

1. LINEAR LIST

 In this algorithm, all the files in a directory are maintained as singly linked list

 Each file contains the pointers to the data blocks which are assigned to it and the next file in the directory

Characteristics

1. When a new file is created, then the entire list is checked whether the new file name is matching to an existing file name
or not In case, it doesn't exist, the file can be created at the beginning or at the end Therefore, searching for a unique
name is a big concern because traversing the whole list takes time

2. The list needs to be traversed in case of every operation (creation, deletion, updating, etc.) on the files therefore the
systems become inefficient

2. HASH TABLE

 To overcome the drawbacks of singly linked list implementation of directories, there is an alternative approach that is
hash table

 This approach suggests to use hash table along with the linked lists

 A key value pair for each file in the directory gets generated and stored in the hash table

 The key can be determined by applying the hash function on the file name while the key points to the corresponding file
stored in the directory

 Now, searching becomes efficient due to the fact that now, entire list will not be searched on every operating

 Only hash table entries are checked using the key and if an entry found then the corresponding file will be fetched using
the value
FILE ALLOCATION METHODS

 Files are allocated disk spaces by operating system.


 Operating systems deploy following three main ways to allocate disk space to files.
 The allocation methods define how the files are stored in the disk blocks.

The main idea behind these methods is to provide:

 Efficient disk space utilization.


 Fast access to the file blocks.
 All the three methods have their own advantages and disadvantages as discussed below:

There are three main disk space or file allocation methods.

1. CONTIGUOUS ALLOCATION

 In this scheme, each file occupies a contiguous set of blocks on the disk (i.e. assigned disk address is in linear order).
 This method is best from the point of view of the individual sequential file.
 Multiple blocks can be read in at a time to improve I/O performance for sequential processing.
 For example, if a file requires n blocks and is given a block b as the starting location, then the blocks assigned to the file
will be: b, b+1, b+2,……b+n-1.
 This means that given the starting block address and the length of the file (in terms of blocks required), we can
determine the blocks occupied by the file.

 The directory entry for a file with contiguous allocation contains

o Address of starting block


o Length of the allocated portion.

 The file C in the following figure starts from the block 18 with
length = 8 blocks. Therefore, it occupies18, 19, 20, 21, 22, 23, 24,
25 blocks.
Advantages

1. It is simple to implement
2. We will get Excellent read performance
3. Supports Random Access into files

Disadvantages

1. The disk will become fragmented (both internal and external fragmentation) This makes it inefficient in terms of memory
utilization
2. It may be difficult to have a file grow (i e difficult to increase the size of the file due to the availability of the contiguous
memory block)

2. LINKED LIST ALLOCATION

 In this scheme, each file is a linked list of disk blocks which need not be contiguous.
 The disk blocks can be scattered anywhere on the disk.
 The directory entry contains a pointer to the starting and the ending file block.
 Each block contains a pointer to the next block occupied by the file.
 Increase in file size is always possible if free disk block is available.

 There is no external fragmentation because only one block at a time is needed but there can be internal fragmentation
but it exists only in the last disk block of file.

 The file ‘jeep’ in following image shows how the blocks are randomly distributed. The last block (25) contains -1 indicating
a null pointer and does not point to any other block.

Advantages

1. There is no external fragmentation with linked


allocation
2. Any free block can be utilized in order to satisfy
the file block requests
3. File can continue to grow as long as the free
blocks are available
4. Directory entry will only contain the starting
block address

Disadvantages

1. Random Access is not provided


2. Pointers require some space in the disk blocks
3. Any of the pointers in the linked list must not be
broken otherwise the file will get corrupted
4. Need to traverse each block

3. INDEXED ALLOCATION

 Provides solutions to problems of contiguous and linked allocation.


 A index block is created having all pointers to files.
 In this scheme, a special block known as the Index block contains the pointers to all the blocks occupied by a file.
 Directory contains the addresses of index blocks of files.
 This allocation technique supports both sequential and direct access to the file and thus is the most popular form of file
allocation.
 Allocation may be on the basis of fixed-size blocks or variable-sized blocks.
 Allocation by blocks eliminates external fragmentation, whereas allocation by variable-size blocks improves locality.
 The directory entry contains the address of the index block as shown in the image:
Advantages

1. Supports direct access


2. A bad data block causes the loss of only that
block

Disadvantages

1. A bad index block could cause the loss of


entire file
2. Size of a file depends upon the number of
pointers, an index block can hold
3. Having an index block for a small file is
totally wastage
4. More pointer overhead
5. Multilevel index

PROTECTION

 Protection refers to a mechanism which controls the access of programs, processes, or users to the resources defined by
a computer system.
 It deals with the access to certain system resources
 There are internal threats associated with protection of the system
 Simple queries are handled in protection
 It tries to determine the files that could be accessed or permeated by a special user
 It implements authorization mechanism

Need of Protection:

 To prevent the access of unauthorized users.


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

SECURITY

 Security grants access to specific users of the system only


 There are external security threats associated with the system
 Convoluted queries are handled by security systems
 Security uses mechanisms like encryption and authentication (also known as certification) are used

The Security Problem

1. Breach of Confidentiality: Unauthorized reading of data.


2. Breach of Integrity: Unauthorized modification of data.
3. Breach of Availability: Unauthorized destruction of data.
4. Theft of Service: Unauthorized use of resources.
5. Denial of Service (DOS): Prevention of legitimate use.

PROTECTION & SECURITY METHODS

 Protection and security requires that computer resources such as CPU, softwares, memory etc. are protected
 If a computer program is run by an unauthorized user, then he/she may cause severe damage to computer or data
stored in it
 This can be done by ensuring integrity, confidentiality and availability in the operating system
 So a computer system must be protected against unauthorized access, malicious access to system memory, viruses,
worms etc.
 The different methods that may provide protect and security for different computer systems are
1. AUTHENTICATION

 Authentication refers to identifying each user of the system and associating the executing programs with those users.

 It is the responsibility of the Operating System to create a protection system which ensures that a user who is running a
particular program is authentic.

 Operating Systems generally identifies/authenticates users using following three ways:

1. Username/Password: User needs to enter a registered username and password with Operating system to login into the
system.

2. User Card/Key: User need to punch card in card slot, or enter key generated by key generator in option provided by
operating system to login into the system.

3. User Attribute (Fingerprint/Eye Retina Pattern/ Signature): User need to pass his/her attribute via designated input
device used by operating system to login into the system.

2. ONE TIME PASSWORDS

 One time passwords provide additional security along with normal authentication
 In One Time Password system, a unique password is required every time user tries to login into the system
 Once a one-time password is used, then it cannot be used again
 One time password are implemented in various ways

1. Random Numbers - Users are provided cards having numbers printed along with corresponding alphabets System asks
for numbers corresponding to few alphabets randomly chosen

2. Secret Key - User are provided a hardware device which can create a secret id mapped with user id System asks for such
secret id which is to be generated every time prior to login

3. Network Password - Some commercial applications send one time passwords to user on registered mobile/ email
which is required to be entered prior to login

3. PROGRAM THREATS

 Operating system's processes and kernel do the designated task as instructed

 If a user program made these process do malicious tasks, then it is known as Program Threats

 One of the common example of program threat is a program installed in a computer which can store and send user
credentials via network to some hacker

 Following is the list of some well-known program threats

1. Trojan Horse - Such program traps user login credentials and stores them to send to malicious user who can later on
login to computer and can access system resources

2. Trap Door - If a program which is designed to work as required, have a security hole in its code and perform illegal
action without knowledge of user then it is called to have a trap door

3. Logic Bomb - Logic bomb is a situation when a program misbehaves only when certain conditions met otherwise it
works as a genuine program It is harder to detect

4. Virus - Virus as name suggest can replicate themselves on computer system. They are highly dangerous and can
modify/delete user files, crash systems. A virus is generally a small code embedded in a program. As user accesses the
program, the virus starts getting embedded in other files/ programs and can make system unusable for user

4. SYSTEM THREATS
 System threats refer to misuse of system services and network connections to put user in trouble.
 System threats can be used to launch program threats on a complete network called as program attack.
 System threats create such an environment that operating system resources/ user files are misused.
 Following is the list of some well-known system threats.

1. Worm - Worm is a process which can choke down a system performance by using system resources to extreme levels. A
Worm process generates its multiple copies where each copy uses system resources, prevents all other processes to get
required resources. Worms processes can even shut down an entire network.

2. Port Scanning - Port scanning is a mechanism or means by which a hacker can detects system vulnerabilities to make
an attack on the system.

3. Denial of Service - Denial of service attacks normally prevents user to make legitimate use of the system. For example,
a user may not be able to use internet if denial of service attacks browser's content settings.

5. COMPUTER SECURITY CLASSIFICATIONS

 As per the U S Department of Defense Trusted Computer System's Evaluation Criteria there are four security
classifications in computer systems A, B, C, and D.

 This is widely used specifications to determine and model the security of systems and of security solutions.

 Following is the brief description of each classification:

Unit 6 - INPUTS / OUTPUTS

I/O HARDWARE IN OPERATING SYSTEM

One of the important jobs of an Operating System is to manage various I/O devices including mouse, keyboards, touch pad,
disk drives, display adapters, USB devices, Bit-mapped screen, LED, Analog-to-digital converter, On/off switch, network
connections, audio I/O, printers etc.

An I/O system is required to take an application I/O request and send it to the physical device, then take whatever response
comes back from the device and send it to the application.

I/O devices can be divided into two categories:


1. Block Devices: A block device is one with which the driver communicates by sending entire blocks of data. For example,
Hard disks, USB cameras, Disk-On-Key etc.

2. Character Devices: A character device is one with which the driver communicates by sending and receiving single
characters (bytes, octets). For example, serial ports, parallel ports, sound cards etc.

Note: Doesn't Really Fit: Some devices don't fit into this division. For instance, clocks aren't block addressable, nor do they
accept character streams. All they do is cause interrupts... at timed intervals. Memory-mapped screens do not fit this division
either.

DEVICE CONTROLLERS

 Device drivers are software modules (device-specific code) that can be plugged into an OS to handle a particular device.

 Operating System takes help from device drivers to handle all I/O devices.

 The Device Controller works like an interface between a device and a device driver.

 I/O units (Keyboard, mouse, printer, etc.) typically consist of a mechanical component and an electronic component
where electronic component is called the device controller.

 There is always a device controller and a device driver for each device to communicate with the Operating Systems.

 A device controller may be able to handle multiple devices. As an interface its main task is to convert serial bit stream to
block of bytes, perform error correction as necessary.

 Any device connected to the computer is connected by a plug and socket, and the socket is connected to a device
controller.

 Following is a model for connecting the CPU, memory, controllers, and I/O devices where CPU and device controllers all
use a common bus for communication.

COMMUNICATION TO I/O DEVICES

The CPU must have a way to pass information to and from an I/O device. There are three approaches available to
communicate with the CPU and Device:

1. Special Instruction I/O: This uses CPU instructions that are specifically made for controlling I/O devices. These
instructions typically allow data to be sent to an I/O device or read from an I/O device

2. Memory mapped I/O - When using memory mapped I/O, the same address space is shared by memory and I/O devices.
The device is connected directly to certain main memory locations so that I/O device can transfer block of data to/from
memory without going through CPU. While using memory mapped I/O, OS allocates buffer in memory and informs I/O
device to use that buffer to send data to the CPU.
I/O device operates asynchronously with CPU, interrupts CPU when finished. The advantage to this method is that every
instruction which can access memory can be used to manipulate an I/O device

Memory mapped I/O is used for most high speed I/O devices like disks, communication interfaces.

3. Direct Memory Access (DMA) - The term DMA


stands for direct memory access. The hardware device
used for direct memory access is called the DMA
controller. DMA controller is a control unit, part of I/O
device‟s interface circuit, which can transfer blocks of
data between I/O devices and main memory with
minimal intervention from the processor.

Direct Memory Access (DMA) means CPU grants I/O


module authority to read from or write to memory
without involvement. DMA module itself controls
exchange of data between main memory and the I/O
device. CPU is only involved at the beginning and end
of the transfer and interrupted only after entire block
has been transferred.

Direct Memory Access needs a special hardware


called DMA controller (DMAC) that manages the data
transfers and arbitrates access to the system bus. The
controllers are programmed with source and
destination pointers (where to read/write the data),
counters to track the number of transferred bytes, and
settings, which includes I/O and memory types,
interrupts and states for the CPU cycles.

Working of DMA:

 Initially, when any device has to send data between the device and the memory, the device has to send DMA request
(DRQ) to DMA controller.

 The DMA controller sends Hold request (HRQ) to the CPU and waits for the CPU to assert the HLDA.

 Then the microprocessor tri-states all the data bus, address bus, and control bus. The CPU leaves the control over bus
and acknowledges the HOLD request through HLDA signal.

 Now the CPU is in HOLD state and the DMA controller has to manage the operations over buses between the CPU,
memory, and I/O devices.
I/O SOFTWARES IN OPERATING S YSTEM

I/O software is often organized in the following layers

1. User Level Libraries - This provides simple interface to the user program to perform input and output. For example,
stdio is a library provided by C and C++ programming languages

2. Kernel Level Modules - This provides device driver to interact with the device controller and device independent I/O
modules used by the device drivers

3. Hardware - This layer includes actual hardware and hardware controller which interact with the device drivers and
makes hardware alive

A key concept in the design of I/O software is that it should be device independent where it should be possible to write
programs that can access any I/O device without having to specify the device in advance.

For example, a program that reads a file as input should be able to read a file on a floppy disk, on a hard disk, or on a CD
ROM, without having to modify the program for each different device.

SECONDARY STORAGE

Secondary storage devices are those devices whose memory


is non-volatile, meaning, the stored data will be intact even if
the system is turned off. A few examples are magnetic disks,
magnetic tapes, removable thumb drives etc.

DISK (MAGNETIC DISK) STRUCTURE

In modern computers, most of the secondary storage is in the


form of magnetic disks (hard disks).

The architecture of a hard disk consists of several physical


components that include:

1. Platters
2. Spindle
3. Read/Write Reads
4. Tracks
5. Sectors
6. Cylinder
1. PLATTERS - Hard disks are organized as a concentric stack of disks. An individual disk is referred to as a platter. Each
platter consists of two surfaces: a lower and an upper surface.

2. SPINDLE - The platters within the hard disk are connected by a spindle
that runs through the middle of the platters.

The spindle moves in a unidirectional manner along its axis (either clockwise
or counterclockwise). The movement of the spindle causes the platters to
rotate as well.

3. READ/WRITEHEAD - Each surface on a platter contains a read/write head that is used to read or write data onto the
disk.

The read/write heads can move back and forth along the surface of a platter. Read/write heads are in turn connected to a
single actuator arm.
4. TRACKS - Each surface of a platter consists of a fixed number of
tracks.

These are circular areas on the surface of a platter that decrease in


circumference as we move towards the center of the platter. Data is
first written to the outermost track.

5. SECTORS - Each track is divided into a fixed number of sectors. Sectors divide track sections and store data.

6. CYLINDER - Logical grouping of the track on each disk surface in a disk unit.
In other words, all the tracks with the same radius are known as a cylinder.

DISK SCHEDULING

Disk scheduling is also known as I/O scheduling. As we know, a process needs two types of time, CPU time and I/O time. For
I/O, it requests the OS to access the disk.

On a typical multiprogramming system, there will usually be multiple disk access requests at any point of time. So those
requests must be scheduled to achieve good efficiency. Disk scheduling is similar to process scheduling.
However, the operating system must be fair enough to satisfy each request and at the same time, operating system must
maintain the efficiency and speed of process execution.

The technique that operating system uses to determine the request which is to be satisfied next is called disk scheduling.
Generally, the scheduling refers to a time table for completing any task or a job

Purpose of Disk Scheduling - The main purpose of disk scheduling algorithm is to select a disk request from the queue of
I/O requests and decide the schedule when this request will be processed

GOALS/OBJECTIVES OF DISK SCHEDULING

Disk Scheduling is important because of the following reasons:

1. Less traveling head time: Hard drives are one of the slowest parts of the computer system. So, to access the hard drive in
conveniently or effectively manner, we need disk scheduling.

2. Throughput must be high: Multiple I/O requests may arrive by different processes and only one I/O request can be
served at a time by the disk controller. Thus other I/O requests need to wait in the waiting queue and need to be scheduled.

3. Fairness: There may be chances that two or more requests can be distant from each other. Therefore, more disk arm
movement can happen. Thus, we need disk scheduling for such handling case.

There are many Disk Scheduling Algorithms but before discussing them let‟s have a quick look at some of the important
terms:

 Seek Time: Seek time is the time taken to locate the disk arm to a specified track where the data is to be read or write.
So the disk scheduling algorithm that gives minimum average seek time is better.

 Rotational Latency: Rotational Latency is the time taken by the desired sector of disk to rotate into a position so that it
can access the read/write heads. So the disk scheduling algorithm that gives minimum rotational latency is better.

 Transfer Time: Transfer time is the time to transfer the data. It depends on the rotating speed of the disk and number
of bytes to be transferred.

 Disk Access Time: Disk Access Time is:

Disk Access Time = Seek Time + Rotational Latency + Transfer Time

 Disk Response Time: Response Time is the average of time spent by a request waiting to perform its I/O
operation. Average Response time is the response time of the all requests. Variance Response Time is measure of how
individual request are serviced with respect to average response time. So the disk scheduling algorithm that gives
minimum variance response time is better.

EXAMPLE - Consider a hard disk with:4 surfaces, 64 tracks/surface, 128 sectors/track, and 256 bytes/sector

1. What is the capacity of the hard disk?

Disk capacity = surfaces * tracks/surface * sectors/track * bytes/sector


= 4 * 64 * 128 * 256 = 8 MB

2. The disk is rotating at 3600 RPM, what is the data transfer rate?
60 sec -> 3600 rotations
1 sec -> 60 rotations

Data transfer rate = number of rotations per second * track capacity * number of surfaces (since 1 R-W head is used for each
surface)

Data transfer rate = 60 * 128 * 256 * 4= 7.5 MB/sec

3. The disk is rotating at 3600 RPM, what is the average access time?

Since, seek time, controller time and the amount of data to be transferred is not given, we consider all the three terms as 0.

Therefore, Average Access time = Average rotational delay


Rotational latency => 60 sec -> 3600 rotations1 sec -> 60 rotations
Rotational latency = (1/60) sec = 16.67 msec.
Average Rotational latency = (16.67)/2= 8.33 msec.
Average Access time = 8.33 msec.

TYPES OF DISK SCHEDULING ALGORITHMS

1. FCFS SCHEDULING ALGORITHM

 FCFS (First Come First Serve) is the easiest or simplest of all the Disk Scheduling Algorithms.

 In FCFS, the I/O requests are addressed in the order they arrive in the disk queue.

 There is no starvation in this algorithm because every request is serviced in FCFS.

Advantages

1. In FCFS disk scheduling, there is no indefinite delay.


2. There is no starvation in FCFS disk scheduling because each request gets a fair chance.

Disadvantages

1. FCFS scheduling may not offered the best possible service.


2. In FCFS, scheduling disk time (seek time) is not optimized.
3. The request may come from different processes therefore there is the possibility of inappropriate movement of the head.

Question - Suppose a disk contains 200 tracks 0 - 199 and the request queue contains track number 43, 176, 42, 148, 27, 14,
180. The current position of the Read/Write head is 55. Now we have to calculate the total number of track movements of
Read/Write head using FCFS scheduling.

Solution - As mentioned in the question, the disk contains 200 tracks, so we take a track line between 0 to 199.

The current position of the read/write head is 55. So, we start from 55 then move read/write head in the FCFS order
Total Number of cylinders moved by the head = (176 - 55) + (176 - 42) + (148 - 42) + (148 - 14) + (180 - 14)
= 121+134+106+134+166 = 661

2. SSTF SCHEDULING ALGORITHM

 In SSTF (Shortest Seek Time First), requests having shortest seek time are executed first.

 So, the seek time of every request is calculated in advance in the queue and then they are scheduled according to their
calculated seek time.

 As a result, the request near the disk arm will get executed first.

 SSTF is certainly an improvement over FCFS as it decreases the average response time (total seek time) and increases the
throughput of system.

Advantages

1. In SSTF disk scheduling, the average response time is decreased.


2. Throughput increases.

Disadvantages

1. In SSTF, there may be a chance of starvation for some requests if it has higher seek time as compared to incoming
requests.
2. SSTF is not an optimal algorithm.
3. There are chances of overhead in SSTF disk scheduling because we have to calculate the seek time in advanced.
4. The speed of this algorithm can be decreased because direction could be switched frequently.

Question - Consider a disk that contains 200 tracks (0 199). The request queue includes track number: 82, 170, 43, 140, 24,
16, 190 respectively. The current position of the Read/Write head is 50. Find the number of head movements in cylinders
using SSTF scheduling.

Solution - As mentioned in the question, the disk contains 200 tracks, so we take a track line between 0 to 199. The current
position of the read/write head is 50. So, we start from 50 then move read/write head in the SSTF order.

Now for further movement of read/write head, we calculate the seek time.

Seek time = Destination Source = Source Destination

Total number of cylinders moved by the head = (50-16) + (190-16) = 34 + 174 = 208

3. SCAN ALGORITHM

 In SCAN algorithm the disk arm moves into a particular direction (direction can be moved towards large value or the
smallest value) and services the requests coming in its path and after reaching the end of disk, it reverses its direction
and again services the request arriving in its path.

 It works in the way an elevator works, elevator moves in a direction completely till the last floor of that direction and
then turns back.
 So SCAN disk scheduling algorithm is also called an elevator algorithm.

 As a result, the requests at the midrange are serviced more and those arriving behind the disk arm will have to wait.

Advantages

1. In SCAN disk scheduling, there is a low variance of response time.


2. In this algorithm, throughput is high.
3. Response time is average.
4. In SCAN disk scheduling, there is no starvation.

Disadvantages

1. SCAN disk scheduling algorithm takes long waiting time for requests for the cylinders (locations), just visited by the head.
2. In SCAN disk scheduling, we have to move the disk head to the end of the disk even when we don‟t have any request to
service.

Question - Consider a disk containing 200 tracks (0 199) and the request queue includes the track number: 93, 176, 42, 148,
27, 14, 180 respectively. The current position of read//write head is 55, and direction is towards the larger value. Calculate the
total number of cylinders moved by the head using SCAN disk scheduling.

Solution - As mentioned in the question, the disk contains 200 tracks, so we take a track line between 0 to 199.

The current position of the read/write head is 55 So, we start from 55 then we move towards the larger value as given in the
question

Total Number of cylinders moved by head = (199-55) + (199-14) = 329

4. C-SCAN ALGORITHM

 C-SCAN is an enhanced version of SCAN disk scheduling.

 In SCAN algorithm, the disk arm again scans the path that has been scanned, after reversing its direction. So, it may be
possible that too many requests are waiting at the other end or there may be zero or few requests pending at the
scanned area.

 These situations are avoided in C-SCAN algorithm in which the disk arm instead of reversing its direction goes to the
other end of the disk and starts servicing the requests from there.

 So, the disk arm moves in a circular fashion and this algorithm is also similar to SCAN algorithm and hence it is known as
C-SCAN (Circular-SCAN).

Advantages

1. C-SCAN offers better uniform waiting time.


2. It offers a better response time.
Disadvantages

1. In C-SCAN disk scheduling, there are more seek movements as compared to SCAN disk scheduling.
2. In C-SCAN disk scheduling, we have to move the disk head to the end of the disk even when we don‟t have any request to
service.

Question - Consider, a disk contains 200 tracks (0 199) and the request queue contains track number: 82, 170, 43, 140, 24,
16,190 respectively. The current position of R/W head is 50, and the direction is towards the larger value. Calculate the total
number of cylinders moved by head using C SCAN disk scheduling.

Solution - As mentioned in the question, the disk contains 200 tracks, so we take a track line between 0 to 199.

The current position of the read/write head is 50 So, we start from 50 then we move towards the larger value as given in the
question.

Total Number of cylinders moved by head = (199-50)+(199-0)+(43-0) = 149+199+43 = 391

5. LOOK SCHEDULING

 Look scheduling is an enhanced version of SCAN disk scheduling.

 Look disk scheduling is similar to the SCAN disk scheduling, but in this scheduling, instead of going till the last track
(stops moving inwards or outwards) , we go till the last request and then change the direction.

 Thus it prevents the extra delay which occurred due to unnecessary traversal to the end of the disk.

Advantages

1. In Look disk scheduling, there is no starvation.


2. Look disk scheduling offers low variance in waiting time and response time.
3. Look disk scheduling offers better performance as compared to the SCAN disk scheduling.
4. In look disk scheduling, there is no requirement of disk head to move till the end to the disk when we do not have any
request to be serviced.

Disadvantages

1. In look disk scheduling, there is more overhead to find the end request.
2. Look disk scheduling is not used in case of more load.

Question - Consider a disk contains 200 tracks (0 199). The request queue includes track number: 82, 170, 43, 140, 24, 16,
190 respectively. The current position of the read/write head is 50. The direction is towards the larger value. Calculate the
total number of cylinders moved by head using look disk scheduling.

Solution - As mentioned in the question, the disk contains 200 tracks, so we take a track line between 0 to 199.

The current position of the read/write head is 50 So, we start from 50 then we move towards the larger value as given in the
question.
Total Number of cylinders moved by head = (190-50) + (190-16) = 314

6. C-LOOK SCHEDULING

 As LOOK is similar to SCAN algorithm, in similar way, C-LOOK is similar to C-SCAN disk scheduling algorithm.

 It is different from C-SCAN algorithm in the sense that, C-SCAN force the disk arm to move till the last cylinder
regardless of knowing whether any request is to be serviced on that cylinder or not.

 In C-LOOK (Circular-LOOK) scheduling, the disk arm moves and service each request till the head reaches its highest
request, and after that, the disk arm jumps to the lowest cylinder without servicing any request, and the disk arm moves
further and service those requests which are remaining.

 Thus, it also prevents the extra delay which occurred due to unnecessary traversal to the end of the disk.

Advantages

1. There is no starvation in C-look disk scheduling.


2. The performance of the C-Look scheduling is better than Look disk scheduling.
3. C-look disk scheduling offers low variance in waiting time and response time.

Disadvantages

1. In C-Look disk scheduling there may be more overhead to determine the end request.

2. There is more overhead in calculations.

Question - Consider a disk containing 200 tracks (0 199). The request queue contains the track number: 93, 176, 42, 148, 27,
14,180 respectively. The current position of the R/W head is 55. The direction is towards the larger value. Calculate the total
number of cylinders moved by head using C LOOK disk scheduling.

Solution - As mentioned in the question, the disk contains 200 tracks, so we take a track line between 0 to 199. The current
position of the read/write head is 55. So, we start from 55 then we move towards the larger value as given in the question

Total Number of cylinders moved by head = (180-55)+(180-14)+(42-14)=125+166+28 = 319


7. N-STEP SCAN (N-SCAN) SCHEDULING

 It is also known as N-STEP LOOK algorithm.

 It divides the request queue into sub-queues of length N. In this, a buffer is created for N requests.

 By doing this it ensures that all requests belonging to a buffer will be serviced in one go.

 Also once the buffer is full no new requests are kept in this buffer and are sent to another one.

 Now, when these N requests are serviced, the time comes for another top N requests and this way all get requests get a
guaranteed service.

 Therefore, starvation is completely eliminated and by doing this N-Step-SCAN allows better throughput.

OS TERMINALS

The devices such as keyboard, monitors etc. are called as terminals. Every computer system has at least one keyboard and
one display that are used to communicate with it.

Basically there are many forms of terminals or you can also say that terminals come in many forms. Here are the list of three
most important form of terminals:

1. RS-232 TERMINAL HARDWARE

 Basically, RS-232 terminals are the hardware devices that contain a keyboard and a display, communicate using a serial
interface, one bit at a time.

 These terminals uses a 9 and 25-pin connector where one pin is for transmitting the data one is for receiving the data,
and one is ground.

 The other pins are used for various control functions, but most of which are not used.

2. GRAPHICAL USER INTERFACE (GUI)

 In today's computer world, almost all the personal computers uses graphical user interface (GUI).

 Some years ago, users were using their personal computers with MS-DOS (Microsoft Disk Operating System) which is a
character-based interface.

 A graphical user interface has the following four essential elements: Windows, Icons, Menus & Pointing device.

 In short, these four essential elements of GUI also called as WIMP which is the short form of windows, icons, menus,
pointing device.

3. NETWORK TERMINALS

 Network terminals are used to connect a remote user to computer over a network, either a local area network (LAN) or a
wide area network (WAN).

 There are following two concept that shows how the network terminal should work:

1. The network terminal should have a large amount of computing power and memory just in order to run the complex
protocols to compress the amount of data that are sent over the network.

2. The network terminal should be extremely simple, basically displaying pixels and not doing much thinking in order to
make it very cheap.
A protocol is a set of requests and responses that a sender and receiver agree upon in order to communicate over a network
or other interface.

OS CLOCKS

 Clocks are also called timers.

 System Clock is maintained by the kernel of an operating system and is used to set the tasks and processes –their
synchronization and scheduling, settings and managing interrupts, setting timer etc.

 The system clock reports seconds and microseconds since a start point from the system boot up procedure.

 There are two main reasons that clocks are very essential to the operation of any multi-programmed system.

 Clock maintains the time of day and prevents one process from monopolizing the CPU among other things.

 The clock software (clock driver) can take the form of a device driver, even though a clock is neither a block device, nor a
character device like disk and mouse.

 The exact function of the clock driver may vary depending on operating system.

The exact duties of the clock driver vary among operating systems, but usually include most of the following:

1. Maintaining the time of the day: The clock driver implements the time of day or the real time clock function. It requires
incrementing a counter at each clock tick.

2. Accounting for CPU usage: Another function performed by clock driver is doing CPU accounting. CPU accounting implies
telling how long the process has run.

3. Preventing processes from running too long: As a process is started, the scheduler initializes the quantum counter in
clock ticks for the process. The clock driver decrements the quantum counter by 1, at every clock interrupts. When the
counter gets to zero, clock driver calls the scheduler to set up another process. Thus clock driver helps in preventing
processes from running longer than time slice allowed.

4. Providing watchdog timers for parts of the system itself: Watchdog timers are the timers set by certain parts of the
system. For example, to use a floppy disk, the system must turn on the motor and then wait about 500msec for it to come up
to speed.

5. Handling the alarm system call made by user processes: In many systems, a process can request that the OS give it a
warning after a certain interval. The warning is usually a signal, interrupt, message, or something similar. One application
requiring such warnings is networking, in which a packet not acknowledged within a certain time interval must be
retransmitted. If the clock driver had enough clocks, it could set a separate clock for each request.
Unit 6 - Deadlock in Operating System

 Deadlock refers to the condition when two or more processes are waiting
for each other to release a resource indefinitely. A process in nature
requests a resource first and uses it and finally releases it.

 Deadlock is a situation that occurs in OS when any process enters a waiting


state because another waiting process is holding the demanded resource.

 Deadlock is a common problem in multi-processing where several processes


share a specific type of mutually exclusive resource known as a soft lock or
software.

System Model

 A system may consist of finite number of resources and is distributed among number of processes.
 There resources are partitioned into several instances each with identical instances.
 In general, a process must request a resource before using it and it must release the resource after using it.
 And any process can request as many resources as it requires in order to complete its designated task.
 And there is a condition that the number of resources requested may not exceed the total number of resources available
in the system.

Utilization of Resources:

Basically in the normal mode of operation, utilization of resources by a process is in the following sequence:

1. Request: Firstly, the process requests the resource. In a case, if the request cannot be granted immediately (e.g. resource
is being used by any other process), then the requesting process must wait until it can acquire the resource.

2. Use: The process can operate on the resource (e.g. if the resource is a printer then in that case process can print on the
printer).

3. Release: The process releases the resource after using it.

Example of Deadlock: Consider a system with one printer and one tape drive. If a process Pi currently holds a printer and a
process Pj holds the tape drive. If process Pi request a tape drive and process Pj request a printer then a deadlock occurs.

Multithread programs are good candidates for deadlock because they compete for shared resources.

Conditions for Deadlock

A deadlock situation can occur if the following 4 conditions occur simultaneously in a system:

1. Mutual Exclusion: Only one process must hold the resource at a time. If any other process requests for the resource,
the requesting process must be delayed until the resource has been released.
2. Hold and Wait: A process must be holding at least one resource and waiting to acquire additional resources that are
currently being hold by the other process.

3. No preemption: Resources can‟t be preempted i.e., only the process holding the resources must release it after the
process has completed its task.

4. Circular Wait: A set {P0, P1 … Pn} of waiting process must exist such that P0 is waiting for a resource i.e. held by P1, P1
is waiting for a resource i.e., held by P2. Pn-1 is waiting for resource held by process Pn and Pn is waiting for the
resource i.e., held by P1.

Example of Deadlock - A real-world example would be traffic, which is going only in one direction. Here, a bridge is
considered a resource. So, when deadlock happens, it can be easily resolved if one car backs up (Preempt resources and
rollback). Several cars may have to be backed up if a deadlock situation occurs. So starvation is possible.

S.N. Deadlock Starvation


1 Deadlock is situation that occurs when one of the When all the low priority processes got blocked, while
processes got blocked. the high priority processes execute then this situation is
termed as starvation.
2 Deadlock is an infinite process. Starvation is a long waiting but it is not an infinite
process.
3 There is starvation in every deadlock. It is not necessary that every starvation is a deadlock.
4 Deadlock happens when mutual exclusion, hold and wait, Starvation occurs due to the uncontrolled priority and
no preemption and circular wait occurs simultaneously. resource management.
5 The requested resource is continuously be used by the The requested resource is continuously be used by the
higher priority processes. higher priority processes.

ADVANTAGES OF DEADLOCK

 This situation works well for processes which perform a single burst of activity
 No preemption needed for Deadlock.
 Convenient method when applied to resources whose state can be saved and restored easily
 Feasible to enforce via compile-time checks
 Needs no run-time computation since the problem is solved in system design

DISADVANTAGES OF DEADLOCK

 Delays process initiation


 Processes must know future resource need
 Pre-empts more often than necessary
 Dis-allows incremental resource requests
 Inherent preemption losses.

Methods for Handing Deadlocks

There are three ways to deal with deadlock problem:

 We can use a protocol to prevent deadlock ensuing that the system will never enter into the deadlock state.
 We allow a system to enter into deadlock state, detect it and recover from it.
 We ignore the problem and pretend that the deadlock never occur in the system. This is used by OS including UNIX.
 To ensure that the deadlock never occur the system can use either deadlock avoidance or a deadlock prevention.

 Deadlock prevention is a set of method for ensuing that at least one of the necessary conditions does not occur.

 Deadlock avoidance requires the OS is given advance information about which resource a process will request and use
during its lifetime.

 If a system does not use either deadlock avoidance or deadlock prevention then a deadlock situation may occur. During
this it can provide an algorithm that examines the state of the system to determine whether a deadlock has occurred
and algorithm to recover from deadlock.

 Undetected deadlock will result in deterioration of the system performance.

Deadlock Ignorance

Also known as Ostrich (name of bird) method.

Deadlock ignorance is the most widely used approach among all the mechanism. This is being used by many operating
systems mainly for end user uses. In this approach, the operating system assumes that deadlock never occurs. It simply
ignores deadlock. This approach is best suitable for a single end user system where user uses the system only for browsing
and all other normal stuff (or tasks).

Thus ignoring the deadlock method can be useful in many cases but it is not perfect in order to remove the deadlock from
operating system. There is always a tradeoff between correctness and performances.

The operating systems like Windows and Linux mainly focus upon performance.

However, the performance of the system decreases of it uses deadlock handling mechanism all the time of deadlock happens
1 out of 100 times then it is completely unnecessary to use the deadlock handling mechanism all the time.

In these types of systems, the user has to simply restart the computer the case of deadlock. Windows and Linux are mainly
using this approach.

Deadlock prevention

 Deadlock happens only when mutual exclusion, hold and wait, no preemption and circular wait occurs simultaneously

 The main aim of the deadlock prevention method is to violate any one condition among the four because if any of one
condition is violated then the problem of deadlock will never occur.

 The idea behind the approach is very simple that we have to fail one of the four conditions but the difficulty can occur
during the physical implementation of this method in the system.

 Let us take an example of a chair, as we know that chair always stands on its four legs. If anyone leg of the chair gets
broken, then definitely it will fall.
 The same is the situation with the deadlock if we become able to violate any condition among the four and do not let
them occur together then there can be prevented from the deadlock problem.

 We will elaborate deadlock prevention approach by examining each of the four necessary conditions separately. Let's
see how we can prevent each of the conditions.

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.

However, if we can be able to violate resources behaving in the mutually exclusive manner then the deadlock can be
prevented.

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

Although, Spooling can be an effective approach to violate mutual exclusion but it suffers from two kinds of problems.

1. This cannot be applied to every resource.


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

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

2. Hold and Wait – Hold and wait condition occurs when a process holds a resource and is also waiting for some other
resource in order to complete its execution. Thus if we did not want the occurrence of this condition then we must guarantee
that when a process requests a resource, it does not hold any other resource.
There are some protocols that can be used in order to ensure that the Hold and Wait condition never occurs:

 According to the first protocol each process must request and gets all its resources before the beginning of its
execution.
 The second protocol allows a process to request resources only when it does not occupy any resource.

Let us illustrate the difference between these two protocols:

 We will consider a process that mainly copies data from a DVD drive to a file on disk, sorts the file and then prints the
results to a printer. If all the resources must be requested at the beginning of the process according to the first protocol,
then the process requests the DVD drive, disk file, and printer initially. It will hold the printer during its entire execution,
even though the printer is needed only at the end.

 While the second method allows the process to request initially only the DVD drive and disk file. It copies the data from
the DVD drive to the disk and then releases both the DVD drive and the disk file. The process must then again request
the disk file and printer. After copying the disk file to the printer, the process releases these two resources as well and
then terminates.

Disadvantages of both protocols

 Utilization of resources may be low, since resources may be allocated but unused for a long period. In the above-
given example, for instance, we can release the DVD drive and disk file and again request the disk file and printer only if
we can be sure that our data will remain on the disk file. Otherwise, we must request all the resources at the beginning
of both protocols.

 There is a possibility of starvation. A process that needs several popular resources may have to wait indefinitely
because at least one of the resources that it needs is always allocated to some other process.

3. No Preemption – The third necessary condition for deadlocks is that there should be no preemption of resources that
have already been allocated.

In order to ensure that this condition does not hold the following protocols can be used:

 According to the first protocol: “If a process that is already holding some resources requests resource and if the
requested resources cannot be allocated to it, then it must release all the resources currently allocated to it.”

 According to the second protocol: “When a process requests some resources, if they are available, then allocate them. If
in case, the requested resource is not available then we will check whether it is being used or is allocated to some other
process waiting for other resources. If that resource is not being used, then the operating system preempts it from the
waiting process and allocates it to the requesting process. And if that resource is being used, then the requesting
process must wait”.

The second protocol can be applied to those resources whose state can be easily saved and restored later for example CPU
registers and memory space, and cannot be applied to resource like printers and tape drivers.

4. Circular Wait

The fourth necessary condition to cause deadlock is circular wait, in order to ensure violate this condition we can do the
following:

 Assign a priority number to each resource.


 There will be a condition that any process cannot request for a lesser priority resource.
 This method ensures that not a single process can request a resource that is being utilized by any other process and due
to which no cycle will be formed.
 Among all the methods, violating circular wait is the only approach that can be implemented practically.
 For example, if P1 process is allocated R5 resources, now next time if P1 ask for R4, R3 lesser than R5 such request will
not be granted, only request for resources more than R5 will be granted.
 This will allows us to compare two resources and determine whether one precedes the other in ordering.
Example: Assume that R5 resource is allocated to P1, if next time P1 asks for R3, R3 that are lesser than R5; then such
request will not be granted. Only the request for resources that are more than R5 will be granted.

S.N. Necessary Conditions Approach Practical Implementation


1 Mutual Exclusion The approach used to violate this condition is Not possible
spooling.
2 Hold and wait In order to violate this condition, the approach is Not possible
to request all the resources for a process initially
3 No Preemption In order to violate this condition, the approach Not possible
is: snatch all the resources from the process.
4 Circular Wait In this approach is to assign priority to each Possible
resources and order them numerically

Deadlock Avoidance

 In this method, the request for any resource will be granted only if the resulting state of the system doesn‟t cause any
deadlock in the system.

 In deadlock avoidance, the operating system checks whether the system is in safe state or in unsafe state at every step
which the operating system performs.

 Any process continues its execution until the system is in a safe state.

 Once the system enters into an unsafe state, the operating system has to take a step back.

 With the help of a deadlock-avoidance algorithm, the operating system keeps an eye on each allocation, and make sure
that allocation does not cause any deadlock in the system.

 In order to avoid deadlocks, the process must tell OS, the maximum number of resources a process can request to
complete its execution.

 The deadlock avoidance algorithm examines the resource allocations so that there can never be a circular wait condition.

 Deadlock avoidance can mainly be done with the help of Banker‟s Algorithm.

Safe state and unsafe state

 A state is safe if the system can allocate resources to each process (up to its maximum requirement) in some order and
still avoid a deadlock. Formally, a system is in a safe state only, if there exists a safe sequence.
 So a safe state is not a deadlocked state and conversely a deadlocked state is an unsafe state.
 In an unsafe state, the operating system cannot prevent processes from requesting resources in such a way that any
deadlock occurs.
 It is not necessary that all unsafe states are deadlocks; an unsafe state may lead to a deadlock.

 The above figure shows the safe, unsafe and deadlocked state spaces.

Deadlock Avoidance Example: Let us consider a system having 12 magnetic tapes and three processes P1, P2, P3. Process
P1 requires 10 magnetic tapes, process P2 may need as many as 4 tapes, and process P3 may need up to 9 tapes. Suppose at
a time t0, process P1 is holding 5 tapes, process P2 is holding 2 tapes and process P3 is holding 2 tapes. (There are 3 free
magnetic tapes)

 So a time t0, the system is in a safe state.


 The sequence is <P2, P1, P3> satisfies the safety condition.
 Process P2 can immediately be allocated all its tape drives and then return them.
 After the return the system will have 5 available tapes, then process P1 can get all its tapes and return them (the system
will then have 10 tapes).
 Finally, process P3 can get all its tapes and return them (the system will then have 12 available tapes).
 A system can go from a safe state to an unsafe state.
 Suppose at time t1, process P3 requests and is allocated one more tape. The system is no longer in a safe state.
 At this point, only process P2 can be allocated all its tapes.
 When it returns them the system will then have only 4 available tapes.
 Since P1 is allocated five tapes but has a maximum of ten so it may request 5 more tapes.
 If it does so, it will have to wait because they are unavailable.
 Similarly, process P3 may request its additional 6 tapes and have to wait which then results in a deadlock.
 The mistake was granting the request from P3 for one more tape.
 If we made P3 wait until either of the other processes had finished and released its resources, then we could have
avoided the deadlock

Note: In a case, if the system is unable to fulfill the request of all processes then the state of the system is called unsafe.

The main key of the deadlock avoidance method is whenever the request is made for resources then the request must only
be approved only in the case if the resulting state is a safe state

Deadlock Avoidance Algorithms

 It is better to avoid a deadlock instead of taking action after the deadlock has occurred.

 It needs additional information, like how resources should be used.

 Deadlock avoidance is the simplest and most useful model that each process declares the maximum number of
resources of each type that it may need.

 The deadlock avoidance algorithm helps you to dynamically assess the resource-allocation state so that there can never
be a circular-wait situation.

A single instance of a resource type

 Use a Resource Allocation Graph.


 Cycles are necessary which are sufficient for Deadlock

Multiples instances of a resource type

 Uses the Banker‟s Algorithm


 Cycles are necessary but never sufficient for Deadlock.

Resource Allocation Graph (RAG)

 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.
 The graph consists of set of vertices (v) and set of edges (e).
 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 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 nay graph contains vertices and edges. So RAG also contains vertices and edges.

RAG Vertices are two types:

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 types:

- Single instance type resource: It represents as a box, inside the box, there will be one dot. So the number of dots
indicates 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.

RAG Edges are two types:

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 want some resource to complete the execution, that is called request
edge.

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.

Single Instances RAG

a. Single Instance Resource with Deadlock:


 If there is a cycle in the RAG 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.

b. Single Instance Resource without Deadlock:

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.

Multi-instances RAG

a. Multi-instances resource without deadlock:


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

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

b. Multi-instances resource with deadlock:


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

Banker’s Algorithm

The banker‟s algorithm is a resource allocation and deadlock avoidance algorithm that tests for safety by simulating the
allocation for predetermined maximum possible amounts of all resources, then makes an “s-state” check to test for possible
activities, before deciding whether allocation should be allowed to continue.

Why Banker’s algorithm is named so? Banker‟s algorithm is named so because it is used in banking system to check
whether loan can be sanctioned to a person or not. Suppose there are n number of account holders in a bank and the total
sum of their money is S. If a person applies for a loan then the bank first subtracts the loan amount from the total money
that bank has and if the remaining amount is greater than S then only the loan is sanctioned. It is done because if all the
account holders come to withdraw their money then the bank can easily do it.

In other words, the bank would never allocate its money in such a way that it can no longer satisfy the needs of all its
customers. The bank would try to be in safe state always.
Following data structures are used to implement the Banker‟s algorithm:

Let „n‟ be the number of processes in the system and „m‟ be the number of resource types.

4. Available:

 It is a 1-d array of size „m‟ indicating the number of available resources of each type.
 Available[j] = k means there are „k‟ instances of resource type Rj

5. Max:

 It is a 2-d array of size „n*m‟ that defines the maximum demand of each process in a system.
 Max[i, j] = k means process Pi may request at most „k‟ instances of resource type R j.

6. Allocation:

 It is a 2-d array of size „n*m‟ that defines the number of resources of each type currently allocated to each process.
 Allocation [i, j] = k means process Pi is currently allocated „k‟ instances of resource type R j

7. Need:

 It is a 2-d array of size „n*m‟ that indicates the remaining resource need of each process.
 Need[i, j] = k means process Pi currently need „k‟ instances of resource type Rj for its execution.
 Need[i, j] = Max[i, j] – Allocation [i, j]

Allocation i specifies the resources currently allocated to process Pi and Needi specifies the additional resources that process
Pi may still request to complete its task.

Banker’s algorithm consists of Safety algorithm and Resource request algorithm.

 Allocationi specifies the resources currently allocated to process Pi and Needi specifies the additional resources that
process
 Pi may still request to complete its task.
 Banker‟s algorithm consists of Safety algorithm and Resource request algorithm

Safety Algorithm - The algorithm for finding out whether or not a system is in a safe state can be described as follows:

1) Let Work and Finish be vectors of length ‘m’ and ‘n’ respectively.
Initialize: Work = Available
Finish[i] = false; for i=1, 2, 3, 4….n
2) Find an i such that both
a) Finish[i] = false
b) Needi <= Work
if no such i exists goto step (4)
3) Work = Work + Allocation[i]
Finish[i] = true
goto step (2)
4) if Finish [i] = true for all i
then the system is in a safe state

Resource-Request Algorithm - Let Requesti be the request array for process Pi. Requesti [j] = k means process Pi wants k
instances of resource type Rj. When a request for resources is made by process Pi, the following actions are taken:

1) If Requesti <= Needi


Goto step (2) ; otherwise, raise an error condition, since the process has exceeded its maximum claim.
2) If Requesti <= Available
Goto step (3); otherwise, Pi must wait, since the resources are not available.
3) Have the system pretend to have allocated the requested resources to process Pi by modifying the state as
follows:
Available = Available – Requesti
Allocationi = Allocationi + Requesti
Needi = Needi– Requesti

EXAMPLE

 5 processes P0 through P4;


 3 resource types: A (10 instances), B (5 instances), and C (7 instances).
 Snapshot at time T0:

Check that Needi <= Available


To calculate first Available matrix is (10 5 7) – (7 2 5) = 3 3 2

To find safe state using Banker‟s Algorithm:

Executing safety algorithm shows that sequence satisfies safety requirement.

Output:

Following is the SAFE Sequence P1 -> P3 -> P4 -> P0 -> P2


Can request for (3, 3, 0) by P4 be granted? –NO.
Can request for (0, 2, 0) by P0 be granted? –NO (Results Unsafe).
Question: What will happen if process P1 requests one additional instance of resource type A and two instances of
resource type C?

We must determine whether this new system state is safe. To do so, we again execute Safety algorithm on the above data
structures.
Hence the new system state is safe, so we can immediately grant the request for process P1 .

Example: Let us consider the following snapshot for understanding the banker's algorithm:

1. Calculate the content of the need matrix?


2. Check if the system is in a safe state?
3. Determine the total sum of each type of resource?

Solution:

1. The Content of the need matrix can be calculated by using the formula given below:

Need = Max – Allocation


2. Let us now check for the safe state.

Safe sequence:

1. For process P0, Need = (3, 2, 1) and


Available = (2, 1, 0)
Need <=Available = False
So, the system will move to the next process.

2. For Process P1, Need = (1, 1, 0)


Available = (2, 1, 0)
Need <= Available = True
Request of P1 is granted.

Available = Available +Allocation


= (2, 1, 0) + (2, 1, 2)
= (4, 2, 2) (New Available)

3. For Process P2, Need = (5, 0, 1)


Available = (4, 2, 2)
Need <=Available = False
So, the system will move to the next process.

4. For Process P3, Need = (7, 3, 3)


Available = (4, 2, 2)
Need <=Available = False
So, the system will move to the next process.

5. For Process P4, Need = (0, 0, 0)


Available = (4, 2, 2)
Need <= Available = True
Request of P4 is granted.

Available = Available + Allocation


= (4, 2, 2) + (1, 1, 2)
= (5, 3, 4) now, (New Available)

6. Now again check for Process P2, Need = (5, 0, 1)


Available = (5, 3, 4)
Need <= Available = True
Request of P2 is granted.

Available = Available + Allocation


= (5, 3, 4) + (4, 0, 1)
= (9, 3, 5) now, (New Available)

7. Now again check for Process P3, Need = (7, 3, 3)


Available = (9, 3, 5)
Need <=Available = True
The request for P3 is granted.

Available = Available +Allocation


= (9, 3, 5) + (0, 2, 0) = (9, 5, 5)

8. Now again check for Process P0, = Need (3, 2, 1)


= Available (9, 5, 5)
Need <= Available = True
So, the request will be granted to P0.

Safe sequence: < P1, P4, P2, P3, P0>

The system allocates all the needed resources to each process. So, we can say that the system is in a safe state.

3. The total amount of resources will be calculated by the following formula:


The total amount of resources= sum of columns of allocation + Available
= [8 5 7] + [2 1 0] = [10 6 7]

Limitations of Banker Algorithm:

 As the processes enter the system, they must predict the maximum number of resources needed which is not impractical
to determine.

 In this algorithm, the number of processes remains fixed which is not possible in interactive systems.

 This algorithm requires that there should be a fixed number of resources to allocate. If a device breaks and becomes
suddenly unavailable the algorithm would not work.

 Overhead cost incurred by the algorithm can be high when there are many processes and resources because it has to be
invoked for every process.

Example of Banker Algorithm of Single Resource:

First let's consider the situation when there is one resource type, think of it as units of money (1K dollars), a banker (the OS)
who has a certain number of units in his bank and a number of customers who can loan a certain number of units from the
bank and later pay the loan back (release the resources). The customers have a credit line that cannot exceed the number of
units initially in the bank, and when they have borrowed their max number of units, they will pay their loan back.

The banker will grant a loan request only if it does not lead to an unsafe state. E.g., the banker initially has 10 K, and four
customers A, B, C and D have credit lines 6K, 5K, 4K and 7K respectively. The state when no loans have been made is then:

HAS MAX Free: 10

A06
B05
C04
D07

Say at some stage the loan situation is

HAS MAX Free: 2

A16
B15
C24
D47
Suppose B loans one more unit:

HAS MAX Free: 1


A16
B25
C24
D47

This is unsafe: if ALL CUSTOMERS ask their MAXIMUM remaining credit, NONE can be satisfied, and we have deadlock. So,
in this case the banker will not grant B the loan; i.e. back to

HAS MAX Free: 2

A16
B15
C24
D47

This state is safe because with 2K left, C can borrow her max remaining credit:

HAS MAX Free: 0

A16
B15
C44
D47

Finish and release her 4 units and finish

HAS MAX Free: 4

A16
B15
D47

The remaining state is safe. Why?

D can loan its 3 units

HAS MAX Free: 1

A16
B15
D77

and terminate

HAS MAX Free: 8

A16
B15

Now A can loan its 5 units and terminate

HAS MAX Free: 9

B15

and now B can go

HAS MAX Free:10

So a safe state is a state where a sequence of ALL processes can get their max required resources (one at the time) and finish
and release all their resources.
Deadlock Detection and Recovery

 This approach let the processes fall in deadlock and then periodically check whether deadlock occur in the system or not
by using some algorithms of the resource-allocation graph.
 This graph is mainly used to represent the allocations of various resources to different processes.
 Do not confuse the deadlock detection algorithm with banker‟s algorithm which is completely different.
 If a system does not employ either a deadlock prevention or deadlock avoidance algorithm then a deadlock situation
may occur.
 After the detection of deadlock, a number of methods can be used in order to recover from that deadlock.
 In this case:

- Apply an algorithm to examine state of system to determine whether deadlock has occurred or not.
- Apply an algorithm to recover from the deadlock.

We have already discussed about deadlock detection method using different algorithms of the resource-allocation graph.

Deadlock Recovery - In order to recover the system from deadlocks, either OS considers resources or processes. Deadlock
can be recovered by:

For Resource:
Preemption (Preempt the Resource):

 The resources that are allocated to the process involved in deadlock are taken away (preempted) and are transferred to
other processes with the expectation that it will complete the execution and will release this resource sooner.
 In this way, system may recover from deadlock as we may change system state.
 Well, choosing a resource which will be snatched is going to be a bit difficult.

Rollback (Rollback to a Safe State):

 The operating system keeps a record of the process state and it can easily make a process roll back to its previous state
due to which deadlock situation can be easily eliminate.
 System passes through various states to get into the deadlock state.
 A state when the system is not in deadlock is called safe state.
 The moment, we get into deadlock (i.e. unsafe state), we will rollback all the allocations to get into the previous safe
state.
 For this purpose, OS needs to implement check pointing at every state.

For Process:

Kill the Process: One way is to kill all the process in deadlock or the second way kill the process one by one, and check after
each if still deadlock exits and do the same till the deadlock is removed.

Kill a process - Killing a process can solve our problem but the bigger concern is to decide which process to kill. Generally,
operating system kills a process which has done least amount of work until now.

Kill all process - This is not a suggestible approach but can be implemented if the problem becomes very serious. Killing all
process will lead to inefficiency in the system because all the processes will execute again from starting.

Advantages of Deadlock Prevention

 No preemption necessary.
 Works well for process that perform a single burst of activity.
 Needs no run-time computation.
 Feasible to enforce via compile time checks.

Disadvantages of Deadlock Prevention

 Inefficient.
 Delay process initiation.
 Subject to cyclic restart.
 Disallows increment resource requests.

Advantages of Deadlock Avoidance

 No preemption necessary

Disadvantages of Deadlock Avoidance

 Future resource requirements must be known.


 Processes can be blocked for long periods.

Advantages of Deadlock Detection

 Never delays process initiation


 Facilitates online handling

Disadvantages of Deadlock Detection

 Inherent preemption losses.


Unit 7 – Real Time System

Real-time operating systems (RTOS) are used in environments where a large


number of events, mostly external to the computer system, must be accepted and
processed in a short time or within certain deadlines. For example: flight control
system, real time monitors etc.

Real time system means that the system is subjected to real time, i.e. response
should be guaranteed within a specified time constraint or system should meet
the specified deadline.

Moreover, the system should perform the given task in a fixed time otherwise; it
results in a system failure.

In a RTOS, processing time requirement are calculated in tenths of seconds increments of time.

 Response time is the time within which the system takes the input, processes the data, and gives the results.

 Real time operating system is also known as embedded operating system.

 Hence, a real-time operating system must be fast and responsive for an embedded system, weapon system, robots,
scientific research & experiments and various real-time objects.

The primary functions of the RTOS are to:

1. Manage the processor and other system resources to meet the requirements of an application.
2. Synchronize with and respond to the system events.
3. Move the data efficiently among processes and to perform coordination among these processes.

For example, real-life situations governing an automatic car, traffic signal, nuclear reactor or an aircraft require an immediate
response to complete tasks within a specified time delay.

General-Purpose Operating System (GPOS) Real-Time Operating System (RTOS)


It used for desktop PC and laptop. It is only applied to the embedded application.
Process-based Scheduling. Time-based scheduling used like round-robin scheduling.
Interrupt latency is not considered as important as in Interrupt lag is minimal, which is measured in a few
RTOS. microseconds.
No priority inversion mechanism is present in the The priority inversion mechanism is current. So it cannot modify
system. by the system.
Kernel's operation may or may not be preempted. Kernel's operation can be preempted.
Priority inversion remain unnoticed No predictability guarantees

TYPES OF REAL-TIME OS

1. HARD REAL-TIME SYSTEMS

 Hard real-time systems guarantee that critical tasks complete on time (i.e. purely deterministic).

 In this, the time constraint is very short and strict. Even seconds of delay is not acceptable. Therefore, it is compulsory to
complete the task within the given time only.

 In a hard real-time system, the secondary storage is either limited or missing, so these system stored data in the ROM.

 For example users expected the output for the given input in 10 sec then system should process the input data and give
th th th
the output exactly by 10 second. It should not give the output by 11thsecond or by 9 second, exactly by 10 second it
should give the output.

 In the hard real time system meeting the deadline is very important. If deadline is not met, it is not accepted by the
system or may face serious issues like a system failure.
Examples: Automobile control system like as Anti-Lock & Air Bag, Airline Control System, Diagnosis Control System etc.

2. SOFT REAL-TIME SYSTEMS

 This operating system provides some relaxation in the time limit.

 In this type of RTOS, there is a deadline assigned for a specific job, but a delay for a small amount of time is acceptable.

 In this, performance is degraded but not destroyed by failure to meet response-time constraints. That means soft real
time systems are less restrictive.

 The usefulness of result produced by a soft real time system decreases gradually with increase in tardiness.

 Soft real-time systems have limited utility than hard real-time systems.

 In this system, do not allow delay in large amount of time.

Examples: Multimedia transmission and reception, Telecom (cellular) networks, Web sites and services, Smart phones,
Reservation system, Online transaction systems, Livestock price quotation systems, Computer games, etc.

3. FIRM REAL-TIME SYSTEMS

 Firm real time O/S accepts the more deadline rules.

 In these systems, although the deadline is given but missing a deadline may not have big impact but could cause
undesired affects, like a huge reduction in quality of a product.

 A firm real-time system is one in which a few missed deadlines will not lead to total failure, but missing more than a few
may lead to complete and catastrophic system failure.

 The responses for the requests (e.g. replies to a query, result of a task, etc.) are worthless once the deadline for that
particular request has passed (i.e. the usefulness of a result is zero after its deadline).

 A hypothetical example can be a storm forecast system (if a storm is predicted before arrival, then the system has done
its job, prediction after the event has already happened or when it is happening is of no value).

Examples are: Automation system in the industry sector like as visual inspection, various multimedia applications etc.

HARD REAL TIME SYSTEM SOFT REAL TIME SYSTEM

In hard real time system, the size of data file is small or In soft real time system, the size of data file is large.
medium.

In this system response time is in millisecond. In this system response time are higher.

Peak load performance should be predictable. In soft real time system, peak load can be tolerated.

In this system safety is critical. In this system safety is not critical.

A hard real time system is very restrictive. A Soft real time system is less restrictive.

In case of an error in a hard real time system, the In case of an soft real time system, computation is rolled back
computation is rolled back. to previously established a checkpoint.

Satellite launch, Railway signaling system etc. DVD player, telephone switches, electronic games etc.

Firm Real-Time System Soft Real-Time System


The system should complete the task before the It also has to complete the task within the deadline but, not
deadline. strictly.
In case, it gives the results after the deadline it has zero If it gives the results after the deadline, the importance
importance. decreases and slowly goes to zero value.
If the results are given after the deadline, they are On the other hand, even if the system gives the results after
considered as incorrect. the deadline they are not considered incorrect.
Multimedia applications use such systems. Practical It is mostly used in practical applications.
systems make less use of them.
Examples are multimedia systems etc. Examples are computer games, online transaction systems,
etc.

CHARACTERISTICS/FEATURES OF RTOS

 It has better reliability. It means, RTOS can work in long time without any human interference.

 Its result is more predictable because its every action is executed into predefined time frame. Repeating an input will
result in the same output (i.e. deterministic).

 Its performance is much better because it can perform more complex tasks without taking more workload.

 All software and hardware are small size, which are used in the RTOS (Real Time Operating system). Due to this,
technician does not get more headaches for finding the errors in the RTOS.

 It has good stability. So due to this feature, we can upgrade or downgrade to RTOS.

 Occupy less resource, consumes low memory and affordable cost.

 RTOS systems are fast and responsive (i.e. high performance) often executing actions within a small fraction of the time
needed by a general OS.

 Kernel helps for storing the states of interrupted tasks for execution at appropriate time frame.

 RTOS are frequently used in critical systems when failures can have catastrophic consequences, such as robotics or flight
controllers. To protect those around them, they must have higher security standards and more reliable safety features.

 In RTOS, priority-based scheduling is done means that actions assigned a high priority are executed first, and those with
lower priority come after.

ADVANTAGES OF RTOS

1. Maximum consumption: Maximum utilization of devices and systems. Thus more output from all the resources.

2. Task Shifting: Time assigned for shifting tasks in these systems is very less. For example, in older systems, it takes about
10 microseconds. Shifting one task to another and in the latest systems, it takes 3 microseconds.

3. Focus On Application: Focus on running applications and less importance to applications that are in the queue.

4. Real-Time Operating System In Embedded System: Since the size of programs is small, RTOS can also be embedded
systems like in transport and others.

5. Error Free: These types of systems are error-free.

6. Memory Allocation: Memory allocation is best managed in these types of systems.

DISADVANTAGES OF RTOS

1. Limited Tasks: Very few tasks run simultaneously, and their concentration is very less on few applications to avoid errors.

2. Use Heavy System Resources: Plenty of resources are used by RTOS, which makes this system expensive.

3. Complex Algorithms: The algorithms are very complex which is difficult to understand.
4. Device Driver and Interrupt signals: It needs specific device drivers and interrupts signals to respond earliest to
interrupts.

5. Thread Priority: It is not good to set thread priority as these systems are very less prone to switching tasks.

6. Minimum Switching: RTOS performs minimal task switching.

APPLICATIONS OF RTOS

 Airlines reservation system.


 Air traffic control system.
 Systems that provide immediate updating such as Stock Exchange.
 For military as a Defense application systems like RADAR.
 Networked Multimedia Systems.
 Automobile Sector such as Anti-Lock Brake and Air Bag.
 Medical Sector like Heart Pacemaker System.
 Home appliance such as washing machine, microwave etc.

UNIT 8 - Distributed System

CENTRALIZED SYSTEMS: We start with centralized systems because they are the most intuitive and easy to understand and
define.

Centralized systems are systems that use client/server architecture where one or more client
nodes are directly connected to a central server. This is the most commonly used type of
system in many organizations where a client sends a request to a company server and receives
the response.

Example – - Wikipedia. Consider a massive server to which we send our requests and the
server responds with the article that we requested. Suppose we enter the search term „junk
food‟ in the Wikipedia search bar. This search term is sent as a request to the Wikipedia servers
(mostly located in Virginia, U.S.A) which then responds back with the articles based on
relevance. In this situation, we are the client node, Wikipedia servers are the central server.

Characteristics of Centralized System:

 Presence of a global clock: As the entire system consists of a central node(a server/ a master) and many client nodes(a
computer/ a slave), all client nodes sync up with the global clock(the clock of the central node).

 One single central unit: One single central unit which serves/coordinates all the other nodes in the system.

 Dependent failure of components: Central


node failure causes the entire system to fail. This
makes sense because when the server is down,
no other entity is there to send/receive
responses/requests.

Distributed Operating System is a model where


distributed applications are running on multiple
computers linked by communications. A
distributed operating system is an extension of the
network operating system that supports higher levels
of communication and integration of the machines on
the network.

Multiple central processors are used by Distributed


systems to serve multiple real-time applications and
multiple users. Accordingly, Data processing jobs are distributed among the processors.

Processors communicate with each other through various communication lines (like high-speed buses or telephone lines).
These are known as loosely coupled systems or distributed systems. Processors in this system may vary in size and function.
They are referred as sites, nodes, computers, and so on.

Distributed operating systems mainly comprise of: Workstations, Database Server Computer Node, Computer Network,
Terminal and File Server

In a DOS the following occurs:

 All software and hardware compounds are located remotely. In order for them to communicate with each other, they
pass messages.

 It is capable for sharing their computational capacity and I/O files (i.e. resource sharing). Resources are managed by
servers and clients use these resources.

TYPES OF DOS

1. Client-Server: This type of system requires the client to request a resource, and then the server provides that resource
which was requested.

When a client is in contact with one server, the server itself may serve multiple clients at the same time. Both the server and
the client will communicate over a computer network, which is part of a distributed system.

2. Three-Tier: The information about the client is stored in the middle tier, instead of storing it in the client, this is done to
simplify development. This architecture is most common in web applications.

3. N Tier: n tier systems are used when the server or application needs to forward requests to additional enterprise services
on the network.

4. Peer to Peer: This type of system contains nodes that are equal participants in data sharing; furthermore, all the tasks are
equally divided between all the nodes. These nodes will interact with each other as required as “share resources”. To
accomplish this, a network is

5. Middleware - Middleware allows the interoperability in the between of all applications, which are running on other
operating systems. With using these services those applications are capable for transferring all data each other.

CHARACTERISTICS OF A DOS

1. Heterogeneity:

In distributed systems, the components can have differences and variety in programming languages, operating systems,
computer hardware, networks, and implementations by different developers.

2. Resource Sharing:

 (Every resource has its own management policies and methods; furthermore every resource is managed by a module,
commonly known as a resource manager).

 Resource sharing is the ability to use hardware, software, or data anywhere in the system. The resource manager controls
access, moreover, it also provides naming scheme and controls concurrency in the DOS.

 The hardware resources are shared for convenience and reductions in cost, in contrast, the data is shared for consistency
and exchange of information.

3. Openness:

 With DOS, the “openness” is related to the extensions and improvements of disturbed systems.

 A well-defined and detailed interface of the components must be published.


 The new component, which is to be added, must be easily integrated with existing components.

4. Concurrency:

 This characteristic of the system enables multiple activities in the system to be executed in the same time. In the
distributed system, the simultaneous execution of tasks happens in different components running on multiple machines,
furthermore, these tasks could execute interactions between each other.

 The benefit of concurrency is increases the efficiency while reducing the latency in the system.

 In the DOS, the components access and update shared resources (device drivers, variables, databases), moreover, if
concurrent updates are not coordinated it may result in inconsistent analysis.

5. Scalability:

 In a DOS, the scalability of a system refers to how it handles the growth as number of users on the system platform
increases. Scaling best occurs by adding more systems to the network.

 These components must be designed in a way that allows them to be scalable too.

6. Fault Tolerance:

 The system is designed in a way that it is still available to the user despite a failure in the hardware, software, or the
network.

 Fault tolerance is achieved by recovery and redundancy.

7. Security:

 In a DOS, the clients send requests to access data managed by servers and resources in the networks.

 In a DOS security is acquired for the following:

 Concealing the contents of messages

 Authentication

8. No Global Clock:

 In a DOS, computers are connected through a network, and each computer has its own clock (so a system can have
many clocks).

 Nodes communicate between each other through message passing, furthermore, their coordination is dependent on
time, as every client/computer has a different time span.

 Even if the clocks were in sync, the individual clocks on each component may run at a different rate, leading them to be
out of sync after one local clock cycle.

 Though accurate time synchronization is impossible in a DOS because it doesn‟t provide a global clock.

9. Flexibility:

 Flexibility in a DOS is enhanced through modular characteristics, and by providing a more enhanced set of high level
services. The quality and completeness of the kernel/microkernel simplifies implementation of such services.

10. Synchronization:

 Systems, which are cooperating concurrent process, have an inherent need for synchronization. This ensures that
changes happen in a correct and predictable manner.

 The three basic circumstances, which define the scope of synchronization, are:
 Synchronization between one or more processes must happen at a single point in order for one or more other processes
to continue.

 One or more processes have to wait for an asynchronous (not existing or occurring at the same time) condition in order
to continue.

 A process must get exclusive access to a shared resource.

 If synchronization is not done properly it may lead to multiple points of failure modes including loss of consistency,
isolation and durability, deadlock, lick, atomicity, and loss of serializability.

11. Transparency:

 DOS are perceived by programmers and users as a whole system, rather than a collection of cooperating components.
Thus meaning the communication between components is hidden. Transparency directly affects the decision making in
every aspect of design of a DOS; it can imply certain requirements and/or restrictions on other design considerations.

ADVANTAGES OF DOS

1. Scalability: As computing occurs on each node independently, it is simple and inexpensive to add more nodes and
functionality as required. These systems are easily scalable as many systems can be easily added to the network.

2. Reliability: Most distributed systems are made from many nodes that work together which ultimately make them fault
tolerant. The system doesn‟t experience any disruptions if a single machine fails.

3. Performance: These systems are regarded to be very efficient as the work load can be broken up and sent to multiple
machines, therefore reducing data processing. It delivers better performance than a single system. Load on host computer
reduces.

4. Data Sharing: Nodes can easily share data with other nodes as they are connected with each other. Since resources are
being shared, computation is highly fast and durable.

5. No domino effect in case of a node failure: The failure of one node in a DOS does not have a domino effect and
enables all other nodes fail. Failure of one will not affect the other network communication, as all systems are independent
from each other.

6. Shareable: Resources, for instance like printers, can be shared with multiple nodes rather than just being constrained to
just one node.

DISADVANTAGES OF DOS

1. Scheduling: The system has to decide which jobs need to be executed, when they should be executed, and where they
should be executed.

2. Latency: The more widely distributed a system is the more latency can be experienced with communications. This
therefore results in teams/developers to make tradeoffs between availability, consistency and latency.

3. Observability: It can be a real challenge to gather, process, present, and monitor hardware usage metrics for large
clusters.

4. Security: It is difficult to place adequate security in DOS, as the nodes and the connections need to be secured.

5. Data Loss: Some data/messages may be lost in the network while moving from one node to another.

6. Complicated Database: In comparison to a single user system, the database connected to a DOS is relatively complicated
and difficult to handle.

7. Overloading: If multiple nodes in DOS send data all at once, then the system network may become overloaded.

8. Expensive: These types of systems are not readily available as they are very expensive.
9. Complex Software: Underlying software is highly complex and is not understood very well compared to other systems.

APPLICATIONS OF DOS

1. Telecommunication Networks: DOS are useful in phone network and cellular networks. Networks such as the Internet,
Wireless sensor networks, routing algorithms are also examples of a DOS.

2. Network Applications: Many network applications utilize DOS, such as Web, P2P networks, multiplayer web based games
and virtual communities. An example of such systems includes: banking systems, and airline reservation booking systems.

3. Real Time Process Control: Real time process control system that operates with a deadline, such examples include aircraft
control systems.

4. Parallel Computation: Systematic computer, which includes cluster computing and grid computing as well as varied
volunteer computing projects are based on DOS.

EXAMPLES OF DOS

 LOCUS - In LOCUS operating system, can be accessed local and remote file in simultaneously without any location
hindrance
 MICROS - MICROS operating system maintains the balance load of data while allotting the jobs to all nodes of entire
system
 IRIX - IRIX operating system is used in the UNIX system V and LINUX
 DYNIX - DYNIX operating system is developed for the Symmetry multiprocessor computers
 AIX - AIX operating system is designed for IBM RS/ 6000 computers
 Solaris - Solaris operating system designed for SUN multiprocessor workstations
 Mach - Mach operating system allows the multithreading and multitasking features
 OSF/ 1 - OSF/ 1 operating system is compatible with UNIX, and it is designed by Open Foundation Software Company

PROCESS AND PROCESSOR IN DOS

In most traditional OS, each process has an address space and a single thread of control. It is desirable to have multiple
threads of control sharing one address space but running in quasi-parallel.

Processor Allocation

 Processor allocation determines which process is assigned to which processor; it is also called load distribution.

Two categories:

1. Static load distribution-non-migratory, once allocated, cannot move, no matter how overloaded the machine is.
2. Dynamic load distribution-migratory, can move even if the execution started. But algorithm is complex.

The Goals of Allocation:

 Maximize CPU utilization.


 Minimize mean response time/ Minimize response ratio.

What is RPC in Operating System?

Remote Procedure Call (RPC) is a powerful technique for constructing distributed, client-server based applications. It is based
on extending the conventional local procedure calling so that the called procedure need not exist in the same address space
as the calling procedure. The two processes may be on the same system, or they may be on different systems with a network
connecting them.

Remote Procedure Call or RPC is a powerful technique for constructing distributed, client-server-based applications. It is also
known as a function call or a subroutine call. A remote procedure call is when a computer program causes a procedure to
execute in a different address space, coded as a local procedure call, without the programmer explicitly stating the details for
the remote interaction. The programmer writes essentially the same code whether the subroutine is local to the executing
program or remote. This is a form of client-server interaction implemented via a request-response message-passing system.
The RPC model implies location transparency that calling procedures are largely the same, whether local or remote. Usually,
they are not identical, so that local calls can be distinguished from remote calls. Remote calls are usually orders of magnitude
slower and less reliable than local calls, so distinguishing them is important.

RPCs are a form of inter-process communication (IPC), in that different processes have different address spaces. They have
distinct virtual address spaces on the same host machine, even though the physical address space is the same. While if they
are on different hosts, the physical address space is different.

1. The calling environment is suspended, procedure parameters are transferred across the network to the environment where
the procedure is to execute, and the procedure is executed there.

2. When the procedure finishes and produces its results, its results are transferred back to the calling environment, where
execution resumes as if returning from a regular procedure call.

NOTE: RPC is especially well suited for client-server (e.g. query-response) interaction in which the flow of
control alternates between the caller and callee. Conceptually, the client and server do not both execute at the same time.
Instead, the thread of execution jumps from the caller to the callee and then back again.

You might also like