Operating System
Operating System
BSDS-302
Unit -1 : Introduction to Operating System
1
does not do anything without properly instructed. Thus, for each one of the power-on activities also, the
computer must have instructions. These instructions are stored in a non-volatile memory, usually in a
ROM. The CPU of the computer takes one instruction from this ROM and executes it before taking next
instruction. One by one the CPU executes these instructions. Once, these instructions are over, the CPU
must obtain instructions from somewhere. Usually these instructions are stored on a secondary storage
device like hard disk, floppy disk or CD-ROM disk. These instructions are collectively known as Oper-
ating System and their primary function is to provide an environment in which users may execute their
own instructions.
Once the operating system is loaded into the main memory, the CPU starts executing it. Operating systems
run in an infinite loop, each time taking instructions in the form of commands or programs from the users
and executing them. This loop continues until the user terminates the loop when the computer shuts down.
1.1.1 Some Definitions
Operating system is the most important program on a computer. It basically runs the computer and allows
other programs to run as well. The operating system does all the basic things that a computer needs to do,
such as recognizing inputs from the mouse or the keyboard. It keeps track of where all the files are on the
computer. It allocates resources to the various programs that are running and it prevents unauthorized
access to the computer.
The most popular operating system today is Microsoft’s Windows operating system. Macintosh
computers have their own operating system, the most recent of which is called Mac OS X. There are also
open source operating systems such as Linux. Some of the important definitions of operating system are
given as under:
1. Operating System is a software program that acts as an interface between the user and the com-
puter. It is a software package which allows the computer to function.
2. An operating system is a computer program that manages the resources of a computer. It accepts
keyboard or mouse inputs from users and displays the results of the actions and allows the user to run
applications, or communicate with other computers via networked connections.
3. An operating system is the collection of software that directs a computer’s operations, controlling
and scheduling the execution of other programs, and managing storage, input/output, and communication
resources is it needed to write sources. (Source:dictionary.com).
4. Operating system is a platform between hardware and user which is responsible for the manage-
ment and coordination of activities and sharing of resources of a computer. It hosts several applications
that run on a computer and handles the operations of computer hardware.
5. An operating system is the program that, after being initially loaded into the computer by a boot pro-
gram, manages all the other programs in a computer. The other programs are called applications or application
programs. The application programs make use of the operating system by making requests for services through
a defined application program interface (API). In addition, users can interact directly with the operating system
through a user interface such as a command language or a Graphical User Interface (GUI).
1.1.2 Goals
An operating system is the most important program in a computer system. This is one program that runs all
the time, as long as the computer is operational and exits only when the computer is shut down.
Operating systems exist because they are a reasonable way to solve the problem of creating a usable
computing system.
2
The fundamental goal of computer systems is to execute user programs and to make solving user problems
easier. Hardware of a computer is equipped with extremely capable resources – memory, CPU, I/O
devices etc. All these hardware units interact with each other in a well-defined manner. Hardware alone is
not enough to solve a problem, particularly easy to use, application programs are developed. These
various programs require certain common operations, such as those controlling the I/O devices. The
common functions of controlling and allocating resources are then brought together into one piece of
software: the operating system.
It is easier to define operating systems by their functions, i.e., by what they do than by what they are. The
computer becomes easier for the users to operate, is the primary goal of an operating system. Efficient
operation of the computer system is a secondary goal of an operating system. This goal is particularly
important for large, shared multi-user systems. These systems are typically expensive, so it is desirable to
make them as efficient as possible.
Operating systems and computer architecture have had a great deal of influence on each other. To facilitate
the use of the hardware, operating systems were developed. As operating systems were designed and
used, it became obvious that changes in the design of the hardware could simplify them. Operating systems
are the programs that make computers operational, hence the name.
Without an operating system, the hardware of a computer is just an inactive electronic machine, possessing
great computational power, but doing nothing for the user. All it can do is to execute fixed number of
instructions stored into its internal memory (ROM: Read Only Memory), each time you switch the power
on, and nothing else.
Operating systems are programs that act as interface between the user and the computer hardware. They
sit between the user and the hardware of the computer providing an operational environment to the users
and application programs. For a user, therefore, a computer is nothing but the operating system running on
it. It is extended machine.
Users do not interact with the hardware of a computer directly but through the services offered by
operating system. This is because the language that users employ is different from that of the hardware.
Whereas users prefer to use natural language or near natural language for interaction, the hardware uses
machine language. It is the operating system that does the necessary translation back and forth and lets the
user interact with the hardware. The operating system speaks users’ language one hand and machine
language on the other. It takes instructions in form of commands from the user and translates into machine
understandable instructions, gets these instructions executed by the CPU and translates the result back
into user-understandable form.
A user can interact with a computer if only he/she understands the language of the resident operating
system. You cannot interact with a computer running UNIX operating system, for instance, if you do not
know ‘UNIX language’ or UNIX commands. A UNIX user can always interact with a computer running
UNIX operating system, no matter what type of computer it is. Thus, for a user operating system itself is
the machine – an extended machine as shown in figure 1.1
3
user user user user
1 2 3 - - - - - n
application programs
Operating Ssystem
Computer hardware
CPU memory
M I/O
5
1.2.1 Single User– Single Processing System
The simplest of all the computer systems is a single user-single processor system. It has a single processor,
runs a single program and interacts with a single user at a time. The operating system for this system is very
simple to design and implement. However, the CPU is not utilized to its full potential, because it sits idle for
most of the time. Figure 1.2
Application user
program
operating system
hardware
6
Check your progress - 1.1
1. An ________ is a computer program that manages the resources of a computer.
(a) Operating System (b) Software
(c) System (d) None of above ( )
2. _______of the computer system is a secondary goal of an operating system.
(a) Encryption (b) Decryption
(c) Efficient operation (d) None of above ( )
3. ________ has a single processor, runs a single program and interacts with a single user at a time.
(a) Real Time systems (b) Distributed Systems
(c) Single user-single processor system (d) All of above ( )
4. The fundamental goal of computer systems is __________ and to make solving user problems
easier.
(a) to provide entertainment to user (b) to provide security to user programs
(c) to execute user programs (d) None of above ( )
5. The main function of a __________is to automatically keep executing one job to the next job in
the batch.
(a) Batch processing system (b) Distributed Systems
(c) Multiprogramming Systems (d) Real Time Systems ( )
´ ´ ´
9
Unit- 2 : User Interface & Computing Environments
Structure of the Unit
2.0 Objective
2.1 Introduction
2.2 User Interfaces
2.2.1 Command Interpreter / Command User Interface (CUI)
2.2.2 Graphical User Interfaces (GUI)
2.2.3 Difference between CUI and GUI
2.3 Computing Environments
2.3.1 Traditional Computing
2.3.2 Client-Server Computing
2.3.3 Peer-to-Peer Computing
2.3.4 Web-based Computing
2.4 System Calls
2.5 Summary
2.6 Answer to the Self Learning Exercises
2.7 Self Assesment Questions
2.0 Objective
This unit provides a brief description and understanding about most often terms of an Operating systems
i.e. ‘User Interfaces’ and ‘Computing Environments’. This unit first presents a brief description about
Command Interpreter and Graphical User Interface and then after it provides information about various
types of ‘computing Environments’ such as Client-Server Computing, Peer-to-Peer Computing, Web
based Computing etc. Self answering questions and names of various reference books are also provided
at the end of this unit.
2.1 Introduction
When the computer is turned on, a small ‘boot program’ loads the operating system. Although additional
system modules may be loaded as needed, the main part, known as the ‘kernel’ resides in memory at all
times. The operating system (OS) sets the standards for all application programs that run in the computer.
Applications ‘talk to’the operating system for all user interfaces and file management operations.Also called
an ‘executive’ or ‘supervisor’, an operating system performs the some functions like – User Interface, Job
Management, Task Management, Data Management, Device Management and Security etc.
2.2 User Interfaces
Operating system works as an Interface between ‘Computer Hardware’ and the ‘User’. Graphics based
user interface includes the windows, menus and method of interaction between the user and the computer.
Prior to graphical user interfaces (GUIs), all operation of the computer was performed by typing in
commands. Not at all extinct, a command-line interface is included in all major operating systems, and
certain highly technical operations must be executed from the command line.
Operating systems may support optional interfaces. Although the overwhelming majority of people work
with the default interface, different ‘shells’ offer variations of appearance and functionality. There are mainly
two types of user interfaces available and known as – Command User Interface (CUI) and Graphical User
10
Interface (GUI). A CUI and a GUI are the two most common interface types associated with computers.
They allow the user to interact with the machine, giving it commands and viewing text or graphics.
2.2.1 Command Interpreter/ Character-Based User Interface (CUI)
CUI stands for Character-based User Interface. Early computers operated on CUIs, and they’re most
iconic for their typical two-colored, black and green screens with nothing but text. Users would use a
keyboard to both navigate (using hotkeys) and enter commands. There was no need for a mouse in these
early days, as Character-Based User Interfaces do not support such advanced hardware.
A program which reads textual commands from the user or from a file and executes them is called
‘Command Interpreter’. Some commands may be executed directly within the interpreter itself (e.g.
setting variables or control constructs), others may cause it to load and execute other files.
In other words, a ‘Command Interpreter’ is the part of a computer operating system that understands and
executes commands that are entered interactively by a human being or from a program.
In some operating systems, the command interpreter is called the shell.
When an IBM PC is booted BIOS loads and runs the MS-DOS command interpreter into memory from
file COMMAND.COM found on a floppy disk or hard disk drive. The commands that
COMMAND.COM recognizes (e.g. COPY, DIR, PRN) are called internal commands, in contrast to
external commands which are executable files. According to technology website, PC Mag, the CUI was
most common on older mainframe and minicomputer terminals in the early days of computing. Some early
computers even featured character-based user interfaces, the most notable being Macintosh’s Apple II
computer. While CUIs have since fallen out of favor for graphical user interfaces, most modern operating
systems feature a modified version of a CUI called a command line interface.
2.2.2 Graphical User Interface (GUI)
A graphical user interface (GUI) is a computer environment that simplifies the user’s interaction with the
computer by representing programs, commands, files, and other options as visual elements, such as icons,
pull-down menus, buttons, scroll bars, windows, and dialog boxes. By selecting one of these graphical
elements, through either use of a mouse or a selection from a menu, the user can initiate different activities,
such as starting a program or printing a document. Prior to the introduction of GUI environments, most
interactive user interface programs were text oriented and required the user to learn a set of often complex
commands that could be unique to a given program.
The first GUI was developed in the 1970s by Xerox Corporation, although GUIs did not become popular
until the 1980s with the emergence of the Apple Macintosh computer. Today, the most familiar GUI
interfaces are Apple Computer’s Macintosh and Microsoft Corporation’s Windows operating systems.
Computer software applications, such as word processing and spreadsheet packages, typically use the set
of GUI elements built into the operating system and then add other elements of their own. The advantage
of the GUI element of any software program is that it provides a standard method for performing a given
task (i.e., copying a file, formatting text, printing a document) each time the user requests that option,
rather than creating a set of commands unique to each potential request.
11
Figure 2.1: Essential components of Windows Operating system as a GUI
Many GUI elements are standard across all packages built on the same operating system, so once a user
is familiar with the GUI elements of one package, it is easier to then work in other packages.
12
Explorer, and similar programs enable a user to access and search the web using the familiar GUI format.
Today, GUIs are synonymous with personal computing. GUIs are much more advanced than their earlier coun-
terparts because of all the computing and graphical power available today. While many operating systems allow
for full control with a keyboard, these systems are much easier to operate with the addition of a mouse.
2.2.3 Difference between CUI and GUI
CUI and GUI are acronyms that stand for different kinds of user interface systems. These are terms used
in reference to computers. CUI stands for Character User Interface while GUI refers to Graphical User
Interface. Though both are interfaces and serve the purpose of running the programs, they differ in their
features and the control they provide to the user.
CUI means you have to take help of a keyboard to type commands to interact with the computer. One can
only type text to give commands to the computer as in MS-DOS on command prompt. There are no
images or graphics on the screen and it is a primitive type of interface. In the beginning, computers had to
be operated through this interface and users who have seen it say that they had to contend with a black
screen with white text only. In those days, there was no need of a mouse as CUI did not support the use
of pointer devices. CUI’s have gradually become outdated with the more advanced GUI taking their
place. However, even the most modern computers have a modified version of CUI called CLI (Command
Line Interface). GUI is an interface that makes use of graphics, images and other visual clues such as icons.
This interface made it possible for a mouse to be used with a computer and interaction really became very
easy as the user could interact with just a click of the mouse rather than having to type every time to give
commands to the computer. Some other differences are given as under,
• CUI is the precursor of GUI and stands for character user interface where user has to type on key-
board to proceed. On the other hand GUI stands for Graphical User Interface which makes it possible to
use a mouse instead of keyboard;
• GUI is event driven in nature but CUI is sequence oriented in nature;
• GUI is much easier to navigate than CUI;
• There is only text in case of CUI whereas there are graphics and other visual clues in case of GUI;
• Most of modern computers use GUI and not CUI;
MS-DOS is an example of CUI whereas MS-Windows is an example of GUI.
Check your progress 2.1
1. GUI stands for_________.
(a) Graphical User Interface (b) Global User Interface
(c) Graphical Universal Interface (d) None of above ( )
2. The first GUI was developed in the ______ by Xerox Corporation.
(a) 1980s (b) 1990s
(c) 1970s (d) 1960s ( )
3. Operating system works as an _______ between ‘Computer Hardware’ and the ‘User’.
(a) Interface (b) System
(c) Processor (d) None of above ( )
4. Modified version of CUI called as________.
(a) CLI (b) GSI
(c) VLSI (d) None of above ( )
5. _________is the most suitable and popular example of GUI.
(a) MS-DOS (b) Disk Operating System
(c) Windows (d) Expert Systems ( )
13
2.3 Computing Environments
An operating system may process its tasks serially or simultaneously, which means that the resources of the
computer system may be dedicated to a single program until its completion or they may be allocated
among several programs in different stages of execution. So, there are several computing environments as
discussed below.
2.3.1 Traditional Computing
Machine language was quite common for early computer systems. Instructions and data used to be fed
into the computer system by means of console switches through a hexadecimal keyboard. Programs used
to be started by loading the program computer register with the address of the first instruction of a
program and its result used to be examined by the contents of various registers and memory locations of
the machine. This programming style caused a low utilization of users and machine. Program started being
coded into programming language are first changed into object code (i.e. binary code) by translator and
then automatically gets loaded into memory by a program called Loader. After transferring the control to
the loaded program, the execution of program begins and its results get displayed or printed. Once in
memory, the program may be re-run with a different set of input data. This type of computing environment
is called ‘Serial Processing’ computing.
‘Batch Processing’ Computing is another type of traditional computing environment. The main function
of a batch processing system is to automatically keep executing one job to the next job in the batch. The
main idea behind a batch processing system is to reduce the interference of the operator during the
processing or execution of jobs by the computer. All functions of a batch processing system are carried out
by the batch monitor. The batch monitor permanently resides in the low end of the main store. The current
jobs out of the whole batch are executed in the remaining storage area. In other words, a batch monitor is
responsible for controlling all the environment of the system operation. The batch monitor accepts batch
initiation commands from the operator, processes a job, performs the job of job termination and batch
termination.
2.3.2 Client-Server Computing
Client–server computing is a distributed computing model in which client applications request services
from server processes. Clients and servers typically run on different computers interconnected by a
computer network. Any use of the Internet, such as information retrieval from the World Wide Web, is an
example of client–server computing.
However, the term is generally applied to systems in which an organization runs programs with
multiple components distributed among computers in a network. The concept is frequently associated
with enterprise computing, which makes the computing resources of an organization available to every part
of its operation.
A client application is a process or program that sends messages to a server via the network. Those
messages request the server to perform a specific task, such as looking up a customer record in a database
or returning a portion of a file on the server’s hard disk. The client manages local resources such as a
display, keyboard, local disks, and other peripherals. The server process or program listens for client
requests that are transmitted via the network. Servers receive those requests and perform actions. Server
processes typically run on powerful PCs, workstations, or mainframe computers.
14
Figure 2.3: Client-Server Computing Environment
An example of a client–server system is a banking application that allows a clerk to access account
information on a central database server. All access is done via a PC client that provides a graphical user
interface (GUI). An account number can be entered into the GUI along with how much money is to be
withdrawn or deposited, respectively. The PC client validates the data provided by the clerk, transmits the
data to the database server, and displays the results that are returned by the server. The client–server
model is an extension of the object based (or modular) programming model, where large pieces of
software are structured into smaller components that have well defined interfaces. This decentralized
approach helps to make complex programs maintainable and extensible. Components interact by
exchanging messages or by Remote Procedure Calling. The calling component becomes the client and the
called component the server.
A client–server environment may use a variety of operating systems and hardware from multiple vendors;
standard network protocols like TCP/IP provide compatibility. Vendor independence and freedom of
choice are further advantages of the model. Client–server systems can be scaled up in size more readily
than centralized solutions since server functions can be distributed across more and more server
computers as the number of clients increases. Server processes can thus run in parallel, each process
serving its own set of clients. However, when there are multiple servers that update information, there must
be some coordination mechanism to avoid inconsistencies.
The drawbacks of the client–server model are that security is more difficult to ensure in a distributed
environment than it is in a centralized one, that the administration of distributed equipment can be much
more expensive than the maintenance of a centralized system, that data distributed across servers needs to
be kept consistent, and that the failure of one server can render a large client–server system unavailable.
If a server fails, none of its clients can make further progress, unless the system is designed to be
fault-tolerant.
The computer network can also become a performance or reliability bottleneck: if the network fails, all
servers become unreachable. If one client produces high network traffic then all clients may suffer from
long response times.
15
Figure 2.4: A typical Client-Server interaction
2.3.3 Peer-to-Peer Computing
The term ‘peer-to-peer’ (P2P) refers to a class of systems and applications that employ distributed
resources to perform a function in a decentralized manner. With the pervasive deployment of computers,
P2P is increasingly receiving attention in research, product development, and investment circles.
In this type of computing all nodes on the network have equal relationships to all others, and all have similar
types of software. Typically, each node has access to at least some of the resources on all other nodes, so
the relationship is nonhierarchical. If they are set up correctly, Operating system give users access to the
resources attached to other computers in the net-work. A peer-to-peer computing environment is shown
in Figure 2.5.
In addition, some high-end peer-to-peer networks allow distributed computing, which enables users to
draw on the processing power of other computers in the network. That means people can transfer tasks
that take a lot of CPU power such as creating computer software to available computers, leaving their own
machines free for other work.
Peer-to-peer computing environment is commonly set up in small organizations (fewer than 50 employees)
or in schools, where the primary benefit of a network is shared storage, printers, and enhanced communication.
Where large data-bases are used, LANs are more likely to include client/server relationships.
A peer-to-peer network can also include a network server. In this case, a peer-to-peer LAN is similar to
a file server network. The only difference between them is that the peer-to-peer network gives users
greater access to the other nodes than a file server network does.
Process 1
Syscal 1 Processor
i = next instruction
7
OS 2
5 ii
System PSW 6 i PSW
call
interrupt 3
handler
to 4
0
6
Syscall
add
Get process attributes and set To Get and Set process attributes
process attributes
For accessing device, system calls are used. Many of the system calls for files are also needed for devices.
In multi-user environment, request is made before to sue of that device. After using any device, it must be
release. So using release system call, device is free to use by another user. These functions are similar to
the open and close system call of files. System calls for ‘I/O Device Management’ are as follows-
Release device Release the device after finished with the device.
The operating system provides a set of operations which are called system calls. A system call interface is
the description of the set of system calls implemented by the operating system.
19
Check your Progress 2.2
1. Serial Processing is the example of ____________.
(a) Traditional computing Environment (b) Web based Computing
(c) Firewall systems (d) None of above ( )
2. Client–server computing is a ___________in which client applications request services
from server processes.
(a) Serial Computing Model (b) Graphics Computing System
(c) Distributed computing model (d) None of above ( )
3. In a Client-Server computing environment, a ________is a process or program that sends
messages to a server via the network.
(a) System Applications (b) Client Application
(c) Security Applications (d) None of above ( )
4. ___________constantly monitors the system for detecting the errors.
(a) Cryptographic systems (b) Firewall systems
(c) Operating Systems (d) None of above ( )
5. In _________computing all nodes on the network have equal relationships to all others,
and all have similar types of software.
(a) Peer-to-Peer Computing (b) Client-Server Computing
(c) Cryptographic systems (d) None of above ( )
2.5 Summary
Operating system works as an Interface between ‘Computer Hardware’ and the ‘User’. There are
mainly two types of user interfaces available and known as ‘Command User Interface’ (CUI) and ‘Graphical
User Interface’ (GUI). A program which reads textual commands from the user or from a file and executes
them is called ‘Command Interpreter’. A graphical user interface (GUI) is a computer environment
that simplifies the user’s interaction with the computer by representing programs, commands, files, and
other options as visual elements, such as icons, pull-down menus, buttons, scroll bars, windows, and
dialog boxes. DOS is an example of CUI whereas Windows is an example of GUI. GUI is event driven in
nature but CUI is sequence oriented in nature. Client–server computing is a distributed computing model
in which client applications request services from server processes. Clients and servers typically run on
different computers interconnected by a computer network. A client application is a process or program
that sends messages to a server via the network. The term ‘peer-to-peer’ (P2P) refers to a class of
systems and applications that employ distributed resources to perform a function in a decentralized man-
ner. In this type of computing all nodes on the network have equal relationships to all others, and all have
similar types of software. Web computing can be defined as a special kind of distributed computing that
involves internet-based collaboration of several remotely located applications. The idea behind Web Com-
puting is to make distributed computing accessible to as many people as possible. System calls provide the
interface between a process and the operating system. A system call instruction is an instruction that
generates an interrupt that cause the operating system to gain control of the processor.
20
2.6 Answer to the Self Learning Exercises
Answer to check your progress 2.1
1. (a) 2. (c) 3. (a) 4. (a) 5. (c)
Answer to check your progress 2.2
1. (a) 2. (c) 3. (b) 4. (c) 5. (a)
2.7 Self Assesment Questions
1. What is a system call? Give some examples of system calls.
2. Discuss the following with proper illustration,
• Peer-to-Peer computing environment
• Client-Server Computing
3. What do you understand by ‘Web-based’ computing?
4. Write a short note on various types of ‘User Interfaces’.
5. What is GUI? Give any three examples of GUI.
6. What is Command interpreter? Give any one example of CUI.
´ ´ ´
21
Unit-3 : Types of Operating Systems
22
User and other system programs
System cells
This operating system requires grouping of similar jobs which consist of programs, data and system
commands. In batch processing, jobs are loaded into a system and processed one at a time. Once loaded,
a job remains in the main memory until its execution is over and next job is loaded automatically, only, after
the completion of current job. This type of processing is suitable for those programs which require large
computation time. In this processing, there is little or no need of user interaction. The user prepares their
program and data and submits them to the operator. The operator gives a command to the system to start
executing the batched jobs. When all jobs in the submitted batch were processed, the operator takes out
the printout and keeps them. These printouts are collected by the user later on. Some example of such
programs includes payroll, forecasting, etc.
In the Batch Processing, the Process Scheduling, Memory Management, File Management and Input/
output Management are simple. As only one program is executive at a time, there is no competition for
Input/output devices. Also, there is hardly a need for the file access control mechanism in this processing.
Here the memory is divided into two parts - one part contains operating system routines while the other
part contain user program to be executed.
24
Limitation:
(1) Time taken between job submission and job completion is very high
(2) As user interaction is very limited, they have no control over the intermediate results of a program.
(3) The programmer cannot correct bugs, the moment it occurs in this approach. Thus programs must
be debugged.
3.3.2 Multiprogramming Operating System
Suppose there is a single program resident in the main memory and it is being executed by the CPU. If the
program is I/O bound, then the program would run for a moment and then pass into the wait state (see
Figure 3.4). During the wait period, the CPU remains idle. Moreover, irrespective of the type of program,
whenever, a program finishes its job, the operating system loads new program from the hard disk into the
memory and hand over the control to CPU. The Hard Disk being a slower device as compared to Central
Processing Unit, a lot of time is wasted during loading of the new program and obviously the CPU remains
idle in the mean time.
Operating System
User Job
Execution
in progress
CPU
The remedy to the above cited problem is multiprogramming. This type of operating system allows con-
current residency of many programs in the main memory of the computer. As there is more than one
program resident in the main memory, another program is available for the execution in a situation where
the current executing program enters in the wait state. Thus the CPU will spend less time idle. When a
program finishes its job, the Central Processing Unit is immediately allotted to another program residing in
the main memory and thus, no time is wasted. The vacancy created by the ongoing program is filled by the
operating system who loads a new program from the hard disk to the vacant area in the main memory.
One of the unique features of multiproamming system is that, storage is allocated for each program. The
areas of primary storage allocated for the individual programs are called partitions. Each partition must
have some form of storage and priority protection to ensure that a program in one portion will not acci-
dently writes over and destroys the instruction of another partition and priority because both, programs
will need access to the central processing unit facilities. Figure 3.5 gives illustration of multiprogramming
system where there three jobs (1, 2 and 3) residing in the memory, out of which job1 is performing I/O
operation, job2 is executing and job3 is waiting for the CPU to become free.
25
Operating system
Secondary Job 2
Disk
Storage Job 3
(Waiting for CPU)
Execution in
Progress CPU
Blocked
27
Real Time System is of two types: Hard and Soft.
A soft real time system is less restrictive and in it critical real time task receives priority over other tasks and
it retains that priority until it completes. For example, Linux provides soft real time support.
A hard real time system has stringent requirement and in it critical real time task is to be completed within
their deadlines. Safety critical systems are hard real time system.
The design of the Real Time Operating System reflects its single purpose nature and is often quite simple.
Their microprocessors are inexpensively mass produced. The main objective of R.T.O.S. is to provide
quick response time. Accordingly, the defining characteristics of the system are to support the time
requirements of the real time tasks. Real Time System meet the time requirement by using Scheduling
algorithm that gives real time processes the highest scheduling priorities.Also, the schedulers must ensure
that the priority of a real time task does not degrade over time. The processor is normally allocated the
highest priority process among those which are ready to execute, user convince and resource utilization
are of secondary concern. As there is a little swapping of program between primary and secondary
memory, the memory management is less demanding in this system, however, the processes tend to
provide protection and sharing of the memory. Real Time System provides sophisticated form of interrupt
management and I / O buffering. As compare to file management, concern is on the speed of access rather
than efficient utilization of the secondary storage.
3.3.6 Multiprocessor Operating System
Multiprocessor system is that system which is composed of several independent processors. These
processors operate in parallel, thereby allowing, simultaneous execution of several programs or of several
parts of the same program. Basic organisation of Multiprocessing system is given in Figure 3.7.
P1 Main Memory P2
28
Multiprocessing System have better performance due to shorter response time and higher throughput.
They have better reliability, that is, if one of the processor break down, the other processor(s) takes over
the system workload automatically until the faulty processor is repaired. Thus in this system many complex
operations can be performed at the same time. Multiprocessing finds its application in rail control, traffic
control, airways, etc.
Multiprocessing System has its limitation. These systems have high initial cost and their regular operation
and maintenance is costlier than single processor system. Also, designing of such system is very complex
and time taking.
3.3.7 Distributed Operating System
A Distributed Operating System is a collection of loosely coupled processors that do not share memory.
These processors communicate with one another through various communication networks. Thus, this
system allows users to share resources on geographically dispersed hosts connected via a computer
network. The processors in a Distributed System may vary in size and functions. They may include small
microprocessor, workstation, minicomputer and large general purpose computer system. A general
structure of a Distributed System is shown in Figure 3.8.
Site A Site B
Communication
Client
Site C
30
9. Differentiate between Multiprogramming and Multiprocessing processing.
10. What is Multitasking Processing?
11. Define the essential properties of the following types of operating system:
(a) Real Time
(b) Time Sharing
(c) Distributed
(d) Handheld
(e) Multiprogrmming
(f) Multitasking
(g) Multiprocessing
(h) Embedded
´ ´ ´
31
Unit - 4 : Process Management
Structure of the Unit
4.0 Objective
4.1 Introduction
4.2 Introduction to Processes
4.3 Process State
4.4 Process Control Block
4.5 Context Switching
4.6 Process Creation & Termination
4.7 Basics of Inter-Process Communication
- Shared Memory & Message Passing System
4.8 Basics of Communication in Client-Server System
- Sockets
- R.P.C.
- R.M.I.
4.9 Summary
4.10 Self Assessment Questions
4.0 Objective
After studying this unit you will be able to define the concept of a process, the process state, control
switching and process control block. You will be able to understand how a process is created and terminated.
Also, you will go through the basics of inter-process communication and Client -Server System.
4.1 Introduction
In early computer systems only one program was executed at a time. This program had the access to the
system's all resources. Today, the computer system allows the multiple programs to run concurrently.
This evolution along with the complexity of the operating system requires firmer control and more
compartmentalization of various programs. Hence, a separate module of Process Management was
developed. This module takes care of creation and termination of processes, scheduling of system
resources to different processes requesting them and providing mechanism for synchronization and
communication among various processes. We may define a process as a program in execution. In other
words, process is a running program with some specific tasks to perform. For example, a word processing
program being run by an individual user on computer is a process.
4.2 Introduction to Processes
A process is defined as a program in execution performing certain task allotted to it. For example a time
sharing user program such as compiler is a process. Shell or command interpreter, in UNIX operating
system, is a process, performing the task of listening to whatever is typed on a terminal. A process needs
resources, including CPU time, memory files and input/output devices to perform its task. These
resources are either given to the process when it is created or allocate to it while running. During creation,
various initialization data may be passed. When the process terminates, the operating system will reclaim
32
any reusable resources. In a system, a process is unit of work. As system is collection of processes,
including both, user processes and operating system processes, all these processes can be executed
concurrently.
A process is an 'active' entity whereas a program is a 'passive' entity. Process, includes current activity
along with the contents of the processor's registers. A process, also include, the process stack containing
the primary data and a data section containing global variables. Also, a process may contain a memory
known as heap which is allocated during process run time. More than one process associated with the
same program is considered separate execution sequences. For example, multiple users running different
copies of the mail program.
4.3 Process State
The state of a process is defined in part by the current activity of that process. During execution, a process
changes its state. Depending on the implementation, the operating systems may differ in the number of
states a process goes through. Each process may be in one of the following five states :
(1) New State : The process being created
(2) Running State : The instructions are being executed.
(3) Waiting State : The process is waiting for some event to occur, for instance an
I/O completion or reception of a signal. In this state a process is
unable to proceed until some external events happen.
(4) Ready State : The process is waiting to be assigned to a processor.
(5) Terminated State : The process has finished execution.
It should be remembered that at any instance, only one process can be running on any processor. The five
state process model is given below in Figure 4.1.
interrupt
Ready Running
scheduler dispatch
Waiting
33
4.4 Process Control Block
Process control block is the representation of a process in the operating system. This block, also known as
task control block, is collection of process attributes needed by the operating system to control a process.
A schematic diagram of the Process Control Block is shown in Figure 4.2.
Process state
Process number
Program counter
Registers
Memory limits
List of open files
34
The switching of the CPU to another process requires performing a state save of the current process and
a state restore of a different process. This task is known as Context Switching. During context switching,
the kernel saves the context of the old process in its PCB and loads the context of the new process
scheduled to run.
Let us understand with the help of an example. Suppose if two processes P0 and P1 are in ready queue. If
CPU is executing Process P0 and Process P1 is in wait state. If an interrupt occurs for Process P0, the
operating system suspends the execution of the first process, and stores the current information of Process
P0 in its PCB and switch to the second process namely Process P1. In doing so, the program counter from
the PCB of Process P1 is loaded, and thus execution can continue with the new process. The switching
between two processes, Process P0 and Process P1 is illustrated in the Figure 4.3 :
sche d
pid = 0
Ine td d tlogin
pid = 13 0 pid = 24 1
csh sdt_she l
pid = 77 68 pid = 33 0
csh
pid = 14 00
is cat
pid = 21 13 pid = 25 26
36
In Solaris operating system, the process at the top of the tree is called sched process with pid 0. The shed
process creates several children processes that are responsible for managing memory and file systems.
The child process includes pageout and fsflush. The sched process creates the init process which serves
as the root parent process for all user processes. Inetd and dlogin are the two children of init. inetd is
responsible for networking services, whereas dlogin is the process representing a user login screen. When
a user logs in, dtlogin creates an X-Window session, which in turns creates the sdt_shel process. Below
sdt_shel, a user's command line shell - csh is created. It is this command-line interface where the user then
invokes various child processes such as the 1s and cat commands. csh process with pid of 7768 represent
a user who has logged onto the system using telnet.
When a process creates a sub process, that sub process can either obtain its resources directly from the
operating system or it may be constrained to a subset of the resources of the parent process. Overloading of
the system due to processes is prevented by restricting a child process to a subset of the parent's resources.
When the process finishes executing its final statement and asks the system to delete it by using the exit ()
system call, a process terminates. At that point the process may return a status value - an integer to its
parent process. All the resources of the process, whether physical or virtual memory, open files, and input/
output devices are de-allocated by the operating system.
A process can terminate another process through appropriate system call, for example, TerminateProcess( )
in Win32. The parent of the process can only invoke a system call to terminate the process. Otherwise,
users could arbitrary kill each other's job. When a process creates a new process the identity of the newly
created process is passed to the parent. A parent may terminate the execution of one of its children if the
child has exceeded its usage of some of its resource or the task assigned to the child is no longer required.
There are some systems who do not allow child to exist if its parent has terminated. In such systems, if a
process terminates than all its children must also be terminated. This type of termination is called
Cascading Termination.
4.7 Basics of Interprocess Communication (IPC)
There are two ways in which the processes execute concurrently - independent processes or cooperating
processes. Independent process is the process which cannot affect or be affected by the other processes.
Thus independent process does not share its data with any other processes. Cooperating process is the
process which can affect or be affected by the other processes. In other words, we can say that
cooperating process are those which share their data with other processes. Various salient features of
cooperating process are Information sharing, computation speedup, modularity and convenience.
Interprocess Communication Mechanism is generally required by cooperating process as it allows it to
exchange information and data. Shared Memory and Message Passing are the two fundamental systems
used in the interprocess communication.
In Share Memory System, after establishing the memory region to be shared by the cooperating process,
exchange of information takes place by means of reading and writing data to the shared region. For
example, the producer-consumer problem, where, a producer process produces information which is
consumed by the consumer process.
In Message Passing System, communication takes place by means of messages exchanged between the
cooperating processes. For example, a chat program used on the world wide web could be designed so
that chat participants communicate with one another by exchanging messages. Generally, two operations
are provided in the message passing model - send (message) and receive (message).
37
Shared memory allows maximum speed and convenience of communication whereas message passing is
useful for exchange of smaller data. In shared memory model, system calls are required to establish the
shared- memory region. Once it is established, all accesses are treated as routine memory accesses and no
help from kernel is required. In message passing model, the message passing system are implemented
using system call, which results in more time consuming task of kernel intervention.
4.8 Basics of Communication in Client-Server Systems
Communication in the client - server systems may use sockets, remote procedure calls (RPCs) and java's
remote method invocation (RMI). We will be giving here brief out lines of the three.
Sockets
A socket is defined as an endpoint for communication. A pair of sockets - one for each process, is
employed for a pair of processes communicating over a network. A socket is identified by an IP address
concatenated with a port number. When a client process initiates a request for a connection, it is assigned
a port by the host computer. This port is some arbitrary number greater than 1024. For instance, if a client
on the host A with IP address 136.86.5.20 wishes to establish a connection with a web server (which is
listening on port 70) at address 151.25.18.4, host A may be assigned port 1525. The connection will
consist of a pair of sockets: (136.86.5.20:1525) on the host A and (151.25.18.4:70) on the web server.
The information travelling between the hosts are delivered to appropriate process based on the destination
port number. Care is to be taken to ensure that all connections consist of a unique pair of sockets.
Remote Procedure Call (RPCs)
RPCs are another form of distributed communication. An RPC occurs when a process calls a procedure
on a remote application. Here message based communication scheme is used to provide remote service.
The important characteristics of RPCs are :
* They provide a very familiar interface for the application developer
* Way of implementing the commonplace request-reply primitive
* The format of the messages is standard, not application dependent
* They make it easier to reuse code, since the RPCs have a standard interface and are separate from
any one application-proper.
The messages exchanged in RPC communication are well structured. Each message is address to an RPC
daemon listening to a port on the remote system and each contains an identifier of the function to execute
and the parameters to pass to that function. The execution of the function takes place according to the
request. After that, in a separate message, an output is sent back to the requester. A system generally has
one network address. It can have many ports to differentiate the many network services it supports. If a
remote process needs a service, it sends a message to the proper port. Suppose a system allows the other
system to share information about its current user, then it would have a daemon supporting such a RPC
attached to a port, say 3125. Any remote system could obtain the needed information by sending an RPC
message to the concerned port, that is, 3125 on the server. The desired information would be received in
the reply message. The RPC system provides stub on the client side which hides the details that allow
communication to take place. Each separate remote procedure has a separate stub. RPC system calls the
appropriate stub when a client invokes a remote procedure. This particular stub locates the port on the
server and packages the parameters into a form which can be transmitted over a network. After that, the
stub transmits a message to the server using message passing. The counter stub on the server side receives
38
this message and invokes the procedure on the server. If required, the return value in the form of output is
passed back to the client using same procedure. In the Figure 4.5 there is illustration of Remote Procedure
Calls.
39
Remote Method Invocation implements the remote object using stubs and skeletons. When a client
invokes a remote method, the stub - a proxy for the remote object, on the client side, is called. This
particular stub creates a parcel containing the name of the method to be invoked on the server and
marshalled the parameters for the method. The stub then sends this parcel to the server where skeleton for
the remote object receives it. This skeleton unmarshalled the parameters and invokes the desired method
on the server. The skeleton, then marshal the return value into a parcel and return it to the client where the
stub on the client side unmarshals the return value and pass it to the client. In the figure 4.6 illustration of
Remote Method Invocation is given.
40
4.10 Self Assessment Questions
1. Define the following:
a. Process
b. Process State
c. Process Control Box
2. How a process is created and terminated?
3. Briefly discuss the following:
a. Shared Memory System
b. Message Passing System
c. Sockets
d. Remote Method Invocation (RMI)
e. Remote Procedure Calls (RPCs)
4. Differentiate between independent process and cooperation process.
5. Differentiate between RMI and RPCs.
6. Briefly discuss various process states.
7. escribe the actions taken by the kernel to context switch between processes.
8. Explain the major components of a process.
´ ´ ´
41
Unit - 5 : Theards
Structure of the Unit
5.0 Objective
5.1 Introduction
5.2 Threads
5.3 Processes Vs Threads
5.3.1 User-Level Threads
5.3.2 Kernel-Level Threads
5.4 Multi Threading Models
5.5 Thread Libraries
5.6 Thread Issues
5.7 Thread Scheduling
5.8 Summary
5.9 Self Assessment Questions
5.0 Objective
After reading this unit, you will be abl to understand general overview of thread, which is the fundamental
unit of CPU utilization, different thread libraries and issues related to multithreaded programming.
5.1 Introduction
Traditionally, programs are single-path execution, hence a single thread. This practice would have made
the production of today’s software production impossible as the need of speed required programs to
perform multiple tasks and events at the same time. With traditional turn-by-turn game, such as tic-tac-toe
or chess, the traditional approach works fine, however with new age multitasking programs where multiple
events need to run in parallel, the traditional approach proves useless.
5.2 Threads
A thread is a single sequence stream within in a process. Because threads have some of the properties of
processes, they are sometimes called lightweight processes. In a process, threads allow multiple executions
of streams. In many respect, threads are popular way to improve application through parallelism. The
CPU switches rapidly back and forth among the threads giving illusion that the threads are running in
parallel. Like a traditional process i.e., process with one thread, a thread can be in any of several states
(Running, Blocked, Ready or Terminated). Since thread will generally call different procedures and thus
will have a different execution history. This is why thread needs its own stack.
A thread has a program counter (PC), a register set, and a stack space. Threads are not independent of
one other like processes and as a result threads share with other their code section, data section, OS
resources also known as task, such as open files and signals with other threads.
42
Similarities
• Like processes threads share CPU and only one thread active (running) at a time.
• Like processes, threads within a processes, is execute sequentially.
• Like processes, thread can create children.
• Like processes, if one thread is blocked, another thread can run.
Differences
• Unlike processes, threads are not independent of one another.
• Unlike processes, all threads can access every address in the task .
• Unlike processes, thread is design to assist one other. Note that processes might or might not
assist one another because processes may originate from different users.
Why Threads?
Threads are cheap because
1. They only need a stack and storage for registers therefore, threads are cheap to create.
2. Threads use very little resources of an operating system in which they are working. That is, threads
do not need new address space, global data, program code or operating system resources.
3. Context switching are fast when working with threads. The reason is that we only have to save
and/or restore PC, SP and registers.
Biggest drawback of threads is that there is no protection between threads.
5.3.1 User-Level Threads
User-level threads implement in user-level libraries, rather than via systems calls, so thread switching does
not need to call operating system and to cause interrupt to the kernel. In fact, the kernel knows nothing
about user-level threads and manages them as if they were single-threaded processes.
Advantages:The most obvious advantage of this technique is that user-level threads package can be
implemented on an operating system that does not support threads. Some other advantages are :
• User-level threads do not require modification to operating systems.
• Simple Representation : Each thread is represented simply by a PC, registers, stack and a small
control block, all stored in the user process address space.
• Simple Management : This simply means that creating a thread, switching between threads and
synchronization between threads can all be done without intervention of the kernel.
• Fast and Efficient : Thread switching is not much expensive than a procedure call.
Disadvantages:
• There is a lack of coordination between threads and operating system kernel. Therefore, process
as whole gets one time slice irrespective of whether process has one thread or 1000 threads
within. It is up to each thread to relinquish control to other threads.
• User-level threads require non-blocking systems call i.e., a multithreaded kernel. Otherwise,
entire process will blocked in the kernel, even if there are runable threads left in the processes. For
example, if one thread causes a page fault, the process blocks.
5.3.2 Kernel-Level Threads
In this method, the kernel knows about and manages the threads. No runtime system is needed in this case.
Instead of thread table in each process, the kernel has a thread table that keeps track of all threads in the
system. In addition, the kernel also maintains the traditional process table to keep track of processes.
Operating system kernel provides system call to create and manage threads.
43
Advantages:
• Because kernel has full knowledge of all threads, scheduler may decide to give more time to a
process having large number of threads than process having small number of threads.
• Kernel-level threads are especially good for applications that frequently block.
Disadvantages:
• The kernel-level threads are slow and inefficient.
• Since kernel must manage and schedule threads as well as processes. It require a full thread
control block (TCB) for each thread to maintain information about threads. As a result there is
significant overhead and increase in kernel complexity.
Advantages of Threads over Multiple Processes
• Context Switching Threads are very inexpensive to create and destroy, and they are inexpen
sive to represent. For example, they require space to store, the PC, the SP, and the general-
purpose registers, but they do not require space to share memory information, information about
open files of I/O devices in use, etc. With so little context, it is much faster to switch between
threads. In other words, it is relatively easier for a context switch using threads.
• Sharing Threads allow the sharing of a lot resources that cannot be shared in process, for
example, sharing code section, data section, operating system resources like open files etc.
Disadvantages of Threads over Multiprocesses
• Blocking The major disadvantage is that if the kernel is single threaded, a system call of one
thread will block the whole process and CPU may be idle during the blocking period.
• Security Since there is, an extensive sharing among threads there is a potential problem of
security. It is quite possible that one thread over writes the stack of another thread (or damaged
shared data) although it is very unlikely since threads are meant to cooperate on a single task.
Application that benefits from Threads
A proxy server satisfying the requests for a number of computers on a LAN would be benefited by a multi-
threaded process. In general, any program that has to do more than one task at a time could benefit from
multitasking. For example, a program that reads input, process it, and outputs could have three threads,
one for each task.
Application that cannot Benefit from Threads
Any sequential process that cannot be divided into parallel task will not benefit from thread, as they would
block until the previous one completes. For example, a program that displays the time of the day would not
benefit from multiple threads.
Resources used in Thread Creation and Process Creation
44
When a new thread is created it shares its code section, data section and operating system resources like
open files with other threads. But it is allocated its own stack, register set and a program counter.
The creation of a new process differs from that of a thread mainly in the fact that all the shared resources
of a thread are needed explicitly for each process. So though two processes may be running the same
piece of code they need to have their own copy of the code in the main memory to be able to run. Two
processes also do not share other resources with each other. This makes the creation of a new process
very costly compared to that of a new thread.
Context Switch
To give each process on a multiprogrammed machine a fair share of the CPU, a hardware clock generates
interrupts periodically. This allows the operating system to schedule all processes in main memory (using
scheduling algorithm) to run on the CPU at equal intervals. Each time a clock interrupt occurs, the interrupt
handler checks how much time the current running process has used. If it has used up its entire time slice,
then the CPU scheduling algorithm (in kernel) picks a different process to run. Each switch of the CPU
from one process to another is called a context switch.
Major Steps of Context Switching
• The values of the CPU registers are saved in the process table of the process that was running just
before the clock interrupt occurred.
• The registers are loaded from the process picked by the CPU scheduler to run next.
In a multiprogrammed uniprocessor computing system, context switches occur frequently enough that all
processes appear to be running concurrently. If a process has more than one thread, the Operating System
can use the context switching technique to schedule the threads so they appear to execute in parallel. This
is the case if threads are implemented at the kernel level. Threads can also be implemented entirely at the
user level in run-time libraries. Since in this case no thread scheduling is provided by the Operating System,
it is the responsibility of the programmer to yield the CPU frequently enough in each thread so all threads
in the process can make progress.
Action of Kernel to Context Switch among Threads
The threads share a lot of resources with other peer threads belonging to the same process. So a context
switch among threads for the same process is easy. It involves switch of register set, the program counter
and the stack. It is relatively easy for the kernel to accomplished this task.
Action of Kernel to Context Switch among Processes
Context switches among processes are expensive. Before a process can be switched its process control
block (PCB) must be saved by the operating system. The PCB consists of the following information:
• The process state.
• The program counter, PC.
• The values of the different registers.
• The CPU scheduling information for the process.
• Memory management information regarding the process.
• Possible accounting information for this process.
• I/O status information of the process.
When the PCB of the currently executing process is saved, the operating system loads the PCB of the next
process that has to be run on CPU. This is a heavy task and it takes a lot of time. Threads are very useful
in modern programming whenever a process has multiple tasks to perform independently of the others.
• This is particularly true when one of the tasks may block, and it is desired to allow the other tasks
to proceed without blocking.
45
• For example in a word processor, a background thread may check spelling and grammar while a
foreground thread processes user input (keystrokes), while yet a third thread loads images from
the hard drive, and a fourth does periodic automatic backups of the file being edited.
• Another example is a web server - Multiple threads allow for multiple requests to be satisfied
simultaneously, without having to service requests sequentially or to fork off separate processes
for every incoming request. (The latter is how this sort of thing was done before the concept of
threads was developed. A daemon would listen at a port, fork off a child for every incoming
request to be processed, and then go back to listening to the port.)
Benefits
There are four major categories of benefits to multi-threading:
1. Responsiveness - One thread may provide rapid response while other threads are blocked or
slowed down doing intensive calculations.
2. Resource sharing - By default; threads share common code, data, and other resources, which
allows multiple tasks to be performed simultaneously in a single address space.
3. Economy - Creating and managing threads (and context switches between them) is much faster
than performing the same tasks for processes.
4. Scalability, i.e. utilization of multiprocessor architectures - A single threaded process can only run
on one CPU, no matter how many may be available, whereas the execution of a multi-threaded
application may be split amongst available processors. (Note that single threaded processes can
still benefit from multi-processor architectures when there are multiple processes contending for
the CPU, i.e. when the load average is above some certain threshold.)
Multicore Programming
• A recent trend in computer architecture is to produce chips with multiple cores, or CPUs on a
single chip.
• A multi-threaded application running on a traditional single-core chip would have to interleave the
threads, as shown in Figure (5.1). On a multi-core chip, however, the threads could be spread
across the available cores, allowing true parallel processing, as shown in Figure (5.2).
46
• For operating systems, multi-core chips require new scheduling algorithms to make better use of
the multiple cores available.
• For application programmers, there are five areas where multi-core chips present new challenges:
1. Dividing activities - Examining applications to find activities that can be performed concurrently.
2. Balance - Finding tasks to run concurrently that provide equal value, i.e. don’t waste a thread on
trivial tasks.
3. Data splitting - To prevent the threads from interfering with one another.
4. Data dependency - If one task is dependent upon the results of another, then the tasks need to be
synchronized to assure access in the proper order.
5. Testing and debugging - Inherently more difficult in parallel processing situations, as the race
conditions become much more complex and difficult to identify.
48
Figure 5.6 : Two Level Model
49
o The creation of new threads requires objects that implement the runnable Interface, which means
they contain a method “public void run( )” . Any descendant of the Thread class will naturally
contain such a method. (In practice the run( ) method must be overridden / provided for the thread
to have any practical functionality.)
o Creating a Thread Object does not start the thread running - To do that the program must call the
Thread’s “start( )” method. Start( ) allocates and initializes memory for the Thread, and then calls
the run( ) method. ( Programmers do not call run( ) directly.)
Because Java does not support global variables, threads must pass a reference to a shared object in order
to share data, in this example the “Sum” Object.
Note that the JVM runs on top of a native OS, and that the JVM specification does not specify what
model to use for mapping Java threads to kernel threads. This decision is JVM implementation dependant,
and may be one-to-one, many-to-many, or any of the other models discussed previously.
51
• UNIX allows individual threads to indicate which signals they are accepting and which they are
ignoring. However the signal can only be delivered to one thread, which is generally the first thread
that is accepting that particular signal.
• Windows does not support signals, but they can be emulated using Asynchronous Procedure Calls
( APCs ). APCs are delivered to specific threads, not processes.
5.6.4 Thread Pools
• Creating new threads every time one is needed and then deleting it when it is done can be inefficient,
and can also lead to a very large (unlimited) number of threads being created.
• An alternative solution is to create a number of threads when the process first starts, and put those
threads into a thread pool.
§ Threads are allocated from the pool as needed, and returned to the pool when no longer needed.
§ When no threads are available in the pool, the process may have to wait until one becomes available.
• The ( maximum ) number of threads available in a thread pool may be determined by adjustable
parameters, possibly dynamically in response to changing system loads.
• Win32 provides thread pools through the “PoolFunction” function. Java also provides support for
thread pools.
5.6.5 Thread-Specific Data
• Most data is shared among threads, and this is one of the major benefits of using threads in the first
place.
• However sometimes threads need thread-specific data also.
• Most major thread libraries (pThreads, Win32, Java) provide support for thread-specific data.
5.6.6 Scheduler Activations
• Many implementations of threads provide a virtual processor as an interface between the user thread
and the kernel thread, particularly for the many-to-many or two-tier models.
• This virtual processor is known as a “Lightweight Process”, LWP.
§ There is a one-to-one correspondence between LWPs and kernel threads.
§ The number of kernel threads available, (and hence the number of LWPs) may change dynamically.
§ The application (user level thread library) maps user threads onto available LWPs.
§ kernel threads are scheduled onto the real processor(s) by the OS.
§ The kernel communicates to the user-level thread library when certain events occur (such as a
thread about to block) via an upcall, which is handled in the thread library by an upcall handler.
The upcall also provides a new LWP for the upcall handler to run on, which it can then use to
reschedule the user thread that is about to become blocked. The OS will also issue upcalls when
a thread becomes unblocked, so the thread library can make appropriate adjustments.
• If the kernel thread blocks, then the LWP blocks, which blocks the user thread.
• Ideally there should be at least as many LWPs available as there could be concurrently blocked kernel
threads. Otherwise if all LWPs are blocked, then user threads will have to wait for one to become
available.
52
Figure 5.8 : Light Weight Process (LWP)
Policy The scheduling policy of a thread defines how the scheduler treats the thread after it gains
control of the CPU.
Priority The scheduling priority of a thread defines the relative importance of the work being done by
each thread.
The scheduling parameters can be set before the thread’s creation or during the thread’s execution. In
general, controlling the scheduling parameters of threads is important only for threads that are CPU-
53
intensive. Thus, the threads library provides default values that are sufficient for most cases.
Contention Scope and Concurrency Level
The contention scope of a user thread defines how it is mapped to a kernel thread. The threads library
defines the following contention scopes:
Process contention scope, sometimes called local
contention scope. Specifies that the thread will be
scheduled against all other local contention scope threads
PTHREAD_SCOPE_PROCESS in the process. A process-contention-scope user thread
is a user thread that shares a kernel thread with other
process-contention-scope user threads in the process. All
user threads in an M:1 thread model have process
contention scope.
PTHREAD_SCOPE_SYSTEM System contention scope, sometimes called global
contention scope. Specifies that the thread will be
scheduled against all other threads in the system and is
directly mapped to one kernel thread. All user threads in
a 1:1 thread model have system contention scope.
In an M:N thread model, user threads can have either system or process contention scope. Therefore, an
M:N thread model is often referred as a mixed-scope model.
The concurrency level is a property of M:N threads libraries. It defines the number of virtual processors
used to run the process-contention scope user threads. This number cannot exceed the number of pro-
cess-contention-scope user threads and is usually dynamically set by the threads library. The system also
sets a limit to the number of available kernel threads.
Setting the Contention Scope
The contention scope can only be set before a thread is created by setting the contention-scope attribute
of a thread attributes object. The pthread_attr_setscope subroutine sets the value of the attribute; the
pthread_attr_getscope returns it.
The contention scope is only meaningful in a mixed-scope M:N library implementation. A
TestImplementation routine could be written as follows:
int TestImplementation()
{
pthread_attr_t a;
int result;
pthread_attr_init(&a);
switch (pthread_attr_setscope(&a, PTHREAD_SCOPE_PROCESS))
{
case 0: result = LIB_MN; break;
case ENOTSUP: result = LIB_11; break;
case ENOSYS: result = NO_PRIO_OPTION; break;
default: result = ERROR; break;
}
pthread_attr_destroy(&a);
return result;
}
54
Impacts of Contention Scope on Scheduling
The contention scope of a thread influences its scheduling. Each contention-scope thread is bound to one
kernel thread. Thus, changing the scheduling policy and priority of a global user thread results in changing
the scheduling policy and priority of the underlying kernel thread.
In AIX®, only kernel threads with root authority can use a fixed-priority scheduling policy (FIFO or
round-robin). The following code will always return the EPERM error code if the calling thread has
system contention scope but does not have root authority. This code would not fail, if the calling thread had
process contention scope.
schedparam.sched_priority = 3;
pthread_setschedparam(pthread_self(), SCHED_FIFO, schedparam);
Using the inheritsched Attribute
The inheritsched attribute of the thread attributes object specifies how the thread’s scheduling attributes
will be defined. The following values are valid:
PTHREAD_INHERIT_SCHED Specifies that the new thread will get the scheduling
attributes (schedpolicy and schedparam attributes) of
its creating thread. Scheduling attributes defined in the
attributes object are ignored.
PTHREAD_EXPLICIT_SCHED Specifies that the new thread will get the scheduling
attributes defined in this attributes object.
The default value of the inheritsched attribute is PTHREAD_INHERIT_SCHED. The attribute is set by
calling the pthread_attr_setinheritsched subroutine. The current value of the attribute is returned by
calling the pthread_attr_getinheritsched subroutine.
To set the scheduling attributes of a thread in the thread attributes object, the inheritsched attribute must
first be set to PTHREAD_EXPLICIT_SCHED. Otherwise, the attributes-object scheduling attributes
are ignored.
Scheduling Policy and Priority
The threads library provides the following scheduling policies:
SCHED_FIFO First-in first-out (FIFO) scheduling. Each thread has a
fixed priority; when multiple threads have the same priority
level, they run to completion in FIFO order.
SCHED_RR Round-robin (RR) scheduling. Each thread has a fixed
priority; when multiple threads have the same priority level,
they run for a fixed time slice in FIFO order.
SCHED_OTHER Default AIX® scheduling. Each thread has an initial priority
that is dynamically modified by the scheduler, according
to the thread’s activity; thread execution is time-sliced.
On other systems, this scheduling policy may be different.
The threads library handles the priority through a sched_param structure, defined in the sys/
sched.h header file. This structure contains the following fields:
sched_priority Specifies the priority.
sched_policy This field is ignored by the threads library. Do not use.
Setting the Scheduling Attributes at Execution Time
The pthread_getschedparam subroutine returns the schedpolicy and schedparam attributes of a thread.
These attributes can be set by calling the pthread_setschedparam subroutine. If the target thread is
55
currently running on a processor, the new scheduling policy and priority will be implemented the next time
the thread is scheduled. If the target thread is not running, it can be scheduled immediately at the end of the
subroutine call.
For example, consider a thread T that is currently running with round-robin policy at the moment the
schedpolicy attribute of T is changed to FIFO. T will run until the end of its time slice, at which time its
scheduling attributes are then re-evaluated. If no threads have higher priority, T will be rescheduled, even
before other threads having the same priority. Consider a second example where a low-priority thread
is not running. If this thread’s priority is raised by another thread calling the pthread_setschedparam
subroutine, the target thread will be scheduled immediately if it is the highest priority runnable thread.
Note:Both subroutines use a policy parameter and a sched_param structure. Although this structure
contains a sched_policy field, programs should not use it. The subroutines use the policy parameter to
pass the scheduling policy, and the subroutines then ignore the sched_policy field.
Scheduling-Policy Considerations
Applications should use the default scheduling policy, unless a specific application requires the use of a
fixed-priority scheduling policy. Consider the following points about using the nondefault policies:
• Using the round-robin policy ensures that all threads having the same priority level will be scheduled
equally, regardless of their activity. This can be useful in programs where threads must read sensors or
write actuators.
• Using the FIFO policy should be done with great care. A thread running with FIFO policy runs to
completion, unless it is blocked by some calls, such as performing input and output operations. A high-priority
FIFO thread may not be preempted and can affect the global performance of the system. For example,
threads doing intensive calculations, such as inverting a large matrix, should never run with FIFO policy.
The setting of scheduling policy and priority is also influenced by the contention scope of threads. Using the
FIFO or the round-robin policy may not always be allowed.
sched_yield Subroutine
The sched_yield subroutine is the equivalent for threads of the yield subroutine. The sched_yield subroutine
forces the calling thread to relinquish the use of its processor and gives other threads an opportunity to be
scheduled. The next scheduled thread may belong to the same process as the calling thread or to another
process. Do not use the yield subroutine in a multi-threaded program.
The interface pthread_yield subroutine is not available in Single UNIX Specification, Version 2.
5.8 Summary
Threads are an inherit part of software products as a fundamental unit of CPU utilization as a basic building block
of multithreaded systems. The use of threads has evolved over the years from each program consisting of
a single thread as the path of execution of it. The notion of multithreading is the expansion of the original
application thread to multiple threads running in parallel handling multiple events and performing multiple
tasks concurrently. Today’s modern operating systems foster the ability of multiple threads controlled by a
single process all within the same address space.
Multithreading brings a higher level of responsiveness to the user as a thread can run while other threads
are on hold awaiting instructions. As all threads are contained within a parent process, they share the
resourses and memory allocated to the process working within the same address space making it less
costly to generate multiple threads vs. Processes. These benefits increase even further when executed on
a multiprocessor architechture as multiple threads can run in parallel across multiple processors as only
one process may execute on one processor.
Threads divide into two types: user-level threads – visible to developers but unknown to the kernel, and
kernel-level threads – managed by the operating system’s kernel.
56
Three models identify the relationships between user-level and kernel-level threads: one-to-one, many-
to-one, and many-to-many.
This chapter explores various notions related to systems with multithreading capability, including POSIX,
Java,Windows 2000,Linux thread libraries. Challenges associated to multithreaded program development
explored in this report are those of thread cancellation, singnal handling, thread-specific data, and symantics
of necessary system calls.
5.9 Self Assessment Questions
1. List two reasons why a context switch between threads may be faster than a context switch
between processes.
2. List three resources that are typically shared by all of the threads of a process.
3. What Components of a program state are shared across threads in a multithreaded process?
4. List differences between user level threads and kernel level threads.
5. What resources are used when a thread is created?
6. Can a multithreaded system using many user-level threadsachieve better performance on a multi
processor system than on a single processor system?
57
Unit - 6 : Process Scheduling : Basic Concept
Structure of the Unit
6.0 Objective
6.1 Introduction
6.2 Type of Schedulers
6.3 CPU-I/O Burst Cycle
6.4 Scheduling Criteria
6.5 Scheduling Algorithms
6.5.1 First-Come First-Served(FCFS) Scheduling
6.5.2 Shortest Job First Scheduling
6.6 Operating system Examples
6.6.1 Linux Scheduling
6.6.2 Windows Scheduling
6.7 Summary
6.8 Self Assessment Questions
6.0 Objective
This chapter provides a general overview of CPU Scheduling, which is the basis for multiprogrammed
operating system and describes various concepts related to scheduling and different CPU scheduling
algorithms.
6.1 Introduction
Scheduling is the method by which threads, processes or data flows are given access to system resources
(e.g. processor time, communications bandwidth). This is usually done to load balance a system effectively
or achieve a target quality of service.
The need for a scheduling algorithm arises from the requirement for most modern systems to perform
multitasking (execute more than one process at a time) and multiplexing (transmit multiple flows
simultaneously).
The scheduler is concerned mainly with:
* Throughput - number of processes that complete their execution per time unit.
* Latency, specifically:
o Turnaround - total time between submission of a process and its completion.
o Response time - amount of time it takes from when a request was submitted until the first
response is produced.
* Fairness / Waiting Time - Equal CPU time to each process (or more generally appropriate times
according to each process’ priority).
In practice, these goals often conflict (e.g. throughput versus latency), thus a scheduler will implement a
suitable compromise. Preference is given to any one of the above mentioned concerns depending upon the
user’s needs and objectives.
58
6.2 Types of schedulers
Operating systems may feature up to 3 distinct types of schedulers, a long-term scheduler (high-level
scheduler), a mid-term or medium-term scheduler and a short-term scheduler. The names suggest the
relative frequency with which these functions are performed. The Scheduler is an operating system module
that selects the next jobs to be admitted into the system and the next process to run.
Long-term scheduling
The long-term scheduler, decides which jobs or processes are to be admitted to the ready queue (in the
Main Memory); that is, when an attempt is made to execute a program, its admission to the set of
currently executing processes is either authorized or delayed by the long-term scheduler. Thus, this
scheduler dictates what processes are to run on a system, and the degree of concurrency to be supported
at any one time - i.e., whether a high or low amount of processes are to be executed concurrently, and how
the split between IO intensive and CPU intensive processes is to be handled.
Medium-term scheduling
The medium-term scheduler temporarily removes processes from main memory and places them on
secondary memory (such as a disk drive) or vice versa. This is commonly referred to as “swapping out” or
“swapping in” (also incorrectly as “paging out” or “paging in”). The medium-term scheduler may decide to
swap out a process which has not been active for some time, or a process which has a low priority, or a
process which is page faulting frequently, or a process which is taking up a large amount of memory in
order to free up main memory for other processes, swapping the process back in later when more memory
is available, or when the process has been unblocked and is no longer waiting for a resource.
In many systems today, the medium-term scheduler may actually perform the role of the long-term
scheduler, by treating binaries as “swapped out processes” upon their execution. In this way, when a
segment of the binary is required it can be swapped in on demand.
Short-term scheduling
The short-term scheduler (also known as the CPU scheduler) decides which of the ready, in-memory
processes are to be executed (allocated a CPU) next following a clock interrupt, an IO interrupt, an
operating system call or another form of signal. Thus the short-term scheduler makes scheduling decisions
much more frequently than the long-term or mid-term schedulers - a scheduling decision will at a minimum
have to be made after every time slice, and these are very short. This scheduler can be preemptive,
implying that it is capable of forcibly removing processes from a CPU when it decides to allocate that CPU
to another process, or non-preemptive (also known as “voluntary” or “co-operative”), in which case the
scheduler is unable to “force” processes off the CPU.In most cases short-term scheduler is written in
assembler because it is a critical part of the operating system.
6.3 CPU-I/O Burst Cycle
A process will run for a while (the CPU burst), perform some I/O (the I/O burst), then run for a while
more (the next CPU burst). How long between I/O operations, depends on the process.
I/O Bound processes are the processes that perform lots of I/O operations. Each I/O operation is
followed by a short CPU burst to process the I/O, then more I/O happens.
CPU bound processes are the processes that perform lots of computation and do little I/O. They tend to
have a few long CPU bursts.
59
Figure 6.1 : Process execution consists of a cycle of CPU execution and I/O wait
* The success of CPU scheduling depends on an observed property of processes:
o Process execution consists of a cycle of CPU execution and I/O wait. Processes alternate
between these two states.
o Process execution begins with a CPU burst. That is followed by an I/O burst, which is
followed by another CPU burst, then another I/O burst, and so on.
* Eventually, the final CPU burst ends with a system request to terminate execution (see the Fig).
The durations of CPU bursts have been measured extensively. They tend to have a frequency curve as in
the figure 6.2.
60
* An I/O-bound program typically has many short CPU bursts.
* A CPU-bound program might have a few long CPU bursts.
This distribution can be important in the selection of an appropriate CPU-scheduling algorithm.
Nearly all processes alternate bursts of computing with (disk) I/O requests, as shown in Figure 6.3.
62
A scheduling algorithm that maximizes throughput may not necessarily minimize turnaround time. Given a
mix of short jobs and long jobs, a scheduler that always ran short jobs and never ran long jobs might
achieve an excellent throughput (many short jobs per hour) but at the expense of a terrible turnaround time
for the long jobs. If short jobs kept arriving at a steady rate, the long jobs might never run, making the
mean turnaround time infinite while achieving a high throughput.
6.5 Scheduling Algorithms
CPUscheduling deals with the problemof deciding which of the processes in the ready queue is to be
allocated the CPU. There are many different algorithms for scheduling CPU. Following are some of them:
6.5.1 First-Come,First-Served(FCFS) Scheduling
By far the simplest CPU-scheduling algorithm is the first-come, first-served (FCFS) scheduling algorithm.
With this algorithm, processes are assigned the CPU in the order they request it. Basically, there is a single
queue of ready processes. Relative importance of jobs measured only by arrival time (poor choice). The
implementation of the FCFS policy is easily managed with a FIFO queue. When a process enters the
ready queue, its PCB is linked onto the tail of the queue.
The average waiting time under the FCFS policy, however, is often quite long. Consider the following set
of processes that arrive at time 0, with the length of the CPU burst given in milliseconds:
24 0 24
3 24 27
3 27 30
Av erage - 17 27
Table 6.1
If the processes arrive in the order P1,P2,P3 and are served in FCFS order, we get the result as
If the processes arrive in the order P2, P3, P1, the results will be
64
Burst Wait ing Turnaround
Process Tim e(m s) Tim e(m s) Tim e(ms)
6 3 9
8 16 24
7 9 16
3 0 3
Av erage - 7 13
Table 6.2
Using SJF scheduling, we would schedule these processes according to the following Figure :
By comparison, if we were using the FCFS scheduling scheme, the average waiting time would be 10.25
milliseconds.
The SJF scheduling algorithm gives the minimum average waiting time for a given set of processes.
* Moving a short process before a long one decreases the waiting time of the short process more
than it increases the waiting time of the long process.
* Consequently, the average waiting time decreases.
The real difficulty with the SJF algorithm is knowing the length of the next CPU request. For long-term
(job) scheduling in a batch system, we can use as the length the process time limit that a user specifies when
he submits the job.
We may not know the length of the next CPU burst, but we may be able to predict its value. We expect
that the next CPU burst will be similar in length to the previous ones.
Also, long running jobs may starve for the CPU when there is a steady supply of short jobs.
Example of Non-Preemptive SJF
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
Table 6.3
65
Waiting time for P1 = 0ms
Waiting time for P2 = ( 8 – 2 )= 6ms
Waiting time for P3 = ( 7 - 4 )= 3ms
Waiting time for P4 = ( 12 - 5 )= 7ms
So average waiting time of above scenario is (0 + 6 + 3 + 7) / 4 = 4
For FCFS scheduling scheme the Average waiting time is (0 + 5 + 7 + 7)/4 = 4.75
Example of Preemptive SJF
Process Arrival Time Burst Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
Table 6.4
Figure 6.4 : Round-robin scheduling. (a) The list of runnable processes. (b) The list of runnable processes
after B uses up its quantum.
The average waiting time under the RR policy is often long. Consider the following set of processes that
arrive at time 0, with the length of the CPU burst given in milliseconds: (a time quantum of 4 milliseconds)
B u rs t W aiting T urn arou nd
24 6 30
3 4 7
3 7 10
A v era g e - 5.6 6 15 .6 6
Table 6.5
Using round-robin scheduling, we would schedule these processes according to the following figure :
P1 P2 P3 P1 P1 P1 P1
0 4 7 10 16 22 26 31
In the RR scheduling algorithm, no process is allocated the CPU for more than 1 time quantum in a row
(unless it is the only runnable process). If a process’s CPU burst exceeds 1 time quantum, that process is
preempted and is put back in the ready queue. The RR scheduling algorithm is thus pre-emptive.
o If there are n processes in the ready queue and the time quantum is q, then each process gets
1/n of the CPU time in chunks of at most q time units.
o Each process must wait no longer than ( n – 1 ) * q time units until its next time quantum.
67
o For example, with five processes and a time quantum of 20 milliseconds, each process will get up
to 20 milliseconds every 100 milliseconds.
The performance of the RR algorithm depends heavily on the size of the time quantum.
o If the time quantum is extremely large, the RR policy is the same as the FCFS policy.
o If the time quantum is extremely small (say, 1 millisecond), the RR approach is called processor
sharing and (in theory) creates the appearance that each of n processes has its own processor
running at 1 / n speed of the real processor.
We need also to consider the effect of context switching on the performance of RR scheduling. Switching
from one process to another requires a certain amount of time for doing the administration’saving and
loading registers and memory maps, updating various tables and lists, flushing and reloading the memory
cache, etc.
o Let us assume that we have only one process of 10 time units.
o If the quantum is 12 time units, the process finishes in less than 1 time quantum, with no overhead.
o If the quantum is 6 time units, however, the process requires 2 quanta, resulting in a context switch.
o If the time quantum is 1 time unit, then nine context switches will occur, slowing the execution of the
process accordingly
* Thus, we want the time quantum to be large with respect to the context-switch time.
o If the context-switch time is approximately 10 percent of the time quantum, then about 10 percent
of the CPU time will be spent in context switching.
o In practice, most modern systems have time quanta ranging from 10 to 100 milliseconds.
o The time required for a context switch is typically less than 10 microseconds; thus, the context-
switch time is a small fraction of the time quantum.
* Setting the quantum too short causes too many process switches and lowers the CPU
efficiency, but setting it too long may cause poor response to short interactive requests.
* Poor average waiting time when job lengths are identical; Imagine 10 jobs each requiring
10 time slices, all complete after about 100 time slices, even FCFS is better!
* In general, the average turnaround time can be improved if most processes finish their next
CPU burst in a single time quantum. If context-switch time is added in, the average turn
around time increases for a smaller time quantum, since more context switches are re
quired.
Although the time quantum should be large compared with the context-switch time, it should not be too
large. If the time quantum is too large, RR scheduling degenerates to FCFS policy.
Table 6.6
68
6.6 Operating System Examples
6.6.1 Linux Scheduling
* The Linux scheduler is a pre-emptive, priority-based algorithm with two separate priority ranges:
o a real-time range from 0 to 99
o and a nice value ranging from 100 to 140.
* These two ranges map into a global priority scheme whereby numerically lower values indicate
higher priorities.
* Linux assigns higher-priority tasks longer time quanta and lower-priority tasks shorter time quanta.
A runnable task is considered eligible for execution on the CPU as long as it has time remaining in
its time slice. When a task has exhausted its time slice, it is considered expired and is not eligible
for execution again until all other tasks have also exhausted their time quanta.
* The kernel maintains a list of all runnable tasks in a runqueue data structure. Because of its support
for SMP, each processor maintains its own runqueue and schedules itself independently.
* Each runqueue contains two priority arrays -active and expired.
o The active array contains all tasks with time remaining in their time slices, and the expired
array contains all expired tasks.
o The scheduler chooses the task with the highest priority from the active array for execution
on the CPU. On multiprocessor machines, this means that each processor is scheduling
the highest-priority task from its own runqueue structure.
o When all tasks have exhausted their time slices (that is, the active array is empty), the two
priority arrays are exchanged; the expired array becomes the active array, and vice versa.
6.6.2 Windows 2000 Scheduling
* Windows 2000 is designed as responsive to the need of a user
* A preemptive scheduler is implemented
* Scheduler has flexible priority levels that include RR scheduling within each level
* Some levels also have dynamic priority variation based on current thread activity
Process and Thread Priorities
* Priorities are organized in two bands or classes: real time and variable
* Each band has 16 levels
* Threads requiring immediate attention are in real time class
* Overall W2K uses a priority driven preemptive scheduler so real time threads have
preference over other threads
Priorities in two classes are handled differently
* All real time class threads have fixed priority that never changes (RR queue is used)
* In the variable class a threads priority begins with some initial value which may then
change up/down during threads life time
* There is a FIFO queue at each priority level, but a process may migrate to one of the other queues
within the variable priority class
* No process can be promoted to the upper class
* Initial priority of a thread in variable class is determined by:
69
Process Based Priority: Can have any value between 0 - 15
Thread Based Priority: This could be equal or within two levels above or below that of the process. For
example if a process has a base priority 4, one of its thread has -1, then initial priority of that thread is 3
Multi-processor Scheduling
For a single processor
* Highest priority thread is always active unless it is blocked for some reason
* If more that one thread has the same priority, processor is shared based on RR
For Multi-processor
* A system with N processor, (N - 1) highest priority threads are always active, running exclusively
on (N - 1) processors.
* The remaining lower priority threads run on the single remaining processor
Very early MS-DOS and Microsoft Windows systems were non-multitasking, and as such did not feature
a scheduler. Windows 3.1x used a non-preemptive scheduler, meaning that it did not interrupt programs.
It relied on the program to end or tell the OS that it didn’t need the processor so that it could move on to
another process. This is usually called cooperative multitasking. Windows 95 introduced a rudimentary
preemptive scheduler; however, for legacy support opted to let 16 bit applications run without preemption.
Windows NT-based operating systems use a multilevel feedback queue. 32 priority levels are defined, 0
through to 31, with priorities 0 through 15 being “normal” priorities and priorities 16 through 31 being soft
real-time priorities, requiring privileges to assign. 0 is reserved for the Operating System. Users can select
5 of these priorities to assign to a running application from the Task Manager application, or through thread
management APIs. The kernel may change the priority level of a thread depending on its I/O and CPU
usage and whether it is interactive (i.e. accepts and responds to input from humans), raising the priority
of interactive and I/O bounded processes and lowering that of CPU bound processes, to increase the
responsiveness of interactive applications. The scheduler was modified in Windows Vista to use the cycle
counter register of modern processors to keep track of exactly how many CPU cycles a thread has
executed, rather than just using an interval-timer interrupt routine. Vista also uses a priority scheduler for
the I/O queue so that disk defragmenters and other such programs don’t interfere with foreground
operations.
6.7 Summary
What is CPU scheduling? Determining which processes run when there are multiple runnable processes.
Why is it important? Because it can can have a big effect on resource utilization and the overall
performance of the system.
CPU/IO burst cycle. A process will run for a while (the CPU burst), perform some IO (the IO burst), then
run for a while more (the next CPU burst). How long between IO operations? Depends on the process.
* IO Bound processes: processes that perform lots of IO operations. Each IO operation is followed
by a short CPU burst to process the IO, then more IO happens.
* CPU bound processes: processes that perform lots of computation and do little IO. Tend to have
a few long CPU bursts.
Preemptive vs. Non-preemptive SJF scheduler. Preemptive scheduler reruns scheduling decision when
process becomes ready. If the new process has priority over running process, the CPU preempts the
running process and executes the new process. Non-preemptive scheduler only does scheduling decision
when running process voluntarily gives up CPU. In effect, it allows every running process to finish its CPU
burst.
70
Long term scheduler is given a set of processes and decides which ones should start to run. Once they start
running, they may suspend because of IO or because of preemption. Short term scheduler decides which
of the available jobs that long term scheduler has decided are runnable to actually run.
Basic assumptions behind most scheduling algorithms:
* There is a pool of runnable processes contending for the CPU.
* The processes are independent and compete for resources.
* The job of the scheduler is to distribute the scarce resource of the CPU to the different processes
“fairly” (according to some definition of fairness) and in a way that optimizes some performance criteria.
First-Come, First-Served. One ready queue, OS runs the process at head of queue, new processes come
in at the end of the queue. A process does not give up CPU until it either terminates or performs IO.
Shortest-Job-First (SJF) can eliminate some of the variance in Waiting and Turnaround time. In fact, it is
optimal with respect to average waiting time. Big problem: how does scheduler figure out how long will it
take the process to run?
Implementing round-robin requires timer interrupts. When schedule a process, set the timer to go off after
the time quantum amount of time expires. If process does IO before timer goes off, no problem - just run
next process. But if process expires its quantum, do a context switch. Save the state of the running process
and run the next process.
6.8 Self Assessment Questions
1. Diffrentiate between Turnaround time and response time.
Given following information.
Process no. Arrival time CPU Burst
1 0 10
2 1 2
3 2 3
4 3 1
5 4 5
a. Compute waiting & turnaround time for FCFS and SJF scheduling algorithms.
b. Which of the schedules in part (a) results in the minimal average waiting time (over all processes).
3. Rank the following scheduling algorithms on a scale of 1 to 4 (1 being the highest) in terms of the
extent to which they facilitate low average waiting time:
n First-Come-First-Served
n Shortest Job First (Non-Preemptive)
n Priority (Preemptive)
n Round-Robin
4. Explain the differences in the degree to which the following scheduling algorithms discriminate in
favour of short processes:
n First-Come-First-Serve
n Round-Robin
5. Explain various scheduling criterias.Why we need scheduling
Algorithms.Explain in brief all the scheduling algorithms.
6. What are CPU Bound And I/O bound Jobs.
7. What do you understand by CPU-I/O burst?
8. Differentiate between preemptive and nonpreemptive scheduling
71
Unit - 7 : Process Synchronization
72
produce (item) // create a new item
enter_item (item) // place item in buffer
Consumer process-
while (true) // loop forever
remove_item (item) // remove an item from the buffer
Here, the producer process must not be allowed to place an item in the buffer unless an empty slot in the
buffer exists. Conversely, the consumer must not be allowed to remove an item from the buffer unless an
item exists. This example illustrates the need for synchronization between processes. This means that a
certain sequence of events must not be allowed to happen. Synchronization is different from mutual
exclusion, in that synchronization would not prevent the producer and consumer from accessing the buffer
at the same time.
7.2 Need of Synchronization
In computing, a process is an instance of a computer program that is being executed. It contains the
program code and its current activity. Depending on the operating system (OS), a process may be made
up of multiple threads of execution that execute instructions concurrently.
7.2.1 Thread and process synchronization
Thread synchronization or serialization, strictly defined, is the application of particular mechanisms to
ensure that two concurrently-executing threads or processes do not execute specific portions of a
program at the same time. If one thread has begun to execute a serialized portion of the program, any other
thread trying to execute this portion must wait until the first thread finishes. Synchronization is used to
control access to state both in small-scale multiprocessing systems — in multithreaded environments and
multiprocessor computers — and in distributed computers consisting of thousands of units — in banking
and database systems, in web servers, and so on.
A thread of execution is the smallest unit of processing that can be scheduled by an operating system.
The implementation of threads and processes differs from one operating system to another, but in most
cases, a thread is contained inside a process. Multiple threads can exist within the same process and share
resources such as memory, while different processes do not share these resources.
73
Many modern operating systems directly support both time-sliced and multiprocessor threading with a
process scheduler. The kernel of an operating system allows programmers to manipulate threads via the
system call interface. Some implementations are called a kernel thread, whereas a lightweight process
(LWP) is a specific type of kernel thread that shares the same state and information.
7.2.2 Data Synchronization
Data synchronization is the process of establishing consistency among data from a source to a target
data storage and vice versa and the continuous harmonization of the data over time.
7.2.3 File-Based Solutions
There are tools available for file synchronization, version control (CVS, Subversion, etc.), distributed file
systems (Coda, etc.), and mirroring, in that these entire attempt to keep sets of files synchronized.
However, only version control and file synchronization tools can deal with modifications to more than one
copy of the files.
File synchronization is commonly used for home backups on external hard drives or updating for transport
on USB flash drives. The automatic process prevents copying already identical files and thus can save
considerable time from a manual copy, also being faster and less error prone.
Version control tools are intended to deal with situations where more than one person wants to
simultaneously modify the same file, while file synchronizers are optimized for situations where only one
copy of the file will be edited at a time. For this reason, although version control tools can be used for file
synchronization, dedicated programs require less overhead.
Distributed file systems may also be seen as ensuring multiple versions of a file are synchronized. This
normally requires that the devices storing the files are always connected, but some distributed file systems
like Coda allow disconnected operation followed by reconciliation. The merging facilities of a distributed
file system are typically more limited than those of a version control system because most file systems do
not keep a version graph.
Mirroring: A mirror is an exact copy of a data set. On the Internet, a mirror site is an exact copy of another
Internet site. Mirror sites are most commonly used to provide multiple sources of the same information,
and are of particular value as a way of providing reliable access to large downloads.
Synchronization can also be useful in encryption for synchronizing Public Key Servers.
7.3 Race Condition
When two processes sharing a common variable try to update it simultaneously, one cannot predict the
output of it, this is the race condition.A thread while updating the variable can be preempted by another
thread and update it differently This is why synchronization mechanisms are used.
When different computational results (e.g., output, values of variables) occur depending. On the particular
timing and resulting order of execution of statements across separate
Threads or processes
Example:
X=5
Process 1: X=X+1
Process 2: X=X+2
Machine code is
LOAD EAX,MEMORY_X
ADD EAX , 1
MOV MEMORY_X,EAX
74
While process1 is executing second line it could be preempted ..And process2 takes turn so ,
MEMORY_X contains value 7. Now Process1 comes back and starts the remaining lines of code .The
memory then contains value 6. Which is the final answer But we should have got a value of 8 instead.
7.3.1 Race Condition Properties
There are three properties that are necessary for a race condition to exist:
1. Concurrency Property. There must be at least two control flows executing concurrently.
2. Shared Object Property. A shared race object must be accessed by both of the concurrent flows.
3. Change State Property. At least one of the control flows must alter the state of the race object.
Solution strategy
1. Need to ensure that only one process or thread accesses a variable or I/O device until it has
completed its required sequence of operations.
2. In general, a thread needs to perform some sequence of operations on I/O device or data
Structure to leave it in a consistent state, before the next thread can access the I/O Device or data
structure.
7.4 Critical-Section Problem
The producer-consumer problem is a specific example of a more general situation known as the critical
section problem. The general idea is that in a number of cooperating processes, each has a critical section
of code, with the following conditions and terminologies:
o Only one process in the group can be allowed to execute in their critical section at any one time. If
one process is already executing their critical section and another process wishes to do so, then
the second process must be made to wait until the first process has completed their critical section
work.
o The code preceding the critical section, and which controls access to the critical section, is termed
the entry section. It acts like a carefully controlled locking door.
o The code following the critical section is termed the exit section. It generally releases the lock on
someone else’s door, or at least lets the world know that they are no longer in their critical section.
The rest of the code not included in either the critical section or the entry or exit sections is termed the
remainder section.
77
Repeat
Waiting[i]=true;
Key=true;
While=waiting[i]and key do key:=Test-and-Set(lock);
Waiting[i]=false;
Critical section
J:=i+1 mod n;
While (j=i) and(not waiting[j])do j:=j+1 mod n;
if j=i then lock:=false
else waiting[j]:=false;
reminder section
until false;
Figure 7.7 : Bounded-waiting mutual exclusion with Test-and-Set.
7.6 Introduction to Semaphore & Monitor
Sometimes a process may need to wait for some other process to finish before it can continue. In this
instance, the two processes need to be synchronized together. There are a number of ways in which this
can be done. A common method in operating systems is to use a variable called a semaphore that only
one process can own at a time. There are two calls associated with a semaphore, one to lock it and one to
unlock it. When a process attempts to lock a semaphore, it will be successful if the semaphore is free. If the
semaphore is already locked, the process requesting the lock will be blocked and remain blocked till the
process that has the semaphore unlocks it. When that happens, the process that was blocked will be
unblocked and the semaphore can then be locked by it.
System semaphores are used by the operating system to control system resources. A program can be
assigned a resource by getting a semaphore (via a system call to the operating system). When the resource
is no longer needed, the semaphore is returned to the operating system, which can then allocate it to
another program
A semaphore is hardware or a software tag variable whose value indicates the status of a common
resource. Its purpose is to lock the resource being used. A process which needs the resource will check
the semaphore for determining the status of the resource followed by the decision for proceeding. In
multitasking operating systems, the activities are synchronized by using the semaphore techniques.
Types of semaphore:
A semaphore is a variable. There are 2 types of semaphores:
Binary semaphores
Counting semaphores
Binary semaphores have two methods associated with it. (up, down / lock, unlock) Binary semaphores
can take only 2 values (0/1). They are used to acquire locks. When a resource is available, the process in
charge set the semaphore to 1 else 0.
Counting Semaphore may have value to be greater than one, typically used to allocate resources from a
pool of identical resources.
A semaphore is a protected variable whose value can be accessed and altered only by the operations P
and V and initialization operation called ‘Semaphore initislize’.
78
Binary Semaphores can assume only the value 0 or the value 1 counting semaphores also called general
semaphores can assume only nonnegative values.
The P (or wait or sleep or down) operation on semaphores S, written as P(S) or wait (S), operates as
follows:
P(S): IF S > 0
THEN S := S - 1
ELSE (wait on S)
The V (or signal or wakeup or up) operation on semaphore S, written as V(S) or signal (S), operates as
follows:
V(S): IF (one or more process are waiting on S)
THEN (let one of these processes proceed)
ELSE S := S +1
Operations P and V are done as single, indivisible, atomic action. It is guaranteed that once a semaphore
operations has stared, no other process can access the semaphore until operation has completed. Mutual
exclusion on the semaphore, S, is enforced within P(S) and V(S).
If several processes attempt a P(S) simultaneously, only process will be allowed to proceed. The other
processes will be kept waiting, but the implementation of P and V guarantees that processes will not suffer
indefinite postponement.
Semaphores solve the lost-wakeup problem.
7.6.1 Producer-Consumer Problem Using Semaphores
The Solution to producer-consumer problem uses three semaphores, namely, full, empty and mutex.
The semaphore ‘full’ is used for counting the number of slots in the buffer that are full. The ‘empty’ for
counting the number of slots that are empty and semaphore ‘mutex’ to make sure that the producer and
consumer do not access modifiable shared section of the buffer simultaneously.
Initialization
* Set full buffer slots to 0.
i.e., semaphore Full = 0.
* Set empty buffer slots to N.
i.e., semaphore empty = N.
* For control access to critical section set mutex to 1.
i.e., semaphore mutex = 1.
Producer ( )
WHILE (true)
produce-Item ( );
P (empty);
P (mutex);
enter-Item ( )
V (mutex)
V (full);
Consumer ( )
79
WHILE (true)
P (full)
P (mutex);
remove-Item ( );
V (mutex);
V (empty);
consume-Item (Item)
7.6.2 What is a Monitor?
A monitor is a set of multiple routines which are protected by a mutual exclusion lock. None of the
routines in the monitor can be executed by a thread until that thread acquires the lock. This means that only
ONE thread can execute within the monitor at a time. Any other threads must wait for the thread that’s
currently executing to give up control of the lock.
However, a thread can actually suspend itself inside a monitor and then wait for an event to occur. If this
happens, then another thread is given the opportunity to enter the monitor. The thread that was suspended
will eventually be notified that the event it was waiting for has now occurred, which means it can wake up
and reacquire the lock.
7.6.3 Differences between Monitors and Semaphores
Both Monitors and Semaphores are used for the same purpose – thread synchronization. But, monitors
are simpler to use than semaphores because they handle all of the details of lock acquisition and release.
An application using semaphores has to release any locks a thread has acquired when the application
terminates – this must be done by the application itself. If the application does not do this, then any other
thread that needs the shared resource will not be able to proceed.
Another difference when using semaphores is that every routine accessing a shared resource has to
explicitly acquire a a lock before using the resource. This can be easily forgotten when coding the routines
dealing with multithreading . Monitors, unlike semaphores, automatically acquire the necessary locks.
7.7 Summary
Process synchronization mean the coordination of simultaneious threads or processes to complete a task
in order to get correct runtime order and avoid unexpected race conditions. On the other hand data
synchronization is to keep multiple copies of dataset in coherence with one another.
A critical section is a piece of code that accesses a shared resource. And critical section problem is to
ensure that when one process is executing in its critical section, no other process is allowed to execute in
its critical section.
Semaphores are used to help with synchronization. If multiple processes share a common resources, they
need a way to be able to use that resource without disrupting each other. You want each process to be able
to read from and write to that resource uninterrupted. A semaphore will either allow or disallow access to
the resource, depending on how it is set up.
7.8 Self-Assessment Questions
1. Explain Process Synchronization. How it is different from Data Synchronization?
2. What is Critical Section Problem. Explain its general structure.
3. Write some hardware instruction used for solving critical section problem.
4. Explain Semaphore and its different types.
80
Unit - 8 : Deadlocks
Structure of the Unit
8.0 Objective
8.1 Introduction
8.2 Necessary Conditions for Deadlocks
8.3 Prevention
8.3.1 Elimination of “Mutual Exclusion” Condition
8.3.2 Elimination of “Hold and Wait” Condition
8.3.3 Elimination of “No-preemption” Condition
8.3.4 Elimination of “Circular Wait” Condition
8.4 Deadlock Avoidance
8.4.1 Banker’s Algorithm
8.5 Deadlock Detection
8.6 Recovery from Deadlock
8.6.1 Recovery from Deadlock : Process Termination
8.6.2 Recovery from Deadlock : Resource Preemption
8.7 Summary
8.8 Self Assessment Question
8.0 Objecitve
After studying this unit you will be able to understand about deadlocks and conditions for deadlocks like
mutual exclusion, hold and wait, no-preemption and circular wait. You will be able to understand about
algorithms like bankers algorithm to avoid deadlocks. Also, you will get ideas about ways to detect
deadlocks and recover from them.
8.1 Introduction
A deadlock is a situation wherein two or more competing actions are each waiting for the other to finish.
Deadlock is a common problem in multiprocessing where many processes share a specific type of
mutually exclusive resource known as a software lock or soft lock. Computers intended for the
time-sharing and/or real-time markets are often equipped with a hardware lock (or hard lock) which
guarantees exclusive access to processes, forcing serialized access. Deadlocks are particularly
troubling because there is no general solution to avoid (soft) deadlocks.
Examples: This situation may be like, two people who are drawing diagrams, with only one pencil and
one ruler between them. If one person takes the pencil and the other takes the ruler, a deadlock occurs
when the person with the pencil needs the ruler and the person with the ruler needs the pencil to finish his
work with the ruler. Neither request can be satisfied, so a deadlock occurs.
When two trains approach each other at a crossing, both shall come to a full stop and neither shall start up
again until the other has gone.
An example of a deadlock which may occur in database products is the following. Client applications using
the database may require exclusive access to a table, and in order to gain exclusive access they ask for a
lock. If one client application holds a lock on a table and attempts to obtain the lock on a second table that
is already held by a second client application, this may lead to deadlock if the second application then
81
attempts to obtain the lock that is held by the first application. (This particular type of deadlock could be
prevented, by using an all-or-none resource allocation algorithm.)
8.2 Necessary Conditions for Deadlocks
There are four necessary conditions for a deadlock to occur
1. Mutual Exclusion: A resource that cannot be used by more than one process at a time
2. Hold and Wait: Processes already holding resources may request new resources held by other
processes
3. No Preemption: No resource can be forcibly removed from a process holding it, resources can
be released only by the explicit action of the process.
Circular Wait: Two or more processes form a circular chain where each process waits for a resource that
the next process in the chain holds. When circular waiting is triggered by mutual exclusion operations it is
sometimes called lock inversion.
82
8.3 Prevention
All four conditions are necessary for deadlock to occur, it follows that deadlock might be prevented by
denying any one of the conditions
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.
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 (9 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.
The no 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 require 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
necessary, request them again together with additional resources. Implementation of this strategy denies
the “no-preemptive” condition effectively.
The last condition, the circular wait, can be denied by imposing a total ordering on all of the resource types
and than 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.
83
Figure 8.2 : Provide a global numbering of all the resources
1 = Card reader
2 = Printer
3 = Plotter
4 = Tape drive
5 = Card punch
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 printer and then a tape drive (order: 2, 4), but it may
not request first a plotter and then a printer (order: 3, 2). The problem with this strategy is that it may be
impossible to find an ordering that satisfies everyone.
8.4 Deadlock Avoidance
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.
8.4.1 Banker’s Algorithm
In this analogy, customers are processes, Units are reprocess, say tape drive and Bauker in the operating
system.
Customers Used Max
A 0 6
B 0 5 Available
C 0 4 Units = 10
D 0 7
Figure 8.3
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
84
Customers Used Max
A 1 6
B 1 5
C 2 4
D 4 7
Figure 8.4
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 or B
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 2.
We would have following situation
Customers Used Max
A 1 6
B 2 5 Available
C 2 4 Units = 1
D 4 7
Figure 8.5
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 [1969] has shown
that 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.
8.5 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 needed resource 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 problem is starvation; same process killed repeatedly.
85
8.6 Recovery from Deadlock
• There are two options for breaking a deadlock:
– To abort one or more processes to break the circular wait (Process Termination).
– To preempt some resources from one or more of deadlock processes (Resource Pre
emption).
8.6.1 Recovery from Deadlock: Process Termination
• Two methods to eliminate deadlocks by aborting a process. In both methods, the system reclaims
all resources allocated to the terminated processes:
– Abort all deadlocked processes: It will break the deadlock cycle, but a great expense.
– Abort one process at a time until the deadlock cycle is eliminated: Overhead, since, after
each process aborted a deadlock-detection algorithm must be invoked to determine whether any
processes are still deadlocked.
• In which order should we choose to abort?
– Priority of the process.
– How long process has computed, and how much longer to completion.
– Resources the process has used.
– Resources process needs to complete.
– How many processes will need to be terminated?
8.6.2 Recovery from Deadlock: Resource Preemption
• If preemption is required to deal with deadlocks, then three issues need to be addressed:
Selecting a victim – which resources and which processes are to be preempted? (minimize cost)
Rollback – If we preempt a resource from a process, what should be done with that process?
We must roll back the process to some safe state, and restart it from that state.
Starvation – That is same process may always be picked as victim, include number of rollback
is cost factor. Insure that a starvation will not occur. That is Guarantee that resources will not
always be preempted from the same process.
8.7 Summary
In a multiprogramming environment, several process compete for resources. A situation may arise where a
process is waiting for a resource that is held by other waiting processes. This situation is called a deadlock.
A system has a finite set of resources such as memory, I/O devices, etc. It also has a finite set of processes
that need to uset these resources. A process that wishesh to use any of these resources, makes a request
to use that resource. If the resource is free the process gets it. If it is being used by another process, it waits
for it to become free. The assumption is that the resource will eventually become free and the waiting
process will then used the resources. But in some situations, the other process may also be waiting for
some resource.
8.8 Self Assessment Questions
Q1. How can we deal with deadlock?
Q2. What is the difference between prevention and avoidance?
Q3. What are examples of strategies for prevention?
Q4. What happens to a process if resources it is holding are preempted?
86
Q5. Is it safe for the process to just go on executing?
Q6. Short of killing the process that is preempted, how might this problem be
addressed?
Q7. What is the difference between a safe state and a deadlock-free state?
Q8. What is a sufficient condition for deadlock in each of these models?
Q9. What you understand by safe and unsafe state? Explain banker’s algorithm.
with necessary data structure for deadlock avoidance.
Q10. Explain how can we eliminating deadlock by aborting deadlock.
Q11. How can we recover from deadlock?
(a) Process termination.
(b) Resource preemption.
Q12. Explain how we eliminating deadlock by resource preemption can.
(a) Selecting victim.
(b) Rollback.
(c) Starvation.
Q13. Explain the algorithm that examines whether the deadlock has accured.
(a) Single instance of each resource type.
(b) Several instance of each resource type.
Q14. Explain the two deadlock avoidance algorithms.
(a) Safe state.
(b) Resource allocation graph algorithm.
Q15. Explain how we can prevent deadlock.
87
Unit - 9 : Memory Management
88
Also, programs move into, and out of, the main memory as they terminate, or get suspended for some IO,
or new executables are required to be loaded in main memory. So, the OS has to have some strategy for
main memory management. In this chapter we shall discuss the management issues and strategies for both
main memory and secondary memory. Let us begin by examining the issues that prompt the main memory
management.
Allocation: First of all the processes that are scheduled to run must be resident in the memory. These
processes must be allocated space in main memory. Swapping, fragmentation and compaction: If a pro-
gram is moved out or terminates, it creates a hole, (i.e. a contiguous unused area) in main memory. When
a new process is to be moved in, it may be allocated one of the available holes. It is quite possible that main
memory has far too many small holes at a certain time. In such a situation none of these holes is really large
enough to be allocated to a new process that may be moving in. The main memory is too Operating
Systems/Memory management fragmented. It is, therefore, essential to attempt compaction. Compaction
means OS re-allocates the existing programs in contiguous regions and creates a large enough free area for
allocation to a new process.
Garbage collection: Some programs use dynamic data structures. These programs dynamically use and
discard memory space. Technically, the deleted data items (from a dynamic data structure) release memory
locations. However, in practice the OS does not collect such free space immediately for allocation. This is
because that affects performance. Such areas, therefore, are called garbage. When such garbage exceeds
a certain threshold, the OS would not have enough memory available for any further allocation. This entails
compaction (or garbage collection), without severely affecting performance.
Protection: With many programs residing in main memory it can happen that due to a programming error
(or with malice) some process writes into data or instruction area of some other process. The OS ensures
that each process accesses only to its own allocated area, i.e. each process is protected from other
processes. Virtual memory: Often a processor sees a large logical storage space (a virtual storage space)
though the actual main memory may not be that large. So some facility needs to be provided to translate a
logical address available to a processor into a physical address to access the desired data or instruction.
IO support: Most of the block-oriented devices are recognized as specialized files. Their buffers need to
be managed within main memory alongside the other processes. The considerations stated above motivate
the study of main memory management. One of the important considerations in locating an executable
program is that it should be possible to relocate it any where in the main memory.
1. Cache Memory-it is less than or equal to 4MB.Acess time is 3-10 nano seconds (10-9), managed
by hardware and backed by main-memory.
2. Main-Memory-It is greater than or equal to 1GB.Access time is 80-400 nano seconds, managed
89
by operating system and back up by hard disk.
3. Disk storage-It is greater than 1 GB. Access time is 5, 000, 000, managed by operating system
and user and backed by magnetic tape.
Address Binding
User programs typically refer to memory addresses with symbolic names such as “i”, “count”, and “aver-
age Temperature”. These symbolic names must be mapped or bound to physical memory addresses,
which typically occurs in several stages:
• Compile Time - If it is known at compile time where a program will reside in physical memory,
then absolute code can be generated by the compiler, containing actual physical addresses. However if
the load address changes at some later time, then the program will have to be recompiled. DOS .COM
programs use compile time binding.
• Load Time - If the location at which a program will be loaded is not known at compile time, then
the compiler must generate relocatable code, which references addresses relative to the start of the
program. If that starting address changes, then the program must be reloaded but not recompiled.
• Execution Time - If a program can be moved around in memory during the course of its execution,
then binding must be delayed until execution time. This requires special hardware, and is the method
implemented by most modern OSes.
Figure 2: shows the various stages of the binding processes and the units involved in each stage:
90
Logical Versus Physical Address Space
• The address generated by the CPU is a logical address, whereas the address actually seen by
the memory hardware is a physical address.
• Addresses bound at compile time or load time have identical logical and physical addresses.
• Addresses created at execution time,have different logical and physical addresses.
o In this case the logical address is also known as a virtual address, and the two terms
are used interchangeably by our text.
o The set of all logical addresses used by a program composes the logical address
space, and the set of all corresponding physical addresses composes the physical
address space.
• The run time mapping of logical to physical addresses is handled by the memory-management
unit, MMU.
o The MMU can take on many forms. One of the simplest is a modification of the base-
register scheme described earlier.
o The base register is now termed a relocation register, whose value is added to every
memory request at the hardware level.
91
Figure 3: Dynamic relocation using a relocation register
Dynamic loading & linking
Dynamic Loading
• Rather than loading an entire program into memory at once, dynamic loading loads up each routine
as it is called. The advantage is that unused routines need never be loaded, reducing total memory usage
and generating faster program startup times. The downside is the added complexity and overhead of
checking to see if a routine is loaded every time it is called and then loading it up if it is not already loaded.
Dynamic Linking and Shared Libraries
• With static linking library modules get fully included in executable modules, wasting both disk
space and main memory usage, because every program that included a certain routine from the library
would have to have their own copy of that routine linked into their executable code.
• With dynamic linking, however, only a stub is linked into the executable module, containing
references to the actual library module linked in at run time.
o This method saves disk space, because the library routines do not need to be fully
included in the executable modules, only the stubs.
o We will also learn that if the code section of the library routines is reentrant, ( meaning it
does not modify the code while it runs, making it safe to re-enter it ), then main memory
can be saved by loading only one copy of dynamically linked routines into memory and
sharing the code amongst all processes that are concurrently using it. ( Each process
would have their own copy of the data section of the routines, but that may be small
relative to the code segments. ) Obviously the OS must manage shared routines in memory.
o An added benefit of dynamically linked libraries (DLLs, also known as shared
libraries or shared objects on UNIX systems) involves easy upgrades and updates.
When a program uses a routine from a standard library and the routine changes, then
the program must be re-built (re-linked) in order to incorporate the changes. However
if DLLs are used, then as long as the stub doesn’t change, the program can be updated
merely by loading new versions of the DLLs onto the system. Version information is
maintained in both the program and the DLLs, so that a program can specify a particular
version of the DLL if necessary.
o In practice, the first time a program calls a DLL routine, the stub will recognize the fact and
will replace itself with the actual routine from the DLL library. Further calls to the same
routine will access the routine directly and not incur the overhead of the stub access.
Swapping
• A process must be loaded into memory in order to execute.
92
• If there is not enough memory available to keep all running processes in memory at the same time,
then some processes which are not currently using the CPU may have their memory swapped out to a fast
local disk called the backing store.
• If compile-time or load-time address binding is used, then processes must be swapped back into
the same memory location from which they were swapped out. If execution time binding is used, then the
processes can be swapped back into any available location.
• Swapping is a very slow process compared to other operations. For example, if a user process
occupied 10 MB and the transfer rate for the backing store were 40 MB per second, then it would take
1/4 second ( 250 milliseconds ) just to do the data transfer. Adding in a latency lag of 8 milliseconds and
ignoring head seek time for the moment, and further recognizing that swapping involves moving old data
out as well as new data in, the overall transfer time required for this swap is 512 milliseconds, or over half
a second. For efficient processor scheduling the CPU time slice should be significantly longer than this lost
transfer time.
• To reduce swapping transfer overhead, it is desired to transfer as little information as possible,
which requires that the system know how much memory a process is using, as opposed to how much it
might use. Programmers can help with this by freeing up dynamic memory that they are no longer using.
• It is important to swap processes out of memory only when they are idle, or more to the point, only
when there are no pending I/O operations. (Otherwise the pending I/O operation could write into the
wrong process’s memory space.) The solution is to either swap only totally idle processes, or do I/O
operations only into and out of OS buffers, which are then transferred to or from process’s main memory
as a second step.
• Most modern OSes no longer use swapping, because it is too slow and there are faster
alternatives available. (E.g. Paging.) However some UNIX systems will still invoke swapping if the system
gets extremely full, and then discontinue swapping when the load reduces again.
93
Memory Mapping and Protection
• The system shown in Figure 5 below allows protection against user programs accessing areas that
they should not, allows programs to be relocated to different memory starting addresses as needed, and
allows the memory space devoted to the OS to grow or shrink dynamically as needs change.
94
• The amount of memory lost to fragmentation may vary with algorithm, usage patterns, and some
design decisions such as which end of a hole to allocate and which end to save on the free list.
• Statistical analysis of first fit, for example, shows that for N blocks of allocated memory, another
0.5 N will be lost to fragmentation.
• Internal fragmentation also occurs, with all memory allocation strategies. This is caused by the
fact that memory is allocated in blocks of a fixed size, whereas the actual memory needed will rarely be
that exact size. For a random distribution of memory requests, on the average 1/2 block will be wasted per
memory request, because on the average the last allocated block will be only half full.
o Note that the same effect happens with hard drives, and that modern hardware gives us
increasingly larger drives and memory at the expense of ever larger block sizes, which translates to more
memory lost to internal fragmentation.
o Some systems use variable size blocks to minimize losses due to internal fragmentation.
• If the programs in memory are relocatable, ( using execution-time address binding ), then the
external fragmentation problem can be reduced via compaction, i.e. moving all processes down to one
end of physical memory. This only involves updating the relocation register for each process, as all internal
work is done using logical addresses.
9.4 Paging
• Paging is a memory management scheme that allows processes physical memory to be
discontinuous, and which eliminates problems with fragmentation by allocating memory in equal sized
blocks known as pages.
• Paging eliminates most of the problems of the other methods discussed previously, and is the
predominant memory management technique used today.
Basic Method
• The basic idea behind paging is to divide physical memory into a number of equal sized blocks
called frames, and to divide a programs logical memory space into blocks of the same size called pages.
• Any page (from any process) can be placed into any available frame.
• The page table is used to look up what frame a particular page is stored in at the moment. In the
following example, for instance, page 2 of the program’s logical memory is currently stored in frame 3 of
physical memory:
• (DOS used to use an addressing scheme with 16 bit frame numbers and 16-bit offsets, on
hardware that only supported 24-bit hardware addresses. The result was a resolution of starting frame
addresses finer than the size of a single frame, and multiple frame-offset combinations that mapped to the
same physical hardware address.)
96
• Consider the following micro example, in which a process has 16 bytes of logical memory, mapped
in 4 byte pages into 32 bytes of physical memory. ( Presumably some other processes would be consum-
ing the remaining 16 bytes of physical memory. )
97
Figure 9: Free frames (a) before allocation and (b) after allocation
Hardware Support
• Page lookups must be done for every memory reference, and whenever a process gets swapped
in or out of the CPU, its page table must be swapped in and out too, along with the instruction registers,
etc. It is therefore appropriate to provide hardware support for this operation, in order to make it as fast
as possible and to make process switches as fast as possible also.
• One option is to use a set of registers for the page table. For example, the DEC PDP-11 uses
16-bit addressing and 8 KB pages, resulting in only 8 pages per process. ( It takes 13 bits to address 8
KB of offset, leaving only 3 bits to define a page number. )
• An alternate option is to store the page table in main memory, and to use a single register ( called
the page-table base register, PTBR ) to record where in memory the page table is located.
o Process switching is fast, because only the single register needs to be changed.
o However memory access just got half as fast, because every memory access now requires two
memory accesses - One to fetch the frame number from memory and then another one to access the
desired memory location.
o The solution to this problem is to use a very special high-speed memory device called the trans-
lation look-aside buffer, TLB.
§ The benefit of the TLB is that it can search an entire table for a key value in parallel, and if it is
found anywhere in the table, then the corresponding lookup value is returned.
98
Figure 10: Paging hardware with TLB
§ The TLB is very expensive, however, and therefore very small. (Not large enough to hold the
entire page table.) It is therefore used as a cache device.
§ Addresses are first checked against the TLB, and if the info is not there ( a TLB miss ), then the
frame is looked up from main memory and the TLB is updated.
§ If the TLB is full, then replacement strategies range from least-recently used, LRU to random.
§ Some TLBs allow some entries to be wired down, which means that they cannot be removed
from the TLB. Typically these would be kernel frames.
§ Some TLBs store address-space identifiers, ASIDs, to keep track of which process “owns” a
particular entry in the TLB. This allows entries from multiple processes to be stored simultaneously in the
TLB without granting one process access to some other process’s memory location. Without this feature
the TLB has to be flushed clean with every process switch.
§ The percentage of time that the desired information is found in the TLB is termed the hit ratio.
§ For example, suppose that it takes 100 nanoseconds to access main memory, and only 20 nano-
seconds to search the TLB. So a TLB hit takes 120 nanoseconds total ( 20 to find the frame number and
then another 100 to go get the data ), and a TLB miss takes 220 ( 20 to search the TLB, 100 to go get the
frame number, and then another 100 to go get the data. ) So with an 80% TLB hit ratio, the average
memory access time would be:
0.80 * 120 + 0.20 * 220 = 140 nanoseconds
for a 40% slowdown to get the frame number. A 98% hit rate would yield 122 nanoseconds average
access time ( you should verify this ), for a 22% slowdown.
Protection
• The page table can also help to protect processes from accessing memory that they shouldn’t, or
their own memory in ways that they shouldn’t.
• A bit or bits can be added to the page table to classify a page as read-write, read-only, read-
write-execute, or some combination of these sorts of things. Then each memory reference can be checked
to ensure it is accessing the memory in the appropriate mode.
• Valid / invalid bits can be added to “mask off” entries in the page table that are not in use by the
current process, as shown by example in Figure 8.12 below.
99
• Note that the valid / invalid bits described above cannot block all illegal memory accesses, due to
the internal fragmentation. ( Areas of memory in the last page that are not entirely filled by the process, and
may contain data left over by whoever used that frame last. )
• Many processes do not use all of the page table available to them, particularly in modern systems
with very large potential page tables. Rather than waste memory by creating a full-size page table for every
process, some systems use a page-table length register, PTLR, to specify the length of the page table.
100
Figure12: Sharing of code in a paging environment
Basic Method
• Most users ( programmers ) do not think of their programs as existing in one continuous linear
address space.
• Rather they tend to think of their memory in multiple segments, each dedicated to a particular use,
such as code, data, the stack, the heap, etc.
• Memory segmentation supports this view by providing addresses with a segment number
( mapped to a segment base address ) and an offset from the beginning of that segment.
• For example, a C compiler might generate 5 segments for the user code, library code, global
(static) variables, the stack, and the heap, as shown in Fig.
101
Hardware
• A segment table maps segment-offset addresses to physical addresses, and simultaneously checks for
invalid addresses, using a system similar to the page tables and relocation base registers discussed previously.
( Note that at this point in the discussion of segmentation, each segment is kept in contiguous memory and may
be of different sizes, but that segmentation can also be combined with paging as we shall see shortly. )
102
Figure 15: Buddy system scheme.
Implementation
The buddy allocator is, in fact, an abstract framework which can be easily specialized to serve one
particular task. It knows nothing about the nature of memory it helps to allocate. In order to beat the
lack of this knowledge, the buddy allocator exports an interface that each of its clients is required to
implement. When supplied with an implementation of this interface, the buddy allocator can use
specialized external functions to find a buddy for a block, split and coalesce blocks, manipulate
block order and mark blocks busy or available.
Data organization. Each entity allocable by the buddy allocator is required to contain space for storing
block order number and a link variable used to interconnect blocks within the same order.Whatever
entities are allocated by the buddy allocator, the first entity within a block is used to represent the entire
block. The first entity keeps the order of the whole block. Other entities within the block are assigned the
magic value BUDDY_INNER_BLOCK. This is especially important for effective identification of buddies
in a one-dimensional array because the entity that represents a potential buddy cannot be associated with
BUDDY_INNER_BLOCK (i.e. if it is associated with BUDDY_INNER_BLOCK then it is not a buddy).
Slab allocator
The majority of memory allocation requests in the kernel is for small, frequently used data structures. The
basic idea behind the slab allocator is that commonly used objects are preallocated in continuous areas of
physical memory called slabs. Whenever an object is to be allocated, the slab allocator returns the first
available item from a suitable slab corresponding to the object type. Due to the fact that the sizes of the
requested and allocated object match, the slab allocator significantly reduces internal fragmentation.
Slab of one object type are organized in a structure called slab cache. There are ususally more slabs in the
slab cache, depending on previous allocations. If the slab cache runs out of available slabs, new slabs are
allocated. In order to exploit parallelism and to avoid locking of shared spinlocks, slab caches can have
variants of processor-private slabs called magazines. On each processor, there is a two-magazine cache.
Full magazines that are not part of any per-processor magazine cache are stored in a global list of full
magazines.
Each object begins its life in a slab. When it is allocated from there, the slab allocator calls a constructor
that is registered in the respective slab cache. The constructor initializes and brings the object into a
known state. The object is then used by the user. When the user later frees the object, the slab allocator
puts it into a processor private magazine cache, from where it can be precedently allocated again. Note
103
that allocations satisfied from a magazine are already initialized by the constructor. When both of the
processor cached magazines get full, the allocator will move one of the magazines to the list of full
magazines. Similarily, when allocating from an empty processor magazine cache, the kernel will reload
only one magazine from the list of full magazines. In other words, the slab allocator tries to keep the
processor magazine cache only half-full in order to prevent thrashing when allocations and deallocations
interleave on magazine boundaries. The advantage of this setup is that during most of the allocations, no
global spinlock needs to be held.
104
allocated from the conventional slab layer and a pointer to it is returned. If also the last magazine is full,
Deallocation. Step 1. During a deallocation request, the slab allocator checks if the current magazine of
the local processor magazine cache is not full. If it is, the pointer to the objects is just pushed into the
magazine and the algorithm returns.Step 2. If the current magazine is full, the allocator will attempt to swap
it with the last magazine from the cache and return to the first step. If also the last magazine is empty, the
algorithm will fall through to Step 3.Step 3. Now the allocator is in the situation when both magazines in the
processor magazine cache are full. The allocator tries to allocate a new empty magazine and flush one of
the full magazines to the shared list of full magazines. If it is successfull, the algoritm continues with Step
1.Step 4. In case of low memory condition when the allocation of empty magazine fails, the object is
moved directly into slab. In the worst case object deallocation does not need to allocate any additional
memory.
9.7 Summary
Computer has main memory of RAM. Various architectures enable various uses of such memory,
internally, memory could be accessed in different ways. Processes cannot run unless their code and data
structures are in the RAM. It is in the main memory where instructions reside and are interpreted by the
processor.
Some important issues related to the need for memory management include :
* Many times an operating system manages many process (multi-programming).
* The code and data for a process must be in RAM before it could be run.
* Process must not be able to access the code and data of other processes without permission. It
means that the processes must be protected.
* Processes must be able to access and share the code and data of other processes if they have
permission.
* There is usually not enough RAM to hold the code and data for all the currently running processes
in RAM.
9.8 Self Assessment Questions
Q.1 What is address space? Defferentiate logical and physical spaces.
Q.2 What is the difference between static relocation and dynamic relocation?
Q.3 What is segmentation? Explain address translation in segmentation.
Q.4 What is memory fragmentation? What are the internal and External memory fragmentations.
Q.5 In the paging scheme, what are pages,frame and page table?
Q.6 Explain how page table is used in address translation.
Q.7 Can we use compactation to solve internal fragmentation problem? Justify your answer.
Q.8 Explain the buddy system for space allocation.What kind of memory fragmentation does it
induce?
Q.9 Explain briefly the role of the compiler,loader,and memory management hardware in the following
address binding schemes:
a) compile time binding
b) load time binding
105
c) runtime binding
Q.10 What is dynamic memory management? Why is it used?Which part of the Kernel is stored there.
Q.11 Can we pricisely restrict a process within its address space in segmentation,paging and paged
segmentation schemes?justify your answer.
Q.12 Explain slab allocator and how it is implemented.
Q.13 What kind of fragmentations(external or internal)do the following memory management schemes
have? In each case.justify your answer.
a) Paging
b) Segmentation with(first fit,best fit and buddy system)
c) Paged segmentation
Q.14 What is Swpping and how it is implemented by virtual memory.
Q.15 Explain memory heirarchy in terms of memory management in operating system.
106
Unit - 10 : Introduction to Paging, Segmentation and
Segmentation with Paging
Structure of the Unit
10.0 Objective
10.1 Introduction
10.2 Segmentation
10.3 Segmentation with Paging
10.4 Basic H/W Support
10.5 Structure of Page Table
10.6 Hierarchical Paging
10.7 Hashed paging
10.8 Inverted Page Tables
10.9 Summary
10.10 Self Assessment Questions
10.0 Objective
• Introduction to paging
• To provide a detailed description of the segmentation and segmentation with paging.
• Structure of page table etc.
10.1 Introduction
Virtual Memory is the separation of user logical memory from physical memory. This separation allows an
extremely large virtual memory for programmers when physical memory is smaller in size. This makes the
task of programmers very easy. In addition to this, VM also allows sharing of files and memory between
several processes and makes implementation of memory protection much easier. Virtual memory can be
implemented by one of the following techniques:
1. Paging
2. Segmentation
3. Segmentation with Paging
Paging
Paging is a memory management scheme in which memories as well as processes are divided into fixed
size of blocks. Paging is the schema that permits the logical address space of a process to be noncontiguous.
Physical memory broken into fixed sized blocks is called Page frames. Logical memory address space/
process is also broken into blocks of same size as page frame called Pages. When a process is to be
executed, its pages are loaded into any available memory frames from secondary storage. The secondary
storage is also divided into fixed size blocks that are of the same size as the memory frames.
The page size is defined by the hardware and typically a power of two. The size of page typically lies
between 512 bytes and 16 MB, depending upon computer architecture.
107
Figure 10.1 : Address Translation Architecture
Page Table
The operating system stores the address translation tables for mapping virtual address to physical address
in a data structure known as a page table. Figure 10.1 : Explain the address translation.
• Page table is kept in main memory.
• Page-table base register (PTBR) points to the page table.
• Page-table length register (PTLR) indicates size of the page table.
• In this scheme every data/instruction access requires two memory accesses. One for the page
table and one for the data/instruction.
• The two memory access problem can be solved by the use of a special fast-lookup hardware
cache called associative memory
• Address generated by CPU is divided into: Page number (p) - used as an index into a page table
which contains base address of each page in physical memory. Page offset (d) - combined with base
address to define the physical memory address that is sent to the memory unit.
If the size f the logical address space is 2m, and a page size is 2n addressing units (bytes or words), then
the high order m-n bits of a logical address designate the page number, and the n low order bits designate
the page offset.
p(page number) d(page offset)
m-n n
108
• Each segment consists of a linear sequence of addresses, from zero to maximum.
• The length of each segment may be anything from 0 to maximum.
• Different segment may have different length.
• The segment length may change during execution.
• The OS maintains a free list and allocate segments to memory holes.
• Simplifies modification and recompilation of procedures and facilitate sharing of procedure and data.
A segment table is very similar to the page table. A segment table entry must have fields for storing the
segment's starting address in main memory and the size of the segment. If the maximum segment size is m
bits, then last m bits of the logical address specify the segment offset. The remaining bits specify the
segment number. The logical address is translated into a physical address by extracting the segment number
and offset from the logical address. The segment number is used as an index into the segment table. The
offset is compared to the segment size. If the offset size is greater than the segment size, invalid address
faults is generated, and abort the program. Otherwise, the offset is added to the segment's starting address
to generate the physical address. To increase speed, the size check and physical address generation can
be performed concurrently.
s p d
+ + pf d
pf
Segment Physical
Table Address
Address Segment
Page
Table
Table
112
Figure 10.7 : A two level page table scheme
One way is to use a two-level paging algorithm, in which the page table itself is also paged Remember our
example of a 32-bit machine with a page size of 4 KB. A logical address is divided into a page number
consisting of 20 bits and a page offset consisting of 12 bits. Because we page the page table, the page
number is further divided into a la-bit page number and a 10-bit page offset. Thus, a logical address is as
follows:
Figure 10.9 : Address Translation for a two level 32 bit paging architecture
10.7 Hashed Paging
A common approach for handling address spaces larger than 32 bits is to use a hashed page table, with the
hash value being the virtual page number. Each entry in the hash table contains a linked list of elements that
hash to the same location (to handle collisions). Each element consists of three fields: (1) the virtual page
number, (2) the value of the mapped page frame, and (3) a pointer to the next element in the linked list.
The algorithm works as follows: The virtual page number in the virtual address is hashed into the hash
table. The virtual page number is compared with field 1 in the first element in the linked list. If there is a
match, the corresponding page frame (field 2) is used to form the desired physical address.
113
If there is no match, subsequent entries in the linked list are searched for a matching virtual page number.
This scheme is shown in Figure 8.16. A variation of this scheme that is favorable for 64-bit address spaces
has been proposed. This variation 1JSeS clustered page tables, which are similar to hashed page tables
except that each entry in the hash table refers to several pages (such as 16) rather than a single page.
Therefore, a single page-table entry can store the mappings for multiple physical-page frames. Clustered
page tables are particularly useful for sparse address spaces, where memory references are noncontiguous
and scattered throughout the address space.
114
Figure 10.11 : Inverted page table architecture
10.9 Summary
One of the most important, and most complex, tasks of an operating system is Memory Management.
Memory management involves treating of main memory as a resource to be allocated to and shared among
a number of active processes.
To efficiently use the processor and the I/O facilities, it is desirable to maintain as many processes in main
memory as possible. In addition, it is desirable to free programmers from size restrictions in program
development. The way to address both of these concerns is to use Virtual Memory. With Virtual memory
all address references are logical references that are translated at run time to real address.
This use allows a process to be allocated anywhere in main memory and for that location to change over
time. Virtual memory also allows a process to be broken up into pieces. These pieces need not be contiguously
located in main memory during execution, and indeed all pieces of the process need not be in main memory.
Two basic approaches to provide virtual memory are Paging and Segmentation. With Paging, each process
is divided into relatively small, fixed-size pages. Segmentation provides for the use of pieces of varying
size. It is also possible to combine segmentation and paging in a single memory management scheme.
10.10 Self Assessment Question
1. Define Memory Management.
2. List the requirements of memory management.
3. State the advantages of logical organization.
4. Define Overlaying.
5. What do you mean by Virtual memory?
6. Differentiate Internal fragmentation and External fragmentation.
7. State the technique used to overcome external fragmentation.
8. Bring out the difference between First fit, Best fit and Next fit.
9. Differentiate Logical address and Physical address.
10. Define: pages and frames.
11. State the advantages and disadvantages of Paging.
12. Define Segmentation.
13. Write down the steps for address translation in segmentation.
115
Unit - 11 : Virtual Memory Management
116
But always remember that the whole programs are never to be copied into the memory, it means
all the programs are copied in form of pages or parts.
11.2 Pre-Paging and Demand Paging
When we execute a program it might be loaded from disk into memory. One option is to load the entire
program in physical memory at the time of execution. However, a problem with this approach is that we
may not initially need the entire program in memory. Consider a program that starts with a list of available
options from which the user is to select. Loading the entire program into memory results in loading the
executable code for all options, regardless of whether an option is ultimately selected by the user or not.
An alternative strategy is to initially load pages only as they are needed.
This technique is known as demand paging and is commonly used in virtual memory systems. With
demand-paged virtual memory, pages are only loaded when they are demanded during program execution,
pages that are never accessed are thus never loaded into physical memory.
When we want to execute a process, we swap it into memory. Rather than swapping the entire process
into memory, however, we use a lazy swapper. A lazy swapper never swaps a page into memory unless
that page will be needed.
117
simply marked invalid or contains the address of the page on disk. This situation is shown in figure 11.1
Marking a page invalid will have no effect if the process never attempts to access that page. Hence, if we
guess right and page in all and only those pages that are actually needed, the process will run exactly as
though we had brought in all pages. While the process executes and accesses pages that are memory
resident, execution proceeds normally.
Figure 11.2 : Page table when some pages are not in main memory
119
11.5 Page Replacement Policies
A reference to a page, not present in main memory, is called page fault. When page fault occurs, it is
necessary for OS to read in the required page from the secondary memory. Removing the page randomly
will not be a good idea hence we need to have page replacement algorithm.
Page replacement algorithms decide what pages to page out (swap out) when a page needs to be allocated.
This happens when a page fault occurs and a free page cannot be used to satisfy the allocation, either
because there are none, or because the number of free pages is lower than some threshold.
When the page that was selected for replacement and paged out is referenced again it has to be paged in,
and this usually involves waiting for I/O completion. This determines the quality of the page replacement
algorithm: the less time wasted by waiting for page-ins, the better the algorithm. A page replacement
algorithm tries, by looking at the limited information about accesses to the pages provided by hardware, to
guess what pages should be replaced in order to minimize the total number of page misses, while balancing
this with the costs (primary storage and processor time) of the algorithm itself.
There are various page replacement algorithms available. Some of the very important and common
algorithms are discussed:
1. FIFO : The first in first out page replacement algorithm selects the page that has been in memory
the longest .To implement this algorithm page table entries must include a field for the swap in time. When
a page is swapped in the OS loads the field with current time. The page selected for replacement will be
the one with the earliest swap in time. Although easy to implement and inexpensive FIFO is not very
efficient. Frequently used pages even though they are in memory should not be swapped out. FIFO
doesn't consider the amount they have been used and swaps them out anyway.
Example:
Figure 11.6
120
3. Second chance page replacement algorithm: The basic algorithm of second chance replacement
is a FIFO replacement algorithm. When page has been selected, we inspect its reference bits. If the value
is 0, we proceed to replace this page and the reference bit is set to 1, however we give that page a second
chance and move on to select the next FIFO page. When a page gets a second chance its referenced bit
is cleared and its arrival time is reset to the current time. Thus, a page that is given a second chance will not
be replaced until all other pages are replaced. In addition if a page is used often enough to keep its
reference bit set, it will never be replaced.
121
The CPU scheduler sees the decreasing CPU utilization and increases the degree of
multiprogramming as a result. The new process tries to get started by taking frames from running processes,
causing more page faults and a longer queue for the paging device. As a result, CPU utilization drops even
further, and the CPU scheduler tries to increase the degree of multiprogramming even more. Thrashing has
occurred, and system throughput plunges. The pagefault rate increases tremendously. As a result, the
effective memory-access time increases. No work is getting done, because the processes are spending all
their tilrle paging.
Figure 11.8
This phenomenon is illustrated in Figure 11.8, in which CPU utilization is plotted against the degree of
multiprogramming. As the degree of multiprogramming increases, CPU utilization also increases, although
more slowly, until a maximum is reached. If the degree of multiprogramming is increased even further,
thrashing sets in, and CPU utilization drops sharply. At this point, to increase CPU utilization and stop
thrashing, we must decrease the degree of multiprogramming.
We can limit the effects of thrashing by using a local replacement algorithm (or priority replacement
algorithm). With local replacement, if one process starts thrashing, it cannot steal frames from another
process and cause the latter to thrash as well. However, the problem is not entirely solved. If processes
are thrashing, they will be in the queue for the paging device HlOSt of the time. The average service time
for a page fault will increase because of the longer average queue for the paging device. Thus, the
effective access time will increase even for a process that is not thrashing.
To prevent thrashing, we must provide a process with as many frames as it needs. But how do we know
how many frames it "needs"? There are several techniques. The working-set strategy starts by looking at
how many frames a process is actually using. This approach defines the locality model of process execution.
The locality model states that, as a process executes, it moves from locality to locality. A locality is a set of
pages that are actively used together. A program is generally composed of several different localities,
which may overlap.
For example, when a function is called, it defines a new locality. In this locality, memory references are
made to the instructions of the function call, its local variables, and a subset of the global variables. When
we exit the function, the process leaves this locality, since the local variables and instructions of the function
are no longer in active use. We may return to this locality later. Thus, we see that localities are defined by
the program structure and its data structures. The locality model states that all programs will exhibit this
basic memory reference structure. Note that the locality model is the unstated principle behind the caching
discussions so far in this book. If accesses to any types of data were random rather than patterned,
caching would be useless.
122
11.7 Summary
• Virtual memory is commonly implemented by demand paging. Pure demand paging never brings in
a page until that page is referenced. The first reference causes a page fault to the operating system. The
operating-system kernel consults an internal table to determine where the page is located on the backing
store. It then finds a free frame and reads the page in from the backing store.
• The page table is updated to reflect this change, and the instruction that caused the page fault is
restarted. This approach allows a process to run even tll0ugh its entire memory image is not in main
memory at once. As long as the page-fault rate is reasonably low, performance is acceptable.
• We can use demand paging to reduce the number of frames allocated to a process. This arrangement
can increase the degree of multiprogramming (allowing more processes to be available for execution at
one time) and -in theory, at least-the CPU utilization of the system. It also allows processes to be run even
though their memory requirements exceed the total available physical memory. Such processes run in
virtual memory.
• If total memory requirements exceed the physical memory, then it may be necessary to replace
pages from memory to free frames for new pages. Various page-replacement algorithms are used. Optimal
page replacement requires future knowledge. LRU replacement is an approximation of optimal page
replacement, but even it may be difficult to implement. Most page-replacement algorithms, such as the
second-chance algorithm, are approximations of LRU replacement.
• Most operating systems provide features for memory mapping files, thus allowing file I/O to be
treated as routine memory access. The Win32 API implements shared memory through memory mapping
files.
11.8 Self Assessment Questions
1. What do you mean by virtual memory
2. What is demand paging
3. What is page table, how you will determine size of a page
4. What is Thrashing.
5. Write any two page replacement policies
123
Unit - 12 : File Management System
124
A file can have more than one attribute attached to it they may be vary from one operating system to
another but typically consist of these:
A file can have following attributes.
NAME: The symbolic file name is the only information kept in human readable form.
Identifier: This is a number or tag which is used to identified the file with in the file system.
Type : this information is needed for system to support different types of file.
Size : The size of file in bytes.
Read-Only: Most software, when seeing a file marked read-only, will refuse to delete or modify it. This
is pretty straight-forward. For example, DOS will say "Access denied" if you try to delete a read-only file.
On the other hand, Windows Explorer will happily munch it. Some will choose the middle ground: they will
let you modify or delete the file, but only after asking for confirmation.
Hidden: if the file is marked hidden then under normal circumstances it is hidden from view. DOS will not
display the file when you type "DIR" unless a special flag is used, as shown in the earlier example.
System: This flag is used to tag important files that are used by the system and should not be altered or
removed from the disk. In essence, this is like a "more serious" read-only flag and is for the most part
treated in this manner.
Volume Label: Every disk volume can be assigned an identifying label, either when it is formatted, or later
through various tools such as the DOS command "LABEL". The volume label is stored in the root directory
as a file entry with the label attribute set.
Directory: This is the bit that differentiates between entries that describe files and those that describe
subdirectories within the current directory. In theory you can convert a file to a directory by changing this
bit. Of course in practice, trying to do this would result in a mess--the entry for a directory has to be in a
specific format.
Archive: This is a special bit that is used as a "communications link" between software applications that
modify files, and those that are used for backup. Most backup software allows the user to do an incremental
backup, which only selects for backup any files that have changed since the last backup. This bit is used for
this purpose. When the backup software backs up ("archives") the file, it clears the archive bit (makes it
zero). Any software that modifies the file subsequently, is supposed to set the archive bit. Then, the next
time that the backup software is run, it knows by looking at the archive bits which files have been modified,
and therefore which need to be backed up. Again, this use of the bit is "voluntary"; the backup software
relies on other software to use the archive bit properly; some programs could modify the file without setting
the archive attribute, but fortunately most software is "well-behaved" and uses the bit properly. Still, you
should not rely on this mechanism absolutely to ensure that your critical files are backed up.
12.3 File Operations
We can perform various operation on a file . a operation system have various system call to create, write,
read, move ,delete a file.
Creating a file. Two steps are necessary to create a file. Space in the file system must be found for the
file. An entry for the new file must be made in the directory.
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. 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. The system needs to keep a read pointer to the location
in the file where the next read is to take place.
125
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/O.
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.
12.4 File Types
The types of files recognized by the system are either regular, directory, or special. However, the operating
system uses many variations of these basic types.
The following basic types of files exist:
regular Stores data (text, binary, and executable)
directory Contains information used to access other files
special Defines a FIFO (first-in, first-out) pipe file or a physical device
All file types recognized by the system fall into one of these categories. However, the operating system
uses many variations of these basic types.
Regular files
Regular files are the most common files and are used to contain data. Regular files are in the form of text
files or binary files.
Text files
Text files are regular files that contain information stored in ASCII format text and are readable by the user.
You can display and print these files. The lines of a text file must not contain NULL characters, and none
can exceed {LINE_MAX} bytes in length, including the newline character.
The term text file does not prevent the inclusion of control or other nonprintable characters (other than
NULL). Therefore, standard utilities that list text files as inputs or outputs are either able to process the
special characters or they explicitly describe their limitations within their individual sections.
Binary files
Binary files are regular files that contain information readable by the computer. Binary files might be
executable files that instruct the system to accomplish a job. Commands and programs are stored in
executable, binary files. Special compiling programs translate ASCII text into binary code.
Text and binary files differ only in that text files have lines of less than {LINE_MAX} bytes, with no NULL
characters, each terminated by a newline character.
Directory files
Directory files contain information that the system needs to access all types of files, but directory files do
not contain the actual file data. As a result, directories occupy less space than a regular file and give the file
system structure flexibility and depth. Each directory entry represents either a file or a subdirectory with
reference to Unix each entry contains the name of the file and the file's index node reference number
(i-node number). The i-node number points to the unique index node assigned to the file. The i-node
number describes the location of the data associated with the file. Directories are created and controlled
by a separate set of commands.
126
Special files
Special files define devices for the system or are temporary files created by processes. The basic types of
special files are FIFO (first-in, first-out), block, and character. FIFO files are also called pipes. Pipes are
created by one process to temporarily allow communication with another process. These files cease to
exist when the first process finishes. Block and character files define devices.
Every file has a set of permissions (called access modes) that determines who can read, modify, or execute the file.
12.5 File Structure
File types also can be used to indicate the internal structure of file.
The operating system support multiple file structures: The resulting size of the operating system is large. If
the operating system defines five different file structures, it needs to contain the code to support these file
structures.
Every file may need to be define as one of the file types supported by the operating system. When new
applications require information structured in ways not supported by the operating system, severe problems
may occur.
Some operating systems require a minimal number of file structures. This approach has been adopted in
UNIX, MS-DOS, and others. UNIX 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.
12.6 Internal File Structure
Files can be structured in any of several ways. Three common possibilities are Stream of Bytes, Records,
Tree of Records.
Stream of Bytes. The file is an unstructured sequence of bytes. In effect, the operating system does not
know or care what is in the file. All it sees are bytes. Both UNIX and Windows use this approach.
Records. The first step up in structure is a file is a sequence of fixed-length records, each with some
internal structure.
Tree of Records. The third kind of file structure organization, a file consists of a tree of records, not
necessarily all the same length, each containing a key field in a fixed position in the record.
Internally, locating an offset within a file can be complicated for the OS.
Disk systems typically have a well-defined block size determined by the size of a sector. All disk I/O 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. Packing a number of logical
records into physical blocks is a common solution to this problem.
For example, the UNIX OS defines all files to be simply streams of bytes. Each byte is individually
addressable by its offset from the beginning (or end) of the file. In this case, the logical record size is 1 byte.
The file system automatically packs and unpacks bytes into physical disk blocks -say, 512 bytes per
block- as necessary. The file may be considered to be a sequence of blocks. All the basic I/O functions
operate in terms of blocks. 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.
127
12.7 Accessing Method
Information is kept in files. Files reside on secondary storage. When this information is to be used, it has to
be accessed and brought into primary main memory. Information in files could be accessed in many ways.
It is usually dependent on an application.
Sequential Access: A simple access method, information in a file is accessed sequentially one record
after, another. To process the next record records previous to it must be accessed. Sequential access is
based on the tape model that is inherently a sequential access device. Sequential access is best suited
where most of the records in a file are to be processed. For example, transaction files.
Direct Access: Sometimes it is not necessary to process every record in a file. It may not be necessary to
process records in the order in which they are present. Information present in a record of a file is to be
accessed only if some key value in that record is known. In all such cases, direct access is used. Direct
access is based on the disk that is a direct access device and allows random access of any file block. Since
a file is a collection of physical blocks, any block and hence the records in that block are accessed.
For example, master files. Databases are often of this type since they allow query processing that involves
immediate access to large amounts of information. All reservation systems fall into this category. Not all
operating systems support direct access files. Usually files are to be defined as sequential or direct at the
time of creation and accessed accordingly later.
Sequential access of a direct access file is possible but direct access of a sequential file is not.
Indexed Sequential Access: This access method is a slight modification of the direct access method. It is in
fact a combination of both the sequential access as well as direct access. The main concept is to access a
file direct first and then sequentially from that point onwards. This access method involves maintaining an
index. The index is a pointer to a block. To access a record in a file, a direct access of the index is made.
The information obtained from this access is used to access the file. For example, the direct access to a file
will give the block address and within the block the record is accessed sequentially. Sometimes indexes
may be big. So hierarchies of indexes are built in which one direct access of an index leads to info to access
another index directly and so on till the actual file is accessed sequentially for the particular record. The
main advantage in this type of access is that both direct and sequential access of files is possible.
12.8 Directory Structure
File systems typically have directories (sometimes called folders) which allow the user to group files. This
may be implemented by connecting the file name to an index in a table of contents or an inode in a Unix-like
file system. Directory structures may be flat (i.e. linear), or allow hierarchies where directories may contain
subdirectories. The first file system to support arbitrary hierarchies of directories was the file system in the
UNIX operating system. The native file systems of Unix-like systems also support arbitrary directory
hierarchies, as do, for example, the FAT file system in MS-DOS 2.0 and later and Microsoft Windows,
the NTFS file system in the Windows NT family of operating systems.
12.9 File Access and Access Control
Many operating systems, including Unix, are multiuser. As such, it is necessary to have mechanisms which
protect a user's file from unwanted access. An integral part of an access control mechanism in a multiuser
operating system is the concept of user ownership of a file and, very often, the concept of group ownership.
However, many operating systems, such as those for personal computers, are single-user operating systems.
In a single-user environment, there is no need for file access mechanisms which protect files from unwanted
access by others because, conceptually, there is only one user. Nonetheless, some single-user operating
systems permit a file to be marked as read-only to protect the file from inadvertent modification or removal.
Both single-user and multiuser operating systems have been used as servers providing transparent file
access. Most multiuser operating systems are already well suited for use as a file server since access
128
protection is already an integral part of its design. For a single-user operating system, it is necessary that
some sort of access control mechanism be implemented when such an operating system is used to provide
file service. This is usually accomplished by creating a separate partition for each user's files. Thus, user
identification is not associated with an individual file but with a partition on the server's disk. File servers
which have single-user operating systems may also have partitions that are accessible by anyone.
Network environments include both servers based on multiuser operating systems and servers based on
single-user operating systems. Application programs on clients may have transparent access to servers
which are based on single-user operating systems which may neither provide owner information nor group
ownership information for a file, but are capable of imposing some level of file access control. That file
access control may be no more than a read/write permission that applies to any user. A very large group of
applications are able to function in such an environment.
12.10 Summary
A file is an abstract data type defined and implemented by the operating system. It is a sequence of logical
records. A logical record may be a byte, a line, or a more complex data item. The operating system may
specifically support various record types or may leave that support to the application program.
The major task for the operating system is to map the logical file concept onto physical storage devices
such as magnetic tape or disk. Since the physical record size of the device may not be the same as the
logical record size, it may be necessary to order logical records into physical records. Again, this task may
be supported by the operating system or left for the application program.
Each device in a file system keeps a volume table of contents or device directory listing the location of the
files on the device. In addition, it is useful to create directories to allow files to be organized.
A tree-structured directory allows a user to create subdirectories to organize files. Acyclic-graph directory
structures enable users to share subdirectories and files but complicate searching and deletion. A general
graph structure allows complete flexibility in the sharing of files and directories but sometimes requires
garbage collection to recover unused disk space.
12.11 Self Assessment Questions
1. What do you mean by file attributes.
2. What are the different operation can be perform on a file.
3. Explain various file accessing methods.
4. Write a short note on file access control.
129
Unit - 13 : I/O System
130
13.2 Overview I/O Hardware
Computers operate a with many kinds of I/O devices. like general categories of storage devices (disks,
tapes), transmission devices (network cards, modems), and human-interface devices (screen, keyboard,
mouse). Here we will learn concepts to understand how the devices are attached and how the software
can control the hardware.
A device communicates with a computer system by sending signals over a cable or even through the air.
The device communicates with the machine via a connection point i.e. port. If devices use a common set of
wires, the connection is called a bus. A bus is a set of wires and a rigidly defined protocol that specifies a
set of messages that can be sent on the wires.
131
The CPU executes I/O requests using the standard data-transfer instructions to read and write the device-
control registers. An I/O port typically consists of four registers, called the (1) status, (2) control, (3) data-in,
and (4) data-out registers.
• The status register contains bits that can be read by the host. These bits indicate states, such as
whether the current command has completed, whether a byte is available to be read from the
data-in register, and whether a device error has occurred.
• The control register can be written by the host to start a command or to change the mode of a
device. For instance, a certain bit in the control register of a serial port chooses between
full-duplex and half-duplex comn1unication, another bit enables parity checking, a third bit sets
the word length to 7 or 8 bits, and other bits select one of the speeds supported by the serial port.
• The data-in register is read by the host to get input.
• The data-out register is written by the host to send output.
The data registers are typically 1 to 4 bytes in size. Some controllers have FIFO chips that can hold several
bytes of input or output data to expand the capacity of the controller beyond the size of the data register.
A FIFO chip can hold a small burst of data until the device or host is able to receive those data.
13.2.1 Polling
Device polling refers to a technique that lets the operating system periodically poll devices, instead of
relying on the devices to generate interrupts when they need attention. This might seem inefficient and
counterintuitive, but when done properly, polling gives more control to the operating system on when and
how to handle devices, with a number of advantages in terms of system responsiveness and performance.
In particular, polling reduces the overhead for context switches which is incurred when servicing interrupts,
and gives more control on the scheduling of the CPU between various tasks (user processes, software
interrupts, device handling) which ultimately reduces the chances of live lock in the system.
13.2.2 Interrupts
The basic interrupt mechanism works as follows. The CPU hardware has a wire called the interrupt-
request line that the CPU senses after executing every instruction. When the CPU detects that a controller
has asserted a signal on the
132
Interrupt request line, the CPU performs a state save and jumps to the interrupt handler routine at a fixed
address in memory. Title interrupt handler determines the cause of the interrupt, performs the necessary
processing, performs a state restore, and executes a return from interrupt instruction to return the CPU to
the execution state prior to the interrupt. We say that the device controller raises an interrupt by asserting
a signal on the interrupt request line, the CPU catches the interrupt and dispatches it to the interrupt
handler, and the handler clears the interrupt by servicing the device. Figure summarizes the interrupt-driven
I/O cycle.
13.2.3 Direct Memory Access
I/O devices such as hard disks write to memory all the time, and with a normal system setup, their requests
go through to the CPU first and then the CPU reads/writes the data from/to the memory sequentially.
When such a request involves polling/busy wait loops, it is known as programmed I/O. The problem with
polling is that it wastes a lot of CPU resources as it sits in a tight loop checking for changes in values. The
second method for I/O devices to access memory is called interrupt-driven. The problem now is that
servicing interrupts is expensive, in the sense that when an interrupt signal is sent by an I/O device, before
or after a memory access, the following has to be done every time:
- The data for the currently running process in the CPU is to be saved to the stack.
- An Interrupt Service Routine is allowed to handle the interrupt
- Data is sent back to the I/O controller
- The data for the previously running process is restored from the stack.
Such a method has a very large overhead. The state information for the current process has to be saved
before the I/O operation can use the CPU, and then restored after it has finished.
A much better way of doing things would be to take away the CPU from the picture all together and have
the I/O devices talking directly to the memory. This is where DMA (Direct Memory Access) comes in.
The DMA controller sits on the shared system bus containing the memory and CPU and allows 7 I/O
devices to connect to it. Once it has all the information it needs about a particular I/O device, such as the
number of words (word = 16 bits) to transfer and the memory address of the first word of input or output
(Memory Address Counter), the DMA controller then transmits the data directly to the memory via the
shared system bus.
The advantages of DMA The most important is that the processor does not have to worry about I/O
operations between computer peripherals and memory. Another advantage is the fact that transfers are
much simpler since they do not require the CPU to execute specific instructions to do the transfer or have
to deal with interrupts being signaled in from I/O devices. All the DMA controller needs is a start indicator
to let it know when it can start transferring data to memory, a counter for how many words are left and a
stop indicator to clean up at the end.
13.3 Application I/O Interface
User application access to a wide variety of different devices is accomplished through layering, and through
encapsulating all of the device-specific code into device drivers, while application layers are presented
with a common interface for all (or at least large general categories of ) devices.
133
Figure 13.3 : Kernel I/O Structure
Most devices can be characterized as either block I/O, character I/O, memory mapped file access, or
network sockets. A few devices are special, such as time-of-day clock and the system timer.
Most OS also have an escape, or back door, which allows applications to send commands directly to
device drivers if needed. In UNIX this is the ioctl( ) system call ( I/O Control ). ioctl( ) takes three
arguments - The file descriptor for the device driver being accessed, an integer indicating the desired
function to be performed, and an address used for communicating or transferring additional information.
13.3.1 Blocked Character Device
Block devices are accessed a block at a time, and are indicated by a "b" as the first character in a long
listing on UNIX systems. Operations supported include read( ), write( ), and seek( ).
Accessing blocks on a hard drive directly (without going through the file system structure) is called raw I/O,
and can speed up certain operations by bypassing the buffering and locking normally conducted by the
OS. (It then becomes the application's responsibility to manage those issues.)
A new alternative is direct I/O, which uses the normal file system access, but which disables buffering and
locking operations.
Memory-mapped file I/O can be layered on top of block-device drivers. Rather than reading in the entire
file, it is mapped to a range of memory addresses, and then paged into memory as needed using the virtual
memory system. Access to the file is then accomplished through normal memory accesses, rather than
through read( ) and write( ) system calls. This approach is commonly used for executable program code.
Character devices are accessed one byte at a time, and are indicated by a "c" in UNIX long listings.
Supported operations include get( ) and put( ), with more advanced functionality such as reading an entire
line supported by higher-level library routines.
13.3.2 Blocking & Non Blocking Input Output
With blocking I/O a process is moved to the wait queue when an I/O request is made, and moved back to
the ready queue when the request completes, allowing other processes to run in the meantime.
With non-blocking I/O the I/O request returns immediately, whether the requested I/O operation has
(completely) occurred or not. This allows the process to check for available data without getting hung
completely if it is not there.
One approach for programmers to implement non-blocking I/O is to have a multi-threaded application, in
which one thread makes blocking I/O calls ( say to read a keyboard or mouse ), while other threads
continue to update the screen or perform other tasks.
134
A subtle variation of the non-blocking I/O is the asynchronous I/O, in which the I/O request returns
immediately allowing the process to continue on with other tasks, and then the process is notified (via
changing a process variable, or a software interrupt, or a callback function) when the I/O operation has
completed and the data is available for use. (The regular non-blocking I/O returns immediately with whatever
results are available, but does not complete the operation and notify the process later.)
13.4 Kernel I/O Sub System
Kernels provide many services related to I/O. Several services-scheduling, buffering, caching, spooling,
device reservation, and error handling-are provided by the kernel's I/O subsystem and build on the hardware
and device driver infrastructure. The I/O subsystem is also responsible for protecting itself from errant
processes and malicious users.
13.4.1 Input Output Scheduling
To schedule a set of I/O requests means to determine a good order in which to execute them. The order in
which applications issue system calls rarely is the best choice. Scheduling can improve overall system
performance, can share device access fairly among processes, and can reduce the average waiting time for
I/O to complete.
Operating-system developers implement scheduling by maintaining a wait queue of requests for each device.
When an application issues a blocking I/O system call, the request is placed on the queue for that device. The
I/O scheduler rearranges the order of the queue to improve the overall system efficiency and the average
response time experienced by applications. The operating system may also try to be fair, so that no one
application receives especially poor service, or it may give priority service for delay-sensitive requests.
One way in which the I/O subsystem improves the efficiency of the computer is by scheduling I/O operations.
Another way is by using storage space in main memory or on disk via techniques called buffering, caching,
and spooling.
13.4.2 Buffering
A buffer is a memory area that stores data while they are transferred between two devices or between a
device and an application. Buffering is done for three reasons. One reason is to cope with a speed mismatch
between the producer and consumer of a data stream. Suppose, for example, that a file is being received
via modem for storage on the hard disk. The modem is about a thousand times slower than the hard disk.
So a buffer is created in main memory to accumulate the bytes received from the modem. When an entire
buffer of data has arrived, the buffer can be written to disk in a single operation. Since the disk write is not
instantaneous and the modem still needs a place to store additional incoming data, two buffers are used.
After the modem fills the first buffer, the disk write is requested. The modem then starts to fill the second
buffer while the first buffer is written to disk. By the time the modem has filled the second buffer, the disk
write from the first one should have completed, so the modem can switch back to the first buffer while the
disk writes the second one. This double buffering decouples the producer of data from the consumer, thus
relaxing timing requirements between them.
13.4.3 Caching
A cache is a region of fast memory that holds copies of data. Access to the cached copy is more efficient
than access to the original. For instance, the instructions of the currently running process are stored on
disk, cached in physical memory, and copied again in the CPU's secondary and primary caches. The
difference between a buffer and a cache is that a buffer may hold the only existing copy of a data item,
whereas a cache, by definition, just holds a copy on faster storage of an item that resides elsewhere.
Caching and buffering are distinct functions, but sometimes a region of memory can be used for both
purposes. For instance, to preserve copy semantics and to enable efficient scheduling of disk I/O, the
operating system uses buffers in main memory to hold disk data. These buffers are also used as a cache, to
improve the I/O efficiency for files that are shared by applications or that are being written and reread
135
rapidly. When the kernel receives a file I/O request, the kernel first accesses the buffer cache to see
whether that region of the file is already available in main memory.
13.4.4 Spooling and Device Reservation
A spool is a buffer that holds output for a device, such as a printer, that cannot accept interleaved data
streams. Although a printer can serve only one job at a time, several applications may wish to print their
output concurrently, without having their output mixed together. The operating system solves this problem
by intercepting all output to the printer. Each application's output is spooled to a separate disk file. When
an application finishes printing, the spooling system queues the corresponding spool file for output to the
printer.
The spooling system copies the queued spool files to the printer one at a time. In some operating systems,
spooling is managed by a system daemon process. In others, it is handled by an in-kernel thread. In either
case, the operating system provides a control interface that enables users and system administrators to
display the queue, to remove unwanted jobs before those jobs print, to suspend printing while the printer
is serviced, and so on.
13.4.5 Error handling
An operating system that uses protected memory can guard against many kinds of hardware and application
errors, so that a complete system failure is not the usual result of each minor mechanical glitch. Devices and
I/O transfers can fail in many ways, either for transient reasons! as when a network becomes overloaded!
or for "permanent" reasons! as when a disk controller becomes defective. Operating systems can often
compensate effectively for transient failures. For instance! a disk read () failure results in a read () retry!
And a network send () error results in a resend ()! If the protocol so specifies. Unfortunately! if an important
component experiences a permanent failure, the operating system is unlikely to recover.
As a general rule, an I/O system call will return one bit of information about the status of the call, signifying
either success or failure. In the UNIX operating system, an additional integer variable named error number
is used to return an error code-one of about a hundred values-indicating the general nature of the failure
(for example, argument out of range, bad pointer, or file not open). By contrast, some hardware can
provide highly detailed error information, although many current operating systems are not designed to
convey this information to the application. For instance, a failure of a SCSI device is reported by the SCSI
protocol in three levels of detail: a sense key that identifies the general nature of the failure, such as a
hardware error or an illegal request; an additional sense code that states the category of failure, such as a
bad command parameter or a self-test failure; and an additional sense-code qualifier that gives even more
detail, such as which command parameter was in error or which hardware subsystem failed its self-test.
Further, many SCSI devices maintain internal pages of error-log information that can be requested by the
host-but that seldom are.
13.4.6 I/O Protection
Errors are closely related to the issue of protection. A user process may accidentally or purposefully
attempt to disrupt the normal operation of a system by attempting to issue illegal I/O instructions. We can
use various mechanisms to ensure that such disruptions cannot take place in the system. To prevent users
from performing illegal I/O, we define all I/O instructions to be privileged instructions. Thus, users cannot
issue I/O instructions directly; they must do it through the operating system.
13.4.7 Kernel Data Structure
The kernel needs to keep state information about the use of I/O components. It does so through a variety
of in-kernel data structures, such as the open-file table structure from Section 11.1. The kernel uses many
similar structures to track network connections, character-device communications, and other I/O activities.
UNIX provides file-system access to a variety of entities, such as user files, raw devices, and the address
spaces of processes. Although each of these entities supports a read () operation, the semantics differ. For
instance, to read a user file, the kernel needs to probe the buffer cache before deciding whether to perforn1
136
a disk I/O. To read a raw disk, the kernel needs to ensure that the request size is a multiple of the disk
sector size and is aligned on a sector boundary. To read a process image, it is merely necessary to
copy data from memory. UNIX encapsulates these differences within a uniform structure by using an
object-oriented technique.
13.5 Summary
The I/O system is modular; you can easily expand or customize it. The OS I/O system consists of
the following software components :
* The Kernel
* File Managers
* Device drivers
* The device descriptor
The kernel, file managers, and device drivers process I/O service requests at different levels. The
device descriptor contains information used to assemble the elements of a particular I/O subsystem. The
file manager, device driver, and device descriptor modules are standard memory modules.
* Management of I/O devices is a very important part of the operating system - so important and so
varied that entire I/O subsystems are devoted to its operation. (Consider the range of devices on a modern
computer, from mice, keyboards, disk drives, display adapters, USB devices, network connections, audio
I/O, printers, special devices for the handicapped, and many special-purpose peripherals.)
* I/O subsystems must contend with two (conflicting?) trends (1) The gravitation towards standard
interfaces for a wide range of devices, making it easier to add newly developed devices to existing systems,
and (2) the development of entirely new types of devices, for which the existing standard interfaces are not
always easy to apply.
* Device drivers are modules that can be plugged into an OS to handle a particular device or
category of similar devices.
13.6 Self Assessment Questions
1. What do you mean by I/O subsystem
2. Explain Following.
(i) Polling
(ii) Interrupts
(iii) Direct Memory Access
3. What do you mean by Kernel input output sub system. Explain following:
(i) Buffering
(ii) Spooling
(iii) Catching
ȇȇȇ
137
Unit - 14 : System Protection
138
the policies for a resource usage may vary dependency upon an application, therefore, protection
mechanisms should be provided as tools for the enforcement of these varying policies.
14.3 Principles of Protection
Frequently, a guiding principle can be used in the design of an operating system. This principle simplifies
design decisions and keeps the system consistent and easy to understand. A key time-tested guiding
principle for protection is the principle of least privilege. It dictates that programs, users and even systems
be given just enough privileges to perform their tasks.
Consider the analogy of a security guard with a passkey. It this key allows the guard into just the public
areas that she guards, then misuse of the key will result in minimal damage. If however, the passkey allows
access to all areas, then damage from its being lost, stolen, misused, copied or otherwise compromised
will be much greater.
An operating system following the principle of least privilege implements its features, programs, system
calls, and data structures so that failure or compromise of a component does the minimum damage and
allows the minimum damage to be done. The overflow of a buffer in a system daemon might cause the
daemon to fail, for example, but should not allow the execution of code from the process's stack that
would enable a remote user to gain maximum privileges and access to the entire system (as happens too
often today.)
Managing users with the principle of least privilege entails creating a separate account for each user, with
just the privileges that the user needs. A operator who needs to mount tapes and backup files on the system
has access to just those commands and files needed to accomplish the job. Some systems implement role-
based access control (RBAC) to provide this functionality.
Computers implemented in a computing facility under the principle of least privilege can be limited to
running specific services, accessing specific remote hosts via specific services, and doing so during specific
time.
The principle of least privilege can help produce a more secure computing e environment. Unfortunately, it
frequently does not. For example, Windows 2000 has a complex protection scheme at its core and yet has
many security holes. By comparison, Solaris is considered relatively secure, even though it is a variant of
UNIX, which historically was designed with little protection in mind. One reason for the difference may be
that Windows 2000 has more lines of code and more services than Solaris and thus has more to secure
and protect. Another reason could be that the protection scheme in Windows 2000 is incomplete or
protects the wrong aspects of the operating system, leaving other area vulnerable.
14.4 Domain of Protection
A computer system is a collection of processes and objects. By objects, we mean both hardware objects
(such as the CPU, memory segments , printers, disks, and tape drives) and software objects (such as files,
programs, and semaphores). Each object has a unique name that differentiates it from all other objects in
the system, and each can be accessed only through well-defined and meaningful operations. Objects are
essentially abstract data types.
The operations that are possible may depend on the object. For example a CPU can only be executed on.
Memory segments can be read and written, whereas a CD-ROM or DVD-ROM can only be read. Tape
drives can be read, written, and rewound. Data files can be created, opened, read, written, closed, and
deleted; program files can be read, written, executed, and deleted.
A process should be allowed to access only those resources for which it has authorization, Furthermore, at
any time, a process should be able to access only those resources that it currently requires to complete its
task. This second requirement, commonly referred to as the need-to-know principle, is useful in limiting
the amount of damage a faulty process can cause in the system. For example, when process p invokes
139
procedure A(), the procedure should be allowed to access only its own variables and the formal param-
eters passed to it, it should not be able to access all the variables of process p. Similarly, consider the case
where process p invokes a compiler to compile a particular file. The compiler should not be able to access
files arbitrarily but should have access only to a well-defined subset of files (such as the source file, listing
file, and so on) related to the file to be compiled. Conversely, the compiler may have private files used for
accounting or optimization purposes that process p should not be able to access. The need to-know
principle is similar to the principle of least privilege, in that the goals of protection are to minimize the risks
of possible security violations.
14.4.1 Domain Structure
To facilitate this scheme, a process operates within a protection domain which specifies the resources that
the process may access. Each domain defines a set of objects and the types of operation that may be
invoked on each object. The ability to execute an operation on an object is an access right. A domain is a
collection of access rights, each of which is an ordered pair <object-name, rights-set>. For example, if
domain D has the access right <file F, {read, write}>, then a process executing in domain D can both read
and write file F; it cannot, however, perform any other operation on that object.
141
user-id of the process is set to A. This temporary user-id change ends when the process accessing the file
exits or when the user logs off, whichever is earlier. When the domain bit is on, the user-id is not changed to
A and remains B i.e. owner of the file. This implies that file is used by B and thus, should not be used by A.
14.5.2 Access Matrix
The access matrix model is the policy for user authentication and has several implementations such as
access control lists (ACLs) and capability lists. It is used to describe which users have access to what
object (resources). Thus, basically it checks the access rights of users on system resources. The access
matrix model consists of four major parts :
- A list of objects
- A list of subjects
- A function T that returns n object's type
The matrix itself, with the objects (resources) making the columns and the subjects making the rows.
In the cells where a subject and object meet, lie the rights the subject has on that object. Some example
access fights (as discussed in previous sub-section) are read, write, execute, list and delete. Figure 14.2
shows an example access matix.
143
They are grouped into classes. For to in a capability list cannot be
example, in UNIX, there are three even named.
Classes: self, group, anybody else.
3. The default is : The default is :
Everyone should be also to access a file. No one should be able to access a
a file unless they have been given a
capability
4. Access lists are simple and are used Capabilities are used in systems that
In almost all file systems. Need to be very secure as if
prohibits sharing of information
unless access is given to a subject.
14.6 Revocation of Access Rights
In a dynamic protection system, we may sometimes need to revoke access right to objects shared
by different users. Various questions about revocation may arise.
The access list is searched for any access rights to be revoked, and they are deleted from the list.
Revocation is immediate and can be general or selective, total or partial, and permanent or temporary.
Capabilities, however, present a much more difficult revocation problem. Since the capabilities are
distributed throughout the system, we must find them before we can revoke them. Schemes that implement
revocation for capabilities include the following:
- Reacquisition: Periodically, capabilities are deleted from each domain. If a process wants to use a
capability, it may find that that capability has been deleted. The process may then try to reacquire the
capability. It access has been revoked, the process will not be able to reacquire the capability.
- Back-pointers: A list of pointers is maintained with each object, pointing to all capabilities
associated with that object.
- Indirection: The capabilities point indirectly, not directly, to the objects Each capability points to a
unique entry in a global table, which in turn points to the object. We implement revocation by searching the
global table for the desire entry and deleting it. Then, when an access is attempted, the capability and its table
entry must match. This scheme was adopted in the CAL system. It does not allow selective revocation.
- Keys: A key is a unique bit pattern that can be associated with a capability. This key is defined
when the capability is created, and it can be neither modified nor inspected by the process owning the
capability. A master key is associated with each object; it can be defined or replace with the set-key
operation. When a capability is created, the current value of the master key is associated with the capability.
When the capability is exercised, its key is compared with the master key. If the keys match, the operation
is allowed to continue; otherwise, an exception condition is raised. Revocation replace the master key with
a new value via the set-key operation, invalidating all previous capabilities for the object.
This scheme does not allow selective revocation, since only one master key is associated with each object.
If we associate a list of keys with each object, then selective revocation can be implemented. Finally, we
can group all keys into one global table of keys. A capability is valid only if its key matches some key in the
global table. We implement revocation by removing the matching key from the table. With this scheme, a
key can be associated with several objects, and several keys can be associated with each object,
providing maximum flexibility.
144
In key-based schemes, the operations of defining keys, inserting them into lists and deleting them from lists
should not be available to all users in particular, it would be reasonable to allow only the owner of an object
to set the keys for that object. This choice, however, is a policy decision that the protection system can
implement but should not define.
14.7 Summary
Computer system objects need to be protected from misuse. Objects may be hardware (memory, CPU &
I/O devices) or software (files, programs & semaphores) An access right is permission to perform an
operation on an object. A domain is a set of access rights. Processes execute in domains. The access
matrix is a general model of protection that provides a mechanism for protection without imposing a
particular policy on the system or its users. Revocation of access rights in a dynamic protection model is
typically easier to implement with an access list scheme then with a capability list.
14.8 Self Assessment Questions
1. Discuss the strengths and weakness of implementing an access matrix using access list that are
associated with objects.
2. Compare capability lists and access lists.
3. Differentiate between static and dynamic association of a process with a domain.
4. What is the “need-to-know” principle? Why is it important for protection system to adhere to this
principle?
5. Suppose that you share a file on a network with fellow student so that he can copy this file from
your system. What right should he be given on this file? What will be the contents of cell of access
matrix corresponding to this grant?
6. Discuss access control mechanism in context to data files.
7. What is a domain? What are access hierarchies and protection rings.
145
Unit - 15 : System Security
Structure of the Unit
15.0 Objective
15.1 Introduction
15.1.1 Need for Security
15.1.2 Principles of Security
15.2 Authentication
15.2.1 Passwords
15.2.2 Artifact based Authentication
15.2.3 Biometrics Techniques
15.3 Encryption
15.4 Program & System threats
15.4.1 Virus
15.4.2 Worms
15.4.3 Trojan horse
15.4.4 Trap Doors
15.4.5 Logic Bomb
15.4.6 Port Scanning
15.4.7 Stack & Buffer Overflow
15.4.8 Denial of services
15.5 Computer Security Classification
15.6 Summary
15.7 Self Assessment Questions
15.0 Objective
After studying this unit you will be able to understand concept of security and its priciples and need. You
will learn about methods of authentication, threats to computer system and classification of security
system. This chapter provides a general overview of
• Security concepts
• Authentication and Encryption
• Program and System Threats
• Computer Security Classification
15.1 Introduction
Security is an important aspect of any OS. In general secure system will control, through use of
specific security features, access of to information that only properly authorized individual or processes
operating on their behalf will have access to read, write, create or delete.
Security Violation (or misuse) of the system can be categorized as intentional or accidental. It is
easier to protect against accidental misuse than against malicious misuse.
146
15.1.1 Need for Security
There in no or very little security in computer application, until the importance of data was truly
realized. The computer applications for financial and personal data were developed, real need for security
was felt & people realized that data on computers are extremely important aspect of modern life. Two
typical examples of such security mechanism were as follows:
- Provide a user id & Password to every user and use that information to authenticate a user.
- Encode information stored in fue databases in some fashion so that it is not visible to users who do
not have right permission.
As technology improved, fue communication infrastructure became extremely mature and newer and newer
application behaves to be developed for various user demands and needs. People realized that basic
security, measurers are not quite enough.
15.1.2 Principles of Security
These are four chief principles of security Authentication, Confidentiality, Non-Repudiation and Integrity.
There are two more, Access control & Availability which are not related to particular message, but are
linked to overall system security as whole.
a. Authentication :
This mechanism helps to establish proof of identity. The origin of an electronic message or document is
correctly identified.
Pose
Fabrication
User C
User C
Interception
Transfer Rs User C
1000 to D Transfer Rs
1000 to C
Actual route of Message
Figure 15.4 : Loss of integrity
Here user C tempers with message and send changed message to user B (Bank). User B has no
way of knowing that contents charged. This type of attack is modification. Modification Courses Loss of
message integrity.
e. Access Control : This determines who should be able to access what. For example user A can
read to file X, Write to Y, Z but can only update P, Q .
This is broadly related in two areas :
- Role management (user side) : Which user can do what?
- Rule management (resource side) : which resource is available in what circumstances/
“Access Control specified who can access what”
f. Availability : This states that resources (i.e. information) should be available to authorize parties
at all times.
User A Server B
User C
Alogrithm and
Decryption key
Figure 15.6 : Enceyption & Decryption Mechanism
150
15.4 Program and System Threats
15.4.1 Virus
Attacker can launch application level or network level attack using virus. A virus is a computer program
that attaches it self to another authorized program and causes damage to the computer system or to the
network. A virus can be repaired (antivirus used) and its damage can be controlled by using good backup
and recovery procedures.
During its life time, A virus goes through four phases :
a. Dormant Phase : Here virus idle, activated based on certain action / event / date-time / key stroke
occurs this is optional phase.
b. Propagation Phase : In this phase virus copies itself and each copy starts creating more copies of
self, them propagating virus.
c. Triggering Phase : A dormant virus moves into this phase when action / event for which it was
waiting is initiated.
d. Execution Phase : this is actual work of virus, which could be destructive (delete files) or harmless
(display some message).
Virus can be classified into following categories :
a. Parasitic Virus : Common type, attaches itself to executable files and keeps replicating.
b. Memory resident Virus : Attaches itself to area of main memory then infect every executables.
c. Boot sector Virus : infects master boot record of disk and spreads when Operating System starts
booting computer.
d. Stealth Virus : Built in intelligence, prevents antivirus software from detecting it.
e. Polymorphic Virus : Virus keeps on changing its signature (identity) on very execution; Hence
difficult to detect.
f. Metamorphic Virus : In addition to changing signature, this virus keeps rewriting itself every time
detection much harder.
15.4.2 Worm
Worm is a piece of code which gets replicated. It is similar to virus but its implementation is different. A
virus modifies a program / data while warm does not modify. It replicates again & again.
Perform resource
eating taks