Download STUDY GUIDE FOR THE MIDTERM EXAM of Tuesday November 3

Document related concepts

Plan 9 from Bell Labs wikipedia , lookup

Mobile operating system wikipedia , lookup

Spring (operating system) wikipedia , lookup

Copland (operating system) wikipedia , lookup

CP/M wikipedia , lookup

Unix security wikipedia , lookup

Security-focused operating system wikipedia , lookup

VS/9 wikipedia , lookup

DNIX wikipedia , lookup

Burroughs MCP wikipedia , lookup

Distributed operating system wikipedia , lookup

Process management (computing) wikipedia , lookup

Transcript
Dr. Dale Parson’s Study
Guide for the CSC343
midterm exam on Nov 3,
Operating Systems, Fall 2015
Operating System Concepts – 8th Edition,
Silberschatz, Galvin and Gagne ©2009
Four Components of a Computer System
Operating System Concepts – 8th Edition
1.2
Silberschatz, Galvin and Gagne ©2009
Computer System Organization
 Computer-system operation

One or more CPUs, device controllers connect through common bus
providing access to shared memory

Concurrent execution of CPUs and devices competing for memory
cycles
Operating System Concepts – 8th Edition
1.3
Silberschatz, Galvin and Gagne ©2009
Common Functions of Interrupts
 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
 A trap is a software-generated interrupt caused either by an error or a user
request
 An operating system is interrupt driven
Operating System Concepts – 8th Edition
1.4
Silberschatz, Galvin and Gagne ©2009
I/O Structure
 After I/O starts, control returns to user program only upon I/O
completion
 Wait instruction idles the CPU until the next interrupt
 Wait loop (contention for memory access)
 At most one I/O request is outstanding at a time, no
simultaneous I/O processing
 After I/O starts, control returns to user program without waiting
for I/O completion
 System call – request to the operating system to allow user
to wait for I/O completion
 Device-status table contains entry for each I/O device
indicating its type, address, and state
 Operating system indexes into I/O device table to determine
device status and to modify table entry to include interrupt
Operating System Concepts – 8th Edition
1.5
Silberschatz, Galvin and Gagne ©2009
Direct Memory Access Structure
 Used for high-speed I/O devices able to transmit information at close to
memory speeds
 Device controller transfers blocks of data from buffer storage directly to
main memory without CPU intervention
 Only one interrupt is generated per block, rather than the one interrupt per
byte
Operating System Concepts – 8th Edition
1.6
Silberschatz, Galvin and Gagne ©2009
How a Modern Computer Works
A von Neumann architecture
Operating System Concepts – 8th Edition
1.7
Silberschatz, Galvin and Gagne ©2009
Storage-Device Hierarchy
Operating System Concepts – 8th Edition
1.8
Silberschatz, Galvin and Gagne ©2009
Multiprocessors
 Review Figures 1 and 3 at:
 http://faculty.kutztown.edu/parson/spring2012/MultiprocessorJanuary2012P
arson.pdf
Operating System Concepts – 8th Edition
1.9
Silberschatz, Galvin and Gagne ©2009
Operating System Structure
 Multiprogramming needed for efficiency

Single user cannot keep CPU and I/O devices busy at all times

Multiprogramming organizes jobs (code and data) so CPU always has
one to execute

A subset of total jobs in system is kept in memory

One job selected and run via job scheduling

When it has to wait (for I/O for example), OS switches to another job
 Timesharing (multitasking) is logical extension in which CPU switches
jobs so frequently that users can interact with each job while it is running,
creating interactive computing

Response time should be < 1 second

Each user has at least one program executing in memory process

If several jobs ready to run at the same time  CPU scheduling

If processes don’t fit in memory, swapping moves them in and out to
run

Virtual memory allows execution of processes not completely in
memory
Operating System Concepts – 8th Edition
1.10
Silberschatz, Galvin and Gagne ©2009
Operating-System Operations
 Interrupt driven by hardware
 Software error or request creates exception or trap

Division by zero, request for operating system service
 Other process problems include infinite loop, processes modifying each
other or the operating system
 Dual-mode operation allows OS to protect itself and other system
components
 User mode and kernel mode
 Mode bit provided by hardware
Provides ability to distinguish when system is running user code or
kernel code
 Some instructions designated as privileged, only executable in
