is112Ch11

advertisement
Chapter 11
Operating Systems
Chapter goals



Describe the functions and layers of an
operating system
List the resources allocated by the
operating system and describe the
complexities of the allocation process
Describe how an operating system
manages programs, processes, and
threads
Chapter goals cont.



Compare and contrast alternative CPU
scheduling methods
Explain how the operating system
manages memory
Describe signals and pipes and how
they are used to coordinate threads and
processes
Role of the Operating System


Critical component of all modern
information systems
Users and application programs depend
on OS to manage



Access to hardware & software resources
Efficient time management of computer
resources
Security and data protection
OS Functions
OS Management functions

Divided between



functions related to managing hardware
resources (CPU and memory)
management tasks oriented to users and
their programs
Focus of this chapter will be on CPU,
memory, and process management
OS hardware management



All running programs require access to
CPU and memory (RAM) in order to
execute
Early computers executed one program
at a time
Modern computers can simultaneously
execute many programs at the same
time
CPU and memory
management


Although it appears, that programs are
running simultaneously, there is only
one CPU
CPU can only execute one instruction at
a time (although that time requirement
is extremely short)
OS management

OS keeps track of




What resources are available
What programs are currently running
OS decides what program gets what
resource
OS decides what program gets to
execute
OS management



Computer can have hundreds or even
thousands of programs executing at the
same time (mainframe)
OS tracks programs from start to finish
Processes all requests for resource
allocation and communication
OS management cont.


All executing programs compete for
access to limited RAM and a small
number (or a single) CPU
OS controls what programs have what
resources and for how long
OS layers


Like most complex software operating
systems are organized into layers
(Figure 11-3)
One layer can be modified without
changing another layer


Outer layer provides services to users and
application programs
Inner layer connects to hardware resources
OS Layers
OS layers cont.

Command Layer – the user’s interface to the
operating system



Service Layer – contains a set of functions called by
application programs and the command layer


Unix/DOS commands like DIR, XCOPY
Windows/GUI icons can also invoke OS functions. Example:
changing desktop settings
File access – open, save, close
Kernel – manages resources and directly interacts
with computer hardware

Interacts with device controllers and other hardware
elements of the computer
Kernel



Portion of OS that interacts with
hardware
Contains a set of interface programs
called device drivers As hardware is
updated or installed, device drivers are
added or modified
**demo device drivers by running add
new hardware
OS Kernel
Resource allocation



Early computers could only manage to
execute one program at a time
As computers grew more powerful
could manage more than one executing
program at a time
OS needs to keep track of each
executing program and keep them out
of each other’s way
Single tasking OS

Involves two executing programs




Application
Operating system
MS-DOS is a single-tasking operating
system
Most service calls are actually indirect
requests for system resources
Multiple process resource
allocation




All modern OS are able to manage
multiple processes
Difference between DOS and Windows
Obviously this is a more complicated
problem
What management problems must OS
solve to allow multi-tasking?
Multi-process OS

Management problems include:





Deciding which process gets the CPU
Deciding which process gets a resource
Keeping track of processes while
suspended
Swapping out one process and replacing
with another
Keep processes separate
Multi-tasking

OS and large scale application systems
are composed of semi-independent
modules or processes



Word processor printer formatting and
interface functions can be in a separate
module
Think about functions WP can perform
What other WP functions could be
separated into modules?
Multi-tasking cont.


Modules can be loaded into memory as
needed and suspended when task is
complete
Modules can operate independently

User can continue with other WP tasks
while a document is printing
System software multi-tasking

System software functions are also
better managed as independent
modules


Network interface process can be loaded
into memory and suspended until needed
Dividing OS tasks among processes
makes the entire system easier to build
and upgrade
Resource allocation goals

A multi-tasking OS manages hardware
resources according to the following
goals



Meet the resource needs of processes
Prevent processes from interfering with
each other
Efficiently use hardware and other
resources
Managing multiple processes

This requires




