0% found this document useful (0 votes)
5 views116 pages

Last Edited Operating System Module (1111)

The document outlines the Operating System module at the Ethiopian Police University, detailing its objectives, structure, and content, which includes topics such as process management, memory management, and security. It aims to provide students with a comprehensive understanding of operating systems, their architecture, and functionality, while also emphasizing practical skills in managing Linux-based systems. The course prepares students for careers in IT, system administration, and software development by equipping them with essential knowledge and skills.

Uploaded by

ajaibnasir821
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views116 pages

Last Edited Operating System Module (1111)

The document outlines the Operating System module at the Ethiopian Police University, detailing its objectives, structure, and content, which includes topics such as process management, memory management, and security. It aims to provide students with a comprehensive understanding of operating systems, their architecture, and functionality, while also emphasizing practical skills in managing Linux-based systems. The course prepares students for careers in IT, system administration, and software development by equipping them with essential knowledge and skills.

Uploaded by

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

ETHIOPIAN POLICE UNIVERSITY

COLLEGE OF CRIME INVESTIGATION AND FORENSIC


SCIENCE
DEPARTMENT OF INFORMATION TECHNOLOGY

Operating System Module


Course code: - ITec2022
Total ECTS: - 5
Time Allowed: - 90 Hours

Compiled by: - Feyisa Changare (Deputy/Insp)

October, 2024
Sendafa, Ethiopia

1
Contents Page
CHAPTER ONE .................................................................................................................................... 1
INTRODUCTION TO OPERATING SYSTEM................................................................................................ 1
1.0 OBJECTIVE ................................................................................................................................. 1
1.1 DEFINITION OF OPERATING SYSTEM .......................................................................................... 1
1.2 OBJECTIVE (GOALS) OF OPERATING SYSTEM ............................................................................. 4
1.3 TYPES OF OPERATING SYSTEM ................................................................................................... 4
1.3.1 Batch Operating System ........................................................................................................ 5
1.1.1 Multiprogramming Operating System ................................................................................... 5
1.1.2 Multitasking Operating System ............................................................................................. 6
1.1.1 Multi-user Operating System................................................................................................. 6
1.1.1 Time-sharing system.............................................................................................................. 7
1.1.1 Real-time systems .................................................................................................................. 8
1.1.2 Distributed Operating Systems .............................................................................................. 8
1.2 COMPUTER SYSTEM ARCHITECTURE .......................................................................................... 9
1.3 TYPES OF MULTIPROCESSOR SYSTEMS ....................................................................................... 9
1.4 SYMMETRIC MULTIPROCESSING SYSTEM(SMP): ..................................................................... 10
1.5 OPERATING SYSTEM INTERFACE .............................................................................................. 11
1.6 SYSTEM CALLS ......................................................................................................................... 11
1.7 TYPES OF SYSTEM CALLS ......................................................................................................... 12
CHAPTER TWO ................................................................................................................................. 16
PROCESS MANAGEMENT ..................................................................................................................... 16
2.0 OBJECTIVE ............................................................................................................................... 16
2.1 PROCESS ................................................................................................................................... 16
2.1.1 Process States ...................................................................................................................... 17
2.2 THREAD.................................................................................................................................... 19
2.3 TYPES OF THREADS .................................................................................................................. 20
2.3.1 User-level thread .................................................................................................................. 20
2.3.2 Kernel level thread ............................................................................................................... 20
2.4 BENEFITS OF THREADS ............................................................................................................. 21
2.5 Multithreading ..................................................................................................................... 22
2.5.1 Multithreading Models ........................................................................................................ 23
CHAPTER THREE ............................................................................................................................. 27
3.0 OBJECTIVE ............................................................................................................................... 27
3.1 PROCESS SCHEDULING CONCEPTS ............................................................................................ 27

i
3.1.1 CPU SCHEDULER ...................................................................................................................... 28
3.2 CPU SCHEDULING POLICIES ..................................................................................................... 30
3.2.1 First Come First Serve (FCFS) ............................................................................................ 32
3.2.2 Shortest Job First ................................................................................................................. 33
3.2.3 Round Robing Scheduling ................................................................................................... 34
3.2.4 Shortest Remaining Time .................................................................................................... 35
3.2.5 Dynamic Priority Scheduling .............................................................................................. 36
3.3 OTHER SCHEDULING POLICIES.................................................................................................. 37
3.3.2 Real-Time Scheduling Policies ........................................................................................... 38
3.3.3 Multiprocessor Systems ....................................................................................................... 38
SUMMARY ........................................................................................................................................... 39
CHAPTER FOUR ................................................................................................................................ 42
DEADLOCK .......................................................................................................................................... 42
4.1 DEFINITION .............................................................................................................................. 42
4.2 PRE-EMPTIBLE AND NONPREEMPTABLE RESOURCES ................................................................. 43
4.3 NECESSARY AND SUFFICIENT DEADLOCK CONDITIONS ........................................................... 43
4.4 RESOURCE-ALLOCATION GRAPH ............................................................................................. 45
4.4.1 Interpreting a Resource Allocation Graph with Single Resource Instances...................... 45
4.5 DEALING WITH DEADLOCK ...................................................................................................... 46
4.6 DEADLOCK PREVENTION ............................................................................................................... 46
4.6.1 Elimination of Mutual Exclusion Condition ........................................................................ 47
4.6.2 Elimination of Hold and Wait Condition ............................................................................ 47
4.6.3 Elimination of No-preemption Condition ............................................................................ 47
4.6.4 Elimination of Circular Wait Condition............................................................................... 48
4.7 DEADLOCK AVOIDANCE ........................................................................................................... 49
4.7.1 Banker‟s Algorithm ............................................................................................................. 49
4.7.2 Evaluation of Deadlock Avoidance Using the Banker‟s Algorithm ................................... 51
4.8 DEADLOCK DETECTION ............................................................................................................ 51
4.8.1 Deadlock Recovery................................................................................................................ 51
4.9 Synchronization ................................................................................................................... 52
CHAPTER FIVE ................................................................................................................................. 55
MEMORY MANAGEMENT .................................................................................................................... 55
Introduction .................................................................................................................................... 55
5.1 OBJECTIVES .............................................................................................................................. 55
AT THE END OF THE UNIT THE STUDENT SHOULD BE ABLE TO: ............................................................. 55
5.2 MEMORY MANAGEMENT ......................................................................................................... 55
5.4 CONTIGUOUS MEMORY ALLOCATION ...................................................................................... 57
5.4.1 Fixed Partitions .................................................................................................................... 58
5.4.2 Dynamic Partitions .............................................................................................................. 58

ii
5.4.3 Swapping ............................................................................................................................. 59
5.5 NONCONTIGUOUS MEMORY ALLOCATION ............................................................................... 59
5.5.1 Paging .................................................................................................................................. 59
5.5.2 SEGMENTATION........................................................................................................................ 60
5.6 VIRTUAL MEMORY................................................................................................................... 61
5.7.1 Fetch Policy ......................................................................................................................... 63
5.7.2 Replacement Policy ............................................................................................................. 63
5.7.3 Frame Allocation ................................................................................................................. 65
5.8 PAGE FAULTS AND PERFORMANCE ISSUES ............................................................................... 65
5.8.1 The Working Set Algorithm ................................................................................................ 65
5.8.2 Thrashing ............................................................................................................................. 66
5.8.3 Caching ................................................................................................................................ 66
CHAPTER 6 ......................................................................................................................................... 69
DEVICE MANAGEMENT ........................................................................................................................ 69
INTRODUCTION .................................................................................................................................... 69
6.1 DEVICE MANAGEMENT TECHNIQUES........................................................................................ 69
VII. POLLING VS. INTERRUPTS I/O ................................................................................................... 72
6.2 SOFTWARE INPUT AND OUTPUT MANAGEMENT ......................................................................... 73
6.3 DEVICE-INDEPENDENT I/O SOFTWARE ..................................................................................... 74
6.4 Buffering .............................................................................................................................. 75
6.6 DISK MANAGEMENT ................................................................................................................. 77
CHAPTER SEVEN .............................................................................................................................. 80
FILE SYSTEM ....................................................................................................................................... 80
7.1 FILE CONCEPT........................................................................................................................... 80
7.2 FILE OPERATIONS ..................................................................................................................... 81
7.3 FILE MANAGEMENT SYSTEM ..................................................................................................... 82
7.4 FILE TYPES ............................................................................................................................... 83
7.5 FILES STRUCTURE ..................................................................................................................... 84
7.6 DIRECTORY OVERVIEW ............................................................................................................ 87
7.7 FILE PROTECTION ..................................................................................................................... 89
7.8 RECOVERY (FILE RECOVERY) ................................................................................................... 91
CHAPTER EIGHT .............................................................................................................................. 94
SECURITY AND PROTECTION ............................................................................................................... 94
8.1 Overview of security and protection system ....................................................................... 94
8.2 Operating system security ................................................................................................... 94
8.3 Computer security classifications ........................................................................................ 97
8.4 Memory Protection .............................................................................................................. 98
8.6 Authentication in cyber security ........................................................................................ 100

iii
8.7 Authentication factors........................................................................................................ 102
8.8 Authentication and authorization....................................................................................... 104
8.9 Encryption ......................................................................................................................... 104
REFERENCEE ................................................................................. ERROR! BOOKMARK NOT DEFINED.

List of Figure
Figure 1.1: The relationship between application & system software ..................................................... 2
Figure 1.2 Basic components of a computer system ................................................................................ 3
Figure 2.1 Diagram of process state ...................................................................................................... 17
Figure 2.2 kernel level threads ............................................................................................................... 21
Figure 2.3 single thread and multithread ............................................................................................... 23
Figure 2.3 many to many models Multithreading .................................................................................. 24
Figure 2.5 One to One models Multithreading ...................................................................................... 25
Figure 3.1 Dispatching technique .......................................................................................................... 29
Figure 4.1 example of Deadlock ............................................................................................................ 44
Figure 6.1 Direct Memory Access module ............................................................................................ 72
Figure 6.2 Layer of I/O Software ........................................................................................................... 73
Figure 7.1 directory and disk structure .................................................................................................. 87
Figure 7.2 Single level directory ............................................................................................................ 88
Figure 7.3 Two level directory............................................................................................................... 89
Figure 7.4 Windows access control management .................................................................................. 91

List of Table
Table 2.1 Process control block ............................................................................................................. 18
Table 2.2 the difference between process and thread. ........................................................................... 20
Table 2.3 Difference between User-Level & Kernel-Level Thread ...................................................... 25
Table 4.1 global Numbering in deadlock ............................................................................................... 48
Table 6.1 step of OS uses DMA ............................................................................................................ 72
Table 7.1 Common file types ................................................................................................................. 84

iv
Module description
The Operating Systems module provides students with a comprehensive understanding of the
principles and concepts that underlie modern operating systems. It covers the architecture and
functionality of various OS types. Students will explore key topics such as process management,
memory management, file systems and device management. Through theoretical insights and practical
applications, this module will equip students with the skills to analyze and design operating systems,
emphasizing the importance of efficiency, reliability and security in system performance.

In addition to foundational concepts, the module delves into advanced topics such as virtualization,
distributed systems and contemporary operating systems like Linux and Windows. Students will
engage in hands-on laboratory sessions equips students with essential skills and practical experience in
navigating and managing a Linux-based operating system. Students learn to utilize the command line
interface (CLI) effectively, mastering commands for file manipulation, system monitoring and
package management. This experience enhances their understanding of system architecture and
improves troubleshooting capabilities, as they gain insights into system processes and resource
utilization. These skills foster a deeper appreciation for open-source environments and prepare
students for various roles in IT, system administration and software development.

Course Objective
At the end of the course students will be able to:
 Gain knowledge of the underlying hardware architecture and how operating systems interact
with hardware components
 Learn about process creation, scheduling, synchronization and termination.
 Understand how operating systems manage multiple processes and allocate CPU time
effectively
 Explore memory allocation techniques including paging, segmentation and virtual memory
 Understand how operating systems handle memory hierarchy and optimize memory usage
 Study the challenges of concurrent execution and the mechanisms used to manage it, such as
locks and semaphores.
 Understand deadlock detection, prevention and recovery strategies
 Learn about device drivers and how operating systems communicate with peripheral devices.

v
 Study the structure and functioning of file systems, including file operations, storage
management and data retrieval.
 Understand how operating systems manage files, directories and permissions
 Explore the principles of system security, including user authentication, access control, and
data protection.
 Understand how operating systems enforce security policies and protect against threats
 Explore the role of user interfaces in operating systems, including command-line interfaces
(CLI) and graphical user interfaces (GUI)

By mastering these objectives, students will be well-prepared to understand the complexities of


operating systems develop essential skills for system administration and contribute to the design and
implementation of robust computing environments. This knowledge is crucial for careers in software
development, system administration, cyber security and IT consulting.

vi
Chapter One
Introduction to Operating System
1.0 Objective
The objective of this chapter is to make the students familiar with the basics of Operating system.
After studying this lesson the students will able to:

 Understand the basic concept of an operating system

 Explain functions performed by an operating system.

 Differentiate the different types of operating systems.

 Identify Computer architecture system

 Understand an Operating System Interface

 Know the System call concept


1.1 Definition of Operating System
Activity 1.1
 How the operating system acts as control program?

Operating system (OS) is a program or set of programs, which acts as an interface between a
user of the computer & the computer hardware. The main purpose of an OS is to provide an
environment in which we can execute programs. The main goals of the OS are;
i. To make the computer system convenient to use,
ii. To make the use of computer hardware in efficient way.
Operating System is system software, which may be viewed as collection of software consisting of
procedures for operating the computer & providing an environment for execution of programs. It‟s
an interface between user & computer. So an OS makes everything in the computer to work together
smoothly & efficiently.

1
Figure 1.1: The relationship between application & system software

Activity 1.2

 Describe the general roles of an operating system, and elaborate why these roles
are important.

Basically, an OS has three main responsibilities:

(a) Perform basic tasks such as recognizing input from the keyboard, sending output to the display screen,
keeping track of files & directories on the disk, & controlling peripheral devices such as disk drives & printers.

(b) Ensure that different programs & users running at the same time do not interfere with each other.

(c) Provide a software platform on top of which other programs can run. The OS is also responsible for
security, ensuring that unauthorized users do not access the system. Figure 1 illustrates the relationship
between application software & system software.
The first two responsibilities address the need for managing the computer hardware & the application programs
that use the hardware. The third responsibility focuses on providing an interface between application software
& hardware so that application software can be efficiently developed. Since the OS is already responsible for
managing the hardware, it should provide a programming interface for application developers. As a user, we
normally interactwith the OS through a set of commands. The commands are accepted & executed by a part of
the OS called the command processor or command line interpreter.
In order to understand operating systems we must understand the computer hardware & the development
of OS from beginning. Hardware means the Physical machine & its electronic components including memory
chips, input/output devices, storage devices & the central processing unit. Software is the programs written for
these computer systems. Main memory is where the data & instructions are stored to be processed.
Input/output devices are the peripherals attached to the system, such as keyboard, printers, disk drives, CD

2
drives, magnetic tape drives, modem, monitor, etc. The central processing unit is the brain of the computer
system; it has circuitry to control the interpretation & execution of instructions. It controls the operation of
entire computer system. All of the storage references, data manipulations & I/O operations are performed by
the CPU. The entire computer systems can be divided into four parts or components

1) Hardware

2) Operating System

3) The application programs & system programs

4) Users.
The hardware provides the basic computing power. The system programs the way in which these resources
are used to solve the computing problems of the users. There may be many different users trying to solve
different problems. The OS controls & coordinates the use of the hardware among the various users & the
application programs.

Figure 1.2 Basic components of a computer system


We can view an OS as a resource allocator. A computer system has many resources which are to be
required to solve a computing problem. These Resources are the CPU time, memory space, files storage
space, input/output devices & so on. The OS acts as a manager of all of these resources & allocates them to
the specific programs & users as needed by their tasks. Since there can be many conflicting requests for the
resources, the OS must decide which requests are to be allocated resources to operate the computer system
fairly & efficiently. An OS can also be viewed as a control program, used to control the various I/O devices
& the users programs. A control program controls the execution of the user programs to prevent errors &
improper use of the computer resources. It is especially concerned with the operation & control of I/O
devices. As stated abovethe fundamental goal of computer system is to execute user programs & solve user
problems. For this goal computer hardware is constructed. But the bare hardware is not easy to use & for
this purpose application/system programs are developed.

