Download Linked

Document related concepts

RSTS/E wikipedia , lookup

Spring (operating system) wikipedia , lookup

Copland (operating system) wikipedia , lookup

VS/9 wikipedia , lookup

Unix security wikipedia , lookup

CP/M wikipedia , lookup

DNIX wikipedia , lookup

Distributed operating system wikipedia , lookup

Burroughs MCP wikipedia , lookup

Paging wikipedia , lookup

Process management (computing) wikipedia , lookup

Transcript
Sulieman Bani-Ahmad
Introduction to Operating
Systems
2012 / 2013
Sulieman Bani-Ahmad
Associate professor
Department of Computer Information Systems
Faculty of Information Technology
Al-Balqa Applied University - Main Campus, Salt - Jordan
Website: http://filer.case.edu/sxb139
Personal CV: http://filer.case.edu/sxb139/files/SCV.pdf
Face Book page: http://www.facebook.com/DrBaniAhmad
Course website: https://sites.google.com/site/suliemancourses
Last updated on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
Course syllabus
Al-Balqa Applied University – Al-Salt Campus
School of Information Technology
Introduction to Operating Systems
Instructor: Dr. Sulieman Bani-Ahmad.
Overview
Part 1: Overview
1. Introduction
2. Operating-System Structures
Part 2: Process Management
3. Processes
4. Threads
5. CPU Scheduling
6. Process Synchronization
7. Deadlocks
Part 3: Memory Management
8. Main Memory
9. Virtual Memory
Instructor and Office Hours
Dr. Sulieman Bani-Ahmad, Science building, first floor
Course notes and Office hours: Check my website
http://sites.google.com/site/suliemancourses
:‫موقع اسئلة امتحانات فصول سابقة‬
http://sites.google.com/site/soloqbank
Textbook
Operating System Concepts
Eight Edition
Avi Silberschatz, Peter Baer Galvin, Greg Gagne
John Wiley & Sons, Inc.
ISBN 978-0-470-12872-5
Specific Course Policies


Attendance is mandatory.
Grading: First exam 25%, Second exam 25%, and Final exam 50%.
P a g e | 2/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
Table of Contents
COURSE SYLLABUS ........................................................................................................................................................ 2
TABLE OF CONTENTS .................................................................................................................................................... 3
WHAT IS AN OPERATING SYSTEM? ............................................................................................................................... 5
OPERATING SYSTEM GOALS ......................................................................................................................................... 5
COMPUTER SYSTEM COMPONENTS .............................................................................................................................. 5
SYSTEM TYPES .............................................................................................................................................................. 6
DESKTOP SYSTEMS ......................................................................................................................................................10
DISTRIBUTED SYSTEMS ................................................................................................................................................11
MULTIPROCESSOR (PARALLEL) SYSTEMS .....................................................................................................................12
REAL-TIME SYSTEMS ....................................................................................................................................................13
HANDHELD SYSTEMS ...................................................................................................................................................14
COMPUTER SYSTEM OPERATION .................................................................................................................................15
POSSIBLE EVENTS TO TRIGGER AN INTERRUPT ............................................................................................................16
DIRECT MEMORY ACCESS (DMA) STRUCTURE..............................................................................................................16
STORAGE HIERARCHY ..................................................................................................................................................18
CACHING .....................................................................................................................................................................18
HARDWARE PROTECTION ............................................................................................................................................19
OPERATING SYSTEM STRUCTURES ...............................................................................................................................24
COMMON SYSTEM COMPONENTS...............................................................................................................................24
PROCESS MANAGEMENT .............................................................................................................................................24
MAIN MEMORY MANAGEMENT ..................................................................................................................................24
FILE MANAGEMENT .....................................................................................................................................................25
I/O SYSTEM MANAGEMENT ........................................................................................................................................25
SECONDARY STORAGE MANAGEMENT ........................................................................................................................26
NETWORKING (DISTRIBUTED SYSTEMS) ......................................................................................................................28
PROTECTION SYSTEM ..................................................................................................................................................28
USER INTERFACE AND COMMAND-INTERPRETER SYSTEM ...........................................................................................29
OPERATING SYSTEM SERVICES ....................................................................................................................................30
SYSTEM CALLS .............................................................................................................................................................31
CLASSIFICATION OF SYSTEM CALLS ..............................................................................................................................32
PROCESS CONCEPT ......................................................................................................................................................35
PROCESS STATE ...........................................................................................................................................................36
PROCESS CONTROL BLOCK (PCB) .................................................................................................................................36
THREADS .....................................................................................................................................................................38
PROCESS SCHEDULING .................................................................................................................................................38
SCHEDULERS ................................................................................................................................................................40
CONTEXT SWITCH ........................................................................................................................................................41
P a g e | 3/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
COOPERATING PROCESSES ..........................................................................................................................................41
BASIC CONCEPTS .........................................................................................................................................................46
CPU SCHEDULER ..........................................................................................................................................................48
PREEMPTIVE SCHEDULING ...........................................................................................................................................48
DISPATCHER ................................................................................................................................................................49
SCHEDULING CRITERIA ................................................................................................................................................49
SCHEDULING ALGORITHMS .........................................................................................................................................50
BACKGROUND .............................................................................................................................................................64
THE CRITICAL-SECTION PROBLEM ................................................................................................................................65
SOFTWARE SOLUTIONS ...............................................................................................................................................66
HARDWARE SOLUTIONS ..............................................................................................................................................69
DEADLOCK AND STARVATION .....................................................................................................................................73
BINARY SEMAPHORES .................................................................................................................................................74
SYSTEM MODEL ...........................................................................................................................................................75
DEADLOCK CHARACTERIZATION ..................................................................................................................................76
METHODS FOR HANDLING DEADLOCKS .......................................................................................................................78
DEADLOCK PREVENTION..............................................................................................................................................79
DEADLOCK AVOIDANCE ...............................................................................................................................................80
SAFE STATE ..................................................................................................................................................................80
RESOURCE-ALLOCATION GRAPH ALGORITHM .............................................................................................................81
BANKER’S ALGORITHM ................................................................................................................................................82
DEADLOCK DETECTION ................................................................................................................................................84
RECOVERY FROM DEADLOCK .......................................................................................................................................87
BACKGROUND .............................................................................................................................................................88
ADDRESS BINDING .......................................................................................................................................................88
LOGICAL VS. PHYSICAL ADDRESS SPACE ......................................................................................................................89
DYNAMIC LOADING .....................................................................................................................................................89
DYNAMIC LINKING AND SHARED LIBRARIES ................................................................................................................90
OVERLAYS ....................................................................................................................................................................90
SWAPPING ...................................................................................................................................................................90
CONTIGUOUS MEMORY ALLOCATION .........................................................................................................................91
MEMORY PROTECTION ................................................................................................................................................92
MEMORY PARTITION ALLOCATION ..............................................................................................................................93
FRAGMENTATION ........................................................................................................................................................96
PAGING .......................................................................................................................................................................97
ADDRESS TRANSLATION SCHEME ................................................................................................................................98
P a g e | 4/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
1. Operating Systems: Introduction
What is an Operating System?
•
A program that manages the computer hardware and acts as an intermediary between a user
of a computer and the computer hardware. [Textbook]
• An operating system (OS) is a collection of software that manages computer hardware
resources and provides common services for computer programs. [Wiki]
Examples of well-known operating systems:
[1]. Three well-known operating systems
Operating system goals
•
•
•
•
Execute user programs and make solving user problems easier.
Make the computer system convenient to use.
Use the computer hardware (computer system resources) in an efficient manner.
Controls and coordinates the use of hardware among the various application programs for the
various users.
Computer System Components
1. Hardware – provides basic computing resources (CPU, memory, I/O devices).
2. Operating system – controls and coordinates the use of the hardware among the various
application programs for the various users.
3. Applications programs – define the ways in which the system resources are used to solve the
computing problems of the users (text editors, compilers, database systems, video games,
business programs).
4. Users (people, machines, other computers).
P a g e | 5/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
[2]. Computer System Components
[3]. Modern computer system
System types
P a g e | 6/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
Mainframe systems
[4]. Mainframe systems


Mainframe computers (colloquially referred to as "big iron") are computers used primarily by
corporate and governmental organizations for critical applications,
Later, the term was used to distinguish high-end commercial machines from less powerful
units.
Batch systems: The next job will not be started until the previous job terminated
[5]. Dividing main memory into two major parts; one for the system and
another for the jobs
Disadvantages:
•
•
P a g e | 7/107
Inefficient in using CPU time (low CPU utilization)
Low system throughput
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
[6]. Batch systems
Multiprogramming Systems: Allowing several jobs to be concurrently served
•
•
•
•
•
•
•
•
All jobs that enter the system are kept in the job pool (on disk).
Several jobs from the job pool are kept in memory at the same time, and the CPU is
multiplexed among them.
The OS picks and begins execution of one of these jobs.
If the job needs to wait for an I/O device the OS switches to executing another job.
The CPU therefore can be kept busy more of the time.
We say these jobs are being run concurrently.
This is an aspect of operating systems that adds much of its complexity.
Objective: Maximize processor use
[7]. Dividing main memory into two major parts; one for the system and
another for all running jobs
P a g e | 8/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
[8]. Multiprogramming with two programs
[9]. Multiprogramming with three programs
Advantages of Multiprogramming Systems:
•
•
Increase CPU utilization
Increase System throughput
Disadvantages of Multiprogramming Systems:
•
Doesn’t provide user interaction with the system.
OS Features Needed for Multiprogramming
•
•
•
•
•
Memory management – the system must allocate the memory to several jobs
Job Scheduling – Choose jobs in the job pool to bring them into memory
CPU scheduling – the system must choose among several jobs ready to run
Allocation of devices
Protection (memory and OS protection)
Time-Sharing (also called multitasking)
•
•
P a g e | 9/107
This is an extension of multiprogramming.
No job is allowed to use the CPU for more than a certain time, called the quantum.
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
•
•
•
•
•
•
•
A timer is set when a job enters the CPU.
If the timer expires, another job is chosen for execution.
If the quantum is small compared to a human time scale, it seems as if many jobs are
running simultaneously.
This allows for a high degree of interaction between the user and the computer
without the associated inefficiency as the CPU waits for the user to push a key.
Similar to when only one person could use a computer at a time.
Objective: Minimize response time
Note: if there is only one CPU, at most one user job can be running at any time.
[10].
Example multiuser computer system
OS Features Needed for Time-Sharing
•
•
•
•
•
Job Scheduling
Memory management
Process management
• CPU scheduling
• Job synchronization and communication
• Deadlock avoidance
Allocation of devices
Protection
Desktop Systems
•
•
Personal computers - computer system dedicated to a single user.
May run several types of operating system (windows, UNIX, Linux …).
P a g e | 10/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
[11].
Example desktop computer system
Distributed Systems
•
•
Based on Asymmetric multiprocessing (AMP) architecture.
Consist of a collection of processors that do not share memory or a clock
• Each processor has its own local memory
• Processors communicate with one another through various communications lines,
such as high-speed buses or telephone lines
• Loosely coupled system
Advantages
•
•
•
•
Resources Sharing --- files, printers, software
Computation speed up – load sharing: A computation may be broken into parts,
each done on a different processor.
Reliability--- If one processor goes down, the rest will still be able to do useful
work.
Communications--- Mail and the Web
[12].
P a g e | 11/107
Example distributed computer system
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
Multiprocessor (Parallel) Systems
•
•
•
Also known as parallel systems, or tightly coupled systems.
Based on Symmetric multiprocessing (SMP) architecture.
SMP involves a multiprocessor computer hardware architecture where two or more
identical processors are connected to a single shared main memory and are controlled by
a single OS instance.
[13].
•
Symmetric multiprocessor system
Have more than one CPU in close communication
• Sharing bus, clock, and (sometimes) memory and peripheral devices
• Communication usually takes place through the shared memory
Advantages
•
•
•
Increased throughput
Economy of scale (save money and time)
Increased reliability -- detect, diagnose, and correct failures
• Graceful degradation, fault tolerant
[14].
P a g e | 12/107
Breaking problems into smaller subproblems in a parallel
computer system
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
Real-Time Systems
•
•
•
•
•
•
A real-time operating system is a multitasking operating system that aims at executing
real-time applications.
Real-time operating systems often use specialized scheduling algorithms so that they can
achieve a deterministic nature of behavior.
The main objective of real-time operating systems is their quick and predictable response
to events.
They have an event-driven or time-sharing design and often aspects of both.
– An event-driven system switches between tasks based on their priorities or external
events while time-sharing operating systems switch tasks based on clock interrupts.
Often used as a control device in a dedicated application such as controlling scientific
experiments, medical imaging systems, industrial control systems, and some display
systems.
Well-defined fixed-time constraints.
• Processing must be done within the defined constraints, or the system will fail
Hard real-time systems
•
•
•
Guarantees that critical tasks be completed on time
Secondary storage limited or absent; data stored in short-term memory, or read-only
memory (ROM).
Conflicts with time-sharing systems; not supported by general-purpose operating
systems.
Soft real-time systems
•
•
•
P a g e | 13/107
A critical real-time task get priority over other tasks, and retains that priority until it
completes
Have more limited utility than hard real-time system.
Useful in applications (multimedia, virtual reality) requiring advanced operatingsystem features.
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
Handheld Systems
•
•
•
•
•
•
•
Uses embedded operating system
Embedded operating systems are designed to be used in embedded computer systems.
Embedded OS are able to operate with a limited number of resources. They are very
compact and extremely efficient by design.
Windows CE and Minix 3 are some examples of embedded operating systems.
Personal Digital Assistants (PDAs) are example handheld systems
A personal digital assistant (PDA), also known as a palmtop computer, or personal
data assistant, is a mobile device that functions as a personal information manager.
PDAs are largely considered obsolete with the widespread adoption of smartphones.
Issues with handheld devices:
• Limited memory
• Slow processors
• Small display screens
• Limited power
[15].
P a g e | 14/107
Modern smart phones
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
2. Computer Systems Structures
Computer System Operation










