Download virtual memory - Welcome to Online Management V2

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

CP/M wikipedia , lookup

Process management (computing) wikipedia , lookup

Memory management unit wikipedia , lookup

Paging wikipedia , lookup

Transcript
Computer Organization
and Architecture + Networks
Lecture 7
•Operating System (O/S)
Overview
• Introduction to the operating system and its
function
• O/S scheduling
• O/S memory management
Introduction
• Operating system:
— Program that manages the resources, provides services to
programmer, schedules execution of other program
— Masks the details of the hardware from the programmer
— Provides programmer with a good interface for using the system
— Mediates programmer and application programs requests for
facilities and services
• Services provided by the O/S:
— Program creation  general utilities help programmer in
creating programs
— Program execution  loading the instruction and data into main
memory, device initializing, etc for executing the program
— Standardized access to I/O devices  I/O devices require
different instructions or control signals, thus standard access is
needed
— Controlled access to files  to access a file, nature of the I/Os
Introduction
• Services provided by the O/S:
— Controlled access to files  to access a file, nature of the I/Os
and file format must be known
— Overall system access control  O/S controls the access to a
shared or public computer system
— Respond to detected errors  O/S will make the response to
clear the error condition eg. Program error or retry the
operation
— Accounting  O/S collects statistics like CPU usage etc.
• O/S as Resource Manager  directs the processor in the
use of the other system resources and in the timing of
the processor execution of other programs
O/S as a Resource Manager
Types of Operating System
• Interactive
— User directly interacts with the OS through a keyboard/terminal
• Batch
— User programs are collected together (“off line”) and submitted
to the OS in a batch by an operator
— Print-out results of the program execution is returned to the
user
— This type of OS is not typical current machines
• Single program (Uni-programming)
— Supports the execution of only 1 program at a time
• Multi-programming (Multi-tasking)
— An OS is said to be multiprogramming if it supports the
simultaneous execution of more than 1 program at a time
— A queue of pending programs is maintained
Types of Operating System
• Multi-programming (Multi-tasking)
— An OS is said to be multiprogramming if it supports the
simultaneous execution of more than 1 program at a time
— A queue of pending programs is maintained
— Current program is swapped out when it is idled waiting for I/O
and other devices
— Next pending program is started
— Time sharing is the term for multiprogramming when applied to
an interactive system
• Two main functions
1. Scheduling  OS determines which process should run at a
given time
2. Memory management  OS has virtual memory that is:
1.
2.
useful to run a program w/o having all the instruction and data to
be presented in the memory
To allow a memory space that exceed the actual memory size
Scheduling
• Key to multi-programming – in multiprogramming OS,
multiple jobs are held in memory and alternate between
using the CPU, using I/O and waiting (idle)
• The key to high efficiency with multiprogramming is
effective scheduling
— Long-, medium- and short-term scheduling
— I/O scheduling
Long-Term Scheduling
• Determines which programs are submitted into the
system for processing
• i.e. controls the degree of multi-programming
• Once submitted jobs are added to the queue of pending
jobs, a job becomes a process for the short term
scheduler
• (or it becomes a swapped out job for the medium term
scheduler)
• Works in batch or interactive modes
Medium-Term Scheduling
• Determines the jobs that can be swapped/replaced in
the memory (swapped-in jobs may be completed in a
short period of time
• Usually based on the need to manage multiprogramming
• If no virtual memory, memory management is also an
issue
Short-Term Scheduling
• This OS segment runs frequently and determines which
pending job will receive the CPU’s attention next
• Based on the normal changes of state that a job/process
goes through
• i.e. which job actually gets to use the processor in the
next time slot
Short-Term Scheduling
• A process is running in the CPU until
— It issues a service call to the OS (eg. for I/O service)
– Process is suspend until the request is satisfied
— Process causes and interrupt and is suspended
• Short-term scheduler is invoked to determine which
process is serviced next
Scheduling queue
Short-Term Scheduling
• Queues are simply lists of jobs that are waiting for some
type of service (eg. CPU cycles, I/O, etc.)
• Once the job has been admitted into the system by the
high- or long-term scheduler, it is allocated its portion of
memory for its program and data requirements
• The objective of the short-term scheduler is to keep the
CPU actively working on one of the pending jobs 
maximize the used CPU cycles
• Problem:
— The high-level scheduler will admit as many jobs as possible
(constrained by system resources such as available memory)
— Despite many jobs in the system, the speed of the CPU
(compared to peripherals) might be such that all jobs waiting
for (slow) I/O and thus the CPU is idled (no jobs in the ready
queue)
— Use memory management to solve this problem
Memory Management
• Uni-program
—Memory split into two
—One for Operating System (monitor)
—One for currently executing program
• Multi-program
—“User” part is sub-divided and shared among active
processes
—The sub-division is done by the OS and this process
is known as memory management
• memory needs to allocated efficiently to pack as many
processes into memory as possible
• Problem to pack many processes in the memory  I/O
is so slow compared with CPU that even in mutiprogram system, CPU can be idle most of the time
Memory Management
• To avoid having the CPU idle because all jobs are
waiting, one could increase the memory size to hold
more jobs
— Memory is expensive  costly solution to the problem
— Programs’ sizes (process sizes) tend to expand to take up
available memory
• A better approach is to use a mechanism to remove a
waiting job from memory and replace it with one that
(hopefully) will run on the CPU in the short-term
scheduling
— This is the idea of swapping
— Idle processes are removed from the memory and placed in an
intermediate queue
— Replace idled job with a “ready” one from the front of the
intermediate queue or with a new job
— Swapping uses I/O operations to read and write to disk
When none of the processes in the
memory is ready, processor remains
idle. To avoid this, processor swaps
one of the processes back out into
an intermediate queue (Temporarily
queue for process kick-out of
memory) and bring in another
process from intermediate queue or
process a new process from the
long-term queue.
Memory Management
• As a job is brought into memory, it must be allocated a portion of
the memory  this is its partition
• Partitions can be fixed sized of variable sized
— Fixed size
 Rigid size divisions of memory
 Process assigned to smallest available partition it fits into
 Can be very wasteful of memory
— Variable size
 Allocated a process to only as much memory as it needs
 Other memory can be used for other processes
 Efficient use of memory but, over time, can lead to large numbers
of little left over pieces of free memory  to small to use
 Must consider use of memory compaction. Compaction is a process
done by the O/S by shifting the processes in memory to put all the
free memory space together but change in addresses for memory
location occurs.
Fixed
Partitioning
Effect of Partitioning
Effect of Dynamic Partitioning
Memory Management
• Since a process can be swapped in and out of memory
and end up in different partitions, addressing within the
process must not be tied to specific physical memory
addresses
— Program/process addresses are logical ones with respect to the
starting address of the program itself
— Processor hardware must convert the logical address reference
of the program into a physical address that the machine can
operate on
— In effect this is a form of index addressing  base addressing
 where the base address is the first address of the program
Terms
Logical Address – location relative to the beginning of the program
Physical Address – actual location in the main memory
Paging
• Subdivide memory into small fixed-size “chunks” called
frames or pages frames
• Split programs into same sized chunks, called pages
• Split memory into equal sized, small chunks, called page
frames
• Loading a program in memory requires the allocation of
the required number of pages
• Limits wasted memory to a fraction of the last page
• Page frames used in loading process need not be
contiguous
— Each program has a page table associated with it that maps
each program page to a memory page frame
— Thus, logical addresses in a program are interpreted as a
physical address  page frame number and an offset within the
page
Allocation of Free Frames
Address Mapping in a Paging System
Virtual Memory
• The implementation of paging systems permit true
multiprogramming systems through the use of virtual
memory/demand paging
— Only the program pages that are required for the execution of
the program are actually loaded  hence the “demanded”
concept
— Only a few (of the many potential) pages of any one program
might be in memory at a time
— Many “active” programs can have pages  increasing the
degree of multiprogramming
• With demand paging, it is possible for a program
consisting of more pages than can (ever) fit into
memory to run
— This is presents a virtual memory system that seems unlimited
in size to the programmer
— Significantly simplifies program development
Virtual Memory
• Demand paging allow a program that is larger than the
main memory to be run
• Page table are maintained in virtual memory. One
approach is called inverted page table (entries are
arranged according to frame number and not according
to virtual page number in inverted page table)
• Each virtual memory access causes 2 physical memory
accesses (i) to fetch the Page Table (ii) to fetch desired
data
• To avoid doubling of memory access time, virtual
memory schemes use a special cache for page table
entries called Translation Lookaside Buffer (TLB).
• TLB contains those page entries that have been recently
used.
Page Table Structure
Segmentation
• In addition to partitioning and paging, segmentation can
be used to subdivide memory
— Programmer subdivides the program into logical units called
segments
 Programs subdivided by function
 Data array items grouped together as a unit
— Advantages
 Simplifies data structure handling
 Allows programs to be altered and recompiled independently
 Supports sharing among processes
 Provides a degree of protection
— Memory reference is in the form of a segment ID and an offset
— Can be combined with paging in a segment ID, page number,
page offset scheme
Pentium II Memory Management
• Hardware for segmentation and paging
• Unsegmented unpaged
— virtual address = physical address
— Low complexity
— High performance
• Unsegmented paged
— Memory viewed as paged linear address space
— Protection and management via paging
— Berkeley UNIX
• Segmented unpaged
— Collection of local address spaces
— Protection to single byte level
— Translation table needed is on chip when segment is in memory
• Segmented paged
— Segmentation used to define logical memory partitions subject to
access control
— Paging manages allocation of memory within partitions
— Unix System V
Pentium II Segmentation
• Each virtual address is 16-bit segment and 32bit offset
• 2 bits of segment are protection mechanism
• 14 bits specify segment
• Unsegmented virtual memory 232 = 4Gbytes
• Segmented 246=64 terabytes
—Can be larger – depends on which process is active
—Half (8K segments of 4Gbytes) is global
—Half is local and distinct for each process
Pentium II Protection
• Protection bits give 4 levels of privilege
—0 most protected, 3 least
—Use of levels software dependent
—Usually level 3 for applications, level 1 for O/S and
level 0 for kernel (level 2 not used)
—Level 2 may be used for apps that have internal
security e.g. database
—Some instructions only work in level 0
Pentium II Paging
• Segmentation may be disabled
—In which case linear address space is used
• Two level page table lookup
—First, page directory
– 1024 entries max
– Splits 4G linear memory into 1024 page groups of 4Mbyte
– Each page table has 1024 entries corresponding to 4Kbyte
pages
– Can use one page directory for all processes, one per
process or mixture
– Page directory for current process always in memory
—Use TLB holding 32 page table entries
—Two page sizes available 4k or 4M
PowerPC Memory Management
Hardware
• 32 bit – paging with simple segmentation
—64 bit paging with more powerful segmentation
• Or, both do block address translation
—Map 4 large blocks of instructions & 4 of memory to
bypass paging
—e.g. OS tables or graphics frame buffers
• 32 bit effective address
—12 bit byte selector
– =4kbyte pages
—16 bit page id
– 64k pages per segment
—4 bits indicate one of 16 segment registers
– Segment registers under OS control
Required Reading
• Stallings chapter 8
• Stallings, W. Operating Systems, Internals and
Design Principles, Prentice Hall 1998
• Loads of Web sites on Operating Systems