detailed records of available resources
knowing which resources are used to satisfy which
requests
Scheduling resources based on specific allocation
policies to meet present and anticipated demands
Updating records to reflect resources commitment
and release by processes and users
System overhead


The resources needed by the OS in
order to manage multiple processes is
called system overhead
Goal is to minimize system overhead as
much as possible, while still providing
enough support for executing processes
Expanding system resources



In order to accommodate as many executing
processes as possible the OS can expand a
computer’s real resources by using virtual resources
Real resource – a computer system’s physical
devices and associated system software
Virtual resource – the resources that are apparent
to a process or user
OS resource management

Providing virtual resources that meet or
exceed real resources is achieved by



Rapidly shifting resources unused by one
process to other processes that need them
Substituting one type of resource for
another when possible and necessary
Main example – virtual memory

Secondary storage (hard drive) extends
main memory
What is a process?



Multi-process OS is capable of
managing several programs running at
once
Programs are usually sub-divided into
separate, independent sections
Process is a unit of executing software
that is managed independently by the
OS
Process definition




Can request hardware resources and OS
services
Stand-alone or part of group of processes
that cooperate for common purpose
Communicate with other processes on same
computer or with processes on other
computer systems (via network or internet)
Look at current processes on this machine
(ctrl-alt-del)
How OS manages processes



OS must keep track of each active
process
Does this with a custom data structure
containing information relevant to an
active process
Data structure is a collection of related
information
Process control data structures




OS keeps track of each process by
creating and updating a data structure
called a process control block (PCB) for
each active process
PCB is created when process begins
Updated whenever process status
changes
Is deleted when process terminates
Use of PCB

OS uses PCB to manage processes



Resource allocation
Secure resource access
Protect active processes from interfering
with each other
PCB layout


PCB varies from operating system to
operating system
Typical data items include







A unique process identification number
The current state of the process
Events for which the process is waiting
Resources allocated exclusively to the process
Machine resources consumed
Process ownership and/or access privileges
Scheduling priority
How the PCB is used


PCBs for all current processes are maintained
in a linked list called a process queue or
process list
OS frequently searches process list




Looking for next process to run
Looking for process that needs a specific resource
Linked list is a data structure that can be
easily searched
Items can be easily added or deleted (unlike
an array or table)
Process Viewer & PCBs
Threads



Thread is a sub-unit of a process
Multiple threads can share resources
allocated to parent process
Thread is a sub-unit of code that can
execute independently
Thread Control Block



TCB is a data structure containing
information about a thread
All current TCBs are kept in a run queue
or a thread list
An OS capable of running multiple
threads related to the same process is
multi-threaded

Windows 2000, Java Virtual Machine, UNIX
CPU Allocation



Access to the CPU is the scarcest
resource in the computer
Most computer systems have only one
or two CPUs
Yet modern OS can execute hundreds
or even thousands of threads within the
same time frame
CPU sharing



OS makes rapid decisions about which
threads receive CPU control and for how
long control is retained
Typically a thread controls the CPU for a
few milliseconds at a time
This is called concurrent execution or
interleaved execution
Managing Concurrent
Processes
Thread states




An active thread can be in one of the
following states:
Ready
Running
Blocked
Thread states



Ready – thread is ready to execute and
is waiting for access to the CPU
Running – thread has control of the
CPU
Blocked – thread is suspended while an
interrupt is being processed
Managing Threads
Thread states

Thread is created and dispatched (given
the CPU)


Loads the instruction pointer with the
starting address of that thread
Thread with CPU is in the running state
until


Thread terminates normally
An interrupt occurs
Threads & interrupts

Interrupts can occur due to




Executing another service call, such as a
request for file I/O
Hardware error such as overflow or power
failure
Interrupt generated by peripheral device
Once interrupt occurs thread loses CPU
and is placed in the blocked state
Interrupt processing


Threads are placed in the blocked state
in response to an interrupt
Interrupts can occur due to error
conditions


If error cannot be corrected the thread is
halted
Otherwise thread remains in blocked state
until error condition is resolved
Interrupt example

Thread attempts to access a file on a
removable disk and disk is not in the
drive