Modern operating systems are interrupt driven.
An interrupt is a signal to the processor emitted by hardware or software indicating an event that
needs immediate attention.
The OS executes the first process and waits for an interrupt from the hardware (by sending a
signal to the CPU) or SW (by executing a special operation called a system call (also called a
monitor call)).
 A hardware interrupt is an electronic alerting signal sent to the processor from an
external device, either a part of the computer itself such as a disk controller or an
external peripheral. For example, pressing a key on the keyboard or moving the
mouse triggers hardware interrupts that cause the processor to read the keystroke or
mouse position.
 A software interrupt is caused either by an exceptional condition in the processor
itself, or a special instruction in the instruction set which causes an interrupt when it is
executed. The former is often called a trap or exception and is used for errors or
events occurring during program execution that are exceptional enough that they
cannot be handled within the program itself.
 For example, if the processor's arithmetic logic unit is commanded to divide a
number by zero, this impossible demand will cause a divide-by-zero
exception, perhaps causing the computer to abandon the calculation or display
an error message.
Again, a trap (or an exception) is a software-generated interrupt caused either by (i) an error
(e.g., division by zero or invalid memory access) or (ii) a user request (system call).
Interrupt transfers control to the interrupt service routine generally, through the interrupt vector,
which contains the addresses of all the service routines.
Interrupt architecture must save the address of the interrupted instruction.
Incoming interrupts are disabled while another interrupt is being processed to prevent a lost
interrupt.
The operating system preserves the state of the CPU by storing registers and the program
counter.
Separate segments of code determine what action should be taken for each type of interrupt.
There are many different types of interrupts on the typical machine and each type has a specific
interrupt handler (Interrupt Service Routine (ISR)). An interrupt handler is a program that
determines nature of the interrupt and performs whatever actions are needed.
P a g e | 15/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
[16].
[17].
Processing interrupts in interrupt driven computer system
Interrupt time line for a single processor doing output
Possible Events to Trigger an Interrupt





Program -- Arithmetic overflow, division by zero, illegal memory access…
Timer -- Allow processor to perform certain functions on a regular basis.
I/O -- Normal completion of operations, error condition.
Hardware failure -- Power failure, memory parity error.
User -- Request for operating-system service.
Direct Memory Access (DMA) Structure


This is another way to improve system performance.
Direct memory access (DMA) is a feature of modern computers that allows certain hardware
subsystems within the computer to access system memory independently of the central
processing unit (CPU).
P a g e | 16/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems




Without DMA, when the CPU is using programmed input/output, it is typically fully
occupied for the entire duration of the read or write operation, and is thus unavailable to
perform other work.
DMA is used for high speed I/O devices that are able to transmit information at close to the
speed of memory. The device controller basically transfers blocks of data from the device's
local storage buffer to main memory without the intervention of the CPU. This has the
advantage of only generating one interrupt per block, rather than one interrupt per byte.
The CPU is only involved at the beginning and end of the transfer
The CPU is free to perform other tasks during data transfer
[18].
The DMA controller can initiate memory read or write cycles
that allows DMA devices such as hard drives to directly read from
and write into memory
[19].
The CPU is involved at the beginning and at the end of
input/output operations to DMA I/O devices; that is when initiating
block reading or writing and after this operation is concluded
(normally or with error)
P a g e | 17/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
[20].
The CPU is involved at the beginning and at the end of
input/output operations to DMA I/O devices; that is when initiating
block reading or writing and after this operation is concluded
(normally or with error)
Storage Hierarchy
The storage system is organized in a hierarchy based on the following three factors
 Speed (Access Time).
 Cost.
 Capacity.
 Volatility.
As goes down the hierarchy…
 Decreasing cost per bit.
 Increasing capacity.
 Increasing access time.
 Decreasing frequency of access of the memory by the processor.
 Becoming non-volatile.
Caching

Use of high-speed memory to hold recently-accessed data.
P a g e | 18/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems



Main memory can be seen as a cache for secondary storage.
Requires a cache management policy.
Caching introduces another level in storage hierarchy. This requires data that is
simultaneously stored in more than one level to be consistent.
[21].
Migration of integer A from disk to computer register
[22].
Storage Hierarchy
Hardware Protection

Sharing both improved utilization and increased problems.
Dual Mode Operation


Sharing system resource requires that an operating system ensure that an incorrect
program cannot cause other programs to execute incorrectly.
Provide hardware support to differentiate between at least two modes of operation.
 User mode - execution done on behalf of the user. This mode should have
restricted privileges.
o A process can access only its own memory
P a g e | 19/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems




o Prevent process from accessing kernel DS or H/W registers that
may affect other processes or OS
 Monitor mode (also kernel mode or system mode) - execution done on behalf
of the operating system. This mode should have full privileges.
o Access all kernel data structures and hardware
Mode bit added to computer hardware (in CPU flags) to indicate the current mode:
monitor (0) or user (1).
When an interrupt or fault occurs hardware switches to monitor mode
Privileged instructions can be issued only in monitor mode.
Privileged instruction is:
 System calls.
 I/O instruction
 Halt instruction
 Turn the interrupt system on and off
[23].
[24].
Switching between user and kernel modes
Transition for user to kernel mode
I/O Protection


All I/O instructions are privileged instructions (to prevent users from performing illegal
I/O).
 Users cannot issue I/O instructions directly
 Users must do I/O through OS … System Call
The system must ensure that it is never possible for a user program to gain control of the
system in monitor mode. It could be possible for a user program to replace an address in
P a g e | 20/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
the interrupt vector with an address of one of its own routines, so when an interrupt is
processed, the control is transferred to the user's program (in monitor mode).


Given the I/O instructions are privileged, how does the user program perform I/O?
System call – the method used by a process to request action by the operating system
 Usually takes the form of a trap to a specific location in the interrupt vector.
 Control passes through the interrupt vector to a service routine in the OS, and the
mode bit is set to monitor mode.
 The monitor verifies that the parameters are correct and legal, executes the request,
and returns control to the instruction following the system call.
[25].
I/O protection through dual mode of operation
Memory Protection



Must provide memory protection at least for the interrupt vector and the interrupt service
routines.
 And protect user programs from one another
In order to have memory protection, add two registers that determine the range of legal
addresses a program may access:
 Base register – holds the smallest legal physical memory address.
 Limit register – contains the size of the range
Memory outside the defined range is protected.
P a g e | 21/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
[26].



Memory protection using base and limit registers
When executing in monitor mode, the operating system has unrestricted access to both
monitor and users' memory.
The load instructions for the base and limit registers are privileged instructions.
In practice, memory protection is much more complicated than this. A device called a
Memory Management Unit (MMU) controls access to memory.
[27].
Validating addresses issued by a process
CPU Protection


To implement CPU protection, the system needs a hardware timer. Each job has an
allotted time to use the CPU. When this time runs out, an interrupt occurs. This is
necessary to ensure that control returns to the operating system so now and again, and
that a user job does not stay on the CPU indefinitely.
Timer – interrupts computer after specified period to ensure operating system maintains
control
P a g e | 22/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems





Timer is decremented every clock tick
When timer reaches the value 0, an interrupt occurs and OS gains the
control
 Set the timer the amount of time that a program is allowed to run
Timer commonly used to implement time sharing.
 Set the time to interrupt every N milliseconds (time slice)
Time also used to compute the current time
Load-timer is a privileged instruction
P a g e | 23/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
3. Operating System Structures
Operating System Structures


An operating system provides the environment within which programs are executed.
Operating systems may be viewed from several points.
 By examining the services that an operating system provides
 By looking at the interface that it makes available to users and programmers.
 By disassembling the system into its components and their interconnections.
Common System Components








Process Management.
Main Memory Management.
File Management.
I/O System Management
Secondary Storage Management
Networking.
Protection System.
User interface and Command-Interpreter System.
Process Management




A process is a program in execution. A process needs certain resources, including CPU
time, memory, files and I/O devices in order to accomplish its task.
Operating-system process vs. user process
The operating system is responsible for the following activities in connection with
process management
 Process creation/ deletion.
 Process suspension/ resumption.
 Provision of a mechanism for:
o process synchronization
o process communication
o deadlock handling
Process management is usually performed by the kernel.
Main Memory Management



Memory is a large array of words or bytes, each with its own address. It is a repository
for quickly accessible data that is shared by both the CPU and I/O devices.
Main memory is a volatile storage device. This means that its contents are lost when the
system fails, or the power is turned off.
A program must be mapped to absolute addresses and loaded into memory for execution.
P a g e | 24/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems

The operating system is responsible for the following activities in connection with
memory management:
 Keeping track of which parts of memory are currently being used and by
whom
 Allocate and deallocate memory space as needed

E.g. the C function 'malloc' (or 'New' in Pascal) allocates a specified amount of
memory; this
happens via an OS call. The functions 'free'(C) and
'Dispose'(Pascal) deallocate this memory.
File Management


A file is a collection of related information defined by its creator. Commonly, files
represent programs (both source and object forms) and data.
The operating system is responsible for the following activities in connection with file
management:
 File creation and deletion.
 Directory creation and deletion.
 Support of primitives for manipulating files and directories.
 Mapping files onto secondary storage.
 File backup on stable (non-volatile) storage media.
I/O System Management

The I/O system consists of
o A memory-management component that includes buffering, caching, and
spooling.
o A general device-driver interface.
o Drivers for specific hardware devices.
Buffering
 It is efficient to read in more data (from input devices) than is requested (by CPU) on
the assumption that the process will want to use other data that is logically close
“Locality”
 Data can be read into an Input Buffer - area of primary memory. Perform physical
Input only when buffer is empty.
 Output - write data to Output Buffer, only writing it away to physical device when
buffer is full.
 Processes then do not have to wait for I/O operation, because (most) reads/writes can
be done in one operation.
Spooling
 Buffering alone is not sufficient for non-shareable devices.
 E.g., all processes outputing to printer would be suspended while one process controls
printer. Print job could last for more than 30 minutes. Unsatisfactory.
 Spooling (Simultaneous Peripheral Operations OnLine) is a more sophisticated form
of buffering.
 Output (input) is stored on disk until device is free.
 Spooler moves data between disk and the device.
 Device can then operate at optimum speed.
P a g e | 25/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems


Jobs in spool queue can be prioritised.
Buffering can be used to smooth the transfer between disk and the device.
Secondary Storage Management



Since main memory (or primary storage) is volatile and too small to accommodate all
data and programs permanently, the computer system must provide secondary storage to
back up main memory.
Most modern computer systems use disks as the principle on-line storage medium for
both programs and data.
The operating system is responsible for the following activities in connection with disk
management:
 Free space management
 Storage allocation
 Disk Scheduling
[28].
P a g e | 26/107
Moving-dead disk mechanism
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
[29].
[30].
P a g e | 27/107
Linked free disk space list on some disk
Contiguous and non-contiguous allocation of disk space
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
[31].
Indexed allocation of disk space
Networking (Distributed Systems)





A distributed system is a collection processors that do not share memory or a clock. Each
processor has its own local memory.
The processors in the system are connected through a communication network.
Communication takes place using a protocol.
A distributed system provides user access to various system resources.
Access to a shared resource allows
 Computation speed-up
 Increased data availability
 Enhanced reliability
Protection System


Protection refers to a mechanism for controlling access by programs, process or users to
both system and user resources.
Operating Systems commonly control access by using permissions. All system resources
have an owner and permission associated with them. Users may be combined into groups
for the purpose of protection.
P a g e | 28/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems


E.g., in UNIX every file has an owner and a group.
The following is a listing of all the information about a file.
rwxr-xr--
martin
staff
[32].

382983 Jan 18 10:20 notes305.html
Security in Unix and Unix-like systems
The first field is the protection information; it shows the permissions for the owner, then
the group, then everybody else.
 The first rwx means that the owner has read, write and execute permissions.
 The next r-x means that the group has read and executes permissions.
 The next r-- means that all other users have only read permission.
 The name of the owner of the file is martin; the name of the group for the file
is staff; the length of the file is 382983 bytes; the file was created on Jan 18 at
10:20 and the name of the file is: notes305.html.
[33].
Security in Windows operating system
User interface and Command-Interpreter System

User interface
o Graphical User Interface
o Command Line Interface
P a g e | 29/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems



Command interpreter:
 Interface between users and the operating system
 Control-card interpreter, command-line interpreter, shell, mouse-based
window-and-menu system
 Get the next statement and execute it
Many commands are given to the operating system by control statements which deal
with:
 process creation and management
 I/O handling
 secondary-storage management
 main-memory management
 file-system access
 protection
 networking
