Midterm Review
Midterm Review
Midterm Review
Operating System Concepts – 9th Edition 1.1 Silberschatz, Galvin and Gagne ©2013
Computer System Structure
Operating System Concepts – 9th Edition 1.2 Silberschatz, Galvin and Gagne ©2013
Four Components of a Computer System
Operating System Concepts – 9th Edition 1.3 Silberschatz, Galvin and Gagne ©2013
Operating System Definition
OS is a resource allocator
Manages all resources
Decides between conflicting requests for efficient and
fair resource use
OS is a control program
Controls execution of programs to prevent errors and
improper use of the computer
Operating System Concepts – 9th Edition 1.4 Silberschatz, Galvin and Gagne ©2013
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. A memory controller synchronizes access to the memory.
Operating System Concepts – 9th Edition 1.5 Silberschatz, Galvin and Gagne ©2013
Storage Hierarchy
Operating System Concepts – 9th Edition 1.6 Silberschatz, Galvin and Gagne ©2013
Storage-Device Hierarchy
Operating System Concepts – 9th Edition 1.7 Silberschatz, Galvin and Gagne ©2013
Caching
Operating System Concepts – 9th Edition 1.8 Silberschatz, Galvin and Gagne ©2013
Operating System Structure
Multiprogramming (Batch system) 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
Operating System Concepts – 9th Edition 1.9 Silberschatz, Galvin and Gagne ©2013
Operating-System Operations (cont.)
Operating System Concepts – 9th Edition 1.10 Silberschatz, Galvin and Gagne ©2013
Operating System Services
Operating systems provide an environment for execution of programs
and services to programs and users
One set of operating-system services provides functions that are
helpful to the user:
User interface - Almost all operating systems have a user
interface (UI).
Varies between Command-Line (CLI), Graphics User
Interface (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
Operating System Concepts – 9th Edition 1.11 Silberschatz, Galvin and Gagne ©2013
Operating System Services (Cont.)
Operating System Concepts – 9th Edition 1.12 Silberschatz, Galvin and Gagne ©2013
Operating System Services (Cont.)
Another set of OS functions exists for ensuring the efficient operation of the
system itself via resource sharing
Resource allocation - When multiple users or multiple jobs running
concurrently, resources must be allocated to each of them
Many types of resources - CPU cycles, main memory, file storage,
I/O devices.
Accounting - To keep track of which users use how much and what
kinds of computer resources
Protection and security - The owners of information stored in a
multiuser or networked computer system may want to control use of
that information, concurrent processes should not interfere with each
other
Protection involves ensuring that all access to system resources is
controlled
Security of the system from outsiders requires user authentication,
extends to defending external I/O devices from invalid access
attempts
Operating System Concepts – 9th Edition 1.13 Silberschatz, Galvin and Gagne ©2013
A View of Operating System Services
Operating System Concepts – 9th Edition 1.14 Silberschatz, Galvin and Gagne ©2013
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 Programming Interface (API) rather than
direct system call use
Three most common APIs are Windows 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)
Operating System Concepts – 9th Edition 1.15 Silberschatz, Galvin and Gagne ©2013
System Call Implementation
Operating System Concepts – 9th Edition 1.16 Silberschatz, Galvin and Gagne ©2013
API – System Call – OS Relationship
Operating System Concepts – 9th Edition 1.17 Silberschatz, Galvin and Gagne ©2013
Operating System Design and Implementation
Operating System Concepts – 9th Edition 1.18 Silberschatz, Galvin and Gagne ©2013
Operating System Design and Implementation (Cont.)
Operating System Concepts – 9th Edition 1.19 Silberschatz, Galvin and Gagne ©2013
Operating System Structure
General-purpose OS is very large program
Various ways to structure ones
Simple structure – MS-DOS
More complex -- UNIX
Layered – an abstrcation
Microkernel -Mach
Operating System Concepts – 9th Edition 1.20 Silberschatz, Galvin and Gagne ©2013
Process Concept
Operating System Concepts – 9th Edition 1.21 Silberschatz, Galvin and Gagne ©2013
Process State
Operating System Concepts – 9th Edition 1.22 Silberschatz, Galvin and Gagne ©2013
Diagram of Process State
Operating System Concepts – 9th Edition 1.23 Silberschatz, Galvin and Gagne ©2013
Process Control Block (PCB)
Information associated with each process
(also called task control block)
Process state – running, waiting, etc
Program counter – location of
instruction to next execute
CPU registers – contents of all process-
centric registers
CPU scheduling information- priorities,
scheduling queue pointers
Memory-management information –
memory allocated to the process
Accounting information – CPU used,
clock time elapsed since start, time
limits
I/O status information – I/O devices
allocated to process, list of open files
Operating System Concepts – 9th Edition 1.24 Silberschatz, Galvin and Gagne ©2013
CPU Switch From Process to Process
Operating System Concepts – 9th Edition 1.25 Silberschatz, Galvin and Gagne ©2013
Process Scheduling
Operating System Concepts – 9th Edition 1.26 Silberschatz, Galvin and Gagne ©2013
Ready Queue And Various I/O Device Queues
Operating System Concepts – 9th Edition 1.27 Silberschatz, Galvin and Gagne ©2013
Representation of Process Scheduling
Operating System Concepts – 9th Edition 1.28 Silberschatz, Galvin and Gagne ©2013
Schedulers
Short-term scheduler (or CPU scheduler) – selects which process should
be executed next and allocates CPU
Sometimes the only scheduler in a system
Short-term scheduler is invoked frequently (milliseconds) (must be
fast)
Long-term scheduler (or job scheduler) – selects which processes should
be brought into the ready queue
Long-term scheduler is invoked 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
Long-term scheduler strives for good process mix
Operating System Concepts – 9th Edition 1.29 Silberschatz, Galvin and Gagne ©2013
Operations on Processes
Operating System Concepts – 9th Edition 1.30 Silberschatz, Galvin and Gagne ©2013
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 options
Parent and children share all resources
Children share subset of parent’s resources
Parent and child share no resources
Execution options
Parent and children execute concurrently
Parent waits until children terminate
Operating System Concepts – 9th Edition 1.31 Silberschatz, Galvin and Gagne ©2013
Process Creation (Cont.)
Address space
Child duplicate of parent (has the same program as the
parent)
Child has a program loaded into it
UNIX examples
fork() system call creates new process. The new process
consists of a copy of the address space of the original
process.
exec() system call used after a fork() to replace the
process’ memory space with a new program
move itself off the ready queue until the termination of the child
Operating System Concepts – 9th Edition 1.32 Silberschatz, Galvin and Gagne ©2013
C Program Forking Separate Process
Operating System Concepts – 9th Edition 1.33 Silberschatz, Galvin and Gagne ©2013
Process Termination
Operating System Concepts – 9th Edition 1.34 Silberschatz, Galvin and Gagne ©2013
Interprocess Communication
Operating System Concepts – 9th Edition 1.35 Silberschatz, Galvin and Gagne ©2013
Communications Models
(a) Message passing. (b) shared memory.
Operating System Concepts – 9th Edition 1.36 Silberschatz, Galvin and Gagne ©2013
Race Condition
Operating System Concepts – 9th Edition 1.37 Silberschatz, Galvin and Gagne ©2013
Critical Section Problem
Consider system of n processes {p0, p1, … pn-1}
Each process has critical section segment of code
Process may be changing common variables, updating
table, writing file, etc
When one process in critical section, no other may be in its
critical section
Critical section problem is to design protocol to solve this
problem
Each process must ask permission to enter critical section in
entry section, may follow critical section with exit section,
then remainder section
Operating System Concepts – 9th Edition 1.38 Silberschatz, Galvin and Gagne ©2013
Critical Section
Operating System Concepts – 9th Edition 1.39 Silberschatz, Galvin and Gagne ©2013
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 – 9th Edition 1.40 Silberschatz, Galvin and Gagne ©2013
Peterson’s Solution
Good algorithmic description of solving the problem
Two process solution
Assume that the load and store machine-language
instructions are atomic; that is, cannot be interrupted
The two processes share two variables:
int turn;
Boolean flag[2]
Operating System Concepts – 9th Edition 1.41 Silberschatz, Galvin and Gagne ©2013
Algorithm for Process Pi
do {
flag[i] = true;
turn = j;
while (flag[j] && turn = = j);
critical section
flag[i] = false;
remainder section
} while (true);
Operating System Concepts – 9th Edition 1.42 Silberschatz, Galvin and Gagne ©2013
Synchronization Hardware
Many systems provide hardware support for implementing the
critical section code.
All solutions below based on idea of locking
Protecting critical regions via locks
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-interruptible
Either test memory word and set value
Or swap contents of two memory words
Operating System Concepts – 9th Edition 1.43 Silberschatz, Galvin and Gagne ©2013
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 – 9th Edition 1.44 Silberschatz, Galvin and Gagne ©2013
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 – 9th Edition 1.45 Silberschatz, Galvin and Gagne ©2013
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 – 9th Edition 1.46 Silberschatz, Galvin and Gagne ©2013
acquire() and release()
acquire() {
while (!available)
; /* busy wait */
available = false;
}
release() {
available = true;
}
do {
acquire lock
critical section
release lock
remainder section
} while (true);
Operating System Concepts – 9th Edition 1.47 Silberschatz, Galvin and Gagne ©2013
Semaphore
Synchronization tool that provides more sophisticated ways (than Mutex locks)
for process to synchronize their activities.
Semaphore S – integer variable
Can only be accessed via two indivisible (atomic) operations
wait() and signal()
Originally called P() and V()
Definition of the wait() operation
wait(S) {
while (S <= 0)
; // busy wait
S--;
}
Definition of the signal() operation
signal(S) {
S++;
}
Operating System Concepts – 9th Edition 1.48 Silberschatz, Galvin and Gagne ©2013
Semaphore Usage
Counting semaphore – integer value can range over an unrestricted
domain
Binary semaphore – integer value can range only between 0 and 1
Same as a mutex lock
Can solve various synchronization problems
Consider P1 and P2 that require S1 to happen before S2
Create a semaphore “synch” initialized to 0
P1:
S1;
signal(synch);
P2:
wait(synch);
S2;
Can implement a counting semaphore S as a binary semaphore
Operating System Concepts – 9th Edition 1.49 Silberschatz, Galvin and Gagne ©2013
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);
Operating System Concepts – 9th Edition 1.50 Silberschatz, Galvin and Gagne ©2013
Classical Problems of Synchronization
Operating System Concepts – 9th Edition 1.51 Silberschatz, Galvin and Gagne ©2013
Bounded-Buffer Problem
Operating System Concepts – 9th Edition 1.52 Silberschatz, Galvin and Gagne ©2013
Bounded Buffer Problem (Cont.)
do {
...
/* produce an item in next_produced */
...
wait(empty);
wait(mutex);
...
/* add next produced to the buffer */
...
signal(mutex);
signal(full);
} while (true);
Operating System Concepts – 9th Edition 1.53 Silberschatz, Galvin and Gagne ©2013
Bounded Buffer Problem (Cont.)
The structure of the consumer process
Do {
wait(full);
wait(mutex);
...
/* remove an item from buffer to next_consumed */
...
signal(mutex);
signal(empty);
...
/* consume the item in next consumed */
...
} while (true);
Operating System Concepts – 9th Edition 1.54 Silberschatz, Galvin and Gagne ©2013
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
Several variations of how readers and writers are considered – all
involve some form of priorities
Shared Data
Data set
Semaphore rw_mutex initialized to 1
Semaphore mutex initialized to 1
Integer read_count initialized to 0
Operating System Concepts – 9th Edition 1.55 Silberschatz, Galvin and Gagne ©2013
Readers-Writers Problem (Cont.)
do {
wait(rw_mutex);
...
/* writing is performed */
...
signal(rw_mutex);
} while (true);
Operating System Concepts – 9th Edition 1.56 Silberschatz, Galvin and Gagne ©2013
Readers-Writers Problem (Cont.)
The structure of a reader process
do {
wait(mutex);
read_count++;
if (read_count == 1)
wait(rw_mutex);
signal(mutex);
...
/* reading is performed */
...
wait(mutex);
read count--;
if (read_count == 0)
signal(rw_mutex);
signal(mutex);
} while (true);
Operating System Concepts – 9th Edition 1.57 Silberschatz, Galvin and Gagne ©2013
Dining-Philosophers Problem
Operating System Concepts – 9th Edition 1.58 Silberschatz, Galvin and Gagne ©2013
Dining-Philosophers Problem Algorithm
The structure of Philosopher i:
do {
wait (chopstick[i] );
wait (chopStick[ (i + 1) % 5] );
// eat
signal (chopstick[i] );
signal (chopstick[ (i + 1) % 5] );
// think
} while (TRUE);
What is the problem with this algorithm?
Operating System Concepts – 9th Edition 1.59 Silberschatz, Galvin and Gagne ©2013
Dining-Philosophers Problem Algorithm (Cont.)
Deadlock handling
Allow at most 4 philosophers to be sitting
simultaneously at the table.
Allow a philosopher to pick up the forks only if both
are available (picking must be done in a critical
section.
Use an asymmetric solution -- an odd-numbered
philosopher picks up first the left chopstick and then
the right chopstick. Even-numbered philosopher picks
up first the right chopstick and then the left chopstick.
Operating System Concepts – 9th Edition 1.60 Silberschatz, Galvin and Gagne ©2013
Monitors
A high-level abstraction that provides a convenient and effective
mechanism for process synchronization
Abstract data type, internal variables only accessible by code within the
procedure
Only one process may be active within the monitor at a time
But not powerful enough to model some synchronization schemes
monitor monitor-name
{
// shared variable declarations
procedure P1 (…) { …. }
Operating System Concepts – 9th Edition 1.61 Silberschatz, Galvin and Gagne ©2013
Condition Variables
condition x, y;
Two operations are allowed on a condition variable:
x.wait() – a process that invokes the operation is
suspended until x.signal()
x.signal() – resumes one of processes (if any) that
invoked x.wait()
If no x.wait() on the variable, then it has no effect on
the variable
Operating System Concepts – 9th Edition 1.62 Silberschatz, Galvin and Gagne ©2013
Monitor with Condition Variables
Operating System Concepts – 9th Edition 1.63 Silberschatz, Galvin and Gagne ©2013
Monitor Solution to Dining Philosophers
monitor DiningPhilosophers
{
enum { THINKING; HUNGRY, EATING) state [5] ;
condition self [5];
Operating System Concepts – 9th Edition 1.64 Silberschatz, Galvin and Gagne ©2013
Solution to Dining Philosophers (Cont.)
void test (int i) {
if ((state[(i + 4) % 5] != EATING) &&
(state[i] == HUNGRY) &&
(state[(i + 1) % 5] != EATING) ) {
state[i] = EATING ;
self[i].signal () ;
}
}
initialization_code() {
for (int i = 0; i < 5; i++)
state[i] = THINKING;
}
}
Operating System Concepts – 9th Edition 1.65 Silberschatz, Galvin and Gagne ©2013
Solution to Dining Philosophers (Cont.)
DiningPhilosophers.pickup(i);
EAT
DiningPhilosophers.putdown(i);
Operating System Concepts – 9th Edition 1.66 Silberschatz, Galvin and Gagne ©2013
CPU Scheduling - Basic Concepts
Operating System Concepts – 9th Edition 1.67 Silberschatz, Galvin and Gagne ©2013
CPU Scheduler
Short-term scheduler selects from among the processes in
ready queue, and allocates the CPU to one of them
Queue may be ordered in various ways
CPU scheduling decisions may take place when a process:
1. Switches from running to waiting state
2. Switches from running to ready state
3. Switches from waiting to ready
4. Terminates
Scheduling under 1 and 4 is nonpreemptive
All other scheduling is preemptive
Operating System Concepts – 9th Edition 1.68 Silberschatz, Galvin and Gagne ©2013
Dispatcher
Operating System Concepts – 9th Edition 1.69 Silberschatz, Galvin and Gagne ©2013
Scheduling Criteria
Operating System Concepts – 9th Edition 1.70 Silberschatz, Galvin and Gagne ©2013
Scheduling Algorithm Optimization Criteria
Operating System Concepts – 9th Edition 1.71 Silberschatz, Galvin and Gagne ©2013
First- Come, First-Served (FCFS) Scheduling
P1 P2 P3
0 24 27 30
Operating System Concepts – 9th Edition 1.72 Silberschatz, Galvin and Gagne ©2013
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
Could ask the user
Operating System Concepts – 9th Edition 1.73 Silberschatz, Galvin and Gagne ©2013
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
Operating System Concepts – 9th Edition 1.74 Silberschatz, Galvin and Gagne ©2013
Priority Scheduling
Operating System Concepts – 9th Edition 1.75 Silberschatz, Galvin and Gagne ©2013
Round Robin (RR)
Each process gets a small unit of CPU time (time quantum q),
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.
Timer interrupts every quantum to schedule next process
Performance
q large FIFO
q small q must be large with respect to context switch,
otherwise overhead is too high
Operating System Concepts – 9th Edition 1.76 Silberschatz, Galvin and Gagne ©2013
Multilevel Queue
Ready queue is partitioned into separate queues, eg:
foreground (interactive)
background (batch)
Process permanently in a given queue
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 – 9th Edition 1.77 Silberschatz, Galvin and Gagne ©2013
Multilevel Feedback Queue
Operating System Concepts – 9th Edition 1.78 Silberschatz, Galvin and Gagne ©2013
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 – 9th Edition 1.79 Silberschatz, Galvin and Gagne ©2013