0% found this document useful (0 votes)
43 views

Exceptional Control Flow

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Exceptional Control Flow

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 45

Exceptional control flow

control flow
• From the time you first apply power to a processor until the
time you shut it off, the program counter assumes a
sequence of values
a0, a1, . . . , an−1
• where each ak is the address of some corresponding
instruction Ik. Each transition from ak to ak+1 is called a
control transfer.
• A sequence of such control transfers is called the flow of
control, or control flow of the processor.
• The simplest kind of control flow is a “smooth” sequence
where each Ik and Ik+1 are adjacent in memory.
Control Flow
• Computer do only one thing
– From startup to shutdown, a CPU simply reads and executes
(interprets) a sequence of instructions, one at a time
– This sequence is the system’s physical control flow (or flow of
control)
Altering the Control Flow
• Up to Now: two mechanisms for changing control flow:
– „ Jumps and branches
– „ Call and return using the stack discipline.
• Such instructions are necessary mechanisms that allow programs
to react to changes in internal program state represented by
program variables.
• But systems must also be able to react to changes in system state
that are not captured by internal program variables and are not
necessarily related to the execution of the program.
• example
– data arrives from a disk or a network adapter.
– Instruction divides by zero
– User hits ctl-c at the keyboard
– System timer expires ….
Exceptional control flow
• Modern systems react to these situations by making abrupt
changes in the control flow.
• In general, we refer to these abrupt changes as exceptional
control flow (ECF).
• Exceptional control flow occurs at all levels of computer system.
• For example:
• At the hardware level, events detected by the hardware trigger abrupt
control transfers to exception handlers.
• At the operating systems level, the kernel transfers control from one user
process to another via context switches.
• At the application level, a process can send a signal to another process
that abruptly transfers control to a signal handler in the recipient.
• An individual program can react to errors by sidestepping the usual stack
discipline and making nonlocal jumps to arbitrary locations in other
functions.
Exceptional control flow …
• As programmers, there are a number of reasons why it is
important for you to understand ECF:
• understand important systems concepts. ECF is the basic
mechanism that operating systems use to implement I/O,
processes, and virtual memory.
• understand how applications interact with the operating
system. Applications request services from the operating
system by using a form of ECF known as a trap or system
call.
• understand concurrency. ECF is a basic mechanism for
implementing concurrency in computer systems.
• understand how software exceptions work. Languages
such as C++ and Java provide software exception
mechanisms via try, catch, and throw statements.
Exceptions
• Exceptions are a form of exceptional control flow that are
implemented partly by the hardware and partly by the operating
system.
• An exception is a transfer of control to the OS in response to
some event (i.e., change in processor state)
Exception Handling
• when the processor detects that the event has occurred,
it makes an indirect procedure call (the exception),
through a jump table called an exception table, to an
operating system subroutine (the exception handler) that
is specifically designed to process this particular kind of
event.
• When the exception handler finishes processing, one of
three things happens, depending on the type of event
that caused the exception:
1. The handler returns control to the current instruction Icurr,
the instruction that was executing when the event occurred.
2. The handler returns control to Inext,, the instruction that
would have executed next had the exception not occurred.
3. The handler aborts the interrupted program.
Interrupt Vectors
• It is a data structure that associates a list of interrupt
handlers with a list of interrupt requests
Classes of Exceptions
• Exceptions can be divided into four classes:
interrupts, traps, faults, and aborts.
Asynchrones Exceptions (Interrupts)
• Caused by events external to the processor
– Indicated by setting the processor’s interrupt pin
– „ handler returns to “next” instruction.
• Examples:
– „ I/O interrupts
• hitting ctl-c at the keyboard
• arrival of a packet from a network
• arrival of a data sector from a disk
– „ Hard reset interrupt
• hitting the reset button
– „ Soft reset interrupt
• hitting ctl-alt-delete on a PC
Synchronous Exceptions
• Caused by events that occur as a result of executing an
instruction:
– Traps
• Intentional
• Examples: system calls, breakpoint traps, special instructions
• Returns control to “next” instruction
– Faults
• Unintentional but possibly recoverable
• Examples: page faults (recoverable).
• Either re-executes faulting (“current”) instruction or aborts.
– Aborts
• unintentional and unrecoverable
• Examples: parity error, machine check.
• Aborts current program
Process
• A process is an instance of a running program.
– Not the same as “program” or “processor”
• Process provides each program with two key
abstractions:
– Logical control flow
• Each program seems to have exclusive use of the CPU.
– Private address space
• Each program seems to have exclusive use of main memory.
• How are these Illusions maintained?
– Process executions interleaved (multitasking)
– Address spaces managed by virtual memory system
Logical Control Flows
• Each process has its own logical control
flow
Concurrent Processes
• Control flows for concurrent processes are physically
disjoint in time.
• However, we can think of concurrent processes are
running in parallel with each other.
Context Switching
• Processes are managed by a shared chunk of OS code called
the kernel
• „Important: the kernel is not a separate process, but rather
runs as part of some user process
• Control flow passes from one process to another via a
context switch.
Summarizing
• Exceptions
– Events that require nonstandard control flow
– Generated externally (interrupts) or internally (traps and faults)
• Processes
– At any given time, system has multiple active processes
– Only one can execute at a time, though
– Each process appears to have total control of processor + private
memory space
Virtual Memory
register

CPU cache

• larger capacity
Main Memory • lower speed
• lower cost

Secondary Storage

Server (or INTERNET)


What is the problem ?
• Now a days a single computer run multiple
programs at a time.
• Main memory might not able to store all the
data in each program.
• The computer will lose performance and gate
stacked.
Solution?
• One of the solution is to use Virtual memory.
• Virtual memory as an alternate set of memory
addresses.
• Programs use these virtual addresses rather
than real addresses to store instructions and
data.
• When the program is actually executed, the
virtual addresses are converted into real
memory addresses.
Objective
• When a computer is executing many programs at the
same time, Virtual memory make the computer to
share memory efficiently.

• Eliminate a restriction that a computer works in


memory which is small and be limited.

• When many programs is running at the same time,


by distributing each suitable memory area to each
program,
– VM protect programs to interfere each other in each
memory area.
Virtual Memory Terminology
Execution of a process
• Operating system brings into main memory a
few pieces of the program
• Resident set
• portion of process that is in main memory
• An interrupt is generated when an address is
needed that is not in main memory
• Operating system places the process in a
blocking state
Execution of a process
• Piece of process that contains the logical address
is brought into main memory
• operating system issues a disk I/O Read request
• another process is dispatched to run while the
disk I/O takes place
• an interrupt is issued when disk I/O is
complete, which causes the operating system
to place the affected process in the Ready state
Implications
• More processes may be maintained in main
memory
• only load in some of the pieces of each
process
• with so many processes in main memory, it
is very likely a process will be in the Ready
state at any particular time
• A process may be larger than all of main
memory
Real and Virtual Memory
How does it work…
• To facilitate copying virtual memory into real
memory,
– the operating system divides virtual memory into pages,
each of which contains a fixed number of addresses.
• Each page is stored on a disk until it is needed.
• When the page is needed,
– the operating system copies it from disk to main memory,
translating the virtual addresses into real addresses.
Cont. …
• Divide physical memory into fixed-sized blocks called frames (size is power
of 2, between 512 bytes and 8192 bytes).
• Divide logical memory into blocks of same size called pages.
• To run a program of size n pages, need to find n free frames and load
program.
• Set up a page table to translate logical to physical addresses.
• Address generated by CPU is divided into:
– Page number (p) - used as an index into a page table which contains
base address of each page in physical memory.
– Page offset (d) - combined with base address to define the physical
memory address that is sent to the memory unit.
MMU (Memory Management Unit)
• A memory management unit (MMU) is a computer hardware component that
handles all memory and catching operations associated with the processor.
• In other words, the MMU is responsible for all aspects of memory
management. It is usually integrated into the processor, although in some
systems it occupies a separate IC (integrated circuit) chip.
• The work of the MMU can be divided into three major categories:
• Hardware memory management, which oversees and regulates the
processor's use of RAM (random access memory) and cache memory.
• OS (operating system) memory management, which ensures the
availability of adequate memory resources for the objects and data
structures of each running programs at all times.
• Application memory management, which allocates each individual
program's required memory, and then recycles freed-up memory space
when the operation concludes.
MMU (Memory Management Unit)
• Memory management in the operating system is
an essential functionality, which allows the
allocation of memory to the processes for
execution and de allocates the memory when the
process is no longer needed.
• The two memory management schemes are
paging and segmentation.
• The basic difference between paging and
segmentation is that, “page” is a fixed-sized block
whereas, a “segment” is a variable-sized block.
Characteristics of Paging and Segmentation
Thrashing
Principle of Locality
• Program and data references within a process
tend to cluster
• Only a few pieces of a process will be needed
over a short period of time
• Therefore it is possible to make intelligent
guesses about which pieces will be needed in
the future
• Avoids thrashing
Support Needed for Virtual Memory
• For virtual memory to be practical and
effective:
• hardware must support paging and
segmentation
• operating system must include software for
managing the movement of pages and/or
segments between secondary memory and
main memory
Paging
• The term virtual memory is usually associated
with systems that employ paging
• Use of paging to achieve virtual memory was
first reported for the Atlas computer
• Each process has its own page table
• each page table entry contains the frame
number of the corresponding page in main
memory
Typical Memory Management Formats
Address Translation in a Paging System
Segmentation
• Segments are contiguous units, and are referred
to in programs by their segment number and an
offset to the requested data.
• Segmentation involves the relocation of variable
sized segments into the physical address space.
• Efficient segmentation relies on programs that are
very thoughtfully written for their target system.
• Since segmentation relies on memory that is
located in single large blocks, it is very possible
that enough free space is available to load a new
module, but can not be utilized.
Paging
• Paging provides a somewhat easier interface for
programs, in that its operation tends to be more
automatic and thus transparent.
• Each unit of transfer, referred to as a page, is of a
fixed size and swapped by the virtual memory
manager outside of the program’s control.
• Instead of utilizing a segment/offset addressing
approach, as seen in segmentation,
– paging uses a linear sequence of virtual addresses which
are mapped to physical memory as necessary.
• Due to this addressing approach, a single program
may refer to series of many non-contiguous
segments.
Paging. …
• A technique used by virtual memory operating
systems to help ensure that the data you need
is available as quickly as possible.
• The operating system copies a certain number
of pages from your storage device to main
memory.
• When a program needs a page that is not in
main memory, the operating system copies
the required page into memory and copies
another page back to the disk.
Page fault
• An interrupt to the software raised by the
hardware when a program accesses a page
that is not mapped in physical memory.

• when a program accesses a memory location


in its memory and the page corresponding to
that memory is not loaded
• when a program accesses a memory location
in its memory and the program does not have
privileges to access the page corresponding to
that memory.
Operation of
Paging and
Translation Look
aside Buffer (TLB)
Paging replacement algorithms
• Optimal Page Replacement : eliminate the
page that is not expected to be used.
• FIFO(first input/first output) : rather than
choosing the victim page at random, the oldest
page is the first to be removed.
• LRU(Least Recently used) : move out the page
that is the least rarely used.
• LFU(Least Frequently used) : move out the
page that is not used often in the past.
Summary
• Virtual memory is a common part of most operating systems
on computers.
• It has become so common because it provides a big benefit
for users at a very low cost.
• benefits of executing a program that is only partially in
memory.
• program is no longer constrained by the amount of physical
memory.
⇒ user would be able to write programs for an
extremely large virtual address space.
• more programs could be run at the same time
⇒ increase CPU utilization and throughput.
• less I/O would be needed to load or swap each user program
⇒ run faster
Thank you

You might also like