How to implement the commands that a CI knows?
1. Internal commands: the CI itself contains the code to execute the command. (in this
case the number of commands determines the size of CI)
2. External commands (System programs): the CI does not understand the command,
it uses the command to identify a file to be loaded into memory and executed.
 Example (UNIX command to delete a file): rm G
 Search for a file called rm
 Load the file into memory
 Execute it with the parameter G
 +ve:
 Programmers can add new commands to the system easily by creating new
files.
 Small CI program does not have to be changed for new commands to be
added.
 Problems:
 Parameter passing from the CI to the system program.
 Parameters interpretation is left up to the system program (parameters may
be provided inconsistently).
 Slower
Operating System Services

For the convenience of the programmer (to make the programming task easier):
 Program execution:
o
Load a program into memory and to run it.
o
End its execution, either normally or abnormally (indicating error).
 I/O operations: since user programs cannot execute I/O operations directly, the
operating system must provide some means to perform I/O.
 File system manipulation: The capability to read, write, create and delete files.
 Communications: The exchange of information between processes that may be
executing on the same computer or on completely different machines. This is usually
implemented via either shared memory or message passing.
 Error detection: ensure correct and consistent computing by detecting errors in the
CPU and memory hardware, in I/O devices, or in user programs.

For ensuring efficient system operations:
P a g e | 30/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems




Resource allocation: allocating resources to multiple users or multiple jobs running
at the same time.
Accounting: keep track of and record which users use how much and what kinds of
computer resources for account billing or for accumulating usage statistics.
Protection: ensuring that all access to system resources is controlled.
Security: ensuring that system resources are used and accessed as intended under all
circumstances.
System Calls






System calls provide the interface between a running program and the operating system.
System calls are generally available as assembly language instructions. (e.g. INT 21h in
DOS).
Languages defined to replace assembly language for systems programming allow system
calls to be made directly (e.g., C, C++, Win32 API).
Example:
 Read data from one file and copy them to another

Ask the names of the two files

Prompt messages on the screen

Read the file names from the keyboard

Open the input file and create the output file

A loop that reads from the input file and writes to the output file

Close both files

Terminate normally

Error handling or abnormally terminate
Most of the details of the OS interface are hidden from the programmer by the compiler and
by the run-time support package
 printf(….), cout(…)

Compiled into a call to a run-time support routine that issues the necessary
system calls, check for errors, and finally returned to the user program.
Three general methods are used to pass parameters between a running program and the
operating system.
 Pass parameters in registers (-ve: more parameters than registers).
 Store the parameters in a table or block in memory, and the table address is
passed as a parameter in a register.
 Push (store) the parameters onto the stack by the program, and pop off the
stack by operating system.
P a g e | 31/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
[34].
Passing parameters as a table.
Classification of System Calls
Process control







End, abort
Load, execute
Create process, terminate process
Get process attributes, set process attributes
Wait for time
Wait event, signal event
Allocate and free memory
File Management




Create file, delete file
Open file, close file
Read, write, reposition
Get file attributes, set file attributes
Device Management




Request device, release device
Read, write, resposition
Get device attributes, set device attributes
Logically attach or detach devices
Information Maintenance




Get time or date, set time or date
Get system data, set system data
Get process, file, or device attributes
Set process, file, or device attributes
Communications




Create, delete communication connection
Send, receive messages
Transfer status information
Attach or detach remote devices
Note: There are two common models of communication:

Message-passing model.
 Useful when smaller numbers of data need to be exchanged.
 Easier to implement.
P a g e | 32/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems

[35].
Communication using Message passing
[36].
Communication using Message passing
Shared-memory model.
 Allows maximum speed.
 Convenience of communication.
[37].
P a g e | 33/107
Communication through writing on a shared memory
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
P a g e | 34/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
4. Processes
Process Concept







The system consists of a collection of processes execute concurrently: Operating system
processes executing system code, and user processes executing user code.
 Even if the user can execute only one program at a time, the operating system may need to
support its own internal programmed activities, such as memory management.
Textbook uses the terms job and process almost interchangeably.
Process – a program in execution.
A process will need certain resources – such as CPU time, memory, files, and I/O devices – to
accomplish its task.
A process includes (figure shown below):
 Text section (program code), data section (global variable)
 Current activities – PC (Program Counter), registers…
 Stack – Temporary data (parameter to functions and subroutines, return address, local
variable…)
Process vs. Program – active vs. passive entities
Operating System Requirements for Processes:
 OS must interleave the execution of several processes to maximize CPU usage while
providing reasonable response time.
 OS must allocate resources to processes (memory, I/O device, etc.) while avoiding
deadlock.
 OS must support inter-process communication, synchronization, and user creation of
processes.
P a g e | 35/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
[38].
Process in memory
Process State


As processes executes it changes states.
Processes may be in one of the following states:
 new: The process is being created. – (put in job queue).
 ready: The process is waiting to be assigned to a processor. – (put in ready queue).
 running: Instructions from the process are being executed.
 waiting: The process is waiting for some event to occur (such as an I/O completion
or reception of a signal). – (put in waiting queue).
 terminated: The process has finished execution. May be normal or abnormal.
Diagram of process states



. ]93[
These names are arbitrary and vary between operating systems. The states that they
represent are found on all systems, however.
Only one process can be running on any processor at any instant.
Many processes may be ready and waiting.
Process Control Block (PCB)

Each process is represented in the OS by process control block (PCB) – also called a task
control block. It contains and maintains every piece of information that the OS needs
about a given process within the system.
P a g e | 36/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
[40].

Process Control Block structure
The components of the PCB are as followed:
 Process identifier, user identifier, parent process identifier.
 Process state: new, ready, running, waiting…
 Program counter: address of the next instruction.
 CPU registers: The registers vary in number and type, depending on the computer
architecture. They include accumulators, index registers, stack pointers, and generalpurpose registers, plus any conditional-code information.
Context
Switch
[41].
P a g e | 37/107
Diagram shows CPU switch from process to process
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems





CPU scheduling information: priority…
Memory-management information: base/limit register, page tables…
Accounting information: the amount of CPU time used, time limits…
I/O status information: list of I/O device allocated to this process, list of open files…
Pointer to other PCBs.
Threads


The process model implies that a process is a program that performs a single thread of
execution.
 In a single-thread word-processor program, the user cannot simultaneously type in
characters and run the spell checker within the same process (i.e., single thread of
control allows the process to perform only one task at one time).
If a process has multiple threads of execution.
 It can perform more than one task at a time.
Process Scheduling

Goal:
Multiprogramming – have some process running at all times.
o Maximize CPU utilization.
 Time Sharing – Let users interact with each program while it is running.
o Minimize response time.
 For a uni-processor system.
o There will never be more than one running process.
• The reset process will have to wait until the CPU is free and can be
rescheduled.

Scheduling Queues





Job queue (New) – set of all newly created processes in the system.
Ready queue (Ready) – set of all processes residing in main memory, ready and waiting
to execute.
Device queues (Waiting) – set of processes waiting for an I/O device.
Process migration between the various queues.
Process Scheduling queues are implement by linked lists.
 Link by pointers in PCBs
P a g e | 38/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
[42].
The ready queue and various I/O device queues.

A new process is initially put in the ready queue. It waits in the ready queue until it is
selected for execution (or dispatched) and is given to the CPU.
 After the process is allocated to the CPU one of the following events can happen
1. The process could issue an I/O request, and then be placed in an I/O queue
2. The process could create a new subprocess and wait for its termination.
3. The process could be removed forcibly from the CPU, as a result of an
interrupt, and be put back in the ready queue
P a g e | 39/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
[43].
Queuing-diagram representation of process scheduling.
SCHEDULERS






A process migrates between the various scheduling queues throughout its lifetime.
The operating system must select, for scheduling purposes, processes from these queues
in some fashion.
The appropriate scheduler carries out the selection process.
Long-term scheduler (or job scheduler) – selects (from pool) which processes should be
brought into the ready queue (New to Ready).
Short-term scheduler (or CPU scheduler) – selects which process should be executed
next and allocates CPU (Ready to Running).
Medium-term scheduler – removes processes from memory; at some later time, the
process can be reintroduced into memory and its execution can be continued where it left
off (called swapping).
P a g e | 40/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
[44].





Addition of medium-term scheduling to the queueing
diagram.
Short-term scheduler is invoked very frequently (milliseconds)  (must be fast).
Long-term scheduler is invoked very infrequently (seconds, minutes)  (may be slow).
 Long-term scheduler controls multiprogramming degree.
 Select a good mix of I/O- and CPU- bound processes.
Processes can be described as either:
 I/O-bound process – spends more time doing I/O than computations, many short
CPU bursts.
 CPU-bound process – spends more time doing computations; few very long CPU
bursts.
If all processes are I/O bound, the ready queue will almost always be empty, and the
short-term scheduler will have little to do.
If all processes are CPU bound, the I/O waiting queue will almost always be empty,
devices will go unused.
Context Switch






When CPU switches to another process, the system must save the state of the old process
and load the saved state for the new process.
The context of a process is represented in the PCB of a process.
Context-switch time is overhead; the system does no useful work while switching.
Time dependent on hardware support, memory management method…
 Eg. Multiple sets of registers
Steps for Context Switch
 Save context of processor including program counter and other registers.
 Update the PCB of the running process with its new state and other associate
information.
 Move PCB to appropriate queue - ready, waiting.
 Select another process for execution (short-term scheduler).
 Update PCB of the selected process.
 Restore CPU context from that of the selected process.
When to Switch a Process?
 A process switch may occur whenever the OS has gained control of CPU
o Trap
 An error resulted from the last instruction. It may cause the process to be
moved to the Exit state.
 System call: Explicit request by the program (ex: file open). The process
will probably be blocked.
o Interrupt
 The cause is external to the execution of the current instruction. Control is
transferred to IH
Cooperating Processes

The concurrent processes may be:
 Independent process cannot affect or be affected by the execution of another process.
P a g e | 41/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems



Cooperating process can affect or be affected by the execution of another process
Advantages of process cooperation
 Information sharing: More than one process may be interested in accessing/manipulating
same data or file at the same time.
 Computation speed-up: tasks may be broken down into multiple smaller subtasks that get
executed in parallel each on some dedicated processor.
 Modularity: constructing complicated software in a modular fashion and having each
module in a separate process can significantly help in efficient and effective software
development.
 Convenience: even individual user may work on many tasks at the same time; for instance,
a user may be editing, printing and compiling at the same time.
Cooperation processes require communication and synchronization
[45].
Communication models: (a) Message passing, (b) Shared
memory.
Producer-Consumer Problem
A Paradigm for cooperating processes, producer process produces information that is consumed by a
consumer process.
P a g e | 42/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
[46].
Producer-consumer problem.





Unbounded-buffer places no practical limit on the size of the buffer (the consumer
may have to wait for new items, but the producer can always produce new items).
 Bounded-buffer assumes that there is a fixed buffer size (the consumer must wait if
the buffer is empty, and the producer must wait if the buffer full).
The producer and consumer must be synchronized
 Consumer does not try to consume an item that has not yet been produced
Buffer implementation
 Explicitly coded by the application programmer with the use of shared memory
 Interprocess communication facility (IPC)
Shared Bounded-Buffer Example
 Shared buffer -- circular array
in: next free position.
out: first full position.
BUFFER_SIZE: max # of items (can only use BUFFER_SIZE – 1 items).
Require that producers and consumers share a common buffer pool, and that the code for
implementing the buffer be written explicitly by the application programmer.
A first Solution
Shared data
#define BUFFER_SIZE 10;
Typedef struct {
...
}item;
item buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
Consumer code
item nextConsumed;
while (1{ )
while (in == out);
//do nothing
nextConsumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
}
Producer code
item nextProduced;
P a g e | 43/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
while (1) {
while (((in + 1) % BUFFER_SIZE) == out); /* do nothing */
buffer[in] = nextProduced;
in = (in + 1) % BUFFER_SIZE;
}
Another solution
Shared data:
#define BUFFER_SIZE 10
typedef struct {
. . .
} item;
item buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
int counter = 0;
Producer:
while(1){
/* produce an item in nextProduct*/
while (counter == BUFFER_SIZE)
; /* do nothing */
buffer[in] = nextProduct;
in = (in + 1) % BUFFER_SIZE;
counter++;
}
Consumer:
while(1){
while (counter == 0)
; /* do nothing */
nextConsumed = buffer[out];
out = (out + 1)% BUFFER_SIZE;
counter--;
/*consume the item in nextConsumed*/
}






P a g e | 44/107
The producer and consumer routines may not function correctly when executed
concurrently.
The statement “counter++” may be implemented in machine language as:
register1 = counter
register1 = register1 + 1
counter = register1
The statement “counter--” may be implemented as:
register2 = counter
register2 = register2 – 1
counter = register2
If both the producer and consumer attempt to update the buffer concurrently, the
assembly language statements may get interleaved.
Interleaving depends upon how the producer and consumer processes are
scheduled.
Assume counter is initially 5. One interleaving of statements is:
Producer: register1 = counter (register1 = 5)
Producer: register1 = register1 + 1 (register1 = 6)
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems


Consumer: register2 = counter (register2 = 5)
Consumer: register2 = register2 – 1 (register2 = 4)
Producer: counter = register1 (counter = 6)
Consumer: counter = register2 (counter = 4)
 The value of count may be either 4 or 6, where the correct result should be 5.
Race condition: The situation where several processes access – and manipulate shared data
concurrently. The final value of the shared data depends upon which process finishes last.
To prevent race conditions, concurrent processes must be synchronized.
P a g e | 45/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
5. CPU Scheduling
Basic Concepts




Maximum CPU utilization obtained with multiprogramming.
 Multiprogramming: are a number of programs can be in memory at the same time.
 The objective of multiprogramming is to have some process running at all times →
maximize CPU utilization.
 CPU scheduling is the basis of multiprogramming.
In a uni-processor only one process in a running state at any given time. If there are more
processes that need to be run, they will have to wait.
All computer resources are scheduled before use. (the CPU is one of the primary computer
resources).
CPU scheduling: Select a process to take over the use of the CPU.
CPU – I/O Burst Cycle

Key to the success of CPU scheduling
 CPU–I/O Burst Cycle – Process execution consists of a cycle of CPU execution and
I/O wait. (Processes alternate between these two states).
 A process begins and ends with a CPU burst.
P a g e | 46/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
[47].
Alternating sequence of CPU and I/O bursts

The following property of processes has been observed:
1. Process execution begins (CPU burst)
2. Process waits for I/O
3. Process does a CPU burst. Now one of two things happens:
 If process is not finished then proceed to step 2.
 If process is finished then quit.
 CPU-burst distribution → a large number of short CPU bursts and a small number of
long CPU bursts
P a g e | 47/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
[48].

Histogram of CPU-Burst times.
Types of processes:
(a) CPU-bound processes (few very long CPU bursts).
(b) I/O-bound processes (many very short CPU bursts).
CPU scheduler



CPU or Short-Term Scheduler: Selects one of the processes in the ready queue to be
executed (allocates the CPU).
A ready queue may be implemented as a FIFO queue, a priority queue, a tree or simply
an unordered link list.
All the processes in the ready queue are lined up waiting for a chance to run on the CPU.
Preemptive Scheduling

CPU scheduling decisions may take place when a process:
1. Switches from running to waiting state. (Ex. I/O request or invocation of wait for
the termination of one of the child processes).
2. Switches from running to ready state. (Ex. Interrupt occurs).
P a g e | 48/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems


3. Switches from waiting to ready. (Ex. Completion of I/O).
4. Switches from new to ready.
5. Terminates.
 Scheduling under 1 and 5 is non-preemptive.
 All others are preemptive.
Non-preemptive or cooperative scheduling
 Once the CPU has been allocated to a process, the process keeps the CUP until it
releases the CPU either by terminating or by switching to the waiting state.
• Microsoft Windows 3.x and Apple Macintosh OS.
Preemptive
 While CPU has been allocated to a process, another process may obtain the CPU
before it terminates or switches to the waiting state.
• Windows 95/98/NT/2000, UNIX.
• MacOS 8 for the PowerPC platform.
 Problem: Ex. The case of two processes sharing data. First process preempted before
updating the data and the second process try to read the data. (Process
synchronization is needed).
Dispatcher



Dispatcher is another part of the scheduling system.
Dispatcher module gives control of the CPU to the process selected by the short-term
scheduler; this involves:
 switching context
 switching to user mode
 jumping to the proper location in the user program to restart that program
Dispatch latency – time it takes for the dispatcher to stop one process and start another
running. (It should be very fast).
Scheduling criteria


Different scheduling algorithms have different properties and may favor one class of processes
over another.
There are many possible scheduling criteria. Below is a list of some of the more common
scheduling criteria:
 CPU utilization – keep the CPU as busy as possible.
 Throughput – # of processes that complete their execution per time unit.
 Waiting time – amount of time a process has been waiting in the ready queue.
• Waiting time of one process: TW= TStart-to-use-CPU – TArrive-to-ReadyQ.
• Total Waiting time of one process (if the process needs CPU burst only):
TWaiting= TFinish-the-job – TArrive – TBurst = TTurnarround – TBurst.
• Average waiting time of n processes: AWT= TWaiting / n.
 Turnaround time – amount of time to execute a particular process.
• Turnaround time of one process: TTurnarround = TFinish-the-job – TArrive).
• Average Turnaround time of n processes: ATT= TTurnarround / n.
 Response time – amount of time it takes from when a request was submitted until the first