3
1.2 Objective (Goals) of Operating System
 Convenience. An operating system makes a computer more convenient to the user of for using. (Easy-
to-use commands, graphical user interface (GUI).
 Efficiency. An OS allows the computer system resources to be used in efficient manner, to ensure
good resource utilization efficiency and provide appropriate corrective action when it becomes low.
 Ability to evolve. An OS should be constructed in such way as to permit the effective development,
testing and introduction of new system functions without interfering with service.
Operating system performs the following functions:
 Booting Booting is a process of starting the computer operating system starts the computer to
work.It checks the computer and makes it ready to work.
 Memory Management It is also an important function of operating system. The memory cannot
be managed without operating system. Different programs and data execute in memory at one
time. If there is no operating system, the programs may mix with each other. The system will not
work properly.
 Loading and Execution a program is loaded in the memory before it can be executed. Operating
system provides the facility to load programs in memory easily and then execute it.
 Data security Data is an important part of computer system. The operating system protects the
data stored onthe computer from illegal use, modification or deletion.
 Disk Management Operating system manages the disk space. It manages the stored files and
folders in a proper way.
 Process Management CPU can perform one task at one time. If there are many tasks, operating
system decides which task should get the CPU.
 Device Controlling Operating system also controls all devices attached to computer. The hardware
devices controlled with the help of small software called device drivers.
 Providing interface It is used in order that user interface acts with a computer mutually. User
interface controls how you input data and instruction and how information is displayed on screen.
The operating system offers two types of the interface to the user:
Graphical-line interface: It interacts with of visual environment to communicate with the computer. It uses
windows, icons, menus and other graphical objects to issues commands.
i. Command-line interface: it provides an interface to communicate with the computer by
typing commands.
1.3 Types of Operating System

4
Activity 1.3
 List and explain the different types of operating system
 Discuss their difference and similarities?

Operating system can be classified into various categories on the basis of several criteria, number of
simultaneously active programs, number of users working simultaneously, number of processors in
the computer system, etc. In the following discussion several types of operating systems are discussed.
1.3.1 Batch Operating System
Batch processing is the most primitive type of operating system. Batch processing generally requires
the program, data, & appropriate system commands to be submitted together in the form of a job. Batch
operating systems usually allow little or no interaction between users & executing programs. Batch
processing has a greater potential for resource utilization than simple serial processing in computer
systems serving multiple users. Due to turnaround delays & offline debugging, batch is not very
convenient for program development. Programs that do not require interaction & programs with long
execution times may be served well by a batch operating system. Examples of such programs include
payroll, forecasting, statistical analysis, & large scientific number-crunching programs. Serial
processing combined with batch like command file is also found on many personal computers.
Scheduling in batch is very simple. Jobs are typically processed in order of their submission, that is,
first-come first-served fashion.
Memory management in batch systems is also very simple. Memory is usually divided into two areas.
The resident portion of the OS permanently occupies one of them, & the other is used to load transient
programs for execution. When a transient program terminates, a new program is loaded into the same
area of memory. Since at most one program is in execution at any time, batch systems do not require
any time-critical device management. For this reason, many serial, I/O & ordinary batch operating
systems use simple, program controlled method of I/O. The lack of contention for I/O devices makes
their allocation & deallocation trivial.
Batch systems often provide simple forms of file management. Since access to files is also serial, little
protection & no concurrency control of file access in required.
1.3.2 Multiprogramming Operating System
A multiprogramming system permits multiple programs to be loaded into memory & execute the
programs concurrently. Concurrent execution of programs has a significant potential for improving
system throughput & resource utilization relative to batch & serial processing. This potential is realized
by a class of operating systems that multiplex resources of a computer system among a multitude of

5
active programs. Such operating systems usually have the prefix multi in their names, such as
multitasking or multiprogramming.
1.3.3 Multitasking Operating System
An instance of a program in execution is called a process or a task. A multitasking OS is distinguished
by its ability to support concurrent execution of two or more active processes. Multitasking is usually
implemented by maintaining code & data of several processes in memory simultaneously & by
multiplexing processor & I/O devices among them. Multitasking is often coupled with hardware &
software support for memory protection in order to prevent erroneous sprocesses from corrupting address
spaces & behavior of other resident processes. Allows more than one program to run concurrently.
The ability to execute more than one task at the same time, a task being a program is called as
multitasking. The terms multitasking & multiprocessing are often used interchangeably, although
multiprocessing sometimes implies that more than oneCPU is involved. In multitasking, only one CPU is
involved, but it switches from one program to another so quickly that it gives the appearance of
executing all ofthe programs at the same time.
There are two basic types of multitasking: preemptive & cooperative. In preemptive multitasking, the OS
parcels out CPU time slices to each program. In cooperative multitasking, each program can control the
CPU for as long as it needs it. If a program is not using the CPU, however, it can allow another program
to use it temporarily. OS/2, Windows 95, Windows NT, & UNIX use preemptive multitasking, whereas
Microsoft Windows & the MultiFinder use cooperative multitasking.
1.3.4 Multi-user Operating System
Multiprogramming operating systems usually support multiple users, in which case they are also called
multi-user systems. Multi-user operating systems provide facilities for maintenance of individual user
environments & therefore require user accounting. In general, multiprogramming implies multitasking, but
multitasking does not imply multi-programming. In effect, multitasking operation is one of the
mechanisms that a multiprogramming OS employs in managing the totality of computer-system resources,
including processor, memory, & I/O devices.
Multitasking operation without multi-user support can be found in operating systems of some advanced
personal computers & in real-time systems. Multi-access operating systems allow simultaneous access to
a computer system through two or more terminals.
In general, multi-access operation does not necessarily imply multiprogramming. An example is provided
by some dedicated transaction-processing systems, such as airline ticket reservation systems, that support
hundreds of active terminals under control of a single program. In general, the multiprocessing or

6
multiprocessor operating systems manage the operation of computer systems that incorporate multiple
processors. Multiprocessor operating systems are multitasking operating systems bydefinition because they
support simultaneous execution of multiple tasks (processes) on different processors. Depending on
implementation, multitasking may or may not be allowed on individual processors. Except for management
& scheduling of multiple processors, multiprocessor operating systems provide the usual complement of
other system services that may qualify them as time-sharing, real-time, or a combination operating system.
1.3.5 Time-sharing system
Time-sharing is a popular representative of multi-programmed, multi-user systems. In addition to general
program-development environments, many large computer-aided design & text-processing systems
belong to this category. One of the primary objectives of multi-user systems in general, & time-sharing
in particular, is good terminal response time. Giving the illusion to each user of having a machine to
oneself, time-sharing systems often attempt to provide equitable sharing of common resources. For
example, when the system is loaded, a user with more demanding processing requirements are made to
wait longer. This philosophy is reflected in the choice of scheduling algorithm.
Most time- sharing systems use time-slicing scheduling. In this approach, programs are executed
with rotating priority that increases during waiting & drops after theservice is granted. In order to
prevent programs from monopolizing the processor, a program executing longer than the system-
defined time slice is interrupted by the OS & placed at the end of the queue of waiting programs. This
mode of operation generally provides quick response time to interactive programs. Memory
management in time-sharing systems provides for isolation &protection of co-resident programs. Some
forms of controlled sharing are sometimes provided to conserve memory & possibly to exchange data
between programs. Being executed on behalf of different users, programs in time-sharing systems
generally do not have much need to communicate with each other. As in most multi-user
environments, allocation & de-allocation of devices must be done in a manner that preserves system
integrity & provides for good performance.

7
1.3.6 Real-time systems
Real time systems are used in time critical environments where data must be processed extremely
quickly because the output influences immediate decisions. Real time systems are used for space
flights, airport traffic control, industrial processes, sophisticated medical equipment‟s, telephone
switching etc. A real time system must be 100 percent responsive in time. Response time is measured
in fractions of seconds. In real time systems the correctness of the computations not only depends upon
the logical correctness of the computation but also upon the time at which the results is produced. If
the timing constraintsof the system are not met, system failure is said to have occurred.

Real-time operating systems are used in environments where a large number of events, mostly external
to the computer system, must be accepted & processed in a short time or within certain deadlines‟
primary objective of real-time systems is to provide quick event-response times, & thus meet the
scheduling deadlines. User convenience & resource utilization are of secondary concern to real-time
system designers. It is notuncommon for a real-time system to be expected to process bursts of thousands
of interrupts per second without missing a single event. Such requirements usually cannot be met by
multi-programming alone, & real-time operating systems usually rely on some specific policies &
techniques for doing their job.
1.3.7 Distributed Operating Systems
A distributed computer system is a collection of autonomous computer systems capable of
communication & cooperation via their hardware & software interconnections. Historically, distributed
computer systems evolved from computer networks in which a number of largely independent hosts are
connected by communication links & protocols. A distributed OS governs the operation of a distributed
computer system & provides a virtual machine abstraction to its users.

The key objective of a distributed OS is transparency. Ideally, component & resource distribution should
be hidden from users & application programs unless they explicitly demand otherwise. Distributed
operating systems usually provide the means for system-wide sharing of resources, such as
computational capacity, files, & I/O devices. In addition to typical operating-system services provided at
each node for the benefit of local clients, a distributed OS may facilitate access to remote resources,
communication with remote processes, & distribution of computations. The added services necessary for
pooling of shared system resources include global naming, distributed file system, & facilities for
distribution.

8
1.4 Computer System Architecture
Computer architecture means construction/design of a computer. A computer system may be
organized in different ways. Some computer systems have single processor and others have
multiprocessors. So based on the processors used in computer systems, they are categorized into
the following systems.
1. Single-processor system
2. Multiprocessor system
3. Clustered Systems:
1. Single-Processor Systems
Some computers use only one processor such as microcomputers (or personal computers PCs). On a
single-processor system, there is only one CPU that performs all the activities in the computer system.
However, most of these systems have other special purpose processors, such as I/O processors that move
data quickly among different components of the computers. These processors execute only a limited
system programs and do not run the user program.

Sometimes they are managed by the operating system. Similarly, PCs contain a special purpose
microprocessor in the keyboard, which converts the keystrokes into computer codes to be sent to the CPU.
The use of special purpose microprocessors is common in microcomputer. But it does not mean that this
system is multiprocessor. A system that has only one general-purpose CPU is considered as single-
processor system.
2. Multiprocessor Systems
In multiprocessor system, two or more processors work together. In this system, multiple programs (more
than one program) are executed on different processors at the same time. This type of processing is known
as multiprocessing. Some operating systems have features of multiprocessing. UNIX is an example of
multiprocessing operating system. Some versions of Microsoft Windows also support multiprocessing.
Multiprocessor system is also known as parallel system. Mostly the processors of multiprocessor system
share the common system bus, clock, memory and peripheral devices. This system is very fast in data
processing.
1.5 Types of Multiprocessor Systems
The multiprocessor systems are further divided into two types:-
(i). Asymmetric multiprocessing system
(ii). Symmetric multiprocessing system
i. Asymmetric Multiprocessing System(AMS)

9
The multiprocessing system, in which each processor is assigned a specific task, is known as
Asymmetric Multiprocessing System. For example, one processor is dedicated for handling user's
requests, one processor is dedicated for running application program, and one processor is dedicated
for running image processing and so on. In this system, one processor works as master processor,
while other processors work as slave processors. The master processor controls the operations of
system. It also schedules and distributes tasks among the slave processors. The slave processors
perform the predefined tasks.
1.6 Symmetric Multiprocessing System (SMP):
The multiprocessing system, in which multiple processors work together on the same task, is known
as Symmetric Multiprocessing System. In this system, each processor can perform all types of
tasks. All processors are treated equally and no master-slave relationship exists between the
processors.

For example, different processors in the system can communicate with each other. Similarly, an I/O
can be processed on any processor. However, I/O must be controlled to ensure that the data reaches
the appropriate processor. Because all the processors share the same memory, so the input data given
to the processors and their results must be separately controlled. Today all modern operating systems
including Windows and Linux provide support for SMP.
It must be noted that in the same computer system, the asymmetric multiprocessing and symmetric
multiprocessing technique can be used through different operating systems.

Figure 1.3 Dual-Core Designs


3. Clustered Systems: Clustered system is another form of multiprocessor system. This system also contains
multiple processors but it differs from multiprocessor system. The clustered system consists of two or more
individual systems that are coupled together.

10
In clustered system, individual systems (or clustered computers) share the same storage and are linked
together, via Local Area Network (LAN).
A layer of cluster software runs on the cluster nodes. Each node can monitor one or more of the other nodes
over the LAN. If the monitored machine fails due to some technical fault (or due to other reason), the
monitoring machine can take ownership of its storage. The monitoring machine can also restart the
applications that were running on the failed machine. The users of the applications see only an interruption
of service.
1.7 Operating System Interface
a. User Operating System Interface;
 CLI Command Line Interface (CLI) or command interpreter allows direct command entry
sometimes implemented in kernel, sometimes by systems program sometimes multiple
flavors implemented – shells primarily fetches a command from user and executes it.
b. User Operating System Interface – GUI
 User-friendly desktop metaphor interface usually mouse, keyboard, and monitor Icons
represent files, programs, actions, etc.
 Various mouse buttons over objects in the interface cause various actions (provide information,
options, execute function, open directory (known as a folder)
 Invented at Xerox PARC
 Many systems now include both CLI and GUI interfaces Microsoft Windows is GUI with CLI
“command” shell
 Apple Mac OS X as “Aqua” GUI interfaces with UNIX kernel underneath and shells available
Solaris is CLI with optional GUI interfaces (Java Desktop, KDE).
1.8 System Calls
Programming interface to the services provided by the OS Typically written in a high-level language (C
or C++) Mostly accessed by programs via a high-level Application Program Interface (API) rather than
direct system call used Three most common APIs are Win32 API for Windows, POSIX API for POSIX-
based systems (including virtually all versions of UNIX, Linux, and Mac OS X), and Java API for the
Java virtual machine (JVM)

System Call Parameter Passing


Often, more information is required than simply identity of desired.

11
 System call exact type and amount of information vary according to OS and call Three
general methods used to pass parameters.
 OS Simplest: pass the parameters in registers.
 In some cases, may be more parameters than registers.

 Parameters stored in a block, or table, in memory, and address of block passed as a


parameterin a register.
 This approach taken by Linux and Solaris.
 Parameters placed, or pushed, onto the stack by the program and popped off the stack
by the operatingsystem.
 Block and stack methods do not limit the number or length of parameters being passed.
1.9 Types of System Calls
a. Process control
b. File management
c. Device management
d. Information maintenance
e. Communications
a. Process control; running needs to halt its execution either normally or abnormally. If a
system call is made to terminate the running program, a dump of memory is sometimes
taken and an error message generated which can be diagnosed by a debugger
 end, abort
 load, execute
 create process, terminate process
 get process attributes, set process attributes
 wait for time
 wait event, signal event
 allocate and free memory
b. File management: - OS provides an API to make these system calls for managing files.
 create file, delete file
 open, close file
 read, write, reposition
 get and set file attributes
c. Device management: - Process requires several resources to execute, if these resources are

12
Available they will be granted and control retuned to user process. Some are physical such as
video card and other such as file. User program request the device and release when finished.
 request device, release device
 read, write, reposition
 get device attributes, set device attributes
 logically attach or detach devices
d. Information maintenance:- System calls exist purely for transferring information between the user
program and OS. It can return information about the system, such as the number of current users, the
version number of the operating system, the amount of free memory or disk space and so on.
 get time or date, set time or date
 get system data, set system data
 get and set process, file, or device attributes
e. Communications
Two common models of communication
 Message-passing model, information is exchanged through an inter process- communication
facility provided by the OS.
 Shared-memory model, processes use map memory system calls to gain access to regions of
memory owned by other processes.
 create, delete communication connection
 send, receive messages
 transfer status information
 attach and detach remote devices

Figure 1.5 Examples of Windows and UNIX System Calls

13
1.10 Operating System Design and Implementation
Some approaches have proven successful Internal structure of different Operating Systems can vary
widely Start by defining goals and specifications Affected by choice of hardware, type of system
User goals and System goals
 User goals – operating system should be convenient to use, easy to learn, reliable, safe, and
fast.
 System goals – operating system should be easy to design, implement, and maintain, as
well as flexible,reliable, error-free, and efficient
Important principle to separate
Policy: What will be done?
Mechanism: How to do it?
Mechanisms determine how to do something; policies decide what will be done. The separation
of policy from mechanism is a very important principle, it allows maximum flexibility ifpolicy
decisions are to be changed later.
Simple Structure
 MS-DOS – written to provide the most functionality in the least space not divided into
modules
 Although MS-DOS has some structure, its interfaces and levels of Functionality are not well
separated.

Figure 1.6 MS-DOS Layer Structure

14
Summary
Operating system is also known as resource manager because its prime responsibility is to manage the
resources of the computer system i.e. memory, processor, devices and files. In addition to these,
operating system provides an interface between the user and the bare machine. Following the course of
the conceptual evolution of operating systems, we have identified the main characteristics of the
program-execution & development environments provided by the bare machine, serial processing,
including batch & multiprogramming.
On the basis of their attributes & design objectives, different types of operating systems were defined &
characterized with respect to scheduling & management of memory, devices, & files. The primary
concerns of a time-sharing system are equitable sharing of resources & responsiveness to interactive
requests. Real- time operating systems are mostly concerned with responsive handling of external events
generated by the controlled system. Distributed operating systems provide facilities for global naming &
accessing of resources, for resource migration, & for distribution of computation.
Typical services provided by an OS to its users were presented from the point of view of command-
language users & system-call users. In general, system calls provide functions similar to those of the
command language but allow finergradation of control.
Chapter One Review Questions
1. What are the objectives of an operating system? Discuss.
2. The services provided by an operating system are implemented as a large set of system
functions. List those functions!
3. Discuss modular approach of development of an operating system.
4. Present a hierarchical structure of an operating system.
5. What is an extended machine view of an operating system?
6. Discuss whether there are any advantages of using a multitasking operating
system, as opposed to a serial processing one.
7. What are the major functions performed by an operating system? Explain
8. What is the Operating System Interface?
9. Explain the difference between Operating System, Kernel and UNIX.

15
Chapter Two
Process Management
Introduction
In the previous chapter, the concept of operating system, types of operating system, computer system
architecture and system call was discussed. In this chapter, you are going to learn about process,
process states, threads, thread types, threads and process similarities and differences and
multithreading models.
2.0 Objective
The major goals of process management are the execution of various tasks such as creation of processes,
scheduling of processes, management of deadlock, and termination of processes. It is responsibility of
operating system to manage all the running processes of the system.
At the end of this chapter the students should able to:
 Understand the basic concepts of Process
 Introduce the notation of a process: - a program in execution, which forms the basis of
all computation
 Process states and how CPU switches from process to process
 Detail understand on process control block
 Understand the basic concepts of Thread
 Differentiate the difference between process, Thread and Program
 Explain the advantages of Process and Thread
 Differentiate the user-level from kernel-level thread
 Analyze multithreading models
2.1 Process
Activity 2.1
 What are the activities of the operating system in regard to process
management?
 Define Process and explain the difference between Process and Program?
 What are the attributes of process?
 Explain the information stored in process control block?
In Operating System, a Process is something that is currently under execution. So, an active
program can be called a Process. For example, when you want to search something on web then
you start a browser. So, this can be process. Another example of process can be starting your

16
music player to listen to some cool music of your choice.
A Process has various attributes associated with it. Some of the attributes of a Process are:
 Process Id: Every process will be given an id called Process Id to uniquely identify that
process from the other processes.
 Process state: Each and every process has some states associated with it at a particular
instant of time. This is denoted by process state. It can be ready, waiting, running, etc.
 CPU scheduling information: Each process is executed by using some process
scheduling algorithms like FCSF, Round-Robin, SJF, etc.
 I/O information: Each process needs some I/O devices for their execution. So, the
information about device allocated and device need is crucial.
2.1.1 Process States
When a process executed, it changes the state, generally the state of process is determined by the
current activity of the process. Each process may be in one of the following states:
 New: The process is being created.
 Running: The process is being executed.
 Waiting: The process is waiting for some event to occur.
 Ready: The process is waiting to be assigned to a processor.
 Terminated: The Process has finished execution.
Only one process can be running in any processor at any time, but much process may be inready
and waiting states. The ready processes are loaded into a “ready queue”

Figure 2.1 Diagram of process state

a) New ->Ready: OS creates process and prepares the process to be executed, and then OS
moved the process into ready queue.

17
b) Ready->Running: OS selects one of the Jobs from ready Queue and move them from ready to
Running.

c) Running->Terminated: When the Execution of a process has Completed, OS terminates that


process from running state. Sometimes OS terminates the process for some other reasons
including Time exceeded, memory unavailable, access violation, protection Error, I/O failure
and soon.

d) Running->Ready: When the time slot of the processor expired (or) if the processor received
any interrupt signal, the OS shifted Running -> Ready State.

e) Running -> Waiting: A process is put into the waiting state, if the process need events occur
(or) an I/O Device require.

f) Waiting->Ready: A process in the waiting state is moved to ready state when the event for
which it has been Completed.
Process Control Block: Each process is represented in the operating System by a Process Control
Block. It is also called Task Control Block. It contains many pieces of information associated with a
specific Process.
Process State

Program Counter

CPU Registers

CPU Scheduling Information

Memory – Management Information

Accounting Information

I/O Status Information

Table 2.1 Process control block


Process Control Block
1. Process State: The State may be new, ready, running, and waiting, terminated.
2. Program Counter: indicates the Address of the next Instruction to be executed.
3. CPUregisters: registers include accumulators, stack pointers,General purpose Registers.

4. CPU-Scheduling Info: i n c l u d e s a process pointer, pointers to scheduling Queues, other


scheduling parameters etc.

18
5. Memory management Info: includes page tables, segmentation tables, value of base and
limit registers.
6. Accounting Information: includes amount of CPU used, time limits, Jobs (or) Process
numbers.
7. I/O Status Information: Includes the list of I/O Devices Allocated to the processes, list of
open files.
2.2 Thread
A process is divide into number of light weight process, each light weight process is said to be a
Thread. The Thread has a program counter (Keeps track of which instruction to execute next),
registers (holds its current working variables), stack (execution History).

Thread States:
 bornState: A thread is just created.
 readystate: The thread is waiting for CPU.
 running: System assigns the processor to the thread.
 sleep: A sleeping thread becomes ready after the designated sleep time expires.
 dead: The Execution of the thread finished.
The differences between Process and Thread

Process Thread
Process takes more time to create. Thread takes less time to create.
it takes more time to complete execution & Less time to terminate.
terminate.
Execution is very slow. Execution is very fast.
It takes more time to switch b/w two It takes less time to switch b/w two
Processes. threads.
Communication b/w two processes are Communication b/w two threads is easy.
difficult.
Process can‟t share the same memory area. Threads can share same memory area.
System calls are requested to System calls are not required.
communicate each other.
Process is loosely coupled. Threads are tightly coupled.
It requires more resources to execute. Requires few resources to execute.

19
Table 2.2 the difference between process and thread.

Activity 2.2
 Differentiate the two types of thread
 Why user-level thread is faster than kernel level?
 Why user-level threads packages are generally cooperatively scheduled?

2.3 Types of Threads


In operating system there are two types of threads.
1 User-level thread
2 Kernel level thread
2.3.1 User-level thread
An operating system does not recognize the user-level thread. User threads can be easily implemented
and it is implemented by the user. If a user performs a user-level thread blocking operation, the whole
process is blocked. The kernel level thread does not know anything about the user level thread. The
kernel-level thread manages user-level threads as if they are single-threaded processes? Examples:
Java thread, POSIX threads, etc.
Advantages of User-level threads
 The user threads can be easily implemented than the kernel thread.
 User-level threads can be applied to such types of operating systems that do not support threads
at the kernel-level.
 It is faster and efficient.
 Context switch time is shorter than the kernel-level threads.
 It does not require modifications of the operating system.
 User-level threads representation is very simple. The register, PC, stack, and mini thread control
blocks are stored in the address space of the user-level process.
 It is simple to create, switch, and synchronize threads without the intervention of the process.
Disadvantages of User-level threads
 User-level threads lack coordination between the thread and the kernel.
 If a thread causes a page fault, the entire process is blocked.
2.3.2 Kernel level thread
The kernel thread recognizes the operating system. There is a thread control block and process control
block in the system for each thread and process in the kernel-level thread. The kernel-level thread is

20
implemented by the operating system. The kernel knows about all the threads and manages them. The
kernel-level thread offers a system call to create and manage the threads from user-space. The
implementation of kernel threads is more difficult than the user thread. Context switch time is longer in
the kernel thread. If a kernel thread performs a blocking operation, the Banky thread execution can
continue. Example: Window Solaris.

Figure 2.2 kernel level threads

Advantages of Kernel-level threads


 The kernel-level thread is fully aware of all threads.
 The scheduler may decide to spend more CPU time in the process of threads being large
numerical.
 The kernel-level thread is good for those applications that block the frequency.
Disadvantages of Kernel-level threads
 The kernel thread manages and schedules all threads.
 The implementation of kernel threads is difficult than the user thread.
 The kernel-level thread is slower than user-level threads.
2.4 Benefits of Threads
 Enhanced throughput of the system: When the process is split into many threads and each
thread is treated as a job, the number of jobs done in the unit time increases. That is why the
throughput of the system also increases.
 Effective Utilization of Multiprocessor system: When you have more than one thread in

21
one process, you can schedule more than one thread in more than one processor.
 Faster context switch: The context switching period between threads is less than the process
context switching. The process context switch means more overhead for the CPU.
 Responsiveness: When the process is split into several threads, and when a thread completes its
execution, that process can be responded to as soon as possible.
 Communication: Multiple-thread communication is simple because the threads share the same
address space, while in process; we adopt just a few exclusive communication strategies for
communication between two processes.
 Resource sharing: Resources can be shared between all threads within a process, such as code,
