Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
22 views

Operating Systems Module Fcit

Uploaded by

Harris Chikunya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Operating Systems Module Fcit

Uploaded by

Harris Chikunya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 69

FACULTY COMMERCE, INNOVATION AND TECHNOLOGY

DEPARTMENT INFORMATION TECHNOLOGY

PROGRAMME BBMIT

LEVEL 2.2

COURSE TITLE OPERATING SYSTEMS

COURSE CODE IT105

FACILITATOR HARRIS K CHIKUNYA

MODULE OVERVIEW The study of operating systems is the study of how a set of computational
resources may be shared safely, efficiently, and effectively amongst many
computing users. This course introduces the major concepts of modern
operating systems and study of the interrelationships between the OS
and the architecture of computer systems. The Course gives a general
overview of an Operating System as a Monitor, Supervisor and Resource
Manager. Therefore, the course teaches the OS design, its usefulness in
resource scheduling and management.

MODULE AIM The aim of the module is to introduce the students to the concepts of
Operating Systems (OS) as a guide towards understanding their design
and implementation as well as their roles in resource management.

LEARNING OUTCOMES By the end of this course, you should be able to:
1. Explain the concept of an operating system and its importance
in a computer system;
2. Describe the architecture of an operating system, especially the
operating systems of LINUX and Microsoft Windows; and
3. Explain the key mechanisms of the operating system like
process management, memory management, files systems and
I/O management.
OBJECTIVES In line with the stated aim of this module, the objectives of this course
are to:
• Expose the students to the different types and components of
operating system.

1
• Introduce to the students Operating System as a resource
manager (process, processor, memory and I/O management).
• Expose the students to the different types scheduling algorithms
and policies.
• Expose the students to the different challenges of concurrent
and cooperation processes (such as Race condition, Deadlock
etc.) and their solutions
• Expose the students to the different debugging methods.
• Expose the students to the different memory management
schemes.
• Expose the students to the different file attributes, protection
and security mechanisms.

RESOURCES/TECHNOLOGY Text Books, Internet, Journals

ASSESSMENT TYPE ✓ Final examination: 70%


✓ Course work: Tests, Assignments and Group Projects 30%
• Individual Assignment
• Practical Group Assignment
• In class Test

WK Module content subtopics


1 Topic 1: Operating Systems: An Overview

1.1. Introduction
Computer software can be divided into two main categories: application software and system
software. Application software consists of the programs for performing tasks particular to the
machine’s utilisation. This software is designed to solve a particular problem for users. Examples
of application software include spreadsheets, database systems, desktop publishing systems,
program development software and games.

On the other hand, system software is more transparent and less noticed by the typical
computer user. This software provides a general programming environment in which
programmers can create specific applications to suit their needs. This environment provides
new functions that are not available at the hardware level and performs tasks related to
executing the application program. System software acts as an interface between the hardware
of the computer and the application software that users need to run on the computer. The
most important type of system software is the operating system.

An Operating Systems (OS) is a collection of programs that acts as an interface between a user
of a computer and the computer hardware. The purpose of an operating system is to provide an

2
environment in which a user may execute the programs. Operating Systems are viewed as
resource managers. The main resource is the computer hardware in the form of processors,
storage, input/output devices, communication devices, and data. Some of the operating system
functions are implementing the user interface, sharing hardware among users, allowing users to
share data among themselves, preventing users from interfering with one another, scheduling
resources usage, facilitating parallel operations, organizing data for secure and rapid access, and
handling network communications. This unit presents the definition of the operating system,
goals of the operating system, generations of OS, different types of OS and functions of OS.

1.2. Learning Outcomes


By the end of this unit, you should be able to:
• explain the purpose of an Operating System;
• discuss the evolution of an Operating System;
• discuss eight types of Operating System;
• describe four desirable qualities of Operating System; and
• describe eight functions performed by the Operating System.

1.3. What is an Operating System


In a computer system, we find four main components: the hardware, the operating system, the
application software and the users. In a computer system the hardware provides the basic
computing resources. The applications programs define the way in which these resources are
used to solve the computing problems of the users. The operating system controls and
coordinates the use of the hardware among the various systems programs and application
programs for the various users. The operating system controls and coordinates the use of the
hardware among the various systems programs and application programs for the various users.

We can view an operating system as a resource allocator. A computer system has many
resources (hardware and software) that may be required to solve a problem: CPU time, memory
space, files storage space, input/output devices etc. The operating system acts as the manager
of these resources and allocates them to specific programs and users as necessary for their
tasks. Since there may be many, possibly conflicting, requests for resources, the operating
system must decide which requests are allocated resources to operate the computer system
fairly and efficiently. An operating system is a control program. This program controls the
execution of user programs to prevent errors and improper use of the computer. Operating
systems exist because: they are a reasonable way to solve the problem of creating a usable
computing system.

While there is no universally agreed upon definition of the concept of an operating system, the
following is a reasonable starting point. A computer’s operating system is a group of programs
designed to serve two basic purpose:
a) To control the allocation and use of the computing system’s resources among the
various users and tasks; and
b) To provide an interface between the computer hardware and the programmer that
simplifies and makes feasible the creation, coding, debugging, and maintenance of
application program.

An effective operating system should accomplish the following function:

3
(a) Should act as a command interpreter by providing a user-friendly environment;
(b) Should facilitate communication with other users;
(c) Facilitate the directory/file creation along with the security option;
(d) Provide routines that handle the intricate details of I/O programming;
(e) Provide access to compilers to translate programs from high-level language to
machine language;
(f) Provide a loader program to move the compiled program code to the computer’s
memory for execution;
(g) Assure that when there are several active processes in the computer, each will get
fair and non-interfering access to the central processing unit for execution;
(h) Take care of storage and device allocation;
(i) Provide for long term storage of user information in the form of files; and
(j) Permit system resources to be shared among users when appropriate and be
protected from unauthorised or mischievous intervention as necessary.

The abstract view of the components of a system and the positioning of OS is shown in the
Figure 1.1.

Figure 1.1: Abstract view of the components of a computer system

Activity 1a
1. What is meant by resource allocator?
2. List five functions which an effective Operating System should accomplish.

1.4. Goals of an Operating System


The primary objective of a computer is to execute an instruction in an efficient manner and to
increase the productivity of processing resources attached with the computer system such as
hardware resources, software resources and the users. In other words, we can say that
maximum CPU utilisation is the main objective, because it is the main device which is to be used
for the execution of the programs of instructions. We can brief the goals as:
a) The primary goal of an operating system is to make the computer convenient to use.

4
b) The secondary goal is to use the hardware in an efficient manner

1.5. History of Operating Systems

The First Generation (1940 to early 1950s)

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

The Second Generation (1955 - 1965)

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

The Third Generation (1965 - 1980)


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

The Fourth Generation (1980 - Present Day)

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

5
was developed by Steve Jobs, a co-founder of Apple. They named the operating system
Macintosh OS or Mac OS.

Activity 1b
1. Explain briefly the development of operating System.
2. Describe the impact of “user-friendly” in the fourth generation.
3. What are the main responsibilities of an Operating System?

1.6. Types of Operating Systems


In this section we will discuss about the different types of operating systems. Modern computer
operating systems may be classified into eight groups, which are distinguished by the nature of
interaction that takes place between the computer user and his or her program during its
processing. The eight groups as follows:

1.6.1. Batch Processing Operating System


In a batch operating system environment user submit jobs to a central place where these jobs
are collected into a batch, and subsequently placed on an input queue at the computer where
they will be run. In this case, the user has no interaction with the job during its processing, and
the computer’s response time is the turnaround time-the time from submission of the job until
execution is complete, and the results are ready for return to the person who submitted the
job.

1.6.2. Time Sharing


Another mode for delivering computing services is provided by time sharing operating systems.
In this environment a computer provides computing services to several or many users
concurrently in-line. Here, the various users are sharing the central processor, the memory, and
other resources of the computer system in a manner facilitated, controlled, and monitored by
the operating system. The user, in this environment, has nearly full interaction with the
program during its execution, and the computer’s response time may be expected to be no
more than a few second.

1.6.3. Real Time Operating System (RTOS)


The third class is the real time operating systems, which are designed to service those
applications where response time is of the essence in order to prevent error, misrepresentation
or even disaster. Examples of real time operating systems are those which handle airlines
reservations, machine tool control, and monitoring of a nuclear power station. The systems, in
this case, are designed to be interrupted by external signals that require the immediate
attention of the computer system.

These time operating systems are used to control machinery, scientific instruments and
industrial systems. An RTOS typically has very little user-interface capability, and no end-user
utilities. A very important part of an RTOS is managing the resources of the computer so that a
particular operation executes in precisely the same amount of time every time it occurs. In a
complex machine, having a part move more quickly just because system resources are available
may be just as catastrophic as having it not move at all because the system is busy.

1.6.4. Multiprogramming Operating System

6
A multiprogramming operating system is a system that allows more than one active user
program (or part of user program) to be stored on main memory simultaneously. Thus, it is
evident that a time-sharing system is a multiprogramming system but note that a
multiprogramming system is not necessarily a time-sharing system. A batch or real time
operating system could, and indeed usually does, have more than one active user program
simultaneously in main storage. Another important, and all too similar, term is
“multiprocessing”.

1.6.5. Multiprocessing System


A multiprocessing system is a computer hardware configuration that includes more than one
independent processing unit. The term multiprocessing is generally used to refer to large
computer hardware complexes found in major scientific or commercial applications.

1.6.6. Networking Operating System


A networked computing system is a collection of physical interconnected computers. The
operating system of each of the interconnected computers must contain, in addition to its own
stand-alone functionality, provisions for handing communication and transfer of program and
data among the other computers with which it is connected.

Network operating systems are not fundamentally different from single processor operating
systems. They obviously need a network interface controller and some low-level software to
drive it, as well as programs to achieve remote login and remote files access, but these
additions do not change the essential of the operating systems.

1.6.7. Distributed Operating System


A distributed computing system consists of a number of computers that are connected and
managed so that they automatically share the job processing load among the constituent
computers, or separate the job load as appropriate particularly configured processors. Such a
system requires an operating system which, in addition to the typical stand-alone functionality,
provides coordination of the operations and information flow among the component
computers. The networked and distributed information flow among the component computers.
The network and distributed computing environments and their respective operating systems
are designed with more complex functional capabilities. In a network operating system, the
users are aware of the existence of multiple computers and can log in to remote machines and
copy files from one machine to another. Each machine runs its own local operating system and
has its own user (or users).

A distributed operating system, in contrast, is one that appears to its users as a traditional
uniprocessor system, even though it is actually composed of multiple processors. In a true
distributed system, users should not be aware of where their programs are being run or where
their files are located; that should all be handled automatically and efficiently by the operating
system.

True distributed operating systems require more than just adding a little code to a uni-
processor operating system, because distributed and centralized systems differ in critical ways.
Distributed systems, for example, often allow program to run on several processors at the same
time, thus requiring more complex processor scheduling algorithms in order to optimize the
amount of parallelism achieved.

7
1.6.8. Operating Systems for Embedded Devices
As embedded systems (PDAs, cellphones, point-of-sale devices, VCR’s, industrial robot control,
or even your toaster) becomes more complex hardware-wise with every generation, and more
features are put into them day-by-day, applications they run require more and more to run on
actual operating system code in order to keep the development time reasonable. Some of the
popular OS are:
a) Nexus’s Conix - an embedded operating system for ARM processors.
b) Sun’s Java OS - a standalone virtual machine not running on top of any other OS; mainly
targeted at embedded systems.
c) Palm Computing’s Palm OS - Currently the leader OS for PDAs, has many applications
and supporting companies.
d) Microsoft’s Windows CE and Windows NT Embedded OS.

Let us study various examples of the popular operating systems in the next section.

Activity 1c
1. Explain briefly eight types of Operating system.
2. Differentiate between Network Operating System and Distributed Operating
System.

1.7. Desirable Qualities of Operating Systems

The desirable qualities of an operating system are in terms of:


1. Usability
a. Robustness;
b. Consistency;
c. Proportionality;
d. Convenience; and
e. Powerful with high level facilities.
2. Facilities
a. Sufficient for intended use;
b. Complete; and
c. Appropriate.
3. Costs
a. Want low cost and efficient services
b. Good Algorithms Make use of space/time tradeoffs, special hardware.
c. Low Overhead Costs of doing nothing should be low. E.g.: idle time at a
terminal.
d. Low Maintenance Cost System should not require constant attention.
4. Adaptability
a. Tailored to the Environment
• Support necessary activities. Do not impose unnecessary
restrictions. What are the things people do most? - make them
easy.
b. Changeable Over Time
• Adapt as needs and resources change. E.g.: expanding memory and
new devices or new user population.

8
c. Extendible - Extensible
• Adding new facilities and features - which look like the old ones.

1.8. Operating Systems: Some Examples


In the earlier section we had seen the types of operating systems. In this section we will study
some popular operating systems.
1. Microsoft Windows
2. MacOS
3. Android
4. Apple iOS
5. Linux
6. Ubuntu
7. ChromeOS
8. Fedora
9. FreeBSD
10. Solaris

Activity 1d
1. Discuss different views of the Operating Systems.
2. Summarise the characteristics of the following Operating Systems: (a) Batch
Processing (b) Time Sharing (c) Real time

1.9. Functions of Operating System


The main functions of an operating system are as follows:
(a) Process management;
(b) Memory management;
(c) Secondary storage management;
(d) I/O management;
(e) File management;
(f) Protection;
(g) Networking management; and
(h) Command interpretation.

1.9.1. Process Management


The CPU executes a large number of programs. While its main concern is the execution of user
programs, the CPU is also needed for other system activities. These activities are called
processes. A process is a program in execution. Typically, a batch job is a process. A time-shared
user program is a process. A system task, such as spooling is also a process. For now, a process
may be considered as a job or a time-shared program, but the concept is actually more general.
The operating system is responsible for the following activities in connection with processes
management:
(a) The creation and deletion of both user and system processes;
(b) The suspension and resumption of processes;
(c) The provision of mechanisms for process synchronisation; and
(d) The provision of mechanisms for deadlock handling

1.9.2. Memory Management

9
Memory is the most expensive part in the computer system. Memory is a large array of words
or bytes, each with its own address. Interaction is achieved through a sequence of reads or
writes of specific memory address. The CPU fetches from and stores in memory. There are
various algorithms that depend on the particular situation to manage the memory. Selection of
a memory management scheme for a specific system depends upon many factors, but
especially upon the hardware design of the system. Each algorithm requires its own hardware
support. The operating system is responsible for the following activities in connection with
memory management.
(a) Keep track of which parts of memory are currently being used and by whom;
(b) Decide which processes are to be loaded into memory when memory space
becomes available; and
(c) Allocate and deallocate memory space as needed.
Memory management techniques will be discussed in great detail later in this course.

1.9.3. Secondary Storage Management