Disk drive controller generates an interrupt
CPU blocks (suspends) the thread
Passes CPU control to supervisor to process
the interrupt
OS displays error message, and if user
inserts correct disk thread is unblocked
Scheduling threads



When more than one thread or process
is competing for access to the CPU, OS
must decide order of execution
This is referred to as scheduling
OS must apply some criteria to
determine which thread goes next
Scheduling methods

Common scheduling methods:



Preemptive scheduling
Priority-based scheduling
Real-time scheduling
Preemptive Scheduling



A thread can be removed involuntarily
from the running state
A running process controls the CPU by
controlling the content of the instruction
pointer
CPU control is lost whenever an
interrupt is received
Supervisor

Portion of OS that receives CPU control
after an interrupt is called the
Supervisor


Supervisor calls appropriate interrupt
handler
When interrupt handler ends Supervisor
calls Scheduler
Scheduler

Scheduler is portion of OS that
manages currently executing threads




Updates the status of any thread or
process affected by the last interrupt
Decides which thread to dispatch to CPU
Updates thread control information and the
stack to reflect the scheduling decision
Dispatches the selected thread
Interrupts & Threads
Other interrupts



Because interrupt arrival is
unpredictable, time that thread remains
in the run state is unpredictable
Theoretically thread could control CPU
indefinitely if it made no service calls
Thread could get stuck in infinite loop
and never relinquish CPU (demo infinite
loop)
Timer interrupts



Timer interrupts happen at regular
intervals (i.e. every two hundred CPU
cycles)
Opportunity for Scheduler to dispatch
another thread
This insures every thread gets an
opportunity to make progress towards
completion
Other scheduling methods

Priority based scheduling uses some
criteria to decide which thread to
dispatch



First come first served
Explicit priority
Shortest time remaining
Scheduling
First come first served:
 The scheduler always dispatches the ready thread
that has been waiting the longest
Explicit priority:
Uses a set of priority levels and assigns a level to each
process or thread
 The scheduling method can use priority levels in two
ways:
 Always dispatch the highest priority ready thread
 Assign larger time slices to high priority threads
Thread priorities
Scheduling cont.
Shortest time remaining:
 Chooses the next process to be dispatched based on
the expected amount of CPU time needed to
complete the process
Real-Time Scheduling:
 Guarantees minimum amount of CPU time to a
thread if the thread makes an explicit request when it
is created
 Used when a thread must have enough resources to
complete its function within a specified time (i.e. ATM
bank transaction)
Memory allocation

Every executing process thread needs
space in memory



Instructions
Data
Every process needs memory that it
does not share with other processes
(i.e. its data & instructions are unique
to that process)
Memory allocation cont.


The more currently executing
processes, the greater the demand on
memory resources
Often demand for memory can exceed
the supply

Solution is virtual memory – image of
memory for suspended processes swapped
out to disk until process is re-started
Single-tasking


Single-tasking memory allocation must
divide memory between OS and single
executing application
Contiguous allocation places application
process adjacent to OS
OS Memory Management
Non-contiguous allocation



Often to save space only the more
frequently used OS service calls are
loaded into memory
Less frequently used sections are
loaded as needed into free areas in
memory
Since these free areas are separate this
is called non-contiguous allocation
Non-contiguous memory
Multi-tasking memory
allocation



Allocation and managing memory
spaces for multiple executing processes
is a much more complicated task
Must keep track of available regions of
memory
Must release memory from processes
that don’t need it anymore
Goals of Multi-tasking OS

Multi-tasking OS needs to




allow as many active processes as possible
Respond quickly to changing memory
demands of processes
Prevent unauthorized changes to a
process’s memory region(s)
Implement memory allocation and
addressing as efficiently as possible
Partitioned memory

In order to distribute memory among
multiple processes, memory is divided
in to partitions



Each partition is same size
Each partition can hold all or part of a
process or thread
Each active process gets a minimum of 1
partition
Memory Partitions
3 processes share memory
with OS
Memory allocation table