response is produced, not output (for time-sharing environment).
 Predictability, fairness, balance resources, priority…
P a g e | 49/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems

Optimization Criteria -- may be conflict
 Max CPU utilization.
 Max throughput.
 Min turnaround time.
 Min waiting time.
 Min response time.
Scheduling algorithms



Below is a list of some well known scheduling algorithms:
 First Come First Served (FCFS) - non-preemptive
 Shortest Job First (SJF) - preemptive or non-preemptive
 Priority - preemptive or non-preemptive
 Round Robin - preemptive
 Multi-Level Queue (MLQ) - preemptive
 Multi-Level Feedback Queue (MLFQ) - preemptive
Each scheduling algorithm has its own criteria to choose the next job that will run on the CPU
Timelines (Gantt chart): to represent the state of the system (and any processes in it) and how it
changes over time.
Gantt chart
First process name
Second process name
more processes
P1 start-time
First-Come, First-Served (FCFS) scheduling








Handles jobs according to their arrival time (The process that arrives first is allocated the
CPU first.).
Simple algorithm to implement (uses a FIFO queue).
When a process enters the ready queue, its PCB is linked onto the tail of the queue.
When the CPU is free, it is allocated to the process at the head of the queue.
Non-preemptive (once the CPU has been allocated to a process, that process keeps the
CPU until it release the CPU, either by terminating or by requesting I/O).
Good for batch systems but not for systems where response time is important (i.e. real
time or time sharing systems).
Turnaround time is changeable.
Example 1:
 Suppose that there are three process that arrive in the order shown below (They all
arrive at time 0, but the system has decided to serve them in this order).
P a g e | 50/107
Process name
Arrive time
TArrive
Burst time
TBurst
Process 1
0
24
Process 2
0
3
Process 3
0
3
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems

The Gantt Chart for the schedule is (ignore the context switch time for simplicity):
Gantt chart for FCFS example 1
P1
P2
0
24
P3
27
30
•
Process 1 is dispatched first and gets to run for 24 time units (there is no
preemption).
• Once process 1 has finished, process 2 is dispatched and gets to run for 3 time
units (24 time units have already elapsed, so process 2 starts at time 24).
• Once process 2 has finished (at time 27) then process 3 is dispatched.
• When process 3 has finished there have been 30 time units used (24 + 3 + 3).
 The following table shows the waiting time for each process:
Process
name
Arrival time
TArrive
Process 1
0
24
24
24
0
Process 2
0
3
27
27
24
Process 3
0
3
30
30
27



Burst time Finish time Turnaround Time
TBurst
TFinish-the-job
TTurnarround
Waiting time
TWaiting
AWT=  TWaiting / n = (0 + 24 + 27) / 3 = 17.
Example2:
The waiting time is obviously dependent on the order in which the processes are served.
Since it was mentioned that all three processes arrive at time 0, the system could have
chosen to dispatch them in this order:

Process
name
Arrival time
TArrive
Burst time
TBurst
Process 2
0
3
Process 3
0
3
Process 1
0
24
Which would have given the following Gantt chart:
Gantt chart for FCFS example 2
P2
0
P a g e | 51/107
P3
3
P1
6
30
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems

The waiting times for the processes in example 2 are:
Process
name
Arrival time
TArrive
Process 1
0
24
30
30
6
Process 2
0
3
3
3
0
Process 3
0
3
6
6
3
Waiting time
TWaiting
AWT=  TWaiting / n = (6 + 0 + 3) / 3 = 3.


Burst time Finish time Turnaround Time
TBurst
TFinish-the-job
TTurnarround
There is a huge difference between the average waiting times in example 1 and example
2, even though the same three processes are being executed.
Shortcomings of FCFS:
 The processes may have to wait for excessively long amounts of time. As shown in
previous two examples.
 Convoy effect: short I/O-bound (short CPU-bursts) wait for CPU-bound (long CPUbursts).

Homework:
Suppose that there are three processes that arrive in the order shown below.
1.

Process
name
Arrive time
TArrive
Burst time
TBurst
Process 1
0
20
Process 2
6
5
Process 3
27
8
Draw the Gantt Chart for the schedule, and Calculate:
1. Average Waiting time (AWT).
2. Average Turnaround time (ATT).
Suppose that there are three processes that arrive in the order shown below.
2.

Process
name
Arrive time
TArrive
Burst time
TBurst
Process 1
2
15
Process 2
5
5
Process 3
25
2
Draw the Gantt Chart for the schedule, and Calculate:
3. Average Waiting time (AWT).
4. Average Turnaround time (ATT).
Shortest Job First (SJF) scheduling

Each process knows the length of its next CPU burst
P a g e | 52/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems





Use these lengths to schedule the process with the shortest time.
If two processes have the same length next CPU burst, FCFS scheduling is used.
Scheduling is done by examining the length of the next CPU burst of a process, rather
than its total length.
Two schemes:
 Non-preemptive – once CPU given to the process it cannot be preempted until
completes its CPU burst.
 Preemptive – if a new process arrives with CPU burst length less than remaining time
of current executing process, preempt.
• Shortest-Remaining-Time-First (SRTF).
SJF is optimal – gives minimum average waiting time for a given set of processes.
(Think about it, if you run all the short jobs first, then each subsequent job has a
relatively short waiting time. If you were to run all the long jobs first, then every
subsequent job would have a longer time to wait).
Non-preemptive SJF example

In the following example there are four processes.
Process
name
Arrival time
TArrive
Burst time
TBurst
Process 1
0
7
Process 2
2
4
Process 3
4
1
Process 4
5
4
•
•
At time 0 only one Process available (Process 1).
At time 2 Process 2 arrives, but Process 1 is already running and there
is no preemption, so even though Process 2 is now the shortest it has to
wait until Process 1 finishes.
• At time 4 Process 3 arrives (it is now the shortest) but Process 1 has
still not finished.
• At time 5 Process 4 arrives and Process 3 is still the shortest.
• When Process 1 finishes, then Process 3 will run (it is the shortest).
• When Process 3 has finished then Process 2 will run (It has the same
length as 4, but arrived earlier so we will choose it).
• Finally Process 4 will run.
 The Gantt Chart for the schedule is:
Gantt chart for SJF example 1
P1
0

P3
7
P4
8
12
16
The waiting times for the processes in this example are:
Process
name
Arrival time
TArrive
Process 1
0
P a g e | 53/107
P2
Burst time Finish time Turnaround Time
TBurst
TFinish-the-job
TTurnarround
7
7
7
Waiting time
TWaiting
0
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
Process 2
2
4
12
10
6
Process 3
4
1
8
4
3
Process 4
5
4
16
11
7
AWT=  TWaiting / n = (0 + 6 + 3 + 7) / 4 = 4.

Preemptive SJF (or shortest-remaining-time-first (SRTF)) example

In the following example there are again four processes (note that process 3 now has a
burst time of 1).
•
•
•
•
•
•

Process
name
Arrival time
TArrive
Burst time
TBurst
Process 1
0
7
Process 2
2
4
Process 3
4
1
Process 4
5
4
At time 0 there is only 1 process available (Process 1).
At time 2 Process 2 arrives and have a shorter next CPU burst than
what is left of the Process 1. Process 1 removed from the CPU and
replaced with process 2 (preemptive SJF).
At time 4 Process 3 arrives (it is now the shortest) and it replaces
Process 2 on the CPU.
At time 5 Process 3 finishes and Process 4 arrives. Process 2 is once
again the shortest and it gains the CPU, running until time 7.
At time 7 Process 4 is the shortest and runs until time 11.
Finally, Process 1 (the only Process left) is now the shortest and
finishes.
The Gantt Chart for the schedule is:
Gantt chart for SJF example 2
P1
0
P2
2

P3
P2
4
P4
5
P1
7
11
16
The waiting times for the processes in this example are:
Process
name
Arrival time
TArrive
Process 1
0
7
16
16
9
Process 2
2
4
7
5
1
Process 3
4
1
5
1
0
Process 4
5
4
11
6
2

P a g e | 54/107
Burst time Finish time Turnaround Time
TBurst
TFinish-the-job
TTurnarround
Waiting time
TWaiting
AWT=  TWaiting / n = (9 + 1 + 0 + 2) / 4 = 3.
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems

Difficulties to Implement SJF or SRTF:
 Starvation problem – Long job may never get CPU
 Determining Length of Next CPU Burst (it hasn't happened yet).
• Can only estimate the length.
• Can be done by using the length of previous CPU bursts, using exponential
averaging.
1. t n  actual lenght of n th CPU burst
2.  n 1  predicted value for the next CPU burst
3.  , 0    1. (more commenly,   1 2)
4. Define :  n1   t n  1    n .
Prediction of the length of the next CPU burst.
[49].
Prediction of the length of the next CPU burst
Homework:

Suppose that there are four processes that arrives in the order shown below
Process
name
Arrival time
TArrive
CPU Burst time
TBurst
I/O burst time
CPU Burst time
TBurst
Process 1
0
7
10
5
Process 2
2
4
20
2
Process 3
4
1
2
3
Process 4
5
4
1
7

Draw the Gantt Chart for the schedule, and Calculate:
5. Average Waiting time (AWT).
6. Average Turnaround time (ATT).
7. CPU Utilization.
P a g e | 55/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems

In answering the questions, use (1) Non-Preemptive SJF Scheduling.
(2) Preemptive SRTF Scheduling.
Priority scheduling


A priority number (integer) is associated with each process.
The CPU is allocated to the process with the highest priority (smallest integer  highest
priority).
 Preemptive (high priority job can remove a low priority job from the CPU and take
over) or Non-preemptive.
 SJF is a special case of the priority scheduling where priority is the predicted next
CPU burst time.
 Equal-priority processes are scheduled in FCFS order.
There are different methods of assigning priorities:
1- Externally: (external to the OS)
Example: assigning based on the position of the user: researcher first, student last.
2- Internally: (By processor manager)
Example: assigning based on characteristics of the job such as:
 Memory requirements.
 Number and type of peripheral devices.
 Total CPU time.
 Amount of time spent in the system.
Problem  Priority based scheduling may not be fair (indefinite blocking (or starvation)
problem) – low priority processes may never execute.
 Solution: Aging – as time passes, increase the process priority.



Example
Process
name
CPU Burst time
TBurst
Priority
Process 1
10
3
Process 2
1
1
Process 3
2
4
Process 4
1
5
Process 5
5
2
Gantt chart Priority scheduling
P2
0
P5
1
P1
P3
6
P4
16
18
Process
name
Arrival time
TArrive
Process 1
0
10
16
16
6
Process 2
0
1
1
1
0
Process 3
0
2
18
18
16
P a g e | 56/107
Burst time Finish time Turnaround Time
TBurst
TFinish-the-job
TTurnarround
19
Waiting time
TWaiting
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
Process 4
0
1
19
19
18
Process 5
0
5
6
6
1
60
41
Total
AWT=  TWaiting / n = (6 + 0 + 16 + 18 + 1) / 5 = 8.2.
Round Robin (RR) scheduling


Designed especially for time-sharing systems.
Each process gets a small unit of CPU time (time quantum or time slice), usually 10-100
milliseconds. After this time has elapsed, the process is preempted and added to the end
of the ready (circular) queue.

If there are n processes in the ready queue and the time quantum is q, then each process
gets 1/n of the CPU time in chunks of at most q time units at once. No process waits
more than (n-1)q time units.
A context switch occurs at the end of each quantum.
Performance
 q large (If the time quantum is larger than the largest CPU burst for all of the
processes, then no process will be preempted). FIFO
 q small  q must be large with respect to context switch, otherwise overhead is too
high.
+ve: it’s fair; each job gets an equal shot at a CPU.
-ve: Averaging waiting time can be bad.




Example: RR with Time Quantum = 20msec.
Process
name
Arrival time
TArrive
Burst time
TBurst
Process 1
0
53
Process 2
0
17
Process 3
0
68
Process 4
0
24
Gantt chart Priority scheduling
P1
0
P2
20
P a g e | 57/107
P3
37
P4
57
P1
77
P3
97
P4
117
P1
121
P3
134
P3
154
162
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
Process
name
Arrival time
TArrive
Burst time Finish time Turnaround Time
TBurst
TFinish-the-job
TTurnarround
Process 1
0
53
134
134
81
Process 2
0
17
37
37
20
Process 3
0
68
162
162
94
Process 4
0
24
121
121
97
454
292
Total
Waiting time
TWaiting
AWT=  TWaiting / n = (81 + 20 + 94 + 97) / 4 =73.

Typically, higher average turnaround than SRTF, but better response.
Homework:



Suppose that there are four processes that arrives in the order shown below
Process
name
Arrival time
TArrive
Burst time
TBurst
Process 1
0
53
Process 2
7
17
Process 3
15
68
Process 4
40
14
Draw the Gantt Chart for the RR schedule with Time Quantum = 20msec, and
Calculate:
1. Average Waiting time (AWT).
2. Average Turnaround time (ATT).
How a Smaller Time Quantum Increases Context Switches
P a g e | 58/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems

Turnaround Time Varies With the Time Quantum
Multi-level queue scheduling


The previous scheduling algorithms have treated different kinds of process in the same
fashion. Multi-level queue scheduling makes a distinction between processes.
Examples:
 Put all CPU-bound jobs in one queue and all I/O-bound jobs in another.
Alternately select jobs from each queue to keep system balanced.
P a g e | 59/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
Put batch (non-interactive) processes in “background queue” and interactive processes
in “foreground queue”; treat foreground queue more favorably than background
queue.
Interactive processes generally require much faster response times than non-interactive
processes. (schedule interactive processes more quickly than non-interactive ones).
Ready queue is partitioned into separate queues
 According to process properties and scheduling needs.
 Foreground (interactive) and background (non-interactive/batch).







When a process enters the system, it is automatically identified as a certain type of
process and allocated to the correct queue.
Processes are permanently assigned to one queue.
Each queue has its own scheduling algorithm, foreground – RR, background – FCFS.
An example of a 5 level queuing strategy (1 has highest priority, 5 has lowest) is shown
below:
1. System processes
2. Interactive processes
3. Interactive editing processes
4. Batch processes
5. Student processes
P a g e | 60/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems

Scheduling must be done between the queues.
 Fixed priority scheduling; i.e., serve all from higher-priority then from lower-priority.
Possibility of starvation.
 Time slice – each queue gets a certain amount of CPU time which it can schedule
amongst its processes; eg., 80% to foreground in RR 20% to background in FCFS.
Multi-level feedback queue scheduling


A process can move between the various queues
Idea
 Separate processes with different CPU-burst characteristics. If a process uses too
much CPU time, it will be moved to a lower-priority queue.
 This scheme Leaves I/O-bound and interactive processes in the higher-priority
queues.
 A process waiting too long in a lower-priority queue may be moved to a higherpriority queue( aging can be implemented this way).

Multilevel-feedback-queue scheduler defined by the following parameters:
 number of queues
 scheduling algorithms for each queue
 method used to determine when to upgrade a process
 method used to determine when to demote a process
 method used to determine which queue a process will enter when that process needs
service

Example of Multilevel Feedback Queue:

Three queues:
• Q0 – time quantum 8 milliseconds
• Q1 – time quantum 16 milliseconds
• Q2 – FCFS
 Scheduling
• A new job enters queue Q0 which is served FCFS. When it gains CPU, job
receives 8 milliseconds. If it does not finish in 8 milliseconds, job is moved to
queue Q1.
P a g e | 61/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
•
At Q1 job is again served FCFS and receives 16 additional milliseconds. If it
still does not complete, it is preempted and moved to queue Q2.
•
Example: FCFS with 3 queues (Q0 –quantum= 8 milliseconds, Q1 –
quantum= 16 milliseconds, Q2 – quantum= ).
Process
name
Arrival time
TArrive
Burst time
TBurst
Process 1
0
28
Process 2
0
10
Process 3
0
5
Process 4
0
26
Gantt chart Priority scheduling



In Q1 (Quantum = 8):
 P1 uses 8 and needs 20 more
 P2 uses 8 and needs 2 more
 P3 uses 5 and terminates
 P4 uses 8 and needs 18 more
So P1 (20), P2 (2) , P4 (18) go to Q2 (Quantum =16):
 P1 uses 16 and needs 4 more;
 P2 uses 2 and terminates;
 P4 uses 16 and needs 2 more;
So P1(4), P4(2) go to Q3 (Quantum= )
 P1 uses 4 and terminates;
 P4 uses 2 and terminates.
P a g e | 62/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
Homework: consider the following set of processes.


Process
name
Arrival time
TArrive
Burst time
TBurst
Process 1
0
44
Process 2
1
9
Process 3
2
13
Assume a context switch takes one time unit. And assume scheduler was invoked at time
2.
Draw four Gantt charts illustrating the execution of these processes and calculate
averaging waiting time using FCFS, SJF, RR (quantum=10), and MLFQ (with 3 queues
as shown below).
P a g e | 63/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
6.
Process Synchronization
Background

The concurrent processes executing in the operating system may be either independent processes
or cooperating processes.
 A process is independent if it cannot affect or be affected by the other processes executing in the
system.
 A process is cooperating if it can affect or be affected by the other processes executing in the
system.
 Provide an environment that allows process cooperation for several reasons:
 Information sharing: several users may be interested in the same piece of information (ex:
a shared file).
 Computation speed-up: break a task into subtasks, each of which will be executing in
parallel with the others.
 Modularity: dividing the system functions into separate processes or threads.
 Convenience: even an individual user may have many tasks on which to work at one time.
(ex: a user may be editing, printing, and compiling in parallel),
 Concurrent access to shared data may result in data inconsistency.
 Cooperation processes require synchronization (to ensure the orderly execution of
cooperating processes).
 Example : Producer - Consumer problem.
 Paradigm for cooperating processes, producer process produces information that is
consumed by a consumer process.
 Unbounded-buffer places no practical limit on the size of the buffer (the consumer
may have to wait for new items, but the producer can always produce new items).
 Bounded-buffer assumes that there is a fixed buffer size (the consumer must wait if
the buffer is empty, and the producer must wait if the buffer full).
 The producer and consumer must be synchronized
 Consumer does not try to consume an item that has not yet been produced
 Shared Bounded-Buffer using counter example:
Shared data:
#define BUFFER_SIZE 10
typedef struct {
. . .
} item;
item buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
int counter = 0;
Producer:
while(1){
/* produce an item in nextProduct*/
while (counter == BUFFER_SIZE)
; /* do nothing */
buffer[in] = nextProduct;
in = (in + 1) % BUFFER_SIZE;
counter++;
}
Consumer:
P a g e | 64/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
while(1){
while (counter == 0)
; /* do nothing */
nextConsumed = buffer[out];
out = (out + 1)% BUFFER_SIZE;
counter--;
/*consume the item in nextConsumed*/
}



The producer and consumer routines may not function correctly when executed
concurrently.
 The statement “counter++” may be implemented in machine language as:
register1 = counter
register1 = register1 + 1
counter = register1
 The statement “counter--” may be implemented as:
register2 = counter
register2 = register2 – 1
counter = register2
 If both the producer and consumer attempt to update the buffer concurrently, the
assembly language statements may get interleaved.
 Interleaving depends upon how the producer and consumer processes are
scheduled.
 Assume counter is initially 5. One interleaving of statements is:
Producer: register1 = counter (register1 = 5)
Producer: register1 = register1 + 1 (register1 = 6)
Consumer: register2 = counter (register2 = 5)
Consumer: register2 = register2 – 1 (register2 = 4)
Producer: counter = register1 (counter = 6)
Consumer: counter = register2 (counter = 4)
 The value of count may be either 4 or 6, where the correct result should be 5.
Race condition: The situation where several processes access – and manipulate shared data
concurrently. The final value of the shared data depends upon which process finishes last.
To prevent race conditions, concurrent processes must be synchronized.
The Critical-Section Problem









n processes all competing to use some shared data.
Each process has a code segment, called critical section (CS), in which the shared data is
accessed.
Problem – ensure that when one process is executing in its critical section, no other process is
allowed to execute in its critical section
 Even with multiple CPUs
The execution of critical sections by the processes is mutually exclusive in time.
Each process must request the permission to enter its CS.
The critical section problem is to design a protocol that the processes can use to cooperate, so
that their action will not depend on the order in which their execution is interleaved (possibly on
many processors).
The section of code implementing this request is called the entry section.
The critical section (CS) might be followed by an exit section.
The remaining code is the remainder section.
P a g e | 65/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
do {
entry section
critical section
exit section
reminder section
} while (1);


General structure of a typical process Pi
A solution to the CS problem must satisfy:
 Mutual Exclusion: At any time, at most one process can be in its critical section (CS)
 Progress: If no process is executing in its CS and there exist some processes that wish to
enter their CS…
 Only processes that are not executing in their RS can participate in the decision of
who will enter next in the CS.
 This selection cannot be postponed indefinitely
 Bounded Waiting:
 After a process has made a request to enter its CS, there is a bound on the number
of times that the other processes are allowed to enter their CS and before that
request is granted
 otherwise the process will suffer from starvation
Types of solutions:
 Software solutions (Algorithms)
 Hardware solutions (some special machine instructions)
 Operation System solutions
Software solutions


We consider first the case of 2 processes
 Algorithm 1 and 2 are incorrect.
 Algorithm 3 is correct (Peterson’s algorithm)
Then we generalize to n processes -- Bakery algorithm
Two-Process Solutions

Algorithm1: (Two Processes (P0 and P1) solutions)
Algorithm 1
Shared variables:
int turn;
initially turn =0
turn=i >>Pi can enter its CS
Process Pi
do{
P a g e | 66/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
while (turn!=i);
critical section
turn =j;
remainder section
}while(1);
Satisfies mutual exclusion, but not progress.
 Ex: P0 has a large RS and P1 has a small RS. If turn=0, P0 enter its CS and then
its long RS (turn=1). P1 enter its CS and then its RS (turn=0) and tries again to
enter its CS: request refused! He has to wait that P0 leaves its RS.
Process P0:
Do
while(turn!=0){};
CS
turn:=1;
RS
While (1);

Process P1:
Do
while(turn!=1){};
CS
turn:=0;
RS
While (1);
Algorithm2: (Two Processes (P0 and P1) solutions)
Algorithm 2
Shared variables:
Boolean flag[2];
initially flag[0]=flag[1]= false.
flag[i]=true >> Pi ready to enter its critical section
Process Pi
do{
flag[i]=true;
while (flag[j]);
critical section
flag[i]=false;
remainder section
}while(1);
Satisfies mutual exclusion, but not progress.

Algorithm3: (Two Processes (P0 and P1) solutions)
Algorithm3(1 + 2)
Shared variables:
combine from 1 and 2
Process Pi
do{
flag[i]=true;
turn =j;
while(flag[j]&&turn==j);
critical section
flag[i]=false;
remainder section
}while(1);
P a g e | 67/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
Meets all three requirements.

Algorithm3 satisfies (Proof of Correctness):
 Mutual Exclusion: (Both P0 and P1 are not in CS at the same time)
• P0, P1 in CS => flag[0] = flag[1] = true;
• P0 in CS => flag[1] = false or turn = 0;
• P1 in CS => flag[0] = false or turn = 1;
• Therefore, P0 and P1 are both in CS => turn is both 0 and 1 which is
impossible
 Progress and Bounded-Waiting:
• Pi cannot enter CS only if stuck in while() with condition flag[ j] = true
and turn = j.
• If Pj is not ready to enter CS then flag[ j] = false and Pi can then enter
its CS
• If Pj has set flag[ j]=true and is in its while(), then either turn=i or
turn=j
• If turn=i, then Pi enters CS. If turn=j then Pj enters CS but will then
reset flag[ j]=false on exit: allowing Pi to enter CS
• but if Pj has time to reset flag[ j]=true, it must also set turn=i
• since Pi does not change value of turn while stuck in while(), Pi will
enter CS after at most one CS entry by Pj (bounded waiting)
Multiple-Process Solution (Bakers Algorithm)

Before entering their CS, each Pi receives a number. Holder of smallest number
enters CS (like in bakeries, ice-cream stores...)
When Pi and Pj receives same number:
 If i<j then Pi is served first, else Pj is served first.
Pi resets its number to 0 in the exit section.
The numbering scheme always generates numbers in increasing order of
enumeration; i.e., 1,2,3,3,3,3,4,5...
Notation:
 (a,b) < (c,d) if a < c or if a == c and b < d
 max(a0,...,an-1) is a number k such that k >= ai for i=0..n-1
Shared data:
 Boolean choosing[n] ;
• initialized to false
 int number[n];
• initialized to 0
Correctness relies on the following fact:
 If Pi is in CS and Pk has already chosen its number[k]!= 0, then (number[i],i) <
(number[k],k)






Algorithm 3 (bakery algorithm)
do {
choosing[i]
number[i] =
choosing[i]
for (j = 0;
P a g e | 68/107
= true;
max(number[0], number[1], …, number [n – 1])+1;
= false;
j < n; j++) {
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
while (choosing[j]);
while ((number[j] != 0) &&
(number[j],j)<(number[i],i)));
}
critical section
number[i] = 0;
remainder section
} while (1);