data, and files. Note: The stack and register cannot be shared between threads. There is a stack and
register for each thread.
2.5 Multithreading
Activity 2.3
 Multi-programming (or multi-tasking) enables more than a single process to
apparently execute simultaneously. How is this achieved on a uniprocessor?

A process is divided into number of smaller tasks each task is called a Thread. Number of
Threads with in a Process execute at a time is called Multithreading. If a program is multithreaded,
even when some portion of it is blocked, the whole program is not blocked. The rest of the program
continues working if multiple CPU‟s are available. Multithreading gives best performance. If we have
only a single thread, number of CPU‟s available, No performance benefits achieved. Process creation
is heavy-weight while thread creation is light-weight and is simplify code, increase efficiency.

22
Figure 2.3 single thread and multithread
Kernels are generally multithreaded
 CODE- Contains instruction
 DATA- holds global variable
 FILES- opening and closing files
 REGISTER- contain information about CPU state
 STACK-parameters, local variables, functions
2.5.1 Multithreading Models
Some operating system provides a combined user level thread and Kernel level thread facility.
Solaris is a good example of this combined approach. In a combined system, multiple threads
within the same application can run in parallel on multiple processors and a blocking system call
need not block the entire process. Multithreading models are three types.
 Many to much relationship.
 Many to one relationship.
 One to one relationship.
Many to Many Models
The many-to-many model multiplexes any number of user threads onto an equal or smaller number of
kernel threads. The following diagram shows the many-to-many threading model where 6 user level
threads are multiplexing with 6 kernel level threads. In this model, developers can create as many user
threads as necessary and the corresponding Kernel threads can run in parallel on a multiprocessor
machine. This model provides the best accuracy on concurrency and when a thread performs a blocking

23
system call, the kernel can schedule another thread for execution.

Figure 2.3 many to many models Multithreading

Many to One Model


Many-to-one model maps many user level threads to one Kernel-level thread. Thread management is
done in user space by the thread library. When thread makes a blocking system call, the entire process
will be blocked. Only one thread can access the Kernel at a time, so multiple threads are unable to run
in parallel on multiprocessors. If the user-level thread libraries are implemented in the operating
system in such a way that the system does not support them, then the Kernel threads use the many-to-
one relationship modes.

Figure 2.4 Many to one models Multithreading


One to One Model
There is one-to-one relationship of user-level thread to the kernel-level thread. This model provides more

24
concurrency than the many-to-one model. It also allows another thread to run when a thread makes a
blocking system call. It supports multiple threads to execute in parallel on microprocessors. The
disadvantage of this model is that creating user thread requires the corresponding Kernel thread. OS/2,
Windows NT and windows 2000 use one to one relationship model.

Figure 2.5 One to One models Multithreading

The difference between User-Level & Kernel-Level Thread


User-Level Threads Kernel-Level Thread
User-level threads are faster to create and Kernel-level threads are slower to create
manage. and manage.
Implementation is by a thread library at the Operating system supports creation of
user level. Kernel threads.
User-level thread is generic and can run on Kernel-level thread is specific to the
any operating system. operating system.
Multi-threaded applications cannot take Kernel routines themselves can be
advantage of multiprocessing. multithreaded.
Table 2.3 Difference between User-Level & Kernel-Level Thread
Summary
An executing program needs more than just the binary code that tells the computer what to do. The
program needs memory and various operating system resources in order to run. A “process” is what we call
a program that has been loaded into memory along with all the resources it needs to operate. The
“operating system” is the brains behind allocating all these resources, and comes in different flavors such

25
as macOS, iOS, Microsoft Windows, Linux, and Android. The OS handles the task of managing the
resources needed to turn your program into a running process.
Some essential resources every process needs are registers, a program counter, and a stack. The “registers”
are data holding places that are part of the CPU. A register may hold an instruction, a storage address, or
other kind of data needed by the process. The “program counter,” also called the “instruction pointer,”
keeps track of where a computer is in its program sequence. The “stack” is a data structure that stores
information about the active subroutines of a computer program and is used as scratch space for the
process. It is distinguished from dynamically allocated memory for the process that is known as the “heap.
Threads are sometimes called lightweight processes because they have their own stack but can access
shared data. Because threads share the same address space as the process and other threads within the
process, the operational cost of communication between the threads is low, this is an advantage. The
disadvantage is that a problem with one thread in a process will certainly affect other threads and the
viability of the process itself.
A process can have multiple threads or sequences of executions. A thread is often called a lightweight
process and is a (dynamic) component of a process. Several threads are usually created within a single
process. These threads share part of the program code and the resources that have been allocated to the
process. Most modern operating systems support multithreading feature that allows multiple threads to
execute within a single process. Multithreading enables a programmer to define several tasks in a single
process; each task is assigned to a thread.
Chapter Two Review Questions
1. What is process?
2. What is the difference between Job and Process?
3. Discuss about process and thread states.
4. What are the major elements of process control block? Explain
5. What is the synchronization process? Why we use it?
6. Explain the difference between Process and Thread?
7. What is race condition?
8. What is the meaning of Thread Priority?
9. What is thread starvation?
10. What are the advantages of multiprogramming?
11. What are the differences between multiprocessing and multiprogramming?
12. Is it possible that each thread can have its stack in multithreaded programming?
Explain.

26
13. According to Amdahl‟s Law, what will happen when the number of processors
approaches one or infinite?
14. Explain the difference between process switching and thread switching.

27
Chapter Three
Process Scheduling
Introduction
In the previous chapter you have been studied about the basic concept of process, thread, their
similarities and differences. In this chapter the basic concept of process scheduling, CPU scheduler,
Scheduling policies and Different types of Scheduling algorithm are discussed.
3.0 Objective
The goals considered in the design of a scheduling discipline. Scheduler consider fairness,
efficiency, response time, turnaround time, throughput, etc., some of these goals depends on the
system one is using for example batch system, interactive system or real-time system, etc.
At the end of this chapter the student will able to:
 Understand the basic concept of process scheduling
 Explain the types of scheduler
 Detail understand on scheduling criteria: CPU utilization, Throughput, Turnaround Time, Waiting
Time, Response Time
 Differentiate the following scheduling algorithm such as Pre-emptive and Non pre-emptive, FCFS,
SJF, RR; Multiprocessor scheduling
 Define Inter-process communication: Critical Section, Race Conditions, Mutual Exclusion, Hardware
Solution,and Strict Alternation etc.
3.1 Process scheduling Concepts
Activity 3.1
 Define Process scheduling, CPU Scheduler, Dispatcher module, and Dispatch
latency
 What are the criteria of CPU Scheduling?
 What is the purpose of Scheduling algorithm?
In a system with a multiprogramming operating system, there are usually several processes in the
ready queue waiting to receive service from the CPU. The degree of multiprogramming represents
the number of processes in memory. CPU scheduling focuses on selecting the next process from the
ready queue and allocating the CPU to that process for the duration of its current CPU burst.
Every process that requests CPU service carries out the following sequence of actions:
1. Join the ready queue and wait for CPU processing.

27
2. Execute (receive CPU service) for the duration of the current CPU burst or for the duration of
the time slice (timeout).
3. Join the I/O queue to wait for I/O service or return to the ready queue to wait for more CPU
service.
4. Terminate and exit if service is completed-Le., if there are no more CPU or I/O bursts. If more
service is required, return to the ready queue to wait for more CPU service.
3.1.1 CPU scheduler
The CPU scheduler is the part of the operating system that selects the next process to which the
CPU will be allocated, deallocates the CPU from the process currently executing, and allocates the
CPU to the newly selected process. The basic mechanism used by the scheduler defines three basic
functions:
 Insertion of processes that request CPU service into the ready queue. This queue is normally
implemented as a linked list of process control blocks (PCBs) belonging to the processes
waiting for CPU service. This queue is usually a data structure that represents a simple first-
in-first-out (FIFO) list, a set of simple lists, or a priority list. This function is carried out by
the enqueuer, a component of the scheduler.
 The occurrence of a context switch, carried out by the context switcher that saves the context
of the current process and deallocates the CPU from that process.
 The selection of the next process from the ready queue and loading its context. This can be
carried out by the dispatcher, which then allocates the CPU to the newly selected process.
Dispatcher: - dispatcher is a special program which comes into play after the scheduler. When
the scheduler completes its job of selecting a process, it is the dispatcher which takes that process to
the desired state/queue. The dispatcher is the module that gives a process control over the CPU after
it has been selected by the short-term scheduler. This function involves the following:

 Switching context
 Switching to user mode
 Jumping to the proper location in the user program to restart that program
Dispatcher different from scheduler by the following concept −
 All the processes are in a ready state with no schedule.
 At that time the scheduler used some algorithm.
 Scheduling is done for all the processes in the ready queue.
 After completing scheduling, the dispatcher enters.

28
 The dispatcher moves the selected process from the ready queue into the running state.
 The same process continues simultaneously.
 Scheduler scheduling the process, at the same time dispatcher dispatches selected processes
to the running state.

Example – There are 4 processes in the ready queue, P1, P2, P3, P4; their arrival times are t0, t1, t2,
and t3 respectively. A First in First out (FIFO) scheduling algorithm is used. Because P1 arrived
first, the scheduler will decide it is the first process that should be executed, and the dispatcher will
remove P1 from the ready queue and give it to the CPU. The scheduler will then determine P2 to be
the next process that should be executed, so when the dispatcher returns to the queue for a new
process, it will take P2 and give it to the CPU. This continues in the same way for P3, and then P4.

Figure 3.1 Dispatching technique


What is Concurrency?
Activity 3.2
 Explain the difference between parallelism and concurrency.
 Discuss the challenge in concurrency.

It refers to the execution of multiple instruction sequences at the same time. It occurs in an operating
system when multiple process threads are executing concurrently. These threads can interact with
one another via shared memory or message passing. Concurrency results in resource sharing, which
causes issues like deadlocks and resource scarcity. It aids with techniques such as process
coordination, memory allocation, and execution schedule to maximize throughput.
Principles of Concurrency
Today's technology, like multi-core processors and parallel processing, allows multiple processes
and threads to be executed simultaneously. Multiple processes and threads can access the same
memory space, the same declared variable in code, or even read or write to the same file. The
amount of time it takes a process to execute cannot be simply estimated, and you cannot predict

29
which process will complete first, enabling you to build techniques to deal with the problems that
concurrency creates. Interleaved and overlapping processes are two types of concurrent processes
with the same problems. It is impossible to predict the relative speed of execution, and the following
factors determine it:
1. The way operating system handles interrupts
2. Other processes' activities
3. The operating system's scheduling policies
Problems in Concurrency
There are various problems in concurrency. Some of them are as follows:
 Locating the programming errors: It's difficult to spot a programming error because
reports are usually repeatable due to the varying states of shared components each time the
code is executed.
 Sharing Global Resources: Sharing global resources is difficult. If two processes utilize a
global variable and both alter the variable's value, the order in which the many changes are
executed is critical.
 Locking the channel: It could be inefficient for the OS to lock the resource and prevent
other processes from using it.
 Optimal Allocation of Resources: It is challenging for the OS to handle resource allocation
properly. Concurrent system design frequently requires developing dependable strategies for
coordinating their execution, data interchange, memory allocation, and execution schedule to
decrease response time and maximize throughput.

3.2 CPU scheduling policies


Activity 3.3
 What is Scheduling Policies?
 Discuss the difference between preemptive and non-Preemptive Scheduling?
 Compare and contrast FCFS, SJF, Round Robin and other scheduling process?
 Which schedule lead to starvation?

There are two general categories of CPU scheduling policies:


 Non preemptive scheduling
 Preemptive scheduling
In non-preemptive scheduling, a process that is executing will continue until completion of its CPU

30
burst. The process will then change to its wait state for I/O service, or terminate (change to the
terminate state) and exit the system. In preemptive scheduling, the process that is executing may be
interrupted before completion of its current CPU burst and moved back to the ready queue. A
process can be interrupted for one of the following reasons:
 The allocated service interval (time slice) expires.
 Another process with a higher priority has arrived into the ready queue.
Priorities can be used with either preemptive or non-preemptive scheduling. Depending on the goals
of an operating system, one or more of various scheduling policies can be used; each will result in a
different system performance. The criteria are based on relevant performance measures and the
various scheduling policies are evaluated based on the criteria. There are several relevant
performance measures to consider:
 CPU utilization: The proportion of time that the CPU spends executing processes.
 Throughput: The total number of processes that are executed and completed during some
observation periods.
 Process average waiting time: The average of the waiting intervals of all processes that are
completed.
 Average turnaround time: The average of the intervals from arrival until completion, for
all processes.
 Average response time: The average of the intervals from the time a process sends a
command or request to the operating system until a response is received, for all processes.
This metric is used mainly in interactive systems.
 Fairness: A metric that indicates if all processes are treated in a similar manner. The
normalized turnaround time is often used for this purpose.
The most relevant scheduling policies in general-purpose operating systems are as follows:
 First-come-first-served (FCFS): The order of process arrival to the ready queue determines
the order of selection for CPU service. This policy is normally single class and non-
preemptive.
 Shortest job first (SJF): The process with the shortest CPU burst is the one selected next
from the ready queue. Also called shortest process next (SPN), it is typically considered a
multiclass and a non-preemptive scheduling policy.

31
 Longest job first (L JF): The process with the longest CPU burst is selected next from the
ready queue. Also called longest job next (LJN), it is considered a multiclass policy and
typically a non-preemptive scheduling policy.
 Priority scheduling: A priority is assigned to each type of process. The process with the
highest priority is the one selected next. These scheduling policies are multiclass and can be
preemptive or non-preemptive.
 Round robin (RR): Processes are basically selected in the same order of arrival to the ready
queue but can only execute until the time slice expires. The interrupted process is placed at
the back of the ready queue. This scheduling policy can be single-class or multiclass, and it
is the most common preemptive scheduling policy used in time-sharing systems.
3.2.1 First Come First Serve (FCFS)
The process that request the CPU first is holds the CPU first. If a process request the CPU then it is
loaded into the ready queue, connect CPU to that process. There is then a context switch and the
new process is started immediately.
 Jobs are executed on first come, first served basis.
 It is a non-preemptive scheduling algorithm.
 Easy to understand and implement.
 Its implementation is based on FIFO queue.
 Poor in performance, as average wait time is high.
Process Burst Time
P1 24
P2 3
P3 3
Suppose that the processes arrive in the order: P1, P2, P3 The Gantt Chart for the schedule is:
P1 P2 P3
0 24 27 30
Waiting time for P1 = 0; P2 = 24; P3 = 27
Average waiting time: (0 + 24 + 27)/3 = 17
Suppose that the processes arrive in the order P2, P3, P1. The Gantt chart for the schedule is:

P2 P3 P1
0 3 6 30

32
Waiting time for P1 = 6; P2 = 0; P3 = 3
Average waiting time: (6 + 0 + 3)/3 = 3
 Much better than previous case. Convoy effect short process behind long process

Example 1
Wait time of
each process
is as follows:

Process Wait time = Service time -Arrival time


P0 0–0=0
P1 5-1 = 4
P2 8–2=6
P3 16 – 3 = 13
Average waiting time (AWT) = (0 + 4 + 6 + 13)/4 = 23/4 = 5.75
3.2.2 Shortest Job First
Shortest process next (SPN), also known as shortest job first (SJF), is a scheduling policy in
which the scheduler selects from the ready queue the process with the shortest CPU service time
interval (burst). This scheduling policy can be considered multiclass because the scheduler gives
preference to the group of processes with the shortest CPU burst. It is also a non-preemptive
scheduling policy. An internal priority is used for each group or class of processes. The operating
system assigns a higher priority to the group of processes that has the shortest CPU service time
interval (or CPU burst). In other words, the scheduler gives preference to the groups of processes
with shorter CPU bursts over other groups of processes. This scheduling policy is not fair compared
to FCFS scheduling. Shortest process next scheduling is probably optimal because it results in the
minimum wait time for processes. However, when processes with shorter service periods continue
arriving into the ready queue, the processes with longer service demand periods may be left waiting
indefinitely. This situation is known as starvation.
SJF is optimal – gives minimum average waiting time for a given set of processes.

33
Process Arrival Time Burst Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
SJF (non-preemptive)
P1 P3 P2 P4
0 7 8 12 16
Average waiting time = [0 +(8-2) +(7-4) +(12-5)] /4 =4

Example of SJF or
SPF

Wait time of each process is as follows:


Process Wait Time: Service Time - Arrival
Time
P0 3–0=3
P1 0–0=0
P2 16 – 2 = 14
P3 8–3=5
Average Wait Time: (3+0+14+5) / 4 = 5.50
3.2.3 Round Robing Scheduling
Round robin (RR) scheduling is used in time-sharing systems. It is the most common of the
preemptive scheduling policies. Every process is allocated the CPU for a short-fixed interval called
the time quantum, or time slice. After this short interval expires, the process that is executing
(receiving CPU service) is interrupted by the operating system. The time slice is usually much shorter
than the average CPU burst of the processes. When the time slice expires, the scheduler carries out a
context switch to the next process selected from the ready queue. After a process executes for the

34
duration of the time slice, it is interrupted and cycled back to the ready queue. In this manner, the
ready queue is treated as a circular queue. A process will continue to cycle through the CPU and ready
queue until it completes its current CPU burst.
The operating system using this scheduling scheme attempts to allocate the CPU in a uniform manner
to all processes in the ready queue for a fixed short interval (the time slice). Thus, all processes in the
ready queue are given an equal chance to receive service from the CPU for a short-fixed period. The
main advantage that this policy provides to users is interactive computing. The time quantum (or time
slice) is considered a system parameter. Its value is usually less than the CPU burst for most
processes, and it is much longer than the context switch time. H the time quantum is too long or too
short, performance will be affected significantly.
Example 1 of RR with Time Quantum = 4
Process Burst Time
P1 24
P2 3
P3 3
The Gantt chart is:
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
Average waiting time = [(30-24) +4+7]/3 = 17/3 =5.66
Example 2 of Round Robing
Consider the following three Round-robin scheduling processes (4 point).
Process Queue Burst time
P1 4
P2 3
P3 5

Based on above three processes:-

a) Calculate the average waiting time for processes when time quantum(time slice=2)
and show an expected step
b) List at least three advantages of Round-robin scheduling processes

3.2.4 Shortest Remaining Time

35
Shortest remaining time (SRT), also known as shortest remaining time first (SRTF), is a preemptive
version of SPN scheduling. With this scheduling policy, a new process that arrives will cause the
scheduler to interrupt the currently executing process if the CPU service time interval of the newly
arrived process is less than the remaining service time interval of the process currently executing
(receiving CPU service). A context switch occurs and the new process is started immediately.
When a process completes CPU service, the next process selected from the ready queue is the one
with the shortest remaining service time. The scheduler selects from the ready queue the process
with the shortest CPU service period (burst). As with SPN, this scheduling policy can be considered
multiclass because the scheduler gives preference to the group of processes with the shortest
remaining service time and the processes with the shortest CPU burst. An internal priority is used
for each group or class of processes. The operating system assigns the highest priority to the groups
of processes that have the shortest CPU service period (or CPU burst). In other words, the scheduler
gives preference to the groups of processes with shorter CPU bursts over other groups of processes.
This scheduling policy is not fair compared to FCFS and RR scheduling. When processes with
shorter service time continue arriving into the ready queue, the processes with longer service
demand times will always be interrupted and may be left waiting indefinitely. As mentioned before,
this situation is known as starvation. This is Example
Process Arrival Time Burst Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
SRT (preemptive)
P1 P2 P3 P2 P4 P1
0 2 4 5 7 11 16
Average waiting time = (9 + 1 + 0 +2)/4 =3

3.2.5 Dynamic Priority Scheduling


In dynamic priority scheduling, the CPU scheduler dynamically adjusts the priority of a process as it is
executing. The typical approach is to adjust the priority based on the level of expectation that the process will

36
carry out a system call (typically an I/O request). However, this requires the CPU scheduler to predict future
process requests. Although we cannot precisely predict the future, we can use an approximation (also known
as a heuristic) that is based on observed program behavior: A process will tend to carry out in the near future
what it has done in the recent past. Thus, a process that has just carried out an I/O operation will tend to
request another I/ 0 operation. This leads to the following algorithm:
1. Allocate the CPU to the highest priority process.
2. When a process is selected for execution, assign it a time slice.
3. If the process requests an I/ 0 operation before the time slice expires, raise its\priority (i.e.,
assume it will carry out another I/O request soon).
4. If the time slice expires, lower its priority (i.e., assume it is now in a CPU burst) and allocate
the CPU to the highest priority ready process.
Some operating systems that implement dynamic priority scheduling will use a fixed time slice value. Other
operating systems will make the time slice a function of the priority (giving a shorter time slice to processes
that are expected to perform I/O, thus allowing a relatively quick decision that the process is now
computing). See
Example given below
and try it by your-self
then check your answer
with done answer

Example of priority
based scheduling

Wait time of each process is as follows:


Process Wait Time: Service time - Arrival time
P0 9–0=9
P1 6–1=5
P2 14 – 2 = 12
P3 0–0=0
Average Wait Time: (9+5+12+0) / 4 = 6.5
3.3 Other scheduling Policies
3.3.1 Longest job first
Longest process next (LPN) scheduling, also known as longest job first (LJF), is not a very common

