Midterm Review: CMSC 421 Fall 05
Midterm Review: CMSC 421 Fall 05
Midterm Review: CMSC 421 Fall 05
Operating-System
Structures
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?
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)
APISystem CallOS Relationship
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
Layered Approach
The operating system is divided into a number of layers (levels), each
built on top of lower layers. The bottom layer (layer 0), is the
hardware; the highest (layer N) is the user interface.
With modularity, layers are selected such that each uses functions
(operations) and services of only lower-level layers
Microkernel System Structure
Moves as much from the kernel into user space
Communication takes place between user modules
using message passing
Benefits:
Easier to extend a microkernel
Easier to port the operating system to new architectures
More reliable (less code is running in kernel mode)
More secure
Detriments:
Performance overhead of user space to kernel space
communication
Modules
Most modern operating systems
implement kernel modules
Uses object-oriented approach
Each core component is separate
Each talks to the others over known interfaces
Each is loadable as needed within the kernel
Overall, similar to layers but with more
flexible
Virtual Machines
A virtual machine takes the layered approach to
its logical conclusion. It treats hardware and the
operating system kernel as though they were all
hardware
A virtual machine provides an interface identical
to the underlying bare hardware
The operating system creates the illusion of
multiple processes, each executing on its own
processor with its own (virtual) memory
Chapter 3
Processes
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
Process State
As a process executes, it changes state
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
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
should be executed next and allocates CPU
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
Context Switch
Buffering
Client-Server Communication
Sockets
Endpoint for communication
Communication consists between a pair of
sockets
Remote Procedure Call (RPC)
Abstracts procedure calls between processes on networked
systems.
Stubs client-side proxy for the actual procedure on the server.
The client-side stub locates the server and marshalls the
parameters.
The server-side stub receives this message, unpacks the
marshalled parameters, and peforms the procedure on the
server.
Chapter 4: Threads
Threads
Benefits: Responsiveness, Resource
Sharing, Economy, Utilization of MP
Architectures
User Threads: POSIX Pthreads, Win32
threads, Java threads
Kernel Threads
Multithreading Models
Many-to-One
One-to-One
Many-to-Many
Threading Issues
Semantics of fork() and exec() system
calls
Thread cancellation
Asynchronous cancellation
Deferred cancellation
Thread pools
Create a number of threads in a pool where
they await work
Thread specific data
Scheduler activations
upcalls
Signal Handling
Signals are used in UNIX systems to notify a process
that a particular event has occurred
A signal handler is used to process signals
Signal is generated by particular event
Signal is delivered to a process
Signal is handled
Options:
Deliver the signal to the thread to which the signal applies
Deliver the signal to every thread in the process
Deliver the signal to certain threads in the process
Assign a specific thread to receive all signals for the process
Windows XP Threads
Implements the one-to-one mapping
Each thread contains
A thread id
Register set
Separate user and kernel stacks
Private data storage area
The register set, stacks, and private storage area are known as the
context of the threads
The primary data structures of a thread include:
ETHREAD (executive thread block)
KTHREAD (kernel thread block)
TEB (thread environment block)
Linux Threads
Linux refers to them as tasks rather than
threads
Thread creation is done through clone()
system call
clone() allows a child task to share the
address space of the parent task (process)
Java Threads
Java threads are managed by the JVM
Java threads may be created by:
Extending Thread class
Implementing the Runnable interface
Chapter 5: CPU
Scheduling
Basic Concepts
Maximum CPU utilization obtained with
multiprogramming
CPUI/O Burst Cycle Process execution
consists of a cycle of CPU execution and
I/O wait
CPU burst distribution
CPU Scheduler
Selects from among the processes in memory that are
ready to execute, and allocates the CPU to one of them
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
Dispatcher
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
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 time-sharing environment)
Scheduling
First Come, First Serve
Shortest-Job-First
Pre-emptive
Non Pre-emptive
Priority
Round Robin
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
Multilevel Feedback Queue
A process can move between the various queues; 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
Multiple-Processor Scheduling
CPU scheduling more complex when multiple
CPUs are available
Homogeneous processors within a
multiprocessor
Load sharing
Asymmetric multiprocessing only one
processor accesses the system data structures,
alleviating the need for data sharing
Real-Time Scheduling
Hard real-time systems required to
complete a critical task within a
guaranteed amount of time
Soft real-time computing requires that
critical processes receive priority over less
fortunate ones
Thread Scheduling
Local Scheduling How the threads
library decides which thread to put onto an
available LWP
Process
Synchronization
Background
Concurrent access to shared data may result in data
inconsistency
Maintaining data consistency requires mechanisms to
ensure the orderly execution of cooperating processes
Producer - writer
Consumer - reader
Race Condition outcome of execution depends on the
orrder in which access to data takes place
Critical Section segment of code in which it is crucial
that no other process is allowed to execute
simultaneously
Critical-Section Problem Solution
Mutual Exclusion - If process Pi is executing in its
critical section, then no other processes can be
executing in their critical sections
do { key = TRUE;
while ( key == TRUE)
Swap (&lock, &key );
//critical section
lock = FALSE;
//remainder section
} while ( TRUE);
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++;
}
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 S; // initialized to 1
wait (S);
Critical Section
signal (S);
Semaphore Implementation
Must guarantee that no two processes can execute
wait () and signal () on the same semaphore at the same
time
Thus, implementation becomes the critical section
problem where the wait and signal code are placed in
the crtical section.
Could now have busy waiting in critical section implementation
But implementation code is short
Little busy waiting if critical section rarely occupied
Note that applications may spend lots of time in critical
sections and therefore this is not a good solution.
Solution : Semaphore with no busy waiting
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);
procedure Pn () {}
Initialization code ( .) { }
}
}
Condition Variables
Chapter 7
Deadlocks
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.
Memory Management
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
Shared Pages
Segmentation
Memory-management scheme that supports user view of memory
A program is a collection of segments. A segment is a logical unit
such as main program, procedure, function, etc.
Segment table maps two-dimensional physical addresses; each
table entry has:
base contains the starting physical address where the segments
reside in memory
limit specifies the length of the segment
Segment-table base register (STBR) points to the segment tables
location in memory
Segment-table length register (STLR) indicates number of segments
used by a program;
segment number s is legal if s < STLR
Segmentation (cont.)
Relocation.
dynamic
by segment table
Sharing.
shared segments
same segment number
Allocation.
first fit/best fit
external fragmentation
Protection. With each entry in segment table associate:
validation bit = 0 illegal segment
read/write/execute privileges
Protection bits associated with segments; code sharing occurs at segment level
Since segments vary in length, memory allocation is a dynamic storage-allocation
problem