Drawbacks of Software Solutions
 Processes that are requesting to enter in their critical section are busy waiting
(consuming processor time needlessly)
 If Critical Sections are long, it would be more efficient to block processes that are
waiting.
Hardware Solutions
Synchronization Hardware
1. Interrupt Disabling
 On a uniprocessor: mutual exclusion is preserved but efficiency of execution is
degraded: while in CS, we cannot interleave execution with other processes that
are in RS (no other instructions would be run, so no unexpected modifications
could be made to the shared variable).
Process Pi:
Do{
disable interrupts
critical section
enable interrupts
remainder section
}while(1);

On a multiprocessor: disabling interrupts on a multiprocessor can be timeconsuming, as the message is passed to all the processors.
 this solution not feasible in a multiprocessor
2. Special Machine Instructions
 The special machines instructions that perform 2 actions atomically
(indivisible)
 Ex: test and modify the content of a word, or to swap the contents of two
words, atomically- that is, as one uninterruptible unit
 The execution of such an instruction is also mutually exclusive (even with
multiple CPUs).
 They can be used to provide mutual exclusion but need to be complemented by
other mechanisms to satisfy the other 2 requirements of the CS problem (and
avoid starvation and deadlock).

P a g e | 69/107
The Test-and-Set Instruction: Test and modify the content of a word atomically.
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
Test-and-Set instruction
boolean TestAndSet(boolean &target){
boolean rv = target;
target = true;
return rv;
}
Mutual Exclusion implementation with TestAndSet
Shared data:
boolean lock; (initially false)
Process Pi
do{
while (TestAndSet(lock));
critical section
lock =false;
remainder section
}while(1);

Mutual exclusion is preserved: if Pi enters CS, the other Pj are busy
waiting.
 When Pi exits CS, the selection of the Pj who will enter CS is arbitrary: no
bounded waiting. Hence starvation is possible.


Mutual-exclusion implementations with TestAndSet instruction do not satisfy
the bounded-waiting requirement.
Solution: Bounded-waiting mutual exclusion with TestAndSet
Bounded-waiting mutual exclusion with TestAndSet
Shared data:
boolean lock; (initially false)
boolean waiting[n]; (initially false)
Process Pi
do{
waiting[i] = true;
key = true; /* local boolean variable for each process */
while (waiting[i] && key)
Key = TestAndSet(lock);
Waiting[i]=false;
critical section
j = (i+1) % n;
while ((j != i) && !waiting[j])
j = (j+1) % n;
If (j == i)
lock =false;
else
Waiting[j] = false;
remainder section
P a g e | 70/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
}while(1);

Mutual Exclusion: Pi can enter CS only if either waiting[i]==false or
key==false
 Key becomes false only if TestAndSet is executed

First process to execute TestAndSet find key==false; others
wait
 waiting[i] becomes false only if other process leaves CS

Only one waiting[i] is set to false
 Bounded waiting: waiting in the cyclic order
Semaphores


Synchronization tool (provided by the OS) that does not require busy waiting.
A semaphore S is an integer variable (counting semaphore) that, apart from initialization, can
only be accessed through 2 atomic and mutually exclusive operations:
 wait(S)
 signal(S)
wait(S) {
while (S<=0)
;// no-loop
S--;
}



signal(S) {
S++;
}
wait(S): decrements the semaphore value.
signal(S): increments the semaphore value.
wait and signal are assumed to be atomic.
 They cannot be interrupted and each routine can be treated as an indivisible step.
Usage


We can use semaphores to deal with the n-process critical-section problem.
The n processes share a semaphore, mutex (standing for mutual exclusion), initialized to
1.
Mutual-exclusion implementation with semaphores
Shared data:
semaphore mutex; //initially mutex = 1
Process Pi
do{
wait(mutex);
critical section
signal(mutex);
remainder section
}while(1);

Example:
P a g e | 71/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems



consider two concurrently running processes:
 P1 with a statement S1
 P2 with a statement S2
Suppose that we require that S2 be executed only after S1 has completed.
Solution:
 P1 and P2 share a common semaphore synch, initialized to 0.
 Insert the following statements in P1
S1;
Signal(synch);
 Insert the following statements in P2
wait(synch);
S2;
Implementation







The main disadvantage of the mutual-exclusion solutions is that they all require busy
waiting.
Busy waiting (continuation looping) is a problem in a real multiprogramming system,
where a single CPU is shared among many processes. → wastes CPU cycles
Spinlock: busy wait.
Spinlocks are useful in multiprocessor systems.
The advantage of a spinlock is that no context switch is required when a process must
wait on a lock.
 It is useful when locks are expected to be held for short times.
To avoid busy waiting: we can modify the definition of the wait and signal semaphore
operations. When a process has to wait, it will be put in a blocked queue of processes
waiting for the same event.
The implementation of the semaphore
Define a semaphore as a record
typedef struct{
int value;
struct process *L;
}semaphore;

Assume two simple operations:
 block suspends the process that invokes it.
 wakeup(P) resumes the execution of a blocked process P.
Define wait semaphore operation
void wait(semaphore S) {
S.value--;
If (S.value < 0) {
add this process to S.L;
Block();
}
}
Define signal semaphore operation
P a g e | 72/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
void signal(semaphore S) {
S.value++;
If (S.value <= 0) {
remove a process P from S.L;
wakeup(P);
}
}



When a process must wait for a semaphore S, it is blocked and put on the
semaphore’s queue.
 The queue links PCB.
 The signal operation removes (according to a fair policy like FIFO) one process
from the queue and puts it in the list of ready processes
 Block and wakeup change process state – they are basic system calls
 Block: from running to waiting
 wakeup: from waiting to ready
Note: under the classical definition of semaphores with busy waiting the semaphore value
is never negative; this implementation may have negative semaphore values.
 If the semaphore value is negative, its magnitude is the number of processes
waiting on that semaphore.
Semaphores: Observations
 When S.value >=0: the number of processes that can execute wait(S) without
being blocked = S.value
 When S.value<0: the number of processes waiting on S is = |S.value|
 Atomicity and mutual exclusion: no 2 process can be in wait(S) and signal(S) (on
the same S) at the same time (even with multiple CPUs)
 Hence the blocks of code defining wait(S) and signal(S) are, in fact, critical
sections
 Wait and Signal must be executed atomically (mutual execution)
 The critical sections defined by wait(S) and signal(S) are very short: typically 10
instructions
 Solutions:
 uniprocessor: disable interrupts during these operations (ie: for a very
short period). This does not work on a multiprocessor machine.
 multiprocessor: use previous software or hardware schemes. The amount
of busy waiting should be small.
Deadlock and Starvation


Deadlock – two or more processes are waiting indefinitely for an event that can be
caused by only one of the waiting processes.
Let S and Q be two semaphores initialized to 1
P0
wait(S);
wait(Q);

signal(S);
signal(Q)

P1
wait(Q);
wait(S);

signal(Q);
signal(S);
Starvation (or indefinite blocking): A process may never be removed from the semaphore
queue in which it is suspended
P a g e | 73/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems

Starvation may occur if we add and remove processes from the semaphore queue in
LIFO order
Binary Semaphores


Two Types of Semaphores
 Counting semaphore – integer value can range over an unrestricted domain.
 Binary semaphore – integer value can range only between 0 and 1; can be simpler
to implement.
Can implement a counting semaphore S as a binary semaphore.
Data structures
binary-semaphore S1,S2;
int C;
Initialization
S1 = 1;
S2 = 0;
C = initial value of semaphore S
wait operation
wait(S1);
C--;
If (C < 0) {
signal(S1);
wait(S2);
}
signal(S1);
signal operation
wait(S1);
C++;
If (C <= 0)
signal(S2);
else
signal(S1);
P a g e | 74/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
DEADLOCKS
INTRODUCTION
The Deadlock Problem
Bridge Crossing Example:
 Traffic only in one direction.
 Each section of a bridge can be viewed as a resource.
 If a deadlock occurs, it can be resolved if one car backs up (preempt resources and rollback).
 Several cars may have to be backed up if a deadlock occurs.
 Starvation is possible.
In multiprogramming environment:
 Several processes may compete for a finite number of resources.
 A process requests resources; if the resources are not available at the time, the process enters
a wait state.
 Waiting processes may never again change state, because the resources they have requested
are held by other waiting processes. (This situation is called a deadlock).
System Model
•
•
Resource types R1, R2, . . ., Rm
 CPU cycles, memory space, I/O devices, file, semaphores, monitors
 Each resource type Ri has Wi identical instances
 Any instance of the type will satisfy a request for that type
Each process utilizes a resource as follows:
 Request: One of two things can happen when a resource is requested:
 The request can be granted immediately, if the resource is available.
 Otherwise the process waiting until it can acquire the resource.
 Use
 Release: When the process no longer needs the resource it releases the resources.
P a g e | 75/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
•
•
•
Request and release are system calls
 Request/release device, open/close file, allocate/free memory
 Wait/signal
A set of processes is in a deadlock state when every process in the set is waiting for an
event that can be caused only by another process in the set.
Example:
 System has 2 tape drives.
 P1 and P2 each hold one tape drive and each needs another one.
Deadlock Characterization
Necessary Conditions
•
Deadlock can arise if four conditions hold simultaneously.
1. Mutual exclusion: At least one resource must be held in a non-sharable mode; that
is, only one process at a time can use the resource.
 Sharable resources such as read only file.
 Non-sharable resources such as printer.
2. Hold and wait: a process holding at least one resource is waiting to acquire
additional resources held by others.
3. No preemption: a resource can be released only voluntarily by the process holding
it, after it has completed its task.
4. Circular wait: there exists a set {P0, P1, …, P0} of waiting processes such that P0 is
waiting for a resource that is held by P1, P1 is waiting for a resource that is held by
P2, …, Pn–1 is waiting for a resource that is held by Pn, and Pn is waiting for a
resource that is held by P0.
Resource-Allocation Graph



A set of vertices V and a set of edges E.
V is partitioned into two types:
o P = {P1, P2, …, Pn} (All processes in the system)
o R = {R1, R2, …, Rm} (All resources in the system)
Two kinds of edges
o Request edge – directed edge Pi  Rj
o Assignment edge – directed edge Rj  Pi
Representation
Process:
Resource type with 4 instances:
Pi requests instance of Rj:
Pi is holding an instance of Rj:
P a g e | 76/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
Example1:
The resource allocation graph shown bellow depicts the following situation.
[50].
Hold-and-wait and circular-wait not satisfies → no deadlock
The sets P, R, and E:
 P= {p1, p2, p3}
 R= {R1, R2, R3, R4}
 E= {P1 →R1, P2 → R3, R1 → P2, R2 → P2, R2 → P1, R3 → P3}