37
scheduling policy. Similar to SPN scheduling, it is a multiclass scheduling policy that can be
preemptive or non-preemptive. The only difference with SPN is that higher priorities are assigned to
the group of processes with longer CPU bursts. LPN scheduling does not exhibit the fairness shown by
FCFS scheduling as processes are given different priorities. Processes with low average CPU service
demand may starve or may have to wait too long compared to the processes in the other groups.
3.3.2 Real-Time Scheduling Policies
Real-time systems are ones that continuously interact with an external environment. In these systems, the
behavior is defined by the specified timing constraints. Real-time systems are sometimes known as
reactive systems. One of the goals of real-time scheduling is to guarantee fast response of the high
priority real-time processes. The second general goal of real-time scheduling is to guarantee that the
processes can be scheduled in some manner in order to meet their individual deadlines. The performance
of the system is based on this guarantee. A real-time process has a deadline requirement. This process
will normally have relatively high priority and must complete its service before the deadline expires. A
real-time process can be periodic or sporadic. A periodic process is started every p time units. This
specific time interval is known as the period. The other two relevant timing properties of a periodic
process are its computation time requirement, c, and its deadline, d. The process must complete
execution before its deadline expires. A sporadic process is normally started by an external random
event. After the occurrence of the specified event, the process must start and complete before its deadline
expires. Real-time scheduling normally includes priorities and preemption. There are two widely known
real-time scheduling policies: rate monotonic and the earliest deadline first. With rate monotonic
scheduling (RMS), priorities of the processes are statically assigned in reverse order of period length.
Higher priorities are assigned to processes with shorter periods, which implies that more frequently
executing processes are given higher priority. With earliest deadline first scheduling (EDFS), the
priorities of the processes are assigned statically or dynamically. Processes with earlier deadlines are
given higher priorities.
3.3.3 Multiprocessor Systems
A multiprocessor computer system has two or more processors. The main goal of these systems is to
improve the overall performance of the system. There are two general categories of multiprocessor
computer systems: tightly coupled and loosely coupled. Tightly coupled computer systems have two or
more processors that share the system main memory or a common block of memory, and are controlled
by the operating system of the computer system. Loosely coupled computer systems are composed of
several semi-autonomous units, each with a processor, memory, and communication facilities.

38
The actual operation and performance of multiprocessor systems depend on the granularity of the
configuration. This relates to the synchronization needed among the various processes that execute
concurrently or simultaneously. On one extreme (an ideal situation), there are several processes
executing in the system; each process is allocated a processor and the execution is completely
independent of the other processes. The other extreme is a very fine granularity of parallelism, in which
a task needs parallel computing to perform its complex algorithm. Between these extreme levels of
granularity, we can identify course, medium, and fine levels of parallelism. Coarse granularity is used
with concurrent processes that need some level of synchronization to share resources and/or to
communicate. Medium granularity is used with threads that are executed concurrently and that need
synchronization. A multicore processor is a processing unit composed of two or more cores. Each core
is capable of executing instructions. Computer systems may be designed using cores configured tightly
or loosely. Multicore processors are used in many applications. As with general multiprocessor
systems, performance gained by the use of a multicore processor depends very much on the software
algorithms and implementation. Many typical applications, however, do not realize significant speedup
factors. The parallelization of software is a significant ongoing topic of research.
One of the goals of conventional processor scheduling is to keep execution units busy by assigning
each processor a thread to run. Recent research on scheduling multicore systems focus on high
utilization of on-chip memory, rather than of execution cores, to reduce the impact of expensive
DRAM and remote cache accesses. A simplified model of a multiprocessor system consists of a single
ready queue and follows a FCFS scheduling policy. This model is an example of a model of a
multiprocessor system with coarse granularity.
Summary
An important, although rarely explicit, function of process management is processor allocation. Three
different schedulers may coexist & interact in a complex operating system: long-term scheduler,
medium-term scheduler, & short-term scheduler. Of the presented scheduling disciplines, FCFS
scheduling is the easiest to implement but is a poor performer. SRTN scheduling is optimal but
unrealizable. RR scheduling is most popular in time-sharing environments, & event-driven & earliest-
deadline scheduling are dominant in real-time & other systems with time-critical requirements.
Multiple-level queue scheduling, & its adaptive variant with feedback, is the most general scheduling
discipline suitable for complex environments that serve a mixture of processes with different
characteristics.

 Long-term scheduling: the decisions to introduce new processes for execution, or re-

39
execution.
 Medium-term scheduling: the decision to add to (grow) the processes that are fully
or partially in memory.
 Short-term scheduling: the decisions as to which (Ready) process to executenext.
 Non-preemptive scheduling: In non-preemptive scheduling, process will continue to
execute until it terminates, or makes an I/O request which would block the process, or
makes an operating system call.
 In preemptive scheduling, the process may be pre-empted by the operating system
when a new process arrives (perhaps at a higher priority), or an interrupt or signal
occurs, or a (frequent) clock interrupt occurs.
Chapter Three Review Questions
1. Discuss various process scheduling policies with their cons & pros.

2. Define process. What is the difference between a process & a program?Explain.


3. What are the different states of a process? Explain using a process state
transition diagram.
4. Which type of scheduling is used in real life operating systems? Why?

5. Which action should the short-term scheduler take when it is invoked but noprocess
is in the ready state? Is this situation possible?
6. How can we compare performance of various scheduling policies beforeactually
implementing them in an operating system?
7. SJF is a sort of priority scheduling. Comment.

8. What do you understand by starvation? How does SJF cause starvation?What is


the solution of this problem?

9. What qualities are to be there in a scheduling policy? Explain.

10. Differentiate between user-oriented scheduling criteria & system-orientedscheduling


criteria.
11. Shown below is the workload for 5 jobs arriving at time zero in the order given below −

Job Burst Time


1 10
2 29

40
3 3
4 7
4 12
Now find out which algorithm among FCFS, SJF and Round Robin with quantum 10, would
give the minimum average time.
12. Explain pseudo parallelism. Describe the process model that makes parallelism easier
to deal with.

41
Chapter Four
Deadlock
Introduction
In the previous chapter you have been studied about the basic concept of process scheduling, CPU scheduler,
Scheduling policies and Different types of Scheduling algorithm. In this chapter, the definition of deadlock,
necessary and sufficient condition of deadlock, Resource Allocation Graph, Dealing with deadlock prevention,
deadlock avoidance and deadlock recoveries are discussed.
Objectives
The objectives of this chapter are to make the students acquainted with the problem of deadlocks. In
this chapter, we characterize the problem of deadlocks and discuss policies, which an OS can use to
ensure their absence. Deadlock detection, resolution, prevention and avoidance have been discussed in
detail in the present lesson. After studying this lesson the students will be familiar with following:
 Condition for deadlock.
 Deadlock prevention
 Deadlock avoidance
 Deadlock detection and recovery
 Evaluating the Approaches to Dealing with Deadlock

4.1 Definition
Activity 4.1
 What is deadlock?
 How can we differentiate deadlock from starvation?
 Discuss the four conditions for deadlock existence.

A deadlock involving a set of processes D is a situation in which:

(a) Every process Pi in D is blocked on some event Ei.

(b) Event Ei can be caused only by action of some processes in D.

A set of process is in a 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 be awakened. It is important to note that the
number of processes and the number and kind of resources possessed and requested are unimportant.

42
The resources 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. The simplest example of deadlock is where process 1 has been allocated non- shareable
resources A, say, a tap drive, and process 2 has be allocated 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 is blocked the other
and all useful work in the system stops. This situation is termed deadlock. The system is in deadlock state
because each process holds a resource being requested by the other process neither process is willing to
release the resource it holds.
4.2 Pre-emptible and Nonpreemptable Resources
Resources come in two flavors: pre-emptible and nonpreemptable. A pre-emptible resource
is one that can be taken away from the process with no ill effects. Memory is an example of a pre-
emptible resource. On the other hand, a nonpreemptable resource is one that cannot be taken away from
process (without causing ill effect). For example, CD resources are not pre-emptible at an arbitrary
moment.
Reallocating resources can resolve deadlocks that involve pre-emptible resources. Deadlocks that
involve nonpreemptable resources are difficult to deal with.
4.3 Necessary and Sufficient Deadlock Conditions
Coffman i d e n t i f i e d four (4) conditions that must hold simultaneously for there to be a deadlock.

1. Mutual Exclusion Condition


The resources involved are non-shareable. At least one resource (thread) 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 process must be delayed until the resource has been released.
2. Hold and Wait Condition
Requesting process hold already, resources while waiting for requested resources. There must
exist a process that is holding a resource already allocated to it while waiting for additional resource that
are currently being heldby other processes.
3. No-Preemptive Condition
Resources already allocated to a process cannot be preempted. Resources cannot be removed from the
processes are used to completion or released voluntarily by the process holding it.

5. Circular Wait Condition

43
Activity 4.2
 What is a critical region?
 How do they relate to controlling access to shared resources?
 How critical/race condition problem can be solved?

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. Condition 1 and 3 pertain to resource utilization policies,
while condition 2 pertains to resource requirements of individual processes. Only condition 4 pertains to
relationships between resource requirements of a group of processes. As an example, consider the traffic
deadlock in the following figure.

Figure 4.1 example of Deadlock

Consider each section of the street as a resource.

1. Mutual exclusion condition applies, since only one vehicle can be on a section of the street at
a time.
2. 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.
3. No-preemptive condition applies, since a section of the street that is a section of the street
that is occupied by a vehicle cannot be taken away from it.
4. Circular wait condition applies, since each vehicle is waiting on the next vehicle to move.
That is, each vehicle in the traffic is waiting for a sectionof 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 is assured that it will not have to stop inside the intersection.

44
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 in a process, because it is the process that holds resources, not the thread that is, each thread has
access to the resources held by the process.
4.4 Resource-Allocation Graph
The deadlock conditions can be modeled using a directed graph called a resource allocation graph
(RAG). A resource allocation graph is a directed graph. It consists of 2 kinds of nodes:
 Boxes: Boxes represent resources, and Instances of the resource are represented as dots within the
box i.e. how many units of that resource exist in the system.
 Circles: Circles represent threads / processes. They may be a user process or a system process.
An edge can exist only between a process node and a resource node. There are 2 kinds of (directed)
edges:

 Request edge: It represents resource request. It starts from process and terminates to a resource.
It indicates the process has requested the resource,and is waiting to acquire it.
 Assignment edge: It represents resource allocation. It starts from resource instance and terminates
to process. It indicates the process is holding the resource instance. When a request is made, a
request edge is added. When request is fulfilled, the request edge is transformed into an assignment
edge. When process releases the resource, the assignment edge is deleted.
4.4.1 Interpreting a Resource Allocation Graph with Single Resource Instances
Following figure shows a resource allocation graph. If the graph does not contain a cycle, then no
deadlock exists. Following figure is an example of a no deadlock situation.

R1 R2

O O

P1 P2 P3

O O

R3 R4

If the graph does contain a cycle, then a deadlock does exist. As following resource allocation
graph depicts a deadlock situation.

45
O O

P1 P2 P3

R3 R4

With single resource instances, a cycle is a necessary and sufficient conditionfor deadlock.
4.5 Dealing with Deadlock
Activity 4.3
 What are the solutions used for deadlock prevention?

There are following approaches to deal with the problem of deadlock.


 The Ostrich Approach: this approach can be quite useful if you believe that they are rarest
chances of deadlock occurrence. In that situation it is not a justifiable proposition to invest a lot
in identifying deadlocks and tackling with it. Rather a better option is ignore it.
 Deadlock detection algorithms: This approach detects when deadlock hasoccurred.
 Deadlock recovery algorithms: After detecting the deadlock, it breaks thedeadlock.
 Deadlock avoidance algorithms: This approach considers resources currently available, resources
allocated to each thread, and possible future requests, and only fulfill requests that will not lead to
deadlock.
 Deadlock prevention: This approach prevents deadlock from occurring by eliminating one of
the four (4) deadlock conditions.
4.6 Deadlock Prevention
Deadlock prevention is based on designing resource allocation policies, which make deadlocks
impossible. Use of the deadlock prevention approach avoids the over- head of deadlock detection and
resolution. However, it incurs two kinds of costs - overhead of using the resource allocation policy,
and cost of resource idling due to the policy. As described in earlier section, four conditions must hold for a
resource deadlock to arise in a system:

 Non-shareable resources

 Hold-and-wait by processes

 No preemption of resources

46
 Circular waits.

