You are on page 1of 64

What is an Operating System?

A program that acts as an intermediary between a


user of a computer and the computer hardware.
Operating system goals:
Execute user programs and make solving user
problems easier.
Sunbeam Infotech
Make the computer system convenient to use.
Use the computer hardware in an efficient
manner.

Sunbeam Infotech 2

Computer System Structure Four Components of Computer System


Hardware provides basic computing resources
CPU, memory, I/O devices
Operating system
Controls and coordinates use of hardware among various
applications and users
Application programs define the ways in which the
system resources are used to solve the computing
problems of the users
Word processors, compilers, web browsers, database
systems, video games
Users
People, machines, other computers

Sunbeam Infotech 3 Sunbeam Infotech 4

Operating System Definition


OS is a resource allocator
Manages all resources and
Decides between conflicting requests for efficient and
fair resource use
OS is a control program
Controls execution of programs to prevent errors and
improper use of the computer
Everything a vendor ships when you order an
operating system
Sunbeam Infotech
The one program running at all times on the
computer is the kernel. Everything else is either a
system program (ships with the operating system) or
an application program

Sunbeam Infotech 5
Hard disks Hard disk settings

There are two types of hard disks The SATA hard disk can be directly connected to
PATA / IDE motherboard using special SATA cable.
SATA The PATA hard disk can be connected to
SATA drives are faster as compared to PATA drives. motherboard using other type of cable.
These disks are connected to motherboard using PATA drive must be configured using jumper settings
different kind of connectors. in one of the following modes:
Todays motherboards have one (or more) port/s for Primary Master
PATA, while more than four ports for SATA. Primary Slave
These ports can be used to connect hard disks, Secondary Master
CD/DVD R/W or printers. Secondary Slave

Sunbeam Infotech 7 Sunbeam Infotech 8

BIOS settings Partitions


The hard disk can be split into multiple partitions, so
Disk configurations that each partition could be treated as one disk.
Many of the older OS do not install on SATA disks. These partitions can be used to install operating
For installing such OS, in BIOS, configure disks as IDE systems and to store data/files.
or PATA instead of AHCI mode. One disk can have different types of partitions:
Boot options: Primary partition
Decides the device from which PC should try to boot. Maximum four can be created on a disk.
The recommended order for boot devices is Extended partition
CD/DVD drive Special type of primary partition
Disk drive Only one extended partition is allowed on a disk
USB drive Logical partition
Network boot Multiple can be created within extended partition
Sunbeam Infotech 9 Sunbeam Infotech 10

File system Installing Windows OS


Insert Windows OS (Windows XP, Windows 2003
Server, Windows Vista, etc.) CD/DVD into CD/DVD
Each partition must have some file system.
drive and boot from it.
Different operating systems can be installed on
The basic OS setup will be loaded into the memory.
different types of file systems.
Windows can be installed on first primary partition
Example file systems are:
(OR any primary or logical partition in special case).
FAT / FAT32 (Windows OS)
Create first primary partition (OR logical partition) of
NTFS (Windows Server OS)
appropriate size for the operating system. If
EXT2 / EXT3 (Linux OS) partitions are already created only select the
HFS (MAC OS) partition. The partition will be formatted and OS files
FAT or NTFS partitions have drive letters that are will be installed on it.
used by Windows OS to specify file paths. Follow the steps as given in the setup program.
Sunbeam Infotech 11 Sunbeam Infotech 12
Installing Linux System
Insert Linux OS (Open SuSE, RHEL, UBUNTU, etc.)
CD/DVD into CD/DVD drive, boot from it and select
installation option.
The basic OS setup will be loaded into the memory.
Select fresh/new installation option.
Linux can be installed on any partition.
Select the Custom Partition setup and Create
appropriate (mostly logical) partition of appropriate size Sunbeam Infotech
for the OS. If partitions are already created only select
the partition. The partition will be formatted and OS files
will be installed on it.
Follow the steps as given by the setup program.
Sunbeam Infotech 13

Bootable device bootstrap program


The procedure of starting a computer system by Bootstrap program locates the kernel, loads it into
loading the kernel is known as booting the system. main memory and starts its execution.
The first sector of any storage device is known as Sometimes two-step process where boot block at
boot sector. fixed location loads complex bootstrap program.
If boot sector of any storage device contains a There is a separate bootstrap program for each
special program called bootstrap program, then that operating system.
device is said to be a bootable device. If multiple operating systems are installed on the
computer, the boot loader encompasses the
Similarly if boot sector of the partition of any disk bootstrap/boot loader for other operating systems.
contains this program, partition is said to be a boot
NTLDR, GRUB, LILO, etc are few examples of boot
partition. loaders.
Sunbeam Infotech 15 Sunbeam Infotech 16

Booting process
When computer is started, the instruction pointer
(program counter) is loaded with a predefined memory
location and execution starts there.
The initial boot program is kept here in ROM, as RAM is
in an unknown state at system startup.
The bootstrap program run diagnostics to determine the
state of machine (hardware).
It also initialize CPU registers, device controllers and such
things so that basic system is initialized.
It loads kernel of OS from the disk/device to the memory
and starts the first process of that kernel.
Finally one or more system processes are created which
functions as an operating system.
Sunbeam Infotech 17 Sunbeam Infotech 18
Computer System Structure

Sunbeam Infotech

Sunbeam Infotech 2

Computer-System Operation Common Functions of Interrupts


One or more CPUs, device controllers connect
through common bus providing access to shared Interrupt transfers control to the interrupt service
memory. routine generally, through the interrupt vector, which
contains the addresses of all service routines.
I/O devices and the CPU can execute concurrently.
Interrupt architecture must save the address of the
Each device controller is in charge of a particular
device type and has a local buffer. interrupted instruction. The OS preserves the state of the
CPU by storing registers and the program counter.
CPU moves data from/to main memory to/from local
buffers. Incoming interrupts are disabled while another interrupt
is being processed to prevent a lost interrupt.
Actual input and output occurs between the device
to local buffer of controller. A trap is a software-generated interrupt caused either by
an error or a user request.
Device controller informs CPU that it has finished its
operation by causing an interrupt. An operating system is interrupt driven.
Sunbeam Infotech 3 Sunbeam Infotech 4

I/O Structure Device-Status Table


In synchronous I/O method, after I/O starts, control
returns to user program only upon completing I/O
wait instruction or loop idles the CPU until the next interrupt
At most one I/O request is outstanding at a time, no
simultaneous I/O processing.
In asynchronous method, after I/O starts, control return
to user program without waiting for I/O completion
System call request to the operating system to allow user to
wait for I/O completion.
Device-status table contains entry for each I/O device
indicating its type, address, and state.
Operating system indexes into I/O device table to determine
device status and to modify table entry to include interrupt.

Sunbeam Infotech 5 Sunbeam Infotech 6


Direct Memory Access Structure Storage Structure
Main memory only large storage media that the
CPU can access directly.
Used for high-speed I/O devices able to transmit
information at close to memory speeds. Secondary storage extension of main memory that
provides large nonvolatile storage capacity.
Device controller transfers blocks of data from buffer
Magnetic disks rigid metal or glass platters covered
storage directly to main memory without CPU with magnetic recording material
intervention. Disk surface is logically divided into tracks, which are
Only one interrupt is generated per block, rather subdivided into sectors.
than the one interrupt per byte. The disk controller determines the logical interaction
between the device and the computer.
Storage systems organized in hierarchy.
Speed, Cost, Volatility
Sunbeam Infotech 7 Sunbeam Infotech 8

Main Memory Magnetic Disks


The main memory and processor registers are
directly accessed by the CPU.
The machine instructions take the addresses from
main memory or registers, not the disk.
In concept of memory mapped I/O, a set of memory
addresses are reserved to tie with device registers.
Even I/O ports are mapped with some addresses.
The user program or OS writes data on these
addresses and then set control register to send the
data.
Access to main memory is slower than the registers.
Sunbeam Infotech 9 Sunbeam Infotech 10

