Ics 2270 Os

Download as pdf or txt
Download as pdf or txt
You are on page 1of 29

ICS2270: OPERATING SYSTEMS

COURSE OUTLINE
Objectives
 Enable the student understand the operating systems in the computer system.
 Enable student identify various functions and roles of the operating systems in the computer system.

No. Topic Sub-topic


1. Introduction to Operating  Parts of an operating system
Systems  Operating system main objectives
 Categories of operating systems
2. Operating Systems Functions  User interface
 Processor, memory, input/output, file management
 Abstraction
 Security administration
3. File Systems  Files and Directories, keeping track of files
 File system manipulation
 File control blocks
4. Process Coordination and  Process states, process control blocks
Synchronization  Process Operations and synchronization
5. Assessment 1 Revision and Continuous Assessment Test 1
6. Inter Process Communication  Why do processes intercommunicate
 Race conditions, critical sections
 Mutual exclusions conditions
7. CPU/Process Scheduling  Process scheduling
 Preemptive, non-preemptive scheduling
 Scheduling algorithms
8. Assessment 2 Revision and Continuous Assessment Test 2
9. Deadlocks  Deadlock defined, Illustration of Deadlocks
 Pre-emptable and Non pre-emptable Resources
 Necessary and sufficient deadlock conditions
 Dealing with deadlock problem
 Deadlock prevention, avoidance detection
10. Assessment 3 Assignment and Revision

Assessment:
 Continuous Assessment Tests 20%
 Assignments 10%
 End of Semester Exam 70%
Total 100%

References:
1 ChaudhuryP (2006);Operating Systems: Principles and Design(5thEdition); New York, USA
2. Tanenbaum A. S.(2006);Operating Systems; Design and Implementation. (2nd Ed); McGraw Hill
3.Galvin B & Gagne G.(2005);Operating System Concepts Peter . (2nd Ed); McGraw Hill
4. The Internet
1.0 Operating System:- Introduction

1. An operating system is “the software that controls the hardware”.

2. An operating system (OS) is software that controls a computer. It manages hardware, runs
applications, provides an interface for users, and stores, retrieves, and manipulates files. In
general, you can think of an operating system as the middleman between applications and
hardware, between the user and hardware, and between the user and applications

3. An operating system is the low-level software that enables a user and higher-level application
software to interact with a computer’s hardware and the data and other programs stored on the
computer. An OS performs basic tasks, such as recognizing input from the keyboard, sending
output to the display screen, keeping track of files and directories on the disk, and controlling
peripheral devices such as printer

4. An operating system is the software that controls the hardware by making the hardware useable by
providing an interface (or translator) between the application software (e.g. word processor) and
the hardware (i.e. tells the hardware what to do and how to communicate between the parts).
After being initially loaded into the computer by a boot program, it manages all the other
programs in the computer.

User

Application s/w Application s/w Application s/w

Operating system

Hardware

1
The operating system is the most important type of system software in a computer system. Without an
operating system, application programs will not run on the computer hardware.
Controlling the computer involves software at several levels. Differentiate kernel services, library
services, and application-level services, all of which are part of the operating system. Processes run
Applications, which are linked together with libraries that perform standard services. The kernel supports
the processes by providing a path to the peripheral devices. The kernel responds to service calls from the
processes and interrupts from the devices. The core of the operating system is the kernel, a control
program that functions in privileged state (an execution context that allows all hardware instructions to
be executed), reacting to interrupts from external devices and to service requests and traps from processes.
Generally, the kernel is a permanent resident of the computer. It creates and terminates processes and
responds to their request for service.
Operating Systems are resource managers. The main resource is computer hardware in the form of
processors, storage, input/output devices, communication devices, and data. Some of the operating system
functions are: implementing the user interface, sharing hardware among users, allowing users to share
data among themselves, preventing users from interfering with one another, scheduling resources among
users, facilitating input/output, recovering from errors, accounting for resource usage, facilitating parallel
operations, organizing data for secure and rapid access, and handling network communications.
Finally, the resources in a computer (CPU, memory, disk space) are limited. The operating system
must act as a resource manager, optimising the use of the resources, and protecting them against misuse
and abuse. When a system provides multiuser or multitasking capabilities, resources must be allocated
fairly and equitably amongst a number of competing requests.

Kernel Mode and User Mode


Because an operating system must hide the computer's hardware, and manage the hardware
resources, it needs to prevent the application software from accessing the hardware directly. Without this
sort of protection, the operating system would not be able to do its job.
The computer's CPU provides two modes of operation which enforce this protection. The
operating system runs in kernel mode, also known as supervisor mode or privileged mode. In kernel
mode, the software has complete access to all of the computer's hardware, and can control the switching
between the CPU modes. Interrupts are also received in the kernel mode software.
The rest of the software runs in user mode. In this mode, direct access to the hardware is
prohibited, and so is any arbitrary switching to kernel mode. Any attempts to violate these restrictions are
reported to the kernel mode software: in other words, to the operating system itself. Programs running in
user mode are given an address space, visible only to themselves, which contains enough memory for
them to do their job.
By having two modes of operation which are enforced by the computer's own hardware, the
operating system can force application programs to use the operating system's abstract services, instead of
circumventing any resource allocations by direct hardware access.

Parts of an Operating System


An operating system for a large-scale computer that is used by many people at once is a very complex
system. It contains many millions of lines of instructions (commands that the computer executes) written
by programmers. To make operating systems easier to write, they are constructed as a series of modules
(programs), each module responsible for one function. Typical modules in a larger multi-user operating
system could be,
 Kernel (also known as the executive)
 Process manager
 Scheduler
 File manager

2
Main Objectives of Operating Systems

Modern Operating systems generally have following three major goals. Operating systems generally
accomplish these goals by running processes in low privilege and providing service calls that invoke the
operating system kernel in high-privilege state.

 To hide details of hardware by creating abstraction


An abstraction is software that hides lower level details and provides a set of higher-level
functions. An operating system transforms the physical world of devices, instructions, memory,
and time into virtual world that is the result of abstractions built by the operating system. There
are several reasons for abstraction.
First, the code needed to control peripheral devices is not standardized. Operating systems provide
subroutines called device drivers that perform operations on behalf of programs for example,
input/output operations.
Second, the operating system introduces new functions as it abstracts the hardware. For instance,
operating system introduces the file abstraction so that programs do not have to deal with disks.
Third, the operating system transforms the computer hardware into multiple virtual computers,
each belonging to a different program. Each program that is running is called a process. Each
process views the hardware through the lens of abstraction.
Fourth, the operating system can enforce security through abstraction.

 To allocate resources to processes (Manage resources)


An operating system controls how processes (the active agents) may access resources (passive
entities).

 Provide a pleasant and effective user interface