kernel mode


System call changes mode to kernel, return from call resets it to user
Operating System Concepts – 8th Edition
1.11
Silberschatz, Galvin and Gagne ©2009
Transition from User to Kernel Mode
 Timer to prevent infinite loop / process hogging resources

Set interrupt after specific period

Operating system decrements counter

When counter zero generate an interrupt

Set up before scheduling process to regain control or terminate program
that exceeds allotted time
Operating System Concepts – 8th Edition
1.12
Silberschatz, Galvin and Gagne ©2009
Migration of Integer A from Disk to Register
 Multitasking environments must be careful to use most recent value, no
matter where it is stored in the storage hierarchy
 Multiprocessor environment must provide cache coherency in hardware
such that all CPUs have the most recent value in their cache
 Distributed environment situation even more complex

Several copies of a datum can exist

Various solutions covered in Chapter 17
Operating System Concepts – 8th Edition
1.13
Silberschatz, Galvin and Gagne ©2009
CH2: A View of Operating System
Services
Operating System Concepts – 8th Edition
1.14
Silberschatz, Galvin and Gagne ©2009
Bourne Shell Command Interpreter
Operating System Concepts – 8th Edition
1.15
Silberschatz, Galvin and Gagne ©2009
GUIs
Operating System Concepts – 8th Edition
1.16
Silberschatz, Galvin and Gagne ©2009
System Calls
 Programming interface to the services provided by the OS
 Typically written in a high-level language (C or C++)
 Mostly accessed by programs via a high-level Application Program Interface
(API) rather than direct system call use
 Three most common APIs are Win32 API for Windows, POSIX API for
POSIX-based systems (including virtually all versions of UNIX, Linux, and
Mac OS X), and Java API for the Java virtual machine (JVM)
 Why use APIs rather than system calls?
(Note that the system-call names used throughout this text are generic)
Operating System Concepts – 8th Edition
1.17
Silberschatz, Galvin and Gagne ©2009
Example of System Calls
 System call sequence to copy the contents of one file to another file
Operating System Concepts – 8th Edition
1.18
Silberschatz, Galvin and Gagne ©2009
System Call Implementation
 Typically, a number associated with each system call

System-call interface maintains a table indexed according to these
numbers
 The system call interface invokes intended system call in OS kernel and
returns status of the system call and any return values
 The caller need know nothing about how the system call is implemented

Just needs to obey API and understand what OS will do as a result call

Most details of OS interface hidden from programmer by API

Managed by run-time support library (set of functions built into
libraries included with compiler)
Operating System Concepts – 8th Edition
1.19
Silberschatz, Galvin and Gagne ©2009
API – System Call – OS Relationship
Operating System Concepts – 8th Edition
1.20
Silberschatz, Galvin and Gagne ©2009
Types of System Calls
 Process control

create process, terminate process

end, abort

load, execute

get process attributes, set process attributes

wait for time

wait event, signal event

allocate and free memory

Dump memory if error

Debugger for determining bugs, single step execution

Locks for managing access to shared data between processes
Operating System Concepts – 8th Edition
1.21
Silberschatz, Galvin and Gagne ©2009
Types of System Calls
 File management

create file, delete file

open, close file

read, write, reposition

get and set file attributes
 Device management

request device, release device

read, write, reposition

get device attributes, set device attributes

logically attach or detach devices
Operating System Concepts – 8th Edition
1.22
Silberschatz, Galvin and Gagne ©2009
Types of System Calls (Cont.)
 Information maintenance

get time or date, set time or date

get system data, set system data

get and set process, file, or device attributes
 Communications

create, delete communication connection

send, receive messages if message passing model to host
name or process name

From client to server

Shared-memory model create and gain access to memory
regions

transfer status information

attach and detach remote devices
Operating System Concepts – 8th Edition
1.23
Silberschatz, Galvin and Gagne ©2009
Types of System Calls (Cont.)
 Protection

Control access to resources

Get and set permissions

Allow and deny user access
Operating System Concepts – 8th Edition
1.24
Silberschatz, Galvin and Gagne ©2009
CHAPTER 3: Process Concept
 An operating system executes a variety of programs:
Batch system – jobs
 Time-shared systems – user programs or tasks
 Textbook uses the terms job and process almost interchangeably
 Process – a program in execution; process execution must