Magnetic Disks Performance of Various Levels of Storage


Magnetic disk speed is based on two factors
Transfer rate: data flow rate betn drive and computer
Positioning time or Random access time
Seek time: move disk arm to desired cylinder
Rotation latency: rotate disk head to desired sector
Drive is attached to computer through I/O bus
Different types of buses are
Enhanced integrated drive electronics (EIDE)
Advanced technology attachment (ATA) : (PATA/SATA)
Small computer systems interface (SCSI)
Host controller is connected at computer end of bus
connecting to drive and do the I/O.
Sunbeam Infotech 11 Sunbeam Infotech 12
Storage-Device Hierarchy Caching
Important principle, performed at many levels in a
computer (in hardware, operating system, software)
Information in use copied from slower to faster
storage temporarily
Faster storage (cache) checked first to determine if
information is there
If it is, information used directly from the cache (fast)
If not, data copied to cache and used there
Cache smaller than storage being cached
Cache management important design problem
Cache size and replacement policy

Sunbeam Infotech 13 Sunbeam Infotech 14

Migration of int A from disk to register Hardware Protection


Early operating systems work as resident monitors.
Then OS start doing additional jobs like I/O, resource
allocator, etc.
Multitasking environments must be careful to use In multiprogramming environment, one program could
most recent value, no matter where it is stored in disturb other program in memory by corrupting its data.
the storage hierarchy The programming errors are detected by hardware and
conveyed to operating system via interrupt. OS should
Multiprocessor environment must provide cache take appropriate action like terminating victim program.
coherency in hardware such that all CPUs have the The following protection mechanisms are available:
most recent value in their cache Dual-Mode Operation
Distributed environment situation even more I/O Protection
complex Memory Protection
Several copies of a datum can exist CPU Protection

Sunbeam Infotech 15 Sunbeam Infotech 16

Dual-Mode Operation User mode and Kernel mode


Sharing system resources requires operating system
to ensure that an incorrect program cannot cause
other programs to execute incorrectly.
Provide hardware support to differentiate between
at least two modes of operations.
User mode execution done on behalf of a user.
Monitor mode (also kernel mode or system mode)
execution done on behalf of operating system.
Mode bit added to computer hardware to indicate
the current mode: monitor (0) or user (1).
When an interrupt or fault occurs hardware switches
to monitor mode.
Sunbeam Infotech 17 Sunbeam Infotech 18
I/O Protection Memory Protection
All I/O instructions are Must provide memory protection at least for the
privileged instructions. interrupt vector and the interrupt service routines.
Since all I/O operations are A user program must be protected from the other
done through IVT, it must user program.
be protected from user
In order to have memory protection, add two
programs.
registers that determine the range of legal addresses
I/O must be done via a program may access:
system calls.
Base register holds the smallest legal physical
Must ensure that a user memory address.
program could never gain
Limit register contains the size of the range
control of the computer in
monitor mode. Memory outside the defined range is protected.

Sunbeam Infotech 19 Sunbeam Infotech 20

Use of Base and Limit Register CPU Protection


When executing in User program may stuck up in infinite loop and may not
monitor mode, the return control to the operating system.
operating system has Timer interrupts computer after specified period to ensure
unrestricted access to operating system maintains control.
both monitor and users Timer is decremented every clock tick.
When timer reaches the value 0, an interrupt occurs.
memory.
Set up before scheduling process to regain control or terminate
The load instructions program that exceeds allotted time
for the base and limit Timer is commonly used to implement time sharing.
registers are privileged Changing timer values is a privileged instruction.
instructions. Computers may have time-of-day clock that is independent of
operating system.

Sunbeam Infotech 21 Sunbeam Infotech 22

Sunbeam Infotech

Sunbeam Infotech 23
Multiprogramming Multitasking
Single user cannot keep CPU and I/O devices Multitasking (Time sharing) is logical extension in
busy at all times which CPU switches jobs so frequently that users can
interact with each job while it is running
Multiprogramming organizes jobs (code and
data) so CPU always has one to execute Response time should be < 1 second
A subset of total jobs in system is kept in If several jobs ready to run at the same time CPU
memory scheduling is necessary to schedule them all.
One job selected and run via job scheduling Multitasking can be achieved in one of the ways:
multi-processing
When any job has to wait (for I/O for example),
OS switches to another job multi-threading
Multiprogramming needed to use the system Virtual memory allows execution of processes not
in efficient way. completely in memory. If processes dont fit in
memory, they are swapped in and out to run.
Sunbeam Infotech 2 Sunbeam Infotech 3

Types of operating systems Mainframe Systems


Batch systems
Few operating systems are designed to be efficient
Reduce setup time by batching similar jobs
while other are designed to be convenient.
Transfers control from one job to another.
Depending on design goals and hardware constraints
Resident monitor
different types of operating systems exists.
initial control in monitor
Mainframe Systems
control transfers to job
Desktop Systems when job completes control transfers back to monitor
Multiprocessor Systems Multiprogrammed systems
Distributed Systems Multiple jobs in memory at a time
Real -Time Systems Time sharing systems
Handheld Systems Executes multiple jobs using time sharing concept

Sunbeam Infotech 4 Sunbeam Infotech 5

Mainframe Systems Desktop Systems

Personal computers computer system dedicated to


a single user.
I/O devices keyboards, mice, display screens, small
printers.
User convenience and responsiveness.
May run several different types of operating systems
(Windows, MacOS, UNIX, Linux)

Simple Batch System Multiprogramming System

Sunbeam Infotech 6 Sunbeam Infotech 7


Multiprocessor Systems Distributed Systems
Multiprocessor systems are also called as parallel systems. Distribute computation among several physical
Multiprocessor systems with more than one CPU in close processors.
communication. It is also called as Loosely coupled system. Because each
Advantages of parallel system: processor has its own local memory; processors
communicate with one another through various
Increased throughput, Economical and Increased reliability
communications lines, such as high-speed buses or
Symmetric multiprocessing (SMP) telephone lines.
Each processor runs and identical copy of the operating system. Advantages of distributed systems.
Most modern operating systems support SMP Resources Sharing, Load balancing, Reliability
Asymmetric multiprocessing Requires networking infrastructure.
Each processor is assigned a specific task; master processor Local area networks (LAN) or Wide area networks (WAN)
schedules and allocated work to slave processors.
More common in extremely large systems
May be either client-server or peer-to-peer systems.

Sunbeam Infotech 8 Sunbeam Infotech 9

Real -Time Systems Handheld Systems


Often used as a control device in a dedicated
application such as controlling scientific experiments, These systems are used for mobile hardware.
medical imaging systems, industrial control systems, Personal Digital Assistants (PDAs)
and some display systems. Cellular phones
Well-defined fixed-time constraints. Portable multimedia systems
These systems may be either hard or soft real-time.
Issues:
Hard real-time
Limited memory
Secondary storage limited or absent, data stored in
short term memory, or read-only memory (ROM) Slow processors
Soft real-time Small display screens
More flexible and hence widely used.

Sunbeam Infotech 10 Sunbeam Infotech 11

What Operating System does? Process Management


A process is a program in execution. It is a unit of work
within the system. Program is passive, process is active.
There are different types of operating systems,
however all these systems have some core services Process needs resources to accomplish its task
CPU, memory, I/O, files
Process Management
Initialization data
Memory Management
Reusable resources are released as process terminates
Storage Management
Process management activities done by OS:
File System Management Creating and deleting both user and system processes
I/O System Management Suspending and resuming processes
Protection and Security Providing mechanisms for process synchronization
User interfacing Providing mechanisms for process communication

Sunbeam Infotech 12 Sunbeam Infotech 13