The user interacts with the operating systems through the user interface and usually interested in
the “look and feel” of the operating system. The most important components of the user interface
are the command interpreter, the file system, on-line help, and application integration. The recent
trend has been toward increasingly integrated graphical user interfaces that encompass the
activities of multiple processes on networks of computers.

One can therefore view operating systems from two points of view: Resource manager and Extended
machine. From Resource manager point of view, operating systems manage the different parts of the
system efficiently and from extended machines point of view, operating systems provide a virtual
machine to users that is more convenient to use. The structurally operating systems can be designed as a
monolithic system, a hierarchy of layers, a virtual machine system, or using the client-server model. The
basic concepts of operating systems are processes, memory management, I/O management, the file
systems, and security.

3
Categories of Operating Systems

Every operating system is different, and each is designed to meet a set of goals. However, operating
systems can generally be classified into the following categories.

(a) Personal Computer Operating System


A personal computer operating system is designed to provide a good environment to a single user at a
time. User interface elements (desktops, GUIs etc.) are important here. Response time and the user
experience is often more important than efficient computer utilisation. Windows XP and MacOS X are
examples.

(b) Mainframe Operating System


A mainframe operating system runs on mainframes, which provide immense I/O and availability.
They may provide a batch environment: jobs are segregated into batches with similar requirements. Each
batch is given to the computer to run. When jobs with similar system requirements are batched together,
this helps to streamline their processing.
Alternatively, the system may provide transaction processing, where hundreds or thousands of
small requests must be performed every second.
User interaction is generally lacking in these systems, as the emphasis is on the computer's
utilisation.
An example mainframe system is IBM's OS/390.

(c) Server Operating System


A server operating system runs on servers. The aim here is to provide services to many users
simultaneously.
Services may include applications, file storage, print and other networking operations. The emphasis here
is on user response time as well as computer utilisation. Server systems are usually more general-purpose
than the other types of operating systems. Examples of multiprogramming systems are Linux, UNIX and
Windows 2000.

(d) Real-Time System


A real-time system is designed to respond to input within certain time constraints. This input usually
comes from external sensors, and not from humans. Thus, there is usually little or no user interaction.
Many embedded systems are also real-time systems.

(e) Embedded Operating System


An embedded operating system usually has the operating system built into the computer (component), and
is used to control external hardware. There is little or no application software in an embedded system.
Examples here are the PDAs that everybody seems to have, and of course the computers built into DVDs,
microwaves, and into most cars. Windows CE is an example of an embedded operating system.

4
Other Categorization of Operating Systems

Within the broad family of operating systems, there are several types of operating systems, categorized
based on the types of computers they control and the sort of applications they support. The categories are:

(a) Single-user, single task - As the name implies, this operating system is designed to manage the
computer so that one user can effectively do one thing at a time. The Palm OS for Palm handheld computers
(PDA) is a good example of a modern single-user, single-task operating system.

(b) Single-user, multi-tasking - This is the type of operating system most people use on their desktop
and laptop computers today. Microsoft's Windows and Apple's MacOS platforms are both examples of
operating systems that will let a single user have several programs in operation at the same time. For
example, it's entirely possible for a Windows user to be writing a note in a word processor while
downloading a file from the Internet while printing the text of an e-mail message.

(c) Multi-user, multi-tasking - A multi-user operating system allows many different users to take
advantage of the computer's resources simultaneously. The operating system must make sure that the
requirements of the various users are balanced, and that each of the programs they are using has sufficient
and separate resources so that a problem with one user doesn't affect the entire community of users. Unix,
VMS and mainframe operating systems, such as MVS, are examples of multi-user operating systems.

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

(e) Process Control: - Generally implies the control by computer of an industrial process such as the
refining of oil, the manufacture of machine tools or even the monitoring of a patient’s condition in a
hospital. The common feature of all these applications is feedback i.e. the computer receives input from
the controlled process, computes a response which will maintain its stability, and initiates the mechanism
for giving it. For example, if the input specifies a dangerous rise in temp, then the response may be to
open a valve in order to raise the flow of coolant. The main function of the OS in process control is to
provide maximum reliability (i.e. avoid faults, tolerate faults and detect and recover from faults) with the
minimum of operator intervention, and to “fail safe” in the event of any h/w malfunctions.

(f) Timesharing
This strategy supports multiple interactive users. Rather than preparing a job for execution ahead of time,
users establish an interactive session with the computer and then provide commands, programs and data
as they are needed during the session.

5
2.0 FUNCTIONS OF OPERATING SYSTEM.

An operating system has a complex mixture of diversified functions. Operating systems is the resource
manager. At the simplest level, an operating system does two things:

 One, it manages the hardware and software resources of the system. In a desktop computer, these
resources include such things as the processor, memory, disk space and more (On a cell phone, they
include the keypad, the screen, the address book, the phone dialer, the battery and the network
connection).
 It provides a stable, consistent way for applications to deal with the hardware without having to know
all the details of the hardware

The main resource is computer hardware in the form of processors, storage, input/output devices,
communication devices, and data. Some of the functions are: implementing the user interface, sharing
hardware among users, allowing users to share data among themselves, preventing users from interfering
with one another, scheduling resources among users, facilitating input/output, recovering from errors,
accounting for resource usage, facilitating parallel operations, organizing data for secure and rapid access,
and handling network communications.
The operating systems generally accomplish these goals by running processes in low privilege and
providing service calls that invoke the operating system kernel in high-privilege state.

2.1 User Interface.


The operating system provides each user with an interface that accepts, interprets and executes user
commands or programs. This interface is commonly called a SHELL or command line interpreter (CLI).
In some systems this might be a simple text mode line-by-line entry using keywords (such as MSDOS or
UNIX), in other systems it might be highly graphical using windows and a pointing device such as a
mouse (such as X-Windows). (SHELL sometimes refers to the rest of the OS except the kernel. Kernel is
also known as real-time executive. Why?)
The operating system provides a convenient interface between a computer and its users. In the
case of personal computers and workstations, graphical user interfaces--such as windows and icons
displayed on a monitor--are convenient for users. A window is a rectangular area on a monitor that
displays a file or part of a file. Many applications can be run concurrently, each in a different window. An
icon is a small picture displayed on a monitor, representing an object that can be manipulated by a user,
typically by clicking on the icon with a hand-controlled device such as a mouse. An icon is essentially a
visual mnemonic (memory aid) for a specific command. For example, an icon depicting a trash can is
used to delete files, programs, and disks.
The user interacts with the operating systems through the user interface and usually interested in
the “look and feel” of the operating system. The most important components of the user interface are the
command interpreter, the file system, on-line help, and application integration. The recent trend has been
toward increasingly integrated graphical user interfaces that encompass the activities of multiple
processes on networks of computers.
The task of providing a consistent application interface, is especially important if there is to be
more than one type of computer using the operating system, or if the hardware making up the computer is
ever open to change. A consistent application program interface (API) allows a software developer to
write an application on one computer and have a high level of confidence that it will run on another
computer of the same type, even if the amount of memory or the quantity of storage is different on the
two machines.
6
Even if a particular computer is unique, an operating system can ensure that applications continue
to run when hardware upgrades and updates occur. This is because the operating system -- not the
application -- is charged with managing the hardware and the distribution of its resources. One of the
challenges facing developers is keeping their operating systems flexible enough to run hardware from the
thousands of vendors manufacturing computer equipment. Today's systems can accommodate thousands
of different printers, disk drives and special peripherals in any possible combination.

