You are on page 1of 3

National University of Singapore

School of Computing
CS2106 Laboratory 8 Semester 1 10/11

Deadline
31 October, 2010, Sunday, 11:59pm.

Platform
This exercise MUST be done on Linux PCs in the OS lab.

Submission
Download the template for your solution:

wget http://www.comp.nus.edu.sg/~cs2106/lab08-A000000X.txt

Rename the file by replacing the string A000000X with your matriculation number.
Enter your answer into the text file and submit the file into IVLE Workbin (a folder named
Lab 8) before the deadline.

Marking Scheme
This lab exercise is worth 10 marks.
0.1 marks will be deducted per minute after the deadline for late submission, and 3 marks
will be deducted for file naming violation or format violation.
It might be useful to review your Lab 3 before you start this exercise.

1. (5 marks) In this question, we are going to see how malloc() interact with brk() and
mmap().
Recall that there are two different ways a process can request for more memory: (i) by
using brk(), which increases the size of data segment, and (ii) by anonymous memory map,
using mmap().
When the existing allocated memory is insufficient, malloc() requests for more memory,
using brk() when the allocation unit is small, and mmap(), when the allocation unit is large.
Thus, there is a threshold τ in the malloc() routine that determines when to use brk()
and when to use mmap(). We will find out what τ is in this exercise.
Download, compile, and run the following program: http://www.comp.nus.edu.sg/~cs2106/
lab08-malloc.c. The program takes in a parameter i, and runs malloc() five times, re-
questing for 2i bytes each time.
(a) (1 point) Run the program using strace -f with parameter i = 1, and observe the
output.
What is the system call that maps the shared library libc.so into the address space?
Is the shared library being mapped as private or shared? Is the mapped memory region
writable?
(b) (1 point) Now identify the system call that increases the size of the data segment from
the output above. Write down the sytem call and its parameter (or parameters) in
your answer.
(c) (1 point) By how much has the data segment size increase as a result of this call?
Now, run the program with strace again, varying i from 2 to 20. Observe the system
calls used to request for memory.
Note down how much memory has been requested in total by either brk() or mmap()
as a result of malloc() (in KB) for different values of i.
(d) (1 point) For what value (or values) of i do you see more than one call to brk() to
increase data segment size? (Note: brk(0) does not increase data segment size.)
Based on your observation, what is the maximum increment in data segment size
requested by malloc() for every call to brk()?
(e) (1 point) For what values of i do you see mmap() being used to request for memory in
the address space? What is the value of τ ?

Page 2
2. (5 marks) The next exercise helps us estimate the overhead of page faults in our system.
Download and read two slightly different programs from the URLs below:

• http://www.comp.nus.edu.sg/~cs2106/lab08-time1.c
• http://www.comp.nus.edu.sg/~cs2106/lab08-time2.c

The given programs take in a single parameter, which is the number of pages to malloc().
Each page is 4KB1 .
lab08-time1.c goes through each page and touches each page once by modifying the
content of one memory location in each page. lab08-time2.c goes through 1/4 of the
pages and touches each page four times, by modifying the content of four memory locations
within a page each time. Both programs make the same number of modifications.
The average time (in µs) to modify the content of a memory location is printed as the
output.
(a) (1 point) A minor page fault is a page fault that does not result in reading of a page
from disk; while a major page fault is a page fault that does.
Which type of page faults (minor or major) would be the majority type of page faults
caused by running both programs? Explain.
(b) (1 point) Estimate the number of page faults that would occur for both programs if
they are executed with command line argument N , where N is a large number.
(c) (1 point) Now run both programs with some large values of N as input. Pick several
values with different order of magnitude (1000, 10000 etc.). Note down your output.
Note that you should pick an N that is large enough so that the timing and looping
overhead is negligible. One way to verify this is to make a copy of lab01-time1.c and
comment out the line inside the for loop (but not the for loop itself). Now you have a
program that measures the timing and looping overhead.
If N is too big, malloc() will fail.
(d) (2 points) Estimate the overhead of a page fault (of the type you identified in Part
(a)) in your system. Show your workings.

3. (0 marks) Did you notice that running the same program with the same argument would
result in different regions of memory allocated almost every time? Why? (Hint: ASLR)

THE END
1
you can verify this by running the command getconf PAGESIZE

Page 3

You might also like