Midterm Review – 7 Silberschatz, Galvin and Gagne ©2007

Midterm Review
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Silberschatz, Galvin and Gagne ©2007
What Have We Covered So Far?


Chapters 1-6 in Silbershatz:
1.
Intro to OS
2.
OS Structures
3.
Processes
4.
Threads
5.
CPU Scheduling
6.
Synchronization
Chapters 1,4,7,10 in Linux Kernel:

Intro

10: System Calls

3: Processes

7: Scheduler
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Review 2
Silberschatz, Galvin and Gagne ©2007
What is an Operating System?




OS is a abstraction
 Provides a process/thread abstraction
 A program that acts as an intermediary between a user of a
computer and the computer hardware.
OS is a resource allocator
 Manages all resources
 Decides between conflicting requests for efficient and fair
resource use
OS is a resource multiplexor
 Shares resources (CPU, memory, disk, etc.) across
users/applications
OS is a control program
 Controls execution of programs to prevent errors and
improper use of the computer
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Review 3
Silberschatz, Galvin and Gagne ©2007
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 with Java – 7th Edition, Nov 15, 2006
Review 4
Silberschatz, Galvin and Gagne ©2007
Interrupts






An operating system is interrupt driven.
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.
The operating system preserves the state of the CPU by storing
registers and the program counter.
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.
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Review 5
Silberschatz, Galvin and Gagne ©2007
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 with Java – 7th Edition, Nov 15, 2006
Review 6
Silberschatz, Galvin and Gagne ©2007
Memory Hierarchy and Caching



Important principle, performed at
many levels in a computer (in
hardware, operating system,
software)
Information in use copied from
slower to faster storage
temporarily
Faster storage (cache) checked
first to determine if information is
there
 If it is, information used
directly from the cache (fast)
 If not, data copied to cache
and used there
Operating System Concepts with Java – 7th Edition, Nov 15, 2006

Review 7
Cache smaller than storage being
cached
 Cache management important
design problem
 Cache size and replacement
policy
Silberschatz, Galvin and Gagne ©2007
Multiprogramming and Timesharing


Multiprogramming needed for efficiency
 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
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Review 8
Silberschatz, Galvin and Gagne ©2007
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 with Java – 7th Edition, Nov 15, 2006
Review 9
Silberschatz, Galvin and Gagne ©2007
Operating System Services






User interface - Almost all operating systems have a user interface
(UI): command-line, GUI, batch
Program execution - The system must be able to load a program
into memory and to run that program, end execution, either normally
or abnormally (indicating error)
I/O operations - A running program may require I/O, which may
involve a file or an I/O device.
File-system manipulation - The file system is of particular interest.
Obviously, programs need to read and write files and directories,
create and delete them, search them, list file Information, permission
management.
Communications – Processes may exchange information, on the
same computer or between computers over a network
Error detection – OS needs to be constantly aware of possible errors
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Review 10
Silberschatz, Galvin and Gagne ©2007
System Programs


System programs provide a convenient
environment for program development and
execution. The can be divided into:
 File manipulation
 Status information
 File modification
 Programming language support
 Program loading and execution
 Communications
 Application programs
Most users’ view of the operation system is
defined by system programs, not the actual
system calls
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Review 11
Silberschatz, Galvin and Gagne ©2007
System Calls





What they are: interface to OS services
System call numbers
How syscalls are implemented
How they pass parameters
Relationship to user space libraries
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Review 12
Silberschatz, Galvin and Gagne ©2007
OS Design and Implementation
Policy vs. Mechanism
 Modules
 System Structure
 Layers
 Monolithic (Linux, UNIX, Windows)
 Micro-kernel

Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Review 13
Silberschatz, Galvin and Gagne ©2007
Virtual Machines



A virtual machine provides
an interface identical to
the underlying bare
hardware
The resources of the
physical computer are
shared to create the
virtual machines
The virtual-machine
concept provides
complete protection of
system resources since
each virtual machine is
isolated from all other
virtual machines.
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Review 14
Silberschatz, Galvin and Gagne ©2007
Process Concept


Process – a program in
execution; process execution
must progress in sequential
fashion
A process includes:
 program counter
 stack
 data section
 code
 heap
 allocated memory
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Review 15
Silberschatz, Galvin and Gagne ©2007
Diagram of Process State