2.2 Processor Management.


Users submit programs or program segments to the computer for processing, requesting different
processors and peripheral equipment. The operating system properly schedules the operations of the
hardware in an efficient manner. If a computer were to run each program one at a time until completion,
the other programs would have to wait idly. In order to finish all programs in a reasonably short and
equitable amount of time, the operating system runs portions of each program in turn, continually
assigning new programs to the processors. This type of computer operation is called time-sharing, in
contrast to batch processing, in which entire programs are run sequentially. If an anomaly--such as a
hardware malfunction, an overflow in addition, or the completion of an operation of the I/O unit--occurs,
the operating system interrupts processing. The operating system takes an appropriate measure and then
resumes processing.

Since the operating system manages many kinds of activities ranging from user programs to
system programs like printer spooler, file server etc., each of these activities is encapsulated in a process.
A process includes the complete execution context (code, data, PC, registers, OS resources in use etc.).
It is important to note that a process is not a program. A process is only ONE instant of a program in
execution. There are many processes that can be running in the same program. The five major activities of
an operating system in regard to process management are

 Creation and deletion of user and system processes.


 Suspension and resumption of processes.
 A mechanism for process synchronization.
 A mechanism for process communication.
 A mechanism for deadlock handling.

2.3 Memory Management.


A computer generally has a mixture of memory types--such as main memory (RAM, ROM), cache, hard
disks, and flush disks--each with a different size and access time. Programs often cannot be stored in their
entirety in the main memory. Thus, they are stored in hard disks that have a large capacity but a slower
access time, and then appropriate segments of the program are transferred for fast execution to the main
memory. Small parts of these segments are further transferred into cache. For fast and reliable processing
of a large number of programs, the operating system must constantly shuffle around the program
segments and the intermediate processing results among these memories.
In some computers, users can access hard disks as an extension of the main memory without being
aware of the distinction between the two levels of storage. Users can then freely employ far greater
memory space than is available in the main memory. This is achieved by treating the memory addresses
of the hard disks as a consecutive extension of the memory addresses of the main memory. This technique
7
is called virtual memory and is managed by the operating system. Paging and segmentation are two
common methods of realizing virtual memory. Blocks of either fixed size (pages) or variable size
(segments) are moved as needed from hard disks to the main memory. As the blocks are moved in, their
virtual addresses are mapped to real (physical) addresses in the main memory.

Note that primary (main) memory is a large array of words or bytes. Each word or byte has its own
address. Main-memory provides storage that can be accessed directly by the CPU. That is to say for a
program to be executed (run), it must in the main memory.

The major activities of an operating in regard to memory-management are:

 Keep track of which part of memory are currently being used and by whom.
 Decide which processes are loaded into memory when memory space becomes available.
 Allocate and de-allocate memory space as needed.

2.4 Input/output Management.

There are all kinds of I/O devices in the computer system. The management of these devices is one of the
most complex aspect of operating system design. This is because I/O devices may differ from one
another on all kinds of aspects. Some are human readable, suitable for communicating with the
computer users, such as printers, monitors, speakers, keyboards, and mice. Some are machine readable,
suitable for communicating with electronic equipment such as disks and tapes. Some others are otherwise
used for communicating with remote devices, such as modems and network cards.
The above categories are obtained based on the functions of I/O devices. What’s more, even those
that fall into one category may still be different from one another in various aspects. They may have
different data transferring speed, different complexity of control, printing density, control mechanism, and
other variables etc.

2.5 File Management

The operating system is also responsible for keeping track of the files and directories that reside on hard
or floppy disks.
A file is a collection of related information defined by its creator. Computer can store files on the disk
(secondary storage), which provide long term storage. Some examples of storage media are magnetic
tape, magnetic disk and optical disk. Each of these media has its own properties like speed, capacity, data
transfer rate and access methods.
A file system is normally organized into directories to ease their use. These directories may
contain files and other directories.

The major activities of an operating system in regard to file management are

1. The creation and deletion of files.


2. The creation and deletion of directories.
3. The mapping of files onto secondary storage.
4. The backup of files on stable storage media.
8
File control blocks (FCB), sometimes referred to as file descriptors, are data structures that hold
information about a file. When an operating system needs to access a file, it creates an associated file
control block to manage the file.
The structure of the file control block differs between operating systems, but most file control
blocks include the following parts
 Filename
 Location of file on secondary storage
 Length of file
 Date and time or creation or last access

2.6 Management of Communication.

The operating system manages communication among computers connected on a network. Since data
exchanged on the network may be received intermittently instead of continuously, they must be combined
by the operating system. The received data is then converted into a form that computers can process. The
operating system also keeps track of the status of the network, disconnecting faulty portions, reporting
computer usage accounts, and so on.
As local-area networks (LANs) continue to become more widely used, management of distributed
processing--i.e., execution of computational tasks by a LAN with many computers and peripheral
equipment--is becoming increasingly important. Many users (clients) on a LAN need to share data, files,
or printers, and they send their requests to servers or central computers. When a network contains many
LANs, it is a challenging task to keep track of which of the many LANs on the network use which servers
because of the network's constantly changing nature.
The communication-network design must consider routing and connection strategies, and the
problems of contention and security.

2.7 Security.

The operating system protects computers from access by illegal users and from data corruption introduced
by unintentional mistakes made by legitimate users. Security is particularly important for computers that
are connected to a communications network, because many users can freely access any computer.
Authorized users are authenticated by entering an individual password, and then a computer usage fee is
charged (if need be) to the account of each user.
If a computer systems has multiple users and allows the concurrent execution of multiple
processes, then the various processes must be protected from one another's activities. Protection refers to
mechanism for controlling the access of programs, processes, or users to the resources defined by a
computer systems.

9
2.8 Abstraction: Hide Details of Hardware

An abstraction is software that hides lower level details and provides a set of higher-level functions. An
operating system transforms the physical world of devices, instructions, memory, and time into virtual
world that is the result of abstractions built by the operating system. There are several reasons for
abstraction.

First, the code needed to control peripheral devices is not standardized. Operating systems provide
subroutines called device drivers that perform operations on behalf of programs for example, input/output
operations.
Second, the operating system introduces new functions as it abstracts the hardware. For instance,
operating system introduces the file abstraction so that programs do not have to deal with disks.
Third, the operating system transforms the computer hardware into multiple virtual computers, each
belonging to a different program. Each program that is running is called a process. Each process views the
hardware through the lens of abstraction.
Fourth, the operating system can enforce security through abstraction.