The main purpose of a computer system is to execute programs. These programs, together with
the data they access, must be in main memory during execution. Since the main memory is too
small to permanently accommodate all data and program, the computer system must provide
secondary storage to backup main memory. Most modem computer systems use disks as the
primary on-line storage of information, of both programs and data. Most programs, like
compilers, assemblers, sort routines, editors, formatters, and so on, are stored on the disk until
loaded into memory, and then use the disk as both the source and destination of their
processing. Hence the proper management of disk storage is of central importance to a
computer system. There are few alternatives. Magnetic tape systems are generally too slow. In
addition, they are limited to sequential access. Thus tapes are more suited for storing
infrequently used files, where speed is not a primary concern.

The operating system is responsible for the following activities in connection with disk
management:
(a) Free space management;
(b) Storage allocation; and
(c) Disk scheduling.

1.9.4. I/O Management


One of the purposes of an operating system is to hide the peculiarities or specific hardware
devices from the user. For example, in UNIX, the peculiarities of I/O devices are hidden from the
bulk of the operating system itself by the I/O system. The operating system is responsible for
the following activities in connection to I/O management:
(a) A buffer caching system;
(b) To activate a general device driver code; and
(c) To run the driver software for specific hardware devices as and when required.

1.9.5. File Management


File management is one of the most visible services of an operating system. Computers can
store information in several different physical forms: magnetic tape, disk and drum are the most
common forms. Each of these devices has it own characteristics and physical organisation.

10
For convenient use of the computer system, the operating system provides a uniform logical
view of information storage. The operating system abstracts from the physical properties of its
storage devices to define a logical storage unit, the file. Files are mapped, by the operating
system, onto physical devices. A file is a collection of related information defined by its creator.
Commonly, files represent programs (both source and object forms) and data. Data files may be
numeric, alphabetic or alphanumeric. Files may be free-form, such as text files or may be rigidly
formatted. In general, a file is a sequence of bits, bytes, lines or records whose meaning is
defined by its creator and user. It is a very general concept.

The operating system implements the abstract concept of the file by managing mass storage
device, such as types and disks. Also, files are normally organised into directories to ease their
use. Finally, when multiple users have access to files, it may be desirable to control by whom
and in what ways files may be accessed.

The operating system is responsible for the following activities in connection to the file
management.
(a) The creation and deletion of files;
(b) The creation and deletion of directory;
(c) The support of primitives for manipulating files and directories;
(d) The mapping of files onto disk storage;
(e) Backup of files on stable (nonvolatile) storage; and
(f) Protection and security of the files.

1.9.6. Protection
The various processes in an operating system must be protected from each other’s activities.
For that purpose, various mechanisms which can be used to ensure that the files, memory
segment, CPU and other resources can be operated on only by those processes that have
gained proper authorisation from the operating system.

For example, memory addressing hardware ensures that a process can only execute within its
own address space. The timer ensures that no process can gain control of the CPU without
relinquishing it. Finally, no process is allowed to do its own I/O, to protect the integrity of the
various peripheral devices. Protection refers to a mechanism for controlling the access of
programs, processes, or users to the resources defined by a computer controls to be imposed,
together with some means of enforcement.

Protection can improve reliability by detecting latent errors at the interfaces between
component subsystems. Early detection of interface errors can often prevent contamination of
a healthy subsystem by a subsystem that is malfunctioning. An unprotected resource cannot
defend against use (or misuse) by an unauthorised or incompetent user.

1.9.7. Networking
A distributed system is a collection of processors that do not share memory or a clock. Instead,
each processor has its own local memory, and the processors communicate with each other
through various communication lines such as high-speed buses or telephone lines. Distributed
systems vary in size and function. They may involve microprocessors, workstations,
minicomputers and large general-purpose computer systems.

11
The processors in the system are connected through a communication network, which can be
configured in the number of different ways. The network may be fully or partially connected.
The communication network design must consider routing and connection strategies and the
problem of connection and security.

A distributed system provides the user with access to the various resources the system
maintains. Access to a shared resource allows computation speed-up, data availability and
reliability.

1.9.8. Command Interpretation


One of the most important components of an operating system is its command interpreter. The
command interpreter is the primary interface between the user and the rest of the system.
Many commands are given to the operating system by control statements. When a new job is
started in a batch system or when a user logs-in to a time-shared system, a program which
reads and interprets control statements is automatically executed. This program is variously
called:
(a) The control card interpreter;
(b) The command line interpreter; and
(c) The shell (in Unix) and so on.
Its function is quite simple: get the next command statement and execute it.
The command statements themselves deal with process management, I/O handling, secondary
storage management, main memory management, file system access, protection and
networking.

The Figure 1.2 depicts the role of the operating system in coordinating all the functions.

Figure 1.2: Functions coordinated by the operating system

Activity 1d
1. Mention the advantanges and limitations of Multiuser Operating Systems.
2. What is a Multitasking system and mention its advantages.
3. Illustrate a simple operating system for a security control system.

12
1.10. Summary
This Unit presented the principle operation of an operating system. In this unit we had briefly
described about the history, the generations and the types of operating systems. An operating
system is a program that acts as an interface between a user of a computer and the computer
hardware. The purpose of an operating system is to provide an environment in which a user
may execute programs. The primary goal of an operating system is to make the computer
convenient to use. And the secondary goal is to use the hardware in an efficient manner.
Operating systems may be classified by both how many tasks they can perform
“simultaneously” and by how many users can be using the system “simultaneously”. That is:
single-user or multi-user and single-task or multi-tasking. A multi-user system must clearly be
multi-tasking. In the next unit we will discuss the concept of processes and their management
by the OS.

1.11. Key terms


Application program Multiprogramming Multi user
Central processing unit (CPU) Operating system
Multiprocessing
Disk operating system (DOS) Embedded device UNIX
Real time operating system (RTOS) Time sharing Macintosh
Single task Windows Memory
Single user

Reading sources/links
1. Deital, H., M. (1984). An introduction to operating systems. Peterson: Addition
Wesley Publishing Company.
2. Dhamdhere, D., M. (2006). Operating systems: A concept-based approach. New
Delhi: Tata McGraw-Hill.
3. Madnicj, & Donovan. (1974). Operating system - Concepts and design. New
York: McGraw-Hill International Education.
4. Milenkovic, M. (2000). Operating systems: Concept and design. New York:
McGrawHill International Education.
5. Silberschatz, A., & James, L. (1985). Operating system concepts. Peterson:
Addition Weslet Publishing Company.
6. Tanenbaum, A., S., & Woodhull, A., S. (2009). Operating system design and
implementation. UK: Pearson.

2 Topic 2: Processes

2.1. Introduction
In the earlier unit we have studied the overview and the functions of an operating system. In
this unit we have detailed discussion on the processes and their management by the operating
system. The other resource management features of operating systems will be discussed in the
subsequent units. The CPU executes a large number of programs. While its main concern is the
execution of user programs, the CPU is also needed for other system activities. These activities

13
are called processes. A process is a program in execution. Typically, a batch job is a process. A
time-shared user program is a process. A system task, such as spooling, is also a process. For
now, a process may be considered as a job or a time-shared program, but the concept is
actually more general.

In general, a process will need certain resources such as CPU time, memory, files, I/O devices,
etc, to accomplish its task. These resources are given to the process when it is created. In
addition to the various physical and logical resources that a process obtains when it is created,
some initialisation data (input) may be passed along. For example, a process whose function is
to display the status of a file, say F1, on the screen will get the name of the file F1 as an input
and execute the appropriate program to obtain the desired information.

We emphasise that a program by itself is not a process; a program is a passive entity, while a
process is an active entity. It is known that two processes may be associated with the same
program; they are nevertheless considered two separate execution sequences. A process is the
topic of work in a system. Such a system consists of a collection of processes, some of which are
operating system processes, those that execute system code, and the rest being user processes,
those that execute user code. All of those processes can potentially execute concurrently.

The operating system is responsible for the following activities in connection with processes
managed.
1. The creation and deletion of both user and system processes;
2. The suspension is resumption of processes;
3. The provision of mechanism for process synchronisation; and
4. The provision of mechanism for deadlock handling.

We will learn the operating system view of the processes, types of schedulers, different types of
scheduling algorithms, in the subsequent sections of this topic.

2.2. Learning Outcomes


By the end of this unit, you should be able to:
• Explain the concepts of process, various states in the process and their scheduling;
• Define a process control block;
• Classify three types of schedulers;
• Explain five types of scheduling algorithms; and
• Compare the performance evaluation of the scheduling algorithms.

2.3. The Concept of Process


The term “process” was first used by the operating system designers of the MULTICS system
way back in 1960s. There are different definitions to explain the concept of process. A process
is:
a. An instance of a program in execution;
b. An asynchronous activity;
c. The “animated spirit” of a procedure;
d. The “locus of control” of a procedure in execution;
e. The “dispatchable” unit; and

14
f. Unit of work individually schedulable by an operating system.

Formally, we can define a process is an executing program, including the current values of the
program counter, registers and variables. The subtle difference between a process and a
program is that the program is a group of instructions whereas the process is the activity.

In multiprogramming systems, processes are performed is a pseudo-parallelism as if each


process has its own processor. In fact, there is only one processor, but it switches back and forth
from process to process. Henceforth, by saying execution of a process, we mean the processor’s
operation on the process like changing its variables, etc and I/O work means the interaction of
the process with the I/O operations like reading something or writing to somewhere. They may
also be named as “processor (CPU) burst” and “I/O burst” respectively. According to these
definitions, we classify program as:
(a) Processor-bound Program
A program having long processor bursts (execution instants).
(b) I/O - bound program
A program having short processor bursts.

The operating system works as the computer system software that assists hardware in
performing process management functions. Operating system keeps track of all the active
processes and allocate system resources to them according to policies devised to meet design
performance objectives. To meet process requirements OS must maintain many data structures
efficiently. The process abstraction is fundamental means for the OS to manage concurrent
program execution. OS must interleave the execution of a number of processes to maximise
processor use while providing reasonable response time. It must allocate resources to processes
in conformance with a specific policy. In general, a process will need certain resources such as
CPU time, memory, files, I/O devices etc to accomplish its tasks. These resources are allocated
to the process when it is created. A single processor may be shared among several processes
with some scheduling algorithm being used to determine when to stop work on one process
and provide service to a different one which we will discuss later in this topic.

Operating systems must provide some way to create all the processes needed. In simple
systems, it may be possible to have all the processes that will ever be needed be present when
the system comes up. In almost all systems however, some way is needed to create and destroy
processes as needed during operations. In UNIX, for instant, processes are created by the fork
system call, which makes an identical copy of the calling process. In other systems, system calls
exist to create a process, load its memory. And start it running. In general, processes need a way
to create other processes. Each process has one parent process, but zero, one, two or more
children processes.

For an OS, the process management functions include:


a) Process Creation;
b) Termination of the Process;
c) Controlling the Progress of the Process;
d) Process Scheduling;
e) Dispatching;
f) Interrupt Handling/Exceptional Handling;
g) Switching between the Processes;

15
h) Process Synchronisation;
i) Inter process Communication Support; and
j) Management of Process Control Blocks.
2.3.1. Processes Relationship
In the concurrent environment basically processes have two relationships, competition and
cooperation. In the concurrent environment, processes compete with each other for allocation
of system resources to execute their instructions. In addition, a collection of related processes
that collectively represent a single logical application cooperate with each other. There should
be a proper operating system to support these relations. In the competition, there should be
proper resource allocation and protections in address generation.

We distinguish between independent process and cooperating process. A process is


independent if it cannot affect or be affected by other processes executing in the system.
1. Independent Process
These types of processes have following features:
(i) Their state is not shared in any way by any other process;
(ii) Their execution is deterministic, i.e. the results of execution depend only on
the input values;
(iii) Their execution is reproducible, i.e. the results of execution will always be
the same for the same input; and
(iv) Their execution can be stopped and restarted without any negative effect.
2. Cooperating Process
In contrast to independent processes, cooperating processes can affect or be
affected by other processes executing the system. They are characterised by:
(i) Their states are shared by other processes; and
(ii) Their execution is not deterministic, i.e. the results of execution depend on
relative execution sequence and cannot be predicted in advance.
Their execution is irreproducible, i.e. the results of execution are not always the same for the
same input.

2.3.2. Process States


As a process executes, it changes state. The state of a process is defined by the current activity
of that process. It is also possible for a process that is ready and able to run to be blocked
because the operating system is decided to allocate the CPU to other process for a while.

The process may be in one of the following states:


1. New
The process is being created.
2. Ready
The process is waiting to be assigned to a processor.
3. Running
Instructions are being executed.
4. Waiting/Suspended/Blocked
The process is waiting for some event to occur.
5. Halted/Terminated
The process has finished execution.
The transition of the process states are shown in Figure 2.1 and their corresponding transition
are described.

16
As shown in Figure 2.1, four transitions are possible among the states. Transition 1 appears
when a process discovers that it cannot continue. In order to get into blocked state, some
systems must execute a system call block. In other systems, when a process reads from a pipe
or special file and there is no input available, the process is automatically blocked.

Transition 2 and 3 are caused by the process schedule, a part of the operating system.
Transition 2 occurs when the scheduler decides that the running process has run long enough,
and it is time to let another process have some CPU time.

Transition 3 occurs when all other processes have had their share and it is time for the first
process to run again.

Transition 4 appears when the external event for which a process was waiting was happened. If
no other process is running at that instant, transition 3 will be triggered immediately, and the
process will start running. Otherwise, it may have to wait in ready state for a little while until
the CPU is available. Using the process model, it becomes easier to think about what is going on
inside the system.

2.3.3. Implementation of Processes


To implement the process model, the operating system maintains a table, an array of
structures, called the Process Table or Process Control Block (PCB) or Switch Frame. Each entry
identifies a process with information such as process state, its program counter, stack pointer,
memory allocation, the status of its open files, its accounting and scheduling information. In
other words,it must contain everything about the process that must be saved when the process
is witched from the running state to the following is the information stores in a PCB.
a) Process number, each indicates the address of the next instruction to be executed for
this process;
b) CPU registers, which vary in number and type, depending on theconcrete
microprocessor architecture;
c) Memory management information, which include base and bounds registers or page
table;
d) I/O status information, composed I/O requests, I/O devices allocated to this process, a
list of open files and so on;

17
e) Processor scheduling information, which includes process priority, pointers to
scheduling queues and any other scheduling parameters;
f) List of open files.

A process structure block is shown in Figure 2.2.

Figure 2.2: Process control block structure

2.3.4. Threads
Threads, sometimes called Light-weight Processes (LWPs) are independently scheduled parts of
a single program. We say that a task is multithreaded if it is composed of several independent
sub-processes which do work on common data, and if each of those pieces could (at least in
principle) run in parallel.

If we write a program which uses threads - there is only one program, one executable file, one
task in the normal sense. Threads simply enable us to split up that program into logically
separate pieces, and have the pieces run independently of one another, until they need to
communicate. In a sense, threads are a further level of object orientation for multitasking
systems. They allow certain functions to be executed in parallel with others.

On a truly parallel computer (several CPUs) we might imagine parts of a program (different
subroutines) running on quite different processors, until they need to communicate. When one
part of the program needs to send data to the other part, the two independent pieces must be
synchronised, or be made to wait for one another. But what is the point of this? We can always
run independent procedures in a program as separate programs, using the process mechanisms
we have already introduced. They could communicate using normal interprocesses
communication. Why introduce another new concept? Why do we need threads?

