Question: Part II Programming Question ( 1 0 points ) : Process Virtual Memory Addresses 1 . Preamble Every Linux process has its own dedicated memory
Part II Programming Question points: Process Virtual Memory Addresses
Preamble
Every Linux process has its own dedicated memory address space. The kernel
maps these "virtual" addresses in the process address space to actual physical
memory addresses as necessary. In other words, the data stored at address
xDEADBEEF in one process is not necessarily the same as the data stored at the
same address in another process. When an attempt is made to access the data, the
kernel translates the virtual address into an appropriate physical address to retrieve
the data from memory. We are not really concerned with physical addresses.
Rather, we are investigating exactly how the operating system allocates more
virtual addresses to a process.
Three Segments of User Space
The user context of a process is made up of the portions of the address space
that are accessible to the process while it is running in user mode.
There are a few definitions that come in handy when we are talking about
memory:
global variable one declared outside of any function.
local static variable one declared inside a function with the static
keyword. Such a variable keeps its value across function calls. For instance,
if it has the value after you return from the function call, when that
function is called again, it will still be
local automatic variable any variable declared inside a function without
the static keyword.
heapis dynamically allocated space.
o In C you use malloc or calloc
o In C you use new
The portions of address space are: text, data, and stack. They are described in
further detail below:
Text
o sometimes called the instruction or code segment
o contains executable program code and constant data
o is read only
o multiple processes can share this segment if a second copy of the
program is to be executed concurrently
Data
o contiguous in a virtual sense with the text segment
o subdivided in to three sections:
initialized data static or global variables
Example: static int a ;
int x ; outside of any functions
uninitialized data static or global variables
Example: static int b;
int y; outside of any functions
heap dynamically allocated
Example: int p new int; in c
int p int mallocsizeofint; in c
o addresses increase as the heap grows
Stack
o used for function call information including:
address to return to
the values or addresses of all parameters
local automatic variables
o addresses decrease as the stack grows
Notice that the stack grows towards the uninitialized data and the heap grows
towards the stack.
Memory references to Text, Data and Stack in a user space program are done
with virtual addresses. The kernel translates these virtual addresses to physical
memory addresses.
In working with these virtual addresses, you have access to three external
variables:
etextfirst valid address above the text segment
edatafirst valid address above initialized data segment
endfirst valid address above the uninitialized data segment
Programming Task
The purpose of this assignment is to explore the virtual memory associated
with the user process: text, data, and stack.
a Copy and paste the following source code, ensure that you understand variables,
functions defined in the file. you also can download the source file hwc from
Canvas under HW:
b Compile and run the program.
$gcc hwc o hw
$hw
$hwoutput.txt
The hexadecimal and decimal addresses are output from the program including:
o etext
o edata
o end
o functions
o global variables
o static local variables
o heap variables
You may want to use a redirect hw output.txt to save the output
for later.
c Your task is to fill in addresses of the variables, and functions, including:
o instructions in machine code
o global initialized variables
o static initialized variables
o global uninitialized variables
o static uninitialized variables
o dynamically allocated variables
o local automatic variables from main
o local automatic variables from each of proc and proc
Fill in the following template you can download the template file
MemoryDiagram.txt from Canvas under HW:
d Please answer the following questions.
As variables are added to the stack, do the addresses get smaller or larger?
Do variables stored on the stack ever have the same address as other
variables? Why or why not?
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