3.0 FILE SYSTEM

In a computer, a file system is the way in which files are named and where they are placed logically
for storage and retrieval. The DOS, Windows, OS/2, Macintosh, and UNIX-based operating systems all
have file systems in which files are placed somewhere in a hierarchical (tree) structure. A file is placed in
a directory (folder in Windows) or subdirectory at the desired place in the tree structure.
File systems specify conventions for naming files. These conventions include the maximum
number of characters in a name, which characters can be used, and, in some systems, how long the file
name suffix can be. A file system also includes a format for specifying the path to a file through the
structure of directories.

Files
Files are storage areas for programs, source code, data, documents etc. They can be accessed by
processes, but don't disappear when processes die, or when the machine is turned off. They are thus
persistent objects. Operating systems provide mechanisms for file manipulation, such as open, close,
create, read and write. As part of the job of hiding the hardware and providing abstract services, the
operating system must map files onto areas on disks and tapes (or any other storage). The operating
system must also deal with files that grow or shrink in size.
Some operating systems don't enforce any structure to files, or enforce particular file types. Others
distinguish between file types and structures, e.g Java source files, text documents, executable files, data
files etc. Most operating systems allow files to have permissions, allowing certain types of file access to
authorized users only.

Directories may exist to allow related files to be collected. The main reason for the existence of
directories is to make file organization easier and more flexible for the user.
10
File System Manipulation