progress in sequential fashion
 A process includes:
 program counter
 stack
 data section


heap (dynamically allocated memory)
 PARSON NOTE: In a multithreaded process each thread has
its own program counter (a.k.a. instruction pointer), and other
CPU registers such as Stack Pointer and Frame Pointer.
Operating System Concepts – 8th Edition
1.25
Silberschatz, Galvin and Gagne ©2009
Process in (Virtual) Memory
Operating System Concepts – 8th Edition
1.26
Silberschatz, Galvin and Gagne ©2009
Process (or Thread!) State

As a process (or thread) executes, it changes state

new: The process is being created

running: Instructions are being executed

waiting: The process is waiting for some event to occur

ready: The process is waiting to be assigned to a processor

terminated: The process has finished execution
Operating System Concepts – 8th Edition
1.27
Silberschatz, Galvin and Gagne ©2009
KNOW fcfs, sjf and rr schedulers.
Operating System Concepts – 8th Edition
1.28
Silberschatz, Galvin and Gagne ©2009
Process Control Block (PCB)
Information associated with each process
 Process state
 Program counter (PER THREAD)
 CPU registers (PER THREAD)
 CPU scheduling information (PER THREAD)
 Memory-management information
 Accounting information (PER PROCESS & THREAD)
 I/O status information (PER PROCESS & THREAD)
Operating System Concepts – 8th Edition
1.29
Silberschatz, Galvin and Gagne ©2009
CPU Switch From Process to Process
Operating System Concepts – 8th Edition
1.30
Silberschatz, Galvin and Gagne ©2009
Process Scheduling Queues
 Job queue – set of all processes in the system
 Ready queue – set of all processes residing in main memory,
ready and waiting to execute
 Device queues – set of processes waiting for an I/O device
 Processes migrate among the various queues
Operating System Concepts – 8th Edition
1.31
Silberschatz, Galvin and Gagne ©2009
Schedulers
 Long-term scheduler (or job scheduler) – selects which
processes should be brought into the ready queue
 Short-term scheduler (or CPU scheduler) – selects which
process (thread!) should be executed next and allocates CPU
Operating System Concepts – 8th Edition
1.32
Silberschatz, Galvin and Gagne ©2009
Representation of Process Scheduling

Queueing diagram represents queues, resources, flows
Operating System Concepts – 8th Edition
1.33
Silberschatz, Galvin and Gagne ©2009
Addition of Medium Term Scheduling
Operating System Concepts – 8th Edition
1.34
Silberschatz, Galvin and Gagne ©2009
Schedulers (Cont)
 Short-term scheduler is invoked very frequently (milliseconds) 
(must be fast)
 Long-term scheduler is invoked very infrequently (seconds,
minutes)  (may be slow)
 The long-term scheduler controls the degree of multiprogramming
 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
Operating System Concepts – 8th Edition
1.35
Silberschatz, Galvin and Gagne ©2009
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 via a context
switch
 Context of a process represented in the PCB
 Context-switch time is overhead; the system does no useful work while
switching
 Time dependent on hardware support
Operating System Concepts – 8th Edition
1.36
Silberschatz, Galvin and Gagne ©2009
Process Creation
 Parent process create children processes, which, in turn create other
processes, forming a tree of processes
 Generally, process identified and managed via a process identifier (pid)
 Resource sharing

Parent and children share all resources

Children share subset of parent’s resources

Parent and child share no resources
 Execution

Parent and children execute concurrently

Parent waits until children terminate
Operating System Concepts – 8th Edition
1.37
Silberschatz, Galvin and Gagne ©2009
Process Termination
 Process executes last statement and asks the operating system to
delete it (exit)

Output data from child to parent (via wait)

Process’ resources are deallocated by operating system
 Parent may terminate execution of children processes (abort)

Child has exceeded allocated resources

Task assigned to child is no longer required

If parent is exiting

Some operating system do not allow child to continue if its
parent terminates
–
All children terminated - cascading termination
Operating System Concepts – 8th Edition
1.38
Silberschatz, Galvin and Gagne ©2009
Interprocess Communication
 Processes within a system may be independent or cooperating
 Cooperating process can affect or be affected by other processes,
including sharing data
 Reasons for cooperating processes:

Information sharing

Computation speedup

Modularity