The point is that threads are cheaper than normal processes, and that they can be scheduled for
execution in a user-dependent way, with less overhead. Threads are cheaper than a whole
process because they do not have a full set of resources each. Whereas the process control
block for a heavyweight process is large and costly to context switch, the PCBs for threads are
much smaller, since each thread has only a stack and some registers to manage. It has no open
file lists or resource lists, no accounting structures to update. All of these resources are shared

18
by all threads within the process. Threads can be assigned priorities - a higher priority thread
will get put to the front of the queue. Let us define heavy and lightweight processes with the
help of a Table 2.1.

Table 2.1: Processes and Resources

Why Use Threads?


The sharing of resources can be made more effective if the scheduler knows known exactly
what each program was going to do in advance. Of course, the scheduling algorithm can never
know this - but the programmer who wrote the program does know. Using threads, it is possible
to organise the execution of a program in such a way that something is always being done,
whenever the scheduler gives the heavyweight process CPU time.
a) Threads allow a programmer to switch between lightweight processes when it is best
for the program. (The programmer has control).
b) A process which uses threads does not get more CPU time than an ordinary process -
but the CPU time is gets is used to do work on the threads. It is possible to write a more
efficient program by making use of threads.
c) Inside a heavyweight process, threads are schedules on a FCFS basis, unless the
program decides to force certain threads to wait for other threads. If there is only one
CPU, then only one thread can be running at a time.
d) Threads context switch without any need to involve the kernel-the switching is
performed by a user level library, so time is saved because the kernel doesn’t need to
know about the threads.

2.3.5. Level of Threads


In modern operating systems, there are two levels at which threads operate:
a) System or kernel threads; and
b) User level threads.

If the kernel itself is multithreaded, the scheduler assigns CPU time on a thread basis rather
than on a process basis. A kernel level thread behaves like a virtual CPU, or power-point to
which user-processes can connect in order to get computing power. The kernel has as many
system level threads as it has CPUs and each of these must be shared between all of the user-
threads, which in turn is equal to the number of CPUs on the system.

Since threads work “inside” a single task, the normal process scheduler cannot normally tell
which thread to run and which not to run - that is up to the program. When the kernel
schedules a process for execution, it must then find out from that process which is the next
thread it must execute. If the program is lucky enough to have more than one processor
available, then several threads can schedules at the same time. Some important
implementation of threads are:

19
a) The Mach System / OSFI user and system level);
b) Solaris 1 (user level);
c) Solaris 2 (user and system level);
d) OS/2 (system level only);
e) NT threads (user and system level);
f) IRIX threads; and
g) POSIX standardised user threads interface

2.4. Systems Calls for Process Management


In this section, we will discuss system calls typically provided by the kernels of
multiprogramming operating systems for process management. System calls provide the
interface between a process and the operating system. These system calls are the routine
services of the operating system.

As an example of how system calls are used, consider writing a simple program to read data
from one file and to copy them to another file. There are two names of two different files in
which one input file and the other is the output file. One approach is for the program to ask the
user the names of the two files. In an interactive system, this approach will require a sequence
of systems calls, first to write a prompting message on the screen and then to read from the
keyboard the character that the two files have. Once the two file names are obtained the
program must open the input file and create the output file. Each of these operations requires
another system call and may encounter possible error conditions. When the program tries to
open the input file, it may find that no file of that name exists or that the file is protected
against access. In these cases the program should print the message on the console and then
terminate abnormally which require another system call. If the input file exists then we must
create the new output file. We may find an output file with the same name. This situation may
cause the program to abort or we may delete the existing file and create a new one. After
opening files, we may enter a loop that reads from input file and writes to output file. Each read
and write must return status information regarding various possible error conditions. Finally,
after the entire file is copies the program may close both files. Examples of some operating
system calls are:
1. Create
In response to the create call the operating system creates a new process with the
specified or default attributes and indentifier. Some of the parameters definable at
the process creation time include:
(i) Level of privilege, such as system or user;
(ii) Priority; (iii)Size and memory requirements;
(iii) Maximum data area and/or stack size;
(iv) Memory protection information and access rights; and
(v) Other system dependent data.
2. Delete
The delete service is also called destroy, terminate or exit. Its execution causes the
operating system to destroy the designated process and remove it from the system.
3. Abort
It is used to terminate the process forcibly. Although a process could concervanly
abort itself, the most frequent use of this call is for involuntary terminations, such
as removal of malfunctioning process from the system.
4. Fork/Join

20
Another method of process creation and termination is by means of FORK/JOIN
pair, originally introduced as primitives for multiprocessor system. The FORK
operations are used to split a sequence of instruction into two concurrently
executable sequences. JOIN is used to merge the two sequences of code divided by
the FORK and it is available to a parent process for synchronisation with a child.
5. Suspend
6. The suspend system call is also called BLOCK in some systems. The designated
process is suspended indefinitely and placed in the suspend state. A process may be
suspended itself or another process when authorized to do so.
6. Resume
The resume system call is also called WAKEUP in some systems. This call resumes
the target process, which is presumably suspended. Obviously, a suspended process
cannot resume itself because a process must be running to have its operating
system call processed. So, a suspended process depends on a partner process to
issue the resume.
7. Delay
The system call delay is also known as SLEEP. The target process is suspended for
the duration of the specified time period. The time may be expressed in terms of
system clock ticks that are system dependent and not portable or in standard time
units such as seconds and minutes. A process may delay itself or optionally, delay
some other process.
8. Get_Attributes
It is an enquiry to which the operating system responds by providing the current
values of the process attributes, or their specified subset, from the PCB.
9. Change Priority
It is an instance of a more general SET-PROCESS-ATTRIBUTES system call. Obviously,
this call is not implemented in systems where process priority is static.

Activity 2a
1) Explain the difference between a process and a thread with some examples.
2) Identify the different states a live process may occupy and show how a process
moves between these states.
3) What are the differences between a thread and a Process?

2.5. Process Scheduling


Scheduling is a fundamental operating system function. All computer resources are scheduled
before use. Since CPU is one of the primary computer resources, its scheduling is central to
operating system design. Scheduling refers to a set of policies and mechanisms supported by
operating system that controls the order in which the work to be done is completed. A
scheduler is an operating system program (module) that select the next job to be admitted for
execution. The main objective of scheduling is to increase CPU utilisation and higher
throughput. Throughput is the amount of work accomplished in a given time interval. CPU
scheduling is the basis of operating system which supports multiprogramming concepts. By
having a number of programs in computer memory at the same time, the CPU may be shared
among them. This mechanism improves the overall efficiency of the computer system by getting
more work done in less time. In this section we will describe the scheduling objectives, the

21
three types of schedulers and performance criteria that schedulers may use in maximising
system performance. Finally, at the end of the unit, we will study various scheduling algorithms.

2.5.1. Scheduling Objectives


The primary objective of scheduling is to improve performance. Various objectives of the
scheduling are as follows:
a) Maximise throughput
Scheduling should attempt to service the largest possible number of processes per unit
time.
b) Maximise Interactive User
Maximise the number of interactive user receiving acceptable response times.
c) Be Predictable
A given job should utilise the same amount of time and should cost the same regardless
of the load on the system.
d) Minimise Overhead
Scheduling should minimise the wasted resources overhead.
e) Balance Resource Use
The scheduling mechanisms should keep the resources of the system busy. Processes
that will use under utilised resources should be favoured.
f) Achieve a Balance between Response and Utilisation
The best way to guarantee good response times is to have sufficient resource available
whenever they are needed. In real time system fast responses are essential and
resource utilisation is less important.
g) Enforce Priorities
In environments in which processes are given priorities, the scheduling mechanism
should favour the higher-priority processes.
h) Give preference to processes holding key resources
Even though a low priority process may be holding a key resource, the process may be
in demand by high priority processes. If the resource is not perceptible, then the
scheduling mechanism should give the process better treatment that it would ordinarily
receive so that the process will release the key resource sooner.
i) Degrade Gracefully Under Heavy Loads
A scheduling mechanism should not collapse under heavy system load. Either it should
prevent excessive loading by not allowing new processes to be created when the load in
heavy or it should provide service to the heavier load by providing a moderately
reduced level of service to all processes.
j) Avoid Indefinite Postponement
It could be fair if all processes are treated the same and no process can suffer indefinite
postponement.

2.5.2. Types of Schedulers


If we consider batch systems, there will often be more processes submitted than the number of
processes that can be executed immediately. So, the incoming processes are spooled onto a
disk. There are three types of schedulers. They are:
a) Short Term Scheduler
The short-term scheduler selects the process for the processor among the processes which
are already in queue (in memory). The scheduler will execute quite frequently (mostly at
least once every 10 milliseconds). It has to be very fast in order to achieve a better

22
processor utilisation. The short term scheduler, like all other OS programs, has to execute
on the processor. If it takes 1 millisecond to choose a process that means (1/(10 + 1)) = 9%
of the processor time is being used for short time scheduling and only 91% may be used by
processes for execution.
b) Long Term Scheduler
The long-term scheduler selects processes from the process pool and loads selected
processes into memory for execution. The long-term scheduler executes much less
frequently when compared with the short term scheduler. It controls the degress of
multiprogramming (no. of processes in memory at a time). If the degree of
multiprogramming is to be kept stable (say 10 processes at a time), the long-term scheduler
may only need to be invoked till the process finishes execution. The long-term scheduler
must select a good process mix of I/O-bound and processor bound processes. If most of the
processes selected are I/O-bound, then the ready queue will almost be empty, while the
device queue(s) will be very crowded. If most of the processes are processor-bound, then
the device queue(s) will almost be empty while the ready queue is very crowded and that
will cause the short-term scheduler to be invoked very frequently. Time-sharing systems
(mostly) have no long-term scheduler. The stability of these systems either depends upon a
physical limitation (no of available terminals) or the self-adjusting nature of users (if you
can’t get response, you quit). It can sometimes be good to reduce the degree of
multiprogramming by removing processes from memory and storing them on disk.
c) Medium Term Scheduler
The processes of reducing the degree of multiprogramming by removing processes from
memory and storing them on disk can then be reintroduced into memory by the medium-
term scheduler. This operation is also known as swapping. Swapping may be necessary to
improve the process mix or to free memory.

2.5.3. Scheduling Criteria


The goal of scheduling algorithm is to identify the process whose selection will result in the
“best” possible system performance. There are different scheduling algorithms, which has
different properties and may favour one class of processes over another, which algorithm is
best, to determine this there are different characteristics used for comparison. The scheduling
algorithms which we will discuss in the next section, determines the importance of each of the
criteria.

In order to achieve an efficient processor management, OS tries to select the most appropriate
process from the ready queue. For selecting, the relative importance of the following may be
considered as performance criteria:
a) CPU Utilisation
The key idea is that if the CPU is busy all the time, the utilisation factor of all the
components of the system will be also high. CPU utilisation is the ration of busy time of
the processor to the total time passes for processes to finish.

b) Throughput

23
It refers to the amount of work completed in a unit of time. One way to measure
throughput is by means of the number of processes that are completed in a unit of
time. The higher the number of processes, the more work apparently is being done by
the system. But this approach is not very useful for comparison because this is
dependent on the characteristics and resource requirement of the process being
executed. Therefore, to compare throughput of several scheduling algorithms it should
be fed into the process with similar requirements. The throughput can be calculated by
using the formula:

c) Turnaround Time
It may be defined as interval from the time of submission of a process to the time of its
completion. It is the sum of the periods spent waiting to get into memory, waiting in the
ready queue, CPU time and I/O operations.

d) Waiting Time
This is the time spent in the ready queue. In multiprogramming operating system
several jobs reside at a time in memory. CPU executes only one job at a time. The rest
of jobs wait for the CPU. The waiting time may be expressed as turnaround time, less
than the actual processing time. But the scheduling algorithm affects or considers the
amount of time that a process spends waiting in a ready queue. Thus, rather than
looking at turnaround time waiting time is usually the waiting time for each process.

e) Response time
It is most frequently considered in time sharing and real time operating system.
However, its characteristics differ in the two systems. In time sharing system it may be
defined as interval from the time the last character of a command line of a program or
transaction is entered to the time the last result appears on the terminal. In real time
system it may be defined as interval from the time an internal or external event is
signaled to the time the first instruction of the respective service routine is executed.

One of the problems in designing schedulers and selecting a set of its performance
criteria is that they often conflict with each other. For example, the fastest response
time in time sharing and real time system may result in low CPU utilisation. Throughput
and CPU utilisation may be increased by executing the large number of processes, but
then response time may suffer. Therefore, the design of a scheduler usually requires

24
balance of all the different requirements and constraints. In the next section we will
discuss various scheduling algorithms.

2.6. Scheduling Algorithms


Now let’s discuss some processor scheduling algorithms again stating that the goal is to select
the most appropriate process in the ready queue.

CPU scheduling deals with the problem of deciding which of the processes in the ready queue is
to be allocated to the CPU. There are several scheduling algorithms which will be examined in
this section. A major division among scheduling algorithms is that whether they support pre-
emptive or non-preemptive scheduling discipline.
a) Preemptive Scheduling
Preemption means the operating system moves a process from running to ready
without the process requesting it. An OS implementing this algorithms switches to the
processing of a new request before completing the processing of the current request.
The preempted request is put back into the list of the pending requests. Its servicing
would be resumed sometime in the future when it is scheduled again. Preemptive
scheduling is more useful in high priority process which requires immediate response.
For example, in Real time system the consequence of missing one interrupt could be
dangerous. Round Robin scheduling, priority-based scheduling or event driven
scheduling and SRTN are considered to be the preemptive scheduling algorithms.
b) Non-Preemptive scheduling
A scheduling is non-preemptive if once a process has been allotted to the CPU, the CPU
cannot be taken away from the process. A non-preemptible discipline always processes
a scheduled request to its completion. In non-preemptive systems, jobs are made to
wait by longer jobs, but the treatment of all processes is fairer. First come First Served
(FCFS) and Shortest Job First (SJF), are considered to be the non-preemptive scheduling
algorithms.

The decision whether to schedule preemptive or not depends on the environment and the type
of application most likely to be supported by a given operating system.

Activity 2a
1. Briefly explain the following Operating System calls: a)
a. Create
b. Fork/Join
c. Delay
2. What is the Process Control Block?
3. Identify the information which must be maintained by the operating system
for each live process.

2.6.1. First-Come First-Serve (FCFS)


The simplest scheduling algorithm is First Come First Serve (FCFS). Jobs are scheduled on the
order they are received. FCFS is non-preemptive. Implementation is easily accomplished by
implementing a queue of the processes to be scheduled or by storing the time the process was
received and selecting the process with the earliest time.

25
FCFS tends to favour CPU-Bound processes. Consider a system with a CPU-bound process and a
number of I/O-bound processes. The I/O bound processes will tend to execute briefly, then
block for I/O. A CPU bound process in the ready should not have to wait long before being made
runable. The system will frequently find itself with all the I/O-Bound processes blocked and
CPU-bound process running. As the I/O operations complete, the ready Queue fill up with the
I/O bound processes.