The output of a program may need to be written into new files or input taken from some files. The
operating system provides this service. The user does not have to worry about secondary storage
management. User gives a command for reading or writing to a file and sees his/her task accomplished.
Thus operating system makes it easier for user programs to accomplish their task.
This service involves secondary storage management. The speed of I/O that depends on secondary
storage management is critical to the speed of many programs and hence it is best relegated to the
operating systems to manage it than giving individual users the control of it. It is not difficult for the user-
level programs to provide these services but for the above mentioned reasons it is best if this service is left
with operating system.
For most users, the file system is the most visible aspect of an operating system, apart from the
user interface. Files store programs and data.
The operating system implements the abstract `file' concept by managing I/O devices, for
example, hard disks. Files are usually organised into directories to make them easier to use. Files also
usually have some form of protections.
The file system provides: (1) An interface to the file objects for both programmers and users and
(2) The policies and mechanisms to store files and their attributes on permanent storage devices.

System Files
System files are files that the OS requires in order to function properly. Each system file has a
function that alters, manages, or runs part of the OS or other system files. Contrast this with a data or
application file, such as a saved game, document, or image file. The existence of an application file will
not alter the behavior of the OS. You can change, create, or delete a non-system file without affecting the
performance of the computer.
In most cases, a deleted, missing, or corrupt system file will result in an improperly functioning
OS feature or an OS that will not function at all. A thorough knowledge of the OS system files and their
functions will allow you to pinpoint and resolve OS problems based on the behavior of the computer or
on the error messages you receive.
Many of the OS system files are location-dependent. That is, they will not be loaded and properly
executed if they are not in a specific location. For this reason, and to keep them from being accidentally
deleted, most system files are hidden. This means that they will not be displayed in the My Computer or
Windows Explorer navigation windows.

File Control Blocks

File control blocks (FCB), sometimes referred to as file descriptors, are data structures that hold
information about a file. When an operating system needs to access a file, it creates an associated file
control block to manage the file.
The structure of the file control block differs between operating systems, but most file control
blocks include the following parts

 Filename
 Location of file on secondary storage
 Length of file
 Date and time or creation or last access
11
Keeping track of files

The hard disk is comprised of a large number of sequentially numbered sectors. As files are created, free
sectors are allocated to hold the file contents and marked as allocated.
To keep track of the sectors and whether they are allocated or free, and to which file they belong, the
operating system maintains a number of tables.
When the operating system is first installed, it creates a root file system on the disk that specifies
how many sectors are available and how they will be allocated.
The root file system is a table of entries like a directory. In general, this is a fixed size, and once
full, no more entries can be added.
Each entry can be either a file or another directory table. The following table depicts this structure.

The root file system is highly operating system specific, but an entry might look like,
 Name of file
 Beginning cluster number
 Length of file in bytes
 Type of file
 Creation date and last modified right
 File permissions (an access control list)

12
4.0 PROCESS COORDINATION & SYNCHRONIZATION

Introduction
Processes in a computer system execute programs to manipulate data. One process may or may not
interact with another process to accomplish its task. Such interactions take place only through shared
data. If two processes use two distinct sets of data items, then they are not considered interacting
processes and their activities do not affect each other. Their behaviors are independent and do not
influence each other. If a process A modifies a data item that another process B reads, then the behaviour
of B may depend on activities of A.

Here we say the two processes are interacting with one another through shared data. The
behaviour of these interacting processes then depends on two factors: the relative speeds of the processes
(what they do with shared data) and how they access the shared data. If the activities of interacting
processes are not controlled suitably, their behaviour may not be as “consistent” as expected from their
specifications.
The theme of process synchronization is the coordination of interacting processes, that is, the
orderly execution of their accesses to shared data. Unlike interprocess communication (discussed later),
process interaction is some what indirect. In many situations, a process may complete its own execution
even if other processes are absent. Processes in general are independent, but they synchronize their
relative speeds and accesses to shared data to ensure consistency of the shared data and/or system states.
Note
1. The operating system or any application execution may be considered to be a collection of
processes in which some interact and some others do not.
2. Synchronization is the single most important topic in highly concurrent systems such as operating
systems, databases, and networks.

Process
A process or task is a portion of a program in some stage of execution. A program can consist of
several tasks, each working on their own or as a unit (periodically communicating with each other).
Each process that runs in an operating system is assigned a process control block that holds
information about the process, such as a unique process ID (a number used to identify the process), the
saved state of the process, the process priority and where it is located in memory.
The process priority is used to determine how often the process receives processor time. The
operating system may run all processes with the same priority, or it may run some processes more often
than others. Processes that have been waiting a long time for execution by the processor may have their
priority increased so that they will be more likely to be executed in the future.

Process Control Block (PCB)


A process control block or PCB is a data structure (a table) that holds information about a process. Every
process or program that runs needs a PCB. When a user requests to run a particular program, the operating
system constructs a process control block for that program.
Typical information that is stored in a process control block is
 the location in memory of where the process is
 the priority of the process
 a unique process identification number (called PID)
 the current process state (ready, running, blocked)
 associated data for the process
13
Process State

A process goes through a series of discrete process states. A process in a computer system may be in one
of a number of different possible states, such as

 New State: - The process is being created.


 Terminated State: - The process has finished execution.
 Blocked (waiting) State: - When a process blocks, it does so because logically it cannot
continue, typically because it is waiting for input that is not yet available. Formally, a process is
said to be blocked if it is waiting for some event to happen (such as an I/O completion) before it
can proceed. In this state a process is unable to run until some external event happens.
 Running State: - A process is said t be running if it currently has the CPU, that is, actually
using the CPU at that particular instant.
 Ready State: - A process is said to be ready if it can use a CPU if one were available. It is
runable but temporarily stopped to let another process run.

Logically, the 'Running' and 'Ready' states are similar. In both cases the process is willing to run, only in
the case of 'Ready' state, there is temporarily no CPU available for it. The 'Blocked' state is different from
the 'Running' and 'Ready' states in that the process cannot run, even if the CPU is available.

When a running process is interrupted by the processor after completing its allotted time, its state is saved
in its process control block, its process state changed to ready and its priority adjusted.

When a running process accesses an input or output device, or for some reason cannot continue, it is
interrupted by the processor, the process state and associated data is saved in the associated process
control block. The process state is changed to blocked and the priority adjusted.

When the scheduler decides the next task to run, it changes the process state of the selected process to
running and loads the saved data associated with that process back into the processor.
14
Process Operations

The two process states are process creation and process termination

Process Creation
In general-purpose systems, some way is needed to create processes as needed during operation. There are
four principal events that lead to processes creation.
 System initialization.
 Execution of a process Create System calls by a running process.
 A user request to create a new process.
 Initialization of a batch job.

Foreground processes interact with users. Background processes that stay in background sleeping but
suddenly springing to life to handle activity such as email, webpage, printing, and so on. Background
processes are called daemons. This call creates an exact clone of the calling process.

A process may create a new process by some create process parameter. If it chooses to do so, the creating
process is called parent process and the created one is called the child process. Only one parent is needed
to create a child process. Note that unlike plants and animals that use sexual representation, a process has
only one parent. This creation of process (processes) yields a hierarchical structure of processes.

Following are some reasons for creation of a process

 User logs on.


 User starts a program.
 Operating systems creates process to provide service, e.g., to manage printer.
 Some program starts another process, e.g., Mozilla calls paint to display a picture.

Process Termination

A process terminates when it finishes executing its last statement. Its resources are returned to the system,
it is purged from any system lists or tables, and its process control block (PCB) is erased i.e., the PCB's
memory space is returned to a free memory pool. The new process terminates the existing process, usually
due to following reasons:

 Normal Exit Most processes terminates because they have done their job.
 Error Exit When process discovers a fatal error. For example, a user tries to compile a program
that does not exist.
 Fatal Error An error caused by process due to a bug in program for example, executing an
illegal instruction, referring non-existing memory or dividing by zero.
 Killed by another Process A process executes a system call telling the Operating Systems to
terminate some other process.
15
Process synchronization

Modern operating systems are multi-process systems. They run many processes concurrently.
Concurrency among processes is exploited to achieve better utilization of system resources. Two
processes are said to be concurrent if their program executions overlap in real time. That is, the first
operation execution of each process starts before the last operation execution of the other process is
complete.
Process synchronization deals with various mechanisms to ensure orderly execution of cooperating
processes that share a logical address space. The basic purpose of process synchronization is to maintain
data consistency. A cooperating process is one that can affect or be affected by other processes executing
in the system.
Concurrently running processes without any sort of data protection scheme can manipulate data
and produce an incorrect result. Moreover, if the outcome of an execution depends on the particular order
in which the data access takes place, a race condition can occur. So we need to implement some sort of
synchronization of the processes.

Process 1

Process 2

Process 3

Time
(a) Multi processor case

Process 1

Process 2

Process 3

Time
(b) Uni processor case

The figure above shows two typical execution scenarios of three concurrent processes. In figure (a), three
processes are being executed in parallel (i.e., simultaneously) by three different processors, and in figure
(b), they are executed in an interleaved fashion by a single processor. The solid line fragments indicate
executions of instructions by a processor. The broken lines represent process durations. The gap between
two solid line fragments in a process indicates that the process is waiting for some condition
(multiprocessor case) and/or is preempted (uni-processor case).
Depending on the hardware platform configuration, execution of individual instructions from
processes may overlap in real time (simultaneity) or interleave. In multiprocessor systems, instruction

16
executions of many processes can proceed simultaneously in real time. In uniprocessor systems, the only
way concurrency is achieved is through interleaving of instruction executions. In either case, an
instruction execution of one process may affect those of other processes if the instruction executions
reference the same piece of shared data.
In multiprocessor systems, different processes are at different states of execution. They normally
execute programs and access data from their respective private address spaces. From time to time, they
execute system call service routines from the kernel space. The system calls occur at unpredictable times,
and they may access the same kernel data. They run at different speeds, and one process has no control
over the speed of others. Consequently, the behaviour of one process may depend on what other processes
do with the shared data, how they manipulate the data, and their relative speeds. The operating system
must ensure that processes see consistent values of shared data. The data values must satisfy prior
specified integrity constraints.
If executions of concurrent interacting processes are not controlled properly, they may store
inconsistent values in shared data and their behaviour may be difficult to predict. Process coordination
(alias, synchronization)is a fundamental problem in multiprocessor operating system design and
development.
In general, a synchronization problem is to achieve a specified coordination among a set of
concurrent processes. Processes themselves must coordinate their activities to maintain a coherent
behaviour. They, in general, are independent, and may not be aware of one another. One process becomes
aware of another by waiting on a condition that is set by the other process. That is, processes need to
communicate (directly or indirectly) information among themselves to coordinate their own activities.
(This is different from inter-process communication.) Like interprocess communications, these
communications are also done through reading- and writing shared variables.
In a nutshell, chaos ensues in the system when more than one agent(thread, process, kernel path,
processor, etc.) accesses a piece of shared data concurrently, unless such accesses are properly controlled.
Coordination or synchronization of the activities of agents is vital to maintain data consistency.
In modern high performance computers, a large number of activities (of kernel paths) proceed
concurrently in the kernel space. Synchronization problems are almost everywhere in operating systems.
Designers and developers of operating systems must understand the intricacies and complexities of these
problems before building highly concurrent operating systems.

5.0 INTERPROCESS COMMUNICATION

When a number of processes are running at the same time in a system, it is essential that they be able to
talk to each other in a mannered way. Many times, a process cannot continue until another process does
some action. The structured form of communication between these processes is what's known as Inter
Process Communication (IPC).
As Tanenbaum puts it, there are three points of importance in IPC:
1. Processes must be able to pass information to one another.
2. Two or more processes must not get into each other's way when engaging in critical activities.
3. When one process depends on another, there must be proper sequencing.

As an example to the third point, if process A produces data and process B prints them, then B must wait
until A has produced data before it starts printing. Processes are programs in execution.

17
Why Do Processes Intercommunicate?

If they never did, then all input to a process would have to come from the user! There are several
reasons why IPC must be provided. Often a problem is broken into several stages, each handled by a
process (e.g compilation), so each process passes information to the next stage.
Sometimes a package is broken up into several parts (e.g. for an accounting package: inventory,
credits,debits, invoicing, payroll, etc). Each part will need to pass/obtain information to/from another part
(e.g sales affect inventory, also the case of a web server and a web browser, etc.).There are many methods
of intercommunicating information between processes.

Inter-process Communication in basic HTTP

Processing order: C1, S1, C2, S2, S3, C3, C4, S4

6.0 CPU/PROCESS SCHEDULING

Introduction

If you look at any process, you'll notice that it spends some time executing instructions (computing) and
then makes some I/O request, for example to read or write data to a file or to get input from a user. After
that, it executes more instructions and then, again, waits on I/O. The period of computation between I/O
requests is called the CPU burst.
18
Interactive processes spend more time performing and waiting for I/O and generally experience short
CPU bursts:

Compute-intensive processes, conversely, spend more time running instructions and less time on I/O.
They exhibit long CPU bursts:

Most interactive processes, in fact, spend the vast bulk of their existence doing nothing but waiting on
data. As I write these notes on my laptop, I have over 30 processes running just under my user account. These includes a few
browser windows, a word processor, spreadsheet, several shell windows, media player (since I’m listening to music while
preparing these notes), and various monitors and utilities. Most of the time, these processes collectively are using
less than 3% of the CPU. This is not surprising since most of these programs are waiting for user input, a
network message, or sleeping and waking up periodically to check some state.

Consider a quad-core 3.4 GHz Intel Core i7 processor. It can execute a peak rate of 13.6 billion
instructions per second (four hyper threaded cores). It can run 136 billion instructions in the ten seconds it
might take you to skim a web page before you click on a link — or 3.4 billion instructions in the quarter
second it might take you to hit the next key as you're typing. The big idea in increasing overall throughput
was the realization that we could switch the processor to running another process when a process has to
do I/O. This is multiprogramming. The next big idea was realizing that you could preempt a process and
let another process run and do this quickly enough to give the illusion that many processes are running at
the same time. This is multitasking / time slicing.

The assignment of physical processors to processes allows processors to accomplish work. The
problem of determining when processors should be assigned and to which processes is called processor
scheduling or CPU scheduling
When more than one process is runable, the operating system must decide which one first. The
part of the operating system concerned with this decision is called the scheduler, and the algorithm it uses
is called the scheduling algorithm.

Process Scheduling
Most systems have a large number of processes with short CPU bursts interspersed between I/O requests
and a small number of processes with long CPU bursts. To provide good time-sharing performance, we
may preempt a running process to let another one run. The ready list, also known as a run queue, in the
operating system keeps a list of all processes that are ready to run and not blocked on some I/O or other
system request, such as a semaphore (a variable or abstract data type that provides a simple but useful
abstraction for controlling access by multiple processes to a common resource in a parallel programming
or multi user environment.). The entries in this list are pointers to the process control block, which stores
all information and state about a process. When an I/O request for a process is complete, the process
moves from the waiting state to the ready state and gets placed on the ready list.
19
The process scheduler is the component of the operating system that is responsible for deciding whether
the currently running process should continue running and, if not, which process should run next. There
are four events that may occur where the scheduler needs to step in and make this decision:

1. The current process goes from the running to the waiting state because it issues an I/O request or
some operating system request that cannot be satisfied immediately.
2. The current process terminates.
3. A timer interrupt causes the scheduler to run and decide that a process has run for its allotted
interval of time and it is time to move it from the running to the ready state.
4. An I/O operation is complete for a process that requested it and the process now moves from the
waiting to the ready state. The scheduler may then decide to move this ready process into the
running state.

Preemptive and Non-preemptive Scheduling

A scheduler is a preemptive scheduler if it has the ability to get invoked by an interrupt and move a
process out of a running state and let another process run. The last two events above may cause this to
happen. If a scheduler cannot take the CPU away from a process then it is a cooperative or non-
preemptive scheduler. Old operating systems such as Windows 3.1 or MacOS before OS X are examples
of such schedulers. Even older batch processing systems had run-to-completion schedulers where a
process ran to termination before any other process would be allowed to run.

The decisions that the scheduler makes concerning the sequence and length of time that processes may
run is called the scheduling algorithm (or scheduling policy). These decisions are not easy ones, as the
scheduler has only a limited amount of information about the processes that are ready to run. A good
scheduling algorithm should:

 Be fair – give each process a fair share of the CPU, allow each process to run in a reasonable
amount of time.
 Be efficient – keep the CPU busy all the time.
 Maximize throughput – service the largest possible number of jobs in a given amount of time;
minimize the amount of time users must wait for their results.
 Minimize response time – interactive users should see good performance.
 Be predictable – a given job should take about the same amount of time to run when run multiple
times. This keeps users sane.
 Minimize overhead – don't waste too many resources. Keep scheduling time and context switch
time at a minimum.
 Maximize resource use – favor processes that will use underutilized resources. There are two
motives for this. Most devices are slow compared to CPU operations. We'll achieve better system
throughput by keeping devices busy as often as possible. Secondly, a process may be holding a
key resource and other, possibly more important, processes cannot use it until it is released
(deadlocks discussed next). Giving the process more CPU time may free up the resource quicker.
 Avoid indefinite postponement – every process should get a chance to run eventually.
 Enforce priorities – if the scheduler allows a process to be assigned a priority, it should be
meaningful and enforced.
 Degrade gracefully – as the system becomes more heavily loaded, performance should deteriorate
gradually, not abruptly.

20
It is clear that some of these goals are contradictory. For example, minimizing overhead means that
jobs should run longer, thus hurting interactive performance. Enforcing priorities means that high-priority
processes will always be favored over low-priority ones, causing indefinite postponement. These factors
make scheduling a task for which there can be no perfect algorithm.
To make matters even more complex, the scheduler does not know much about the behavior of
each process. As indicated earlier, some processes perform a lot of input/output operations but use little
CPU time (e.g. web browsers, etc.). They spend much of their time in the blocked state in between little
bursts of computation. The overall performance of these I/O bound processes is constrained by the speed
of the I/O devices. CPU-bound processes spend most of their time computing (e.g. circuit simulators).
Their execution time is determined by the speed of the CPU and the amount of CPU time they can get.
To help the scheduler monitor processes and the amount of CPU time that they use, a
programmable interval timer interrupts the processor periodically (typically 50 or 60 times per second).
This timer is programmed when the operating system initializes itself. At each interrupt, the operating
system's scheduler gets to run and decide whether the currently running process should be allowed to
continue running or whether it should be suspended and another ready process allowed to run. This is the
mechanism used for preemptive scheduling.
Preemptive scheduling allows the scheduler to control response times by taking the CPU away
from a process that it decided has been running too long. It has more overhead than non-preemptive
scheduling since it has to deal with the overhead of context switching (computing process of storing and
restoring the state (context) of a CPU so that execution can be resumed from the same point at a later
time) processes instead of allowing a process to run to completion or until the next I/O operation or other
system call. However, it allows for higher degrees of concurrency and better interactive performance.

Scheduling Algorithms
The scheduling algorithm has the task of figuring out whether a process should be switched out for
another process and which process should get to run next. The dispatcher is the component of the
scheduler that handles the mechanism of actually getting that process to run on the processor. This
requires loading the saved context of the selected process, which is stored in the process control block and
comprises the set of registers, stack pointer, flags (status word), and a pointer to the memory mapping
(typically a pointer to the page table). Once this context is loaded, the dispatcher switches to user mode
via a return from interrupt operation that causes the process to execute from the location that was saved
on the stack at the time that the program stopped running — either via an interrupt or a system call.

First-Come, First-Served Scheduling

Possibly the most straightforward approach to scheduling processes is to maintain a FIFO (first-in,
first-out) ready queue. New processes go to the end of the queue. When the scheduler needs to run a
process, it picks the process that is at the head of the queue. The scheduler is non-preemptive. If the
process has to block on I/O, it enters the waiting state and the scheduler picks the process from the head
of the queue. When I/O is complete and the process is ready to run again, it gets put at the end of the
queue.

21
With first-come, first-served scheduling, a process with a long CPU burst will hold up other
processes. Moreover, it can hurt overall throughput
since I/O on processes in the waiting state may
complete while the CPU bound process is still
running. Now devices are not being used
effectively. For increasing throughput, it would
have been great if the scheduler instead could have
briefly run some I/O bound process that could
request some I/O and wait. Because CPU bound
processes don't get preempted, they hurt interactive
performance because the interactive process won't
get scheduled until the CPU bound one has
completed.

Advantage: FIFO scheduling is simple to


implement. It is also intuitively fair (the first one in line gets to run first).

Disadvantage: The greatest drawback of first-come, first-served scheduling is that it is not preemptive.
Because of this, it is not suitable for interactive jobs. Another drawback is that a long-running process will
delay all jobs behind it.

Round robin scheduling


Round robin scheduling is a preemptive version of
first-come, first-served scheduling. Processes are
dispatched in a first-in-first-out sequence but each
process is allowed to run for only a limited amount
of time. This time interval is known as a time-slice
or quantum. If a process does not complete or get
blocked because of an I/O operation within the time
slice, the time slice expires and the process is
preempted, process gets blocked because of an I/O
operation), it is then preempted. This preempted
process is placed at the back of the ready list where
it must wait for the processes that were already on
the list to cycle through the CPU.

With round robin scheduling, interactive


performance depends on the length of the quantum and the number of processes in the ready list (run
queue). A very long quantum makes the algorithm behave very much like first come, first served
scheduling since it's very likely that a process will block or complete before the time slice is up. A small
quantum lets the system cycle through processes quickly. This is wonderful for interactive processes.
Unfortunately, there is an overhead to context switching and having to do so frequently increases the
percentage of system time that is used on context switching rather than real work.

Advantage: Round robin scheduling is fair in that every process gets an equal share of the CPU. It is easy
to implement and, if we know the number of processes on the ready list, we can know the worst-case
response time for a process.

Disadvantage: Giving every process an equal share of the CPU is not always a good idea. For instance,
highly interactive processes will get scheduled no more frequently than CPU-bound processes.
22
Setting the quantum size
What should the length of a quantum be to get "good" performance? A short quantum is good because it
allows many processes to circulate through the processor quickly, each getting a brief chance to run.
Thus, highly interactive jobs that usually do not use up their quantum will not have to wait as long before
they get the CPU again, hence improving interactive performance. On the other hand, a short quantum is
bad because the operating system must perform a context switch whenever a process gets preempted. This
is overhead: anything that the CPU does other than executing user code is essentially overhead. A short
quantum implies many such context switches per unit time, taking the CPU away from performing useful
work (i.e., work on behalf of a process).

The overhead associated with a context switch can be expressed as:

context switch overhead = C / (Q+C)

 Where Q is the length of the time-slice and C is the context switch time.
 An increase in Q increases efficiency but reduces average response time.

Q = 100 msec. Q = 10 msec.


As an example, suppose that there are Process No.
delay (msec.) delay (msec.)
ten processes ready to run, Q = 100 msec, and C 0 0 0
= 5 msec. Process 0 (at the head of the ready
queue) gets to run immediately. Process 1 can 1 105 15
run only after Process 0's quantum expires (100 2 210 30
msec) and the context switch takes place (5 3 315 45
msec), so it starts to run at 105 msec. Likewise,
4 420 60
process 2 can run only after another 105 msec.
We can compute the amount of time that each 5 525 75
process will be delayed and compare the delays 6 630 90
between a small quantum (10 msec) and a long 7 735 105
quantum (100 msec.):
8 840 120
9 945 135
It can be seen that, with a quantum of 100 msec
and ten processes, a process at the end of the queue will have to wait almost a second before it gets a
chance to run. This is much too slow for interactive tasks. When the quantum is reduced to 10 msec, the
last process has to wait less than 1/7 second before it gets the CPU. The downside of this is that with a
quantum that small, the context switch overhead (5/(10+5)) is 331⁄3%. This means that we are wasting
over a third of the CPU just switching processes! With a quantum of 100 msec, the context switch
overhead is just 4%.

Shortest remaining time first scheduling


The shortest remaining time first scheduling algorithm is a preemptive version to an older non-
preemptive algorithm known as shortest job first scheduling. Shortest job first scheduling runs a process
to completion before running the next one. The queue of jobs is sorted by estimated job length, so that
short programs get to run first and not be held up by long ones. This minimizes average response time.
With the shortest remaining time first algorithm, we take into account the fact that a process runs
as a series of CPU bursts: processes may leave the running state because they need to wait on I/O or

23
because their quantum expired. The algorithm sorts the ready list by the process' anticipated CPU burst
time, picking the shortest burst time. Doing so will optimize the average response time of processes.
Here's an example of five processes in the ready list. If we process them in a FIFO manner, we see
that all the CPU bursts add up to 25 (pick your favorite time unit; this is just an example). The mean run
time for a process, however, is the mean of all the run times, where the run time is the time spent waiting
to run + the CPU burst time of the process. In this example, our mean run time is (8 + 11 + 21 + 23 +
25)/5, or 17.6.

If we reorder the processes in the queue by the estimated CPU burst time, we still have the same overall
total (the processes take the same amount of time to run) but the mean run time changes. It is now (2 + 4
+ 7 + 15 + 25), or 10.6. We reduced the average run time for our processes by 40%!

Other scheduling algorithms include but not limited to;


 SJF Scheduling.
 Priority Scheduling.
 Multilevel Queue Scheduling.
 Multilevel Feedback Queue Scheduling.

Assignment: Discuss each of these scheduling algorithms

7.0 DEADLOCKS

Introduction
Some I/O media such as disks are easily sharable. However, some I/O media such as tape drives or
printers cannot. So they have to be allocated for one process exclusively.
Computer processing is full of resources that can only be used by one process at a time.
Unpredictable results can occur if two or more processes use the same resource at the same time. The OS
control this problem by arbitrating resource requests by granting exclusive access and managing access to
system resources. By allowing processes to have exclusive access to resources, problems may occur: –
Deadlock.
24
Whenever two or more processes try to access a resource that is in use by some other process then
it is said to be a deadlock situation. A deadlock is a situation in which two or more competing actions are
each waiting for the other to finish, and thus neither ever does. Deadlock causes all processes involved in
the deadlock to become permanently blocked. Each process is waiting on resources owned by other
processes that are also deadlocked. Deadlocks occur when processes have been granted exclusive access
to resources.

A set of process is in a deadlock state if each process in the set is waiting for an event that can be
caused by only another process in the set (including itself). In other words, each member of the set of
deadlock processes is waiting for a resource that can be released only by a deadlock process. None of the
processes can run, none of them can release any resources, and none of them can be awakened. It is
important to note that the number of processes and the number and kind of resources possessed and
requested are unimportant.
The resources may be either physical or logical. Examples of physical resources are Printers, Tape
Drives, Memory Space, and CPU Cycles. Examples of logical resources are Files, processes, etc.

Deadlock defined

In a multiprogramming environment, several processes may compete for a finite number of


resources. When a process requests resources and if the resources are not available at that time, the
process enters into a wait state. Waiting processes may never again change their state, because the
resources they have requested are held by other waiting processes. This situation is known as deadlock.

Assume two processes p1 and p2 running simultaneously. Half way through, p1 requests the
operating system for a file in the tape drive, and p2 requests the operating system for the printer. Assume
also that both requests are granted. After a while, p1 requests for the printer without giving the tape drive.
Simultaneously p2 requests the same tape drive without giving up control of the printer. In this situation,
both processes cannot proceed. P1 will wait until p2 releases the printer. But that can happen only if p2
can proceed further and finish off its processing with the printer. And this can happen only if p2 gets the
same tape drive that p1 is holding. P2 can get the tape drive only if p1 can proceed further and completes
its work with the tape drive. That cannot happen too, unless p1 gets the printer which p2 is holding. This
situation is “deadlock”.

Pre-emptable and Non pre-emptable Resources


Resources come in two flavors: preemptable and nonpreemptable. A preemptable resource is one
that can be taken away from the process with no ill effects. Memory is an example of a preemptable
resource. On the other hand, a nonpreemptable resource is one that cannot be taken away from process
(without causing ill effect). For example, CD resources are not preemptable at an arbitrary moment.
` Reallocating resources can resolve deadlocks that involve preemptable resources. Deadlocks that
involve nonpreemptable resources are difficult to deal with.