Convenience
 Cooperating processes need interprocess communication (IPC)
 Two models of IPC

Shared memory

Message passing
Operating System Concepts – 8th Edition
1.39
Silberschatz, Galvin and Gagne ©2009
CH4: Single and Multithreaded Processes
Operating System Concepts – 8th Edition
1.40
Silberschatz, Galvin and Gagne ©2009
Benefits
 Responsiveness
 Resource Sharing
 Economy
 Scalability
Operating System Concepts – 8th Edition
1.41
Silberschatz, Galvin and Gagne ©2009
Multithreaded Server Architecture
Operating System Concepts – 8th Edition
1.42
Silberschatz, Galvin and Gagne ©2009
Concurrent Execution on a Single-core System
Operating System Concepts – 8th Edition
1.43
Silberschatz, Galvin and Gagne ©2009
Parallel Execution on a Multicore System
Operating System Concepts – 8th Edition
1.44
Silberschatz, Galvin and Gagne ©2009
Many-to-One Model
(BEFORE THREAD-SAFE KERNELS!)
Operating System Concepts – 8th Edition
1.45
Silberschatz, Galvin and Gagne ©2009
One-to-OneModel (THREAD SAFE!)
Operating System Concepts – 8th Edition
1.46
Silberschatz, Galvin and Gagne ©2009
Many-to-Many Model (NOT THERE YET)
Operating System Concepts – 8th Edition
1.47
Silberschatz, Galvin and Gagne ©2009
CH5: Race Condition & Deadlocks
 Results of execution and possible
bugs depend on relative timing of
threads interacting in a critical
section.
 A critical section comprises regions
of code that interact over a shared,
non-atomic data set.
Operating System Concepts – 8th Edition
1.48
Silberschatz, Galvin and Gagne ©2009
Solution to Critical-Section Problem
1. Mutual Exclusion - If process Pi is executing in its critical section, then no
other processes can be executing in their critical sections
2. Progress - If no process is executing in its critical section and there exist
some processes that wish to enter their critical section, then the selection
of the processes that will enter the critical section next cannot be
postponed indefinitely
3. Bounded Waiting - A bound must exist on the number of times that other
processes are allowed to enter their critical sections after a process has
made a request to enter its critical section and before that request is
granted


Assume that each process executes at a nonzero speed
No assumption concerning relative speed of the N processes
Operating System Concepts – 8th Edition
1.49
Silberschatz, Galvin and Gagne ©2009
Critical-Section Handling in OS
Two approaches depending on if kernel is preemptive or nonpreemptive

Preemptive – allows preemption of process when running
in kernel mode

Non-preemptive – runs until exits kernel mode, blocks, or
voluntarily yields CPU
Essentially free of race conditions in kernel mode
Operating System Concepts – 8th Edition
1.50
Silberschatz, Galvin and Gagne ©2009
Synchronization Hardware
 Many systems provide hardware support for critical section code
 Uniprocessors – could disable interrupts

Currently running code would execute without preemption

Generally too inefficient on multiprocessor systems
 Operating systems using this not broadly scalable
 Modern machines provide special atomic hardware instructions
 Atomic = non-interruptable
 Either test memory word and set value

Or swap contents of two memory words
Operating System Concepts – 8th Edition
1.51
Silberschatz, Galvin and Gagne ©2009
test_and_set Instruction
Definition:
boolean test_and_set (boolean *target)
{
boolean rv = *target;
*target = TRUE;
return rv:
}
1. Executed atomically
2. Returns the original value of passed parameter
3. Set the new value of passed parameter to “TRUE”.
Operating System Concepts – 8th Edition
1.52
Silberschatz, Galvin and Gagne ©2009
compare_and_swap Instruction
Definition:
int compare _and_swap(int *value, int expected, int new_value) {
int temp = *value;
if (*value == expected)
*value = new_value;
return temp;
}
1. Executed atomically
2. Returns the original value of passed parameter “value”
3. Set the variable “value” the value of the passed parameter “new_value”
but only if “value” ==“expected”. That is, the swap takes place only under
this condition.
Operating System Concepts – 8th Edition
1.53
Silberschatz, Galvin and Gagne ©2009
Mutex Locks
 Previous solutions are complicated and generally inaccessible
to application programmers
 OS designers build software tools to solve critical section
problem
 Simplest is
