You are on page 1of 124

OPERATING SYSTEMS NOTES

Prepared by,
Mrs K. Jayanthi, MCA, ME(CSE).,
Assistant Professor,
KGiSL Institute of Information Management.

1
SYLLABUS

Unit:1 INTRODUCTION 12Hours


INTRODUCTION: Definition of OS-Mainframe System-Desktop Systems-Multi processor
System-Distributed-Clustered-Real time Systems-Handheld Systems-Operating System
Structure-System Components-Services-System Calls-System Programs-System Design and
Implementation.
Unit:2 PROCESS MANAGEMENT 12Hours
PROCESS MANAGEMENT: Concepts-Process Scheduling-Operations on Processes
Cooperating Processes-Inter Process Communication-CPU Scheduling-Scheduling Concepts
Criteria-Scheduling Algorithms-Multiprocessor Scheduling-Real time Scheduling.
Unit:3 PROCESS SYNCHRONIZATION 12Hours
PROCESS SYNCHRONIZATION: Critical Section-Synchronization Hardware Semaphores
Problems of Synchronization-Critical Regions-Monitors-Deadlocks Characterization-
Handling Deadlocks-Deadlock Prevention – Avoidance-Detection-Deadlock Recovery.
Unit:4 MEMORY MANAGEMENT 12 hours
MEMORY MANAGEMENT: Storage Hierarchy-Storage Management Strategies Contiguous
- Non-Contiguous Storage Allocation-Single User-Fixed Partition-Variable Partition
Swapping Virtual Memory-Basic Concepts-Multilevel Organization-Block Mapping-Paging
Segmentation-Page Replacement Methods-Locality-Working Sets.
Unit:5 I/O AND FILE SYSTEMS 10Hours
I/O AND FILE SYSTEMS: Disk Scheduling-File Concepts-File System Structure-Access
Methods-Directory Structure-Protection-Directory Implementation-Allocation Methods-Free
Space Management Case Study: Linux Operating System – Commands, Shell Programming,
Report writing
Text Books
1 Silberschatz and Galvin, Operating System Concepts, 6th Edition, John Wiley & Sons, Inc.,
2004.
2 Milankovic M., Operating System Concepts and Design, 2nd Edition, McGraw Hill, 1992.
Reference Books
1 P.C.Bhatt, An Introduction to Operating Systems-Concepts and Practice, Prentice Hall Of
India, 2004.
2 H.M.Deitel, An Introduction to Operating Systems, 2nd Edition, Pearson Education, 2002.

2
UNIT 1
INTRODUCTION
Definition of OS-Mainframe System-Desktop Systems-Multi processor System-
Distributed-Clustered-Real time Systems-Handheld Systems-Operating System
Structure-System Components-Services-System Calls-System Programs-System Design
and Implementation.
Definition of OS
An Operating System (OS) is an interface between a computer user and computer
hardware. An operating system is a software which performs all the basic tasks like file
management, memory management, process management, handling input and output, and
controlling peripheral devices such as disk drives and printers.
Some popular Operating Systems include Linux Operating System, Windows Operating
System, VMS, OS/400, AIX, z/OS, etc.
An operating system is a program that acts as an interface between the user and the computer
hardware and controls the execution of all kinds of programs.

Following are some of important functions of an operating System.


• Memory Management
• Processor Management
• Device Management
• File Management
• Security

3
• Control over system performance
• Job accounting
• Error detecting aids
• Coordination between other software and users

Mainframe System
Mainframes are a type of computers, which are made for ‘throughput’ as fast as
possible; a throughput can be defined as “the rate at which the data is processed”. And also,
mainframes are majorly used for transaction processing; a transaction can be defined as “a set
of operations including disk read and write, operating system calls, transferring data from one
subsystem to another, etc…”
The mainframes have more processing power compared to servers and microcomputers
(like- laptop, PC, etc…), but have less processing power compared to a supercomputer.
The main focus of the Main-frames is throughput, “A throughput is a rate at which something
is processed.”
Components of a Mainframe Computer
The mainframes have two types of processors, the first one is the Main processor and
another one is System assistance Processor or SAP. The SAP processors do not process any
data but move the data from one place to another as fast as possible.
Each processor can have up to 7 to 10 cores which are specially designed and engineered for
‘higher throughput’. Each mainframe can have up to 160 of I/O cards. And also, they have got
some serious amount of ROM (Solid State Drives) for faster data storage and transfer.
The fact that the main-frames have so many I/O cards is because these are made for
redundancy that is, if one card fails, other cards will take over the work-load of that card until
the card is replaced.
Why are mainframe computers used?
Main-frames are used for Reliability, Redundancy, and availability. These are must
computers to have where ‘0’ downtime is acceptable.
Due to the fact that these computers are reliable and have redundancy that is if an I/O card fails
due to any reason, its workload will automatically get transferred to other I/O card ensuring ‘0’
downtime which is of great importance for ensuring proper transactions.
Similarly, if there is some problem with a processor module, the workload will be transferred
to other processors.
How are Mainframes different from Supercomputer?
The Main-Frames are used for fast processing or ‘throughput’ whereas the
Supercomputers are used for number crunching where they deal with a colossal amount of data
to predict weather, solve complex mathematical models for computation and are majorly used
in research only.

4
Advantages

• The processing speed is very fast.


• Can handle multiple inputs at same time.
• Redundancy, can withstand failure of a part without affecting the function of rest of the
computer.
• Always available, as once started they will remain on for rest of the time.
• Reliability.
Disadvantages

• Mainframes cannot be used as a normal computer, because they are made for specific
task only.
• It requires a special OS to run.
• Are very expensive.
• Mainly used for commercial purposes like transaction processing.
• Cannot calculate or solve complex problems like a supercomputer.

Desktop Systems

What is Desktop Operating System?

The desktop OS is the environment where the user controls a personal computer (Desktop,
Notebook PC). It aids in the management of computer hardware and software resources. It
supports fundamental features such as task scheduling, peripheral control, printing,
input/output, and memory allocation. The operating system serves as a bridge between
programs and computer hardware.

Some operating systems need installation, while others may be preinstalled on new computers.
Microsoft Windows, Linux, and MacOS are the most popular desktop operating systems.
Modern OS uses the Graphical User Interface. The GUI permits you to click on icons, buttons,
and menus using your mouse. Everything on the screen is displayed using a combination of
text and graphics.

The Microsoft Windows operating system was first released in the mid-1980s, and the most
recent versions are Windows 11 (2021), Windows 10 (2015), Windows 8 (2012), Windows 7
(2009), and Windows Vista (2007). Windows comes preinstalled in the new operating systems,
which makes it the most popular OS in the world. MacOS is another popular OS that was
developed by Apple, and it comes preinstalled on all Macintosh computers. Some popular
versions of the MacOS are Mojave (2018), High Sierra (2017), and Sierra (2016). MacOS has
a market share of less than 10% of all operating systems worldwide, far less than Microsoft
Windows (More than 80%).

5
Advantages and disadvantages of Desktop Operating System

There are various advantages and disadvantages of the Desktop Operating System. Some of
the advantages and of the Desktop Operating System are as follows:

Advantages

1. Operating System provides a user-friendly graphic interface for all users. It also offers
several icons, menus, buttons, and various navigations. So, users may interact with the
computer system easily.
2. The operating system permits the user to be shared the data or related data with other
users via Modems, Printers, and Players. Furthermore, a single user can share the same
material with several people simultaneously via email. Moreover, many photos,
software, and media files may be transferred from one system to another system using
the operating system.
3. An operating system is a piece of software that must be updated regularly to keep up
with the rapidly changing features. The operating systems must improve their
benchmarks and handle all aspects of computer operation with other apps and software
receiving updates to increase their usefulness. An operating system may be easily
updated without any difficulty.
4. An operating system may manage several tasks at the same time. It permits users to
perform multiple tasks simultaneously. There is no requirement to close one window to
open another window.
5. On the computer, there is a large amount of user data that may only be accessed with
the assistance of an operating system. Aside from storing and accessing data, and OS's
other important responsibility is to handle data safely and securely.

Disadvantages

1. Some OSs are more expensive than open-source platforms like Linux. While users may
use a free operating system, they are often more difficult to operate than conventional
operating systems. Furthermore, operating systems with GUI functionality and other
built-in features are expensive, such as Microsoft Windows.
2. Operating systems are highly complicated, and the language used to design them is not
straightforward or well-defined. Moreover, the user may not understand whether there
is a problem with the OS. So, it can't be resolved quickly.
3. Fragmentation occurs in a computer when storage memory is broken up into parts.
Internal fragmentation happens when the method of process exceeds the memory size.
On the other hand, external fragmentation happens when the method or process
eliminates.
4. If the central OS fails, the whole system will halt, and the system will not work.
Furthermore, an operating system is the core of a computer system, and without an
operating system, it cannot work.

6
Multiprocessor Operating system

Multiprocessor system means, there are more than one processor which work parallel to
perform the required operations.
It allows the multiple processors, and they are connected with physical memory, computer
buses, clocks, and peripheral devices.
The main objective of using a multiprocessor operating system is to increase the execution
speed of the system and consume high computing power.
Advantages
The advantages of multiprocessor systems are as follows −
• If there are multiple processors working at the same time, more processes can
be executed parallel at the same time. Therefore, the throughput of the system
will increase.
• Multiprocessor systems are more reliable. Due to the fact that there are more
than one processor, in case of failure of any one processor will not make the
system come to a halt. Although the system will become slow if it happens but
still it will work.
• Electricity consumption of a multiprocessor system is less than the single
processor system. This is because, in single processor systems, many processes
have to be executed by only one processor so there is a lot of load on it. But in
case of multiple processor systems, there are many processors to execute the
processes so the load on each processor will be comparatively less so electricity
consumed will also be less.
Fields
The different fields of multiprocessor operating systems used are as follows −
• Asymmetric Multiprocessor − Every processor is given seeded tasks in this
operating system, and the master processor has the power for running the entire
system. In the course, it uses the master-slave relationship.
• Symmetric Multiprocessor − In this system, every processor owns a similar copy
of the OS, and they can make communication in between one another. All
processors are connected with peering relationship nature, meaning it won’t be
using master & slave relation.
• Shared memory Multiprocessor − As the name indicates, each central
processing unit contains distributable common memory.
• Uniform Memory Access Multiprocessor (UMA) − In this system, it allows
accessing all memory at a consistent speed rate for all processors.
• Distributed memory Multiprocessor − A computer system consisting of a range
of processors, each with its own local memory, connected through a network,
which means all the kinds of processors consist of their own private memory.

7
• NUMA Multiprocessor − The abbreviation of NUMA is Non-Uniform Memory
Access Multiprocessor. It entails some areas of the memory for approaching at
a swift rate and the remaining parts of memory are used for other tasks.
The best Operating system in multiprocessor and parallel computing environment is UNIX,
because it has many advantages such as,
• It is multi-user.
• It is portable.
• It is good for multitasking.
• It has an organized file system.
• It has device independence.
• Utilities are brief and operation commands can be combined in a single line.
• Unix provides various services, as it has built-in administrative tools,
• UNIX can share files over electronic networks with many various kinds of
equipment.

Distributed Operating System

A distributed operating system (DOS) is an essential type of operating system.


Distributed systems use many central processors to serve multiple real-time applications and
users. As a result, data processing jobs are distributed between the processors.

It connects multiple computers via a single communication channel. Furthermore, each


of these systems has its own processor and memory. Additionally, these CPUs communicate
via high-speed buses or telephone lines. Individual systems that communicate via a single
channel are regarded as a single entity. They're also known as loosely coupled systems.

This operating system consists of numerous computers, nodes, and sites joined together
via LAN/WAN lines. It enables the distribution of full systems on a couple of center
processors, and it supports many real-time products and different users. Distributed operating

8
systems can share their computing resources and I/O files while providing users with virtual
machine abstraction.

Types of Distributed Operating System

There are various types of Distributed Operating systems. Some of them are as follows:

1. Client-Server Systems
2. Peer-to-Peer Systems
3. Middleware
4. Three-tier
5. N-tier

Client-Server System

This type of system requires the client to request a resource, after which the server gives the
requested resource. When a client connects to a server, the server may serve multiple clients at
the same time.

Client-Server Systems are also referred to as "Tightly Coupled Operating Systems". This
system is primarily intended for multiprocessors and homogenous multicomputer. Client-
Server Systems function as a centralized server since they approve all requests issued by client
systems.

Server systems can be divided into two parts:

1. Computer Server System

This system allows the interface, and the client then sends its own requests to be executed as
an action. After completing the activity, it sends a back response and transfers the result to the
client.

2. File Server System

It provides a file system interface for clients, allowing them to execute actions like file creation,
updating, deletion, and more.

Peer-to-Peer System

The nodes play an important role in this system. The task is evenly distributed among the nodes.
Additionally, these nodes can share data and resources as needed. Once again, they require a
network to connect.

The Peer-to-Peer System is known as a "Loosely Couple System". This concept is used in
computer network applications since they contain a large number of processors that do not
9
share memory or clocks. Each processor has its own local memory, and they interact with one
another via a variety of communication methods like telephone lines or high-speed buses.

Middleware

Middleware enables the interoperability of all applications running on different operating


systems. Those programs are capable of transferring all data to one other by using these
services.

Three-tier

The information about the client is saved in the intermediate tier rather than in the client, which
simplifies development. This type of architecture is most commonly used in online
applications.

N-tier

When a server or application has to transmit requests to other enterprise services on the
network, n-tier systems are used.

Features of Distributed Operating System

There are various features of the distributed operating system. Some of them are as follows:

Scalability

It refers to the fact that the system's efficiency should not vary as new nodes are added to the
system. Furthermore, the performance of a system with 100 nodes should be the same as that
of a system with 1000 nodes.

Resource Sharing

Its most essential feature is that it allows users to share resources. They can also share resources
in a secure and controlled manner. Printers, files, data, storage, web pages, etc., are examples
of shared resources.

Flexibility

A DOS's flexibility is enhanced by modular qualities and delivers a more advanced range of
high-level services. The kernel/ microkernel's quality and completeness simplify the
implementation of such services.

Transparency

10
It is the most important feature of the distributed operating system. The primary purpose of a
distributed operating system is to hide the fact that resources are shared. Transparency also
implies that the user should be unaware that the resources he is accessing are shared.
Furthermore, the system should be a separate independent unit for the user.

Heterogeneity

The components of distributed systems may differ and vary in operating systems, networks,
programming languages, computer hardware, and implementations by different developers.

Fault Tolerance

Fault tolerance is that process in which user may continue their work if the software or
hardware fails.

Examples of Distributed Operating System

There are various examples of the distributed operating system. Some of them are as follows:

Solaris

It is designed for the SUN multiprocessor workstations

OSF/1

It's compatible with Unix and was designed by the Open Foundation Software Company.

Micros

The MICROS operating system ensures a balanced data load while allocating jobs to all nodes
in the system.

DYNIX

It is developed for the Symmetry multiprocessor computers.

Locus

It may be accessed local and remote files at the same time without any location hindrance.

Mach

It allows the multithreading and multitasking features.

Applications of Distributed Operating System

11
There are various applications of the distributed operating system. Some of them are as follows:

Network Applications

DOS is used by many network applications, including the Web, peer-to-peer networks,
multiplayer web-based games, and virtual communities.

Telecommunication Networks

DOS is useful in phones and cellular networks. A DOS can be found in networks like the
Internet, wireless sensor networks, and routing algorithms.

Parallel Computation

DOS is the basis of systematic computing, which includes cluster computing and grid
computing, and a variety of volunteer computing projects.

Real-Time Process Control

The real-time process control system operates with a deadline, and such examples include
aircraft control systems.

Advantages and Disadvantages of Distributed Operating System

There are various advantages and disadvantages of the distributed operating system. Some of
them are as follows:

Advantages

There are various advantages of the distributed operating system. Some of them are as follow:

1. It may share all resources (CPU, disk, network interface, nodes, computers, and so on)
from one site to another, increasing data availability across the entire system.
2. It reduces the probability of data corruption because all data is replicated across all
sites; if one site fails, the user can access data from another operational site.
3. The entire system operates independently of one another, and as a result, if one site
crashes, the entire system does not halt.
4. It increases the speed of data exchange from one site to another site.
5. It is an open system since it may be accessed from both local and remote locations.
6. It helps in the reduction of data processing time.
7. Most distributed systems are made up of several nodes that interact to make them fault-
tolerant. If a single machine fails, the system remains operational.