As a process 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
process
 terminated: The process has finished execution
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Review 16
Silberschatz, Galvin and Gagne ©2007
Process Control Block (PCB)
Information associated with
each process:
 Process state
 Program counter
 CPU registers
 CPU scheduling
information
 Memory-management
information
 Accounting information
 I/O status information
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Review 17
Silberschatz, Galvin and Gagne ©2007
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
Context-switch time is overhead; the system does no
useful work while switching
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Review 18
Silberschatz, Galvin and Gagne ©2007
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 with Java – 7th Edition, Nov 15, 2006
Review 19
Silberschatz, Galvin and Gagne ©2007
Process Creation and Deletion





Process Tree
Resource sharing variations
 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
Fork/exec
Kill/wait
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Review 20
Silberschatz, Galvin and Gagne ©2007
Interprocess Communication
Message Passing
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Shared Memory
Review 21
Silberschatz, Galvin and Gagne ©2007
Interprocess Communication



Direct vs. Indirect
 Names vs. hard-wired numbers
 Resolved statically or dynamically
Message Passing:
 Send vs. receive
 Blocking vs. non-blocking
 Buffering: none, limited, infinite
 Uni-directional vs. bidirectional
Examples
 Sockets
 RPC
 RMI
 Unix shared-memory
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Review 22
Silberschatz, Galvin and Gagne ©2007
Threads and Processes




Most operating systems therefore support two entities:
 the process,
 which defines the address space and general process
attributes
 the thread,
 which defines a sequential execution stream within a process
A thread is bound to a single process.
 For each process, however, there may be many threads.
Threads are the unit of scheduling
Processes are containers in which threads execute
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Review 23
Silberschatz, Galvin and Gagne ©2007
Single and Multithreaded Processes
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Review 24
Silberschatz, Galvin and Gagne ©2007
Threads vs. Processes