OS maintains a memory partition table
and updates it every time is allocated or
released
When a process ready to be loaded, OS
searches table for free memory
(partition), then allocates to process
When process terminates, its memory is
released
Memory Allocation Table
Memory Management Over
Time
Wasted memory


Continuous program loadings plus fixed
size partitions leads to wasted memory
Free (available) memory is broken into
smaller and smaller pieces
Memory fragmentation


Over time, this type of memory
allocation results is small bits of free
memory scattered throughout RAM
Wasted memory is caused by


Wasted (unused) memory inside a partition
Fragments of free memory too small for a
new process
Compaction



One solution to fragmented memory is
to periodically relocate all current
processes
Move processes together and group
free space together
Time consuming
Managing Free Space
Another solution

Non-contiguous allocation can solve
memory fragmentation problems


Smaller fixed size partitions (~64 kb)
Memory requirements of a process can be
split between non-contiguous sections
Non-contiguous memory
Virtual memory management

It is not absolutely necessary for an
entire process to reside in memory
while executing


The bare minimum is the next instruction
to be fetched and any operands currently
stored in memory
Everything else could be stored elsewhere
– i.e. in “virtual memory”
Virtual memory cont.


If OS minimizes the memory
requirements of individual processes,
that allows a greater number of
processes to be active
OS virtual memory management needs
to keep track of each part of an active
process and load portions into memory
as needed
Virtual memory cont.



Virtual memory management divides an
active process into partitions called
pages
Each page is a small, fixed size portion
(~1-4 kb)
Memory is also divided into pages of
the same size
Pages & page frames



Each memory page is called a page
frame
During program execution one or more
pages is allocated to page frames and
the rest continues to reside on disk
Pages are swapped into memory as
needed during execution
Swapping pages

When ever executing program attempts
to access memory, OS intervenes and
determines if that page is actually in
memory or residing on disk


Page hit – page is already in memory as
code is able to execute
Page fault – page needed is out on disk,
needs to be swapped in, then code can
execute
Page tables


OS manages virtual memory by
maintaining page tables for each active
process
Page table keeps track of what pages
exist for the process and where they
are located


Location in memory or
Location on hard drive
Process Page Table
Using secondary storage


Secondary storage is used in virtual
memory management to extend the
resources of memory
A region on hard drive called the swap
space is reserved for holding pages not
in memory
Swapping pages


If memory reference is to a page in
swap space, that page is loaded into a
page frame in memory
If all the page frames are currently
allocated, a page currently in memory
known as the victim must be swapped
out to storage
Swapping pages

Choosing page to swap out of page frame is
based on



Least frequently used
Least recently used
This requires OS overhead


Updating information about access to pages
Search page table and select pages to swap out
Memory protection



Refers to the protection of memory
allocated to one program from
unauthorized access by another
program
Prevents errors in one program from
causing errors in another program
Adds OS overhead to every write
operation
Memory management
hardware



Early versions of multi-tasking, virtual
memory management OSs were
implemented in software
This resulted in severe performance
penalties
Newer CPUs have built-in hardware to
perform memory allocation and address
resolution
Coordination between processes



Modern systems are made up of
distributed components (processes) that
may be running concurrently
Often need to communicate,
synchronize their activates, or exchange
data
Can share memory through mechanism
known as a pipe
Inter-process shared data
Uses of shared memory



Producer-consumer – one process
creates data, other process uses data
Send signals for synchronization of
processes
For example, print process can send
signal to word processing process that
print job is completed
Summary



An operating system is the most
complex component of system software
The operating system allocates
hardware resources to individual user
processes on demand
The operating system stores information
about each process in a PCB
Summary cont.



Application software is simpler to develop if
programs are unaware of resource allocation
functions
An active thread is always in one of three
states – ready, running, or blocked
Memory is divided into fixed-size partitions
and processes are allocated one or more
memory partitions to store instructions and
data
Summary cont.

Modern operating systems implement a
form of memory allocation and
management called virtual memory
management

Processes and threads often need to
synchronize their actions or transfer data
among themselves using signals and pipes
Download