0% found this document useful (0 votes)
217 views34 pages

Unit 4 - Run - Time Environment

The document discusses run-time environments and issues related to memory management during program execution. It covers topics like storage allocation, access to variables, linking between procedures, passing parameters, interface to operating systems, input/output devices, memory management techniques like stack allocation, heap management, and garbage collection. Examples of activation records, activation trees, and calling sequences are provided to illustrate how run-time environments manage memory and procedure calls.

Uploaded by

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

Unit 4 - Run - Time Environment

The document discusses run-time environments and issues related to memory management during program execution. It covers topics like storage allocation, access to variables, linking between procedures, passing parameters, interface to operating systems, input/output devices, memory management techniques like stack allocation, heap management, and garbage collection. Examples of activation records, activation trees, and calling sequences are provided to illustrate how run-time environments manage memory and procedure calls.

Uploaded by

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

Dr. K.

Velmurugan
Run-time environment
Creates and manages a run-time
environment to execute programs in target
machine

Ex: JRE
issues
storage allocation
access to variables and data
Linkages between procedures
Passing parameters
Interface to OS
Input/output devices
issues

Memory management
Stack allocation
Heap management
Garbage collection
Storage Organization

Padding
Static vs. Dynamic Allocation
Static: Compile time,
Dynamic: Runtime allocation
Many compilers use some combination of following
Stack storage: for local variables, parameters and so on
Heap storage: Data that may outlive the call to the
procedure that created it
Stack allocation is a valid allocation for procedures
since procedure calls are nested
Activation tree
Each node corresponds to one activation
Root is the activation of main procedure

Procedure p calls q
Three cases
Sketch of a quicksort program
Activation for Quicksort
Activation tree representing calls during
an execution of quicksort
Suppose that the program of Fig.7.2 uses a partition
function that always picks a[m] as the separator v. Also,
when the array a[m], , a[n] is reordered, assume that
the order is preserved as much as possible. That is,
first come all the elements less than v, in their original
order, then all elements equal to v, and finally all
elements greater than v, in their original order.
Draw the activation tree when the numbers
9,8,7,6,5,4,3,2,1 are sorted.
What is the largest number of activation records that
ever appear together on the stack?
Repeat Exercise 7.2.1 when the initial order of the
numbers is 1,3,5,7,9,2,4,6,8
In Fig. 7.9 is C code to compute Fibonacci numbers recursively.
Suppose that the activation record for f includes the following
elements in order: (return value, argument n, local s, local t); there will
normally be other elements in the activation record as well. The
questions below assume that the initial call is f(5).
int f(int n)
{ int t, s;
if (n < 2) return 1;
s = f(n-1); t = f(n-2);
return s+t; }
Show the complete activation tree.
What dose the stack and its activation records look like the first time
f(1) is about to return?
! What does the stack and its activation records look like the fifth time
f(1) is about to return?
Activation records
Procedure calls and returns are usaully managed by a
run-time stack called the control stack.
Each live activation has an activation record
(sometimes called a frame)
The root of activation tree is at the bottom of the stack
The current execution path specifies the content of the
stack with the last activation has record in the top of
the stack.
In a language that passes parameters by reference,
there is a function f(x, y) that does the following:
x = x + 1; y = y + 2; return x+y; If a is assigned the value
3, and then f(a, a) is called, what is returned?
A General Activation Record
Activation Record
Temporary values
Local data
A saved machine status
An access link
A control link
Space for the return value of the called function
The actual parameters used by the calling procedure
Here is a sketch of two C functions f and g:
int f(int x){int i;...return i+1;...} int g(int y) {int j;...f(j+1). ..} That is,
function g calls f. Draw the top of the stack, starting with the activation
record for g, after g calls f, and f is about to return. You can consider
only return values, parameters, control links, and space for local
variables; you do not have to consider stored state or temporary or local
values not shown in the code sketch. However, you should indicate:
Which function creates the space on the stack for each element?
Which function writes the value of each element?
To which activation record does the element belong?
Downward-growing stack of activation records
Designing Calling Sequences
Values communicated between caller and callee are
generally placed at the beginning of callees activation
record
Fixed-length items: are generally placed at the middle
Items whose size may not be known early enough: are
placed at the end of activation record
We must locate the top-of-stack pointer judiciously: a
common approach is to have it point to the end of
fixed length fields.
Division of tasks between caller and callee
calling sequence
The caller evaluates the actual parameters
The caller stores a return address and the old value of
top-sp into the callee's activation record.
The callee saves the register values and other status
information.
The callee initializes its local data and begins
execution.
corresponding return sequence
The callee places the return value next to the
parameters
Using information in the machine-status field, the
callee restores top-sp and other registers, and then
branches to the return address that the caller placed in
the status field.
Although top-sp has been decremented, the caller
knows where the return value is, relative to the current
value of top-sp; the caller therefore may use that value.
Access to dynamically allocated arrays
Memory Manager
Two basic functions:
Allocation
Deallocation
Properties of memory managers:
Space efficiency
Program efficiency
Low overhead
Typical Memory Hierarchy Configurations
Locality in Programs
The conventional wisdom is that programs spend 90% of
their time executing 10% of the code:
Programs often contain many instructions that are
never executed.
Only a small fraction of the code that could be invoked
is actually executed in a typical run of the program.
The typical program spends most of its time executing
innermost loops and tight recursive cycles in a
program.
Best-fit and next-fit object
placement
First-fit
Best-fit
Binning
Next-fit
Garbage collection
Programming is easier if the run-time system
garbage-collects --- makes space belonging to
unusable data available for reuse.
.

30
Manual deallocation
Memory-leak error
Dangling pointer reference
Programming conventions
Object ownership
Reference counting
Region-based allocation
Suppose the heap consists of seven chunks, starting at address 0. The
sizes of the chunks, in order, are 80, 30, 60, 50, 70, 20, 40 bytes. When
we place an object in a chunk, we put it at the high end if there is
enough space remaining to form a smaller chunk (so that the smaller
chunk can easily remain on the linked list of free space) . However , we
cannot tolerate chunks of fewer that 8 bytes, so if an object is almost as
large as the selected chunk, we give it the entire chunk and place the
object at the low end of the chunk. If we request space for objects of the
following sizes: 32, 64, 48, 16, in that order, what does the free space list
look like after satisfying the requests, if the method of selecting chunks
is
First fit.
Best fit.
First fit.
48, 32(32), 14, 16(16), 60, 50(48), 70(64), 20, 40

Best fit.
80, 30, 60, 50(48), 70(64), 20(16), 8, 32(32)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy