Lab3 - Memory
Lab3 - Memory
Lab 3 - Memory
Lab 3: Memory
Overview
Your prior labs have involved understanding how the CPU executes and schedules processes according to
scheduling policies dictated by the operating system. After the CPU, memory is the most important resource
for the OS to manage. This lab introduces you to memory virtualization.
This lab assumes you have read the textbook chapters Mechanism: Address Translation and Segmentation
before coming to lab. If you have not, please do so before proceeding with the lab material.
Create a new directory called L3 in your CMPT 360 Git repository. In that directory, you will add a file
called answers.txt, which will contain the solutions to questions in the lab.
This lab will have you run some simulation scripts, in a manner similar to earlier labs. The script will allow
you to check your work. As a result, you must not simply give the answers to questions posed but include
showing your work. Lastly, the simulator scripts you will download will compute values both in
hexadecimal and decimal; please do your calculations in hex. Doing so is slightly more foreign to us but the
practice is important, and the questions you are to answer will involve "round numbers" in hexadecimal
anyway.
Your answers are to be written in short-answer form, with relevant program output as appropriate. Write
your answers in a manner befitting an upper-level university course: submit complete sentences with
proper punctuation, please. Poor writing style will be penalized. If you are looking for an appropriate tonal
voice for your technical writing, I suggest imitating that of the Linux manual: precise and clear, but not
overly stuffy. If you need assistance with your writing skills, I would encourage you to get in touch with
MacEwan Writing and Learning Services.
Introduction
Earlier in the course we have seen that certain regions of a process' address space are fixed in the
executable's ELF section data. For instance, recall that we have seen how the ELF file contains the location of
memory that the process should start executing from. If every program had a global "view" of the
computer's memory space, this would mean that no two programs could have overlapping code sections.
As a result, the operating system conspires with the computer hardware to present a virtualised address space,
where each running process perceives that it has full, unfettered access to the full address space of the
computer.
Pause and ponder: how does this compare to the notion of the scheduler making each process believe that
https://learn.macewan.ca/webapps/blackboard/execute/content/file?cmd=view&content_id=_3445914_1&course_id=_94552_1&framesetWrapped=true 1/6
10/20/21, 11:16 PM Lab 3 - Memory – CMPT-360-AS01(1) - 2021 Fall - Intro to...
Pause and ponder: how does this compare to the notion of the scheduler making each process believe that
they are the only process running on the system?
As a result, a process with a pointer to some address in memory in fact contains a so-called virtual address:
this address is translated into the actual "offset in RAM" called the physical address by means that we shall
begin to experiment with today.
Recall that with a base-and-bound memory virtualisation strategy, some contiguous region of physical
memory, beginning at some base and running to some _bound_ed number of bytes, comprises the address
space for a given process.
Translating a virtual address to a physical address means adding the base physical address to the virtual
address. For instance, virtual address 42 in a base-and-bound memory region beginning at physical address
0x1000 would be computed in the following way:
PA = base address + VA
PA = 0x1000 + 42
PA = 0x1000 + 0x2a
PA = 0x102a
In this example, 42 is a valid virtual address only if the limit is equal to or greater than 42. Note that the
process will have no idea what its base address or limit is; instead, it thinks it is simply accessing "memory
location 42". The base and limit values -- indeed, anything pertaining to physical memory -- is abstracted
away from the process by the OS and the hardware.
Before proceeding: Pause and ponder the relationship between this style of addressing and computing the
address of some element in a C array.
As with previous labs, we will use a simulator program to concoct various scenarios with different kinds of
memory virtualisation techniques. Part 1 will have you experiment with the base-and-bound style of
dynamic memory relocation, as described in the textbook and lecture.
The first program is called relocation.py : you may acquire it from the OStep textbook authors' Git
repository by running the following command in your shell:
You do not need to check this script in as you will not be modifying it.
As before, running the simulator with the -h flag will explain the different knobs that you can fiddle with:
macdonellc4@students:~> ./relocation.py -h
Usage: relocation.py [options]
Options:
-h, --help show this help message and exit
-s SEED, --seed=SEED the random seed randomvaluesgenerated
Fhition -a ASIZE --asize=ASIZE
https://learn.macewan.ca/webapps/blackboard/execute/content/file?cmd=view&content_id=_3445914_1&course_id=_94552_1&framesetWrapped=true 2/6
10/20/21, 11:16 PM Lab 3 - Memory – CMPT-360-AS01(1) - 2021 Fall - Intro to...
a ASIZE,
asize=ASIZE
address space size (e.g., 16, 64k, 32m, 1g)
Question 2 : Generate 16 virtual addresses with a random seed of 31335, referring to the usage string (-h) if
you are unsure how. What flags did you pass to the simulator? How big does each process' address space
need to be in order to ensure that all of the virtual addresses are valid? Explain.
It is 1988 and the MacEwantosh iifx personal computer has just been released. It is a computer with 512k of
physical memory, and makes use of base-and-bound memory addressing. Each process on the iifx can
address up to 32k of virtual memory.
Question 3: Assume the kernel always requires 64kb of contiguous memory. In that scenario, how many
processes can be concurrently running on this computer before memory is exhausted? Is there any benefit in
terms of the number of processes that can be run if kernel developers optimized the kernel to only require
48k of contiguous memory? Explain why or why not.
Question 4: In this question, consider just a single process again. Generate ten valid virtual addresses given
the hardware specifications of the iifx, varying the random number generator seed as necessary. What flags
and values did you pass to the simulator? Next, report the base register value that your simulation chose.
What is the maximum address that the register could hold, such that the virtual address space still fits into
physical memory in its entirety?
Part 2: Segmentation
Recall that segmentation is a refinement of the base-and-bound memory addressing model, where different
parts of the address space (namely, the heap, the stack, and executable code) are stored in their own logical
segment.
In this part of the lab, we will simulate a segmentation-based memory model. As in part 1, there is a
simulator written by the OStep authors that you may download:
wget https://raw.githubusercontent.com/remzi-arpacidusseau/ostep-homew
ork/master/vm-segmentation/segmentation.py
chmod +x ./segmentation.py
As in part 1, we can inspect the usage string to find out how this program runs:
macdonellc4@students:~> ./segmentation.py -h
Usage: segmentation.py [options]
https://learn.macewan.ca/webapps/blackboard/execute/content/file?cmd=view&content_id=_3445914_1&course_id=_94552_1&framesetWrapped=true 3/6
10/20/21, 11:16 PM Lab 3 - Memory – CMPT-360-AS01(1) - 2021 Fall - Intro to...
Options:
-h, --help show this help message and exit
-s SEED, --seed=SEED the random seed
-A ADDRESSES, --addresses=ADDRESSES
a set of comma-separated pages to access; -1 m
eans
randomly generate
-a ASIZE, --asize=ASIZE
address space size (e.g., 16, 64k, 32m, 1g)
-p PSIZE, --physmem=PSIZE
physical memory size (e.g., 16, 64k, 32m, 1g)
-n NUM, --numaddrs=NUM
number of virtual addresses to generate
-b BASE0, --b0=BASE0 value of segment 0 base register
-l LEN0, --l0=LEN0 value of segment 0 limit register
-B BASE1, --b1=BASE1 value of segment 1 base register
-L LEN1, --l1=LEN1 value of segment 1 limit register
-c compute answers for me
macdonellc4@students:~>
(the following text is adapted from the segmentation.py readme by Remzi Arpaci-Dusseau.)
The MacEwantosh iici is the next-generation personal computer with state-of-the-art (for 1991!) virtual
memory: it, unlike its predecessor, uses segmentation to virtualise memory. A process running on the iici
has the following characteristics: each process has 256k of virtual address space that the OS will manage out
of 4 whole megabytes of physical memory.
This program allows you to see how address translations are performed in a system with segmentation. The
segmentation that this system uses is pretty simple: an address space has just two segments; further, the top
bit of the virtual address generated by the process determines which segment the address is in: 0 for
segment 0 (where, say, code and the heap would reside) and 1 for segment 1 (where the stack lives).
Segment 0 grows in a positive direction (towards higher addresses), whereas segment 1 grows in the
negative direction.
Ft
|-------------|
| |
Sk | seg1 |
|-------------| virtual address max (size of address space) 256K
With segmentation, as you might recall, there is a base/limit pair of registers per segment. Thus, in this
problem, there are two base/limit pairs. The segment-0 base tells which physical address the top of segment
0 has been placed in physical memory and the limit tells how big the segment is; the segment-1 base tells
where the bottom of segment 1 has been placed in physical memory and the corresponding limit also tells
h bi th ti ( h f it i th ti di ti )
https://learn.macewan.ca/webapps/blackboard/execute/content/file?cmd=view&content_id=_3445914_1&course_id=_94552_1&framesetWrapped=true 4/6
10/20/21, 11:16 PM Lab 3 - Memory – CMPT-360-AS01(1) - 2021 Fall - Intro to...
us how big the segment is (or how far it grows in the negative direction).
On the iici home computer, The seg0 segment starts at the top of the virtual address space and is 64k in
size; seg1 begins at the end of the virtual address space and is 8k in size.
(This follows a common trend: generally-speaking, less memory is allocated for a process' stack than for its
heap.)
Question 5: Assume that the next-generation kernel now takes up a constant 64k of space in the iici's
physical memory. How many processes could fit in total in the amount of supplied physical memory before
RAM is exhausted?
The final two questions only consider one process on this system.
Question 6: What is the highest legal virtual address in segment 0? What about the lowest legal virtual
address in segment 1? (Note the -A flag that the simulator provides for you to check your work. Mind your
off-by-ones!)
Question 7: Report the command-line invocation that will simulate one process on the iici. Then, as in
question 1, with random number generator seed 31335, generate the default (5) number of virtual addressed
and determine which will fault and which will not, showing your work.
You will likely notice that the odds of an arbitrary address in this address space faulting is higher than in
part 1. This more closely mimics the behaviour you will see on a real system: even if you are computing
large amounts of data, in a 64-bit or 48-bit virtual address space a very small fraction of your virtual address
space will map to physical memory!
https://learn.macewan.ca/webapps/blackboard/execute/content/file?cmd=view&content_id=_3445914_1&course_id=_94552_1&framesetWrapped=true 5/6
10/20/21, 11:16 PM Lab 3 - Memory – CMPT-360-AS01(1) - 2021 Fall - Intro to...
https://learn.macewan.ca/webapps/blackboard/execute/content/file?cmd=view&content_id=_3445914_1&course_id=_94552_1&framesetWrapped=true 6/6
macD conf tests in as folder
test2
testempty
13 at file
Amptempt360memoryLab contains relocation
py and segmentation.py
Illustrating Segmentation
000 blewecanletitgrow
code wedonotallocate512
heap segment 0 away
right
I
limit
may it'saregisterinmmu
so
I mmudeterminesifthere's
gym
200 initimgf
i
segment
offset segment forlimittogrowIfso
space
stack site base
register m
h
virtualAddress
am
forsegment1
Ep Let's say welookatvirtual of950andhave astacklimitof200then
address
segmentsize
virtual
golessthan824 1024200
addresscan't
that
note
fr
not
Max VA cozy950 74 a stack
limit
1200
anysegment
q
incode youhaveto VAis I
subtract where
from whenlooking atlimits haveto determine if thesegment toeor regrowsanddetermine
you
thesegmentstarts thestacklimitfromthat
206 J 100100101
uit
E
00001011 I segl
egg ÉÉÉÉÉ
He to
VA 001 1110 I Sego
Ej
322 1100100011 Ik
00100010
Jsegl
VA
y
00and ol belong to Segal
10and 11 belongtoSegI