12
Disadvantages

There are various disadvantages of the distributed operating system. Some of them are as
follows:

1. The system must decide which jobs must be executed when they must be executed, and
where they must be executed. A scheduler has limitations, which can lead to
underutilized hardware and unpredictable runtimes.
2. It is hard to implement adequate security in DOS since the nodes and connections must
be secured.
3. The database connected to a DOS is relatively complicated and hard to manage in
contrast to a single-user system.
4. The underlying software is extremely complex and is not understood very well
compared to other systems.
5. The more widely distributed a system is, the more communication latency can be
expected. As a result, teams and developers must choose between availability,
consistency, and latency.
6. These systems aren't widely available because they're thought to be too expensive.
7. Gathering, processing, presenting, and monitoring hardware use metrics for big clusters
can be a real issue.

Clustered Operating System

Cluster systems are similar to parallel systems because both systems use multiple CPUs. The
primary difference is that clustered systems are made up of two or more independent systems
linked together. They have independent computer systems and a shared storage media, and all
systems work together to complete all tasks. All cluster nodes use two different approaches to
interact with one another, like message passing interface (MPI) and parallel virtual machine
(PVM).

In this article, you will learn about the Clustered Operating system, its types, classification,
advantages, and disadvantages.

13
What is the Clustered Operating System?

Cluster operating systems are a combination of software and hardware clusters. Hardware
clusters aid in the sharing of high-performance disks among all computer systems, while
software clusters give a better environment for all systems to operate. A cluster system consists
of various nodes, each of which contains its cluster software. The cluster software is installed
on each node in the clustered system, and it monitors the cluster system and ensures that it is
operating properly. If one of the clustered system's nodes fails, the other nodes take over its
storage and resources and try to restart.

Cluster components are generally linked via fast area networks, and each node executing its
instance of an operating system. In most cases, all nodes share the same hardware and operating
system, while different hardware or different operating systems could be used in other cases.
The primary purpose of using a cluster system is to assist with weather forecasting, scientific
computing, and supercomputing systems.

There are two clusters available to make a more efficient cluster. These are as follows:

1. Software Cluster
2. Hardware Cluster

Software Cluster

The Software Clusters allows all the systems to work together.

Hardware Cluster

It helps to allow high-performance disk sharing among systems.

14
Types of Clustered Operating System

There are mainly three types of the clustered operating system:

1. Asymmetric Clustering System


2. Symmetric Clustering System
3. Parallel Cluster System

Asymmetric Clustering System

In the asymmetric cluster system, one node out of all nodes is in hot standby mode, while the
remaining nodes run the essential applications. Hot standby mode is completely fail-safe and
also a component of the cluster system. The node monitors all server functions; the hot standby
node swaps this position if it comes to a halt.

Symmetric Clustering System

Multiple nodes help run all applications in this system, and it monitors all nodes
simultaneously. Because it uses all hardware resources, this cluster system is more reliable than
asymmetric cluster systems.

Parallel Cluster System

A parallel cluster system enables several users to access similar data on the same shared storage
system. The system is made possible by a particular software version and other apps.

Classification of clusters

Computer clusters are managed to support various purposes, from general-purpose business
requirements like web-service support to computation-intensive scientific calculations. There
are various classifications of clusters. Some of them are as follows:

1. Fail Over Clusters

The process of moving applications and data resources from a failed system to another system
in the cluster is referred to as fail-over. These are the databases used to cluster important
missions, application servers, mail, and file.

2. Load Balancing Cluster

The cluster requires better load balancing abilities amongst all available computer systems. All
nodes in this type of cluster can share their computing workload with other nodes, resulting in
better overall performance. For example, a web-based cluster can allot various web queries to
various nodes, so it helps to improve the system speed. When it comes to grabbing requests,
only a few cluster systems use the round-robin method.

15
3. High Availability Clusters

These are also referred to as "HA clusters". They provide a high probability that all resources
will be available. If a failure occurs, such as a system failure or the loss of a disk volume, the
queries in the process are lost. If a lost query is retried, it will be handled by a different cluster
computer. It is widely used in news, email, FTP servers, and the web.

Advantages and Disadvantages of Cluster Operating System

Various advantages and disadvantages of the Clustered Operating System are as follows:

Advantages

Various advantages of Clustered Operating System are as follows:

1. High Availability

Although every node in a cluster is a standalone computer, the failure of a single node doesn't
mean a loss of service. A single node could be pulled down for maintenance while the
remaining clusters take on a load of that single node.

2. Cost Efficiency

When compared to highly reliable and larger storage mainframe computers, these types of
cluster computing systems are thought to be more cost-effective and cheaper. Furthermore,
most of these systems outperform mainframe computer systems in terms of performance.

3. Additional Scalability

A cluster is set up in such a way that more systems could be added to it in minor increments.
Clusters may add systems in a horizontal fashion. It means that additional systems could be
added to clusters to improve their performance, fault tolerance, and redundancy.

4. Fault Tolerance

Clustered systems are quite fault-tolerance, and the loss of a single node does not result in the
system's failure. They might also have one or more nodes in hot standby mode, which allows
them to replace failed nodes.

5. Performance

The clusters are commonly used to improve the availability and performance over the single
computer systems, whereas usually being much more cost-effective than the single computer
system of comparable speed or availability.

16
6. Processing Speed

The processing speed is also similar to mainframe systems and other types of supercomputers
on the market.

Disadvantages

Various disadvantages of the Clustered Operating System are as follows:

1. Cost-Effective

One major disadvantage of this design is that it is not cost-effective. The cost is high, and the
cluster will be more expensive than a non-clustered server management design since it requires
good hardware and a design.

2. Required Resources

Clustering necessitates the use of additional servers and hardware, making monitoring and
maintenance difficult. As a result, infrastructure must be improved.

3. Maintenance

It isn't easy to system establishment, monitor, and maintenance this system.

Real-Time operating system

What do you mean by Real-Time Operating System?

A real-time operating system (RTOS) is a special-purpose operating system used in


computers that has strict time constraints for any job to be performed. It is employed mostly in
those systems in which the results of the computations are used to influence a process while it
is executing. Whenever an event external to the computer occurs, it is communicated to the
computer with the help of some sensor used to monitor the event. The sensor produces the
signal that is interpreted by the operating system as an interrupt. On receiving an interrupt, the
operating system invokes a specific process or a set of processes to serve the interrupt.

17
This process is completely uninterrupted unless a higher priority interrupt occurs during its
execution. Therefore, there must be a strict hierarchy of priority among the interrupts. The
interrupt with the highest priority must be allowed to initiate the process , while lower priority
interrupts should be kept in a buffer that will be handled later. Interrupt management is
important in such an operating system.Java Try Catch

Real-time operating systems employ special-purpose operating systems because


conventional operating systems do not provide such performance.

The various examples of Real-time operating systems are:

o MTS
o Lynx
o QNX
o VxWorks etc.

Applications of Real-time operating system (RTOS):

RTOS is used in real-time applications that must work within specific deadlines. Following are
the common areas of applications of Real-time operating systems are given below.

o Real-time running structures are used inside the Radar gadget.


o Real-time running structures are utilized in Missile guidance.
o Real-time running structures are utilized in on line inventory trading.
o Real-time running structures are used inside the cell phone switching gadget.
o Real-time running structures are utilized by Air site visitors to manipulate structures.
o Real-time running structures are used in Medical Imaging Systems.
o Real-time running structures are used inside the Fuel injection gadget.

18
o Real-time running structures are used inside the Traffic manipulate gadget.
o Real-time running structures are utilized in Autopilot travel simulators.

Types of Real-time operating system

Following are the three types of RTOS systems are:

Hard Real-Time operating system:

In Hard RTOS, all critical tasks must be completed within the specified time duration, i.e.,
within the given deadline. Not meeting the deadline would result in critical failures such as
damage to equipment or even loss of human life.

For Example,

Let's take an example of airbags provided by carmakers along with a handle in the driver's seat.
When the driver applies brakes at a particular instance, the airbags grow and prevent the driver's
head from hitting the handle. Had there been some delay even of milliseconds, then it would
have resulted in an accident.

Similarly, consider an on-stock trading software. If someone wants to sell a particular share,
the system must ensure that command is performed within a given critical time. Otherwise, if
the market falls abruptly, it may cause a huge loss to the trader.

19
Soft Real-Time operating system:

Soft RTOS accepts a few delays via the means of the Operating system. In this kind of RTOS,
there may be a closing date assigned for a particular job, but a delay for a small amount of time
is acceptable. So, cut off dates are treated softly via means of this kind of RTOS.

For Example,

This type of system is used in Online Transaction systems and Livestock price quotation
Systems.

Firm Real-Time operating system:

In Firm RTOS additionally want to observe the deadlines. However, lacking a closing date
might not have a massive effect, however may want to purposely undesired effects, like a
massive discount within the fine of a product.

For Example, this system is used in various forms of Multimedia applications.

Advantages of Real-time operating system:

The benefits of real-time operating system are as follows-:

o Easy to layout, develop and execute real-time applications under the real-time operating
system.
o The real-time working structures are extra compact, so those structures require much
less memory space.
o In a Real-time operating system, the maximum utilization of devices and systems.
o Focus on running applications and less importance to applications that are in the queue.
o Since the size of programs is small, RTOS can also be embedded systems like in
transport and others.
o These types of systems are error-free.
o Memory allocation is best managed in these types of systems.

Disadvantages of Real-time operating system:

The disadvantages of real-time operating systems are as follows-

o Real-time operating systems have complicated layout principles and are very costly to
develop.
o Real-time operating systems are very complex and can consume critical CPU cycles.

20
Handheld Operating System
Handheld operating systems are present in all handheld devices like Smartphones and
tablets. It is also called a Personal Digital Assistant. The popular handheld device in today’s
market is android and iOS.
These operating systems need a high processing processor and also embedded with
different types of sensors.
Handheld computers, or PDAs (personal digital assistants), are becoming more popular
as the world transitions into a place where people receive their news as it happens, rather than
the day after it happens. Handheld computers allow the owner to take their work with them
wherever they go while staying connected to the Internet. While there are several benefits to
these computers, there are also some drawbacks to handhelds.

Advantage: Portability

One advantage of handheld computers is their portability. They are smaller than the
typical laptop, allowing them to easily be tucked away into a backpack, briefcase or even one's
pocket. Within the classroom, they can replace notebooks and allow students to access course
material faster. They also provide a new and innovative way to educate. For businesspeople,
they can be easily brought onto airplanes and into meetings.

Disadvantage: Smaller Keyboards

In the case of handheld computers, the keyboards are smaller than those of laptop or
desktop computers. This can limit use to actions that require minimal typing, because the small
keyboard isn't conducive to productive word processing. For instance, instead of typing full
documents, as one would on a desktop or laptop computer, the user might only be able to
productively check email and surf the Internet on a handheld device.

Advantage: Price

Typical desktop and laptop computers can run more than $1,000. The handheld
computer is significantly cheaper. According to Consumer Search, the Palm TX PDA costs
about $260. The Palm Tungsten E2 costs about $190. So while these are miniaturized
computers, they are hundreds of dollars cheaper than the larger versions. Handheld computers
can be a cost-effective computing solution for a cash-strapped company, household or school
district.

21
Disadvantage: Size

While the size of handheld computers is an advantage in portability, it can be a


disadvantage, too. For instance, the smaller in size something is, the more likely it is to
forgotten or stolen. Typically, burglars are looking for the most valuable thing they can find as
quickly as they can get it. Since handheld computers can be stowed in bags, purses and
backpacks, the probability of them being stolen increases. Furthermore, the smaller something
is, the more likely it is to be lost or misplaced by the owner.

Operating System Structure

An operating system provides the environment within which programs are executed.
Internally, operating systems vary greatly in their makeup, since they are organized along many
different lines. There are, however, many commonalities, which we consider in this section.
One of the most important aspects of operating systems is the ability to multiprogram.
A single user cannot, in general, keep either the CPU or the I/O devices always busy.
Multiprogramming increases CPU utilization by organizing jobs (code and data) so that the
CPU always has one to execute. The idea is as follows: The operating system keeps several
jobs in memory simultaneously. This set of jobs can be a subset of the jobs kept in the job
pool—which contains all jobs that enter the system—since the number of jobs that can be kept
simultaneously in memory is usually smaller than the number of jobs that can be kept in the
job pool.

22
The operating system picks and begins to execute one of the jobs in memory.
Eventually, the job may have to wait for some tasks, such as an I/O operation, to complete.
When that job needs to wait, the CPU is switched to another job, and so on. Eventually, the
first job finishes waiting and gets the CPU back. If at least one job needs to execute, the CPU
is never idle.
This idea is common in other life situations. A lawyer does not work for only one client
at a time, for example. While one case is waiting to go to trial or have papers typed, the lawyer
can work on another case. If he has enough clients, the lawyer will never be idle for lack of
work. Multiprogrammed systems provide an environment in which the various system
resources (for example, CPU, memory, and peripheral devices) are utilized effectively, but they
do not provide for user interaction with the computer system. Time sharing (or multitasking)
is a logical extension of multiprogramming.
In time-sharing systems, the CPU executes multiple jobs by switching among them, but
the switches occur so frequently that the users can interact with each program while it is
running. Time sharing requires an interactive (or hands-on) computer system, which provides
direct communication between the user and the system. The user gives instructions to the
operating system or to a program directly, using a input device such as a keyboard or a mouse,
and waits for immediate results on an output device. Accordingly, the response time should be
short—typically less than one second. A time-shared operating system allows many users to
share the computer simultaneously. Since each action or command in a time-shared system
tends to be short, only a little CPU time is needed for each user.
As the system switches rapidly from one user to the next, each user is given the
impression that the entire computer system is dedicated to his use, even though it is being
shared among many users. A time-shared operating system uses CPU scheduling and
multiprogramming to provide each user with a small portion of a time-shared computer. Each
user has at least one separate program in memory. A program loaded into 1.5 Operating-System
Operations 17 memory and executing is called a process.
When a process executes, it typically executes for only a short time before it either
finishes or needs to perform I/O. I/O may be interactive; that is, output goes to a display for the
user, and input comes from a user keyboard, mouse, or other device. Since interactive I/O
typically runs at "people speeds," it may take a long time to complete. Input, for example, may
be bounded by the user's typing speed; seven characters per second is fast for people but
incredibly slow for computers. Rather than let the CPU sit idle as this interactive input takes
place, the operating system will rapidly switch the CPU to the program of some other user.
Time-sharing and multiprogramming require several jobs to be kept simultaneously in
memory. Since in general main memory is too small to accommodate all jobs, the jobs are kept
initially on the disk in the job pool. This pool consists of all processes residing on disk awaiting
allocation of main memory. If several jobs are ready to be brought into memory, and if there is
not enough room for all of them, then the system must choose among them. When the operating
system selects a job from the job pool, it loads that job into memory for execution Finally,
running multiple jobs concurrently requires that their ability to affect one another be limited in

23
all phases of the operating system, including process scheduling, disk storage, and memory
management. These considerations are discussed throughout the text. In a time-sharing system,
the operating system must ensure reasonable response time, which is sometimes accomplished
through swapping, where processes are swapped in and out of main memory to the disk.
The main advantage of the virtual-memory scheme is that it enables users to run
programs that are larger than actual physical memory. Further, it abstracts main memory into
a large, uniform array of storage, separating logical memory as viewed by the user from
physical memory.

System Components
Components of Operating System: An operating system is a complex and vast system. We can
only create the operating system by dividing it into small parts. Each part must be a well-
defined portion of a system that appropriately describes the functions, inputs, and outputs.
There are various operating systems that do not have a similar structure, such as Windows,
Unix, Linux, and Mac. But many operating systems use the same operating system components
such as Memory Management, Process Management, and Input/output device Management.
There are various types of operating system components:

1. Process Management
2. File Management
3. Network Management
4. Main Memory Management
5. Secondary Storage Management
6. I/O Device Management
7. Security Management
8. Command Interpreter System

Process Management

The term ‘Process Management’ means a mechanism which is used to handle multiple
processes that are running in the operating system concurrently. Each software application
program contains processes that are associated with the software application program when
they are running.
For example: - If we use Google Chrome browser then a process also run for the
browser program. In the operating system, there are various processes which also run and
perform several functions.With the help of the process management, all the processes are
managed and kept running effectively.
Functions of Process Management
There are various types of process management functions:

• Synchronization Process

24
• Suspension and Resumption.
• Communication Process
• Process Creation and Deletion.

File Management

A file is defined as a set of correlated information and it is defined by the creator of the
file. Mostly files represent data, source and object forms, and programs. Data files can be of
any type like alphabetic, numeric, and alphanumeric.
Functions of File Management
There are various functions of file management:

• Mapping Files onto secondary storage.


• With the help of a file management we can create and delete a file.
• It offers the facility of directory creation and deletion.
• Helps in backup files on to secondary memory.
• File and directory manipulation.

Network Management

We can understand from the network management that it is the process of managing
and administering the computer network. Network management comprises fault analysis,
maintaining the quality of service, provisioning of networks, and performance management.
The collection of a computer/processors is known as a distributed system.
A distributed system is one which does not share its memory. In a distributed system,
each processor has its separate memory, and the communication between the processors takes
place with the help of different communication lines such as telephone lines, fiber optics, etc.
In the network, with the help of the computer network, computers are connected, and it is
configured in various ways. Network management provides the facility of network connection,
whether it is fully or partially connected network. And it helps to design routing and connection
strategies, which helps in overcoming the issues of security and connection.

Main Memory Management

Main memory is defined as a vast array of bytes or words; each contains its own
memory address. With the help of a series of reads or writes of a particular memory address,
the memory management process is performed.
Functions of Main Memory Management
There are various functions of main memory management:

• It helps in memory allocation when a process demand or requests.


• With the help of main memory management, we can preserve track of the primary
memory.

25
• Main memory management also helps in memory deallocation process that no longer
need the memory or if the process is terminated.
• Main memory management is also supported in a multiprogramming system where the
operating system has to decide which process will get the memory and how much.
• It helps to specify which part of the memory is in use and which part of the memory is
not in use.

Secondary Storage Management

The main task performed by the operating system is to execute the programs. And along
with the data, these programs are helpful to access the programs, which are present in the main
memory at the time of execution.
The memory size is too small, so we cannot store all the programs and data in the
memory permanently. So, the computer system provides the facility of secondary storage. In
today’s scenario, hard drives/SSD are used in most of the computer system as the primary
memory for both data as well as programs.
Although we also use secondary storage management with various storage devices such
as CD/DVD drives and USB flash drive.There are various programs such as compilers,
assemblers, etc. that are stored on a disk till the program is loaded into the memory, and then
for processing, programs use the disk as a source and the destination.
Functions of Secondary Storage Management
There are various functions of secondary storage management:

• Secondary storage helps in Disk Scheduling.


• Storage Allocation
• Free Space Management

I/O Device Management

The operating system is responsible to hide the details of the hardware devices from the
user.
Functions of the I/O Management
There are various functions of I/O management:

• It offers a general device driver code.


• The I/O device management provides a buffer caching system.
• The I/O device management provides the drivers for a specific hardware device.

Security Management

In an operating system, there are several processes that are required to be secured from
different activities. For this purpose, there are different types of procedures we use, which

26
ensures that the processes that you want to operate for the hardware resources such as memory
CPU, files, etc. must have appropriate authorization from the operating system.
For Example: With the help of the memory addressing hardware, we can ensure that a
process cannot execute outside its own address space or vice versa. Time confirms that there
is no process that has control over the CPU without leaving it.
Finally, to protect the process, there is no process that can do its own Input/Output. And it helps
to maintain the integrity of different peripheral devices.

Command Interpreter System


Command interpreter is an essential component of the operating system. It works as a primary
interface between the user and the system.

Operating System Services


An operating system provides an environment for the execution of programs. It
provides certain services to programs and to the users of those programs. The specific services
provided, of course, differ from one operating system to another, but we can identify common
classes. These operating system services are provided for the convenience of the programmer,
to make the programming task easier.

The above figure shows one view of the various operating-system services and how
they interrelate. One set of operating system services provides functions that are helpful to the
user.
• User interface. Almost all operating systems have a user interface (UI). This interface
can take several forms. One is a command-line interface (CLI), which uses text commands and
a method for entering them (say, a keyboard for typing in commands in a specific format with
specific options). Another is a batch interface, in which commands and directives to control
those commands are entered into files, and those files are executed. Most commonly, a

27
graphical user interface (GUI) is used. Here, the interface is a window system with a pointing
device to direct I/O, choose from menus, and make selections and a keyboard to enter text.
Some systems provide two or all three of these variations.
• Program execution. The system must be able to load a program into memory and to
run that program. The program must be able to end its execution, either normally or abnormally
(indicating error).
• I/O operations. A running program may require I/O, which may involve a file or an
I/O device. For specific devices, special functions may be desired (such as recording to a CD
or DVD drive or blanking a display screen). For efficiency and protection, users usually cannot
control I/O devices directly. Therefore, the operating system must provide a means to do I/O.
• File-system manipulation. The file system is of particular interest. Obviously,
programs need to read and write files and directories. They also need to create and delete them
by name, search for a given file, and list file information. Finally, some operating systems
include permissions management to allow or deny access to files or directories based on file
ownership. Many operating systems provide a variety of file systems, sometimes to allow
personal choice and sometimes to provide specific features or performance characteristics.
Communications. There are many circumstances in which one process needs to
exchange information with another process. Such communication may occur between
processes that are executing on the same computer or between processes that are executing on
different computer systems tied together by a computer network. Communications may be
implemented via shared memory, in which two or more processes read and write to a shared
section of memory, or message passing, in which packets of information in predefined formats
are moved between processes by the operating system.
• Error detection. The operating system needs to be detecting and correcting errors
constantly. Errors may occur in the CPU and memory hardware (such as a memory error or a
power failure), in I/O devices (such as a parity error on disk, a connection failure on a network,
or lack of paper in the printer), and in the user program (such as an arithmetic overflow, an
attempt to access an illegal memory location, or a too-great use of CPU time). For each type of
error, the operating system should take the appropriate action to ensure correct and consistent
computing. Sometimes, it has no choice but to halt the system. At other times, it might
terminate an error-causing process or return an error code to a process for the process to detect
and possibly correct. Another set of operating system functions exists not for helping the user
but rather for ensuring the efficient operation of the system itself. Systems with multiple users
can gain efficiency by sharing the computer resources among the users.
• Resource allocation. When there are multiple users or multiple jobs running at the
same time, resources must be allocated to each of them. The operating system manages many
different types of resources. Some (such as CPU cycles, main memory, and file storage) may
have special allocation code, whereas others (such as I/O devices) may have much more general
request and release code. For instance, in determining how best to use the CPU, operating
systems have CPU-scheduling routines that consider the speed of the CPU, the jobs that must

28
be executed, the number of registers available, and other factors. There may also be routines to
allocate printers, USB storage drives, and other peripheral devices.
• Accounting. We want to keep track of which users use how much and what kinds of
computer resources. This record keeping may be used for accounting (so that users can be
billed) or simply for accumulating usage statistics. Usage statistics may be a valuable tool for
researchers who wish to reconfigure the system to improve computing services.
• Protection and security. The owners of information stored in a multiuser or networked
computer system may want to control use of that information. When several separate processes
execute concurrently, it should not be possible for one process to interfere with the others or
with the operating system itself. Protection involves ensuring that all access to system resources
is controlled. Security of the system from outsiders is also important. Such security starts with
requiring each user to authenticate himself or herself to the system, usually by means of a
password, to gain access to system resources. It extends to defending external I/O devices,
including network adapters, from invalid access attempts and to recording all such connections
for detection of break-ins. If a system is to be protected and secure, precautions must be
instituted throughout it. A chain is only as strong as its weakest link.

System Calls
The interface between a process and an operating system is provided by system calls. In
general, system calls are available as assembly language instructions. They are also included
in the manuals used by the assembly level programmers. System calls are usually made when
a process in user mode requires access to a resource. Then it requests the kernel to provide the
resource via a system call.
A figure representing the execution of the system call is given as follows −

29
As can be seen from this diagram, the processes execute normally in the user mode until a
system call interrupts this. Then the system call is executed on a priority basis in the kernel
mode. After the execution of the system call, the control returns to the user mode and execution
of user processes can be resumed.
In general, system calls are required in the following situations −
• If a file system requires the creation or deletion of files. Reading and writing
from files also require a system call.
• Creation and management of new processes.
• Network connections also require system calls. This includes sending and
receiving packets.
• Access to a hardware devices such as a printer, scanner etc. requires a system
call.

Types of System Calls

There are mainly five types of system calls. These are explained in detail as follows −
Process Control
These system calls deal with processes such as process creation, process termination etc.
File Management
These system calls are responsible for file manipulation such as creating a file, reading a file,
writing into a file etc.
Device Management
These system calls are responsible for device manipulation such as reading from device
buffers, writing into device buffers etc.
Information Maintenance
These system calls handle information and its transfer between the operating system and the
user program.
Communication
These system calls are useful for interprocess communication. They also deal with creating
and deleting a communication connection.
Some of the examples of all the above types of system calls in Windows and Unix are given
as follows −

Types of
Windows Linux
System Calls

Process CreateProcess()ExitProcess()WaitForSingleObjec
fork()exit()wait()
Control t()

30
Types of
Windows Linux
System Calls

File open()read()write()close
CreateFile()ReadFile()WriteFile()CloseHandle()
Management ()

Device
SetConsoleMode()ReadConsole()WriteConsole() ioctl()read()write()
Management

Information
GetCurrentProcessID()SetTimer()Sleep() getpid()alarm()sleep()
Maintenance

Communicatio CreatePipe()CreateFileMapping()MapViewOfFile
pipe()shmget()mmap()
n ()

There are many different system calls as shown above. Details of some of those system calls
are as follows −

open()

The open() system call is used to provide access to a file in a file system. This system call
allocates resources to the file and provides a handle that the process uses to refer to the file. A
file can be opened by multiple processes at the same time or be restricted to one process. It all
depends on the file organisation and file system.

read()

The read() system call is used to access data from a file that is stored in the file system. The
file to read can be identified by its file descriptor and it should be opened using open() before
it can be read. In general, the read() system calls takes three arguments i.e. the file descriptor,
buffer which stores read data and number of bytes to be read from the file.

write()

The write() system calls writes the data from a user buffer into a device such as a file. This
system call is one of the ways to output data from a program. In general, the write system calls
takes three arguments i.e. file descriptor, pointer to the buffer where data is stored and number
of bytes to write from the buffer.

close()

The close() system call is used to terminate access to a file system. Using this system call
means that the file is no longer required by the program and so the buffers are flushed, the file
metadata is updated and the file resources are de-allocated.

31
System Programs
Another aspect of a modern system is its collection of system programs.

In the above Figure, which depicted the logical computer hierarchy.At the lowest level
is hardware. Next is the operating system, then the system programs, and finally the application
programs. System programs, also known as system utilities, provide a convenient environment
for program development and execution. Some of them are simply user interfaces to system
calls. Others are considerably more complex. They can be divided into these categories:
• File management. These programs create, delete, copy, rename, print, dump, list, and
generally manipulate files and directories.
• Status information. Some programs simply ask the system for the date, time, amount
of available memory or disk space, number of users, or similar status information. Others are
more complex, providing detailed performance, logging, and debugging information.
Typically, these programs format and print the output to the terminal or other output devices
or files or display it in a window of the GUI. Some systems also support a registry, which is
used to store and retrieve configuration information.
• File modification. Several text editors may be available to create and modify the
content of files stored on disk or other storage devices. There may also be special commands
to search contents of files or perform transformations of the text.
• Programming-language support. Compilers, assemblers, debuggers, and interpreters
for common programming languages (such as C, C++, Java, and PERL) are often provided
with the operating system or available as a separate download.
• Program loading and execution. Once a program is assembled or compiled, it must
be loaded into memory to be executed. The system may provide absolute loaders, relocatable
loaders, linkage editors, and overlay loaders. Debugging systems for either higher-level
languages or machine language are needed as well.

32
• Communications. These programs provide the mechanism for creating virtual
connections among processes, users, and computer systems. They allow users to send messages
to one another’s screens, to browse Web pages, to send e-mail messages, to log in remotely, or
to transfer files from one machine to another.
• Background services. All general-purpose systems have methods for launching
certain system-program processes at boot time. Some of these processes terminate after
completing their tasks, while others continue to run until the system is halted. Constantly
running system-program processes are known as services, subsystems, or daemons.
Other examples include process schedulers that start processes according to a specified
schedule, system error monitoring services, and print servers. Typical systems have dozens 2.6
Operating-System Design and Implementation 75 of daemons. In addition, operating systems
that run important activities in user context rather than in kernel context may use daemons to
run these activities. Along with system programs, most operating systems are supplied with
programs that are useful in solving common problems or performing common operations. Such
application programs include Web browsers, word processors and text formatters,
spreadsheets, database systems, compilers, plotting and statistical-analysis packages, and
games.
The view of the operating system seen by most users is defined by the application and
system programs, rather than by the actual system calls. Consider a user’s PC. When a user’s
computer is running the Mac OS X operating system, the user might see the GUI, featuring a
mouse-and-windows interface. Alternatively, or even in one of the windows, the user might
have a command-line UNIX shell. Both use the same set of system calls, but the system calls
look different and act in different ways. Further confusing the user view, consider the user dual-
booting from Mac OS X into Windows. Now the same user on the same hardware has two
entirely different interfaces and two sets of applications using the same physical resources. On
the same hardware, then, a user can be exposed to multiple user interfaces sequentially or
concurrently.

System Design and Implementation.


Operating-System Design and Implementation In this section, we discuss problems we face in
designing and implementing an operating system. There are, of course, no complete solutions
to such problems, but there are approaches that have proved successful.

Design Goals
The first problem in designing a system is to define goals and specifications. At the
highest level, the design of the system will be affected by the choice of hardware and the type
of system: batch, time sharing, single user, multiuser, distributed, real time, or general purpose.
Beyond this highest design level, the requirements may be much harder to specify. The
requirements can, however, be divided into two basic groups: user goals and system goals.
Users want certain obvious properties in a system. The system should be convenient to use,
easy to learn and to use, reliable, safe, and fast. Of course, these specifications are not
particularly useful in the system design, since there is no general agreement on how to achieve

33
them. A similar set of requirements can be defined by those people who must design, create,
maintain, and operate the system. The system should be easy to design, implement, and
maintain; and it should be flexible, reliable, error free, and efficient.

Mechanisms and Policies


One important principle is the separation of policy from mechanism. Mechanisms
determine how to do something; policies determine what will be done. For example, the timer
is a mechanism for ensuring CPU protection, but deciding how long the timer is to be set for a
particular user is a policy decision. The separation of policy and mechanism is important for
flexibility. Policies are likely to change across places or over time. In the worst case, each
change in policy would require a change in the underlying mechanism.
A general mechanism insensitive to changes in policy would be more desirable. A
change in policy would then require redefinition of only certain parameters of the system. For
instance, consider a mechanism for giving priority to certain types of programs over others. If
the mechanism is properly separated from policy, it can be used either to support a policy
decision that I/O-intensive programs should have priority over CPU-intensive ones or to
support the opposite policy.
Implementation
Once an operating system is designed, it must be implemented. Because operating
systems are collections of many programs, written by many people over a long period of time,
it is difficult to make general statements about how they are implemented.
Early operating systems were written in assembly language. Now, although some
operating systems are still written in assembly language, most are written in a higher-level
language such as C or an even higher-level language such as C++. An operating system can be
written in more than one language.
The lowest levels of the kernel might be assembly language. Higher-level routines
might be in C, and system programs might be in C or C++, in interpreted scripting languages
like PERL or Python, or in shell scripts. In fact, a given Linux distribution probably includes
programs written in all of those languages.
The first system that was not written in assembly language was probably the Master
Control Program (MCP) for Burroughs computers. MCP was written in a variant of ALGOL.
MULTICS, developed at MIT, was written mainly in the system programming language PL/1.
The Linux and Windows operating system kernels are written mostly in C, although there are
some small sections of assembly code for device drivers and for saving and restoring the state
of registers.
The advantages of using a higher-level language, or at least a systems implementation
language, for implementing operating systems are the same as those gained when the language
is used for application programs: the code can be written faster, is more compact, and is easier
to understand and debug. In addition, improvements in compiler technology will improve the
generated code for the entire operating system by simple recompilation. Finally, an operating

34
system is far easier to port—to move to some other hardware— if it is written in a higher-level
language.

35
Unit:2 PROCESS MANAGEMENT
PROCESS MANAGEMENT-Concepts

A Program does nothing unless its instructions are executed by a CPU. A program in
execution is called a process. In order to accomplish its task, process needs the computer
resources.

A batch system executes jobs, whereas a time-shared system has user programs, or
tasks. Even on a single-user system, a user may be able to run several programs at one time: a
word processor, a Web browser, and an e-mail package. And even if a user can execute only
one program at a time, such as on an embedded device that does not support multitasking, the
operating system may need to support its own internal programmed activities, such as memory
management. In many respects, all these activities are similar, so we call all of them processes.
The terms job and process are used almost interchangeably in this text. Although we
personally prefer the term process, much of operating-system theory and terminology was
developed during a time when the major activity of operating systems was job processing. It
would be misleading to avoid the use of commonly accepted terms that include the word job
(such as job scheduling) simply because process has superseded job.
The Process Informally, as mentioned earlier, a process is a program in execution. A
process is more than the program code, which is sometimes known as the text section. It also
includes the current activity, as represented by the value of the program counter and the
contents of the processor’s registers. A process generally also includes the process stack, which
contains temporary data (such as function parameters, return addresses, and local variables),
and a data section, which contains global variables.
A process may also include a heap, which is memory that is dynamically allocated
during process run time. The structure of a process in memory is shown in Figure We
emphasize that a program by itself is not a process. A program is a passive entity, such as a file
containing a list of instructions stored on disk (often called an executable file). In contrast, a
process is an active entity, with a program counter specifying the next instruction to execute
and a set of associated resources. A program becomes a process when an executable file is
loaded into memory.

36
Although two processes may be associated with the same program, they are
nevertheless considered two separate execution sequences. For instance, several users may be
running different copies of the mail program, or the same user may invoke many copies of the
web browser program. Each of these is a separate process; and although the text sections are
equivalent, the data, heap, and stack sections vary. It is also common to have a process that
spawns many processes as it runs.
Process State
As a process executes, it changes state. The state of a process is defined in part by the
current activity of that process. A process may be in one of the following states:
• New. The process is being created.
• Running. Instructions are being executed.
• Waiting. The process is waiting for some event to occur (such as an I/O completion
or reception of a signal).
• Ready. The process is waiting to be assigned to a processor.
• Terminated. The process has finished execution. These names are arbitrary, and they
vary across operating systems. The states that they represent are found on all systems, however.
Certain operating systems also more finely delineate process states. It is important to realize
that only one process can be running on any processor at any instant. Many processes may be
ready and waiting, however. The state diagram corresponding to these states is presented in
Figure.

37
• Process state. The state may be new, ready, running, waiting, halted, and so on.
• Program counter. The counter indicates the address of the next instruction to be
executed for this process.
• CPU registers. The registers vary in number and type, depending on the computer
architecture. They include accumulators, index registers, stack pointers, and general-purpose
registers, plus any condition-code information. Along with the program counter, this state
information must be saved when an interrupt occurs, to allow the process to be continued
correctly afterward.
. • CPU-scheduling information. This information includes a process priority, pointers
to scheduling queues, and any other scheduling parameters.
• Memory-management information. This information may include such items as the
value of the base and limit registers and the page tables, or the segment tables, depending on
the memory system used by the operating system.

38
• Accounting information. This information includes the amount of CPU and real
time used, time limits, account numbers, job or process numbers, and so on.
• I/O status information. This information includes the list of I/O devices allocated to
the process, a list of open files, and so on.

Process Scheduling
The objective of multiprogramming is to have some process running at all times, to
maximize CPU utilization. The objective of time sharing is to switch the CPU among processes
so frequently that users can interact with each program while it is running. To meet these
objectives, the process scheduler selects an available process (possibly from a set of several
available processes) for program execution on the CPU. For a single-processor system, there
will never be more than one running process. If there are more processes, the rest will have to
wait until the CPU is free and can be rescheduled.

Scheduling Queues As processes enter the system, they are put into a job queue, which
consists of all processes in the system. The processes that are residing in main memory and are
ready and waiting to execute are kept on a list called the ready queue. This queue is generally
stored as a linked list. A ready-queue header contains pointers to the first and final PCBs in the
list. Each PCB includes a pointer field that points to the next PCB in the ready queue.

39
The system also includes other queues. When a process is allocated the CPU, it executes
for a while and eventually quits, is interrupted, or waits for the occurrence of a particular event,
such as the completion of an I/O request. Suppose the process makes an I/O request to a shared
device, such as a disk. Since there are many processes in the system, the disk may be busy with
the I/O request of some other process. The process therefore may have to wait for the disk. The
list of processes waiting for a particular I/O device is called a device queue.

A common representation of process scheduling is a queueing diagram, such as that in


Figure 3.6. Each rectangular box represents a queue. Two types of queues are present: the ready
queue and a set of device queues. The circles represent the resources that serve the queues, and
the arrows indicate the flow of processes in the system. A new process is initially put in the
ready queue. It waits there until it is selected for execution, or dispatched. Once the process is
allocated the CPU and is executing, one of several events could occur:
• The process could issue an I/O request and then be placed in an I/O queue.
• The process could create a new child process and wait for the child’s termination.
• The process could be removed forcibly from the CPU, as a result of an interrupt, and
be put back in the ready queue.
In the first two cases, the process eventually switches from the waiting state to the
ready state and is then put back in the ready queue. A process continues this cycle until it
terminates, at which time it is removed from all queues and has its PCB and resources
deallocated.
Schedulers:
A process migrates among the various scheduling queues throughout its lifetime. The
operating system must select, for scheduling purposes, processes from these queues in some

40
fashion. The selection process is carried out by the appropriate scheduler. Often, in a batch
system, more processes are submitted than can be executed immediately.
These processes are spooled to a mass-storage device (typically a disk), where they are
kept for later execution. The long-term scheduler, or job scheduler, selects processes from this
pool and loads them into memory for execution. The short-term scheduler, or CPU scheduler,
selects from among the processes that are ready to execute and allocates the CPU to one of
them.
The primary distinction between these two schedulers lies in frequency of execution.
The short-term scheduler must select a new process for the CPU frequently. A process may
execute for only a few milliseconds before waiting for an I/O request. Often, the short-term
scheduler executes at least once every 100 milliseconds. Because of the short time between
executions, the short-term scheduler must be fast. If it takes 10 milliseconds to decide to
execute a process for 100 milliseconds, then 10/(100 + 10) = 9 percent of the CPU is being
used (wasted) simply for scheduling the work. The long-term scheduler executes much less
frequently; minutes may separate the creation of one new process and the next.
The long-term scheduler controls the degree of multiprogramming (the number of
processes in memory). If the degree of multiprogramming is stable, then the average rate of
process creation must be equal to the average departure rate of processes leaving the system.
Thus, the long-term scheduler may need to be invoked only when a process leaves the system.
Because of the longer interval between executions, the long-term scheduler can afford to take
more time to decide which process should be selected for execution.
It is important that the long-term scheduler make a careful selection. In general, most
processes can be described as either I/O bound or CPU bound. An I/O-bound process is one
that spends more of its time doing I/O than it spends doing computations. A CPU-bound
process, in contrast, generates I/O requests infrequently, using more of its time doing
computations. It is important that the long-term scheduler select a good process mix of I/O-
bound and CPU-bound processes. If all processes are I/O bound, the ready queue will almost
always be empty, and the short-term scheduler will have little to do. If all processes are CPU
bound, the I/O waiting queue will almost always be empty, devices will go unused, and again
the system will be unbalanced. The system with the best performance will thus have a
combination of CPU-bound and I/O-bound processes.
On some systems, the long-term scheduler may be absent or minimal. For example,
time-sharing systems such as UNIX and Microsoft Windows systems often have no long-term
scheduler but simply put every new process in memory for the short-term scheduler. The
stability of these systems depends either on a physical limitation (such as the number of
available terminals) or on the self-adjusting nature of human users. If performance declines to
unacceptable levels on a multiuser system, some users will simply quit.
Some operating systems, such as time-sharing systems, may introduce an additional,
intermediate level of scheduling. This medium-term scheduler is diagrammed in Figure 3.7.
The key idea behind a medium-term scheduler is that sometimes it can be advantageous to
remove a process from memory (and from active contention for the CPU) and thus reduce the

41
degree of multiprogramming. Later, the process can be reintroduced into memory, and its
execution can be continued where it left off. This scheme is called swapping. The process is
swapped out, and is later swapped in, by the medium-term scheduler. Swapping may be
necessary to improve the process mix or because a change in memory requirements has
overcommitted available memory, requiring memory to be freed up.

interrupts cause the operating system to change a CPU from its current task and to run
a kernel routine. Such operations happen frequently on general-purpose systems. When an
interrupt occurs, the system needs to save the current context of the process running on the
CPU so that it can restore that context when its processing is done, essentially suspending the
process and then resuming it. The context is represented in the PCB of the process. It includes
the value of the CPU registers, the process state and memory-management information.
Generically, we perform a state save of the current state of the CPU, be it in kernel or
user mode, and then a state restore to resume operations. Switching the CPU to another process
requires performing a state save of the current process and a state restore of a different process.
This task is known as a context switch. When a context switch occurs, the kernel saves the
context of the old process in its PCB and loads the saved context of the new process scheduled
to run. Context-switch time is pure overhead, because 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 (such as a
single instruction to load or store all registers). A typical speed is a few milliseconds. Context-
switch times are highly dependent on hardware support. For instance, some processors (such
as the Sun UltraSPARC) provide multiple sets of registers. A context switch here simply
requires changing the pointer to the current register set. Of course, if there are more active
processes than there are register sets, the system resorts to copying register data to and from
memory, as before. Also, the more complex the operating system, the greater the amount of
work that must be done during a context switch.

42
Operations on Processes
There are many operations that can be performed on processes. Some of these are process
creation, process preemption, process blocking, and process termination. These are given in
detail as follows −

Process Creation

Processes need to be created in the system for different operations. This can be done by the
following events −
• User request for process creation
• System initialization
• Execution of a process creation system call by a running process
• Batch job initialization
A process may be created by another process using fork(). The creating process is called the
parent process and the created process is the child process. A child process can have only one
parent but a parent process may have many children. Both the parent and child processes have
the same memory image, open files, and environment strings. However, they have distinct
address spaces.
A diagram that demonstrates process creation using fork() is as follows −

Process Preemption

An interrupt mechanism is used in preemption that suspends the process executing currently
and the next process to execute is determined by the short-term scheduler. Preemption makes
sure that all processes get some CPU time for execution.
A diagram that demonstrates process preemption is as follows −

43
Process Blocking

The process is blocked if it is waiting for some event to occur. This event may be I/O as the
I/O events are executed in the main memory and don't require the processor. After the event
is complete, the process again goes to the ready state.
A diagram that demonstrates process blocking is as follows −

Process Termination

After the process has completed the execution of its last instruction, it is terminated. The
resources held by a process are released after it is terminated.
A child process can be terminated by its parent process if its task is no longer relevant. The
child process sends its status information to the parent process before it terminates. Also, when

44
a parent process is terminated, its child processes are terminated as well as the child processes
cannot run if the parent processes are terminated.

Cooperating Processes

What is Cooperating Process?

There are various processes in a computer system, which can be either independent or
cooperating processes that operate in the operating system. It is considered independent when
any other processes operating on the system may not impact a process. Process-independent
processes don't share any data with other processes. On the other way, a collaborating process
may be affected by any other process executing on the system. A cooperating process shares
data with another.

Advantages of Cooperating Process in Operating System

There are various advantages of cooperating process in the operating system. Some advantages
of the cooperating system are as follows:

1. Information Sharing

Cooperating processes can be used to share information between various processes. It could
involve having access to the same files. A technique is necessary so that the processes may
access the files concurrently.

2. Modularity

Modularity refers to the division of complex tasks into smaller subtasks. Different cooperating
processes can complete these smaller subtasks. As a result, the required tasks are completed
more quickly and efficiently.

3. Computation Speedup

Cooperating processes can be used to accomplish subtasks of a single task simultaneously. It


improves computation speed by allowing the task to be accomplished faster. Although, it is
only possible if the system contains several processing elements.

4. Convenience

There are multiple tasks that a user requires to perform, such as printing, compiling, editing,
etc. It is more convenient if these activities may be managed through cooperating processes.

Concurrent execution of cooperating processes needs systems that enable processes to


communicate and synchronize their actions.

45
Methods of Cooperating Process

Cooperating processes may coordinate with each other by sharing data or messages. The
methods are given below:

1. Cooperation by sharing

The processes may cooperate by sharing data, including variables, memory, databases, etc. The
critical section provides data integrity, and writing is mutually exclusive to avoid inconsistent
data.

2. Cooperation by Communication

The cooperating processes may cooperate by using messages. If every process waits for a
message from another process to execute a task, it may cause a deadlock. If a process does not
receive any messages, it may cause starvation.

46
Here, you have seen a diagram that shows cooperation by communication. In this diagram,
Process P1 and P2 may cooperate by using messages to communicate.

Example: Producer-Consumer Problem

Let's take an example of two cooperating processes. It is referred to as the Producer-Consumer


Problem, and it involves two processes: the producer and the consumer.

Producer Process

It generates information that the consumer would consume.

Consumer Process

It consumes the information that the producer produces.

Both processes run simultaneously. The customer waits if there is nothing to consume.

There is a producer and a consumer; the producer creates the item and stores it in a buffer while
the consumer consumes it. For example, print software generates characters that the printer
driver consumes. A compiler can generate assembly code, which an assembler can use. In
addition, the assembler may produce object modules that are used by the loader.

Producer Process

while(true)
{
produce an item &
while(counter = = buffer-size);
buffer[int] = next produced;

47
in = (in+1) % buffer- size;
counter ++;
}

Consumer Process
While(true)
{
while (counter = = 0);
next consumed = buffer[out];
out= (out+1) % buffer size;
counter--;
}

Where,

o The producer uses the in variable to determine the next empty slot in the buffer.
o The out variable is used by the consumer to determine where the item is located.
o The counter is used by producers and consumers to determine the number of filled slots
in the buffer.

Shared Resources

There are two shared resources:

1. Buffer
2. Counter

Inconsistency occurs when the producer and consumer are not executed on time. If both the
producer and the consumer execute concurrently without any control, the value of a counter
used by both will be incorrect. These processes share the following variables:

var n;
type item = .....;
var Buffer : array [0,n-1] of item;
In, out:0..n-1;
The variables in and out are both set to 0 by default. The shared buffer contains two logical
pointers, in and out, which are implemented as a circular array. The In variables point to the
buffer's next free position, while the Out variables point to the buffer's first full position. The
buffer is empty when in = out, and it is filled when in+1 mod n = out.

48
Inter Process Communication
Interprocess communication is the mechanism provided by the operating system that allows
processes to communicate with each other. This communication could involve a process
letting another process know that some event has occurred or the transferring of data from one
process to another.
A diagram that illustrates interprocess communication is as follows −

Synchronization in Interprocess Communication

Synchronization is a necessary part of interprocess communication. It is either provided by the


interprocess control mechanism or handled by the communicating processes. Some of the
methods to provide synchronization are as follows −
• Semaphore
A semaphore is a variable that controls the access to a common resource by
multiple processes. The two types of semaphores are binary semaphores and
counting semaphores.
• Mutual Exclusion
Mutual exclusion requires that only one process thread can enter the critical
section at a time. This is useful for synchronization and also prevents race
conditions.
• Barrier
A barrier does not allow individual processes to proceed until all the processes
reach it. Many parallel languages and collective routines impose barriers.
• Spinlock
This is a type of lock. The processes trying to acquire this lock wait in a loop
while checking if the lock is available or not. This is known as busy waiting
because the process is not doing any useful operation even though it is active.

Approaches to Interprocess Communication

The different approaches to implement interprocess communication are given as follows −


• Pipe
A pipe is a data channel that is unidirectional. Two pipes can be used to create a
two-way data channel between two processes. This uses standard input and