mutex lock
 Protect a critical section by first
acquire() a lock then
release() the lock

Boolean variable indicating if lock is available or not
 Calls to
acquire() and release() must be atomic

Usually implemented via hardware atomic instructions
 But this solution requires busy waiting
 This lock therefore called a spinlock
Operating System Concepts – 8th Edition
1.54
Silberschatz, Galvin and Gagne ©2009
Solution to Critical-section Problem Using Locks
do {
acquire lock
critical section
release lock
remainder section
} while (TRUE);
We have simulated both spin locks that use atomic variables in Atomic.stm,
and mutex locks that cause waiting threads to block on an O.S. queue that
holds threads waiting on the lock in Mutex.stm.
UNDERSTAND WHAT YOU HAVE MODELED.
Operating System Concepts – 8th Edition
1.55
Silberschatz, Galvin and Gagne ©2009
Semaphore

Synchronization tool that does not require busy waiting

Semaphore S – integer variable

Two standard operations modify S: wait() and signal()

Originally called P() and V()

Less complicated

Can only be accessed via two indivisible (atomic) operations

wait (S) {
while S <= 0
; // no-op
S--;
}
 signal (S) {
S++;
}
Operating System Concepts – 8th Edition
1.56
Silberschatz, Galvin and Gagne ©2009
Semaphore as General Synchronization Tool

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

Also known as mutex locks

Can implement a counting semaphore S as a binary semaphore

Provides mutual exclusion
Semaphore mutex;
// initialized to 1
do {
wait (mutex);
// Critical Section
signal (mutex);
// remainder section
} while (TRUE);
Operating System Concepts – 8th Edition
1.57
Silberschatz, Galvin and Gagne ©2009
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
P1
wait (S);
wait (Q);
wait (Q);
wait (S);
.
.
.
.
.
.
signal (S);
signal (Q);
signal (Q);
signal (S);
 Starvation – indefinite blocking. A process may never be removed from the
semaphore queue in which it is suspended
 Priority Inversion - Scheduling problem when lower-priority process holds a
lock needed by higher-priority process
Operating System Concepts – 8th Edition
1.58
Silberschatz, Galvin and Gagne ©2009
Readers-Writers Problem
 A data set is shared among a number of concurrent processes

Readers – only read the data set; they do not perform any
updates

Writers – can both read and write
 Problem – allow multiple readers to read at the same time. Only
one single writer can access the shared data at the same time
 Shared Data

Data set

Semaphore mutex initialized to 1

Semaphore wrt initialized to 1

Integer readcount initialized to 0
Operating System Concepts – 8th Edition
1.59
Silberschatz, Galvin and Gagne ©2009
Problems with Semaphores

Correct use of semaphore operations:

signal (mutex) …. wait (mutex)

wait (mutex) … wait (mutex)

Omitting of wait (mutex) or signal (mutex) (or both)

There are similar problems with MUTEX (e.g., missing
unlock(), forgetting to lock() a critical section) and
CONDITION VARIABLE (forgetting to signal, using signal
where signalAll may be necessary when the thread at the
front of the queue does not have its condition satisfied).
Operating System Concepts – 8th Edition
1.60
Silberschatz, Galvin and Gagne ©2009
Monitors

A high-level LANGUAGE abstraction that provides a convenient and
effective mechanism for process synchronization

Only one process may be active within the monitor at a time
monitor monitor-name
{
// shared variable declarations
procedure P1 (…) { …. }
…
procedure Pn (…) {……}
Initialization code ( ….) { … }
…
}
}
Operating System Concepts – 8th Edition
1.61
Silberschatz, Galvin and Gagne ©2009
Condition Variables
 CONDITION VARIABLES PROVIDE A WAY FOR ONE
THREAD THAT IS HOLDING A MUTEX TO SIGNAL ONE OR
MORE OTHER THREADS THAT ARE WAITING FOR A
CONDITION TO BECOME TRUE IN A CRITICAL SECTION
PROTECTED BY THAT MUTEX!!!!!!!!!
 condition x, y;
 Two operations on a condition variable:

x.wait () – a process that invokes the operation is
suspended.

x.signal () – resumes one of processes (if any) that
invoked x.wait ()
Operating System Concepts – 8th Edition
1.62
Silberschatz, Galvin and Gagne ©2009
The Deadlock Problem
 A set of blocked processes each holding a resource and waiting to
acquire a resource held by another process in the set


Example

System has 2 disk drives

P1 and P2 each hold one disk drive and each needs another one
Example

semaphores A and B, initialized to 1
P0
P1
wait (A);
wait(B)
wait (B);
wait(A)
Operating System Concepts – 8th Edition
1.63
Silberschatz, Galvin and Gagne ©2009
Deadlock Characterization
Deadlock can arise if four conditions hold simultaneously.
 Mutual exclusion: only one process at a time can use a
resource
 Hold and wait: a process holding at least one resource is
waiting to acquire additional resources held by other
processes
 No preemption: a resource can be released only
voluntarily by the process holding it, after that process has
completed its task
 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 P0 is waiting for a resource that is held by P0.
Operating System Concepts – 8th Edition
1.64
Silberschatz, Galvin and Gagne ©2009
Parson’s Basic Facts
 PARSON’S NOTE FOR THE PREVIOUS SLIDES AND
THIS SECTION:

Cyclic waiting among threads of execution is the
main aspect of deadlock to memorize. The previous
slide calls it Circular Wait. It implies the other three
requirements of Mutual Exclusion, Hold & Wait,
and No Preemption.

One way to avoid cycles is to sort locked objects
(e.g., mutexes) into some full order, and always
request from sorted set in that order.

Sometimes you can’t do the above because you don’t
have access to all locked objects in your code, e.g., if
the locking occurs in imported library code.
Operating System Concepts – 8th Edition
1.65
Silberschatz, Galvin and Gagne ©2009
Basic Facts
 If graph contains no cycles  no deadlock
 If graph contains a cycle 

if only one instance per resource type, then deadlock

if several instances per resource type, possibility of
deadlock
Operating System Concepts – 8th Edition
1.66
Silberschatz, Galvin and Gagne ©2009
Deadlock Prevention
Restrain the ways request can be made
 Mutual Exclusion – not required for sharable resources; must hold
for nonsharable resources
 Hold and Wait – must guarantee that whenever a process
requests a resource, it does not hold any other resources

Require process to request and be allocated all its resources
before it begins execution, or allow process to request
resources only when the process has none

Low resource utilization; starvation possible
Operating System Concepts – 8th Edition
1.67
Silberschatz, Galvin and Gagne ©2009
Deadlock Prevention (Cont.)
 No Preemption –

If a process that is holding some resources requests another
resource that cannot be immediately allocated to it, then all
resources currently being held are released

Preempted resources are added to the list of resources for which
the process is waiting

Process will be restarted only when it can regain its old resources,
as well as the new ones that it is requesting
 Circular Wait – impose a total ordering of all resource types, and
require that each process requests resources in an increasing order of
enumeration
Operating System Concepts – 8th Edition
1.68
Silberschatz, Galvin and Gagne ©2009
CH6: Histogram of CPU-burst Times
http://faculty.kutztown.edu/parson/fall2013/distributions.pdf
Operating System Concepts – 8th Edition
1.69
Silberschatz, Galvin and Gagne ©2009
Alternating Sequence of CPU And I/O Bursts
Operating System Concepts – 8th Edition
1.70
Silberschatz, Galvin and Gagne ©2009
Scheduling Criteria
 CPU utilization – keep the CPU as busy as possible
 Throughput – # of processes that complete their execution per
time unit
 Turnaround time – amount of time to execute a particular process
 Waiting time – amount of time a process has been waiting in the
ready queue
 Response time – amount of time it takes from when a request was
submitted until the first response is produced, not output (for timesharing environment)
Operating System Concepts – 8th Edition
1.71
Silberschatz, Galvin and Gagne ©2009
Scheduling Algorithm Optimization Criteria
 Max CPU utilization
 Max throughput
 Min turnaround time
 Min waiting time
 Min response time
Operating System Concepts – 8th Edition
1.72
Silberschatz, Galvin and Gagne ©2009
First-Come, First-Served (FCFS) Scheduling
Process
Burst Time
P1
24
P2
3
P3
3
 Suppose that the processes arrive in the order: P1 , P2 , P3
The Gantt Chart for the schedule is:
P1
P2
0
24
P3
27
30
 Waiting time for P1 = 0; P2 = 24; P3 = 27
 Average waiting time: (0 + 24 + 27)/3 = 17