25
Necessary and Sufficient Deadlock Conditions

Coffman identified four (4) conditions that must hold simultaneously for there to be a deadlock.

1. Mutual Exclusion Condition


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

2. Hold and Wait Condition


Requesting process already hold resources, while waiting for requested resources. i. e. There must
exist a process that is holding a resource already allocated to it while waiting for additional
resource that are being held by other processes.

3. No-Preemptive Condition
Resources already allocated to a process cannot be preempted. i.e. Resources cannot be removed
from the processes are used to completion or released voluntarily by the process holding it.

4. Circular Wait Condition


The processes in the system form a circular list or chain where each process in the list is waiting
for a resource held by the next process in the list.
As an example, consider the traffic deadlock in the following figure

Consider each section of the street as a resource.

1. Mutual exclusion condition applies, since only one vehicle can be on a section of the street at a
time.
2. Hold-and-wait condition applies, since each vehicle is occupying a section of the street, and
waiting to move on to the next section of the street.
3. No-preemptive condition applies, since a section of the street that is occupied by a vehicle cannot
be taken away from it.
4. Circular wait condition applies, since each vehicle is waiting on the next vehicle to move. That
is, each vehicle in the traffic is waiting for a section of street held by the next vehicle in the traffic.
26
The simple rule to avoid traffic deadlock is that a vehicle should only enter an intersection if it is assured
that it will not have to stop inside the intersection.