Under some circumstances, CPU utilisation can also suffer. In the situation described above,
once a CPU-bound process does issue an I/O request, the CPU can return to process all the I/O-
bound processes. If their processing completes before the CPU-bound process’s I/O completes,
the CPU sits idle. So, with no preemption, component utilisation and the system throughput
rate may be quite low.

Example:
Calculate the turn-around time, waiting time, average turnaround time, average waiting time,
throughput and processor utilisation for the given set of processes that arrive at a given arrive
time shown in the table, with the length of processing time given in milliseconds:

If the processes arrives as per the arrival time, the Gantt chart will be:

Average around Time = Waiting time + Burst Time(Processing time)


Turn Around Time for P1 => 0+3= 3
Turn Around Time for P2 => 1+3 = 4
Turn Around Time for P3 => 3+1 = 4
Turn Around Time for P4 => 2+ 4= 6
Turn Around Time for P5 => 3+ 2= 5
Average Turn Around Time => ( 3+4+4+6+5 )/5 =>22/5 = 4.4 ms.

Waiting Time = Turnaround Time – Processing Time


Waiting time for P1 3 - 3 = 0
Waiting time for P2 4 – 3 = 1
Waiting time for P3 4 – 1 = 3
Waiting time for P4 6 – 4 = 2
Waiting time for P5 5 – 2 = 3
Average waiting time = (0+1+3+2+3)/5 = 9/5 = 1.8ms

26
Average Response Time :
Formula : Response Time = First Response - Arrival Time
Response Time of P1 = 0
Response Time of P2 => 3-2 = 1
Response Time of P3 => 6-3 = 3
Response Time of P4 => 7-5 = 2
Response Time of P5 => 11-8 =3
Average Response Time => ( 0+1+3+2+3 )/5 => 9/5 = 1.8 ms

Throughput = (No. of processes completed)/(Unit time) = 5/13 = 0.38/ms

Processor Utilisation = (Processor Busy time)/(Processor Busy Time + Processor idle Time) =
13/(13+0)=1 * 100% = 100%

2.6.2. Shortest-Job First (SJF)


This algorithm is assigned to the process that has smallest next CPU processing time, when the
CPU is available. In case of a time, FCFS scheduling algorithm can be used. It is originally
implemented in a batch-processing environment. SJF relied on a time estimate supplied with
the batch job.

Example: Consider the following set of processes with the following processing time which
arrived at the same time

Process Process Time


P1 06
P2 08
P3 07
P4 03

Using SJF scheduling because the shortest length of process will first get execution, the Gantt
chart will be:

Solution
Average Waiting Time :
Formula = Staring Time - Arrival Time
waiting Time for P1 => 3-0 = 3
waiting Time for P2 => 16-0 = 16
waiting Time for P3 => 9-0 = 9
waiting Time for P4 => 0-0=0
Average waiting time => (3+16+9+0 )/4 => 28/4 =7 ms

Average Turn Around Time :

27
Formula = waiting Time + burst Time (Process time)
Turn Around Time for P1 => 3+6 =9
Turn Around for P2 => 16+8 =24
Turn Around for P3 => 9+7 = 16
Turn Around Time for P4 => 0+3 =3
Average Turn around time => ( 9+24+16+3 )/4 => 52/4 = 13 ms

Average Response Time :


Formula : First Response - Arrival Time
First Response time for P1 =>3-0 = 3
First Response time for P2 => 16-0 = 16
First Response time for P3 => 9-0 = 9
First Response time for P4 => 0-0 = 0
Average Response Time => ( 3+16+9+0 )/4 => 28/4 = 7 ms

Throughput=> 4/24 = 0.16

Processor Utilisation=> (24/24)*100 = 100%

2.6.3. Round Robin (RR)


Round Robin (RR) scheduling is a preemptive algorithm that relates the process that has been
waiting the longest. This is one of the oldest, simplest and widely used algorithms. The round
robin scheduling algorithm is primarily used in time-sharing and a multi-user system
environment where the primary requirement is to provide reasonably good response times and
in general to share the system fairly among all system users. Basically, the CPU time is divided
into time slices.

Each process is allocated a small time-slice called quantum. No process can run for more than
one quantum while others are waiting in the ready queue. If a process needs more CPU time to
complete after exhausting one quantum, it goes to the end of ready queue to await the next
allocation. To implement the RR scheduling, Queue data structure is used to maintain the
Queue of Ready processes. A new process is added at the tail of that Queue. The CPU scheduler
picks the first process from the ready Queue, Allocate processor for a specified time Quantum.
After that time the CPU scheduler will select the next process is the ready Queue.

Example:
Consider the following set of process with the processing time given in milliseconds

If we use a time Quantum of 4 milliseconds then process P1 gets the first 4 milliseconds. Since it
requires another 20 milliseconds, it is preempted after the first time Quantum, and the CPU is

28
given to the next process in the Queue, Process P2. Since process P2 does not need and
milliseconds, it quits before its time Quantum expires. The CPU is then given to the next
process, Process P3 one each process has received 1 time Quantum, the CPU is returned to
process P1 for an additional time quantum. The Gantt chart will be:

Solution
AVERAGE TURN AROUND TIME :
FORMULA : Turn around time = waiting time + burst Time
Turn around time for P1 => 14+30 =44
Turn around time for P2 => 15+6 = 21
Turn around time for P3 => 16+8 = 24
Average turn around time => ( 44+21+24 )/3 = 29.66 ms

Average Response Time :


Formula : First Response - Arrival Time
First Response time for P1 =>3-0 = 3
First Response time for P2 => 16-0 = 16
First Response time for P3 => 9-0 = 9
First Response time for P4 => 0-0 = 0
Average Response Time => ( 3+16+9+0 )/4 => 28/7 = 7 ms

Throughput => 3/30 = 0.1

Processor Utilisation(=> 30/30%)*=100%

2.6.4. Shortest Remaining Time Next (SRTN)


This is the preemptive version of shortest job first. This permits a process that enters the ready
list to preempt the running process if the time for the new process (or for its next burst) is less
the remaining time for the running process (or for its current burst). Let us understand with the
help of an example.

Example:
Consider the set of four processes arrived as per timings described in the table:

At time 0, only process P1 has entered the system, so it is the process that executes. At time 1,
process P2 arrives. At that time, process P1 has 4 time units left to execute. At this juncture
process 2’s processing time is less compared to the P1 left out time (4 units). So P2 starts
executing at time 1. At time 2, process P3 enters the system with the processing time 5 units.
Process P2 continues executing as it has the minimum number of time units when compared

29
with P1 and P3. At time 3, process P2 terminates and process P4 enters the system. Of the
processes P1, P3 and P4.

P4 has the smallest remaining execution time so it starts executing. When process P1
terminates at time 10, process P3 executes. The Gantt chart is shown below:

Solution:
Turnaround time for each process can computed by subtracting the time it terminated from the
arrival time. Turn around Time = t(Process Completed) - t(Process Submitted) The turnaround
time for each of the processes is:

The average turnaround time is (10+2+13+3)/4 = 7 The waiting time can be computed by
subtracting processing time from turnaround time, yielding the following 4 results for the
processes as

The Average Waiting Time = (5+0+8+0) / 4 = 3.25 milliseconds Four jobs executed in 15 time
units, so throughput is 15/4 = 3.75 time units/job

2.6.5. Priority Based Scheduling or Event Driven (ED) Scheduling


A priority is associated with each process and the scheduler always picks up the highest priority
process for execution from the ready queue. Equal priority processes are scheduled FCFS. The
level of priority may be determined on the basis of resource requirements, processes
characteristics and its run time behavior.
A major problem with a priority-based scheduling is indefinite blocking of a lot priority process
by a high priority process. In general, completion of a process within finite time cannot be
guaranteed with this scheduling algorithm. A solution to the problem of indefinite blockage of
low priority process is provided by aging priority. Aging priority is a technique of gradually
increasing the priority of processes (of low priority) that wait in the system for a long time.
Eventually, the older processes attain high priority and are ensured if completion in a finite
time.

Example:
Consider the following set of five processes, assumed to have arrived at the same time with the
length of processor timing in milliseconds.

30
Using priority scheduling we would schedule these processes according to the following Gantt
chart:

Average Turn Around Time = (1+6+16+18+19) / 5 = 60/5 = 12


Average Waiting Time = (6+0+16+18+1) /5 = 8.2
Throughput = 5/19 = 0.26
Processor Utilisation = (30/30) * 100 = 100%

Priorities can be defined either internally or externally. Internally defined priorities use one
measurable quantity or quantities to complete the priority of a process.

Activity 2a
1. What is the different between preemptive dan non-preemptive scheduling.
2. Explain the algorithms of Round Robin scheduling.

2.7. Performance Evaluation of the Scheduling Algorithms


Performance of an algorithm for a given set of processes can be analysed if the appropriate
information about the process is provided. But how do we select a CPU-scheduling algorithm for
a particular system? There are many scheduling algorithms so the selection of an algorithm for a
particular system can be difficult. To select an algorithm there are some specific criteria such as:

a). Maximise CPU utilisation with the maximum response time; and
b). Maximise throughput.

Example:
Assume that we have the following five processes arrived at time 0, in the order given with the
length of CPU time given in milliseconds.

31
First consider the FCFS scheduling algorithm for the set of processes. For FCFS scheduling the
Gantt chart will be:

Average Waiting Time: (0+10+39+42+49)/5 = 28 milliseconds. Now consider the SJF scheduling,
the Gantt chart will be:

Average Waiting Time: (10+32+0+3+20)/5 = 13 milliseconds.


Now consider the Round Robin scheduling algorithm with a quantum of 10 milliseconds. The
Gantt chart will be:

32
Average Waiting Time: (10+32+20+23+40)/5 = 23 milliseconds

Now if we compare average waiting time above algorithms, we see that SJF policy results in less
than one half of the average waiting time to that of FCFS scheduling; the RR algorithm gives us
an intermediate value. So performance of algorithm can be measured when all the necessary
information is provided.

Activity 2a
1. Explain the difference between voluntary or co-operative scheduling and
preemptive scheduling. Give two examples of preemptive and of non-
preemptive scheduling algorithms.
2. Outline how different process priorities can be implemented in a scheduling
algorithm. Explain why these priorities need to be dynamically adjusted.
3. Draw the Gantt chart for the FCFS policy, considering the following set of
processes that arrive at time 0, with the length of CPU time given in
milliseconds. Also calculate the Average Waiting Time.

4. For the given five processes arriving at time 0, in the order with the length of
CPU time in milliseconds.

Consider the FCFS, SJF and RR (time slice = 10 milliseconds) scheduling


algorithms for the above set of process which algorithm would give the
minimum average waiting time?

2.8. Summary
A process is an instance of a program in execution. A process can be defined by the system or
process is an important concept in modern operating system. Processes provide a suitable
means for informing the operating system about independent activities that may be scheduled
for concurrent execution. Each process is represented by a Process Control Block (PCB). Several
PCBs can be linked together to form a queue of waiting processes. The selection and allocation
of processes is done by a scheduler. There are several scheduling algorithms. We have discussed
FCFS, SJF, RR, SJRT and priority algorithms along with the performance evaluation.

2.9. Key terms

33
Reading sources /links
1. Deital, H., M. (1984). An introduction to operating systems. Peterson: Addition Wesley
Publishing Company.
2. Dhamdhere, D., M. (2006). Operating systems: A concept-based approach. New Delhi: Tata
McGraw-Hill.
3. Madnicj, & Donovan. (1974). Operating system - Concepts and design. New York: McGraw-
Hill International Education.
4. Milenkovic, M. (2000). Operating systems: Concept and design. New York: McGrawHill
International Education.
5. Silberschatz, A., & James, L. (1985). Operating system concepts. Peterson: Addition Weslet
Publishing Company.
6. Tanenbaum, A., S. & Woodhull, A., S. (2009). Operating system design and implementation.
UK: Pearson.

3 Topic 3 Inter process Communication and Synchronisation


3.1 Introduction
In the earlier unit we have studied the concept of processes. In addition to process scheduling,
another important responsibility of the operating system is process synchronisation.
Synchronisation involves the orderly sharing of system resources by processes.

A simple batch operating system can be viewed as three processes - a reader process, an
executor process and a printer process. The reader reads cards from card reader and places
card images in an input buffer. The executor process reads card images from input buffer and
performs the specified computation and store the result in an output buffer. The printer
process retrieves the data from the output buffer and writes them to a printer. Concurrent
processing is the basis of operating system which supports multiprogramming. The operating
system supports concurrent execution of a program without necessarily supporting elaborate
form of memory and file management. This form of operation is also known as multitasking.
One of the benefits of multitasking is that several processes can be made to cooperate in order
to achieve their goals. To do this, they must do one the following:
a) Communicate
Interprocess communication (IPC) involves sending information from one process to
another. This can be achieved using a “mailbox” system, a socket which behaves like a
virtual communication network (loop back), or through the use of „pipes”, Pipes are a

34
system construction which enable one process to open another process as if it were a
file for writing or reading.
b) Share Data
A segment of memory must be available to both the processes. (Most memory is locked
to a single process).
c) Waiting
Some processes wait for other processes to give a signal before continuing, This is an
issue of synchronisation.

In order to cooperate concurrently executing processes must communicate and synchronise.


Interprocess communication is based on the use of shared variables (variables that can be
referenced by more than one process) or message passing.

Synchronisation is often necessary when processes communicate. Processes are executed with
unpredictable speeds. Yet to communicate one process must perform some action such as
setting the value of a variable or sending a message that the other detects. This only works if
the events perform an action or detect an action are constrained to happen in that order. Thus
one can view synchronisation as a set of constrainst on the ordering of events. The programmer
employs a synchronisation mechanism to delay execution of a process in order to satisfy such
constraints.

In this unit, let us study the concept of interprocess communication and synchronisation, need
of semaphores, classical problems in concurrent processing, critical regions, monitors and
message passing.

3.2 Learning Outcomes


• By the end of this unit, you should be able to:
• Identify the signifance of interprocess communication and synchronisation;
• Describe two ways of interprocess communication namely shared memory and
message passing;
• Discuss the usage of semaphores, locks and monitors in interprocess and
synchronisation; and Solve classical problems in concurrent programming.

3.3 Interprocess Communication


Interprocess communication (IPC) is a capability supported by operating system that allows one
process to communicate with another process. The processes can be running on the same
computer or on different computers connected through a network. IPC enables one application
to control another application, and for several applications to share the same data without
interfering with one another. IPC is required in all multiprogramming systems, but it is not
generally supported by single process operating systems such as DOS. OS/2 and MS-Windows
support an IPC mechanism called Dynamic Data Exchange.
IPC allows the process to communicate and to synchronise their actions without sharing the
same address space. This concept can be illustrated with the example of a shared printer as
given below.

Example:
Consider a machine with a single printer running a time-sharing operation system. If a process
needs to print its results, it must request that the operating system gives it access to the

35
printer’s device driver. At this point, the operating system must decide whether to grant this
request, depending upon whether the printer is already being used by another process. If it is
not, the operating system should grant the request and allow the process to continue;
otherwise, the operating system should deny the request and perhaps classify the process as a
waiting process until the printer becomes available. Indeed, if two processes were given
simultaneous access to the machine’s printer, the results would be worthless to both.

