Question: Task 2 In this task, you will take a closer look at how the heap works and what malloc actually does. Setup Start with a
Task
In this task, you will take a closer look at how the heap works and what malloc actually does.
Setup
Start with a program that does not terminate immediately, for example by having a getchar line at the end. Before that, allocate memory on the heap as in Task MB and print out the memory address you get. When you run the program, use the pmap tool to examine the memory from outside. Start by reading the manual for pmap. As you can see, we need the process ID of the program to use pmap. You can either print this from the program or find it from outside using ps pgrep, top, or similar tools. It can be combined in one line like this:
pmap x $pgrep aout
Submission
For each subtask, submit the code you have written and a written response to what the task asks for.
a
By comparing the memory address that malloc gives you with the corresponding address in pmap, we see that the address malloc gives us is bytes x more than what pmap shows. Find a way to read this memory, either by changing the code or by using a debugger like lldb What is stored here? And what do you think malloc uses this memory for?
Submit heapac heapatxt
b
With pmap x you can see the size of a memory area, but also how much memory is actually in physical memory RSS As you may remember, virtual memory is mapped to physical memory in pages of a fixed size. Try changing the program so that you write something to the area malloc gives you. Can you find out how big a page is with this? And why do we get a page in RSS immediately, even when we havent written anything to memory?
Submit heapbc heapbtxt
c
Use the command free h and vmstat in another terminal to see how much memory you have available. Gradually try to allocate more memory in a single call to malloc. How much can you allocate in one malloc before it fails and malloc returns NULL? Does this correspond to something found in the free h output? Include the free h output and your value in the text file.
Then try multiple calls to malloc with a fixed large value per call that still works. Is there a limit to how much memory you can allocate now? We dont need an exact number, but the reasoning.
Finally, repeat the last step, but this time write something to memory as well. How does the program crash now? do not do this outside of a VM
Submit heapcc heapctxt
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