Memory Management Storage Management
Usually disks used to store data that does not fit in main
Memory is a large array of words or bytes, each with memory or data that must be kept for a long period of time
its own address. It is a quickly accessible by the CPU
Most modern computer systems use disks as the principle on-
and I/O devices. line storage medium, for both programs and data.
All data is in memory before and after processing. Entire speed of computer operation hinges on disk subsystem
All instructions are in memory for execution. and its algorithms
Memory management activities OS activities
Free-space management
Keeping track of which parts of memory are currently Storage allocation
being used and by whom Disk scheduling
Deciding which processes (or parts thereof) and data Some storage need not be fast
to move into and out of memory Tertiary storage includes optical storage, magnetic tape
Allocating and deallocating memory space as needed WORM (write-once, read-many-times) and RW (read-write)

Sunbeam Infotech 14 Sunbeam Infotech 15

File Management I/O Management


A file is a collection of related information defined by
its creator. Commonly, files represent programs One purpose of OS is to hide peculiarities of
(both source and object forms) and data. hardware devices from the user
Files usually organized into directories I/O subsystem responsible for
Access control on most systems to determine Memory management of I/O including buffering
who can access what (storing data temporarily while it is being transferred),
caching (storing parts of data in faster storage for
OS activities include performance), spooling (the overlapping of output of
Creating and deleting files and directories one job with input of other jobs)
Primitives to manipulate files and directories General device-driver interface
Mapping files onto secondary storage Drivers for specific hardware devices
Backup files onto stable (non-volatile) storage media
Sunbeam Infotech 16 Sunbeam Infotech 17

Protection and Security Operating System Interface - CLI