A thread has no data segment or
heap
A thread cannot live on its own, it
must live within a process
There can be more than one
thread in a process, the first thread
calls main & has the process’s
stack
Inexpensive creation
Inexpensive context switching
If a thread dies, its stack is
reclaimed
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
•
•
•
•
•
•
A process has code/data/heap &
other segments
A process has at least one thread
Threads within a process share
code/data/heap, share I/O, but
each has its own stack & registers
Expensive creation
Expensive context switching
If a process dies, its resources are
reclaimed & all threads die
Review 25
Silberschatz, Galvin and Gagne ©2007
User vs. Kernel Threads
(a) A user-level threads package. (b) A threads package
managed by the kernel.
Example from Tanenbaum, Modern Operating Systems 3 e,
(c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Review 26
Silberschatz, Galvin and Gagne ©2007
User-Level vs. Kernel Threads
User-Level





Managed by application
Kernel not aware of thread
Context switching cheap
Can create many more
Must be used with care
Kernel-Level





Managed by kernel
Consumes kernel resources
Context switching expensive
Number limited by kernel
resources
Simpler to use
Key issue: kernel threads provide virtual processors to user-level threads,
but if all of kernel threads block, then all user-level threads will
block even if the program logic allows them to proceed
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Review 27
Silberschatz, Galvin and Gagne ©2007
Multithreading Models
1:1
N:1
M:N
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Review 28
Silberschatz, Galvin and Gagne ©2007
Race conditions


Def: a timing dependent error involving shared state
 Whether it happens depends on how threads scheduled
 In effect, once thread A starts doing something, it needs to
“race” to finish it because if thread B looks at the shared
memory region before A is done, it may see something
inconsistent
Hard to detect:
 All possible schedules (permutations) have to be safe
 Number of possible schedule permutations is huge
 Some bad schedules? Some that will work sometimes?
 Intermittent  Unpredictable
 Timing dependent = small changes can hide bug

If a bug is deterministic and repeatable, celebrate!
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Review 29
Silberschatz, Galvin and Gagne ©2007
Critical Section Problem

Problem: Design a protocol for processes to cooperate, such that only one
process is in its critical section at any time
 How to make multiple instructions seem like one?
Process 1
CS1
Process 2
CS2
Time 
Processes progress with non-zero speed, no assumption on clock speed
Used extensively in operating systems:
Queues, shared variables, interrupt handlers, etc.
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Review 30
Silberschatz, Galvin and Gagne ©2007
Critical-Section Problem
1.
2.
3.
4.
Race Condition - When there
is concurrent access to
shared data and the final
outcome depends upon order
of execution.
Critical Section - Section of
code where shared data is
accessed.
Entry Section - Code that
requests permission to enter
its critical section.
Exit Section - Code that is run
after exiting the critical
section
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Review 31
Silberschatz, Galvin and Gagne ©2007
Critical Section Goals

We would like
 Safety (aka mutual exclusion)
 No more than one thread can be in a critical section at any time.
 Liveness (aka progress)
 A thread that is seeking to enter the critical section will eventually
succeed
 Bounded waiting
 A bound must exist on the number of times that other threads are
allowed to enter their critical sections after a thread 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

Ideally we would like fairness as well
 If two threads are both trying to enter a critical section, they have equal
chances of success
 … in practice, fairness is rarely guaranteed
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Review 32
Silberschatz, Galvin and Gagne ©2007
Peterson’s Algorithm
CSEnter(int i)
{
inside[i] = true;
turn = J;
while(inside[J] && turn == J)
continue;
}

CSExit(int i)
{
inside[i] = false;
}
Now ask:
 Is this Safe? Live? Bounded waiting?
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Review 33
Silberschatz, Galvin and Gagne ©2007
Bakery Algorithm


int ticket[N]; /* Important: Disable thread scheduling when changing N */
boolean choosing[N] = false;
CSEnter(int i)
{
CSExit(int i)
do {
{
ticket[i] = 0;
ticket[i] = 0;
choosing[i] = true;
}
ticket[i] = max(ticket[0], … ticket[N-1])+1;
choosing[i] = false;
} while(ticket[i] >= MAXIMUM);
for(J = 0; J < N; J++) {
while(choosing[J]) continue;
while(ticket[J] && (ticket[J],J) < (ticket[i],i))
continue;
}
}
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Review 34
Silberschatz, Galvin and Gagne ©2007
Approaches to Critical Sections




Everything we’ve seen so far is a software-only solution that
relies on reads and writes being atomic
 Atomic = non-interruptible
Another approach: disable interrupts
 Disable interrupts briefly when calling CSEnter() and CSExit()
 Currently running code would execute without preemption
 Available only in the kernel (why?)
 Generally doesn’t work on multiprocessor systems
 Operating systems using this not broadly scalable
Modern machines provide hardware “help”: atomic instructions
 Either test memory word and set value (test and set)
 Or swap contents of two memory words (compare and swap)
Idea is to provide a mechanism for critical sections: a lock
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Review 35
Silberschatz, Galvin and Gagne ©2007
Hardware Implementations of Locks

Modern machines provide
hardware “help”: atomic
instructions
 Either test memory
word and set value
(test and set)
 Or swap contents of
two memory words
(compare and swap)
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Review 36
Silberschatz, Galvin and Gagne ©2007
Semaphore





Synchronization tool that does not require busy waiting
Semaphore S – integer variable
Two standard operations modify S: acquire() and release()
 Originally called P() and V()
 from Dutch Proberen and Verhogen (Dijkstra)
 Also called down() and up()
 And even wait() and signal()
Higher-level abstraction, less complicated
Can only be accessed via two indivisible (atomic) operations
P(S) {
while(S ≤ 0)
;
S--;
}
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
V(S) {
S++;
}
Review 37
Silberschatz, Galvin and Gagne ©2007
Semaphore Types


Counting semaphore – integer value can range
over an unrestricted domain
 Used for synchronization
Binary semaphore – integer value can range only
between 0 and 1; can be simpler to implement
 Used for mutual exclusion: known as a mutex
Process i
P(S);
Critical Section
V(S);
Remainder Section
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Review 38
Silberschatz, Galvin and Gagne ©2007
Semaphore Implementation with no Busy waiting



With each semaphore there is an associated
waiting queue. Each entry in a waiting queue
has two data items:
 value (of type integer)
 pointer to next record in the list
Two operations:
 block – place the process invoking the
operation on the appropriate waiting queue.
 wakeup – remove one of processes in the
waiting queue and place it in the ready
queue.
Potential queuing policies: FIFO, LIFO, undef
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Review 39
Silberschatz, Galvin and Gagne ©2007
Producer-Consumer Problem
Shared: Semaphores mutex, empty, full;
Init: mutex = 1; /* for mutual exclusion*/
empty = N; /* number empty buf entries */
full = 0;
/* number full buf entries */
Producer
Consumer
do {
...
// produce an item in nextp
...
P(empty);
P(mutex);
...
// add nextp to buffer
...
V(mutex);
V(full);
} while (true);
do {
P(full);
P(mutex);
...
// remove item to nextc
...
V(mutex);
V(empty);
...
// consume item in nextc
...
} while (true);
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Review 40
Silberschatz, Galvin and Gagne ©2007
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
S.acquire();
Q.acquire();
Q.acquire();
S.acquire();
.
.
.
.
.
.
S.release();
Q.release();
Q.release();
S.release();
Starvation – indefinite blocking. A process may never be removed
from the semaphore queue in which it is suspended.
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Review 41
Silberschatz, Galvin and Gagne ©2007
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
In other words, a
language
implementation of
mutexes
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Review 42
Silberschatz, Galvin and Gagne ©2007
Condition Variables




Condition x, y;
Two operations on a condition variable:
 wait () – a process that invokes the operation is
suspended.
 signal () / notify() – resumes one of processes (if
any) that invoked wait ()
Subtleties between condition variables and semaphores:
 Semaphores have memory: V() will increment the
semaphore, even if no one has called P()
 Condition variables do not: if no one is waiting for a
signal() / notify(), it is not saved
Condition variables in monitors:
 Calling wait() releases the monitor
 Returning from wait() re-acquires the monitor
 Does signal() release the monitor?
 Who runs after a signal() is called?
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Review 43
Silberschatz, Galvin and Gagne ©2007
Classical Problems of Synchronization



Bounded-Buffer/Producer-Consumer Problem
Readers and Writers Problem
Dining-Philosophers Problem
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Review 44
Silberschatz, Galvin and Gagne ©2007
Readers-Writers Solution
Shared variables: Semaphore mutex, wrl;
integer rcount;
Init: mutex = 1, wrl = 1, rcount = 0;
Writer
do {
P(wrl);
...
/*writing is performed*/
...
V(wrl);
} while(TRUE);
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Review 45
Reader
do {
P(mutex);
rcount++;
if (rcount == 1)
P(wrl);
V(mutex);
...
/*reading is performed*/
...
P(mutex);
rcount--;
if (rcount == 0)
V(wrl);
V(mutex);
} while(TRUE);
Silberschatz, Galvin and Gagne ©2007
Dining Philosophers Solution
Shared: int state[5], semaphore s[5], semaphore mutex;
Init: mutex = 1; s[i] = 0, state[i] = thinking, for all i=0 .. 4;
take_chopstick(i) {
P(mutex);
Philosopher i
state[i] = hungry;
test(i);
do {
V(mutex);
take_chopstick(i);
P(s[i]);
eat();
put_chopstick(i); }
think();
put_chopstick(i) {
} while(true);
P(mutex);
state[i] = thinking;
test((i+1)%N);
test((i-1+N)%N);
V(mutex);
}
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
test(i) {
if(state[i] == hungry
&& state[(i+1)%N] != eating
&& state[(i-1+N)%N != eating)
{
state[i] = eating;
V(s[i]);
}
Review 46
Silberschatz, Galvin and Gagne ©2007
Scheduling Concepts









CPU–I/O Burst Cycle
Preemptive vs. non-preemptive
Gantt charts
Time quanta
Multilevel feedback queues
Multiprocessor Scheduling
Real-time scheduling
Evaluating Algorithms
Workloads
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Review 47
Silberschatz, Galvin and Gagne ©2007
Scheduling Criteria / Metrics






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 total output (for time-sharing environment)
 E.g., when a Web page starts rendering (response time)
vs. when it finishes (turnaround time)
Fairness – does every job get a chance to run
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Review 48
Silberschatz, Galvin and Gagne ©2007
Scheduling Algorithms





First-Come First-Server (FCFS)
Shortest Job First
Shortest Remaining Processing Time
Priority
Round-robin
Operating System Concepts with Java – 7th Edition, Nov 15, 2006
Review 49
Silberschatz, Galvin and Gagne ©2007