Operating System Concepts – 8th Edition
1.73
Silberschatz, Galvin and Gagne ©2009
FCFS Scheduling (Cont)
Suppose that the processes arrive in the order
P2 , P3 , P1
 The Gantt chart for the schedule is:
P2
0
P3
3
P1
6
30
 Waiting time for P1 = 6; P2 = 0; P3 = 3
 Average waiting time: (6 + 0 + 3)/3 = 3
 Much better than previous case
 Convoy effect short process behind long process
Operating System Concepts – 8th Edition
1.74
Silberschatz, Galvin and Gagne ©2009
Shortest-Job-First (SJF) Scheduling
 Associate with each process the length of its next CPU burst. Use these
lengths to schedule the process with the shortest time
 SJF is optimal – gives minimum average waiting time for a given set of
processes

The difficulty is knowing the length of the next CPU request
Operating System Concepts – 8th Edition
1.75
Silberschatz, Galvin and Gagne ©2009
Example of SJF
Process
Arrival Time
Burst Time
P1
0.0
6
P2
2.0
8
P3
4.0
7
P4
5.0
3
 SJF scheduling chart
P4
0
P3
P1
3
9
P2
16
24
 Average waiting time = (3 + 16 + 9 + 0) / 4 = 7
Operating System Concepts – 8th Edition
1.76
Silberschatz, Galvin and Gagne ©2009
Determining Length of Next CPU Burst
 Can only estimate the length – should be similar to the previous one

Then pick process with shortest predicted next CPU burst
 Can be done by using the length of previous CPU bursts, using
exponential averaging
1. t n  actual length of n th CPU burst
2.  n 1  predicted value for the next CPU burst
3.  , 0    1
4. Define :  n 1   t n  1    n .
 Commonly, α set to ½
 Preemptive version called shortest-remaining-time-first
Operating System Concepts – 8th Edition
1.77
Silberschatz, Galvin and Gagne ©2009
Prediction of the Length of the Next CPU Burst
Operating System Concepts – 8th Edition
1.78
Silberschatz, Galvin and Gagne ©2009
Examples of Exponential Averaging
  =0
n+1 = n
 Recent history does not count

  =1
n+1 =  tn
 Only the actual last CPU burst counts
 If we expand the formula, we get:
n+1 =  tn+(1 - ) tn -1 + …

+(1 -  )j  tn -j + …
+(1 -  )n +1 0
 Since both  and (1 - ) are less than or equal to 1, each successive term
has less weight than its predecessor
Operating System Concepts – 8th Edition
1.79
Silberschatz, Galvin and Gagne ©2009
Example of Shortest-remaining-time-first
 Now we add the concepts of varying arrival times and preemption to
the analysis
ProcessAarri Arrival TimeT
Burst Time
P1
0
8
P2
1
4
P3
2
9
P4
3
5
 Preemptive SJF Gantt Chart
P1
0
P2
1
P4
5
P1
10
P3
17
26
 Average waiting time = [(10-1)+(1-1)+(17-2)+5-3)]/4 = 26/4 = 6.5
msec
Operating System Concepts – 8th Edition
1.80
Silberschatz, Galvin and Gagne ©2009
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

Nonpreemptive
 SJF is priority scheduling where priority is the inverse of predicted
next CPU burst time
 Problem  Starvation – low priority processes may never execute
 Solution  Aging – as time progresses increase the priority of the
process
Operating System Concepts – 8th Edition
1.81
Silberschatz, Galvin and Gagne ©2009
Example of Priority Scheduling
ProcessA arri Burst TimeT
Priority
P1
10
3
P2
1
1
P3
2
4
P4
1
5
P5
5
2
 Priority scheduling Gantt Chart
P1
0
P2
1
P1
6
P3
16
P4
18 19
 Average waiting time = 8.2 msec
Operating System Concepts – 8th Edition
1.82
Silberschatz, Galvin and Gagne ©2009
Round Robin (RR)
 Each process gets a small unit of CPU time (time quantum),
usually 10-100 milliseconds. After this time has elapsed, the
process is preempted and added to the end of the ready 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.
 Performance

q large  FIFO