Protection is mechanism for controlling access of User communicates with the core OS graphical or
processes or users to resources defined by the OS. command line interface.
Systems generally first distinguish among users, to
determine who can do what CLI allows direct command entry
User identities (user IDs, security IDs) include name and
associated number, one per user Sometimes implemented in kernel, sometimes by
User ID then associated with all files, processes of that user systems program
to determine access control
Group identifier (group ID) allows set of users to be defined Sometimes multiple flavors implemented shells
and controls managed, then also associated with each
process, file Primarily fetches a command from user and execute it
Privilege escalation allows user to change to effective ID with
more rights Sometimes commands built-in, sometimes just names of
Security is defense of system against internal/external programs
attacks
Huge range, including denial-of-service, worms, viruses, If the latter, adding new features doesnt require shell
identity theft, theft of service modification
Operating System Interface - GUI Computing Environments
User-friendly desktop metaphor interface Traditional computer
Usually mouse, keyboard, and monitor
Blurring over time
Icons represent files, programs, actions, etc
Various mouse buttons over objects in the interface cause Office environment
various actions (provide information, options, execute PCs connected to a network, terminals attached to
function, open directory (known as a folder) mainframe or minicomputers providing batch and
Invented at Xerox PARC timesharing
Many systems now include both CLI and GUI Now portals allowing networked and remote systems
interfaces access to same resources
Microsoft Windows is GUI with CLI command shell Home networks
Apple Mac OS X as Aqua GUI interface with UNIX kernel
underneath and shells available Used to be single system, then modems
Solaris is CLI with optional GUI (Java desktop, KDE) Now fire walled, networked
Sunbeam Infotech 21

Client Server Computing Peer-to-Peer Computing


Another model of distributed system
P2P does not distinguish clients and servers
Instead all nodes are considered peers
May each act as client, server or both
Dumb terminals supplanted by smart PCs
Node must join P2P network
Many systems now servers, responding to requests
Registers its service with central lookup service on
generated by clients network, or
Compute-server provides an interface to client to request Broadcast request for service and respond to requests
services (i.e. database) for service via discovery protocol
File-server provides interface for clients to store and
Examples include Napster and Gnutella
retrieve files

Sunbeam Infotech 22 Sunbeam Infotech 23

Web-Based Computing
Web has become ubiquitous
PCs most prevalent devices
More devices becoming networked to allow web
access
New category of devices to manage web traffic
among similar servers: load balancers
Use of operating systems like Windows 95, client-
side, have evolved into Linux and Windows XP, which
can be clients and servers

Sunbeam Infotech 24 Sunbeam Infotech 25


System Calls
System calls provide the interface between a running
program and the operating system.
Typically written in a high-level language (C or C++)
Mostly accessed by programs via a high-level
Application Program Interface (API) rather than
direct system call use
Sunbeam Infotech Win32 API for Windows
POSIX API for POSIX-based systems (including virtually
all versions of UNIX, Linux, and Mac OS X)
Java API for the Java virtual machine (JVM)
Why use APIs rather than system calls?

System Call Example Standard API example


System call sequence to copy the contents of one file to
another file

A description of the parameters passed to ReadFile()


HANDLE filethe file to be read
LPVOID buffera buffer where the data will be read into and
written from
DWORD bytesToReadthe number of bytes to be read into the
buffer
LPDWORD bytesReadthe number of bytes read during the last
read
LPOVERLAPPED ovlindicates if overlapped I/O is being used

System Call Implementation API System Call OS Relationship


Typically, a number associated with each system call
System-call interface maintains a table indexed according
to these numbers
The system call interface invokes intended system call in
OS kernel and returns status of system call and any
return values
The caller need not be aware of system call
implementation
Just needs to obey API and understand what OS will do
as a result call
Details of OS interface hidden from programmer by API
Managed by run-time support library
Standard C Library Example: printf() System Call Parameters
More information is required than identity of system call
Exact type and amount of information vary with OS and call
Three methods used to pass parameters to the OS
Simplest: pass the parameters in registers
In some cases, may be more parameters than registers
Parameters stored in a block, or table, in memory, and address
of block passed as a parameter in a register
This approach taken by Linux and Solaris
Parameters placed, or pushed, onto the stack by the program
and popped off the stack by the operating system
Block and stack methods do not limit the number or length of
parameters being passed

Parameter Passing via Table Types of System Calls


Operating system does different activities for the
services that are provided by it
There are system calls corresponding to most of
these activities
The types of system calls will depend on the services
Process control
File management
Device management
Information maintenance
Communications

Running Operating Systems System programs


MS-DOS Execution Free-BSD UNIX
System programs provide a convenient environment for
startup (a) and running program (b) multiprogramming
program development and execution. They are classified as:
File manipulation
File modification
Status information
Programming language support
Program loading and execution
Communications
Application programs
Most users view of the operation system is defined by
system programs, not the actual system calls
Some of the system programs are simply user interfaces to
system calls; others are considerably more complex.
System programs (contd) System programs (contd)
File management: to manipulate files and directories Programming-language support
Text editors to create and modify files Compilers, assemblers, debuggers and interpreters
Special commands to search contents of files and do Program loading and execution
transformations Absolute, relocatable or overlay loaders
Status information debugging systems for high-level and machine language
Some ask the system for info - date, time, amount of available Communications - Provide the mechanism for creating virtual
memory, disk space, number of users connections among processes, users, and computer systems
Others provide detailed performance, logging, and debugging Allow users to send messages to one anothers screens
information
browse web pages,
Typically, these programs format and print the output to the
send electronic-mail messages
terminal or other output devices
log in remotely
Some systems implement a registry - used to store and retrieve
configuration information transfer files from one machine to another

OS Design and Implementation OS Design and Implementation


Design and Implementation of OS not solvable, but Important principle to separate
some approaches have proven successful Policy: What will be done?
Internals of different Operating Systems can vary widely Mechanism: How to do it?
Start by defining goals and specifications Mechanisms determine how to do something,
Affected by choice of hardware, type of system policies decide what will be done.
User goals and System goals The separation of policy from mechanism is a very
User goals operating system should be convenient to use, important principle, it allows maximum flexibility if
easy to learn, reliable, safe, and fast policy decisions are to be changed later
System goals operating system should be easy to design, There are different examples of operating systems
implement, and maintain, as well as flexible, reliable, error- depending on the goals of designs and the policies
free, and efficient and mechanisms.

Simple Structure : MS-DOS Layered Structure


The operating system is
MS-DOS written to divided into a number of
provide the most layers (levels), each built
functionality in the least on top of lower layers.
space The bottom layer (layer 0),
Not divided into modules is the hardware; the
highest (layer N) is the
Although MS-DOS has
some structure, its user interface.
interfaces and levels of With modularity, layers
functionality are not well are selected such that
separated each uses functions
(operations) and services
of only lower-level layers
UNIX System Microkernel Structure: MAC OS X
UNIX is limited by hardware
functionality, the original UNIX Moves as much from the kernel
had limited structuring. into user space
UNIX consists two separate parts
Communication takes place
Systems programs
between user modules using
The kernel
message passing
Consists of everything
below the system-call Easier to extend a microkernel
interface and above the Easier to port on new
physical hardware architecture
Provides the file system,
More reliable (less code
CPU scheduling, memory
running in kernel mode)
management, and other
operating-system More secure
functions; a large number Performance overhead of user
of functions for one level space to kernel space commn

Modular Structure: Solaris Virtual Machines


Most modern operating Virtual machine takes layered approach for logical conclusion
systems implement kernel It treats hardware and the operating system kernel as though
they were all hardware
modules
A virtual machine provides an interface identical to the
Uses object-oriented underlying bare hardware
approach The operating system creates the illusion of multiple
Each core component is processes, each executing on its own processor with its own
separate (virtual) memory
Each talks to the others The resources of the physical computer are shared to create
over known interfaces the virtual machines
CPU scheduling can create the appearance that users have their own
Each is loadable as needed processor
within the kernel Spooling and a file system can provide virtual card readers and virtual
line printers
Overall, similar to layers but A normal user time-sharing terminal serves as the virtual machine
with more flexible operators console

Sunbeam Infotech 22

Virtual Machine (contd) Virtual Machine (contd)


The virtual-machine concept provides complete protection of
system resources since each virtual machine is isolated from
all other virtual machines. This isolation, however, permits no
direct sharing of resources.
A virtual-machine system is a perfect vehicle for operating-
systems research and development. System development is
done on the virtual machine, instead of on a physical machine
and so does not disrupt normal system operation.
The virtual machine concept is difficult to implement due to
the effort required to provide an exact duplicate to the
underlying machine
(a) Non-Virtual Machine AND (b) Virtual Machine
VMware Architecture Java Virtual Machine
While generating
machine code,
compiler assumes
certain machine
configuration. This
machine assumed
as virtual machine.
JAVA compiler
assumes JVM and
creates byte code,
that is executed on
JVM installed on
host system.
Process Concept
An OS executes a variety of
programs:
Batch system: jobs
Time-shared systems: user programs
or tasks
Process a program in execution;
process execution must progress in
SunBeam Infotech sequential fashion
A process includes:
program counter
stack
data section

Process State Process Control Block (PCB)


Information associated with
each process
Process state
Program counter
CPU registers
CPU scheduling information
As a process executes, it changes state
Memory-management
new: The process is being created
information
running: Instructions are being executed
waiting: The process is waiting for some event to occur
Accounting information
ready: The process is waiting to be assigned to a processor I/O status information
terminated: The process has finished execution

CPU Switch From Process to Process Process Scheduling Queues


Job queue set of all processes in system
Ready queue set of all processes residing in
main memory, ready and waiting to execute
Device queues set of processes waiting for an
I/O device
Processes migrate among the various queues
Ready Queue & I/O Device Queues Representation of process scheduling

Schedulers Schedulers & Medium term schedulers


Long-term scheduler (or job scheduler) selects
which processes should be brought into ready queue
Short-term scheduler (or CPU scheduler) selects
which process should be executed next and allocates
CPU
Short-term scheduler is invoked very frequently
(milliseconds) (must be fast) Processes can be described as either:
Long-term scheduler is invoked very infrequently I/O-bound process spends more time doing I/O than
(seconds, minutes) (may be slow) computations, many short CPU bursts
The long-term scheduler controls the degree of CPU-bound process spends more time doing
computations; few very long CPU bursts
multiprogramming

Context Switch Process Creation


When CPU switches to another process, the system Parent process create children processes, which, in
must save the state of the old process and load the turn create other processes, forming a tree of
saved state for the new process processes
Context-switch time is overhead; the system does no Resource sharing
useful work while switching Parent and children share all resources
Time dependent on hardware support Children share subset of parents resources
Parent and child share no resources
Execution
Parent and children execute concurrently
Parent waits until children terminate
Process Creation (Cont.) C Program Fork() Separate Process
Address space int main()
{
Child duplicate of parent pid_t pid;
Child has a program loaded into it pid = fork(); /* fork another process */
if (pid < 0) { /* error occurred */
UNIX examples
fprintf(stderr, "Fork Failed");
fork system call creates new process exit(1);
exec system call used after a fork to replace the process }
memory space with a new program else if (pid == 0) { /* child process */
execlp("/bin/ls", "ls", NULL);
}
else { /* parent process */
wait (NULL); /* parent will wait for the child to complete */
printf ("Child Complete");
exit(0);
}
}

A processes tree on typical Solaris Process Termination


Process executes last statement and asks the
operating system to delete it (exit)
Output data from child to parent (via wait)
Process resources are deallocated by operating
system
Parent may terminate execution of children
processes (abort)
Child has exceeded allocated resources
Task assigned to child is no longer required
If parent is exiting
Some operating system do not allow child to continue if
its parent terminates
All children terminated - cascading termination

Cooperating Processes Interprocess Communication (IPC)


Independent process cannot affect or be affected Mechanism for processes to communicate and to
by the execution of another process synchronize their actions
Cooperating process can affect or be affected by Message system processes communicate with each
the execution of another process
other without resorting to shared variables
Advantages of process cooperation
Information sharing Computation speed-up IPC facility provides two operations:
Modularity Convenience send(message) message size fixed or variable
Paradigm for cooperating processes, producer receive(message)
process produces info that is consumed by a
consumer process If P and Q wish to communicate, they need to:
unbounded-buffer places no practical limit on size of the establish a communication link between them
buffer exchange messages via send/receive
bounded-buffer assumes that there is a fixed buffer size
Implementing IPC Communications Models
Implementation of communication link
physical (e.g., shared memory, hardware bus)
logical (e.g., logical properties)
Implementation questions:
How are links established?
Can a link be associated with more than two processes?
How many links can there be between every pair of
communicating processes?
What is the capacity of a link?
Is the size of a message that the link can accommodate
fixed or variable?
Is a link unidirectional or bi-directional?

Direct Communication Indirect Communication


Processes must name each other explicitly: Messages are directed and received from
send (P, message) send a message to the process P mailboxes (also referred to as ports)
receive(Q, message) receive a message from the Each mailbox has a unique id
process Q Processes can communicate only if they share a
Properties of communication link mailbox
Links are established automatically Properties of communication link
A link is associated with exactly one pair of Link established only if processes share a common
communicating processes
mailbox
Between each pair there exists exactly one link
A link may be associated with many processes
The link may be unidirectional, but is usually bi-
directional Each pair of processes may share several
communication links
Link may be unidirectional or bi-directional

Indirect Communication Synchronization


Operations Message passing may be either blocking or non-
create a new mailbox
blocking
send and receive messages through mailbox

destroy a mailbox
Blocking is considered synchronous
Mailbox sharing Blocking send has the sender block until the message
P1, P2, and P3 share mailbox A is received
P1, sends; P2 and P3 receive Blocking receive has the receiver block until a
Who gets the message? message is available
Solutions Non-blocking is considered asynchronous
Allow a link to be associated with at most two processes
Non-blocking send has the sender send the message
Allow only one process at a time to execute a receive
operation and continue
Allow the system to select arbitrarily the receiver. Sender Non-blocking receive has the receiver receive a valid
is notified who the receiver was. message or null
Buffering Client-Server Communication
Queue of messages attached to the link; Sockets
implemented in one of three ways Remote Procedure Calls
Zero capacity 0 messages
Remote Method Invocation (Java)
Sender must wait for receiver (rendezvous)
Bounded capacity finite length of n messages
Sender must wait if link full
Unbounded capacity infinite length
Sender never waits

Sockets Remote Procedure Calls


A socket is defined as
Remote procedure call (RPC) abstracts procedure
an endpoint for
communication calls between processes on networked systems.
Concatenation of IP Stubs client-side proxy for the actual procedure on
address and port the server.
The socket The client-side stub locates the server and marshalls
161.25.19.8:1625 refers the parameters.
to port 1625 on host The server-side stub receives this message, unpacks
161.25.19.8
the marshalled parameters, and peforms the
Communication procedure on the server.
consists between a pair
of sockets

Remote Method Invocation


Remote Method Invocation (RMI) is a Java
mechanism similar to RPCs.
RMI allows a Java program on one machine to invoke
a method on a remote object.
Marshalling Parameters

Single & Multithreaded Processes

SunBeam Infotech

Benefits of Multi-threading
Responsiveness Resource Sharing
Economy Utilization of MP Architecture

Single & Multithreaded Processes User threads Vs Kernel threads


Single-threaded process has one program counter Thread management Supported by the
specifying location of next instruction to execute done by user-level Kernel
Process executes instructions sequentially, one at a threads library
time, until completion
Examples
Multi-threaded process has one program counter
Three primary thread Windows XP/2000
per thread
libraries: Solaris
Typically system has many processes, some user,
some operating system running concurrently on one POSIX Pthreads Linux
or more CPUs Win32 threads Tru64 UNIX
Concurrency by multiplexing the CPUs among the Java threads Mac OS X
processes / threads
Multithreading: Many-to-One Model Multithreading: One-to-one Model
Many user-level threads Each user-level thread maps to kernel thread
mapped to single kernel Examples
thread Windows NT/XP/2000 Linux Solaris 9 and later
Examples:
Solaris Green Threads
GNU Portable Threads

Multithreading: Many-to-Many Model Multithreading: Two-level Model


Similar to M:M,
Allows many user level except that it
threads to be mapped to allows a user
many kernel threads thread to be
bound to kernel
Allows the operating thread
system to create a Examples
sufficient number of IRIX
kernel threads HP-UX
Solaris prior to version 9 Tru64 UNIX
Windows NT/2000 with Solaris 8 and
earlier
the ThreadFiber package

Threading Issues Threading Issues : Signal Handling


Signals are used in UNIX systems to notify a process
Semantics of fork() and exec() system calls
that a particular event has occurred
Does fork() duplicate only the calling thread or all
A signal handler is used to process signals
threads?
Signal is generated by particular event
Thread cancellation Signal is delivered to a process
Terminating a thread before it has finished Signal is handled
Two general approaches: Options:
Asynchronous cancellation terminates the target thread Deliver the signal to thread to which the signal applies
immediately
Deliver the signal to every thread in the process
Deferred cancellation allows the target thread to
Deliver the signal to certain threads in the process
periodically check if it should be cancelled
Assign a specific thread to receive all signals for rocess
Threading Issues Threading Issues
Thread pools Scheduler activations
Create a number of threads in a pool where they await
Both M:M and Two-level models require
work
communication to maintain the appropriate number
Advantages:
of kernel threads allocated to the application
Usually slightly faster to service a request with an
existing thread than create a new thread Scheduler activations provide upcalls - a
Allows the number of threads in the application(s) to be communication mechanism from the kernel to the
bound to the size of the pool thread library
Thread specific data This communication allows an application to maintain
Allows each thread to have its own copy of data the correct number kernel threads
Useful when you do not have control over the thread
creation process (i.e., when using a thread pool)

P-threads & Linux threads Windows XP Threads


A POSIX standard (IEEE 1003.1c) API for thread Implements the one-to-one mapping
creation and synchronization Each thread contains
A thread id
API specifies behavior of the thread library,
Register set
implementation is up to development of the library Separate user and kernel stacks
Common in UNIX operating systems (Solaris, Linux, Private data storage area
Mac OS X) The register set, stacks, and private storage area are
Linux refers to them as tasks rather than threads known as the context of the threads
Thread creation is done through clone() system call The primary data structures of a thread include:
ETHREAD (executive thread block)
clone() allows a child task to share the address space KTHREAD (kernel thread block)
of the parent task (process) TEB (thread environment block)

Java Threads
Java threads are managed by the JVM
Java threads may be created by:
Extending Thread class
Implementing the Runnable interface
Basic Concepts
Maximum CPU
utilization obtained
with multiprogramming
CPUI/O Burst Cycle
Process execution
consists of a cycle of
SunBeam Infotech CPU execution and I/O
wait
CPU burst distribution

CPU Scheduler Dispatcher


Selects from among the processes in memory that Dispatcher module gives control of the CPU to the
are ready to execute, and allocates the CPU to one process selected by the short-term scheduler; this
of them involves:
CPU scheduling decisions may take place when a switching context
process:
switching to user mode
1. Switches from running to waiting state
jumping to the proper location in the user program to
2. Switches from running to ready state
restart that program
3. Switches from waiting to ready
4. Terminates
Dispatch latency time it takes for the dispatcher to
stop one process and start another running
Scheduling under 1 and 4 is non-preemptive
All other scheduling is preemptive

Scheduling Criteria FCFS Scheduling


CPU utilization keep the CPU as busy as possible Process Burst Time
(Ideally Max)
P1 24 P1 P2 P3
Throughput # of processes that complete their
execution per time unit (Ideally Max) P2 3 0 24 27 30
Turnaround time amount of time to execute a P3 3
particular process (Ideally Min)
Waiting time amount of time a process has been If processes arrive in the order: P1 , P2 , P3 ,
waiting in the ready queue (Ideally Min) The Gantt Chart for the schedule is given above:
Response time amount of time it takes from when
a request was submitted until the first response is Waiting time for P1 = 0; P2 = 24; P3 = 27
produced, not output (for time-sharing Average waiting time: (0 + 24 + 27)/3 = 17
environment) (Ideally Min)
FCFS Scheduling (Cont.) Shortest-Job-First (SJF) Scheduling
If processes arrive in the order P2 , P3 , P1 Associate with each process the length of its next
The Gantt chart for the schedule is: CPU burst. Use these lengths to schedule the
process with the shortest time
Two schemes:
P2 P3 P1
nonpreemptive once CPU given to the process it
0 3 6 30 cannot be preempted until completes its CPU burst
preemptive if a new process arrives with CPU burst
Waiting time for P1 = 6; P2 = 0; P3 = 3 length less than remaining time of current executing
Average waiting time: (6 + 0 + 3)/3 = 3 process, preempt. This scheme is know as the
Much better than previous case Shortest-Remaining-Time-First (SRTF)
Convoy effect short process behind long process SJF is optimal gives minimum average waiting time
for a given set of processes

Example of Non-Preemptive SJF Example of Preemptive SJF


Process Arrival Time Burst Time Process Arrival Time Burst Time
P1 0.0 7 P1 0.0 7
P2 2.0 4 P2 2.0 4
P3 4.0 1 P3 4.0 1
P4 5.0 4 P4 5.0 4

P1 P3 P2 P4 P1 P2 P3 P2 P4 P1

0 3 7 8 12 16 0 2 4 5 7 11 16

SJF (non-preemptive) SJF (non-preemptive)


Average waiting time = (0 + 6 + 3 + 7)/4 = 4 Average waiting time = (9 + 1 + 0 +2)/4 = 3

determining length of next CPU burst Examples of Exponential Averaging


Can only estimate the length c =0
vn+1 = vn
Can be done by using the length of previous CPU
Recent history does not count
bursts, using exponential averaging c =1

v n ?1 ? c t n - *1 / c +v n . vn+1 = c tn
Only the actual last CPU burst counts
If we expand the formula, we get:
1. t n ? actual length of n th CPU burst vn+1 = c tn+(1 - c)c tn -1 +
2. v n -1 ? predicted value for the next CPU burst +(1 - c )j c tn -j +
3. c , 0 c 1 +(1 - c )n +1 v0
Since both c and (1 - c) are less than or equal to 1, each
4. Define : successive term has less weight than its predecessor
Priority Scheduling Round Robin (RR)
A priority number (integer) is associated with each Each process gets a small unit of CPU time (time
process quantum), usually 10-100 milliseconds. After this
The CPU is allocated to the process with the highest time has elapsed, the process is preempted and
priority (smallest integer highest priority) added to the end of the ready queue.
Preemptive If there are n processes in the ready queue and the
nonpreemptive time quantum is q, then each process gets 1/n of the
SJF is a priority scheduling where priority is the CPU time in chunks of at most q time units at once.
predicted next CPU burst time No process waits more than (n-1)q time units.
Problem Starvation low priority processes may Performance
never execute q large FIFO
Solution Aging as time progresses increase the
priority of the process q small q must be large with respect to context
switch, otherwise overhead is too high

R-R Scheduling : Time Quantum=20 time quantum & context switch time
Process Burst Time
P1 53
P2 17
P3 68
P4 24

P1 P2 P3 P4 P1 P3 P4 P1 P3 P3

0 20 37 57 77 97 117 121 134 154 162

Typically, higher average turnaround than SJF,


but better response

Multilevel Queue Multilevel Queue Scheduling


Ready queue is partitioned into separate queues:
foreground (interactive) background (batch)
Each queue has its own scheduling algorithm
foreground RR background FCFS
Scheduling must be done between the queues
Fixed priority scheduling; (i.e., serve all from
foreground then from background). Possibility of
starvation.
Time slice each queue gets a certain amount of CPU
time which it can schedule amongst its processes; i.e.,
80% to foreground in RR
20% to background in FCFS
Multilevel Feedback Queue Example: multilevel feedback queue
A process can move between the various queues; Three queues:
aging can be implemented this way Q0 RR with time quantum 8 milliseconds
Q1 RR time quantum 16 milliseconds
Multilevel-feedback-queue scheduler defined by the
following parameters: Q2 FCFS
Scheduling
number of queues
A new job enters queue Q0 which is served FCFS.
scheduling algorithms for each queue When it gains CPU, job receives 8 milliseconds. If it
method used to determine when to upgrade a process does not finish in 8 milliseconds, job is moved to
method used to determine when to demote a process queue Q1.
method used to determine which queue a process will At Q1 job is again served FCFS and receives 16
additional milliseconds. If it still does not complete, it
enter when that process needs service
is preempted and moved to queue Q2.

Multilevel Feedback Queues Scheduling Environments


Multi processor scheduling
CPU scheduling more complex when multiple CPUs are
available
Homogeneous processors within a multiprocessor
Load sharing
Asymmetric multiprocessing only one processor
accesses the system data structures, alleviating the
need for data sharing

Scheduling Environments OS Example: Solaris 2 Scheduling


Real Time Scheduling
Hard real-time systems required to complete a
critical task within a guaranteed amount of time
Soft real-time computing requires that critical
processes receive priority over less fortunate ones

Thread Scheduling
Local Scheduling How the threads library decides
which thread to put onto an available LWP
Global Scheduling How the kernel decides which
kernel thread to run next
OS Example: Windows XP Priorities OS Example: Linux Scheduling
Two algorithms: time-sharing and real-time
Time-sharing
Prioritized credit-based process with most credits is
scheduled next
Credit subtracted when timer interrupt occurs
When credit = 0, another process chosen
When all processes have credit = 0, recrediting occurs
Based on factors including priority and history

Real-time
Soft real-time
Posix.1b compliant two classes
FCFS and RR
Highest priority process always runs first

Java Thread Scheduling Algorithm Evaluation


JVM Uses a Preemptive, Priority-Based Scheduling Deterministic
Algorithm modeling takes
FIFO Queue is Used if There Are Multiple Threads a particular
With the Same Priority predetermined
JVM Schedules a Thread to Run When: workload and
defines the
The Currently Running Thread Exits the Runnable
State performance of
A Higher Priority Thread Enters the Runnable State each algorithm
for that workload
Note the JVM Does Not Specify Whether Threads
are Time-Sliced or Not Queueing models
Since the JVM Doesnt Ensure Time-Slicing, the Implementation
yield() method may be used. This Yields Control to
Another Thread of Equal Priority.

SunBeam Infotech
Background Producer
Concurrent access to shared data may result in data
inconsistency while (true) {
Maintaining data consistency requires mechanisms
to ensure the orderly execution of cooperating /* produce an item and put in nextProduced */
processes
while (count == BUFFER_SIZE)
Suppose that we wanted to provide a solution to the
consumer-producer problem that fills all the buffers. ; // do nothing
We can do so by having an integer count that keeps buffer [in] = nextProduced;
track of the number of full buffers. Initially, count is in = (in + 1) % BUFFER_SIZE;
set to 0. It is incremented by the producer after it count++;
produces a new buffer and is decremented by the
consumer after it consumes a buffer. }

Consider this execution


Consumer Race Condition interleaving with count = 5
initially:
count++ could be S0: producer execute
while (true) {
implemented as regs1 = count {regs1 = 5}
while (count == 0) register1 = count S1: producer execute

; // do nothing register1 = register1 + 1 regs1 = regs1 + 1 {regs1 = 6}


count = register1 S2: consumer execute
nextConsumed = buffer[out];
regs2 = count {regs2 = 5}
out = (out + 1) % BUFFER_SIZE; count-- could be S3: consumer execute

count--; implemented as regs2 = regs2 - 1 {regs2 = 4}


register2 = count S4: producer execute

register2 = register2 - 1 count = regs1 {count = 6 }


/* consume the item in nextConsumed S5: consumer execute
count = register2
} count = regs2 {count = 4}

Solution to Critical-Section Problem Petersons Solution


1. Mutual Exclusion - If process Pi is executing in its critical
section, then no other processes can be executing in their Two process solution
critical sections Assume that the LOAD and STORE instructions are
2. Progress - If no process is executing in its critical section atomic; that is, cannot be interrupted.
and there exist some processes that wish to enter their The two processes share two variables:
critical section, then the selection of the processes that
will enter the critical section next cannot be postponed int turn;
indefinitely Boolean flag[2]
3. Bounded Waiting - A bound must exist on the number of The variable turn indicates whose turn it is to enter
times that other processes are allowed to enter their
critical sections after a process has made a request to the critical section.
enter its critical section & before that request is granted The flag array is used to indicate if a process is ready
Assume that each process executes at a nonzero speed to enter the critical section. flag[i] = true implies that
No assumption concerning relative speed of the N processes process Pi is ready!
Synchronization Hardware Semaphore
Many systems provide hardware support for critical
Synchronization tool that
section code does not require busy wait (S) {
Uniprocessors could disable interrupts waiting while S <= 0
Currently running code would execute without Semaphore S integer
preemption variable ; // no-op
Generally too inefficient on multiprocessor systems Two standard operations S--;
Operating systems using this not broadly scalable modify S: wait() and signal() }
Modern machines provide special atomic hardware Originally called P() and V() signal (S) {
instructions Less complicated S++;
Atomic = non-interruptable Can only be accessed via two }
Either test memory word and set value
indivisible (atomic)
operations
Or swap contents of two memory words

Semaphore as General Sync Tool Semaphore Implementation


Counting semaphore integer value can range over
an unrestricted domain Must guarantee that no two processes can execute
wait () and signal () on the same semaphore at the
Binary semaphore integer value can range only same time
between 0 and 1; can be simpler to implement Thus, implementation becomes the critical section
Also known as mutex locks problem where the wait and signal code are placed
in the crtical section.
Can implement a counting semaphore S as a binary Could now have busy waiting in critical section
semaphore implementation
Provides mutual exclusion But implementation code is short
Little busy waiting if critical section rarely occupied
Semaphore S; // initialized to 1
wait (S);
Note that applications may spend lots of time in
Critical Section
critical sections and therefore this is not a good
signal (S);
solution.

Deadlock and Starvation Classical problems of synchronization


Deadlock two or more processes are waiting Bounded-Buffer Problem
indefinitely for an event that can be caused by only N buffers, each can hold one item
one of the waiting processes ??
Let S and Q be two semaphores initialized to 1 Readers and Writers Problem
P0 P1 A data set is shared among a number of concurrent
wait (S); wait (Q); processes
wait (Q); wait (S); Readers only read the data set
. . Writers can both read and write.
signal (S); signal (Q); Problem allow multiple readers to read at the same
signal (Q); signal (S); time. Only one single writer can access the shared
Starvation indefinite blocking. A process may data at the same time.
never be removed from the semaphore queue in Dining-Philosophers Problem
which it is suspended. ??
Dining-Philosophers Problem Problems with Semaphores

Incorrect use of semaphore operations:

signal (mutex) . wait (mutex)

wait (mutex) wait (mutex)

Omitting of wait (mutex) or signal (mutex) (or both)

Monitors Schematic view of a Monitor


A high-level abstraction that provides a convenient and
effective mechanism for process synchronization
Only one process may be active within the monitor at a time
monitor monitor-name
{
// shared variable declarations
procedure P1 () { . }

procedure Pn () {}
Initialization code ( .) { }

}
}

Monitor with Condition Variables Solaris Synchronization


condition x, y; Implements a variety of locks to support
Two operations on a multitasking, multithreading (including real-time
condition variable: threads), and multiprocessing
x.wait () a process Uses adaptive mutexes for efficiency when
that invokes the protecting data from short code segments
operation is Uses condition variables and readers-writers locks
suspended
when longer sections of code need access to data
x.signal () resumes
Uses turnstiles to order the list of threads waiting to
one of processes (if
acquire either an adaptive mutex or reader-writer
any) that invoked
lock
x.wait ()
Windows XP Synchronization Linux Synchronization
Uses interrupt masks to protect access to global Linux:
resources on uniprocessor systems disables interrupts to implement short critical sections
Uses spinlocks on multiprocessor systems
Also provides dispatcher objects which may act as Linux provides:
either mutexes and semaphores semaphores
Dispatcher objects may also provide events spin locks
An event acts much like a condition variable

Pthreads Synchronization System Model


Assures that operations happen as a single logical
Pthreads API is OS-independent unit of work, in its entirety, or not at all
It provides: Related to field of database systems
Challenge is assuring atomicity despite computer
mutex locks system failures
condition variables Transaction - collection of instructions or operations
that performs single logical function
Here we are concerned with changes to stable storage
Non-portable extensions include: disk
Transaction is series of read and write operations
read-write locks Terminated by commit (transaction successful) or abort
spin locks (transaction failed) operation
Aborted transaction must be rolled back to undo any
changes it performed

Types of Storage Media Log-Based Recovery


Volatile storage information stored here does not Record to stable storage information about all
modifications by a transaction
survive system crashes
Most common is write-ahead logging
Example: main memory, cache
Log on stable storage, each log record describes single
Nonvolatile storage Information usually survives transaction write operation, including
crashes Transaction name Data item name
Example: disk and tape Old value New value
Stable storage Information never lost <Ti starts> written to log when transaction Ti starts
Not actually possible, so approximated via replication <Ti commits> written when Ti commits
or RAID to devices with independent failure modes Log entry must reach stable storage before
operation on data occurs
The Deadlock Problem Bridge Crossing Example
A set of blocked processes each holding a resource
and waiting to acquire a resource held by another
process in the set. For Example:
System has 2 disk drives.
Traffic only in one direction.
P1 and P2 each hold one disk drive and each needs
another one. Each section of a bridge can be viewed as a resource.
Example If a deadlock occurs, it can be resolved if one car
semaphores A and B, initialized to 1 backs up (preempt resources and rollback).
Several cars may have to be backed up if a deadlock
P0 P1
wait (A); wait(B)
occurs.
wait (B); wait(A) Starvation is possible.

Deadlock Characterization Deadlock Prevention


Mutual exclusion: only one process at a time can
use a resource. Mutual Exclusion not required for sharable
Hold and wait: a process holding at least one resources; must hold for nonsharable resources.
resource is waiting to acquire additional resources Hold and Wait must guarantee that whenever a
held by other processes. process requests a resource, it does not hold any
No preemption: a resource can be released only other resources.
voluntarily by the process holding it, after that Require process to request and be allocated all its
process has completed its task. resources before it begins execution, or allow process
to request resources only when the process has none.
Circular wait: there exists a set {P0, P1, , P0} of
waiting processes such that P0 is waiting for a Low resource utilization; starvation possible.
resource that is held by P1, P1 is waiting for a Circular Wait impose a total ordering of all
resource that is held by resource types, and require that each process
P2, , Pn1 is waiting for a resource that is held by requests resources in an increasing order of
Pn, and P0 is waiting for a resource that is held by P0. enumeration.

Deadlock Prevention (Cont.) Deadlock Avoidance


No Preemption Simplest and most useful model requires that each
If a process that is holding some resources requests
process declare the maximum number of resources
of each type that it may need.
another resource that cannot be immediately
The deadlock-avoidance algorithm dynamically
allocated to it, then all resources currently being held examines the resource-allocation state to ensure
are released. that there can never be a circular-wait condition.
Preempted resources are added to the list of Resource-allocation state is defined by the number
resources for which the process is waiting. of available and allocated resources, and the
Process will be restarted only when it can regain its old maximum demands of the processes.
resources, as well as the new ones that it is Avoidance Algorithm 1: Single instance of a resource
requesting.
type. Use a resource-allocation graph
Avoidance Algorithm 2: Multiple instances of a
resource type. Use the bankers algorithm
  
    
   

   
  
     
  
ZWh
D
 
 
 Wh
W

6XQ%HDP,QIRWHFK 

    

 
 
 /
 

>D 



E


6XQ%HDP,QIRWHFK  6XQ%HDP,QIRWHFK 

       


   
d
 

> Wh

W 
>




6XQ%HDP,QIRWHFK  6XQ%HDP,QIRWHFK 

!" #
      

,

/DDh


d 
 

6XQ%HDP,QIRWHFK  6XQ%HDP,QIRWHFK 

#
  #
  
Z >
 ^
 
h ^
 
E K
 

^

6XQ%HDP,QIRWHFK  6XQ%HDP,QIRWHFK 

$   %$$





 


Z 


D

D
hE/y>t
^ 


6XQ%HDP,QIRWHFK  6XQ%HDP,QIRWHFK 
&  '(   $) 
D
Z

h
Z




>


DDh

6XQ%HDP,QIRWHFK  6XQ%HDP,QIRWHFK 

& !&" #
     )
D
,
, 


& 
t
  

K
 W
t 

W
&


6XQ%HDP,QIRWHFK  6XQ%HDP,QIRWHFK 

*  
&  >
 
/&  
 
 <<
 
Z
^ <

  d  
 
/K ^
>/K
/KK^ /
6XQ%HDP,QIRWHFK  6XQ%HDP,QIRWHFK 
 +   ' $ 
Wh
W 


W 


SDJHQXPEHU SDJHRIIVHW
S G
P Q Q
&

6XQ%HDP,QIRWHFK  6XQ%HDP,QIRWHFK 

 
 
,-!./ )

0 )
"

6XQ%HDP,QIRWHFK  6XQ%HDP,QIRWHFK 

* *  1+)


W
WWdZ 
WWZ>Z 

/
K

d


d>
^d> ^/
d> 
  

6XQ%HDP,QIRWHFK  6XQ%HDP,QIRWHFK 
 
' $ (+



/ 3DJH )UDPH

K


6XQ%HDP,QIRWHFK  6XQ%HDP,QIRWHFK 

,  + 


  
>H  D
 
,  s 

 
,D 
d d 

dHD H D
H D

6XQ%HDP,QIRWHFK  6XQ%HDP,QIRWHFK 

%!"21!" )  


^
K


^

W


d

6XQ%HDP,QIRWHFK  6XQ%HDP,QIRWHFK 
 ,-    )3'   









6XQ%HDP,QIRWHFK  6XQ%HDP,QIRWHFK 

+$ ,-   +  


<



^



d
SDJHQXPEHU SDJHRIIVHW
SL S G
  

 

6XQ%HDP,QIRWHFK  6XQ%HDP,QIRWHFK 

   )3' '+)


d
d


s
/


6XQ%HDP,QIRWHFK  6XQ%HDP,QIRWHFK 
   )31  1 +)   
K






h 
 

6XQ%HDP,QIRWHFK  6XQ%HDP,QIRWHFK 

  %$


D XVHUVSDFH SK\VLFDOPHPRU\VSDFH

 

 





 
 
 




6XQ%HDP,QIRWHFK  6XQ%HDP,QIRWHFK 

       !&"


> W
 t
^   



 W
 
^^dZ  ^
 
^^d>Z 
 
  ^d>Z

6XQ%HDP,QIRWHFK  6XQ%HDP,QIRWHFK 
' $  ,-

6XQ%HDP,QIRWHFK  6XQ%HDP,QIRWHFK 

,-3+1 1
^

Wh
'
t

>
t

WDDh

6XQ%HDP,QIRWHFK  6XQ%HDP,QIRWHFK 

    -3  .

6XQ%HDP,QIRWHFK  6XQ%HDP,QIRWHFK 


^/

6XQ%HDP,QIRWHFK 

 
s 

K

>




s


6XQ%HDP,QIRWHFK  6XQ%HDP,QIRWHFK 

 

>/K
>
&
D
W


> 

^

6XQ%HDP,QIRWHFK  6XQ%HDP,QIRWHFK 
  

t

)UDPH YDOLGLQYDOLGELW

Y
Y
/ Y
 Y
 L

L

L
 SDJHWDEOH
 

6XQ%HDP,QIRWHFK  6XQ%HDP,QIRWHFK 

 

/


K
/
:
'
^
Z
^
Z

6XQ%HDP,QIRWHFK  6XQ%HDP,QIRWHFK 

  


W&Z


dd
d 





6XQ%HDP,QIRWHFK  6XQ%HDP,QIRWHFK 
 
K
D t
 tKt
d   

 
/
 
/ Kt
d 
d & 

6XQ%HDP,QIRWHFK  6XQ%HDP,QIRWHFK 

 
/
/


W


h
 
W
/  
 

6XQ%HDP,QIRWHFK  6XQ%HDP,QIRWHFK 

  
^&
^&
/
/

^

^Z

6XQ%HDP,QIRWHFK  6XQ%HDP,QIRWHFK 
 
t 'W&sd
E&







/



6XQ%HDP,QIRWHFK  6XQ%HDP,QIRWHFK 

  
Z

   
    SDJHIDXOWV
   
    
    SDJHIDXOWV
  
  

 

6XQ%HDP,QIRWHFK  6XQ%HDP,QIRWHFK 

 
Z Z

    

    

    
,
    
h



t


6XQ%HDP,QIRWHFK  6XQ%HDP,QIRWHFK 
 

^ 

W



E

6XQ%HDP,QIRWHFK  6XQ%HDP,QIRWHFK 

 
Z
t
t
Z
t
^
E

/





6XQ%HDP,QIRWHFK  6XQ%HDP,QIRWHFK 

 

< /D ^^DKs

>&h 
D&h 
 
 d
&


h
6XQ%HDP,QIRWHFK  6XQ%HDP,QIRWHFK 
 
h /
/W  d
Wh


 
 
W d { 
'  t
 W
 >
> 
t
 6 

6XQ%HDP,QIRWHFK  6XQ%HDP,QIRWHFK 

 
D/K/K
 



^


^/K
 


6XQ%HDP,QIRWHFK  6XQ%HDP,QIRWHFK 

 
h D

W  > 
 
t  
 D 

 W
t W
  ^ d
  
t W
 

6XQ%HDP,QIRWHFK  6XQ%HDP,QIRWHFK 
6XQ%HDP,QIRWHFK 

E
 

d

W
^/
&




 
E  E 
^ / 

&
d 
s
> 
^
^ 
&
W 
Z
t d 
K 
W /


 
& ^
Z
 &
t 
Z &
Z  


d 

K& 
& 
& &  &
 
  
^














  



'LUHFWRU\




 )LOHV
 ) )
)
)

 )Q


  

Z
^


>
Z
d
&K^

,
 

 
E 
d
d
' 
: 
E
'

 

^
W


E

 

 ,
'


  








d
/  
^

s


E
> 
Z 


 
 
,


'

K<

   
^ h
^
D&dW
K


E&^E&^ ^ 
  

h/ 
/^
 >WE^E/^
'/  
 
  
 
Z 
 ^

Z d/K

 &^&^
^E&^ 
 hh&^
 t

^

&^
t

 

& D
 d
 Zty
d  
Z  
 
t
'



&
 
>

 

&
>
 

&


&
^/ 
&




  

 K^
s 
/

&D & 
&

 

   
s&^s&^



s&^

W/


dW/s&^


 

>  

 
 ^
,d  t&
>


 
 
 /

/Z

 

 
 
  d



>

'
d

d

  



W
 

 

W
ZD

 
  
  >
 


h  
 ,
 d
Z  
t

/


    



>E hE/y
d^^K^  
^ s&^ s&^ 
hW/W 
/ 
 ds&^
 
 
E&^ E&^

E&^ E&^ 
 /E&^

//K
 
W
  
 
/K

/K
D /K

6XQ%HDP,QIRWHFK 

   

6XQ%HDP,QIRWHFK  6XQ%HDP,QIRWHFK 

 Wh/ /K


/ 
D 
 /
 /K

^
/

6XQ%HDP,QIRWHFK  6XQ%HDP,QIRWHFK 
  

6XQ%HDP,QIRWHFK  6XQ%HDP,QIRWHFK 

   
/K

/K



^
^
^
 

6XQ%HDP,QIRWHFK  6XQ%HDP,QIRWHFK 

  



Z/K
D



JHWSXW
>

6XQ%HDP,QIRWHFK  6XQ%HDP,QIRWHFK 
 

s W

W 
htEd 

^ LRFWO hE/y/K
/VHOHFW 

&/&K

6XQ%HDP,QIRWHFK  6XQ%HDP,QIRWHFK 

   
 /K

/
E /K
h/K
/
Z
 /K

/K/K
6XQ%HDP,QIRWHFK  6XQ%HDP,QIRWHFK 

   
 
^ 
^/K <
^K^
^ 
/
 
W

d  

d
^
d
t
6XQ%HDP,QIRWHFK  6XQ%HDP,QIRWHFK 
 

K^ </K
 

D/K
 D

^
^
/K

6XQ%HDP,QIRWHFK  6XQ%HDP,QIRWHFK 

    




d
W
D
Z

6XQ%HDP,QIRWHFK  6XQ%HDP,QIRWHFK 


^dZD 
h
^s
^dZD
^dZD

^dZD
 

D

6XQ%HDP,QIRWHFK  6XQ%HDP,QIRWHFK 
 

/K

Wh/K



E

6XQ%HDP,QIRWHFK  6XQ%HDP,QIRWHFK 

 

6XQ%HDP,QIRWHFK  6XQ%HDP,QIRWHFK 

You might also like