49
output methods. Pipes are used in all POSIX systems as well as Windows
operating systems.
• Socket
The socket is the endpoint for sending or receiving data in a network. This is
true for data sent between processes on the same computer or data sent between
different computers on the same network. Most of the operating systems use
sockets for interprocess communication.
• File
A file is a data record that may be stored on a disk or acquired on demand by a
file server. Multiple processes can access a file as required. All operating
systems use files for data storage.
• Signal
Signals are useful in interprocess communication in a limited way. They are
system messages that are sent from one process to another. Normally, signals
are not used to transfer data but are used for remote commands between
processes.
• Shared Memory
Shared memory is the memory that can be simultaneously accessed by multiple
processes. This is done so that the processes can communicate with each other.
All POSIX systems, as well as Windows operating systems use shared memory.
• Message Queue
Multiple processes can read and write data to the message queue without being
connected to each other. Messages are stored in the queue until their recipient
retrieves them. Message queues are quite useful for interprocess communication
and are used by most operating systems.
A diagram that demonstrates message queue and shared memory methods of interprocess
communication is as follows −

50
CPU Scheduling

In the uniprogrammming systems like MS DOS, when a process waits for any I/O
operation to be done, the CPU remains idol. 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.

What is saved in the Process Control Block?

The Operating system maintains a process control block during the lifetime of the process. The
Process control block is deleted when the process is terminated or killed. There is the following
information which is saved in the process control block and is changing with the state of the
process.

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 idol. The task of Operating system is to optimize the utilization of
resources.

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

Scheduling Concepts Criteria & Scheduling Algorithms


A Process Scheduler schedules different processes to be assigned to the CPU based on
particular scheduling algorithms. There are six popular process scheduling algorithms which
we are going to discuss in this chapter −
• First-Come, First-Served (FCFS) Scheduling
• Shortest-Job-Next (SJN) Scheduling
• Priority Scheduling
• Shortest Remaining Time
• Round Robin(RR) Scheduling
• Multiple-Level Queues Scheduling
These algorithms are either non-preemptive or preemptive. Non-preemptive algorithms are
designed so that once a process enters the running state, it cannot be preempted until it
completes its allotted time, whereas the preemptive scheduling is based on priority where a
scheduler may preempt a low priority running process anytime when a high priority process
enters into a ready state.

First Come First Serve (FCFS)

• Jobs are executed on first come, first serve basis.


• It is a non-preemptive, pre-emptive scheduling algorithm.
• Easy to understand and implement.
• Its implementation is based on FIFO queue.
• Poor in performance as average wait time is high.

52
Process Wait Time : Service Time - Arrival Time
Wait
P0 0-0=0

P1 5-1=4

P2 8-2=6

P3 16 - 3 = 13
time of each process is as follows −
Average Wait Time: (0+4+6+13) / 4 = 5.75

Shortest Job Next (SJN)

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


• This is a non-preemptive, pre-emptive scheduling algorithm.
• Best approach to minimize waiting time.
• Easy to implement in Batch systems where required CPU time is known in
advance.
• Impossible to implement in interactive systems where required CPU time is not
known.
• The processer should know in advance how much time process will take.
Given: Table of processes, and their Arrival time, Execution time

Process Arrival Time Execution Time Service Time

53
P0 0 5 0

P1 1 3 5

P2 2 8 14

P3 3 6 8

Waiting time of each process is as follows −

Process Waiting Time

P0 0-0=0

P1 5-1=4

P2 14 - 2 = 12

P3 8-3=5

Average Wait Time: (0 + 4 + 12 + 5)/4 = 21 / 4 = 5.25

Priority Based Scheduling

• Priority scheduling is a non-preemptive algorithm and one of the most common


scheduling algorithms in batch systems.
• Each process is assigned a priority. Process with highest priority is to be
executed first and so on.
• Processes with same priority are executed on first come first served basis.

54
• Priority can be decided based on memory requirements, time requirements or
any other resource requirement.
Given: Table of processes, and their Arrival time, Execution time, and priority. Here we are
considering 1 is the lowest priority.

Process Arrival Time Execution Priority Service Time


Time

P0 0 5 1 0

P1 1 3 2 11

P2 2 8 1 14

P3 3 6 3 5

Waiting time of each process is as follows −

Process Waiting Time

P0 0-0=0

P1 11 - 1 = 10

P2 14 - 2 = 12

P3 5-3=2

Average Wait Time: (0 + 10 + 12 + 2)/4 = 24 / 4 = 6

Shortest Remaining Time

55
• Shortest remaining time (SRT) is the preemptive version of the SJN algorithm.
• The processor is allocated to the job closest to completion but it can be
preempted by a newer ready job with shorter time to completion.
• Impossible to implement in interactive systems where required CPU time is not
known.
• It is often used in batch environments where short jobs need to give preference.

Round Robin Scheduling

• Round Robin is the preemptive process scheduling algorithm.


• Each process is provided a fix time to execute, it is called a quantum.
• Once a process is executed for a given time period, it is preempted and other
process executes for a given time period.
• Context switching is used to save states of preempted processes.

Wait time of each process is as follows −

Process Wait Time : Service Time - Arrival Time

P0 (0 - 0) + (12 - 3) = 9

P1 (3 - 1) = 2

P2 (6 - 2) + (14 - 9) + (20 - 17) = 12

P3 (9 - 3) + (17 - 12) = 11

Average Wait Time: (9+2+12+11) / 4 = 8.5

Multiple-Level Queues Scheduling

Multiple-level queues are not an independent scheduling algorithm. They make use of other
existing algorithms to group and schedule jobs with common characteristics.
• Multiple queues are maintained for processes with common characteristics.
• Each queue can have its own scheduling algorithms.
• Priorities are assigned to each queue.

56
For example, CPU-bound jobs can be scheduled in one queue and all I/O-bound jobs in another
queue. The Process Scheduler then alternately selects jobs from each queue and assigns them
to the CPU based on the algorithm assigned to the queue.

Multiprocessor Scheduling

Multiple processor scheduling or multiprocessor scheduling focuses on designing the system's


scheduling function, which consists of more than one processor. Multiple CPUs share the load
(load sharing) in multiprocessor scheduling so that various processes run simultaneously. In
general, multiprocessor scheduling is complex as compared to single processor scheduling. In
the multiprocessor scheduling, there are many processors, and they are identical, and we can
run any process at any time.

The multiple CPUs in the system are in close communication, which shares a common bus,
memory, and other peripheral devices. So we can say that the system is tightly coupled. These
systems are used when we want to process a bulk amount of data, and these systems are mainly
used in satellite, weather forecasting, etc.

There are cases when the processors are identical, i.e., homogenous, in terms of their
functionality in multiple-processor scheduling. We can use any processor available to run any
process in the queue.

Multiprocessor systems may be heterogeneous (different kinds of CPUs) or homogenous (the


same CPU). There may be special scheduling constraints, such as devices connected via a
private bus to only one CPU.

There is no policy or rule which can be declared as the best scheduling solution to a system
with a single processor. Similarly, there is no best scheduling solution for a system with
multiple processors as well.

Approaches to Multiple Processor Scheduling

There are two approaches to multiple processor scheduling in the operating system: Symmetric
Multiprocessing and Asymmetric Multiprocessing.

57
1. Symmetric Multiprocessing: It is used where each processor is self-scheduling. All
processes may be in a common ready queue, or each processor may have its private
queue for ready processes. The scheduling proceeds further by having the scheduler for
each processor examine the ready queue and select a process to execute.
2. Asymmetric Multiprocessing: It is used when all the scheduling decisions and I/O
processing are handled by a single processor called the Master Server. The other
processors execute only the user code. This is simple and reduces the need for data
sharing, and this entire scenario is called Asymmetric Multiprocessing.

Processor Affinity

Processor Affinity means a process has an affinity for the processor on which it is currently
running. When a process runs on a specific processor, there are certain effects on the cache
memory. The data most recently accessed by the process populate the cache for the processor.
As a result, successive memory access by the process is often satisfied in the cache memory.

Now, suppose the process migrates to another processor. In that case, the contents of the cache
memory must be invalidated for the first processor, and the cache for the second processor
must be repopulated. Because of the high cost of invalidating and repopulating caches, most
SMP(symmetric multiprocessing) systems try to avoid migrating processes from one processor
to another and keep a process running on the same processor. This is known as processor
affinity. There are two types of processor affinity, such as:

1. Soft Affinity: When an operating system has a policy of keeping a process running on
the same processor but not guaranteeing it will do so, this situation is called soft affinity.
2. Hard Affinity: Hard Affinity allows a process to specify a subset of processors on which
it may run. Some Linux systems implement soft affinity and provide system calls
like sched_setaffinity() that also support hard affinity.

58
Load Balancing

Load Balancing is the phenomenon that keeps the workload evenly distributed across all
processors in an SMP system. Load balancing is necessary only on systems where each
processor has its own private queue of a process that is eligible to execute.

Load balancing is unnecessary because it immediately extracts a runnable process from the
common run queue once a processor becomes idle. On SMP (symmetric multiprocessing), it is
important to keep the workload balanced among all processors to utilize the benefits of having
more than one processor fully. One or more processors will sit idle while other processors have
high workloads along with lists of processors awaiting the CPU. There are two general
approaches to load balancing:

1. Push Migration: In push migration, a task routinely checks the load on each processor.
If it finds an imbalance, it evenly distributes the load on each processor by moving the
processes from overloaded to idle or less busy processors.
2. Pull Migration:Pull Migration occurs when an idle processor pulls a waiting task from
a busy processor for its execution.

Multi-core Processors

In multi-core processors, multiple processor cores are placed on the same physical chip. Each
core has a register set to maintain its architectural state and thus appears to the operating system
as a separate physical processor. SMP systems that use multi-core processors are faster and
consume less power than systems in which each processor has its own physical chip.

However, multi-core processors may complicate the scheduling problems. When the processor
accesses memory, it spends a significant amount of time waiting for the data to become
available. This situation is called a Memory stall. It occurs for various reasons, such as cache
miss, which is accessing the data that is not in the cache memory.

59
In such cases, the processor can spend upto 50% of its time waiting for data to become available
from memory. To solve this problem, recent hardware designs have implemented
multithreaded processor cores in which two or more hardware threads are assigned to each
core. Therefore if one thread stalls while waiting for the memory, the core can switch to another
thread. There are two ways to multithread a processor:

1. Coarse-Grained Multithreading: A thread executes on a processor until a long latency


event such as a memory stall occurs in coarse-grained multithreading. Because of the
delay caused by the long latency event, the processor must switch to another thread to
begin execution. The cost of switching between threads is high as the instruction
pipeline must be terminated before the other thread can begin execution on the
processor core. Once this new thread begins execution, it begins filling the pipeline
with its instructions.
2. Fine-Grained Multithreading: This multithreading switches between threads at a much
finer level, mainly at the boundary of an instruction cycle. The architectural design of
fine-grained systems includes logic for thread switching, and as a result, the cost of
switching between threads is small.

Symmetric Multiprocessor

Symmetric Multiprocessors (SMP) is the third model. There is one copy of the OS in memory
in this model, but any central processing unit can run it. Now, when a system call is made, the
central processing unit on which the system call was made traps the kernel and processed that
system call. This model balances processes and memory dynamically. This approach uses
Symmetric Multiprocessing, where each processor is self-scheduling.

The scheduling proceeds further by having the scheduler for each processor examine the ready
queue and select a process to execute. In this system, this is possible that all the process may
be in a common ready queue or each processor may have its private queue for the ready process.
There are mainly three sources of contention that can be found in a multiprocessor operating
system.

60
o Locking system: As we know that the resources are shared in the multiprocessor
system, there is a need to protect these resources for safe access among the multiple
processors. The main purpose of the locking scheme is to serialize access of the
resources by the multiple processors.
o Shared data: When the multiple processors access the same data at the same time, then
there may be a chance of inconsistency of data, so to protect this, we have to use some
protocols or locking schemes.
o Cache coherence: It is the shared resource data that is stored in multiple local caches.
Suppose two clients have a cached copy of memory and one client change the memory
block. The other client could be left with an invalid cache without notification of the
change, so this conflict can be resolved by maintaining a coherent view of the data.

Master-Slave Multiprocessor

In this multiprocessor model, there is a single data structure that keeps track of the ready
processes. In this model, one central processing unit works as a master and another as a slave.
All the processors are handled by a single processor, which is called the master server.

The master server runs the operating system process, and the slave server runs the user
processes. The memory and input-output devices are shared among all the processors, and all
the processors are connected to a common bus. This system is simple and reduces data sharing,
so this system is called Asymmetric multiprocessing.

Virtualization and Threading

In this type of multiple processor scheduling, even a single CPU system acts as a multiple
processor system. In a system with virtualization, the virtualization presents one or more virtual
CPUs to each of the virtual machines running on the system. It then schedules the use of
physical CPUs among the virtual machines.

o Most virtualized environments have one host operating system and many guest
operating systems, and the host operating system creates and manages the virtual
machines.
o Each virtual machine has a guest operating system installed, and applications run within
that guest.

61
o Each guest operating system may be assigned for specific use cases, applications, or
users, including time-sharing or real-time operation.
o Any guest operating-system scheduling algorithm that assumes a certain amount of
progress in a given amount of time will be negatively impacted by the virtualization.
o A time-sharing operating system tries to allot 100 milliseconds to each time slice to
give users a reasonable response time. A given 100 millisecond time slice may take
much more than 100 milliseconds of virtual CPU time. Depending on how busy the
system is, the time slice may take a second or more, which results in a very poor
response time for users logged into that virtual machine.
o The net effect of such scheduling layering is that individual virtualized operating
systems receive only a portion of the available CPU cycles, even though they believe
they are receiving all cycles and scheduling all of those cycles. The time-of-day clocks
in virtual machines are often incorrect because timers take no longer to trigger than they
would on dedicated CPUs.
o Virtualizations can thus undo the good scheduling algorithm efforts of the operating
systems within virtual machines.

Real time Scheduling


Real-time systems are systems that carry real-time tasks. These tasks need to be
performed immediately with a certain degree of urgency. In particular, these tasks are related
to control of certain events (or) reacting to them. Real-time tasks can be classified as hard
real-time tasks and soft real-time tasks.
A hard real-time task must be performed at a specified time which could otherwise
lead to huge losses. In soft real-time tasks, a specified deadline can be missed. This is
because the task can be rescheduled (or) can be completed after the specified time,
In real-time systems, the scheduler is considered as the most important component
which is typically a short-term task scheduler. The main focus of this scheduler is to reduce
the response time associated with each of the associated processes instead of handling the
deadline.
If a preemptive scheduler is used, the real-time task needs to wait until its
corresponding tasks time slice completes. In the case of a non-preemptive scheduler, even if
the highest priority is allocated to the task, it needs to wait until the completion of the current
task. This task can be slow (or) of the lower priority and can lead to a longer wait.
A better approach is designed by combining both preemptive and non-preemptive
scheduling. This can be done by introducing time-based interrupts in priority-based systems
which means the currently running process is interrupted on a time-based interval and if a
higher priority process is present in a ready queue, it is executed by preempting the current
process.

62
Based on schedulability, implementation (static or dynamic), and the result (self or
dependent) of analysis, the scheduling algorithm are classified as follows.

Static table-driven approaches:


These algorithms usually perform a static analysis associated with scheduling and capture the
schedules that are advantageous. This helps in providing a schedule that can point out a task
with which the execution must be started at run time.

Static priority-driven preemptive approaches:


Similar to the first approach, this type of algorithms also uses static analysis of scheduling.
The difference is that instead of selecting a particular schedule, it provides a useful way of
assigning priorities among various tasks in preemptive scheduling.

Dynamic planning-based approaches:


Here, the feasible schedules are identified dynamically (at run time). It carries a certain fixed
time interval and a process is executed if and only if satisfies the time constraint.

Dynamic best effort approaches:


These types of approaches consider deadlines instead of feasible schedules. Therefore the
task is aborted if its deadline is reached. This approach is used widely is most of the real-time
systems.

63
Unit:3 PROCESS SYNCHRONIZATION
PROCESS SYNCHRONIZATION: Critical Section

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

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

The procedure involved in preserving the appropriate order of execution of cooperative


processes is known as Process Synchronization. There are various synchronization
mechanisms that are used to synchronize the processes.

Race Condition

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

Critical Section

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

The Critical Section Problem

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

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

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

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

Requirements of Synchronization mechanisms

64
Primary
1. Mutual Exclusion

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

65
2. Progress

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

Secondary
1. Bounded Waiting

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

2. Architectural Neutrality

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

Synchronization Hardware Semaphores Problems of Synchronization


We have just described one software-based solution to the critical-section problem.
However, as mentioned, software-based solutions such as Peterson’s are not guaranteed to
work on modern computer architectures. In the following discussions, we explore several more
solutions to the critical-section problem using techniques ranging from hardware to software-
based APIs available to both kernel developers and application programmers. All these
solutions are based on the premise of locking —that is, protecting critical regions through the
use of locks. As we shall see, the designs of such locks can be quite sophisticated. We start by
presenting some simple hardware instructions that are available on many systems and showing
how they can be used effectively in solving the critical-section problem.
Hardware features can make any programming task easier and improve system
efficiency. The critical-section problem could be solved simply in a single-processor
environment if we could prevent interrupts from occurring while a shared variable was being
modified. In this way, we could be sure that the current sequence of instructions would be
allowed to execute in order without preemption. No other instructions would be run, so no
unexpected modifications could be made to the shared variable.
Unfortunately, this solution is not as feasible in a multiprocessor environment.
Disabling interrupts on a multiprocessor can be time consuming, since the message is passed
to all the processors. This message passing delays entry into each critical section, and system
efficiency decreases. Also consider the effect on a system’s clock if the clock is kept updated
by interrupts. Many modern computer systems therefore provide special hardware instructions
that allow us either to test and modify the content of a word or to swap the contents of two
words atomically—that is, as one uninterruptible unit. We can use these special instructions to
solve the critical-section problem in a relatively simple manner.

66
Classical problems of Synchronization with Semaphore Solution
In this article, we will see number of classical problems of synchronization as examples of a
large class of concurrency-control problems. In our solutions to the problems, we
use semaphores for synchronization, since that is the traditional way to present such
solutions. However, actual implementations of these solutions could use mutex locks in place
of binary semaphores.
These problems are used for testing nearly every newly proposed synchronization scheme.
The following problems of synchronization are considered as classical problems:
1. Bounded-buffer (or Producer-Consumer) Problem,
2. Dining-Philosophers Problem,
3. Readers and Writers Problem,
4. Sleeping Barber Problem
These are summarized, for detailed explanation, you can view the linked articles for each.
Bounded-buffer (or Producer-Consumer) Problem:
Bounded Buffer problem is also called producer consumer problem. This problem is
generalized in terms of the Producer-Consumer problem. Solution to this problem is, creating
two counting semaphores “full” and “empty” to keep track of the current number of full and
empty buffers respectively. Producers produce a product and consumers consume the
product, but both use of one of the containers each time.

Dining-Philosophers Problem:
The Dining Philosopher Problem states that K philosophers seated around a circular table
with one chopstick between each pair of philosophers. There is one chopstick between each
philosopher. A philosopher may eat if he can pickup the two chopsticks adjacent to him. One
chopstick may be picked up by any one of its adjacent followers but not both. This problem
involves the allocation of limited resources to a group of processes in a deadlock-free and
starvation-free manner.

Readers and Writers Problem:

67
Suppose that a database is to be shared among several concurrent processes. Some of these
processes may want only to read the database, whereas others may want to update (that is, to
read and write) the database. We distinguish between these two types of processes by
referring to the former as readers and to the latter as writers. Precisely in OS we call this
situation as the readers-writers problem. Problem parameters:
One set of data is shared among a number of processes.

Once a writer is ready, it performs its write. Only one writer may write at a time.

If a process is writing, no other process can read it.

If at least one reader is reading, no other process can write.

Readers may not write and only read.

Sleeping Barber Problem:


Barber shop with one barber, one barber chair and N chairs to wait in. When no customers
the barber goes to sleep in barber chair and must be woken when a customer comes in. When
barber is cutting hair new customers take empty seats to wait, or leave if no vacancy.

Critical Regions
When more than one processes access the same code segment that segment is known
as the critical section. The critical section contains shared variables or resources which are
needed to be synchronized to maintain the consistency of data variables. In simple terms, a
critical section is a group of instructions/statements or region of code that need to be executed
atomically (read this post for atomicity), such as accessing a resource (file, input or output port,
global data, etc.). In concurrent programming, if one thread tries to change the value of shared

68
data at the same time as another thread tries to read the value (i.e. data race across threads), the
result is unpredictable.
The access to such shared variables (shared memory, shared files, shared port, etc…)
is to be synchronized. Few programming languages have built-in support for synchronization.
It is critical to understand the importance of race conditions while writing kernel-mode
programming (a device driver, kernel thread, etc.). since the programmer can directly access
and modify kernel data structures.

It could be visualized using the pseudo-code below:-


do{
flag=1;
while(flag); // (entry section)
// critical section
if (!flag)
// remainder section
} while(true);
A simple solution to the critical section can be thought of as shown below,
acquireLock();
Process Critical Section
releaseLock();
A thread must acquire a lock prior to executing a critical section. The lock can be acquired by
only one thread. There are various ways to implement locks in the above pseudo-code. Let us
discuss them in future articles. Please write comments if you find anything incorrect, or you
want to share more information about the topic discussed above.

69
Monitors
Monitors are used for process synchronization. With the help of programming languages, we
can use a monitor to achieve mutual exclusion among the processes. Example of
monitors: Java Synchronized methods such as Java offers notify() and wait() constructs.
In other words, monitors are defined as the construct of programming language, which helps
in controlling shared data access.
The Monitor is a module or package which encapsulates shared data structure, procedures,
and the synchronization between the concurrent procedure invocations.
Characteristics of Monitors.

1. Inside the monitors, we can only execute one process at a time.


2. Monitors are the group of procedures, and condition variables that are merged
together in a special type of module.

3. If the process is running outside the monitor, then it cannot access the monitor’s internal
variable. But a process can call the procedures of the monitor.
4. Monitors offer high-level of synchronization
Monitors were derived to simplify the complexity of synchronization problems.
6. There is only one process that can be active at a time inside the monitor.
Components of Monitor
There are four main components of the monitor:

1. Initialization
2. Private data
3. Monitor procedure
4. Monitor entry queue

Initialization: - Initialization comprises the code, and when the monitors are created, we use
this code exactly once.
Private Data: - Private data is another component of the monitor. It comprises all the private
data, and the private data contains private procedures that can only be used within the
monitor. So, outside the monitor, private data is not visible.
Monitor Procedure: - Monitors Procedures are those procedures that can be called from
outside the monitor.
Monitor Entry Queue: - Monitor entry queue is another essential component of the monitor
that includes all the threads, which are called procedures.
Syntax of monitor

70
Condition Variables

There are two types of operations that we can perform on the condition variables of the
monitor:

1. Wait
2. Signal

Wait Operation
a.wait(): - The process that performs wait operation on the condition variables are
suspended and locate the suspended process in a block queue of that condition variable.
Signal Operation
a.signal() : - If a signal operation is performed by the process on the condition variable,
then a chance is provided to one of the blocked processes.
Advantages of Monitor
It makes the parallel programming easy, and if monitors are used, then there is less error-
prone as compared to the semaphore.
Difference between Monitors and Semaphore

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

1. The process requests for some resource.


2. OS grant the resource if it is available otherwise let the process waits.
3. The process uses it and release on the completion.

Monitors Semaphore

In semaphore, we can use condition


We can use condition variables only in variables anywhere in the program, but
the monitors. we cannot use conditions variables in a
semaphore.

In monitors, wait always block the In semaphore, wait does not always
caller. block the caller.

The monitors are comprised of the The semaphore S value means the
shared variables and the procedures number of shared resources that are
which operate the shared variable. present in the system.

Condition variables are present in the Condition variables are not present in
monitor. the semaphore.

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

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

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

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

Difference between Starvation and Deadlock

Necessary conditions for Deadlocks

1. Mutual Exclusion

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

2. Hold and Wait

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

3. No preemption

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

4. Circular Wait

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

Handling Deadlocks
1. Deadlock Ignorance

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.

There is always a tradeoff between Correctness and performance. The operating


systems like Windows and Linux mainly focus upon performance. However, the performance
of the system decreases if it uses deadlock handling mechanism all the time if 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 in the case of
deadlock. Windows and Linux are mainly using this approach.

2. Deadlock prevention

Deadlock happens only when Mutual Exclusion, hold and wait, No preemption and
circular wait holds simultaneously. If it is possible to violate one of the four conditions at any
time then the deadlock can never occur in the system.

The idea behind the approach is very simple that we have to fail one of the four
conditions but there can be a big argument on its physical implementation in the system.

74
3. Deadlock avoidance

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. The process continues
until the system is in safe state. Once the system moves to unsafe state, the OS has to backtrack
one step.

4. Deadlock detection and recovery

This approach let the processes fall in deadlock and then periodically check whether
deadlock occur in the system or not. If it occurs then it applies some of the recovery methods
to the system to get rid of deadlock.

Deadlock Prevention
If we simulate deadlock with a table which is standing on its four legs then we can also simulate
four legs with the four conditions which when occurs simultaneously, cause the deadlock.

However, if we break one of the legs of the table then the table will fall definitely. The same
happens with deadlock, if we can be able to violate one of the four necessary conditions and
don't let them occur together then we can prevent the deadlock.

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.

75
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 lies when a process holds a resource and waiting for some other
resource to complete its task. Deadlock occurs because there can be more than one process
which are holding one resource and waiting for other in the cyclic order.

However, we have to find out some mechanism by which a process either doesn't hold any
resource or doesn't wait. That means, a process must be assigned all the necessary resources
before the execution starts. A process must not wait for any resource once the execution has
been started.

!(Hold and wait) = !hold or !wait (negation of hold and wait is, either you don't hold or
you don't wait)

This can be implemented practically if a process declares all the resources initially. However,
this sounds very practical but can't be done in the computer system because a process can't
determine necessary resources initially.

76
Process is the set of instructions which are executed by the CPU. Each of the instruction may
demand multiple resources at the multiple times. The need cannot be fixed by the OS.

The problem with the approach is:

1. Practically not possible.


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

3. No Preemption

Deadlock arises due to the fact that a process can't be stopped once it starts. However, if we
take the resource away from the process which is causing deadlock then we can prevent
deadlock.

This is not a good approach at all since if we take a resource away which is being used by the
process then all the work which it has done till now can become inconsistent.

Consider a printer is being used by any process. If we take the printer away from that process
and assign it to some other process then all the data which has been printed can become
inconsistent and ineffective and also the fact that the process can't start printing again from
where it has left which causes performance inefficiency.

4. Circular Wait

To violate circular wait, we can assign a priority number to each of the resource. A process
can't request for a lesser priority resource. This ensures that not a single process can request a
resource which is being utilized by some other process and no cycle will be formed.

77
Among all the methods, violating Circular wait is the only approach that can be implemented
practically.

Deadlock avoidance
In deadlock avoidance, the request for any resource will be granted if the resulting state of the
system doesn't cause deadlock in the system. The state of the system will continuously be
checked for safe and unsafe states.

In order to avoid deadlocks, the process must tell OS, the maximum number of resources a
process can request to complete its execution.

The simplest and most useful approach states that the process should declare the maximum
number of resources of each type it may ever need. The Deadlock avoidance algorithm
examines the resource allocations so that there can never be a circular wait condition.

The resource allocation state of a system can be defined by the instances of available and
allocated resources, and the maximum instance of the resources demanded by the processes.

A state of a system recorded at some random time is shown below.

Resources Assigned

Process Type 1 Type 2 Type 3 Type


4

A 3 0 2 2

B 0 0 1 1

C 1 1 1 0

D 2 1 4 0

Resources still needed

Process Type 1 Type 2 Type 3 Type 4

78
A 1 1 0 0

B 0 1 1 2

C 1 2 1 0

D 2 1 1 2

1. E = (7 6 8 4)
2. P = (6 2 8 3)
3. A = (1 4 0 1)

Above tables and vector E, P and A describes the resource allocation state of a system. There
are 4 processes and 4 types of the resources in a system. Table 1 shows the instances of each
resource assigned to each process.

Table 2 shows the instances of the resources, each process still needs. Vector E is the
representation of total instances of each resource in the system.

Vector P represents the instances of resources that have been assigned to processes. Vector A
represents the number of resources that are not in use.

A state of the system is called safe if the system can allocate all the resources requested by all
the processes without entering into deadlock.

If the system cannot fulfill the request of all processes then the state of the system is called
unsafe.

The key of Deadlock avoidance approach is when the request is made for resources then the
request must only be approved in the case if the resulting state is also a safe state.

Deadlock Detection and Recovery


In this approach, The OS doesn't apply any mechanism to avoid or prevent the deadlocks.
Therefore the system considers that the deadlock will definitely occur. In order to get rid of
deadlocks, The OS periodically checks the system for any deadlock. In case, it finds any of the
deadlock then the OS will recover the system using some recovery techniques.

79
The main task of the OS is detecting the deadlocks. The OS can detect the deadlocks with the
help of Resource allocation graph.

In single instanced resource types, if a cycle is being formed in the system then there will
definitely be a deadlock. On the other hand, in multiple instanced resource type graph, detecting
a cycle is not just enough. We have to apply the safety algorithm on the system by converting
the resource allocation graph into the allocation matrix and request matrix.

In order to recover the system from deadlocks, either OS considers resources or processes.

For Resource
Preempt the resource

We can snatch one of the resources from the owner of the resource (process) and give it to the
other process with the expectation that it will complete the execution and will release this
resource sooner. Well, choosing a resource which will be snatched is going to be a bit difficult.

Rollback to a safe state

System passes through various states to get into the deadlock state. The operating system
canrollback the system to the previous safe state. For this purpose, OS needs to implement
check pointing at every state.

The moment, we get into deadlock, we will rollback all the allocations to get into the previous
safe state.

For Process

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

81
Unit:4 MEMORY MANAGEMENT
MEMORY MANAGEMENT: Storage Hierarchy-Storage Management Strategies Contiguous
- Non-Contiguous Storage Allocation-Single User-Fixed Partition-Variable Partition
Swapping Virtual Memory-Basic Concepts-Multilevel Organization-Block Mapping-Paging
Segmentation-Page Replacement Methods-Locality-Working Sets.

MEMORY MANAGEMENT: Storage Hierarchy


Memory management is the functionality of an operating system which handles or manages
primary memory and moves processes back and forth between main memory and disk during execution.
Memory management keeps track of each and every memory location, regardless of either it is allocated
to some process or it is free. It checks how much memory is to be allocated to processes. It decides
which process will get memory at what time. It tracks whenever some memory gets freed or unallocated
and correspondingly it updates the status.

The Computer memory hierarchy looks like a pyramid structure which is used to describe the
differences among memory types. It separates the computer storage based on hierarchy.
Level 0: CPU registers
Level 1: Cache memory
Level 2: Main memory or primary memory
Level 3: Magnetic disks or secondary memory
Level 4: Optical disks or magnetic types or tertiary Memory

82
In Memory Hierarchy the cost of memory, capacity is inversely proportional to speed. Here
the devices are arranged in a manner Fast to slow, that is form register to Tertiary memory.
Let us discuss each level in detail:
Level-0 − Registers
The registers are present inside the CPU. As they are present inside the CPU, they have least
access time. Registers are most expensive and smallest in size generally in kilobytes. They are
implemented by using Flip-Flops.
Level-1 − Cache
Cache memory is used to store the segments of a program that are frequently accessed by the
processor. It is expensive and smaller in size generally in Megabytes and is implemented by
using static RAM.
Level-2 − Primary or Main Memory
It directly communicates with the CPU and with auxiliary memory devices through an I/O
processor. Main memory is less expensive than cache memory and larger in size generally in
Gigabytes. This memory is implemented by using dynamic RAM.
Level-3 − Secondary storage
Secondary storage devices like Magnetic Disk are present at level 3. They are used as backup
storage. They are cheaper than main memory and larger in size generally in a few TB.
Level-4 − Tertiary storage
Tertiary storage devices like magnetic tape are present at level 4. They are used to store
removable files and are the cheapest and largest in size (1-20 TB).

Level Register Cache Primary Secondary


memory memory
Bandwidth 4k to 32k 800 to 5k 400 to 2k 4 to 32 MB/sec
MB/sec MB/sec MB/sec
Size Less than 1KB Less than 4MB Less than 2 GB Greater than 2
GB
Access time 2 to 5nsec 3 to 10 nsec 80 to 400 nsec 5ms
Managed by Compiler Hardware Operating OS or user
system

Storage Management Strategies

83
Contiguous and Non-Contiguous Memory Allocation in Operating System

Memory is a huge collection of bytes, and memory allocation refers to allocating space to
computer applications. There are mainly two types of memory allocation: contiguous and non-
contiguous memory allocation. Contiguous memory allocation allows a single memory space
to complete the tasks. On the other hand, non-contiguous memory allocation assigns the
method to distinct memory sections at numerous memory locations.

In this article, you will learn about contiguous and non-contiguous memory allocation with
their advantages, disadvantages, and differences.

What is Contiguous Memory Allocation?

It is the type of memory allocation method. When a process requests the memory, a single
contiguous section of memory blocks is allotted depending on its requirements.

It is completed by partitioning the memory into fixed-sized partitions and assigning every
partition to a single process. However, it will limit the degree of multiprogramming to the
number of fixed partitions done in memory.

This allocation also leads to internal fragmentation. For example, suppose a fixed-sized
memory block assigned to a process is slightly bigger than its demand. In that case, the
remaining memory space in the block is referred to as internal fragmentation. When a process
in a partition finishes, the partition becomes accessible for another process to run.

The OS preserves a table showing which memory partitions are free and occupied by processes
in the variable partitioning scheme. Contiguous memory allocation speeds up process
execution by decreasing address translation overheads.

Advantages and Disadvantages of Contiguous Memory Allocation

There are various advantages and disadvantages of contiguous memory allocation. Some of the
advantages and disadvantages are as follows:

Advantages

1. It is simple to keep track of how many memory blocks are left, which determines how many
more processes can be granted memory space.
2. The read performance of contiguous memory allocation is good because the complete file may
be read from the disk in a single task.
3. The contiguous allocation is simple to set up and performs well.

84
Disadvantages

1. Fragmentation isn't a problem because every new file may be written to the end of the disk after
the previous one.
2. When generating a new file, it must know its eventual size to select the appropriate hole size.
3. When the disk is filled up, it would be necessary to compress or reuse the spare space in the
holes.

What is Non-Contiguous Memory Allocation?

It allows a process to obtain multiple memory blocks in various locations in memory


based on its requirements. The non-contiguous memory allocation also reduces memory
wastage caused by internal and external fragmentation because it uses the memory holes
created by internal and external fragmentation.

The two methods for making a process's physical address space non-contiguous are paging and
segmentation. Non-contiguous memory allocation divides the process into blocks (pages or
segments) that are allocated to different areas of memory space based on memory availability.

Non-contiguous memory allocation can decrease memory wastage, but it also raises address
translation overheads. As the process portions are stored in separate locations in memory, the
memory execution is slowed because time is consumed in address translation.

Advantages and Disadvantages of Non-Contiguous Memory


Allocation
There are various advantages and disadvantages of non-contiguous memory allocation. Some
of the advantages and disadvantages are as follows:

Advantages

1. It has the advantage of reducing memory waste, but it increases overhead because of the address
translation.
2. It slows down the memory execution because time is consumed in address translation.

Disadvantages

1. The downside of this memory allocation is that the access is slow because you must reach the
other nodes using pointers and traverse them.

Key differences between the Contiguous and Non-Contiguous Memory Allocation

Here, you will learn the various key differences between Contiguous and Non-Contiguous
Memory Allocation. Some of the key differences between Contiguous and Non-Contiguous
Memory Allocation are as follows:

85
1. The contiguous memory allocation assigns only one memory contiguous block to the
process. In contrast, non-contiguous allocation breaks the process into many blocks and
stores them in distinct memory address regions.
2. The operating system must preserve a table in contiguous memory allocation that shows
which partitions are available and utilized by the process. In contrast, in non-contiguous
memory allocation, a table is preserved for each process containing the base address of
each process block placed in memory space.
3. There is no overhead of address translation during execution because the process is
stored in contiguous memory space in contiguous memory allocation. In contrast, in
non-contiguous memory allocation, there is an overhead of address translation during
process execution since the process blocks are spread across the memory space.
4. The Operating System can better control contiguous memory allocation. On the other
hand, the Non-Contiguous Memory Allocation is difficult for the Operating System to
manage.
5. In Contiguous Memory Allocation, the process runs faster as the entire process is in a
sequential block. In contrast, the non-contiguous memory allocation execution of the
process is slowed down because the process is in multiple positions in the memory.
6. Contiguous memory allocation contains two memory allocations: single partition and
multi-partition. On the other hand, the Non-Contiguous memory allocation contains
Paging and Segmentation.
7. Both internal and external fragmentation occurs in the contiguous memory allocation.
In contrast, non-contiguous memory allocation causes external fragmentation.
8. Swapped-in processes are placed in the initially allotted space in the contiguous
memory allocation. In contrast, swapped-in processes in non-contiguous memory
allocation can be organized in any location in memory.

Single User-Fixed Partition

The earliest and one of the simplest technique which can be used to load more than one
processes into the main memory is Fixed partitioning or Contiguous memory allocation.

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

In fixed partitioning,

1. The partitions cannot overlap.


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

There are various cons of using this technique.

86
1. Internal Fragmentation

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

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

2. External Fragmentation

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

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

3. Limitation on the size of the process

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

4. Degree of multiprogramming is less

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

87
Variable Partition or Dynamic Partition

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

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

88
Advantages of Dynamic Partitioning over fixed partitioning

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

2. No Limitation on the size of the process


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

7 to find Nth Highest Salary in SQL

3. Degree of multiprogramming is dynamic


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

Disadvantages of dynamic partitioning

89
External Fragmentation

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

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

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

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

Complex Memory Allocation

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

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

90
Swapping in Operating System

Swapping is a memory management scheme in which any process can be temporarily swapped
from main memory to secondary memory so that the main memory can be made available for
other processes. It is used to improve main memory utilization. In secondary memory, the place
where the swapped-out process is stored is called swap space.

The purpose of the swapping in operating system is to access the data present in the hard disk
and bring it to RAM so that the application programs can use it. The thing to remember is that
swapping is used only when data is not present in RAM

Although the process of swapping affects the performance of the system, it helps to run larger
and more than one process. This is the reason why swapping is also referred to as memory
compaction.

The concept of swapping has divided into two more concepts: Swap-in and Swap-out.

o Swap-out is a method of removing a process from RAM and adding it to the hard disk.
o Swap-in is a method of removing a program from a hard disk and putting it back into
the main memory or RAM.

Advantages of Swapping

1. It helps the CPU to manage multiple processes within a single main memory.
2. It helps to create and use virtual memory.
3. Swapping allows the CPU to perform multiple tasks simultaneously. Therefore,
processes do not have to wait very long before they are executed.
4. It improves the main memory utilization.

Disadvantages of Swapping

1. If the computer system loses power, the user may lose all information related to the
program in case of substantial swapping activity.
2. If the swapping algorithm is not good, the composite method can increase the number
of Page Fault and decrease the overall processing performance.

Note:

o In a single tasking operating system, only one process occupies the user program area
of memory and stays in memory until the process is complete.

91
o In a multitasking operating system, a situation arises when all the active processes
cannot coordinate in the main memory, then a process is swap out from the main
memory so that other processes can enter it.

Virtual Memory-Basic Concepts

A computer can address more memory than the amount physically installed on the
system. This extra memory is actually called virtual memory and it is a section of a hard disk
that's set up to emulate the computer's RAM.
The main visible advantage of this scheme is that programs can be larger than physical
memory. Virtual memory serves two purposes. First, it allows us to extend the use of physical
memory by using disk. Second, it allows us to have memory protection, because each virtual
address is translated to a physical address.
Following are the situations, when entire program is not required to be loaded fully in main
memory.
• User written error handling routines are used only when an error occurred in the
data or computation.
• Certain options and features of a program may be used rarely.
• Many tables are assigned a fixed amount of address space even though only a
small amount of the table is actually used.
• The ability to execute a program that is only partially in memory would counter
many benefits.
• Less number of I/O would be needed to load or swap each user program into
memory.
• A program would no longer be constrained by the amount of physical memory
that is available.
• Each user program could take less physical memory, more programs could be
run the same time, with a corresponding increase in CPU utilization and
throughput.
Modern microprocessors intended for general-purpose use, a memory management unit, or
MMU, is built into the hardware. The MMU's job is to translate virtual addresses into physical
addresses. A basic example is given below −

92
Virtual memory is commonly implemented by demand paging. It can also be implemented in
a segmentation system. Demand segmentation can also be used to provide virtual memory.

Demand Paging

A demand paging system is quite similar to a paging system with swapping where processes
reside in secondary memory and pages are loaded only on demand, not in advance. When a
context switch occurs, the operating system does not copy any of the old program’s pages out
to the disk or any of the new program’s pages into the main memory Instead, it just begins
executing the new program after loading the first page and fetches that program’s pages as they
are referenced.

93
While executing a program, if the program references a page which is not available in the main
memory because it was swapped out a little ago, the processor treats this invalid memory
reference as a page fault and transfers control from the program to the operating system to
demand the page back into the memory.

Advantages
Following are the advantages of Demand Paging −
• Large virtual memory.
• More efficient use of memory.
• There is no limit on degree of multiprogramming.
Disadvantages

• Number of tables and the amount of processor overhead for handling page
interrupts are greater than in the case of the simple paged management
techniques.

94
Paging Segmentation

Pure segmentation is not very popular and not being used in many of the operating systems.
However, Segmentation can be combined with Paging to get the best features out of both the
techniques.

In Segmented Paging, the main memory is divided into variable size segments which are further
divided into fixed size pages.

1. Pages are smaller than segments.


2. Each Segment has a page table which means every program has multiple page tables.
3. The logical address is represented as Segment Number (base address), Page number
and page offset.

Segment Number → It points to the appropriate Segment Number.

Page Number → It Points to the exact page within the segment

Page Offset → Used as an offset within the page frame

Each Page table contains the various information about every page of the segment. The
Segment Table contains the information about every segment. Each segment table entry points
to a page table entry and every page table entry is mapped to one of the page within a segment.

95
Translation of logical address to physical address

The CPU generates a logical address which is divided into two parts: Segment Number and
Segment Offset. The Segment Offset must be less than the segment limit. Offset is further
divided into Page number and Page Offset. To map the exact page number in the page table,
the page number is added into the page table base.

The actual frame number with the page offset is mapped to the main memory to get the desired
word in the page of the certain segment of the process.

96
Advantages of Segmented Paging

1. It reduces memory usage.


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

Disadvantages of Segmented Paging

1. Internal Fragmentation will be there.


2. The complexity level will be much higher as compare to paging.
3. Page Tables need to be contiguously stored in the memory.

97
Page Replacement Methods/Page Replacement Algorithm
Page replacement algorithms are the techniques using which an Operating System
decides which memory pages to swap out, write to disk when a page of memory needs to be
allocated. Paging happens whenever a page fault occurs and a free page cannot be used for
allocation purpose accounting to reason that pages are not available or the number of free pages
is lower than required pages.
When the page that was selected for replacement and was paged out, is referenced
again, it has to read in from disk, and this requires for I/O completion. This process determines
the quality of the page replacement algorithm: the lesser the time waiting for page-ins, the
better is the algorithm.
A page replacement algorithm looks at the limited information about accessing the
pages provided by hardware, and tries to select which pages should be replaced to minimize
the total number of page misses, while balancing it with the costs of primary storage and
processor time of the algorithm itself. There are many different page replacement algorithms.
We evaluate an algorithm by running it on a particular string of memory reference and
computing the number of page faults,

Reference String

The string of memory references is called reference string. Reference strings are
generated artificially or by tracing a given system and recording the address of each memory
reference. The latter choice produces a large number of data, where we note two things.
• For a given page size, we need to consider only the page number, not the entire
address.
• If we have a reference to a page p, then any immediately following references
to page p will never cause a page fault. Page p will be in memory after the first
reference; the immediately following references will not fault.
• For example, consider the following sequence of addresses −
123,215,600,1234,76,96
• If page size is 100, then the reference string is 1,2,6,12,0,0

First In First Out (FIFO) algorithm

• Oldest page in main memory is the one which will be selected for replacement.
• Easy to implement, keep a list, replace pages from the tail and add new pages at
the head.

98
Optimal Page algorithm

• An optimal page-replacement algorithm has the lowest page-fault rate of all


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

Least Recently Used (LRU) algorithm

99
• Page which has not been used for the longest time in main memory is the one
which will be selected for replacement.
• Easy to implement, keep a list, replace pages by looking back into time.

Page Buffering algorithm

• To get a process start quickly, keep a pool of free frames.


• On page fault, select a page to be replaced.
• Write the new page in the frame of free pool, mark the page table and restart
the process.
• Now write the dirty page out of disk and place the frame holding replaced page
in free pool.

Least frequently Used(LFU) algorithm

• The page with the smallest count is the one which will be selected for
replacement.
• This algorithm suffers from the situation in which a page is used heavily during
the initial phase of a process, but then is never used again.

Most frequently Used(MFU) algorithm

• This algorithm is based on the argument that the page with the smallest count
was probably just brought in and has yet to be used.

100
Locality
Locality of reference refers to the tendency of the computer program to access the same set of
memory locations for a particular time period. The property of Locality of Reference is mainly
shown by loops and subroutine calls in a program.
On an abstract level there are two types of localities which are as follows −
• Temporal locality
• Spatial locality
Temporal locality

This type of optimization includes bringing in the frequently accessed memory references to
a nearby memory location for a short duration of time so that the future accesses are much
faster.
For example, if in an instruction set we have a variable declared that is being accessed very
frequently we bring in that variable in a memory register which is the nearest in memory
hierarchy for faster access.

Spatial locality

This type of optimization assumes that if a memory location has been accessed it is highly
likely that a nearby/consecutive memory location will be accessed as well and hence we bring
in the nearby memory references too in a nearby memory location for faster access.
For example, traversal of a one-dimensional array in any instruction set will benefit from this
optimization.
Using these optimizations we can greatly improve upon the efficiency of the programs and
can be implemented on hardware level or on software level.
Let us see the locality of reference relationship with cache memory and hit ratio.

Relationship with Cache memory

Cache is a specially designed faster but smaller memory area, generally used to keep recently
referenced data and data near recently referenced data, which can lead to potential performance
increases.
Data in cache does not necessarily correspond to data that is spatially close in main memory.
However, data elements are brought into cache one cache line at a time. This means that spatial
locality is again important. If one element is referenced, a few neighbour elements will also
be brought into cache.

101
Finally, temporal locality plays a role on the lowest level, since results that are referenced very
closely together can be kept in the machine registers. Programming languages such as C allow
the programmer to suggest that certain variables are kept in registers.

Relationship with Hit ratio

Hit ratio is the concept defined for any two adjacent levels of a memory hierarchy. It is the
probability that an information item will be found in the memory we are looking at.
So locality of reference needs a good Hit ratio for achieving Fast Access time.

102
Unit:5 I/O AND FILE SYSTEMS

What is Disk Scheduling Algorithm?


A Process makes the I/O requests to the operating system to access the disk. Disk
Scheduling Algorithm manages those requests and decides the order of the disk
access given to the requests.

Why Disk Scheduling Algorithm is needed?


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

Important Terms related to Disk Scheduling Algorithms


Seek Time - It is the time taken by the disk arm to locate the desired track.
Rotational Latency - The time taken by a desired sector of the disk to rotate itself
to the position where it can access the Read/Write heads is called Rotational
Latency.
Transfer Time - It is the time taken to transfer the data requested by the
processes.
Disk Access Time - Disk Access time is the sum of the Seek Time, Rotational
Latency, and Transfer Time.
Disk Scheduling Algorithms
First Come First Serve (FCFS)
In this algorithm, the requests are served in the order they come. Those who come
first are served first. This is the simplest algorithm.

Eg. Suppose the order of requests are 70, 140, 50, 125, 30, 25, 160 and the initial
position of the Read-Write head is 60.

103
Seek Time = Distance Moved by the disk arm = (140-70)+(140-50)+(125-
50)+(125-30)+(30-25)+(160-25)=480

Shortest Seek Time First (SSTF)


In this algorithm, the shortest seek time is checked from the current position and
those requests which have the shortest seek time is served first. In simple words,
the closest request from the disk arm is served first.

Eg. Suppose the order of requests are 70, 140, 50, 125, 30, 25, 160 and the initial
position of the Read-Write head is 60.

Seek Time = Distance Moved by the disk arm = (60-50)+(50-30)+(30-25)+(70-


25)+(125-70)+(140-125)+(160-125)=270

104
SCAN
In this algorithm, the disk arm moves in a particular direction till the end and
serves all the requests in its path, then it returns to the opposite direction and
moves till the last request is found in that direction and serves all of them.

Eg. Suppose the order of requests are 70, 140, 50, 125, 30, 25, 160 and the initial
position of the Read-Write head is 60. And it is given that the disk arm should
move towards the larger value.

Seek Time = Distance Moved by the disk arm = (170-60)+(170-25)=255

LOOK
In this algorithm, the disk arm moves in a particular direction till the last request
is found in that direction and serves all of them found in the path, and then
reverses its direction and serves the requests found in the path again up to the last
request found. The only difference between SCAN and LOOK is, it doesn't go to
the end it only moves up to which the request is found.

Eg. Suppose the order of requests are 70, 140, 50, 125, 30, 25, 160 and the initial
position of the Read-Write head is 60. And it is given that the disk arm should
move towards the larger value.

105
Seek Time = Distance Moved by the disk arm = (170-60)+(170-25)=235

C-SCAN
This algorithm is the same as the SCAN algorithm. The only difference between
SCAN and C-SCAN is, it moves in a particular direction till the last and serves
the requests in its path. Then, it returns in the opposite direction till the end and
doesn't serve the request while returning. Then, again reverses the direction and
serves the requests found in the path. It moves circularly.

Eg. Suppose the order of requests are 70, 140, 50, 125, 30, 25, 160 and the initial
position of the Read-Write head is 60. And it is given that the disk arm should
move towards the larger value.

Seek Time = Distance Moved by the disk arm = (170-60)+(170-0)+(50-0)=330

C-LOOK

106
This algorithm is also the same as the LOOK algorithm. The only difference
between LOOK and C-LOOK is, it moves in a particular direction till the last
request is found and serves the requests in its path. Then, it returns in the opposite
direction till the last request is found in that direction and doesn't serve the request
while returning. Then, again reverses the direction and serves the requests found
in the path. It also moves circularly.

Eg. Suppose the order of requests are 70, 140, 50, 125, 30, 25, 160 and the initial
position of the Read-Write head is 60. And it is given that the disk arm should
move towards the larger value.

Seek Time = Distance Moved by the disk arm = (160-60)+(160-25)+(50-25)=260

File Concepts-File System Structure-Access Methods

File

A file is a named collection of related information that is recorded on secondary storage such
as magnetic disks, magnetic tapes and optical disks. In general, a file is a sequence of bits,
bytes, lines or records whose meaning is defined by the files creator and user.

File Structure

A File Structure should be according to a required format that the operating system can
understand.
• A file has a certain defined structure according to its type.
• A text file is a sequence of characters organized into lines.
• A source file is a sequence of procedures and functions.

107
• An object file is a sequence of bytes organized into blocks that are
understandable by the machine.
• When operating system defines different file structures, it also contains the code
to support these file structure. Unix, MS-DOS support minimum number of file
structure.

File Type

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

Ordinary files
• These are the files that contain user information.
• These may have text, databases or executable program.
• The user can apply various operations on such files like add, modify, delete or even
remove the entire file.
Directory files
• These files contain list of file names and other information related to these files.
Special files
• These files are also known as device files.
• These files represent physical device like disks, terminals, printers, networks, tape
drive etc.
These files are of two types −
• Character special files − data is handled character by character as in case of
terminals or printers.
• Block special files − data is handled in blocks as in the case of disks and tapes.

File Access Mechanisms

File access mechanism refers to the manner in which the records of a file may be accessed.
There are several ways to access files −
• Sequential access
• Direct/Random access
• Indexed sequential access
Sequential access
A sequential access is that in which the records are accessed in some sequence, i.e., the
information in the file is processed in order, one record after the other. This access method is
the most primitive one. Example: Compilers usually access files in this fashion.

Direct/Random access
• Random access file organization provides, accessing the records directly.

108
• 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 records need not be in any sequence within the file and they need not be in
adjacent locations on the storage medium.
Indexed sequential 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.

Space Allocation

Files are allocated disk spaces by operating system. Operating systems deploy following three
main ways to allocate disk space to files.
• Contiguous Allocation
• Linked Allocation
• Indexed Allocation
Contiguous Allocation
• Each file occupies a contiguous address space on disk.
• Assigned disk address is in linear order.
• Easy to implement.
• External fragmentation is a major issue with this type of allocation technique.
Linked Allocation
• Each file carries a list of links to disk blocks.
• Directory contains link / pointer to first block of a file.
• No external fragmentation
• Effectively used in sequential access file.
• Inefficient in case of direct access file.
Indexed Allocation
• Provides solutions to problems of contiguous and linked allocation.
• A index block is created having all pointers to files.
• Each file has its own index block which stores the addresses of disk space occupied by
the file.
• Directory contains the addresses of index blocks of files.

109
Directory Structure
What is a directory?
Directory can be defined as the listing of the related files on the disk. The directory may store
some or the entire file attributes.

To get the benefit of different file systems on the different operating systems, A hard disk can
be divided into the number of partitions of different sizes. The partitions are also called volumes
or mini disks.

Each partition must have at least one directory in which, all the files of the partition can be
listed. A directory entry is maintained for each file in the directory which stores all the
information related to that file.

A directory can be viewed as a file which contains the Meta data of the bunch of files.

Every Directory supports a number of common operations on the file:

1. File Creation
2. Search for the file
3. File deletion
4. Renaming the file
5. Traversing Files
6. Listing of files

110
Single Level Directory

The simplest method is to have one big list of all the files on the disk. The entire system will
contain only one directory which is supposed to mention all the files present in the file system.
The directory contains one entry per each file present on the file system.

This type of directories can be used for a simple system.

Advantages

1. Implementation is very simple.


2. If the sizes of the files are very small then the searching becomes faster.
3. File creation, searching, deletion is very simple since we have only one directory.

Disadvantages

1. We cannot have two files with the same name.


2. The directory may be very big therefore searching for a file may take so much time.
3. Protection cannot be implemented for multiple users.
4. There are no ways to group same kind of files.
5. Choosing the unique name for every file is a bit complex and limits the number of files in the
system because most of the Operating System limits the number of characters used to construct
the file name.

Two Level Directory


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

111
Characteristics of two level directory system

1. Each files has a path name as /User-name/directory-name/


2. Different users can have the same file name.
3. Searching becomes more efficient as only one user's list needs to be traversed.
4. The same kind of files cannot be grouped into a single directory for a particular user.

Every Operating System maintains a variable as PWD which contains the present directory
name (present user name) so that the searching can be done appropriately.

Tree Structured Directory


In Tree structured directory system, any directory entry can either be a file or sub directory.
Tree structured directory system overcomes the drawbacks of two level directory system. The
similar kind of files can now be grouped in one directory.

Each user has its own directory and it cannot enter in the other user's directory. However, the
user has the permission to read the root's data but he cannot write or modify this. Only
administrator of the system has the complete access of root directory.

Searching is more efficient in this directory structure. The concept of current working directory
is used. A file can be accessed by two types of path, either relative or absolute.

Absolute path is the path of the file with respect to the root directory of the system while relative
path is the path with respect to the current working directory of the system. In tree structured
directory systems, the user is given the privilege to create the files as well as directories.

112
Permissions on the file and directory

A tree structured directory system may consist of various levels therefore there is a set of
permissions assigned to each file and directory.

The permissions are R W X which are regarding reading, writing and the execution of the files
or directory. The permissions are assigned to three types of users: owner, group and others.

There is a identification bit which differentiate between directory and file. For a directory, it
is d and for a file, it is dot (.)

The following snapshot shows the permissions assigned to a file in a Linux based system. Initial
bit d represents that it is a directory.

Protection in File System


In computer systems, alot of user’s information is stored, the objective of the operating
system is to keep safe the data of the user from the improper access to the system. Protection
can be provided in number of ways. For a single laptop system, we might provide protection
by locking the computer in a desk drawer or file cabinet. For multi-user systems, different
mechanisms are used for the protection.

Types of Access :
The files which have direct access of the any user have the need of protection. The files
which are not accessible to other users doesn’t require any kind of protection. The mechanism
of the protection provide the facility of the controlled access by just limiting the types of access
to the file. Access can be given or not given to any user depends on several factors, one of
which is the type of access required. Several different types of operations can be controlled:

113
Read – Reading from a file.
Write – Writing or rewriting the file.
Execute – Loading the file and after loading the execution process starts.
Append – Writing the new information to the already existing file, editing must be end at the
end of the existing file.
Delete – Deleting the file which is of no use and using its space for the another data.
List – List the name and attributes of the file.
Operations like renaming, editing the existing file, copying; these can also be
controlled. There are many protection mechanism. each of them mechanism have different
advantages and disadvantages and must be appropriate for the intended application.
Access Control :
There are different methods used by different users to access any file. The general way
of protection is to associate identity-dependent access with all the files and directories an list
called access-control list (ACL) which specify the names of the users and the types of access
associate with each of the user. The main problem with the access list is their length. If we
want to allow everyone to read a file, we must list all the users with the read access. This
technique has two undesirable consequences:

Constructing such a list may be tedious and unrewarding task, especially if we do not
know in advance the list of the users in the system.

Previously, the entry of the any directory is of the fixed size but now it changes to the
variable size which results in the complicates space management. These problems can
be resolved by use of a condensed version of the access list. To condense the length of
the access-control list, many systems recognize three classification of users in
connection with each file:

Owner – Owner is the user who has created the file.


Group – A group is a set of members who has similar needs and they are sharing the same
file.
Universe – In the system, all other users are under the category called universe.
The most common recent approach is to combine access-control lists with the normal
general owner, group, and universe access control scheme. For example: Solaris uses the three

114
categories of access by default but allows access-control lists to be added to specific files and
directories when more fine-grained access control is desired.

Other Protection Approaches:

The access to any system is also controlled by the password. If the use of
password is random and it is changed often, this may be result in limit the
effective access to a file.

The use of passwords has a few disadvantages:


The number of passwords are very large so it is difficult to remember the large passwords.
If one password is used for all the files, then once it is discovered, all files are accessible;
protection is on all-or-none basis.

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 lined list. Each file contains
the pointers to the data blocks which are assigned to it and the next file in the directory.

Characteristics

57.2OOPs Concepts in Java

1. When a new file is created, then the entire list is checked whether the new file name is matching
to a 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.

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

116
Allocation Methods

There are various methods which can be used to allocate disk space to the files. Selection of an
appropriate allocation method will significantly affect the performance and efficiency of the
system. Allocation method provides a way in which the disk will be utilized and the files will
be accessed.

There are following methods which can be used for allocation.

1. Contiguous Allocation.
2. Extents
3. Linked Allocation
4. Clustering
5. FAT
6. Indexed Allocation
7. Linked Indexed Allocation
8. Multilevel Indexed Allocation
9. Inode

Contiguous Allocation

If the blocks are allocated to the file in such a way that all the logical blocks of the file get the
contiguous physical block in the hard disk then such allocation scheme is known as contiguous
allocation.

In the image shown below, there are three files in the directory. The starting block and the
length of each file are mentioned in the table. We can check in the table that the contiguous
blocks are assigned to each file as per its need.

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


2. It may be difficult to have a file grow.

Linked List Allocation


Linked List allocation solves all problems of contiguous allocation. In linked list
allocation, each file is considered as the linked list of disk blocks. However, the disks blocks
allocated to a particular file need not to be contiguous on the disk. Each disk block allocated to
a file contains a pointer which points to the next disk block allocated to the same file.

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

File Allocation Table

The main disadvantage of linked list allocation is that the Random access to a
particular block is not provided. In order to access a block, we need to access all its previous
blocks.File Allocation Table overcomes this drawback of linked list allocation. In this
scheme, a file allocation table is maintained, which gathers all the disk block links. The table
has one entry for each disk block and is indexed by block number.

File allocation table needs to be cached in order to reduce the number of head seeks.
Now the head doesn't need to traverse all the disk blocks in order to access one successive
block.

It simply accesses the file allocation table, read the desired block entry from there and
access that block. This is the way by which the random access is accomplished by using FAT.
It is used by MS-DOS and pre-NT Windows versions.

119
Advantages

1. Uses the whole disk block for data.


2. A bad disk block doesn't cause all successive blocks lost.
3. Random access is provided although its not too fast.
4. Only FAT needs to be traversed in each file operation.

Disadvantages

1. Each Disk block needs a FAT entry.


2. FAT size may be very big depending upon the number of FAT entries.
3. Number of FAT entries can be reduced by increasing the block size but it will also increase
Internal Fragmentation.

Indexed Allocation
Limitation of FAT
Limitation in the existing technology causes the evolution of a new technology. Till
now, we have seen various allocation methods; each of them was carrying several advantages
and disadvantages.

120
File allocation table tries to solve as many problems as possible but leads to a drawback.
The more the number of blocks, the more will be the size of FAT.

Therefore, we need to allocate more space to a file allocation table. Since, file allocation table
needs to be cached therefore it is impossible to have as many space in cache. Here we need a
new technology which can solve such problems.

Indexed Allocation Scheme

Instead of maintaining a file allocation table of all the disk pointers, Indexed allocation scheme
stores all the disk pointers in one of the blocks called as indexed block. Indexed block doesn't
hold the file data, but it holds the pointers to all the disk blocks allocated to that particular file.
Directory entry will only contain the index block address.

Advantages

1. Supports direct access


2. A bad data block causes the lost of only that block.

Disadvantages

1. A bad index block could cause the lost of entire file.


2. Size of a file depends upon the number of pointers, a index block can hold.
3. Having an index block for a small file is totally wastage.

121
4. More pointer overhead

Linked Index Allocation


Single level linked Index Allocation

In index allocation, the file size depends on the size of a disk block. To allow large files, we
have to link several index blocks together. In linked index allocation,

o Small header giving the name of the file


o Set of the first 100 block addresses
o Pointer to another index block

For the larger files, the last entry of the index block is a pointer which points to another index
block. This is also called as linked schema.

Advantage: It removes file size limitations

Disadvantage: Random Access becomes a bit harder

Multilevel Index Allocation

In Multilevel index allocation, we have various levels of indices. There are outer level index
blocks which contain the pointers to the inner level index blocks and the inner level index
blocks contain the pointers to the file data.

o The outer level index is used to find the inner level index.
o The inner level index is used to find the desired data block.

Advantage: Random Access becomes better and efficient.

122
Disadvantage: Access time for a file will be higher.

Inode
In UNIX based operating systems, each file is indexed by an Inode. Inode are the special disk
block which is created with the creation of the file system. The number of files or directories
in a file system depends on the number of Inodes in the file system.

An Inode includes the following information

1. Attributes (permissions, time stamp, ownership details, etc) of the file


2. A number of direct blocks which contains the pointers to first 12 blocks of the file.
3. A single indirect pointer which points to an index block. If the file cannot be indexed entirely
by the direct blocks then the single indirect pointer is used.
4. A double indirect pointer which points to a disk block that is a collection of the pointers to the
disk blocks which are index blocks. Double index pointer is used if the file is too big to be
indexed entirely by the direct blocks as well as the single indirect pointer.

123
5. A triple index pointer that points to a disk block that is a collection of pointers. Each of the
pointers is separately pointing to a disk block which also contains a collection of pointers which
are separately pointing to an index block that contains the pointers to the file blocks.

Free Space Management

A file system is responsible to allocate the free blocks to the file therefore it has to keep track
of all the free blocks present in the disk. There are mainly two approaches by using which, the
free blocks in the disk are managed.

1. Bit Vector

In this approach, the free space list is implemented as a bit map vector. It contains the number
of bits where each bit represents each block.

If the block is empty then the bit is 1 otherwise it is 0. Initially all the blocks are empty therefore
each bit in the bit map vector contains 1.

LAs the space allocation proceeds, the file system starts allocating blocks to the files and setting
the respective bit to 0.

2. Linked List

It is another approach for free space management. This approach suggests linking together all
the free blocks and keeping a pointer in the cache which points to the first free block.

Therefore, all the free blocks on the disks will be linked together with a pointer. Whenever a
block gets allocated, its previous free block will be linked to its next free block.

124

You might also like