q small  q must be large with respect to context switch,
otherwise overhead is too high
Operating System Concepts – 8th Edition
1.83
Silberschatz, Galvin and Gagne ©2009
Example of RR with Time Quantum = 4
Process
P1
P2
P3
Burst Time
24
3
3
 The Gantt chart is:
P1
0
P2
4
P3
7
P1
10
P1
14
P1
18 22
P1
26
P1
30
 Typically, higher average turnaround than SJF, but better response
Operating System Concepts – 8th Edition
1.84
Silberschatz, Galvin and Gagne ©2009
Time Quantum and Context Switch Time
Operating System Concepts – 8th Edition
1.85
Silberschatz, Galvin and Gagne ©2009
Multilevel Queue
 Ready queue is partitioned into separate queues:
foreground (interactive)
background (batch)
 Each queue has its own scheduling algorithm

foreground – RR

background – FCFS
 Scheduling must be done between the queues

Fixed priority scheduling; (i.e., serve all from foreground then from
background). Possibility of starvation.

Time slice – each queue gets a certain amount of CPU time which it can
schedule amongst its processes; i.e., 80% to foreground in RR

20% to background in FCFS
Operating System Concepts – 8th Edition
1.86
Silberschatz, Galvin and Gagne ©2009
Multilevel Queue Scheduling
Operating System Concepts – 8th Edition
1.87
Silberschatz, Galvin and Gagne ©2009
Example of Multilevel Feedback Queue
 Three queues:

Q0 – RR with time quantum 8
milliseconds

Q1 – RR 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
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
Operating System Concepts – 8th Edition
1.88
Silberschatz, Galvin and Gagne ©2009
Multiple-Processor Scheduling
 CPU scheduling more complex when multiple CPUs are
available
 Homogeneous processors within a multiprocessor
 Asymmetric multiprocessing – only one processor accesses
the system data structures, alleviating the need for data sharing
 Symmetric multiprocessing (SMP) – each processor is self-
scheduling, all processes in common ready queue, or each has
its own private queue of ready processes

Currently, most common
 Processor affinity – process has affinity for processor on which
it is currently running

soft affinity

hard affinity

Variations including processor sets
Operating System Concepts – 8th Edition
1.89
Silberschatz, Galvin and Gagne ©2009
NUMA and CPU Scheduling
Note that memory-placement algorithms can also consider affinity
Operating System Concepts – 8th Edition
1.90
Silberschatz, Galvin and Gagne ©2009
Multiple-Processor Scheduling – Load Balancing
 If SMP, need to keep all CPUs loaded for efficiency
 Load balancing attempts to keep workload evenly distributed
 Push migration – periodic task checks load on each processor,
and if found pushes task from overloaded CPU to other CPUs
 Pull migration – idle processors pulls waiting task from busy
processor
Operating System Concepts – 8th Edition
1.91
Silberschatz, Galvin and Gagne ©2009
Multithreaded Multicore System
Operating System Concepts – 8th Edition
1.92
Silberschatz, Galvin and Gagne ©2009
Real-Time CPU Scheduling
 Can present obvious
challenges
 Soft real-time systems – no
guarantee as to when critical
real-time process will be
scheduled
 Hard real-time systems –
task must be serviced by its
deadline
 Two types of latencies affect
performance
1.
Interrupt latency – time from
arrival of interrupt to start of
routine that services interrupt
2.
Dispatch latency – time for
schedule to take current process
off CPU and switch to another
Operating System Concepts – 8th Edition
1.93
Silberschatz, Galvin and Gagne ©2009
Priority-based Scheduling
 For real-time scheduling, scheduler must support preemptive, priority-
based scheduling

But only guarantees soft real-time
 For hard real-time must also provide ability to meet deadlines
 Processes have new characteristics: periodic ones require CPU at
constant intervals

Has processing time t, deadline d, period p

0≤t≤d≤p

Rate of periodic task is 1/p
Operating System Concepts – 8th Edition
1.94
Silberschatz, Galvin and Gagne ©2009
Virtualization and Scheduling
 Virtualization software schedules multiple guests onto
CPU(s)
 Each guest doing its own scheduling

Not knowing it doesn’t own the CPUs

Can result in poor response time

Can effect time-of-day clocks in guests
 Can undo good scheduling algorithm efforts of guests
Operating System Concepts – 8th Edition
1.95
Silberschatz, Galvin and Gagne ©2009