Consider the following related definition to understand the example in a better way:
a) Critical Resource
It is a resource shared with constraints on its use (e.g, memory, files, printers, etc).
b) Critical Exclusion
It is code that accesses a critical resource.
c) Mutual Exclusion
At most one process may be executing a critical section with respect to a particular
critical resource simultaneously.

In the example given above, the printer is the critical resource. Let’s suppose that the processes
which are sharing this resource are called process A and process B. The critical sections of
process A and process B are the section of the code which issue the print command. In order to
ensure that both processes do not attempt to use the printer at the same, they must be granted
mutually exclusive access to the printer driver. First, we consider the interprocess
communication part. There exist two complementary interprocess communication types:
a) Shared-memory system; and
b) Message-passing system.

It is clear that these two schemes are not mutually exclusive and could be used simultaneously
within a single operating system.

3.3.1. Shared-Memory System


Shared-memory systems require communication processes to share some variables. The
processes are expected to exchange information through the use of these shared variables. In a
shared-memory scheme, the responsibility for providing communication rests with the
application programmers. The operating system only needs to provide shared memory.

A critical problem occurring in shared-memory system is that two or more processes are
reading or writing some shared variables or shared data, and the final results depend on who
runs precisely and when. Such situations are called race conditions. In order to avoid race
conditions, we must find some way to prevent more than one process from reading and writing
shared variables or shared data at the same time, i.e., we need the concept of mutual exclusion
(which we will discuss in the later section). It must be sure that if one process is using a shared
variable, the other process will be excluded from doing the same thing.

3.3.2. Message-Passing System


Message passing systems allow communication processes to exchange messages. In this
scheme, the responsibility rests with the operating system itself.

The function of a message-passing system is to allow processes to communicate with each


other without the need to resort to shared variable. An interprocess communication facility

36
basically provides two operations: send (message) and receive (message). In order to send and
to receive messages, a communication link must exist between two involved processes. This link
can be implemented in different ways. The possible basic implementation questions are:
a) How are links established?
b) Can a link be associated with more than two processes?
c) How many links can there be between every pair of process?
d) What is the capacity of a link? That is, does the link have some buffer space? If so, how
much?
e) What is the size of the message? Can the link accommodate variable size or fixed-size
message?
f) Is the link unidirectional or bi-directional?

In the following, we consider several methods for logically implementing a communication link
and the send/receive operations. These methods can be classified into two categories:

a) Naming
It consists of direct and indirect communication.
i. Direct Communication
In direct communication, each process that wants to send or receive a message
must explicitly name the recipient or sender of the communication. In this case, the
send and receive primitives are defined as follows:
• Send (P, message). To send a message to the process P
• Receive (Q, message). To receive a message from process Q
This scheme shows the symmetry in addressing, i.e, both the sender and the
receiver have to name one another in order to communicate. In contrast to this, a
symmetry in addressing can be used, i.e, only the sender has to name the recipient;
the recipient is not required to name the sender. So the send and receive primitives
can be defined as follows:
• Send (P, message). To send a message to the process P
• Receive (id, message). To receive a message from any process; id is set to the
name of the process with whom the communication has taken place.
ii. Indirect Communication With indirect communication, the messages are sent to,
and received from a mailbox. A mailbox can be abstractly viewed as an object into
which messages may be placed and from which messages may be removed by
processes. In order to distinguish one from the other, each mailbox owns a unique
identification. A process may communicate with some other process by a number of
different mailboxes. The send and receive primitives are defined as follows:
• Send (A, message). To send a message to the mailbox A.
• Receive (A, message). To receive a message from the mailbox A.

Mailboxes may be owned either by a process or by the system. If the mailbox is owned
by a process, then we distinguish between the owner who can only receive from this
mailbox and user who can only send message to the mailbox. When a process that
owns a mailbox terminates, its mailbox disappears. Any process that sends a message to
this mailbox must be notified in the form of an exception that the mailbox no longer
exists. If the mailbox is owned by the operating system, then it has an existence of its

37
own, i.e, it is independent and not attached to any particular process. The operating
system provides a mechanism that allows a process to:
a. Create a new mailbox end and receive message through the mailbox; and
b. Destroy a mailbox
Since all processes with access rights to a mailbox may terminate, a mailbox may no
longer be accessible by any process after some time. In this case, the operating system
should reclaim whatever space was used for the mailbox.

b) Buffering
i. Capacity Link
A link has some capacity that determines the number of messages that can
temporarily reside in it. This propriety can be viewed as a queue of messages
attached to the link. Basically there are three ways through which such a queue can
be implemented.
• Zero capacity
This link has a message queue length of zero, i.e, no message can wait in it,
The sender must wait until the recipient receives the message. The two
processes must be synchronised for a message transfer to take place. The
zero-capacity link is referred to as a message passing system without
buffering.
• Bounded capacity
This link has a limited message queue length of n, i.e, at most n message
can reside in it. If a new message is sent, and the queue is not full, it is
placed in the queue by copying the message or by keeping a pointer to the
message and the sender should continue execution without waiting.
Otherwise, the sender must be delayed until space is available in the queue.
• Unbounded capacity
This queue has potentially infinite length, i.e, any number of messages can
wait in it. That is why the sender is never delayed.

Bounded and unbounded capacity link provide message-passing system


with automatic buffering.
iii. Messages
Messages sent by a process may be one of three varieties:
• fixed-sized,
• variablesized and
• typed messages.
If only fixed-sized messages can be sent, the physical implementation is
straightforward. However, this makes the task of programming more difficult. On
the other hand, variable-size messages require more complex physical
implementation, but the programming becomes simpler. Typed messages, i.e,
associating a type with each mailbox, are applicable only to indirect
communication. The messages that can be sent to and received from a mailbox are
restricted to the designated type.

Activity 3a

38
1. Explain the purpose of Interprocess Communication (IPC)
2. Differentiate the methods of naming between direct and indirect communication.
3. Explain the capacity link methods in buffering.

3.4 Critical Section Problem


The key to preventing trouble involving shared storage is fi nd some way to prohibit more than
one process from reading and writing the shared data simultaneously. That part of the program
where the shared memory is accessed is called the Critical Section. To avoid race conditions and
flawed results, one must identify codes in Critical Sections in each thread. The characteristic
properties of the code that form a Critical Section are:
1. Codes that reference one or more variables in a “read-update-write” fashion while any of
those variables is possibly being altered by another thread.
2. Codes that alter one or more variables that are possibly being referenced in “read-update-
write” fashion by another thread.
3. Codes use a data structure while any part of it is possibly being altered by another thread.
4. Codes alter any part of a data structure while it is possibly in use by another thread.

Here, the important point is that when one process is executing shared modifiable data in its
critical section, no other process is to be allowed to execute in its critical section. Thus, the
execution of critical sections by the processes is mutually exclusive in time.

A way of making sure that if one process is using a shared modifiable data, the other processes
will be excluded from doing the same thing.

Formally, while one process executes the shared variable, all other processes desiring to do so
at the same time moment should be kept waiting; when that process has finished executing the
shared variable, one of the processes waiting; while that process has finished executing the
shared variable, one of the processes waiting to do so should be allowed to proceed. In this
fashion, each process executing the shared data (variables) excludes all others from doing so
simultaneously. This is called Mutual Exclusion.

3.4.1. Mutual Exclusion (Mutexes)


When two or more processes must share some object, an arbitration mechanism is needed so
that they do not try to use it at the same time. The particular object being shared does not have
a great impact on the choice of such mechanisms.

Consider the following examples: Two processes sharing a printer must take turns using it; if
they attempt to use it simultaneously, the output from the two processes must be mixed into
an arbitrary jumble which is unlikely to be of any use. two processes attempting to update the

39
same bank account must take turns; if each process reads the current balance from some
database, updates it, and writes it back, one of the updates will be lost.

Mutual exclusion can be achieved by a system of locks. A mutual exclusion lock is colloquially
called a mutex.

3.4.2. Mutual Exclusion Conditions


If you could arrange matters such that no two processes were ever in their critical sections
simultaneously, you could avoid race conditions. You need four conditions to hold to have a
good solution for the critical section problem (mutual exclusion).
1. No two processes may at the same moment inside their critical sections.
2. No assumptions are made about relative speeds of processes or number of CPUs.
3. No process outside its critical section should block other processes.
4. No process should wait arbitrary long to enter its critical section.

3.4.2.1. Proposals for Achieving Mutual Exclusion


The mutual exclusion problem is to devise a pre-protocol (or entry protocol) and a post-protocol
(or exit protocol) to keep two or more threads from being in their critical sections at the same
time.

Problem: When one process is updating shared modifiable data in its critical section, no other
process should be allowed to enter in its critical section.

Proposal 1: Disabling Interrupts (Hardware Solution)


Each process disables all interrupts just after entering in its critical section and re-enable all
interrupts just before leaving critical section. With interrupts turned off the CPU could not be
switched to other process. Hence, no other process will enter its critical and mutual exclusion
achieved.

Conclusion
Disabling interrupts is sometimes a useful technique within the kernel of an operating system,
but it is not appropriate as a general mutual exclusion mechanism for users process. The reason
is that it is unwise to give user process the power to turn off interrupts.

Proposal 2: Lock Variable (Software Solution)


In this solution, you consider a single, shared, (lock) variable, initially 0. When a process wants
to enter in its critical section, it first tests the lock. If lock is 0, the process first sets it to 1 and
then enters the critical section. If the lock is already 1, the process just waits until (lock) variable
becomes 0. Thus, a 0 means that no process in its critical section, and 1 means hold your horses
- some process is in its critical section.

Conclusion
The flaw in this proposal can be best explained by example. Suppose process A sees that the
lock is 0. Before it can set the lock to 1 another process B is scheduled, runs, and sets the lock to
1. When the process A runs again, it will also set the lock to 1, and two processes will be in their
critical section simultaneously.

Proposal 3: Strict Alteration

40
In this proposed solution, the integer variable ‘turn’ keeps track of whose turn is to enter the
critical section. Initially, process A inspect turn, finds it to be 0, and enters in its critical section.
Process B also finds it to be 0 and sits in a loop continually testing ‘turn’ to see when it becomes
1. Continuously testing a variable waiting for some value to appear is called the Busy-Waiting.

Conclusion
Taking turns is not a good idea when one of the processes is much slower than the other.
Suppose process 0 fi nishes its critical section quickly, so both processes are now in their
noncritical section. This situation violates above mentioned condition 3.

Using Systems calls ‘sleep’ and ‘wakeup’


Basically, what above mentioned solution do is this: when a processes wants to enter in its
critical section, it checks to see if then entry is allowed. If it is not, the process goes into tight
loop and waits (i.e., start busy waiting) until it is allowed to enter. This approach waste CPU-
time.

Now look at some interprocess communication primitives which is the pair of sleep-wakeup.

Sleep
It is a system call that causes the caller to block, that is, be suspended until some other process
wakes it up.

Wakeup
It is a system call that wakes up the process. Both ‘sleep’ and ‘wakeup’ system calls have one
parameter that represents a memory address used to match up ‘sleeps’ and ‘wakeups.

3.5 Semaphores
The effective synchronisation tools often used to realise mutual exclusion in more complex
systems are semaphores. A semaphore S is an integer variable which can be accessed only
through two standard atomic operations: wait and signal. Semaphore is an integer variable S.
This integer variable S is initialized to the number of resources present in the system. The value
of semaphore S can be modified only by two functions wait() and signal(). The wait() and
signal() operation modifies the value of the semaphore S indivisibly. Which means when a
process is modifying the value of the semaphore, no other process can simultaneously modify
the value of the semaphore.

3.5.1. Forms of Semaphores


1. Counting Semaphores
The value of semaphore S is initialized to the number of resources present in the system.
Whenever a process wants to access the shared resources, it performs wait() operation on the
semaphore which decrements the value of semaphore by one. When it releases the shared
resource, it performs a signal() operation on the semaphore which increments the value of
semaphore by one. When the semaphore count goes to 0, it means all resources are occupied
by the processes. If a process need to use a resource when semaphore count is 0, it executes
wait() and get blocked until a process utilizing the shared resources releases it and the value of
semaphore becomes greater than 0.
2. Binary semaphore

41
The value of semaphore ranges between 0 and 1. It is similar to mutex lock, but mutex is a
locking mechanism whereas, the semaphore is a signalling mechanism. In binary semaphore, if
a process wants to access the resource it performs wait() operation on the semaphore and
decrements the value of semaphore from 1 to 0. When process releases the resource, it
performs a signal() operation on the semaphore and increments its value to 1. If the value of
the semaphore is 0 and a process want to access the resource it performs wait() operation and
block itself till the current process utilizing the resources releases the resource.

3.6. Monitors
Monitor is a software synchronization tool with high-level of abstraction that provides a
convenient and effective mechanism for process synchronization. The concept of monitors was
introduced to overcome the timing errors that occurs while using semaphores for process
synchronization. It allows only one process to be active within the monitor at a time. If other
process tries to access the shared variable in monitor, it gets blocked and is lined up in the
queue to get the access to shared data when previously accessing process releases it.

3.6.1. Semaphore vs Monitor


The basic difference between semaphore and monitor is that the semaphore is an integer
variable S which indicate the number of resources available in the system whereas, the monitor
is the abstract data type which allows only one process to execute in critical section at a time.
The value of semaphore can be modified by wait() and signal() operation only. On the other
hand, a monitor has the shared variables and the procedures only through which shared
variables can be accessed by the processes. In Semaphore when a process wants to access
shared resources the process performs wait() operation and block the resources and when it
release the resources it performs signal() operation. In monitors when a process needs to access
shared resources, it has to access them through procedures in monitor. Monitor type has
condition variables which semaphore does not have.

Activity 3a
1. What is the purpose of semaphores?
2. Explain the wait and signal operations in semaphores.
3. What are race conditions? How race conditions occur in Operating Systems?
4. What is a critical section? Explain.

3.7. Summary
Interprocess communication provides a mechanism to allow process to communicate with
other processes. Interprocess communication system is best provided by a message passing
system. Messages systems can be defined in many different ways. If there are collection of
cooperating sequential processes that share some data. Mutual exclusion must be provided.
Different algorithms are available for solving the critical section problem which we have
discussion in this topic. The bakery algorithm is used for solving the n process critical section
problem. Interprocess synchronisation provides the processes to synchronise their activities.
Semaphores can be used to solve synchronisation problems. Semaphore can only be accessed
through two atomic operations and can be implemented efficiently. The two operations are
wait and signal.

3.8. Key Terms


Bakery’s algorithm Critical sections

42
Deadlocks Dekker’s solution Dynamic data exchange
Interprocess communication (IPC) Locks Locks
Mailbox Message passing Monitor Multithreaded
Mutual exclusion Mutually exclusive Semaphores
Shared memory Synchronisation

Reading sources/links
1. Deital, H., M. (1984). An introduction to operating systems. Peterson: Addition Wesley
Publishing Company.
2. Dhamdhere, D., M. (2006). Operating systems: A concept-based approach. New Delhi:
Tata McGraw-Hill.
3. Milenkovic, M. (2000). Operating systems: Concept and design. New York: McGraw-Hill
International Education.
4. Tanenbaum, A., S., & Woodhull, A., S. (2009). Operating system design and
implementation. UK: Pearson.
5. Stalling, W. (2001). Operating system (4th ed.). New Jersey: Prentice Hall.

4 Topic 4: Deadlocks

4.1. Introduction
In a computer system, we have a finite number of resources to be distributed among a number
of competing processes. These system resources are classified in several types which may be
either physical or logical. Examples of physical resources are Printers, Tape drivers, Memory
space and CPU cycles. Examples of logical resources are Files, Semaphores and Monitors. Each
resource type can have some identical instances.
A process must request a resource before using it and release the resource after using it. It is
clear that the number of resources requested cannot exceed the total number of resources
available in the system.
In a normal operation, a process may utilise a resource only in the following sequence:
a) Request
If the request cannot be immediately granted, then the requesting process must wait
until it can get the resource.
b) Use
The requesting process can operate on the resource.
c) Release
The process releases the resource after using it.

Examples for request and release of system resources are:


a) Request and release the device;
b) Opening and closing file; and
c) Allocating and freeing the memory.

The operating system is responsible for making sure that the requesting process has been
allocated the resource. A system table indicates if each resource is free or allocated, and if
allocated, to which process. If a process requests a resource that is currently allocated to
another process, it can be added to a queue of processes waiting for this resource.

43
In some cases, several processes may compete for a fixed number of resources. A process
requests resources and if the resources are not available at that time, it enters a wait state. It
may happen that it will never gain access to the resources, since those resources are being held
by other waiting processes.

For example, assume a system with one tape drive and one plotter. Process P1 requests the
tape drive and process P2 requests the plotter. Both requests are granted. Now P1 requests the
plotter (without giving up the tape drive) and P2 requests the tape drive (without giving up the
plotter). Neither request can be granted so both processes enter a situation called the deadlock
situations.

A deadlock is a situation where a group of processes is permanently blocked as a result of each


process having acquired a set of resources needed for its completion and having to wait for the
release of the remaining resources held by others thus making it impossible for any of the
deadlocked processes to proceed.

In the earlier units, we have gone through the concept of process and the need for the
interprocess communication and synchronisation. In this unit we will study about the deadlocks,
its characterisation, deadlock avoidance and its recovery.

4.2. Learning Outcomes


By the end of this unit, you should be able to:
• Define a deadlock;
• Discuss four conditions for a deadlock;
• Explain the ways of avoiding deadlocks; and
• Describe two ways to recover from the deadlock situation.

4.3. Deadlocks
Before studying about deadlocks, let us look at the various types of resources. There are two
types of resources namely: Pre-emptable and Non-pre-emptable Resources.
a) Pre-emptable Resources
This resource can be taken away from the process with no ill effects. Memory is an
example of a pre-emptable resource.
b) Non-preemptable Resource
This resource cannot be taken away from the process (without causing ill effect). For
example, CD resources are not pre-emptable at an arbitrary moment.

Reallocating resources can resolve deadlocks that involve pre-emptable resources. Deadlocks
that involve non-preemptable resources are difficult to deal with. Let us see how a deadlock
occurs. Definition A set of processes is in deadlock state if each process in the set is waiting for
an event that can be caused by only another process in the set. In other words, each member of
the set of deadlock processes is waiting for a resource that can be released only by a deadlock
process. None of the processes can run, none of them can release any resources and none of
them can awakened. It is important to note that the number of processes and the number and
kind of resources processed and requested are unimportant.
Let us understand the deadlock situation with the help of examples.

Example 1:

44
The simplest example of deadlock is where process 1 has been allocated a non-shareable
resource A, say, a tape drive, and process 2 has been allocated a non-sharable resource B, say, a
printer. Now, if it turns out that process 1 needs resource B (printer) to proceed and process 2
needs resource A (the tape drive) to proceed and these are the only two processes in the
system, each has blocked the other and all useful work in the system stops. This situation is
termed as deadlock.
The system is in deadlock state because each process holds a resource being requested by the
other process and neither process is willing to release the resource it holds.

Example 2:
Consider a system with three disk drivers. Suppose there are three processes, each is holding
one of these three disk drives. If each process now requests another disk drive, three processes
will be in a deadlock state, because each process is waiting for the event “disk drive is released”,
which can only be caused by one of the other waiting processes. Deadlock state involves
processes competing not only for the same resource type, but also for different resource types.
Deadlocks occur most commonly in multitasking and client/server environments and are also
known as a “Deadly Embrace”. Ideally, the program that are deadlocked or the operating
system should resolve the deadlock, but this doesn’t always happen.

From the above examples. We have understood the concept of deadlocks. In the examples, we
were given some instances, but we will study the necessary conditions for a deadlock to occur,
in the next section.

4.4. Characterisation of a Deadlock


Coffman (1971) identified four necessary conditions that must hold simultaneously for a
deadlock to occur.

4.4.1. Mutual Exclusion Condition


The resources involved are non-shareable. At least one resource must be held in a non-
shareable mode, that is, only one process at a time claims exclusive control of the resource. If
another process requests that resource, the requesting must be delayed until the resource has
been released

4.4.2. Hold and Wait Condition


In this condition, a requesting process already holds resources and waiting for the requested
resources. A process, holding a resource allocated to it waits for an additional resource(s) that
is/are currently being held by other processes.

4.4.3. No-Preemptive Condition


Resources already allocated to a process cannot be preempted. Resources cannot be removed
forcibly from the processes. After completion, they will be released voluntarily by the process
holding it.

4.4.4. Circular Wait Condition


The processes in the system form a circular list or chain where each process in the list is waiting
for a resource held by the next process in the list. Let us understand this by a common example.
Consider the traffic deadlock shown in the Figure 4.1.

45
Consider each section of the street as a resource. In this situation:
a) Mutual exclusion condition applies, since only one vehicle can be on a section of the
street at a time.
b) Hold-and-wait condition applies, since each vehicle is occupying a section of the street,
and waiting to move on to the next section of the street.
c) Non-preemptive condition applies, since a section of the street that is occupied by a
vehicle cannot be taken away from it.
d) Circular wait condition applies, since each vehicle is waiting for the next vehicle to
move. That is, each vehicle in the traffic is waiting for a section of the street held by the
next vehicle in the traffic.

The simple rule to avoid traffic deadlock is that a vehicle should only enter an intersection if it
assured that it will not have to stop inside the intersection. It is not possible to have a deadlock
involving only one single process. The deadlock involves a circular “hold-and-wait” condition
between two or more processes, so “one” process cannot hold a resource, yet be waiting for
another resource that it is holding. In addition, deadlock is not possible between two threads,
that is, each thread has access to the resources held by the process.

4.5. A Resource Allocation Graph


The idea behind the resource allocation graph is to have a graph which has two different types
of nodes, the process nodes and resource nodes (process represented by circles, resource node
represented by rectangles). For different instances of a resource, there is a dot in the resource
node rectangle. For example, if there are two identical printers, the printer resource might have
two dots to indicate that we don’t really care which is used, as long as we acquire the resource.
The edges among these nodes represent resource allocation and release. Edges are directed,
and if the edge goes from resource to process node that means the process has acquired the
resource. If the edge goes from process node to resource node that means the process has
requested the resource.

We can use these graphs to determine if a deadline has occurred or may occur. If for example,
all resources have only one instance (all resource node rectangles have one dot) and the graph
is circular, then a deadlock has occurred. If on the other hand some resources have several
instances, then a deadlock may occur. If the graph is not circular, a deadlock cannot occur (the

46
circular wait condition wouldn’t be satisfied). The following are the tips which will help you to
check the graph easily to predict the presence of cycles:
a) If no cycle exists in the resource allocation graph, there is no deadlock.
b) If there is a cycle in the graph and each resource has only one instance, then there is a
deadlock. In this case, a cycle is a necessary and sufficient condition for deadlock.
c) If there is a cycle in the graph, and each resource has more than one instance, there
may or may not be a deadlock. (A cycle may be broken if some process outside the cycle
has a resource instance that can break the cycle). Therefore, a cycle in the resource
allocation graph is a necessary but not sufficient condition for deadlock, when multiple
resource instances are considered.

Figure 4.2: Resource allocation graph showing deadlock

Figure 4.3: Resource allocation graph having a cycle and not in a deadlock

The above graph shown in Figure 4.3 has a cycle and is not in deadlock.
(Resource 1 has one instance shown by a star)
(Resource 2 has two instances a and b, shown as two stars)

47
If P1 finishes, P2 can get R1 and finish, so there is no deadlock.

4.6. Dealing with Deadlock Situations


There are possible strategies to deal with deadlocks. They are:
a) Deadlocks prevention;
b) Deadlock avoidance; and
c) Deadlock detection and recovery.

Let’s examine each strategy one by one to evaluate their respective strengths and weaknesses.

4.6.1. Deadlock Prevention


Havender in his pioneering work showed that since all four of the conditions are necessary for
deadlock to occur, it follows that deadlock might be prevented by denying any one of the
conditions. Let us study Havender’s algorithm.
a) Havender’s Algorithm

i. Elimination of “Mutual Exclusion” Condition


The mutual exclusion condition must hold for non-shareable resources. That is,
several processes cannot simultaneously share a single resource. This condition is
difficult to eliminate because some resources, such as the tap driver and printer,
are inherently non-shareable. Note that shareable resources like read-only-file do
not require mutually exclusive access and thus cannot be involved in deadlock.

ii. Elimination of “Hold and Wait” Condition There are two possibilities for the
elimination of the second condition. The first alternative is that a process request
be granted all the resources it needs at once, prior to execution. The second
alternative is to disallow a process from requesting resources whenever it has
previously allocated resources. This strategy requires that all the resources a
process will need must be requested at once. The system must grant resources on
“all or none” basis. If the complete set of resources needed by a process is not
currently available, then the process must wait until the complete set is available.
While the process waits, however, it may not hold any resources. Thus the “wait
for” condition is denied, and deadlocks simply cannot occur. This strategy can lead
to serious waste of resources.

For example, a program requiring ten tap drives must request and receive all ten
drives before it begins executing. If the program needs only one tap drive to begin
execution and then does not need the remaining tap drives for several hours then
substantial computer resources (nine tape drives) will sit idle for several hours. This
strategy can cause indefinite postponement (starvation), since not all the required
resources may become available at once.

iii. Elimination of “Non-preemption” Condition The non-preemption condition can be


alleviated by forcing a process waiting for a resource that cannot immediately be
allocated, to relinquish all of its currently held resources, so that other processes

48
may use them to finish their needs. Suppose a system does allow processes to hold
resources while requesting additional resources. Consider what happens when a
request cannot be satisfied. A process holds resources a second process may need
in order to proceed, while the second process may hold the resources needed by
the first process. This a deadlock. This strategy requires that when a process that is
holding some resources is denied a request for additional resources, the process
must release its held resources and, of necessary, request them again together with
additional resources. Implementation of this strategy denies the “no-preemptive”
condition effectively.

b) High Cost When a process releases resources, the process may lose all its work to that
point. One serious consequence of this strategy is the possibility of indefinite
postponement (starvation). A process might be held off indefinitely as it repeatedly
requests and releases the same resources.

i. Elimination of “Circular Wait” Condition The last condition, the circular wait, can be
denied by imposing a total ordering on all of the resource types and then forcing all
processes to request the resources in order (increasing or decreasing). This strategy
imposes a total ordering of all resource types and requires that each process
requests resources in a numerical order of enumeration. With this rule, the
resource allocation graph can never have a cycle. For example, provide a global
numbering of all the resources, as shown in the given Table 4.1.

Now, we will see the rule for this:

Rule: Processes can request resources whenever they want to, but all requests must
be made in numerical order. A process may request first printer and then a tape
drive (order: 2,4), but it may not request first a plotter and then a printer (order:
3,2). The problem with this strategy is that may be impossible to find an ordering
that satisfies everyone.
This strategy, if adopted, may result in low resource utilisation and in some cases
starvation is possible too.

4.6.2. Deadlock Avoidance


This approach to the deadlock problem anticipates a deadlock before it actually occurs. This
approach employs an algorithm to access the possibility that deadlock could occur and act

49
accordingly. This method differs from deadlock prevention, which guarantees that deadlock
cannot occur by denying one of the necessary conditions of deadlock. The most famous
deadlock avoidance algorithm, from Dijkstra (1965), is the Banker’s algorithm. It is named as
Banker’s algorithm because the process is analogous to that used by a banker in deciding if a
loan can be safely made a not.

The Banker’s Algorithm is based on the banking system, which never allocates its available cash
in such a manner that it can no longer satisfy the needs of all its customers. Here we must have
the advance knowledge of the maximum possible claims for each process, which is limited by
the resource availability. During the run of the system we should keep monitoring the resource
allocation status to ensure that no circular wait condition can exist.
If the necessary conditions for a deadlock are in place, it is still possible to avoid deadlock by
being careful when resources are allocated. The following are the features that are to be
considered for avoidance of the deadlock as per the Banker’s Algorithms.

a) Each process declares maximum number of resources of each type that it may need;
b) Keep the system in a safe state in which we can allocate resources to each process in
some order and avoid deadlock;
c) Check for the safe state by finding a safe sequence: <Pi, P2,...,Pn where resources that
Pi needs can be satisfied by available resources plus resources held by Pj where j < i;
and
d) Resource allocation graph algorithm uses claim edges to check for a safe state.

The resource allocation state is now defined by the number of available and allocated
resources, and the maximum demands of the processes. Subsequently the system can be in
either of the following states:
a) Safe State
Such a state occurs when the system can allocate resources to each process (up to its
maximum) in some order and avoid a deadlock. This state will be characterised by a safe
sequence. It must be mentioned here that we should not falsely conclude that all
unsafe states are deadlocked although it may eventually lead to a deadlock.
b) Unsafe State
If the system did not follow the safe sequence of resource allocation from the beginning
and it is now in a situation, which may lead to a deadlock, then it is in an unsafe state.
c) Deadlock State
If the system has some circular wait condition existing for some processes, then it is in
deadlock state.

Let us study this concept with the help of an example as shown below: Consider an analogy in
which four processes (P1, P2, P3 and P4) can be compared with the customers in a bank,
resources such as printers etc. as cash available in the bank and the operating system as the
Banker.

50
Let us assume that total available resources = 10 In the above table, we see four processes,
each of which has been granted a number of maximum resources that can be used. The
operating system reserved only 10 resources rather than 22 units to service them. At a certain
moment, the situation becomes:

Available resources = 2
Safe State: The key to a state being safe is that there is at least one way for all users to finish. In
other words, the state of Table 4.2 is safe because with 2 units left, the operating system can
delay any request except P3, thus letting P3 finish and release all four resources. With four units
in hand, the Operating system can let either P4 or P2 have the necessary units and so on.
Unsafe State: Consider what would happen if a request from P2 for one more unit was granted
in Table 4.3. We would have following situation as shown in Table 4.4.

Available resources = 1
This is an unsafe state.

51
If all the processes request for their maximum resources respectively, then the operating
system could not satisfy any of them and we would have a deadlock.

Important Note: It is important to note that an unsafe state does not imply the existence or
even the eventual existence of a deadlock. What an unsafe state does imply is simply that some
unfortunate sequence of events might lead to a deadlock.

The Banker’s algorithm is thus used to consider each request as it occurs and see if granting it
leads to a safe state. If it does, the request is granted, otherwise, it is postponed until later.
Haberman [1969] has shown that executing of the algorithm has a complexity proportional to
N2 where N is the number of processes and since the algorithm is executed each time a
resource request occurs; the overhead is significant.

Limitations of the Banker’s Algorithm


There are some problems with the Banker’s algorithm as given below:
a) It is time consuming to execute on the operation of every resource.
b) If the claim information is not accurate, system resources may be underutilised.
c) Another difficulty can occur when a system is heavily loaded. Lauesen states that in this
situation “so many resources are granted away that very few safe sequences remain,
and as a consequence, the jobs will be executed sequentially”. Therefore, the Banker’s
algorithm is referred to as the “Most Liberal” granting policy: that is, it gives away
everything that it can without regard to the consequences.
d) New processes arriving may cause a problem.
i. The process’s claim must be less than the total number of units of the resource in
the system. If not, the process is not accepted by the manager.
ii. Since the state without the new process is safe, so is the state with the new
process. Just use the order you had originally and put the new process at the end.
iii. Ensuring fairness (starvation freedom) needs a little more work, but isn’t too hard
either (once every hour stop taking new processes until all current processes finish)
e) A resource becoming unavailable (e.g. a tape drive breaking), can result in an unsafe
state.

4.6.3. Deadlock Detection and Recovery


Detection of deadlocks is the most practical policy, which being both liberal and cost efficient
most operating systems deploy. To detect a deadlock, we must go about in a recursive manner
and simulate the most favoured execution of each unblocked process:
a) An unblocked process may acquire all the needed resources and will execute;
b) It will then release all the acquired resources and remain dormant thereafter;
c) The now released resources may wake up some previously blocked process;
d) Continue the above steps as long as possible; and
e) If any blocked processes remain, they are deadlocked.

Recovery from Deadlock:


a) Recovery by Process Termination.
In this approach we terminate deadlocked processes in a systematic way taking into
account their priorities. The moment, enough processes are terminated to recover from

52
deadlock, we stop the process terminations. Though the policy is simple, there are
some problems associated with it.

Consider the scenario where a process is in the state of updating a data file and it is
terminated. The file may be left in an incorrect state by the unexpected termination of
the updating process. Further, processes should be terminated based on some
criterion/policy. Some of the criteria may be as follows:
(i) Priority of a process;
(ii) CPU time used and expected usage before completion;
(iii) Number and type of resources being used (can they be pre-empted easily?);
(iv) Number of resources needed for completion;
(v) Number if processes needed to be terminated; and
(vi) Are the processes interactive or batch?

b) Recovery by Checkpointing and Rollback (Resource Pre-emption)


Some systems facilitate deadlock recovery by implementing checkpointing and rollback.
Checkpointing is saving enough state of a process so that the process can be restarted
at the point in the computation where the checkpoint was taken. Autosaving file edits
are a form of checkpointing. Checkpointing costs depend on the underlying algorithm.
Very simple algorithm (like linear primality testing) can be checkpointed with a few
words of data. More complicated processes may have to save all the process state and
memory.

If a deadlock is detected, one or more processes are restarted from their last
checkpoint. Restarting a process from a checkpoint is called rollback. It is done with the
expectation that the resource requests will not interleave again to produce deadlock.

Deadlock recovery is generally used when deadlocks are rare, and the cost of recovery
(process termination or rollback) is low.

Process checkpointing can also be used to improve reliability (long running


computations), assist in process migration, or reduce startup costs.
Activity 4a
1. What is the purpose of deadlock?
2. What is the different between process nodes and resource nodes?
3. What is a deadlock and what are the four conditions that will create the
deadlock situation?
4. How can deadlock be avoided? Explain with the help of an example.

4.7. Summary
A deadlock occurs a process has some resource and is waiting to acquire another resource,
while that resource is being held by some process that is waiting to acquire the resource that is
being held by the first process. A deadlock needs four conditions to occur: Mutual exclusion,
hold and wait, non-preemption and circular waiting. We can handle deadlocks in three major
ways: We can prevent them, handle them when we detect them, or simply ignore the whole
deadlock issue altogether.

4.8. Key Terms

53
Banker’s algorithm Checkpointing
Circular waiting Deadlocks
Havender’s algorithm Hold-and-wait
Mutual exclusion Non-preemption Rollback

Reading sources/links
1. Deital, H., M. (1984). An introduction to operating systems. Peterson: Addition Wesley
Publishing Company.
2. Dhamdhere, D., M. (2006). Operating systems: A concept-based approach. New Delhi: Tata
McGraw-Hill.
3. Madnicj, & Donovan. (1974). Operating system - Concepts and design. New York: McGraw-
Hill International Education.
4. Milenkovic, M. (2000). Operating systems: Concept and design. New York: McGrawHill
International Education.
5. Silberschatz, A., & James, L. (1985). Operating system concepts. Peterson: Addition Weslet
Publishing Company.
6. Tanenbaum, A., S., & Woodhull, A., S. (2009). Operating system design and implementation.
UK: Pearson.
5 Topic 5: Memory Management

5.5. Introduction
In the previous units, we have studied about introductory concepts of the OS, process
management and deadlocks. In this unit, we will go through another important function of the
Operating System - the memory management.
Memory is central to the operation of a modern computer system. Memory is a large array of
words or bytes, each location with its own address. Interaction is achieved through a sequence
of reads/writes of specific memory address. The CPU fetches from the program from the hard
disk and stores in memory. If a program is to be executed, it must be mapped to absolute
addresses and loaded into memory.

In a multiprogramming environment, in order to improve both the CPU utilisation and the
speed of the computer’s response, several processes must be kept in memory. There are many
different algorithms depending on the particular situation to manage the memory. Selection of
a memory management scheme for a specific system depends upon many factors, but
especially upon the hardware design of the system. Each algorithm requires its own hardware
support.

The Operating System is responsible for the following activities in connection with memory
management:
a) Keep track of which parts of memory are currently being used and by whom;
b) Decide which processes are to be loaded into memory when memory space becomes
available; and
c) Allocate and deallocate memory space as needed.

In the multiprogramming environment operating system dynamically allocates memory to


multiple processes. Thus memory plays a significant role in the important aspects of computer
system like performance, S/W support, reliability and stability. Memory can be broadly

54
classified into two categories the primary memory (like cache and RAM) and the secondary
memory (like magnetic tape, disc. etc.). The memory is a resource that needs effective and
efficient management. The part of OS that perform this vital task of memory management is
known as memory manager.

In multiprogramming system, as available memory is shared among number of process, so the


allocation speed and the efficient memory utilisation (in terms of minimal overheads and
reuse/relocation of released memory block) are of prime concern. Protection is difficult to
achieve with relocation requirement, as location of process and absolute address in memory is
unpredictable. But at run-time, it can be done. Fortunately, we have mechanisms supporting
protection like processor (hardware) support that is able the instructions violating protection
and trying to interrupt other processes.

5.6. Learning Outcomes


By the end of this unit, you should be able to:
• Describe the various activities handled by the operating system while performing the
memory management function;
• Explain how to allocate the processes of memory management; Explain how to
reallocate when processes of memory management terminated;
• Identify the logical and physical memory organisation;
• Discuss the memory protection against unauthorised access and sharing;
• Manage swapping between main memory and disk in case main storage is small to hold
all processes;
• Summarise the principles of memory management as applied to paging and
segmentation; Compare and contrast paging and segmentation techniques; and
• Analyse the various memory portioning/partitioning techniques including overlays,
swapping, and placement and replacement policies.

5.7. Overlays and Swapping


Usually, programs reside on a disk in the form of executable files and for this execution they
must be brought into memory and must be placed within a process. Such programs form the
ready queue. In general scenario, processes are fetched from ready queue, loaded into memory
and then executed. During these stages, address must be represented in different ways like in
source code addresses or in symbolic from (ex. LABEL). Compiler will bind this symbolic address
to relocatable addresses (for example, 16 bytes from base address or start of module). The
linking editor will bind these relocatable addresses to absolute addresses. Before we learn a
program is memory we must bind the memory addresses that the program is going to use.
Binding is basically assigning which address the code and data are going to occupy. You can bind
at compile-time, load-time or execution time and explanation as follows:
a) Compile-time
If memory location is known a priori, absolute code can be generated.
b) Load-time
If it is known, it must generate relocatable at complete time.
c) Execution-time
Binding is delayed until run-time; process can be moved during its execution. We need
H/W support for address maps (base and limit registers).

55
For better memory utilisation all modules can be kept on disk in a relocatable format and only
main program is loaded into memory and executed. Only on need the other routines are called,
loaded and address is updated. Such scheme is called dynamic loading, which is user’s
responsibility rather than OS. But Operating System provides library routines to implement
dynamic loading.

In the above discussion, we have seen that entire program and its related data is loaded in
physical memory for execution. But what if process is large than the amount of memory
allocated to it? We can overcome this problem by adopting a technique called as Overlays. Like
dynamic loading, overlays can also be implemented by users without OS support. The entire
program or application is divided into instructions and data sets such that when one instruction
set needed it is loaded in memory and after its execution is over, the space is released. Such
instructions can be called as overlays, which are loaded and unloaded by the program.

Definition: An overlay is a part of an application, which has been loaded at same origin where
previously some other part(s) of the program was residing.
A program based on overlap scheme mainly consists of following:
a) A roof piece which is always memory resident; and
b) Sort of overlays.

Overlay gives the program a way to extend limited main storage. An important aspect related to
overlays identification in program is concept of mutual exclusion i.e., routines which do not
invoke each and not loaded in memory simultaneously. For example, suppose total available
memory is 140K. Consider a program with four subroutines named as: Read ( ),Funtion1 (
),Funtion2 ( ) and Display ( ). First, Read is invoked that reads a set of data. Based on this data
set values, conditionally either one of routine Function1 or Funtion2 is called. And then Display
is called to output results. Here, Funtion1 and Funtion2 are manually exclusive and are not
required simultaneously in memory. The memory requirement can be shown as in Figure 5.1.

Figure 5.1: Example of overlay

Without the overlay it requires 180K of memory and with the overlay support memory
requirement is 130K. Overlay manager/driver is responsible for loading and unloading on
overlay segment as per requirement. But this scheme suffers from following limitations:

a) Require careful and time-consuming planning;


b) Programmer is responsible for organising overlay structure of program with the help of
file structures etc. and also to ensure that piece of code is already loaded when it is
called; and
c) Operating System provides the facility to load files into overlay region.

56
Swapping
Swapping is an approach for memory management by bringing each process in entirety, running
it and then putting it back on the disk, so that another program may be loaded into that space.
Swapping is a technique that lets you use a disk file as an extension of memory. Lower priority
user processes are swapped to backing store (disk) when they are waiting for I/O or some other
event like arrival of higher priority processes. This is Rollout Swapping. Swapping the process
back into store when some event occurs or when needed (may be in a different partition) is
known as Roll-in swapping. Figure 5.2 depicts techniques of swapping.

Figure 5.2: Swapping

Major benefits of using swapping are:


(a) Allows higher of multiprogramming;
(b) Allows dynamic relocation. i.e., if address binding at execution time is being used we
can swap in different location else in case of compile and load time bindings processes
have to be moved to same location only;
(c) Better memory utilisation;
(d) Less wastage of CPU time on compaction; and
(e) Can easily be applied on priority-based scheduling algorithms to improve performance.

Through swapping has these benefits but it has few limitations also like entire program must be
resident in store when it is executing. Also processes with changing memory requirements will
need to issue system calls for requesting and releasing memory. It is necessary to know exactly
how much memory a user process is using and also that it is blocked or swapping for I/O.

Transfer time = 100K/ 1,000 = 1/10 seconds = 100 milliseconds


Access time = 10 milliseconds
Total time = 110 milliseconds
As both the swap out and swap in should take place, the total swap time is then about 220
milliseconds (above time is doubled). A round robin CPU scheduling should have a time slot size
much larger relative to swap time of 220 milliseconds. Also if process is not utilising memory
space and just waiting for I/O operation or blocked, it should be swapped.

5.8. Logical and Physical Address Space


The computer interacts via logical and physical addressing to map memory. Logical address is
the one that is generated by CPU, also referred to as virtual address. The program perceives this
address space. Physical address is the actual address understood by computer hardware i.e.,
memory unit. Logical to physical address translation is taken care by the Operating System. The
term virtual memory refers to the abstraction of separating LOGICAL memory (i.e., memory as

57
seen by the process) from PHYSICAL memory (i.e., memory as seen by the processor). Because
of this separation, the programmer needs to be aware of only the logical memory space while
the operating system maintains two or more levels of physical memory space.

Activity 5a
1. Explain the concept of overlays.
2. What is meant by swapping?

In compile-time and load-time address binding schemes these two tend to be the same. These
differ in execution-time address binding scheme and the MMU Memory Management Unit
(MMU) handles translation of these addresses.
Definition: MMU (as shown in the Figure 5.3) is a hardware device that maps logical address to
the physical address. It maps the virtual address to the real store location. The simple MMU
scheme adds the relocation register contents to the base address of the program that is
generated at the time it is sent to memory.

Figure 5.3: Role of MMU

5.9. Single Process Monitor (Monoprogramming)


In the simplest case of single-user system everything was easy as at a time was just one process
in memory and no address translation was done by the operating system dynamically during
execution. Protection of OS (or part of it) can be achieved by keeping it in ROM. We can also
have a separate space only accessible in supervisor mode as show in Figure 5.4.

Figure 5.4: Single partition system


The user can employ overlays if memory requirement by a program exceeds the size of physical
memory. In this approach only one process at a time can be in running state in the system.
Example of such system is MS-DOS which is a single tasking system having a command
interpreter. Such an arrangement is limited in capability and performance.

5.10. Contiguous Allocation Methods


In a practical scenario Operating System could be divided into several categories:
(a) Single process system; and
(b) Multiple process system with two types: Fixed partition memory and variable partition
memory.

58
Figure 5.5 shows the hierarchical of memory allocation.

Figure 5.5: The hierarchical of memory allocation


Further we will learn these schemes in next section.

Partitioned Memory Allocation:

The concept of multiprogramming emphasises on maximising CPU utilisation by overlapping


CPU and I/O. Memory may be allocated as:
(a) Single large partition for processes to use; or
(b) Multiple partitions with a single process a single partition.

5.10.1. Single-partition System


The approach keeps the Operating System in the lower part of the memory and other user
processes in the upper part. With this scheme, Operating System can be protected from
updating in user processes. Relocation-register scheme known as dynamic relocation is useful
for this purpose. It not only protects user processes from each other but also from changing OS
code and data. Two registers are used: relocation register, contains value of the smallest
physical address and limit register, contains logical addresses range. Both these are set by
Operating System when the job starts. At load time of program (i.e., when it has to be
relocated) we must establish “addressability” by adjusting the relocation register contents to
the new starting address for the program. The scheme is shown in Figure 5.6.

Figure 5.6: Dynamic relocation


The contents of a relocation register are implicitly added to any address references generated
by the program. Some systems use base registers as relocation register for easy addressability
as these are within programmer’s control. Also, in some systems relocation is managed and
accessed by Operating System only. To summarise this, we can say, in dynamic relocation

59
scheme if the logical address space range is 0 to Max the physical address space range is R+0 to
R+Max (where R is relocation register contents). Similarly, a limit resister is checked by
hardware to be sure that logical address generated by CPU is not bigger than size of the
program.

5.10.2. Multiple–partition System: Fixed-sized Partition


This is also known as static partitioning schemes as shown in Figure 5.7. Simple memory
management scheme is to divide memory into n (possible unequal) fixed-sized partitions, each
of which can hold exactly one process. The degree of multiprogramming is dependent on the
number of partitions. IBM used this scheme for system 360 OS/MFT (Multiprogramming with a
fixed number of tasks). The partition boundaries are not movable (must reboot to move a job).
We can have one queue per partition or just a single queue for all the partitions.

Figure 5.7: Multiple partition system

Initially, whole memory is available for user processes and is like large block of available
memory. Operating System keeps details of available memory block and occupied blocks in
tabular form. OS also keeps track on memory requirements of each process. As processes enter
into the input queue and when sufficient space for it is available, process is allocated space and
loaded. After its execution is over it releases its occupied and OS fills this space with other
processes in input queue. The block of available memory is known as a Hole. Hole of various
sizes are scattered throughout the memory. When any process arrives, it is allocated memory
from a hole that is large enough to accommodate it. This example is shown in Figure 5.8.

Figure 5.8: Fixed-sized partition scheme

If a hole is too large, it is divided into two parts:


(a) One that is allocated to next process of input queue; and
(b) Added with set of holes.

60
Within a partition if two holes are adjacent then they can be merged to make a single large
hole. But this scheme suffers from fragmentation problem. Storage fragmentation occurs either
because the user processes do not completely accommodate the allocated the allotted partition
or partition remains unused, if it is too small to hold any process form input queue. Main
memory utilisation is extremely inefficient.
Any program, no matter how small, occupies entire partition. In our example, process B takes
150K of partition2 (200K) size). We are left with 50K sized hole. This phenomenon, in which
there is wasted space internal to a partition, is known as internal fragmentation. It occurs
because initially process is loaded in partition that is large enough to hold it (i.e., allocated
memory that is internal to a partition, but is not in use

Variable-sized Partition:
This scheme is also known as dynamic partitioning. I this scheme, boundaries are not fixed.
Processes accommodate memory according to their requirement. There is no wastage as
partition size is exactly same as the size of the user process. Initially when processes start this
wastage can be avoided but later on when they terminate they leave holes in the main storage.
Other processes can accommodate these, but eventually they become too small to
accommodate new jobs as shown in Figure 5.9.

Figure 5.9: Variable sized partitions

IBM used this technique for OS/MVT (Multiprogramming with a Variable number of Tasks) as
the partitions are of variable length and number. But still fragmentation anomaly exists in this
scheme. As time goes on and processes are loaded and removed from memory, fragmentation
increase and memory utilisation declines. This wastage of memory, which is external to
partition, is known as external fragmentation. In this, though there is enough total memory to
satisfy a request but as it is not contiguous and it is fragmented into small holes, that can’t be
utilised.

External fragmentation problem can be resolved by coalescing holes and storage compaction.
Coalescing holes is process of merging existing hole adjacent to a process that will terminate
and free its allocated space. Thus, new adjacent holes and existing holes can be viewed as a
single large hole and can be efficiently utilised. There is another possibility that holes are
distributed throughout the memory. For utilising such scattered, shuffle all occupied areas of
memory to one end and leave all free memory space as a single large block, which can further
be utilised. This mechanism is known as Storage Compaction, as shown in Figure 5.10.

61
Figure 5.10: Storage compaction

But storage compaction also has its limitations as shown below:


(a) It requires extra overheads in terms of resource utilisation and large response time.
(b) Compaction is required frequently because jobs terminate rapidly. This enhances
system resource consumption and makes compaction expensive.
(c) Compaction is possible only if dynamic relocation is being used (at run-time). This is
because the memory contents that are shuffled (i.e., relocated and executed in new
location require all internal addresses to be relocated.

In a multiprogramming system memory is divided into a number of fixed size or variable sized
partitions or regions, which are allocated to running process. For example: a process needs m
words of memory may run in a partition of n words where n is greater than or equal to m. The
variable size partition scheme may result in a situation where available memory is not
contiguous, but fragmentation and external fragmentation. The difference (n-m) is called
internal fragmentation, memory which is internal to a partition but is not being use. If a
partition is unused and available, but too small to be used by any waiting process, then it is
accounted for external fragmentation. These memory fragments cannot be used.
In order to solve this problem, we can either compact the memory making large free memory
blocks or implement paging scheme which allows a program’s memory to be non-contiguous,
thus permitting a program to be allocated physical memory wherever it is available.

Activity 5b
1. What are the four important tasks of a memory manager?
2. What are the three tricks used to resolve absolute addresses?
3. What are the problems that arise with absolute addresses in terms of
swapping?
4. Briefly explain:
(a) Single-partition system
(b) Multiple-partition system
5. List three limitations of the storage compaction.

5.11. Paging
We will see the principles of operation of the paging in the next section. Paging scheme solves
the problem faced in variable sized partitions like external fragmentation.

5.11.1. Principles of Operation

62
In a paged system, logical memory is divided into a number of fixed sizes chunks’ called pages.
The physical memory is also pre-divided into same fixed sized blocks (as is the size of pages)
called page frames. The pages sizes (also the frame sizes) are always powers of 2 and vary
between 512 bytes to 8192 bytes per page. The reason behind this is implementation of paging
mechanism using page offset. This is discussed in detail in the following sections. Each process
page is loaded to some memory frame. These pages can be loaded into contiguous frames in
memory or into non-contiguous frames also as shown in Figure 5.11. The external
fragmentation is alleviated since processes are loaded into separate holes.

Figure 5.11: Principle of operation of paging


5.11.2. Page Allocation
In variable sized partitioning of memory every time when a process of sizes n is to be loaded, it
is important to know the best location from the list of available/free holes. This dynamic storage
allocation is necessary to increase efficiency and throughput of system. Most commonly used
strategies to make such selection are:
(a) Best-fit Policy
Allocating the hole in which the process firs most „tightly” i.e., the difference between
the hole size and the process size is the minimum one.
(b) First-fit Policy
Allocating the hole first available hole (according to memory order), which is big enough
to accommodate the new process.
(c) Worst-fit Policy
Allocating the largest hole that will leave maximum amount of unused space i.e.,
leftover space is maximum after allocation.

Now, question arises which strategy is likely to be used? In practice, best-fit and first-fit are
better than worst-fit. Both these are efficient in terms of time and storage requirement. Best-fit
on the other hand requires least overheads in its implementation because of its simplicity.
Possibly worst-fit also sometimes leaves large holes that could further be used to accommodate
other processes. Thus, all these policies have their own merits and demerits.

5.11.3. Hardware Support for Paging


Every logical page in paging scheme is divided into two parts:
(a) A page numbering (p) in logical address space; and
(b) The displacement (or offset) in page p at which item resides (i.e., from start of page).

63
This is known as Address Translation scheme. For example, a 16-bit address can be divided as
given in Figure 5.12.

Figure 5.12: Example of a16-bit address

Here, as page number takes 5bits, so range of values is 0 to 31(i.e. 25 - 1). Similarly, offset value
uses 11-bits, so range is 0 to 2023 (i.e., 211 - 1). Summarizing this we can say paging scheme
uses 32 pages, each with 2024 locations.
The table, holds virtual address to physical address translations, is called the page table. As
displacement is constant, so only translation of virtual page number to physical page is
required. This can be seen diagrammatically in Figure 5.12.

Page number is used as an index into a page table and the latter contains base address of each
corresponding physical memory page number (Frame). This reduces dynamic relocation efforts.
The Paging hardware support is shown diagrammatically in Figure 5.13.

Figure 5.13: Address translation scheme

Figure 5.14: Direct mapping

Paging Address Translation by Direct mapping

64
This is the case of direct mapping as page table sends directly to physical memory page. This is
shown in Figure 5.14. But disadvantage of this scheme is its speed of translation. This is because
page table is kept in primary storage and its size can be considerably large which increases
instruction execution time (also access time) and hence decreases system speed. To overcome
this additional hardware support of registers and buffers can be used. This is explained in next
section.

Paging Address Translation with Associative Mapping


This scheme is based on the use of dedicated registers with high speed and efficiency. These
small, fast-lookup cache help to place the entire page table into a content-addresses associative
storage, hence speed-up the lookup problem with a cache. These are known as associative
registers or Translation Look-aside Buffers (TLB’s). Each register consists of two entries:
(a) Key, which is matched with logical page p; and
(b) Value which return page frame number corresponding to p.

It is similar to direct mapping scheme but here as TLB’s contain only few page table entries, so
search is fast. But it is quite expensive due to register support. So, both direct and associative
mapping scheme can also be combined to get more benefits. Here, page number is matched
with all associative registers simultaneously, the percentage of the number of times the page is
found in TLB’s is called hit ration. If it is not found, it is searched in page table and added into
TLB. But if TLB is already full then page replacement policies can be used. Entries in TLB can be
limited only. This combined scheme is shown in Figure 5.15.

Figure 5.15: Combined associative/direct mapping

5.11.4. Protection and Sharing


Paging hardware typically also contains some protection mechanism. In page table
corresponding to each frame a protection bit is association. This bit can tell if page is read-only
or read-write. Sharing code and data takes place if two-page table entries in different processes
point to same physical page, the processes share the memory. If one process writes the data
other process will see the changes. It is a very efficient way to communicate. Sharing must also

65
be controlled to protect modification and accessing data in one process by another process. For
this program are kept separately as procedures and data, share procedures and data that are
non-modifiatable (pure/reentrant code) can be shared. Reentrant code cannot modify itself and
must make sure that it has a separate copy of per-process global variable. Modifiable
procedures are also known as pure procedures or reentrant codes (can’t change during
execution). For example, only one copy of editor or compiler code can be kept in memory, and
all editor or compiler processes can execute that single copy of the code. This helps memory
utilisation. Major advantages of paging scheme are:

(a) Virtual address space must be greater than main memory size i.e., can execute program
with large logical address space as compared with physical address space;
(b) Avoid external fragmentation and hence storage compaction; and
(c) Full utilisation of available main storage.

Disadvantages of paging include internal fragmentation problem i.e., wastage within allocated
page when process is smaller than page boundary. Also, extra resource consumption and
overheads for paging hardware and virtual address to physical address translation takes place.

Activity 5c
1. Why do Operating System employ paging?
2. What is the different between paging address transalation by direct mapping
and with association mapping?

5.12. Segmentation
In the earlier section we have seen the memory management scheme called as paging. In
general, a user or a programmer prefers to view system memory as a collection of variable-sized
segment rather than as a linear array of words. Segmentation is a memory management
scheme that supports this view of memory.

5.12.1. Principles of Operation


Segmentation presents an alternative scheme for memory management. This scheme divides
the logical address space into variable length chunks, called segment, with no proper ordering
among them. Each segment has a name and a length. Thus, the logical addresses are expressed
as a pair of segment number and offset within segment. It allows a program to be broken down
into logical parts according to the user view of the memory, which is the mapped into physical
memory. Though logical address is two-dimensional but actual physical address are still one-
dimensional array of bytes only.

5.12.2. Address Translation

66
Figure 5.16: Address translation

The offset d must range between 0 and segment limit/length, otherwise it will generate address
error. For example, consider situation shown in Figure 5.17.

Figure 5.17: Principle pf operation of representation

This scheme is similar to variable partition allocation method with improvement that the
process is divided into parts. For fast retrieval we can use registers as paged scheme. This is
known as a segment-table length register (STLR). The segments in a segmentation scheme
correspond to logical division of the process and are defined by program names. Extract the
segment number and offset from logical from logical address first. Then use segment number as
index into segment table to obtain segment base address and its limit/length. Also, check that
the offset is not greater than given limit in segment table. Now, general physical address is
obtained by adding the offset to the base address.

5.12.3. Protection and Sharing


This method also allows segments that are read-only to be shared, so that two processes can
use shared code for better memory efficiency. The implementation is such that no program can
read from or write to segments belonging to another program, except the segments that have
been set up to be shared. With each segment-table entry protection bit specifying segment as
read-only or execute only can be used. Hence illegal attempts to write into a read-only segment
can be prevented.

67
Sharing of segments can be done by making common/same entries in segment tables of two
different processes which point to same physical location. Segmentation may suffer from
external fragmentation i.e., when blocks of free memory are not enough to accommodate a
segment. Storage compaction and coalescing can minimise this drawback.

Activity 5d
1. What is the advantage of using Base and Limit registers?
2. How does lookup work with TLB’s? (c) Why is page size always powers of 2?
3. A system with 18-bit address uses 6 bits for page number and next 12 bits for
offset. Compare the total number of pages and express the following address
according to paging scheme 001011 (page number) and 000000111000
(offset)?

5.13. Summary
In this unit, we have learnt memory resource is managed and how processes are protected from
each other. The previous two sections covered memory allocation techniques like swapping and
overlays, which tackle the utilisation of memory. Paging and segmentation was presented as
memory management schemes. Both have their own merits and demerits. We have also seen
how paging is based on physical form of process and is independent of the programming
structures, while segmentation is dependent on logical structure of process as viewed by user.
We have also considered level of fragmentation (internal and external) problems and ways to
tackle them to increase level of multiprogramming and system efficiency. Concept of relocation
and compaction helps to overcome external fragmentation.

5.14. Key Terms


Address translation Compile-time Direct mapping
Dynamic partition Execution-time Load-time
Main memory Memory Single-partition system
Memory management unit (MMU) Overlays Monoprogramming
Page Segmentation Static partition Storage Swapping

Reading sources/links
1. Deital, H., M. (1984). An introduction to operating systems. Peterson: Addition Wesley
Publishing Company.
2. Ritchie, C. (2003). Operating system incorporation UNIX and Windows (4th ed.). New Delhi:
BPB Pulbication.
3. Silberschatz, A., & Galvin, P. B. (1997). Operating system concepts. (5th ed.). New Delhi:
Wiley and Sons (Asia) Publication.
4. Tanenbaum, A., S., & Woodhull, A., S. (2009). Operating system design and implementation.
UK: Pearson.

7 Topic 6…………………………………………………………………………………………………………………………………….
Introduction

6.1. Sub topic1

68
6.2. Sub topic 2

6.3. Sub topic 3


Activity 3a

Reading sources/links

7 Topic 7…………………………………………………………………………………………………………………………………….
7.1. Introduction

7.2. Sub topic1

7.3. Sub topic 2

7.4. Sub topic 3


Activity 3a

Reading sources/links

MODULE SUMMARY/CONCLUSION
4 PRACTICUUM

5 REVISION
6 EXAMINATION

Lbh/ps/ns/202

69

You might also like