Resource instances:
 One instance of resource type R1
 Two instances of resource type R2
 One instance of resource type R3
 Three instances of resource type R4
Process States:
• P1 is holding an instance of resource type R2, and is waiting for an instance of resource type
R1 .
• P2 is holding an instance of R1 and R2, and is waiting for an instance of resource type R3.
• P3 is holding an instance of R3.
Example2: Resource Allocation Graph With Deadlock:
P a g e | 77/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
[51].
Four conditions satisfies → deadlock
Example3: Resource Allocation Graph With a cycle but no deadlock:
Hold-and-wait not satisfy → no deadlock
. ]25[
Basic Facts :
• If graph contains no cycles → no deadlock.
• If graph contains a cycle
• If only one instance per resource type, then a cycle is both a necessary and a sufficient
condition for the existence of deadlock.
• If each resource type has several instances, then a cycle is a necessary but not a sufficient
condition for the existence of deadlock.
Methods for handling Deadlocks
Use a protocol to prevent or avoid deadlocks, ensuring that the system will never enter a deadlock
state
P a g e | 78/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
• Deadlock prevention
• Deadlock avoidance
• Allow the system to enter a deadlock state, detect it and then recover (Deadlock Detection).
Ignore the problem and pretend that deadlocks never occur in the system
• Used by most operating systems, including UNIX
• The undetected deadlock will result in the deterioration of the system performance.
Eventually, the system will stop functioning and will need to be restarted manually
Deadlock Prevention
•
•
Ensure that at least one of the necessary conditions cannot hold
Prevent deadlocks by constraining how requests for resources can be made
Mutual Exclusion



Not required for sharable resources; must hold for non-sharable resources.
For example:
o A printer (non-sharable) cannot be simultaneously shared by several processes.
o Read only files (sharable) not require mutual exclusion access.
In general, however, we cannot prevent deadlocks by denying the mutual-exclusion
condition: some resources are intrinsically non-sharable.
Hold and Wait



We must guarantee that whenever a process requests a resource, it does not hold any other
resources.
o Method1: require each process to request and be allocated all its resources before it
begins execution
o Method 2: allow a process to request resources only when the process has none
Example: copy data from tape drive to disk file, sort disk file, print
Disadvantage
o Low resource utilization
o Starvation possible
No Preemption

If process A holding resources requests another resource that cannot be immediately allocated
to it
o Method: Check the requested resources for following conditions
o If it is allocated to a process waiting for additional resources, preempt it from the
waiting process and allocate it to A
o If it is held by a process not waiting, A must wait
 A’s resources may be preempted (implicitly released), but only if another process requests
them
o Often applied to resources whose state can be easily saved and restored later, such as
CPU registers and memory space
Circular Wait
•
•
Impose a total ordering of all resource types, and each process requests resources in an
increasing order of enumeration.
Assign each resource type a unique integer number.
 Example: F(tape drive) = 1, F(disk drive) = 5, F(Printer) = 12
P a g e | 79/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems

•
•
•
F is defined according to the normal order of resource usage
Method 1: require that each process requests resources in an increasing order of
enumeration
 OK if tape drive → disk drive → Printer
 Not OK if disk drive → tape drive →Printer
Method 2: Whenever a process requests an instance of Rj, it has released any resources
Ri such that F(Ri) > F(Rj)
Proof by contradiction:
 Let the set of processes involved in the circular wait be {P0, P1, …., Pn}, where Pi is
waiting for a resource Ri, which is held by process Pi+1.
 Modulo arithmetic is used on the indexes, so that Pn is waiting for a resource Rn
held by P0
 We must have F(Ri) < F(Ri+1), for all i. → F(R0) < F(R1) < .. < F(Rn) < F(R0).
 By transitivity, F(R0) < F(R0), which is impossible. → no circular wait.
Deadlock Avoidance
•
•
•
•
Require some additional a priori information about which resources a process will
request and use during its life time
The simplest and most useful model requires that each process declare the maximum
number of resources of each type that it may need
The deadlock-avoidance algorithm dynamically examines the resource-allocation state to
ensure that there can never be a circular-wait condition
Resource-allocation state is defined by the number of available and allocated resources,
and the maximum demands of the processes
Safe State
•
•
•
•
•
•
•
•
A state is safe if the system can allocate resources to each process (up to its maximum) in
some order and still avoid a deadlock.
System is in safe state only if there is a safe sequence of all processes.
Sequence <P1, P2, …, Pn> is safe if for each Pi, the resources that Pi can still request can
be satisfied by currently available resources + resources held by all the Pj, with j<i.
 If Pi resource needs are not immediately available, then Pi can wait until all Pj have
finished.
 When Pj-1 is finished, Pi can obtain needed resources, execute, return allocated
resources, and terminate.
 When Pi terminates, Pi+1 can obtain its needed resources, etc.
When a process requests an available resource, system must decide if immediate
allocation leaves the system in a safe state.
If a system is in safe state → no deadlocks.
If a system is in unsafe state → possibility of deadlock.
Avoidance → ensure that a system will never enter an unsafe state.
Example: Consider a System with 12 magnetic tape drives and 3 processes: P0, P1 and
P2.
P0
P a g e | 80/107
Maximum Needs
10
Allocation (at t0)
5
Needs
5
Available
3
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
P1
P2


4
9
2
2
2
7
The sequence < P1, P0, P2> satisfies the safety condition.
Suppose that, at time t1, process P2 requests and is allocated 1 more tape drive. The
system is no longer in a safe state. Only P1 can be allocated all its tape drives.
[53].
Safe, unsafe, and deadlock state spaces
Resource-Allocation Graph Algorithm
•
•
Work for a resource-allocation system with only one instance of each resource type
Claim edge Pi  Rj indicated that process Pj may request resource Rj; represented by a
dashed line
 Claim edge converts to request edge when a process requests a resource
 When a resource is released by a process, assignment edge reconverts to a claim
edge
 Resources must be claimed a priori in the system
No cycle exists.
The system in a
safe state
[54].


Resource-allocation graph for deadlock avoidance
Suppose that P2 requests R2. Although R2 is currently free, we cannot allocate it to p2, since this
action will create a cycle in the graph.
A cycle indicates that the system is in an unsafe state (deadlock may occur).
P a g e | 81/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
[55].
An unsafe state in a resource-allocation graph
Banker’s Algorithm
•
•
•
•
•
Work for a resource-allocation system with multiple instances of each resource type
Each process must declare a priori claim maximum use
When a process requests a resource it may have to wait even if the requested resources
are available
When a process gets all its resources it must return them in a finite amount of time
Data Structures for the Banker’s Algorithm
 n = number of processes, m = number of resources types
 Available: Vector of length m. If Available[j] = k, there are k instances of resource
type Rj available.
 Max: n x m matrix. If Max [i,j] = k, then process Pi may request at most k instances
of resource type Rj.
 Allocation: n x m matrix. If Allocation [i,j] = k then Pi is currently allocated k
instances of Rj.
 Need: n x m matrix. If Need [i,j] = k, then Pi may need k more instances of Rj to
complete its task.
 Need [i,j] = Max[i,j] – Allocation [i,j].
Safety Algorithm
1. Let Work and Finish be vectors of length m and n, respectively. Initialize:
Work := Available
Finish [i] = false for i = 1, 2, 3, …, n.
2. Find an i such that both:
(a) Finish [i] = false
Y  X if and only if Y[i]  X[i]
(b) Needi  Work (ith row of Need)
(0, 3, 2, 1)  (1, 7, 3, 2)
 If no such i exists, go to step 4.
3. Work: = Work + Allocationi
Finish[i]:= true
go to step 2.
4. If Finish [i] = true for all i, then the system is in a safe state, Otherwise the system
in unsafe state.
Resource-Request Algorithm

P a g e | 82/107
Requesti = request vector for process Pi. If Requesti [j] = k then process Pi wants
k instances of resource type Rj.
1. If Requesti  Needi go to step 2. Otherwise, raise error condition, since
process has exceeded its maximum claim.
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
If Requesti  Available, go to step 3. Otherwise Pi must wait, since
resources are not available.
3. Pretend to allocate requested resources to Pi by modifying the state as
follows:
Available := Available – Requesti;
Allocationi := Allocationi + Requesti;
Needi := Needi – Requesti;
If safe  the resources are allocated to Pi.
If unsafe  Pi must wait, and the old resource-allocation state is restored
2.


Example of Banker’s Algorithm
•
•
5 processes P0 through P4; 3 resource types A (10 instances), B (5 instances) and C (7
instances).
Snapshot at time T0:
P0
P1
Allocation
A B C A
0
1 0 7
2
0 0 3
Max
B C
5
3
2
2
P2
P3
P4
3
2
0
0
2
3
0
1
0
2
1
2
9
2
4
Available
A B C
3
3
2
2
2
3
•
The content of the matrix Need is defined to be Max – Allocation.
Need
A
B C
P0
7
4
3
P1
1
2
2
P2
6
0
0
P3
0
1
1
P4
4
3
1
•
The system is in a safe state since the sequence < P1, P3, P4, P2, P0> satisfies safety
criteria.
Safety Check!!
Allocation
Need
Available
A B C
A B C
A B C
P0
0 1 0
7 4 3
3 3 2
P1
2 0 0
1 2 2
P1
5 3 2
P2
3 0 2
6 0 0
P3
2 1 1
0 1 1
P3
7 4 3
P4
0 0 2
4 3 1
P4
7 4 5
•
P a g e | 83/107
10 4 7
P2
10 5 7
P0
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
•
Suppose now that process P1 requests one additional instance of resource type A and two
instances of resource type C, so Request1=(1,0,2).
1. Check that Request1  Need1 (that is, (1,0,2) (1,2,2)  true. )
2. Check that Request1  Available (that is, (1,0,2)  (3,3,2)  true.)
3. Pretend that this request has been fulfilled, the new state are:
P0
P1
P2
P3
P4
•
•
•
Allocation
A B C
0 1 0
3 0 2
3 0 2
2 1 1
0 0 2
A
7
0
6
0
4
B
4
2
0
1
3
Need
C
3
0
0
1
1
Available
A B C
2 3 0
Executing safety algorithm shows that sequence <P1, P3, P4, P0, P2> satisfies safety
requirement.
When the system is in this state, a request for (3,3,0) by P4 cannot be granted, since the
resources are not available.
A request for (0,2,0) by P0 cannot be granted, even though the resources are available,
since the resulting state is unsafe.
Deadlock Detection
•
•
Allow system to enter a deadlock state
In this environment, the system must provide:
 Detection algorithm: examine the system state to determine whether a deadlock has
occurred
 Recovery scheme: recover from the deadlock
Single Instance of Each Resource Type
•
•
•
Maintain wait-for graph (variant of the resource-allocation graph).
 Nodes are processes.
 This graph is obtained from the resource-allocation graph by removing the nodes of
type resource and collapsing the appropriate edges
 Pi  Pj (Pi is waiting for Pj ) exists in a wait-for graph if and only if the
corresponding resource-allocation graph contains two edges Pi  Rq and Rq  Pj.
Periodically invoke an algorithm that searches for a cycle in the graph.
An algorithm to detect a cycle in a graph requires an order of n2 operations, where n is
the number of vertices in the graph.
P a g e | 84/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
[56].
(a) Resource-allocation graph. (b) Corresponding wait-for
graph.
Several Instances of a Resource Type
•
Data structures:
 Available: A vector of length m indicates the number of available resources of each
type
 Allocation: An n x m matrix defines the number of resources of each type currently
allocated to each process
 Request: An n x m matrix indicates the current request of each process. If
Request[i,j] = k, then process Pi is requesting k more instances of resource type Rj
Detection Algorithm
1. Let Work and Finish be vectors of length m and n, respectively Initialize:
Work := Available
For i = 1,2, …, n, if Allocationi  0, then Finish[i] := false;
otherwise, Finish[i] := true.
2. Find an index i such that both:
(a) Finish[i] = false
(b) Requesti  Work
If no such i exists, go to step 4.
3. Work := Work + Allocationi
Finish[i] := true
go to step 2.
4. If Finish[i] = false, for some i, 1  i  n, then the system is in deadlock state.
Moreover, if Finish[i] = false, then Pi is deadlocked.
 Algorithm requires an order of m x n2 operations to detect whether the system is in
deadlocked state.
Example of Detection Algorithm
P a g e | 85/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
We have:
•
Five Processes P0 through P4.
•
Three resource types A, B, C.
•

Resource type A has 7 instances.

Resource type B has 2 instances.

Resource type C has 6 instances.
Snapshot at time T0:
P0
P1
P2
P3
P4
•
•
Allocation
A B C
0 1 0
2 0 0
3 0 3
2 1 1
0 0 2
Request
B C
0 0
0 2
0 0
0 0
0 2
Available
A B C
0 0 0
Sequence < P0, P2, P3, P1, P4> result in finish[i] = true for all i.
Suppose now that process P2 request an additional instance of type C.
P0
P1
P2
P3
P4
•
A
0
2
0
1
0
A
0
2
0
1
0
Request
B C
0 0
0 2
0 1
0 0
0 2
State of system?
 Can reclaim resources held by process P0 but insufficient resources to
fulfill other processes; requests.
 Deadlock exists, consisting of processes P1, P2, P3, and P4.
Deadlock-Algorithm Usage
•
•
When, and how often, to invoke depends on:
 How often a deadlock is likely to occur?
 How many processes will need to be rolled back?
• one for each disjoint cycle
If detection algorithm is invoked arbitrarily, there may be many cycles in the resource
graph and so we would not be able to tell which of the many deadlocked processes
“caused” the deadlock.
 Whenever some process makes a request that cannot be granted immediately
 Whenever CPU utilization drop below a threshold
P a g e | 86/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
Recovery from Deadlock
•
When a detection algorithm determines that a deadlock exists, several alternatives exist:
1. Inform the operator that a deadlock has occurred (operator deal with the deadlock
manually).
2. The system recovers from the deadlock automatically.
(a) Process termination
(b) Resource preemption
Process Termination
•
•
•
Method1: Abort all deadlocked processes.
Method2: Abort one process at a time until the deadlock cycle is eliminated. (-ve:
overhead, after each process is aborted, a deadlock-detection algorithm must be invoked)
In which order should we choose to abort?
 Priority of the process.
 How long process has computed, and how much longer to completion.
 Resources the process has used.
 Resources process needs to complete.
 How many processes will need to be terminated.
 Is process interactive or batch?
Resource Preemption
•
•
Preempt some resources from processes and give these resources to other processes until
the deadlock cycle is broken.
Three issues need to be addressed:
1. Selecting a victim: which resources and which processes are to be preempted?
(Determine the order of preemption to minimize cost).
2. Rollback: if we preempt a resource from a process, what should be done with that
process? (We must roll back the process to some safe state, and restart it from that
state.)
 Require the system to keep more information about the state of all the running
processes
3. Starvation: how do we ensure that starvation will not occur? Same process may
always be picked as victim. Solution: include number of rollback in cost factor.
P a g e | 87/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
Memory Management
Background




The main purpose of a computer system is to execute programs. These programs together
with the data they access, must be in main memory (at least partially) during execution.
Memory consists of a large array of words or bytes, each with its own address.
Main memory is usually too small to accommodate all the data and programs
permanently, the computer system must provide secondary storage to back up main
memory.
Input queue – collection of processes on the disk that are waiting to be brought into
memory for execution.
Address Binding


User programs go through several steps before being executed (compiling, linking,
loading …..).
Address representation
 Source program: symbolic (such as count)
 After compiling: re-locatable address
 14 bytes from the beginning of this module
 After linkage editor or loader: absolute address
 Physical memory address 74014
2000
0
int I;
goto p1;
p1
250
Symbolic Address


Re-locatable Address
2250
Absolute Address
(Physical Memory)
Binding is mapping from one address space to another.
Address binding of instructions and data to memory addresses can happen at three
different stages.
 Compile time: If memory location known a priori, absolute code can be generated;
must recompile code if starting location changes.
 Load time: Must generate relocatable code if memory location is not known at
compile time.
P a g e | 88/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems

Execution time: Binding delayed until run time if the process can be moved during
its execution from one memory segment to another. Need hardware support for
address maps (e.g., base and limit registers).
Logical vs. Physical Address Space



The concept of a logical address space that is bound to a separate physical address space
is central to proper memory management.
 Logical address – generated by the CPU; also referred to as virtual address.
 Physical address – address seen by the memory unit.
Logical and physical addresses are the same in compile-time and load-time addressbinding schemes; logical (virtual) and physical addresses differ in execution-time
address-binding scheme.
Memory Management Unit (MMU)
 Hardware device that maps virtual to physical address.
 In MMU scheme, the value in the relocation register is added to every address
generated by a user process at the time it is sent to memory.
 The user program deals with logical addresses; it never sees the real physical
addresses.
[57].
Dynamic relocation using a relocation register
Notice:
 The user program never sees the real physical addresses.
 The user program deals with logical addresses.
 Two different types of addresses:
1. Logical Addresses: range 0 to max.
2. Phssical Addresses: range R+0 to R+max, where R is a base value.
The base value is stored in relocation register.
Dynamic Loading



Routine is not loaded until it is called.
Better memory-space utilization; unused routine is never loaded.
Useful when large amounts of code are needed to handle infrequently occurring cases;
such as Like error routines
P a g e | 89/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems

No special support from the operating system is required; implemented through program
design.
Dynamic Linking and Shared Libraries



Static linking vs dynamic linking.
The linking of some external modules is done after the creation of the load module
(executable file)
 Windows: external modules are .DLL files
 Unix: external modules are .SO files (shared library)
The load module contains references to external modules which are resolved either at:
 Loading time (load-time dynamic linking)
 Run time: when a call is made to a procedure defined in the external module (runtime dynamic linking)
Overlays




To enable a process to be larger than the amount of memory allocated to it, we can use
overlays.
The idea is to Keep in memory only those instructions and data that are needed at any
given time. When other instructions are needed, they are loaded into space occupied
previously by instructions that are no longer needed.
Implemented by user, no special support needed from operating system; programming
design of overlay structure is complex.
Popular with Micro-controllers and sytems with limited physical memory.
Pass 1
70K
Pass 2
80K
Sym. Table
20K
Common Rou. 30K
Assembler
Total Memory
Available = 150K
Swapping



A process needs to be in memory to be executed.
When insufficient main memory is available, one or more processes may be removed from
main memory and swapped onto secondary memory (backing store).
Swap in: When process has completed, the swap out process may be reload into memory.
P a g e | 90/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
Examples:
1. assume A multi-programming enviroment with round-robin CPU-Scheduling, when a
quantum expires, the memory manager swap-out the prosses that just finished, and
swap-in another process to the memory space hat has been freed.
2. The swapping policy can be used for priority based sceduling.
If a higher-priority process arrives and wants service, the memory manager
can swap-out the lower-priority process so that it can load and execute the
higher-priority process.
3. Medium-term scheduler

Major part of swap time is transfer time (the rest is latency, head seek time etc.); total
transfer time is directly proportional to the amount of memory swapped.


Swapping requires a backing store. The backing store is a fast disk and large enough
The effect of address binding on swapping
 Binding at compile or load time: process cannot be moved to different locations
 Execution-time binding: It’s possible to swap a process into a different memory space

Whenever the CPU scheduler decides to excute a process, it calls the dispatcher. The
dispatcher checks to see whether the next process in the queue is in memory. If the
process is not, and ther is no free memory region, the dispatcher swaps-out a process
currently in memory and swaps-in the desired process.
Contiguous Memory Allocation
Memory Partition

Main memory usually devided into two partitions:
 Resident operating system, usually held in low memory with interrupt vector.
P a g e | 91/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems

User processes then held in high memory.
Memory Protection


Protect user processes from each other, and from changing operating-system code and
data.
Relocation-register scheme used for memory protection.
 Hardware-support scheme
 Relocation register contains value of smallest physical address
 Limit register contains range of logical addresses – each logical address must be less
than the limit register. (for example, relocation = 100040 and limit = 74600).
 Each program had to occupy a single contiguous block of storage locations.
 MMU maps the logical address dynamically by adding the value in the relocation
register
P a g e | 92/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
[58].
Hardware Support for Relocation and Limit Registers
Memory Partition Allocation


Several user processes residing in memory at the same time.
Method1: fixed-partition scheme (the simplest method):
 Memory can be divided into a number of fixed-sized partitions, where each partition
may contain exactly one process.
 The degree of multiprogramming is bound by the number of partitions.
P a g e | 93/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems


When a partition is free, a process is selected from the input queue and is loaded into
the free partion. When the process terminates, the partition becomes available for
another process.
Method2: generalization of the fixed-partition scheme
 The OS keeps a table indicating which parts of memory are available and which are
occupied. Initially, all memory is available for user processes, and is considered as
one large block of available memory, a hole.
 Hole – block of available memory; holes of various sizes are scattered throughout
memory.
 When a process arrives, it is allocated memory from a hole large enough to
accommodate it.
 The hole is split into two parts: (1) one for the arriving process, (2) one
returned to the set of hole
 When a process terminates, its allocated memory is returned to the set of hole
 Maybe merged with adjacent holes
 Operating system maintains information about:
A) Allocated partitions
B) Free partitions (hole)
OS
OS
OS
OS
process 5
process 5
process 5
process 8
process 2
OS
OS
OS
process 5
process 5
process 9
process 9
process 2
process 10
process 2
P a g e | 94/107
process 2
process 2
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
Swapping two processes using disk as a backing store
. ]23[
Dynamic Storage-Allocation Problem
How to satisfy a request of size n from a list of free holes?


First-fit: Allocate the first hole that is big enough.
Best-fit: Allocate the smallest hole that is big enough; must search entire
list, unless ordered by size. Produces the smallest leftover hole.
 Worst-fit: Allocate the largest hole; must also search entire list. Produces
the largest leftover hole.
 First-fit and best-fit better than worst-fit in terms of speed and storage utilization.
 These algorithms, suffer from external fragmentation.
Common
strategies used
to select a free
hole
Example
P a g e | 95/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
Fragmentation


Can be internal as well as external.
External fragmentation – exists when enough total memory space exists to satisfy a
request, but it is not contiguous; storage is fragmented into a large number of small
holes.
OS
Example:- We have a total external
fragmentation of (300k + 260k)= 560k.
400k
P1
1000k
P4
If P5 is 500k, then this space would be large
enough to run P5. But the space is not contiguous.
The free memory space is fragmented into two
pices, neither one of which is large enough, by
itself, to satisfy the memory request of process
1700k
P a g e | 96/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
Free
2000k
P3
P5.
The selection of first-fit vs. best-fit can affect the
amount of fragmentation.
2300k
2560k
Free

One solution to the problem of external fragmentation is by compaction. The goal is to
shuffle memory contents to place all free memory together in one large block.
 Compaction is possible only if relocation is dynamic, and is done at execution time.

Another possible solution is to permit the logical-adress space of a process to be
noncontiguous
 Paging
 Segmentation
 Combined (paging + segmentation)

Internal fragmentation
 The general approach is to break the physical memory into fixed-sized blocks, and
allocate memory in unit of block sizes.
 The memory allocated to a process may be slightly larger than requested
memory; this size difference is memory internal to a partition, but not being
used (internal fragmentation).
OS
P7
next request is for
18,462 bytes
Free
hole of 18,464 bytes
P43

If we allocate exactly the requestd block, we are left with a hole of 2 bytes
(18464-18462 =2).
 The overhead to keep track of this hole will br larger than hole itself.
 So, we ignore this small hole.
 This small hole called Internal fragmentation.
Paging





Paging is a memory-management scheme that permits the physical address space of a
process to be noncontiguous.
Divide physical memory into fixed-sized blocks called frames (size is power of 2, between
512 bytes and 16MB bytes).
Divide logical memory into blocks of same size called pages.
The page size (like the frame size) is defined by the hardware depending on the computer
architecture.
Divide backing store into fixed-sized blocks that are the same size as the memory frames.
P a g e | 97/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems




OS has to keep track of all free frames to run a program of size n pages, need to find n
free frames and load program from backing store.
When a process is to be executed, its pages are loaded into any available memory frames
from the backing store.
OS must set up a page table to translate logical to physical addresses (address
translation scheme).
with paging scheme, we have no external fragmentation: any free frame can be allocated
to a process that needs it. But may have some internal fragmentation.
 For example, if pages are 2,048 bytes, a process of 72,766 bytes would need 35 pages
plus 1,086 bytes. The internal fragmentation is 2048 – 1086 = 962 bytes.
Address Translation Scheme

Address generated by CPU (logical address) is divided into:
 Page number (p) – used as an index into a page table which contains base address of
each page in physical memory.
 Page offset (d) – combined with base address to define the physical memory address
that is sent to the memory unit.

If the size of the logical address space is 2m and a page size is 2n addressing units
(bytes), then the high-order (m-n) bits of a logical address designate the page number,
and the (n) low-oreder bits designate the page-offset.
page number
page offset
p
d

The page table has 2
P a g e | 98/107
m-n
m-n
entries
n
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
Example:
 Page
size = 22 =4
 Logical
address space = 28 = 256
256
 64
4
 Number
of pages =
 Number
of bits = 8 - 2 = 6 bits
[60].
Paging Hardware
Example: Using a page size of 4 bytes and a physical memory of 32 bytes (8 pages).
P a g e | 99/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
P a g e | 100/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems




Logical Address 0 (Page 0; Offset 0) ==> (Frame 5: 5*4 + 0) Physical Address 20
Logical Address 3 (Page 0; Offset 3) ==> (Frame 5: 5*4 + 3) Physical Address 23
Logical Address 4 (Page 1; Offset 0) ==> (Frame 6: 6*4 + 0) Physical Address 24
Logical Address 13 (Page 3; Offset 1) ==> (Frame 2: 2*4 + 1) Physical Address 9
Example: Consider a logical address of 8 pages of 1024 words each, mapped onto a
physical memory of 32 frames.
1) How many bits are there in the logical address?
No. of pages * page size = 2^(logical address bits)
3
10
logical address bits
2 * 2 =2
So there are 13 bits in the logical address
2) How many bits are there in the physical address?
No. of framess * frame size = 2^(physical address bits)
P a g e | 101/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
5
10
physical address bits
2 * 2 =2
So there are 15 bits in the physical address
P a g e | 102/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
Segmentation
P a g e | 103/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
P a g e | 104/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
P a g e | 105/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
P a g e | 106/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013
Course Notes – Introduction to Operating Systems
P a g e | 107/107
Last updated by Dr. Sulieman Bani-Ahmad on Monday, September 30, 2013