Ensuring that one of these conditions cannot be satisfied prevents deadlocks.We first discuss how each
of these conditions can be prevented and then discuss a couple of resource allocation policies based on the
prevention approach. 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.
4.6.1 Elimination of Mutual Exclusion Condition
The mutual exclusion condition must hold for non-sharable resources. That is, several processes cannot
simultaneously share a single resource. This condition is difficult to eliminate because some resources,
such as the tap drive 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.
4.6.2 Elimination of Hold and Wait Condition
There are two possibilities for elimination of the second condition. The first alternative is that a process
request be granted all of 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 of 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 derives 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 (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.
4.6.3 Elimination of No-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 may
use them to finish. 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 second process may hold the resources needed by
the first process. This is 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, if

47
necessary, request them again together with additional resources. Implementation of this strategy denies
the “no-preemptive” condition effectively.
The main drawback of this approach is 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.
4.6.4 Elimination of Circular Wait Condition
The Presence of a cycle in resource allocation graph indicates the “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
impose a total ordering of all resources types, and to require that each process requests resources in a
numerical order (increasing or decreasing) 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
Card
1 ≡
Reader
2 ≡ Printer
3 ≡ Plotter
4 ≡ Tape drive
Card
5 ≡
Punch
Table 4.1 global Numbering in deadlock
Now the rule is this: processes can request resources whenever they want to, but all requests must be
made in numerical order. A process may request first printerand 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 it may be
impossible to find an ordering that satisfies everyone. The resource ranking policy works best when all
processes require their resources in the order of increasing ranks. However, difficulty arises when a
process requires resources in some other order. Now processes may tend to circumvent such difficulties
by acquiring lower ranking resources much before they are actually needed. In the worst case this policy
may degenerate into the „all requests together‟ policy of resource allocation. Anyway this policy is
attractive due to its simplicity once resourceranks have been assigned.

48
“All requests together” is the simplest of all deadlock prevention policies. A process must make its
resource requests together-typically, at the start of its execution. This restriction permits a process to
make only one multiple request in its lifetime. Since resources requested in a multiple request are
allocated together, a blocked process does not hold any resources. The hold-and-wait condition is
satisfied. Hence paths of length larger than 1 cannot exist in the Resource Allocation Graph, a mutual
wait-for relationships cannot develop in the system. Thus, deadlocks cannot arise.
4.7 Deadlock Avoidance
Activity 4.4
 What must be known by the banker's algorithm to avoid deadlock?
 Discuss about safe and unsafe state.
 What make banker’s algorithm differ from other algorithm?

This approach to the deadlock problem anticipates deadlock before it actually occurs. This approach
employs an algorithm to access the possibility that deadlock could occur and acting accordingly. This
method differs from deadlock prevention, which guarantees that deadlock cannot occur by denying one
of the necessary conditions of deadlock.
If the necessary conditions for a deadlock are in place, it is still possible to avoid deadlock by being
careful when resources are allocated. Perhaps the most famous deadlock avoidance algorithm, due to
Dijkstra, is the Banker‟s algorithm. So named because the process is analogous to that used by a banker
in deciding if a loan can be safely made.
4.7.1 Banker’s Algorithm

In this analogy

Customers ≡ Processes
resources,
Units ≡
say, tape drive
Operating
Banker ≡
System

Customers Used Max


A 0 6

B 0 5 Available

49
C 0 4 Units = 10
D 0 7
In the above figure, we see four customers each of whom has been granted a number of credit
units. The banker reserved only 10 units rather than 22 units to service them. At certain
moment, the situation becomes
Customers Used Max
A 1 6
B 1 5 Available

C 2 4 Units = 2
D 4 7
Safe State The key to a state being safe is that there is at least one way for all users to finish. In other
analogy, the state of figure 2 is safe because with 2 units left, the banker can delay any request except
C's, thus letting C finish and release all four resources. With four units in hand, the banker can let
either D orB have the necessary units and so on.
Unsafe State Consider what would happen if a request from B for one more unit were granted in above
figure.
We would have following situation

Customers Used Max


A 1 6
B 2 5 Available

C 2 4 Units = 1

D 4 7
This is an unsafe state.

If all the customers namely A, B, C, and D asked for their maximum loans, then banker 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 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 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 postponed until later. Haberman has shown that

50
executing of the algorithm has 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.
4.7.2 Evaluation of Deadlock Avoidance Using the Banker’s Algorithm
There are following advantages and disadvantages of deadlock avoidance using Banker‟s
algorithm.
Advantages:

 There is no need to preempt resources and rollback state (as in deadlock detection &
recovery)
 It is less restrictive than deadlock prevention
Disadvantages:

 In this case maximum resource requirement for each process must bestated in advance.
 Processes being considered must be independent (i.e., unconstrained by synchronization
requirements)
 There must be a fixed number of resources (i.e., can‟t add resources, resources can‟t break)
and processes (i.e., can‟t add or delete processes)
 Huge overhead Operating system must use the algorithm every time aresource is requested. So a
huge overhead is involved.
4.8 Deadlock Detection
Deadlock detection is the process of actually determining that a deadlock exists and identifying the
processes and resources involved in the deadlock. The basic idea is to check allocation against
resource availability for all possible allocation sequences to determine if the system is in deadlocked
state a. Of course, the deadlock detection algorithm is only half of this strategy. Once a deadlock is
detected, there needs to be a way to recover several alternatives exists:
 Temporarily prevent resources from deadlocked processes.
 Back off a process to some check point allowing preemption of a neededresource and restarting
the process at the checkpoint later.
 Successively kill processes until the system is deadlock free.
These methods are expensive in the sense that each iteration calls the detection algorithm until the
system proves to be deadlock free. The complexity of algorithm is O(N2) where N is the number of
proceeds. Another potential problemis starvation; same process killed repeatedly.
4.8.1 Deadlock Recovery
Once you've discovered that there is a deadlock, what do you do about it? One thing to do is simply re-boot.

51
A less drastic approach is to yank back a resource from a process to break a cycle. As we saw, if there are
no cycles, there is no deadlock. If the resource is not pre-emptible, snatching it back from a process may
do irreparable harm to the process. It may be necessary to kill the process, under the principle that at least that's better
than crashing the whole system.
Sometimes, we can do better. For example, if we checkpoint a process from timeto time, we can roll it back
to the latest checkpoint, hopefully to a time before it grabbed the resource in question. Database systems use
checkpoints, as well as a technique called logging, allowing them to run processes “backwards,” undoing
everything they have done. It works like this: Each time the process performs an action, it writes a log
record containing enough information to undo the action. For example, if the action is to assign a value to a
variable, the log record contains the previous value of the record. When a database discovers a deadlock, it
picks a victim and rolls it back.
Rolling back processes involved in deadlocks can lead to a form of starvation, if we always choose the same
victim. We can avoid this problem by always choosing the youngest process in a cycle. After being rolled
back enough times,a process will grow old enough that it never gets chosen as the victim--at worst by the
time it is the oldest process in the system. If deadlock recovery involves killing a process altogether and
restarting it, it is important to mark the “starting time” of the reincarnated process as being that of its
original version, so that itwill look older that new processes started.
4.9 Synchronization
Synchronization is the coordination of the activities of two or more processes that usually need to carry out the
following activities:
 Compete for resources in a mutually exclusive manner.
 Cooperate in sequencing or ordering specific events in their individual activities.
No synchronization
When two or more processes execute and independently attempt to simultaneously share the same
resources, their executions generally will not produce correct results. Since synchronization is absent,
the results of these process executions depend on their relative speeds and on the order of use of the
resources.
Summary
 A set of process is in a deadlock state if each process in the set is waiting for an event that can be
caused by only another process in the set. Processes compete for physical and logical resources
in the system. Deadlock affects the progress of processes by causing indefinite delays in resource
allocation.
 There are four Necessary and Sufficient Deadlock Conditions (1) Mutual Exclusion

52
 Condition: The resources involved are non-shareable, (2) Hold and Wait Condition:

Requesting process hold already, resources while waiting for requested resources,(3) No-
Preemptive Condition: Resources already allocated to a process cannot be preempted,(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.
 The deadlock conditions can be modeled using a directed graph called a resource allocation graph
(RAG) consisting of boxes (resource), circles (process) and edges (request edge and assignment
edge). The resource allocation graph helps in identifying the deadlocks.
 There are following approaches to deal with the problem of deadlock: (1) The Ostrich Approach
stick your head in the sand and ignore the problem, (2) Deadlock prevention which prevent
deadlock from occurring by eliminating one of the 4 deadlock conditions, (3) Deadlock detection
algorithms detect when deadlock has occurred, (4) Deadlock recovery algorithms break the
deadlock, (5) Deadlock avoidance algorithms consider resources currently available, resources
allocated to each thread, and possible future requests, and only fulfill requests that will not lead to
deadlock
 There are merits/demerits of each approach. The Ostrich Approach is a good solution if deadlock is
not frequent. Deadlock prevention may be overly restrictive. In Deadlock detection and recovery
there is a tradeoff between frequency of detection and performance / overhead added, Deadlock
avoidance needs too much a priori information and not very dynamic (can‟t add processes or
resources), and involves huge overhead.

53
Chapter Four Review Questions
1. What do you understand by deadlock? What are the necessary conditions for deadlock?
2. What do you understand by resource allocation graph (RAG)? Explain using suitable
examples, how can you use it to detect the deadlock?
3. Compare and contrast the following policies of resource allocation:

(a) All resources requests together.

(b) Allocation using resource ranking.

(c) Allocation using Banker‟s algorithm


4. How can pre-emption be used to resolve deadlock?

5. Why Banker‟s algorithm is called so?

6. Under what condition(s) a wait state becomes a deadlock?

7. Explain how mutual exclusion prevents deadlock.

8. Discuss the merits and demerits of each approach dealing with theproblem of deadlock.
9. Differentiate between deadlock avoidance and deadlock prevention.

10. A system contains 6 units of a resource, and 3 processes that need to use this resource. If the
maximum resource requirement of each process is 3 units, will the system be free of deadlocks for
all time?

54
Chapter Five
Memory Management

Introduction
In previous chapter you have been studied about the definition of deadlock, necessary and sufficient
condition of deadlock, Resource Allocation Graph, Dealing with deadlock prevention, deadlock avoidance and
deadlock recoveries. In this chapter memory management, static and dynamic loading, contiguous memory
allocation, non-contiguous memory allocation, virtual memories, paging policies and thrashing are discussed.
5.1 Objectives
The main goal of memory management is to keep track of the status of memory location, either
allocated or free. It determines how memory is allocated among competing processes, deciding which
gets memory, when they receive it, and how much they are allowed.
At the end of the chapter the students should be able to:
 Define the memory management
 Differentiate Static and Dynamic loading
 Essay mechanism of memory management in operating system
 Explain the virtual memory and implementation concept
 Explain the relocation policy in memory management
 Detail understanding on partition, swapping, segmentation, paging policy and etc.
 Analyzing thrashing and catching mechanism in Operating system
5.2 Memory Management
Activity 5.1
 What is Memory management?
 What are the major activities of an operating system in regard to memory
management?

The major tasks of the memory manager are the allocation and deallocation of main memory. Because
main memory is one of the most important resources in a computer system, the management of
memory can significantly affect the performance of the computer system. Memory management is an
important part of the functions of the operating system.
In simple operating systems without multiprogramming, memory management is extremely primitive;

55
memory is allocated to only one program (or job) at a time. In early operating systems with
multiprogramming, memory was divided into a number of partitions-blocks of contiguous memory that
could be allocated to a process. The degree of multiprogramming determines the number of partitions
in memory (i.e., the maximum number of processes that can reside in memory). When a process
completes and terminates, memory is deallocated and the partition becomes available for allocation to
another process.
One of the problems present in memory management with partitions is memory fragmentation the
existence of some amount of allocated memory that is not used by the process, or of relatively small
memory blocks that cannot be allocated to a process. This problem reduces the memory utilization and
can affect other system performance metrics.
Process Address Space
A logical address is a reference to some location of a process. The process address space is the set of
logical addresses that a process references in its code. The operating system provides a mechanism that
maps the logical addresses to physical addresses. When memory is allocated to the process, its set of
logical addresses will be bound to physical addresses. Three types of addresses are used in a program
before and after memory is allocated:
1. Symbolic addresses: The addresses used in a source program. The variable names, symbolic
constants, and instruction labels are the basic elements of the symbolic address space.
2. Relative addresses: A compiler converts symbolic addresses into relative addresses.
3. Physical addresses: The final address generated when a program is loaded and ready to execute
in physical memory; the loader generates these addresses.
Binding
The memory manager allocates a block of memory locations to the absolute program and the loader
moves the program into the allocated memory block. At load time, the absolute program is loaded
starting at a particular physical address in main memory. The relocatable addresses are mapped to
physical addresses; each logical address is bound to a physical address in memory. The absolute
program becomes an executable program right after its logical addresses are translated (mapped) to the
corresponding physical addresses.
The general address translation procedure is called address binding. If the mapping (or conversion) of
logical addresses to physical addresses is carried out before execution time, it is known as early or
static binding. A more advanced binding technique delays the mapping from logical to physical
addresses until the process starts to execute. This second type of binding is called late or dynamic

56
binding.
5.3 Static and Dynamic Loading
Activity 5.2
 Define the Static and dynamic Loading?
 Essay the similarities and difference between Static and dynamic loading?

With static loading, the absolute program (and data) is loaded into memory in order for execution to
start. With dynamic loading, the modules of an external library needed by a program are not loaded
with the program. The routines of the library are stored on a disk in relocatable form and are loaded
into memory only when they are needed by the program. The main advantage of dynamic loading is the
improved memory utilization.
Static and Dynamic Linking
When static linking is used, the linker combines all other modules needed by a program into a single
absolute load module before execution of the program starts. When dynamic linking is used, the
building of the absolute form of a program is delayed until execution time. For every call to a routine of
a library, a stub is executed to find the appropriate library in memory. This type of linking is commonly
used with shared libraries such as Dynamic Linked Libraries (DLL). Only a single copy of a shared
library is needed in memory.
5.4 Contiguous Memory Allocation
Operating systems with multiprogramming and simple memory management divide the system
memory into partitions-blocks of contiguous memory, each one allocated to an active process. The
degree of multiprogramming is determined by the number of partitions in memory. As mentioned
before, when a process completes and terminates, its memory space is deallocated and that amount of
memory becomes available. Known as memory partitioning, this type of memory management was
used in the early multiprogramming operating systems. In addition to the allocation and deallocation of
partitions to and from processes, the memory manager also provides two additional basic functions.
The first is the protection of the memory space in the partition allocated to a process from the memory
references generated by a process in a different partition. The second function is the management of
shared memory in a partition by two or more processes.
Partitioned memory allocation can be fixed or dynamic, depending on whether the partitions are fixed-
sized or variable-sized blocks of memory. With fixed partitions, the number of partitions is fixed; with
variable partitions, the number and size of partitions vary because these are dynamically created when
memory is allocated to a process.

57
5.4.1 Fixed Partitions
In this memory management scheme, memory is divided into fixed-sized partitions that are not
normally of the same size. One partition is allocated to each active process in the multiprogramming
set. The number and the size of the partitions are fixed. There is one special partition, the system
partition, in which the memory-resident portion of the operating system is always stored. The rest of the
partitions are allocated to user processes.
An important problem in memory allocation with fixed partitions is fragmentation, the portion of
memory allocated but not used. The unused portion of memory inside a partition is called internal
fragmentation. The selection of a process from the input queue allocating a partition to it is an
important issue in memory allocation. There are two general techniques for this:
 A technique using a queue for every partition. A process will be assigned the smallest partition
large enough for the process. This technique minimizes the internal fragmentation.
 A technique using a single queue for the processes waiting for memory. The next process is
selected from the queue and the system assigns the smallest available partition to the process.
5.4.2 Dynamic Partitions
Dynamic partitioning is a memory management scheme that uses variable size partitions; the system
allocates a block of memory sufficiently large according to the requirements of a process. The partition
is created dynamically, when there is sufficient memory available. The number of partitions is also
variable. The memory manager allocates memory to requesting processes until there is no more
memory available or until there are no more processes waiting for memory. Assume that memory was
allocated to processes P6, P5, P2, P3, and P4, in that order. The five partitions were created
dynamically, and the amount of memory left is located at the top of memory. Contiguous blocks of
available (unallocated) memory are called holes. If a hole is sufficiently large, it can be allocated to a
process of the same or smaller memory size.
In dynamic partitioning, the holes represent the memory that is available, and if they are too small, they
cannot be allocated. They represent external fragmentation. The total fragmentation in this case is 75K
+ 200K. The problem is that this total amount of memory is not contiguous memory; it is fragmented.
The operating system can use several techniques to allocate holes to processes requesting memory. The
first technique is called best-fit: It selects the hole that is closest in size to the process. The second
technique is called first-fit: It selects the first available hole that is large enough for the process. The
third technique is called next-fit: It selects the next available hole that is large enough for the process,
starting at the location of the last allocation. The most appropriate allocation technique is not easy to

58
determine. It depends on the arrival sequence of processes into the input queue and their corresponding
sizes.
Dynamic partitioning requires the system to use dynamic relocation, a facility to relocate processes in
memory, even after execution has begun. This is considered late binding. With hardware support, the
relocation of the relative addresses can be performed each time the CPU makes a reference to memory,
during the execution of a process.
5.4.3 Swapping
Dynamic relocation is also important in swapping. When a process is blocked (suspended) while
waiting for I/O service, the system assumes that the process will not become ready for a relatively long-
time interval. The system can swap out (or move) the blocked process to secondary storage (disk) and
make that memory available to other processes. At some later time, the system swaps back the process
from the secondary storage to main memory. The locations in memory into which the process is
swapped back are not normally the same locations where the process was originally stored in main
memory.
Performance is usually affected by swapping. The total overhead time includes the time it takes to
move the entire process to a disk and to copy the process back to memory, as well as the time the
process takes competing to regain main memory (memory allocation). In order for swapping to be
effective, this total time must be less than the time the process is to spend blocked (waiting for I/O
service). Another important problem is the accumulated wait time of a process when there is not
sufficient memory available for the process to be loaded and executed. These two quantities can be
used as performance measures of the memory management subsystem.
5.5 Noncontiguous Memory Allocation
Fragmentation (internal and external) is the main problem in contiguous memory allocation. Modern
operating systems use more advanced memory allocation schemes. This section discusses two common
techniques for noncontiguous memory allocation: paging and segmentation.
5.5.1 Paging
Activity 5.3
 Define paging?
 How paging is differentiated from Swapping?
 What are the steps required to handle a page fault in demand paging?

With noncontiguous memory allocation, the process address space is divided into small fixed-sized
blocks of logical memory called pages. The size of a process is consequently measured in the number

59
of pages. In a similar manner, physical memory is divided into small fixed-sized blocks of (physical)
memory called frames. If a 15-page process is waiting for memory, the system needs to find any 15
frames to allocate to this process. The size of a page is a power of two-for example, a size of 1K = 1024
bytes. The size of a frame is the same as that of a page because the system allocates any available frame
to a page of a process. The frames allocated to the pages of a process need not be contiguous; in
general, the system can allocate any empty frame to a page of a particular process. With paging, there is
no external fragmentation, but there is potential for a small amount of internal fragmentation that would
occur on the last page of a process.
Logical Addresses
A logical address of a process consists of a page number and an offset. Any address referenced in a
process is defined by the page that the address belongs to and the relative address within that page.
When the system allocates a frame to this page, it translates this logical address into a physical address
that consists of a frame number and the offset. For memory referencing, the system needs to know the
correspondence of a page of a process to a frame in physical memory, and for this, it uses a page table.
A page table is a data structure (array or linked list) used by the OS with data about the pages of a
process. There is one table entry for every page. Since the logical address of a process consists of the
page number and the offset, the least significant bits of the address correspond to the offset, and the
most significant bits to the page number.
Address Translation
The operating system maintains a table or list of the currently empty (and available) frames. In addition
to this table, for every process the system maintains a table with the frame allocation to each page of
the process.
5.5.2 Segmentation
Segments are variable-length modules of a program that correspond to logical units of the program; that
is, segments represent the modular structure of how a program is organized. Each segment is actually a
different logical address space of the program. Examples of segments are the program's main function,
additional functions, data structures, and so on. Before a program can execute, all its segments need to
be loaded non-contiguously into memory (these segments do not need to be contiguous in memory).
For every segment, the operating system needs to find a contiguous block of available memory to
allocate to the segment. There are three main differences between segmentation and paging:
 Not all segments of a process are of the same size.
 The sizes of segments are relatively large compared to the size of a page.

60
 The number of segments of a process is relatively small.
The operating system maintains a segment table for every process and a list of free memory blocks. The
segment table consists of an entry for every segment in a process. For each segment, the table stores the
starting address of the segment and the length of the segment. When the system allocates memory to
each segment of a process, a segment table is set up for the process. A logical address of a process
consists of two parts: the segment number and an offset. For example, suppose a 20-bit address is used
with 8 bits for the segment number and 12 bits for the segment offset. The maximum segment size is
4096 (212) and the maximum number of segments that can be referenced is 256 (28). The translation of
a logical address to a physical address with segmentation is carried out using the segment table. With
the segment number in the left 8 bits of the logical address, the system looks up the segment number
from the segment table. Using the length of the segment from the table, the system checks if the address
is valid by comparing the segment length with the offset. The starting physical address of the segment
is retrieved from the table and added to the offset.
5.6 Virtual Memory
The memory space of a process is normally divided into blocks that are either pages or segments.
Virtual memory management takes advantage of the typical behavior of a process: Not all blocks of the
process are needed simultaneously during the execution of a process. Therefore, not all the blocks of a
process need separate main memory allocation. Thus, the physical address space of a process is smaller
than its logical address space.
Basic Concepts
The virtual address space of a process is the entire set of all its addresses in the absolute program. After
linkage, the absolute version of the program is stored on disk. The disk area that stores all the processes
in absolute form is called the virtual memory. The physical address space of a process is much smaller
than its virtual address because only a portion of the process will ever be loaded into main memory.
Assuming that virtual memory is implemented with paging, not all the pages of a process are stored in
physical memory. A page reference is the page that has the address being referenced. The virtual
memory manager swaps in a page of an executing process whenever the execution of a process
references a page that is not in physical memory. Any unused page will normally be swapped out to a
disk. The operating system should provide efficient means to translate virtual addresses to physical
addresses. The size of the virtual address space is greater than the size of the physical address space.
Thus, the operating system must also provide effective and efficient techniques to load the needed
blocks of a program as it continues executing. Operating systems implement virtual memory

61
management using segments or pages.
Process Locality
A process in execution only references a subset of its addresses during a specific interval of time. This
behavior is called reference locality. A process executes in a series of phases and spends a finite
amount of time in each phase, referencing a subset of its pages in each phase. This subset of pages is
called a process locality. The process starts execution in the first phase of execution referencing a
subset of its pages (its virtual address space) and is spending a small amount of time in this phase. The
process then moves to its next phase of execution and uses another subset of its pages for some other
amount of time, and so on until the process terminates. Each subset of its pages is called a locality. The
executing process changes from locality to locality.
Memory Protection
Modern operating systems have memory protection that has two goals:
 Processes will not adversely affect other processes.
 Programming errors will be caught before large amounts of damage can be done.
The first goal is achieved by ensuring that a process can access memory only within its own address
space. Thus, the operating system will block memory accesses for addresses that are beyond the bounds
of the process's memory. The second goal is achieved by portions of the process's memory being
marked as
Follows:
1. Read enabled: The memory can be read as data.
2. Write enabled: Variables in this area can be changed.
3. Execute enabled: The area contains instructions.
Shared Memory
It is often useful for multiple processes to have access to shared code in memory, which is most often
implemented for shared libraries. In this scenario, some of the code needed by a process will be in a
dynamic linked library (DLL). When a process first attempts to use this library module, the OS will
ensure that it is loaded into memory. If additional processes wish to use this library module, the OS will
recognize that it is already loaded into memory and will arrange for the additional processes to have
access to the module. Dynamic linked libraries are used for common libraries that many applications
use. Using shared libraries saves memory because only one copy of the library module needs to be
loaded. Execution time is also saved for the additional processes that wish to use the library module.
Paging with Virtual Memory

62
As mentioned previously, the operating system translates a virtual address after a physical address after
allocating a frame to a page when necessary. The system needs hardware support to carry out the
translation from a virtual address to a physical address. As a process proceeds in its execution, it
references only a subset of its pages during any given time interval. These are the pages that the process
needs to proceed in its current phase of execution; these pages are the current locality of the process.
5.7 Paging Policies
Activity 5.4
 What is a page policy?
 What is the difference between fetch policies and replacement policies? Explain.

Several important issues have to be resolved to completely implement virtual memory:


 When to swap in a page-the fetch policy.
 The selection of the page in memory to replace when there are no empty frames the replacement
policy.
 The selection of the frame in which to place the page that was fetched-the placement policy
 The number of frames to allocate to a process.
5.7.1 Fetch Policy
For resolving the fetch policy, there are two approaches to consider:
 Demand paging, in which a page is not loaded until it is referenced by a process.
 Preparing, in which a page is loaded before it is referenced by a process.
In demand paging, a process generates a page fault when it references a page that is not in memory.
This can occur in either of the following conditions:
 The process is fetching an instruction.
 The process is fetching an operand of an instruction.
With preparing, other pages are loaded into memory. It is not very useful if these additional pages
loaded are not referenced soon by the process. Preparing can be used initially when the process starts.
These pages are loaded into memory from the secondary storage device (virtual memory).
5.7.2 Replacement Policy
The locality of a process consists of the subset of the pages that are used together at a particular time.
As a process proceeds in its execution, its locality changes and one or more pages not in memory will
be needed by the process in order to continue execution. The following steps are carried out by the
operating system when a page fault occurs:

63
1. The process that generated the page fault is suspended.
2. The operating system locates the referenced page in the secondary storage device, using the
information in the page tables.
3. If there are no free frames, a page is selected to be replaced and this page is transferred back to
the secondary storage device if necessary.
4. The referenced page is loaded into the selected frame, and the page and frame tables are
updated.
5. The interrupted program is scheduled to resume execution.
If there are no free frames in memory, a page in memory is replaced to make available an empty frame
for the new page. The replaced page may have to be transferred back into the secondary storage device,
if it has been modified. There are several replacement policies that are discussed in the sections that
follow. The placement policy determines in which frame to store the fetched page. This is not a real
issue in paged virtual memory management. In a more detailed view, when a page fault occurs, the
MMU (memory management unit) hardware will cause a page fault interrupt to the CPU. The operating
system responds to the interrupt by taking the following steps to handle the page fault:
1. Verify that the page reference address is a valid (or potentially valid) address for the process
that caused the page fault. If it is not, then the process will be terminated with a protection
violation.
2. Locate a frame into which the desired page can be loaded. Clearly, a frame that is not currently
being used is desired. If all frames are currently in use, choose a frame using one of the paging
policies discussed next. This step is a resource allocation procedure and therefore must be
performed under lock (since multiple processes may simultaneously need to have additional
frames allocated).
3. Swap out the page that is currently occupying the frame that was selected in Step 2 above. This
procedure can be optimized by noting that pages that have not changed do not need to be
swapped out (a copy of the page already exists on the disk). Clearly, pages that do not have
Write enabled cannot have changed, so they never need to be swapped out. Even a page that
does have Write enabled may not have actually changed since it was loaded from a disk. In
many systems, the MMU will mark a page entry whenever a write occurs to that page. The OS
can examine this "modified" bit (also known as a "dirty" bit) to determine if it needs to swap out
the page.

64
4. Load the desired page. The OS can locate a copy of the page on disk: either in the original
program file (if it has never been loaded before) or in the swap file.
5.7.3 Frame Allocation
There are two general groups of paging algorithms: those that use static allocation and those that use
dynamic allocation. In static allocation, the system allocates a fixed number of frames to a process. In
dynamic allocation, the system dynamically changes the number of frames it allocates to a process
during its execution. The number of frames to allocate to a process is important for the performance of
the system. In general, the more frames that are allocated to a process, the better the performance will
be because there will be a reduced number of total page faults during the execution of a process. Too
many frames allocated to a process would have the overall effect of reducing the degree of
multiprogramming, which in turn reduces performance.
If the number of frames is too small, there will be too many page faults during execution of a process.
An excessive number of page faults could lead to thrashing, a situation that can be worse than deadlock
because one or more processes will be making no progress in their executions and it can completely
bring the system down. Two general schemes are used by the operating system to allocate frames to the
various processes. The simplest one is called equal allocation, and it divides the available frames
equally among the active processes. The second scheme is called proportional allocation, and in it, the
number of frames is proportional to its size (in pages) and also depends on the priority of the process.
5.8 Page Faults and Performance Issues
A page fault requires the operating system to handle the page fault, as discussed previously. The total
time it takes to service a page fault includes several time components. The following are most relevant:
 The time interval to service the page fault interrupt.
 The time interval to store back (swap out) the replaced page to the secondary storage device.
 The time interval to load (swap in) the referenced page from the secondary storage device (disk
unit).
 Delay in queuing for the secondary storage device.
 Delay in scheduling the process with the referenced page.
The most significant time component is the disk I/O time to swap out the replaced page and to swap in
the referenced page. These I/O operations take several orders of magnitude more time than the access
time to physical memory.
5.8.1 The Working Set Algorithm
The working set algorithm estimates the number of frames needed by a process in its next execution

65
phase based on its current memory requirements. Given the last WSW page references, the working set
is the set of pages referenced in that window. The quantity WSW is called the working set window. A
good value of WSW results when the working set of a process equals its current locality. Based on the
size of the working set, more frames can be allocated or deallocated to or from the process. The total
number of pages that should be allocated to a process is the size of its working set.
5.8.2 Thrashing
Activity 5.5
 What is thrashing?
 What is the cause of Thrashing? How does the system detect thrashing? Once it
detects thrashing, what can the system do to eliminate this problem?

Thrashing is a condition or state in the system into which all the time a process spends is dedicated for
swapping pages; thus, no computation is carried out by the process. In principle, each active process
should be allocated a sufficient number of frames for its current locality. If the number of frames
allocated is too low, the execution of the process will generate an excessive number of page faults, and
eventually the process will make no progress in its execution because it will spend practically all of its
time paging-that is, swapping pages in and out. This condition is called thrashing. The operating
system should manage the allocation of frames in such a manner that, when it deallocates frames to one
or more processes, it can increase the degree of multiprogramming, if necessary. When one or more
processes need more frames, the system must suspend some other process to increase the number of
frames available and to allocate these to the processes that need them.
One approach used to prevent thrashing is to determine or estimate, for every active process, the sizes
of the localities of the process in order to decide on the appropriate number of frames to allocate to that
process. The performance of the system is affected by thrashing. The CPU utilization and throughput
decrease at a very fast rate when thrashing occurs. The operating system increases the degree of
multiprogramming in an attempt to improve the CPU utilization, and this might cause other processes
to thrash, causing lower throughput and lower CPU utilization.
Thrashing of one process may cause other processes to thrash, if the operating system uses a global
allocation strategy. This allocation strategy provides a global pool of frames for processes to select a
replacement frame when needed. With local allocation, a process can select a replacement frame only
from the set of frames already allocated to it.
5.8.3 Caching
The most common technique used to reduce disk accesses is the block cache or buffer cache. (Cache

66
is pronounced „„cash‟‟ and is derived from the French cacher, meaning to hide.) In this context, a cache
is a collection of blocks that logically belong on the disk but are being kept in memory for performance
reasons. Various algorithms can be used to manage the cache, but a common one is to check all read
requests to see if the needed block is in the cache. If it is, the read request can be satisfied without a
disk access. If the block is not in the cache, it is first read into the cache, and then copied to wherever it
is needed. Subsequent requests for the same block can be satisfied from the cache. Since there are many
(often thousands of) blocks in the cache, some way is needed to determine quickly if a given block is
present. The usual way is to hash the device and disk address and look up the result in a hash table. All
the blocks with the same hash value are chained together on a linked list so the collision chain can be
followed.
Summary
In this lesson, we have presented several schemes for management of main memory that are
characterized by contiguous allocation of memory. Except for the single partition, which is
inefficient in terms of both CPU & memory utilization, all schemes support multiprogramming by
allowing address spaces of several processes to reside in main memory simultaneously. One approach
is to statically divide the available physical memory into a number of fixed partitions & to satisfy
requests for memory by granting suitable free partitions, if any. Fixed partition sizes limit the
maximum allowable virtual-address space of any given process to the size of the largest partition
(unless overlays are used). The total number of partitions in a given system limits the number of
resident processes. Within the confines of this limit, the effectiveness of the short-term scheduler may
be improved by employing swapping to increase the ratio of resident to ready processes. Systems with
static partitioning suffer from internal fragmentation of memory.
Variable (dynamic) partitioning allows allocation of the entire physical memory, except for the
resident part of the OS, to a single process. Thus, in systems with dynamic partitioning, the virtual-
address space of any given process or an overlay is limited only by the capacity of the physical
memory in a given system. Dynamic creation of partitions according to the specific needs of
requesting processes also eliminates the problem of internal fragmentation. Dynamic allocation of
partitions requires the use of more complex algorithms for de-allocation of partitions & coalescing of
free memory in order to combat external fragmentation. The need for occasional compaction of
memory is also a major contributor to the increased time & space complexity of dynamic partitioning.
Both fixed & variable partitioning of memory rely on virtually identical hardware support for
relocation & protection. Sharing is quite restrictive in both systems. Segmentation allows breaking of

67
the virtual address space of a single process into separate entities (segments) that may be placed in
noncontiguous areas of physical memory. As a result, the virtual-to-physical address translation at
instruction execution time in such systems is more complex, & some dedicated hardware support is
necessary to avoid a drastic reduction in effective memory bandwidth. Since average segment sizes
are usually smaller than average process sizes, segmentation can reduce the impact of external
fragmentation on the performance of systems with dynamically partitioned memory. Other
advantages of segmentation include dynamic relocation, finely grained protection both within &
between address spaces, ease of sharing, & facilitation of dynamic linking & loading. Unless virtual
segmentation is supported, segmentation does not remove the problem of limiting the size of a
process's virtual space by the size of the available physical memory.
Chapter Five Review Questions
1. What functions does a memory manager perform?

2. How is user address space loaded in one partition of memory protectedfrom others?
3. What is the problem of fragmentation? How is it dealt with?

4. What do you understand by program relocatability? Differentiate between static and


dynamic relocation.
5. What are the differences between first fit, best fit, and worst fit memory allocation
strategies. Discuss their merits and demerits.
6. How the track of status of memory is kept in partitioned memory management?

7. What do you mean by relocation of address space? What problems doesit cause?
8. Compare and contrast internal fragmentation with external fragmentation?

9. What is compaction? What are the merits and demerits of compaction?


10. What do you understand by segmentation? Discuss the address translation
in segmented systems.
11. What are three basic approaches to sharing in systems with fixed partitioning of
memory?
12. Why Page size selection is critical?
13. Assuming that Page size = 4,096 bytes Process size = 72,766 bytes Calculate the number of pages
and internal fragmentation?

68
Chapter 6
Device management
Introduction
In previous chapter you have been studied the memory management, static and dynamic loading,
contiguous memory allocation, non-contiguous memory allocation, virtual memories, paging policies
and thrashing. In this chapter Device management entity, Software input and output management,
buffering, different types of I/O buffering techniques and disk management and its aspects are
discussed.
6.0 Objective
One of the important jobs of an Operating System is to manage various I/O devices including mouse,
keyboards, touch pad, disk drives, display adapters, USB devices, Bitmapped screen, LED, Analog-to-
digital converter, On/off switch, network connections, audio I/O, printers etc. An I/O system is required
to take an application I/O request and send it to the physical device, then take whatever response comes
back from the device and send it to the application.
At the end of the unit the students should be able to:-
 Essay the techniques of device management
 understand the input output management in operating system
 Describe polling I/O and Interrupt I/O with their difference
 show the steps used in Direct Memory access
 describe the input output procedure
 Analyze the mandate of operating system for several aspects of disk management
 describe Buffer basic concept
 explain the spooling techniques
6.1 Device Management entities
Activity 6.1
Define the following terms:-
 Device Controllers
 Synchronous and Asynchronous I/O
 Communication to I/O Devices

i. Device Controllers

69
Device drivers are software modules that can be plugged into an OS to handle a particular device.
Operating System takes help from device drivers to handle all I/O devices. The Device Controller
works like an interface between a device and a device driver. I/O units (Keyboard, mouse, printer, etc.)
typically consist of a mechanical component and an electronic component where electronic component
is called the device controller. There is always a device controller and a device driver for each device to
communicate with the Operating Systems. A device controller may be able to handle multiple devices.
As an interface its main task is to convert serial bit stream to block of bytes, perform error correction as
necessary.
Any device connected to the computer is connected by a plug and socket, and the socket is connected to
a device controller. Following is a model for connecting the CPU, memory, controllers, and I/O devices
where CPU and device controllers all use a common bus for communication.
ii. Synchronous vs. Asynchronous I/O
 Synchronous I/O In this scheme CPU execution waits while I/O proceeds
 Asynchronous I/O proceeds concurrently with CPU execution
iii. Communication to I/O Devices
The CPU must have a way to pass information to and from an I/O device. There are three approaches available to
communicate with the CPU and Device.
 Special Instruction I/O
 Memory-mapped I/O
 Direct memory access (DMA)
iv. Special Instruction I/O
This uses CPU instructions that are specifically made for controlling I/O devices. These instructions typically
allow data to be sent to an I/O device or read from an I/O device.
v. Memory-mapped I/O
When using memory-mapped I/O, the same address space is shared by memory and I/O devices. The device is
connected directly to certain main memory locations so that I/O device can transfer block of data to/from
memory without going through CPU.

70
Figure 6.1 Direct Memory mapped I/O.
While using memory mapped IO, OS allocates buffer in memory and informs I/O device to use that
buffer to send data to the CPU. I/O device operates asynchronously with CPU, interrupts CPU when
finished. The advantage to this method is that every instruction which can access memory can be used
to manipulate an I/O device. Memory mapped IO is used for most high-speed I/O devices like disks,
communication interfaces.
vi. Direct Memory Access (DMA)
Activity 6.2
 How does DMA increases systems concurrency?

Slow devices like keyboards will generate an interrupt to the main CPU after each byte is transferred. If
a fast device such as a disk generated an interrupt for each byte, the operating system would spend most
of its time handling these interrupts. So, a typical computer uses direct memory access (DMA)
hardware to reduce this overhead.
Direct Memory Access (DMA) means CPU grants I/O module authority to read from or write to
memory without involvement. DMA module itself controls exchange of data between main memory
and the I/O device. CPU is only involved at the beginning and end of the transfer and interrupted only
after entire block has been transferred. Direct Memory Access needs a special hardware called DMA
controller (DMAC) that manages the data transfers and arbitrates access to the system bus. The
controllers are programmed with source and destination pointers (where to read/write the data),
counters to track the number of transferred bytes, and settings, which includes I/O and memory types,
interrupts and states for the CPU cycles.

71
Figure 6.2 Direct Memory Access module
The operating system uses the DMA hardware as follows:
Step Description
1 Device driver is instructed to transfer disk data to a buffer address X.
2 Device driver then instruct disk controller to transfer data to buffer.
3 Disk controller starts DMA transfer.
4 Disk controller sends each byte to DMA controller.
5 DMA controller transfers bytes to buffer, increases the memory address,
decreases the counter C until C becomes zero.
6 When C becomes zero, DMA interrupts CPU to signal transfer completion.
Table 6.1 step of OS uses DMA
vii. Polling vs. Interrupts I/O
A computer must have a way of detecting the arrival of any type of input. There are two ways that this
can happen, known as polling and interrupts. Both of these techniques allow the processor to deal
with events that can happen at any time and that are not related to the process it is currently running.
 Polling I/O:-Polling is the simplest way for an I/O device to communicate with the processor
to the processor. The process of periodically checking status of the device to see if it is time for
the next I/O operation is called polling. The I/O device simply puts the information in a Status
register, and the processor must come and get the information. Most of the time, devices will
not require attention and when one does it will have to wait until it is next interrogated by the
polling program. This is an inefficient method and much of the processors time is wasted on
unnecessary polls. Compare this method to a teacher continually asking every student in a

72
class, one after another, if they need help. Obviously the more efficient method would be for a
student to inform the teacher whenever they require assistance.
 Interrupts I/O:-An alternative scheme for dealing with I/O is the interrupt-driven method. An
interrupt is a signal to the microprocessor from a device that requires attention. A device
controller puts an interrupt signal on the bus when it needs CPU‟s attention when CPU receives
an interrupt; it saves its current state and invokes the appropriate interrupt handler using the
interrupt vector (addresses of OS routines to handle various events). When the interrupting
device has been dealt with, the CPU continues with its original task as if it had never been
interrupted.
6.2 Software input and output management
I/O software is often organized in the following layers:
 User Level Libraries: This provides simple interface to the user program to perform input
and output. For example, stdio is a library provided by C and C++ programming languages.
 Kernel Level Modules: This provides device driver to interact with the device controller and
device independent I/O modules used by the device drivers.
 Hardware: This layer includes actual hardware and hardware controller which interact with
the device drivers and makes hardware alive.
A key concept in the design of I/O software is that it should be device independent where it should be
possible to write programs that can access any I/O device without having to specify the device in
advance. For example, a program that reads a file as input should be able to read a file on a floppy disk,
on a hard disk, or on a CD-ROM, without having to modify the program for each different device.

Figure 6.3 Layer of I/O Software


Device drivers are software modules that can be plugged into an OS to handle a particular device.

73
Operating System takes help from device drivers to handle all I/O devices. Device drivers encapsulate
device-dependent code and implement a standard interface in such a way that code contains device-
specific register reads/writes. Device driver, is generally written by the device's manufacturer and
delivered along with the device on a CD-ROM. A device driver performs the following jobs:
 To accept request from the device independent software above to it.
 Interact with the device controller to take and give I/O and perform required error handling
 Making sure that the request is executed successfully
How a device driver handles a request is as follows: Suppose a request comes to read a block N. If the
driver is idle at the time a request arrives, it starts carrying out the request immediately. Otherwise, if
the driver is already busy with some other request, it places the new request in the queue of pending
requests.
Interrupt handlers
An interrupt handler, also known as an interrupt service routine or ISR, is a piece of software or more
specifically a callback function in an operating system or more specifically in a device driver, whose
execution is triggered by the reception of an interrupt. When the interrupt happens, the interrupt
procedure does whatever it has to in order to handle the interrupt, updates data structures and wakes up
process that was waiting for an interrupt to happen. The interrupt mechanism accepts an address a
number that selects a specific interrupt handling routine/function from a small set. In most architecture,
this address is an offset stored in a table called the interrupt vector table. This vector contains the
memory addresses of specialized interrupt handlers.
6.3 Device-Independent I/O Software
The basic function of the device-independent software is to perform the I/O functions that are common
to all devices and to provide a uniform interface to the user-level software. Though it is difficult to
write completely device independent software but we can write some modules which are common
among all the devices. Following is a list of functions of device-independent I/O Software:
 Uniform interfacing for device drivers
 Device naming Mnemonic names mapped to Major and Minor device numbers
 Device protection
 Providing a device-independent block size
 Buffering because data coming off a device cannot be stored in final destination.
 Storage allocation on block devices
 Allocation and releasing dedicated devices

74
 Error Reporting
6.4 Buffering
Activity 6.3
 Describe buffering in the I/O subsystem of an operating system. Give reasons why it is
required, and give a case where it is an advantageous and a case where it is
disadvantageous.

A buffer is a memory area that stores data being transferred between two devices or between a device
and an application.
Uses of I/O Buffering:
 Buffering is done to deal effectively with a speed mismatch between the producer and consumer
of the data stream.
 A buffer is produced in main memory to heap up the bytes received from modem.
 After receiving the data in the buffer, the data get transferred to disk from buffer in a single
operation.
 This process of data transfer is not instantaneous; therefore the modem needs another buffer in
order to store additional incoming data.
 When the first buffer got filled, then it is requested to transfer the data to disk.
 The modem then starts filling the additional incoming data in the second buffer while the data in
the first buffer getting transferred to disk.
 When both the buffers completed their tasks, then the modem switches back to the first buffer
while the data from the second buffer get transferred to the disk.
 The use of two buffers disintegrates the producer and the consumer of the data, thus minimizes
the time requirements between them.
 Buffering also provides variations for devices that have different data transfer sizes.
6.5 Types of various I/O buffering techniques
1. Single buffer: a buffer is provided by the operating system to the system portion of the main
memory.
Block oriented device
o System buffer takes the input.
o After taking the input, the block gets transferred to the user space by the process and
then the process requests for another block.

75
o Two blocks works simultaneously, when one block of data is processed by the user
process, the next block is being read in.
o OS can swap the processes.
o OS can record the data of system buffer to user processes.
Stream oriented device –
 Line- at a time operation is used for scroll made terminals. User inputs one line at a
time, with a carriage return signaling at the end of a line.
 Byte-at a time operation is used on forms mode, terminals when each keystroke is
significant.

2. Double buffer
Block oriented
 There are two buffers in the system.
 One buffer is used by the driver or controller to store data while waiting for it to be taken
by higher level of the hierarchy.
 Other buffer is used to store data from the lower level module.
 Double buffering is also known as buffer swapping.
 A major disadvantage of double buffering is that the complexity of the process gets
increased.
 If the process performs rapid bursts of I/O, then using double buffering may be deficient.
Stream oriented –
 Line- at a time I/O, the user process need not be suspended for input or output, unless
process runs ahead of the double buffer.
 Byte- at a time operations, double buffer offers no advantage over a single buffer of
twice the length.

76
3. Circular buffer:
 When more than two buffers are used, the collection of buffers is itself referred to as a circular
buffer.
 In this, the data do not directly passed from the producer to the consumer because the data
would change due to overwriting of buffers before they had been consumed.
 The producer can only fill up to buffer i-1 while data in buffer i is waiting to be consumed.

Figure 6.4 Circular buffer


6.6 Disk management
Activity 6.4
 Describe the role of operating system in a disk management?

The operating system is responsible for several aspects of disk management.


 Disk Formatting: A new magnetic disk is a blank slate. It is just platters of a magnetic
recording material. Before a disk can store data, it must be divided into sectors that the disk
controller can read and write. This process is called low-level formatting (or physical
formatting).
 Low-level formatting fills the disk with a special data structure for each sector. The data
structure for a sector consists of a header, a data area, and a trailer. The header and trailer

77
contain information used by the disk controller, such as a sector number and an error-
correcting code (ECC).
To use a disk to hold files, the operating system still needs to record its own data structures on the disk.
It does so in two steps. The first step is to partition the disk into one or more groups of cylinders. The
operating system can treat each partition as though it were a separate disk. For instance, one partition
can hold a copy of the operating system‟s executable code, while another holds user files. After
partitioning, the second step is logical formatting (or creation of a file system). In this step, the
operating system stores the initial file-system data structures onto the disk.
 Boot block: When a computer is powered up or rebooted, it needs to have an initial program to
run. This initial program is called the bootstrap program. It initializes all aspects of the system
(i.e. from CPU registers to device controllers and the contents of main memory) and then starts
the operating system. To do its job, the bootstrap program finds the operating system kernel on
disk, loads that kernel into memory, and jumps to an initial address to begin the operating-
system execution.
For most computers, the bootstrap is stored in read-only memory (ROM). This location is convenient
because ROM needs no initialization and is at a fixed location that the processor can start executing
when powered up or reset. And since ROM is read-only, it cannot be infected by a computer virus. The
problem is that changing this bootstrap code requires changing the ROM hardware chips.
 Swap-space management: Swapping is a memory management technique used in multi-
programming to increase the number of process sharing the CPU. It is a technique of removing
a process from main memory and storing it into secondary memory, and then bringing it back
into main memory for continued execution. This action of moving a process out from main
memory to secondary memory is called Swap Out and the action of moving a process out from
secondary memory to main memory is called Swap In.
Summary
Device management is the process of managing the implementation, operation and maintenance of a
physical and/or virtual device. It is a broad term that includes various administrative tools and
processes for the maintenance and upkeep of a computing, network, mobile and/or virtual device.
Operating system (OS) handles communication with the devices via their drivers. The OS component
gives a uniform interface for accessing devices with various physical features. There are various
functions of device management in the operating system. Some of them are as follows:
 It keeps track of data, status, location, uses, etc. The file system is a term used to define a group
of facilities.

78
 It enforces the pre-determined policies and decides which process receives the device when and
for how long.
 It improves the performance of specific devices.
 It monitors the status of every device, including printers, storage drivers, and other devices.
 It allocates and effectively deallocates the device. De-allocating differentiates the devices at two
levels: first, when an I/O command is issued and temporarily freed. Second, when the job is
completed, and the device is permanently release.

Chapter Six Review Questions


1. Device controllers are generally becoming more complex in the functionality they provide (e.g.
think about the difference between implementing a serial port with a flip-flop controlled by the
CPU and a multi-gigabit network adapter with the TCP/IP stack on the card itself). What effect
might this have on the operating system and system performance?
2. List and discus about interrupt handlers?
3. What is polling?
4. Compare I/O based on polling with interrupt-driven I/O. In what situation would you favor one
technique over the other?
5. What is disk interleaving? What problem is it trying to solve?
6. What the difference between direct memory accesses, memory mapped and special instruction?
7. Show clear idea about direct memory access and illustrate clearly?
8. List and discus about user space I/O software and kernel I/O subsystem?

79
Chapter Seven
File System
Introduction
In previous chapter you have been studied the Device management entity, Software input and
output management, buffering, different types of I/O buffering techniques and disk management.in
this chapter file concept, file operation, file management system, file types, file structure, access
methods, directory overview, file protection and file recovery are discussed.
7.0 Objective
The objective of this lesson is to discuss the various concepts of file system and make the
students familiar with the different techniques of file allocation and access methods. We also discuss
the ways to handle file protection, which is necessary in an environment where multiple users have
access to files and where it is usually desirable to control by whom and in what ways files may be
accessed.
7.1 File concept
Activity 7.1
 What is file?
 What are the information’s associated with an open file?
 Distinguish file from directory.
 What are the common attributes of file?

The file system is the most visible aspect of an operating system. While the memory manager
is responsible for the maintenance of primary memory, the file manager is responsible for the
maintenance of secondary storage (e.g., hard disks). It provides the mechanism for on-line storage of
and access to both data and programs of the operating system and all the users of the computer
system. The file system consists to two distinct parts: a collection of files, each storing related data
and a directory structure, which organizes and provides information about all the files in the system.
Some file systems have a third part, partitions, which are used to separate physically or logically large
collections of directories
 Data: -a collection of facts, observations, or other information related to a particular question or
problem; as, the historical data show that the budget deficit is only a small factor in determining
interest rates. The term in this sense is used especially in reference to experimental observations

80
collected in the course of a controlled scientific investigation.
 Metadata: -Metadata is "data that provides information about other data". In other words, it is
"data about data." Many distinct types of metadata exist, including descriptive metadata,
structural metadata, administrative metadata, reference metadata and statistical metadata.
 Files: - A file is a named collection of related information that is recorded on secondary storage.
From a user's perspective, a file is the smallest allotment of logical secondary storage; that is,
data cannot be written to secondary storage unless they are within a file. Commonly, files
represent programs (both source and object forms) and data. Data files may be numeric,
alphabetic, alphanumeric, or binary. Files may be free form, such as text files, or may be
formatted rigidly. In general, a file is a sequence of bits, bytes, lines, or records, the meaning of
which is defined by the file's creator and user. The concept of a file is thus extremely general.
File Attributes: -A file is named, for the convenience of its human users, and is referred to by its name.
A name is usually a string of characters. A file's attributes vary from one operating system to another
but typically consist of these:
 Name. The symbolic file name is the only information kept in human readable form.
 Identifier. This unique tag, usually a number, identifies the file within the file system; it is the
non-human-readable name for the file.
 Type. This information is needed for systems that support different types of files.
 Location. This information is a pointer to a device and to the location of the file on that device.
 Size. The current size of the file (in bytes, words, or blocks) and possibly the maximum allowed
size is included in this attribute.
 Protection. Access-control information determines who can do reading, writing, executing, and
so on.
 Time, date, and user identification. This information may be kept for creation, last
modification, and last use. These data can be useful for protection, security, and usage
monitoring.
7.2 File Operations
To define a file properly, we need to consider the operations that can be performed on files. The
operating system can provide system calls to create, write, read, reposition, delete, and truncate files.
Let's examine what the operating system must do to perform each of these six basic file operations.
 Creating a file: - Two steps are necessary to create a file. First, space in the file system must be
found for the file. Second, an entry for the new file must be made in the directory.

81
 Writing a file: - To write a file, we make a system call specifying both the name of the file and
the information to be written to the file. Given the name of the file, the system searches the
directory to find the file's location. The system must keep a write pointer to the location in the
file where the next write is to take place. The write pointer must be updated whenever a write
occurs.
 Reading a file: - To read from a file, we use a system call that specifies the name of the file and
where (in memory) the next block of the file should be put. Again, the directory is searched for
the associated entry, and the system needs to keep a read pointer to the location in the file
where the next read is to take place. Once the read has taken place, the read pointer is updated.
Because a process is usually either reading from or writing to a file, the current operation
location can be kept as a per-process current file-position pointer. Both the read and write
operations use this same pointer, saving space and reducing system complexity.
 Repositioning within a file: - The directory is searched for the appropriate entry, and the
current-file-position pointer is repositioned to a given value. Repositioning within a file need
not involve any actual I/0. This file operation is also known as a file seek.
 Deleting a file: - To delete a file, we search the directory for the named file. Having found the
associated directory entry, we release all file space, so that it can be reused by other files, and
erase the directory entry.
 Truncating a file. The user may want to erase the contents of a file but keep its attributes.
Rather than forcing the user to delete the file and then recreate it, this function allows all
attributes to remain unchanged -except for file length-but lets the file be reset to length zero and
its file space released.
7.3 File management system
Activity 7.2
 List various layers of a file system?
 What are the activities of the operating system in regard to file management?
 What is the purpose of file management system?

A file management system is that set of system software that provides services to users and applications
in the use of files. Typically, the only way that a user or application may access files is through the file
management system. This relieves the user or programmer of the necessity of developing special-
purpose software for each application and provides the system with a consistent, well-defined means of
controlling its most important asset. the following objectives for a file management system:

82
 To meet the data management needs and requirements of the user, which include storage of data
and the ability to perform the aforementioned operations
 To guarantee, to the extent possible, that the data in the file are valid
 To optimize performance, both from the system point of view in terms of overall throughput and
from the user‟s point of view in terms of response time
 To provide I/O support for a variety of storage device types
 To minimize or eliminate the potential for lost or destroyed data
 To provide a standardized set of I/O interface routines to user processes
 To provide I/O support for multiple users, in the case of multiple-user systems
7.4 File Types
When we design a file system-indeed, an entire operating system-we always consider whether the
operating system should recognize and support file types. If an operating system recognizes the type of
a file, it can then operate on the file in reasonable ways. For example, a common mistake occurs when a
user tries to print the binary-object form of a program. This attempt normally produces garbage;
however, the attempt can succeed if the operating system has been told that the file is a binary-object
program.
A common technique for implementing file types is to include the type as part of the file name. The
name is split into two parts-a name and an extension, usually separated by a period character. For
example, most operating systems allow users to specify a file name as a sequence of characters
followed by a period and terminated by an extension of additional characters. File name examples
include resume.doc, Server.java.
The system uses the extension to indicate the type of the file and the type of operations that can be done
on that file. Only a file with a .com, .exe, or .bat extension can be executed, for instance. The .com and
.exe files are two forms of binary executable files, whereas a .bat file is a batch file containing, in
ASCII format, commands to the operating system. MS-DOS recognizes only a few extensions, but
application programs also use extensions to indicate file types in which they are interested. For
example, assemblers expect source files to have an .asm extension, and the Microsoft Word processor
expects its files to end with a .doc(docx) extension. These extensions are not required, so a user may
specify a file without the extension (to save typing), and the application will look for a file with the
given name and the extension it expects. Because these extensions are not supported by the operating
system, they can be considered as "hints" to the applications that operate on them.
File type Usual extension Function

83
Executable Exe, com, bin Ready to run machine language program
Object Obj, o compiled, machine language, not linked
Source code Pl, java, py, cpp, c, asm, a source code in various languages
Batch Bat, sh commands to the command interpreter
Text Txt, doc Textual data, documents
Word processor Docx, rtf, doc, wp, various word processor formats
Library Lib, a, so, dll libraries of routines for programmers
Print or view Ps, pdf, jpg ASCII or binary file in a format for
printing or viewing
Archive Arch, zip, rar related files grouped into one file
sometimes compressed, for archiving or
storage
Multimedia Mpeg, mov, wav, mp3, mp4, avi binary file containing audio or A/V
information
Table 7.1 Common file types
7.5 Files structure
File types also can be used to indicate the internal structure of the file. File has its own structure.
Further, certain files must conform to a required structure that is understood by the operating system.
For example, the operating system requires that an executable file have a specific structure so that it can
determine where in memory to load the file and what the location of the first instruction is. Some
operating systems extend this idea into a set of system-supported file structures, with sets of special
operations for manipulating files with those structures. For instance, DEC's VMS operating system has
a file system that supports three defined file structures.
Some operating systems impose (and support) a minimal number of file structures. This approach has
been adopted in UNIX, MS-DOS, and others. UN1X considers each file to be a sequence of 8-bit bytes;
no interpretation of these bits is made by the operating system. This scheme provides maximum
flexibility but little support. Each application program must include its own code to interpret an input
file as to the appropriate structure. However, all operating systems must support at least one structure-
that of an executable file-so that the system is able to load and run programs.
The Macintosh operating system also supports a minimal number of file structures. It expects files to
contain two parts: a resource fork and a data fork. The resource fork contains information of interest to
the user. For instance, it holds the labels of any buttons displayed by the program. A foreign user may

84
want to re-label these buttons in his own language, and the Macintosh operating system provides tools
to allow modification of the data in the resource fork. The data fork contains program code or data-the
traditional file contents. To accomplish the same task on a UNIX or MS-DOS system, the programmer
would need to change and recompile the source code, unless she created her own user-changeable data
file. Clearly, it is useful for an operating system to support structures that will be used frequently and
that will save the programmer substantial effort. Too few structures make programming inconvenient,
whereas too many causes operating-system bloat
and programmer confusion.
 Internal File Structure: - Internally, locating an offset within a file can be complicated for the
operating system. Disk systems typically have a well-defined block size determined by the size
of a sector. All disk I/0 is performed in units of one block (physical record), and all blocks are
the same size. It is unlikely that the physical record size will exactly match the length of the
desired logical record. Logical records may even vary in length. Padding a number of logical
records into physical blocks is a common solution to this problem.
The logical record size, physical block size, and packing technique determine how many logical records
are in each physical block. The packing can be done either by the user's application program or by the
operating system. In either case, the file may be considered a sequence of blocks. All the basic I/O
functions operate in terms of blocks. The conversion from logical records to physical blocks is a
relatively simple software problem.
Because disk space is always allocated in blocks, some portion of the last block of each file is generally
wasted. If each block were 512 bytes, for example, then a file of 1,949 bytes would be allocated four
blocks (2,048 bytes); the last 99 bytes would be wasted. The waste incurred to keep everything in units
of blocks (instead of bytes) is internal fragmentation All file systems suffer from internal
fragmentation; the larger the block size, the greater the internal fragmentation.
Access Methods
Activity 7.3
 What are the different file access methods? Which one is simpler?
Files store information. When it is used, this information must be accessed and read into computer
memory. The information in the file can be accessed in several ways. Some systems provide only one
access method for files. Other systems, such as those of IBM, support many access methods, and
choosing the right one for a particular application is a major design problem.

85
1. Sequential Access: -The simplest access method is sequential access. Information in the file is
processed in order, one record after the other. This mode of access is by far the most common;
for example, editors and compilers usually access files in this fashion.
2. Direct Access: -Another method is direct access (or relatively access) A file is made up of fixed
length logical record that allow programs to read and write records rapidly in no particular
order. The direct-access method is based on a disk model of a file, since disks allow random
access to any file block. For direct access, the file is viewed as a numbered sequence of blocks
or records. Thus, we may read block 14, then read block 53, and then write block 7. There are
no restrictions on the order of reading or writing for a direct-access file.
Direct-access files are of great use for immediate access to large amounts of information. Databases are
often of this type. When a query concerning a particular subject arrives, we compute which block
contains the answer and then read that block directly to provide the desired information.
 Directory and Disk Structure:-Next, we consider how to store files. Certainly, no general-
purpose computer stores just one file. There are typically thousand, millions, and even billions
of files within a computer. Files are stored on random-access storage devices, including hard
disks, optical disks, and solid state (memory based) disks. A storage device can be used in its
entirety for a file system. It can also be subdivided for finer grained control. For example, a disk
can be partitioned into quarters, and each quarter can hold a file system. Storage devices can
also be collected together into RAID sets that provide protection from the failure of a single
disk. Sometimes, disks are subdivided and also collected into RAID sets.
Partitioning is useful for limiting the sizes of individual file systems, putting multiple file-system types
on the same device, or leaving part of the device available for other uses, such as swap space or
unformatted (raw) disk space. Partitions are also known as slices or (in the IBM world) minidisks. A
file system can be created on each of these parts of the disk. Any entity containing a file system is
generally known as a Volume. The volume may be a subset of a device, a whole device, or multiple
devices linked together into a RAID set. Each volume can be thought of as a virtual disk. Volumes can
also store multiple operating systems, allowing a system to boot and run more than one operating
system.
Each volume that contains a file system must also contain information about the files in the system.
This information is kept in entries in a device directory or volume table of contents. The device
directory (more commonly known simply as that directory) records information -such as name,
location, size, and type-for all files on that volume. Figure below shows a typical file-system

86
organization.

Figure 7.1 directory and disk structure

 Storage Structure:-Common file system in a Solaris operating system: -


 Tmpfs: -a "temporary" file system. that is created in volatile main memory and has its
contents erased if the system reboots or crashes
 Objfs: -a "virtual" file system (essentially an interface to the kernel that looks like a file
system) that gives debuggers access to kernel symbols
 Dfs: -a virtual file system that maintains "contract" information to manage which
processes start when the system boots and must continue to run during operation
 Lofs: -a "loop back" file system that allows one file system to be accessed in place of
another one
 Prods: -a virtual file system that presents information on all processes as a file system
 ufs, zfs: -general-purpose file systems
7.6 Directory Overview
The directory can be viewed as a symbol table that translates file names into their directory entries. If
we take such a view, we see that the directory itself can be organized in many ways. We want to be able
to insert entries, to delete entries, to search for a named entry, and to list all the entries in the directory.
When considering a particular directory structure, we need to keep in mind the operations that are to be
performed on a directory:
1. Search for a file. We need to be able to search a directory structure to find the entry for a
particular file. Since files have symbolic names, and similar names may indicate a relationship
between files, we may want to be able to find all files whose names match a particular pattern.
2. Create a file. New files need to be created and added to the directory.

87
3. Delete a file. When a file is no longer needed, we want to be able to remove it from the directory.
4. List a directory. We need to be able to list the files in a directory and the contents of the
directory entry for each file in the list.
5. Rename a file. Because the name of a file represents its contents to its users, we must be able to
change the name when the contents or use of the file changes. Renaming a file may also allow its
position within the directory structure to be changed.
6. Traverse the file system. We may wish to access every directory and every file within a
directory structure. For reliability, it is a good idea to save the contents and structure of the entire
file system at regular intervals. Often, we do this by copying all files to magnetic tape. This
technique provides a backup copy in case of system failure. In addition, if a file is no longer in
use, the file can be copied to tape and the disk space of that file released for reuse by another file.
In. the following is most common schemes for defining the logical structure of a directory.
Single-level Directory: - The simplest directory structure is the single-level directory. All files are
contained in the same directory, which is easy to support and understand.

Figure 7.2 Single level directory


Two-Level Directory: - In the two-level directory structure, each user has his own user file directory
(UFD). The UFDs have similar structures, but each list only the files of a single user. W11en a user job
starts or a user logs in, the system's master file directory (MFD) is searched. The MFD is indexed by
user name or account number, and each entry points to the UFD for that user.

88
Figure 7.3 Two level directory
File System Mounting (initiating)
The mount procedure is straightforward. The operating system is given the name of the device and
mounts point the location within the file structure where the file system is to be attached. Some
operating systems require that a file system type be provided, while others inspect the structures of the
device and determine the type of file system. Typically, a mount point is an empty directory. For
instance, on a UNIX system, a file system containing a user's home directories might be mounted as
/home; then, to access the directory structure within that file system, we could precede the directory
names with /home, as in /home/Husen-Adem/. Mounting that file system under /users would result in
the path name /users/Husen-Adem, which we could use to reach the same directory.
Next, the operating system verifies that the device contains a valid file system. It does so by asking the
device driver to read the device directory and verifying that the directory has the expected format.
Finally, the operating system notes in its directory structure that a file system is mounted at the
specified mount point. This scheme enables the operating system to traverse its directory structure,
switching among file systems, and even file systems of varying types, as appropriate.
Virtual File Systems
An obvious but suboptimal method of implementing multiple types of file systems is to write directory
and file routines for each type. Instead, however, most operating systems, including UNIX, use object-
oriented techniques to simplify, organize, and modularize the implementation. The use of these
methods allows very dissimilar file-system types to be implemented within the same structure,
including network file systems, such as NFS. Users can access files that are contained within multiple
file systems on the local disk or even on file systems available across the network.
Virtual file system has two important function: -
1. It separates file-system-generic operations from their implementation by defining a clean VFS
interface. Several implementations for the VFS interface may coexist on the same machine,
allowing transparent access to different types of file systems mounted locally.
2. It provides a mechanism for uniquely representing a file throughout a network. The VFS is
based on a file-representation structure, called a that contains a numerical designator for a
network-wide unique file. (UNIX) Inodes are unique within only a single file system.) This
network-wide uniqueness is required for support of network file systems. The kernel maintains
one Vnode structure for each active node (file or directory).
7.7 File Protection

89
When information is stored in a computer system, we want to keep it safe from physical damage (the
issue of reliability) and improper access (the issue of protection). Protection can be provided in many
ways. For a small single-user system, we might provide protection by physically removing the floppy
disks and locking them in a desk drawer or file cabinet. In a multiuser system, however, other
mechanisms are needed.
Types of Access The need to protect files is a direct result of the ability to access files. Systems that do
not permit access to the files of other users do not need protection. Thus, we could provide complete
protection by prohibiting access. Alternatively, we could provide free access with no protection. Both
approaches are too extreme for general use. What is needed is controlled access.
Protection mechanisms provide controlled access by limiting the types of file access that can be made.
Access is permitted or denied depending on several factors, one of which is the type of access
requested. Several different types of operations may be controlled:
 Read. Read from the file.
 Write. Write or rewrite the file.
 Execute. Load the file into memory and execute it.
 Append. Write new information at the end of the file.
 Delete. Delete the file and free its space for possible reuse.
 List. List the name and attributes of the file.
Access Control
The most common approach to the protection problem is to make access dependent on the identity of
the user. Different users may need different types of access to a file or directory. The most general
scheme to implement identity dependent access is to associate with each file and directory an access
control list (ACL) specifying user names and the types of access allowed for each user. When a user
requests access to a particular file, the operating system checks the access list associated with that file.
If that user is listed for the requested access, the access is allowed. Otherwise, a protection violation
occurs, and the user job is denied access to the file.

90
Figure 7.4 Windows access control management
7.8 Recovery (file recovery)
A system crash can cause inconsistencies among on-disk file-system data structures, such as directory
structures, free-block pointers, and free FCB pointers. Many file systems apply changes to these
structures in place. A typical operation, such as creating a file, can involve many structural changes
within the file system on the disk Directory structures are modified, FCBs are allocated, data blocks are
allocated, and the free counts for all of these blocks are decreased. These changes can be interrupted by
a crash, and inconsistencies among the structures can result. For example, the free FCB count might
indicate that an FCB had been allocated, but the directory structure might not point to the FCB.
Compounding this problem is the caching that operating systems do to optimize I/0 performances.
Some changes may go directly to disk, while others may be cached. If the cached changes do not reach
disk before a crash occurs, more corruption is possible. In addition to crashes, bugs in file-system
implementation, disk controllers, and even user applications can corrupt a file system. File systems
have varying methods to deal with corruption, depending on the file-system data structures and
algorithms.
NTF (network file system)
Network file systems are common place. They are typically integrated with the overall directory
structure and interface of the client system. NFS is a good example of a widely used, well implemented
client-server network file system. Here, we use it as an example to explore the implementation details
of network file systems.

91
NFS is both an implementation and a specification of a software system for accessing remote files
across LANs (or even WANs). NFS is part of ONC+, which most UNIX vendors and some PC
operating systems support. The implementation described here is part of the Solaris operating system,
which is a modified version of UNIX SVR4 running on Sun workstations and other hardware. It uses
either the TCP or UDP /IP protocol (depending on the interconnecting network).
The specification and the implementation are intertwined in our description of NFS. Whenever detail is
needed, we refer to the Sun implementation; whenever the description is general, it applies to the
specification also.

92
Summary
The file system resides permanently on secondary storage, which has the main requirement that it
must be able to hold a large amount of data, permanently. The various files can be allocated space on
the disk in three ways: through contagious, linked or indexed allocation. Contagious allocation can
suffer from external fragmentation. Direct- access is very inefficient with linked-allocation. Indexed
allocation may require substantial overhead for its index block. There are many ways in which these
algorithms can be optimized.
Free space allocation methods also influence the efficiency of the use of disk space, the
performance of the file system and the reliability of secondary storage.
Chapter seven Review Questions
1. What do you understand by a file? What is a file system?
2. Explain the objective of file management system

3. What are the different modes to share a file?

4. What are the different methods to access the information from a file? Discuss their
advantages and disadvantages.

5. Essay the difference between single level directory and two level directory
6. What are the advantages of indexed allocation over linked allocation and contiguous
space allocation? Explain.
7. Differentiate first fit, best fit and worst fit storage allocation strategies.
8. What are the major functions of virtual file system?
9. What are the importances of Network file system?

93
Chapter Eight
Security and Protection
Introduction
In previous chapter you have been studied file concept, file operation, file management system, file
types, file structure, access methods, directory overview, file protection and file recovery. In this
chapter Overview of security and protection system, operating system security, computer security
classification, memory protection, authentication, authorization and encryption are discussed.
Objective
The main goals of operating system security is to protect the OS from various threats, including
malicious software such as worms, Trojans and other viruses, misconfigurations, remote intrusions and
etc.at the end of this chapter the students will able to:-
 Understand the role of operating system in system security and protection
 Differentiate computer security classification
 Differentiate various threats of operating system
 Describe how protection mechanisms can mitigate system attacks
 Apply the methods to ensure Protection and Security in Operating System
 Analyze the best security solution in operating system

8.1 Overview of security and protection system


The operating system security measures we discussed in this chapter are in common use in companies
around the globe. The various steps we went over when we discussed hardening operating systems are
usually implemented by any competent organization that is building servers for deployment, particularly
in cases where these servers will be Internet facing. Depending on the organization in question and its
security posture, we may or may not find such measures to have been carried out on client machines.
Although such basic hardening measures are a way in which we can increase our security with relative
ease, we do so at the potential expense of ease of use and productivity.
The use of anti-malware tools, HIDS, and software firewalls is also rather ubiquitous in many
organizations of any appreciable size. We will commonly see anti-malware tools installed on proxy
servers filtering Web and mail traffic as it enters from the Internet. Without such tools in place, even if
we have very strong border security in the form of firewalls and IDS, when something does manage to
make it through these measures, it will cause great havoc on our internal networks.

8.2 Operating system security

94
Activity 8.1
 What is security?
 What are the threats of world securities?
 What is the role of operating system in securities?
The topic of operating system security is a very lengthy discussion owing to not only the vast amount
of technical details but also the number of operating systems and versions. Our focus is Solaris
operating system security.
Changing Some Defaults:- Let's start by addressing system defaults. When the operating system
is installed, there are some default configurations that should be changed. Below the Solaris
operating systems there is a firmware called Openboot. The Openboot resides on the
Programmable Read Only Memory (PROM) and starts every time the systems start and even
determines the boot behavior at startup. Needless to say, we must protect the Openboot from
unauthorized access. By default the Openboot is without a password and is set at the lowest level
of security. Openboot has three levels of security: none, command, and full.
The security level none requires no password and allows full access to the Openboot commands. The
security level command requires a password for all commands other than boot and go. boot is the
Openboot command to initiate the boot process utilizing any options. go resumes processing after halting
the system. Security level full requires a password for all commands other than go. It is best to set the
security to command or better with a specified password.
SECURITY:-Security refers to providing a protection system to computer system resources
such as CPU, memory, disk, software programs and most importantly data/information stored in
the computer system. If a computer program is run by an unauthorized user, then he/she may
cause severe damage to computer or data stored in it. So a computer system must be protected
against unauthorized access, malicious access to system memory, viruses, worms etc. We're
going to discuss following topics in this chapter.
 Authentication
 One Time passwords

 Program Threats

 System Threats

 Computer Security Classifications

 Authentication:-Authentication refers to identifying each user of the system and associating


the executing programs with those users. It is the responsibility of the Operating System to

95
create a protection system which ensures that a user who is running a particular program is
authentic. Operating Systems generally identifies/authenticates users using following three ways

 Username / Password − User need to enter a registered username and password with
Operating system to login into the system.

 User card/key − User need to punch card in card slot, or enter key generated by key
generator in option provided by operating system to login into the system.

 User attribute - fingerprint/ eye retina pattern/ signature − User need to pass his/her
attribute via designated input device used by operating system to login into the system.

 One Time passwords:- One-time passwords provide additional security along with normal
authentication. In One-Time Password system, a unique password is required every time user
tries to login into the system. Once a one-time password is used, then it cannot be used again.
One-time password is implemented in various ways.

 Random numbers − Users are provided cards having numbers printed along with
corresponding alphabets. System asks for numbers corresponding to few alphabets
randomly chosen.

 Secret key − User are provided a hardware device which can create a secret id mapped
with user id. System asks for such secret id which is to be generated every time prior to
login.

 Network password − Some commercial applications send one-time passwords to user


on registered mobile/ email which is required to be entered prior to login.

 Program threats:- Operating system's processes and kernel do the designated task as
instructed. If a user program made these process do malicious tasks, then it is known
as Program Threats. One of the common examples of program threat is a program installed in
a computer which can store and send user credentials via network to some hacker. Following is
the list of some well-known program threats.

 Trojan Horse − Such program traps user login credentials and stores them to send to
malicious user who can later on login to computer and can access system resources.

96
 Trap Door − If a program which is designed to work as required, have a security hole in
its code and perform illegal action without knowledge of user then it is called to have a
trap door.

 Logic Bomb − Logic bomb is a situation when a program misbehaves only when certain
conditions met otherwise it works as a genuine program. It is harder to detect.

 Virus − Virus as name suggest can replicate themselves on computer system. They are
highly dangerous and can modify/delete user files, crash systems. A virus is generally a
small code embedded in a program. As user accesses the program, the virus starts getting
embedded in other files/ programs and can make system unusable for user

 System threats: - System threats refer to misuse of system services and network connections to
put user in trouble. System threats can be used to launch program threats on a complete network
called as program attack. System threats create such an environment that operating system
resources/ user files are misused. Following is the list of some well-known system threats.

 Worm − Worm is a process which can choked down a system performance by using
system resources to extreme levels. A Worm process generates its multiple copies where
each copy uses system resources, prevents all other processes to get required resources.
Worms processes can even shut down an entire network.

 Port Scanning − Port scanning is a mechanism or means by which a hacker can detects
system vulnerabilities to make an attack on the system.

 Denial of Service − Denial of service attacks normally prevents user to make legitimate
use of the system. For example, a user may not be able to use internet if denial of service
attacks browser's content settings.

8.3 Computer security classifications


Activity 8.2
 What do you about security classification?
 Which classification is widely used?

As per the U.S. Department of Defense Trusted Computer System's Evaluation Criteria there are four
security classifications in computer systems: A, B, C, and D. this is widely used specifications to
determine and model the security of systems and of security solutions. Following is the brief

97
description of each classification.

1
Type A

Highest Level. Uses formal design specifications and verification techniques. Grants a high
degree of assurance of process security.

2
Type B

Provides mandatory protection system. Have all the properties of a class C2 system.
Attaches a sensitivity label to each object. It is of three types.

 B1 − Maintains the security label of each object in the system. Label is used for
making decisions to access control.

 B2 − Extends the sensitivity labels to each system resource, such as storage objects,
supports covert channels and auditing of events.

 B3 − Allows creating lists or user groups for access-control to grant access or revoke
access to a given named object.

3 Type C

Provides protection and user accountability using audit capabilities. It is of two types.

 C1 − Incorporates controls so that users can protect their private information and
keep other users from accidentally reading / deleting their data. UNIX versions are
mostly Cl class.

 C2 − Adds an individual-level access control to the capabilities of a Cl level system.

8.4 Memory Protection


Activity 8.3
 What are the techniques used for memory protection?
 How can we apply those techniques?
 How much those techniques reduce securities problem?

98
Memory protection prevents one process from affecting the confidentiality, integrity, or availability of
another. This is a requirement for secure multiuser (i.e., more than one user logged in simultaneously)
and multitasking (i.e., more than one process running simultaneously) systems.
PROCESS ISOLATION
Process isolation is a logical control that attempts to prevent one process from interfering with another.
This is a common feature among multiuser operating systems such as Linux, UNIX, or recent
Microsoft Windows operating systems. Older operating systems such as MS-DOS provide no process
isolation, which means a crash in any MS-DOS application could crash the entire system.
Hardware segmentation
Hardware segmentation takes process isolation one step further by mapping processes to specific
memory locations. This provides more security than logical process isolation alone.

Virtual memory
Virtual memory provides virtual address mapping between applications and hardware memory. Virtual
memory provides many functions, including multitasking (multiple tasks executing at once on one
CPU), swapping, and allowing multiple processes to access the same shared library in memory, among
others.
Swapping and paging
Swapping uses virtual memory to copy contents of primary memory (RAM) to or from secondary
memory (not directly addressable by the CPU, on disk). Swap space is often a dedicated disk
partition that is used to extend the amount of available memory. If the kernel attempts to access a page
(a fixed-length block of memory) stored in swap space, a page fault occurs, which means that the page
is not located in RAM and the page is “swapped” from disk to RAM.
Basic input/output system
The IBM PC-compatible basic input/output system (BIOS) contains code in firmware that is executed
when a PC is powered on. It first runs the power-on self-test (POST), which performs basic tests,
including verifying the integrity of the BIOS itself, testing the memory, and identifying system devices,
among other tasks. Once the POST process is complete and successful, it locates the boot sector (for
systems that boot off disks), which contains the machine code for the operating system kernel. The
kernel then loads and executes, and the operating system boots up.
WORM storage
WORM (write once, read many) storage, like its name suggests, can be written to once and read many
times. It is often used to support records retention for legal or regulatory compliance. WORM storage

99
helps assure the integrity of the data it contains; there is some assurance that it has not been and cannot
be altered, short of destroying the media itself.

8.5 Authentication
Activity 8.4
 Essay the difference between authentication and authorization in
operating system?
 How authentication is works?

Authentication is the process of determining whether someone or something is, in fact, who or what it
declares itself to be. Authentication technology provides access control for systems by checking to see
if a user's credentials match the credentials in a database of authorized users or in a data authentication
server. Users are usually identified with a user ID, and authentication is accomplished when the user
provides a credential, for example a password, that matches with that user ID. Most users are most
familiar with using a password, which, as a piece of information that should be known only to the user,
is called a knowledge authentication factor. Other authentication factors, and how they are used for
two-factor or multifactor authentication (MFA), are described below.
8.6 Authentication in cyber security
Authentication is important because it enables organizations to keep their networks secure by
permitting only authenticated users (or processes) to access its protected resources, which may include
computer systems, networks, databases, websites and other network-based applications or services.
Once authenticated, a user or process is usually subjected to an authorization process as well, to
determine whether the authenticated entity should be permitted access to a protected resource or
system. A user can be authenticated but fail to be given access to a resource if that user was not granted
permission to access it.
The terms authentication and authorization are often used interchangeably; while they may often be
implemented together the two functions are distinct. While authentication is the process of validating
the identity of a registered user before allowing access to the protected resource, authorization is the
process of validating that the authenticated user has been granted permission to access the requested
resources. The process by which access to those resources is restricted to a certain number of users is
called access control. The authentication process always comes before the authorization process.
8.6.1 How authentication is used

100
User authentication occurs within most human-to-computer interactions outside of guest accounts,
automatically logged-in accounts and kiosk computer systems. Generally, a user has to choose a
username or user ID and provide a valid password to begin using a system. User authentication
authorizes human-to-machine interactions in operating systems and applications, as well as both wired
and wireless networks to enable access to networked and internet-connected systems, applications and
resources.
Many companies use authentication to validate users who log into their websites. Without the right
security measures, user data, such as credit and debit card numbers, as well as Social Security numbers,
could get into the hands of cybercriminals. Organizations also use authentication to control which users
have access to corporate networks and resources, as well as to identify and control which machines and
servers have access. Companies also use authentication to enable remote employees to securely access
their applications and networks. For enterprises and other large organizations, authentication may be
accomplished using a single sign-on (SSO) system, which grants access to multiple systems with a
single set of login credentials.
8.6.2 How authentication works
During authentication, credentials provided by the user are compared to those on file in a database of
authorized users' information either on the local operating system or through an authentication server. If
the credentials match, and the authenticated entity is authorized to use the resource, the process is
completed and the user is granted access. The permissions and folders returned define both the
environment the user sees and the way he can interact with it, including hours of access and other rights
such as the amount of resource storage space.
Traditionally, authentication was accomplished by the systems or resources being accessed; for
example, a server would authenticate users using its own password system, implemented locally,
using login IDs (user names) and passwords. Knowledge of the login credentials is assumed to
guarantee that the user is authentic. Each user registers initially (or is registered by someone else, such
as a systems administrator), using an assigned or self-declared password. On each subsequent use, the
user must know and use the previously declared password.
However, the web's application protocols, HTTP and HTTPS, are stateless, meaning that strict
authentication would require end users authenticate each time they access a resource using HTTPS.
Rather than burden end users with that process for each interaction over the web, protected systems
often rely on token-based authentication, in which authentication is performed once at the start of a

101
session. The authenticating system issues a signed authentication token to the end-user application, and
that token is appended to every request from the client.
Entity authentication for systems and processes can be carried out using machine credentials that work
like a user's ID and password, except the credentials are submitted automatically by the device in
question. They may also use digital certificates that were issued and verified by a certificate authority
as part of a public key infrastructure to authenticate an identity while exchanging information over the
internet.
8.7 Authentication factors
Authenticating a user with a user ID and a password is usually considered the most basic type of
authentication, and it depends on the user knowing two pieces of information: the user ID or username,
and the password. Since this type of authentication relies on just one authentication factor, it is a type
of single-factor authentication. Strong authentication is a term that has not been formally defined, but
usually is used to mean that the type of authentication being used is more reliable and resistant to
attack; achieving that is generally acknowledged to require using at least two different types of
authentication factors.
An authentication factor represents some piece of data or attribute that can be used to authenticate a
user requesting access to a system. An old security adage has it that authentication factors can be
"something you know, something you have or something you are." These three factors correspond to
the knowledge factor, the possession factor and the inherence factor. Additional factors have been
proposed and put into use in recent years, with location serving in many cases as the fourth factor, and
time serving as the fifth factor.
Currently used authentication factors include:
 Knowledge factor: "Something you know." The knowledge factor may be any authentication
credentials that consist of information that the user possesses, including a personal
identification number (PIN), a user name, a password or the answer to a secret question.
 Possession factor: "Something you have." The possession factor may be any credential based on
items that the user can own and carry with them, including hardware devices like a security
token or a mobile phone used to accept a text message or to run an authentication app that can
generate a one-time password or PIN.
 Inherence factor: "Something you are." The inherence factor is typically based on some form of
biometric identification, including finger or thumb prints, facial recognition, retina scan or any
other form of biometric data.

102
 Location factor: "Where you are." While it may be less specific, the location factor is sometimes
used as an adjunct to the other factors. Location can be determined to reasonable accuracy by
devices equipped with GPS, or with less accuracy by checking network routes. The location
factor cannot usually stand on its own for authentication, but it can supplement the other factors
by providing a means of ruling out some requests. For example, it can prevent an attacker
located in a remote geographical area from posing as a user who normally logs in only from
home or office in the organization's home country.
 Time factor: "When you are authenticating." Like the location factor, the time factor is not
sufficient on its own, but it can be a supplemental mechanism for weeding out attackers who
attempt to access a resource at a time when that resource is not available to the authorized user.
It may also be used together with location as well. For example, if the user was last
authenticated at noon in the U.S., an attempt to authenticate from Asia one hour later would be
rejected based on the combination of time and location.
Despite being used as supplemental authentication factors, user location and current time by themselves
are not sufficient, without at least one of the first three factors, to authenticate a user. However, the
ubiquity of smartphones is helping to ease the burdens of multifactor authentication for many users.
Most smartphones are equipped with GPS, enabling reasonable confidence in confirmation of the login
location; smartphone MAC addresses may also be used to help authenticate a remote user, despite the
fact that MAC addresses are relatively easy to spoof.
Two factor and multifactor authentication
Adding authentication factors to the authentication process typically improves security. Strong
authentication usually refers to authentication that uses at least two factors, where those factors are of
different types. The distinction is important; since both username and password can be considered types
of knowledge factor, basic username and password authentication could be said to use two knowledge
factors to authenticate -- however, that would not be considered a form of two-factor
authentication (2FA). Likewise for authentication systems that rely on "security questions," which are
also "something you know," to supplement user ID and passwords.
Two-factor authentication usually depends on the knowledge factor combined with either a biometric
factor or a possession factor like a security token. Multifactor authentication can include any type of
authentication that depends on two or more factors, but an authentication process that uses a password
plus two different types of biometric would not be considered three-factor authentication, although if
the process required a knowledge factor, a possession factor and an inherence factor, it would be.

103
Systems that call for those three factors plus a geographic or time factor are considered examples
of four-factor authentication.
8.8 Authentication and authorization
Authorization includes the process through which an administrator grants rights to authenticated users,
as well as the process of checking user account permissions to verify that the user has been granted
access to those resources. The privileges and preferences granted for the authorized account depend on
the user's permissions, which are either stored locally or on the authentication server. The settings
defined for all these environment variables are set by an administrator.
Systems and processes may also need to authorize their automated actions within a network. Online
backup services, patching and updating systems and remote monitoring systems, such as those used in
telemedicine and smart grid technologies, all need to securely authenticate before they can verify that it
is the authorized system involved in any interaction and not a hacker.
8.9 Encryption
Activity 8.5
 What do we mean encryption?
 How encryption works?

Most search engines, regardless of if they track you, encrypt your search data. This is how search
engines, including Google, Yahoo and Search Encrypt, all protect their users‟ information. Google,
which collects tons of user data, is obligated to protect that information. SSL encryption is a standard for
protecting sensitive information, for search engines and other websites.
What is encryption?
Encryption is a process that encodes a message or file so that it can be only be read by certain people.
Encryption uses an algorithm to scramble, or encrypt, data and then uses a key for the receiving party to
unscramble, or decrypt, the information. The message contained in an encrypted message is referred to
as plaintext. In its encrypted, unreadable form it is referred to as ciphertext. Basic forms of encryption
may be as simple as switching letters. As cryptography advanced, cryptographers added more steps, and
decryption became more difficult. Wheels and gears would be combined to create complex encryption
systems. Computer algorithms have now replaced mechanical encryption.
How encryption works?

104
Encryption uses algorithms to scramble your information. It is then transmitted to the receiving party,
who is able to decode the message with a key. There are many types of algorithms, which all involve
different ways of scrambling and then decrypting information.
Recovery management
One price of extensibility and distribution, as implemented in Quicksilver, is a more complicated set of
failure modes, and the consequent necessity of dealing with them. In traditional operating systems,
services (e.g., file, display) are intrinsic pieces of the kernel. Process state is maintained in kernel
tables, and the kernel contains explicit cleanup code (e.g., to close files, reclaim memory, and get rid of
process images after hardware or software failures). Quicksilver, however, is structured according to
the client-server model, and as in many systems of its type, system services are implemented by user-
level processes that maintain a substantial amount of client process state. Examples of this state are the
open files, screen windows, address space, etc., belonging to a process. Failure resilience in such an
environment requires that clients and servers be aware of problems involving each other. Examples of
the way one would like the system to behave include having files closed and windows removed from
the screen when a client terminates, and having clients see bad return codes (rather than hanging) when
a file server crashes. This motivates a number of design goals:
 Properly written programs (especially servers) should be resilient to external process and
machine failures, and should be able to recover all resources associated with failed entities.
 Server processes should contain their own recovery code. The kernel should not make any
distinction between system service processes and normal application processes.
 To avoid the proliferation of ad-hoc recovery mechanisms, there should be a uniform system-
wide architecture for recovery management.
 A client may invoke several independent servers to perform a set of logically related activities
(a unit of work) that must execute atomically in the presence of failures, that is, either all the
related activities should occur or none of them should. The recovery mechanism should support
this.
Summary
The security of the operating system is vital for both its own and its applications‟ sakes. Security
failures in the software allow essentially limitless bad consequences. While achieving system
security is challenging, there are known design principles that can help. These principles are useful
not only in designing operating systems, but in designing any large software system. Achieving
security in operating systems depends on the security goals

105
one has. These goals will typically include goals related to confidentiality, integrity, and
availability. In any given system, the more detailed particulars of these security goals vary, which
implies that different systems will have different security policies intended to help them meet their
specific security goals. As in other areas of operating system design, we handle these varying
needs by separating the specific policies used by any particular system from the general
mechanisms used to implement the policies for all systems.
Chapter Eight Review Questions
1. List and discus the components security of a system which depends on.
2. Describe briefly about trap door within example.
3. What mean social engineering? How you describe by your own words?
4. What be the factors of authentication?
5. What make difference authentication from authorization?
6. Compare and contrast the windows and Ubuntu‟s security which one is preferable?
7. List and describe system vulnerability!
8. Compare and contrast decryption and encryption briefly!
9. List and Explain the step and procedure followed in encryption and decryption?

106
Referencees
Assistant Prof. Dr. Qasim Mohammed Hussein. Lecture Note on Operating system, [1-3],
2019

https://www.google.com/url?tutorialspoint.com%2Foperating_system% [online]

Chervenak, and Kurmas, Z.: „„Protecting File Systems: A Survey of Backup Techniques,‟‟ Proc.
15th IEEE Symp. on Mass Storage Systems, IEEE, 2017

Dr. Syed Mansoor Sarwar, Basic concept of Operating System book, Virtual University of
Pakistan, 2016

Daniel P. Bovet, Marco Cesati, Understanding the Linux Kernel, 5th Edition, 2018

Dr. Rakesh Kumar, Introduction to Operating System,april 2017

Fusco, J.: The Linux Programmer’s Toolbox, Upper Saddle River, NJ: Prentice Hall, 2016

Harrison, M.A., Ruzzo, W.L. and Ullman, J.D.: „„Protection in Operating Systems,‟‟
Commun. of the ACM, 2013

https://www.hysolate.com/learn/sandboxing/understanding-os-security-threats-and-security-
controls/[online]

Levine, G.N.: „„Defining Deadlock,‟‟ Operating Systems Rev., Jan. 2019

Curtsinger, C. and Berger, E.D.: „„threads: Efficient Deterministic Multi- threading,‟‟ Proc. 23rd
Symp. of Operating Systems Principles, ACM, 2018.

Marino, Hammer, C and Vitek, J.: „„Detecting Deadlock in Programs with Data-Centric
Synchronization,‟‟ Proc. Int’l Conf. on Soft- ware Engineering, IEEE, 2016

Marsh, B.D, Scott, M.L and Markatos, E.P.: „„First-Class User-Level Threads,‟‟ Proc. 13th
Symp. on Operating Systems Principles, ACM, pp., 2016.
Stallings, W: Operating Systems, 9th ed., Upper Saddle River, NJ: Prentice Hall, 2017.

Teory, T.J: „„Properties of Disk Scheduling Policies in Multiprogrammed Computer Systems,‟‟


Proc. AFIPS Fall Joint Computer Conf., AFIPS, pp, 2014.

https://www.guru99.com/process-scheduling.html[online]

Vaghani, S.B: „„Virtual Machine File System,‟‟ ACM SIGOPS Operating Systems Rev, 2013.

https://opentextbc.ca/computerstudies/chapter/basic-file-management/[online]

107
.

108

You might also like