It is not possible to have a deadlock involving only one single process. The deadlock involves a circular
“hold-and-wait” condition between two or more processes, so “one” process cannot hold a resource, yet
be waiting for another resource that it is holding.

Dealing with Deadlock Problem

Deadlock Prevention
Havender in his pioneering work showed that since all four of the conditions are necessary for deadlock to
occur, it follows that deadlock might be prevented by denying any one of the conditions.

Elimination of “Mutual Exclusion” Condition


The mutual exclusion condition must hold for non-sharable resources. That is, several processes cannot
simultaneously share a single resource. This condition is difficult to eliminate because some resources,
such as the tape 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.

Elimination of “Hold and Wait” Condition


There are two possibilities for elimination of the second condition. The first alternative is that a process
request be granted all of the resources it needs at once, prior to execution. The second alternative is to
disallow a process from requesting resources whenever it has previously allocated resources. This strategy
requires that all of the resources a process will need must be requested at once. The system must grant
resources on “all or none” basis. If the complete set of resources needed by a process is not currently
available, then the process must wait until the complete set is available. While the process waits, however,
it may not hold any resources. Thus the “wait for” condition is denied and deadlocks simply cannot occur.
This strategy can lead to serious waste of resources. For example, a program requiring ten tape drives
must request and receive all ten drives before it begins executing. If the program needs only one tape
drive to begin execution and then does not need the remaining tape 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.

Elimination of “No-preemption” Condition


The non preemption condition can be alleviated by forcing a process waiting for a resource that cannot
immediately be allocated to relinquish all of its currently held resources, so that other processes may use
them to finish. Suppose a system does allow processes to hold resources while requesting additional
resources. Consider what happens when a request cannot be satisfied. A process holds resources a second
process may need in order to proceed while second process may hold the resources needed by the first
process. This is a deadlock. This strategy requires that when a process that is holding some resources is
denied a request for additional resources, the process must release its held resources and, if necessary,
request them again together with additional resources. Implementation of this strategy denies the “no-
preemptive” condition effectively.
When a process releases resources, the process may lose all its work to that point. One serious
consequence of this strategy is the possibility of indefinite postponement (starvation). A process might be
held off indefinitely as it repeatedly requests and releases the same resources.

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

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

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

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

28

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy