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 (10 points): Process Virtual Memory Addresses
1. 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
0xDEADBEEF 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.
2. 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 57 after you return from the function call, when that
function is called again, it will still be 57.
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 =10;
int x =10; // 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[1000]; // in c++
int* p =(int *) malloc(1000*sizeof(int));// 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
3. 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 hw8.c from
Canvas under HW8):
b. Compile and run the program.
$gcc hw8.c -o hw8
$./hw8
$./hw8>output.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 (./hw8> 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 proc1 and proc2
Fill in the following template (you can download the template file
MemoryDiagram.txt from Canvas under HW8):
d. Please answer the following 2 questions.
1) As variables are added to the stack, do the addresses get smaller or larger?
2) Do variables stored on the stack ever have the same address as other
variables? Why or why not?
Part II Programming Question ( 1 0 points ) :

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!