1
00:00:00,001 --> 00:00:10,440
Let's get into how Virtual Memory Systems 
implement all these nice features for our 

2
00:00:10,440 --> 00:00:17,333
processes. 
let's review the problems that virtual 

3
00:00:17,333 --> 00:00:23,250
memory is trying to address. 
First problem is we have to fit a huge 

4
00:00:23,250 --> 00:00:27,209
memory address base into a tiny physical 
memory. 

5
00:00:27,209 --> 00:00:32,009
again physical memory does not have 16 
exabytes of space, typically it's just a 

6
00:00:32,009 --> 00:00:36,550
few gigabytes, so how do we think about 
that? 

7
00:00:36,550 --> 00:00:40,540
then how do we manage the memory spaces 
for multiple processes, make the problem 

8
00:00:40,540 --> 00:00:44,497
even harder. 
Now it's not just 16 exabytes but it's 16 

9
00:00:44,497 --> 00:00:50,070
exabytes for each process. 
and then how do we protect processes from 

10
00:00:50,070 --> 00:00:54,820
stepping on each other's each other's 
memory. 

11
00:00:54,820 --> 00:00:58,460
At the same time, we want to allow 
processes to share common parts of memory 

12
00:00:58,460 --> 00:01:01,932
so that we can be more efficient with 
this little physical memory that we do 

13
00:01:01,932 --> 00:01:06,130
have. 
So how would you go about solving these 

14
00:01:06,130 --> 00:01:09,136
problems? 
it turns out there's one there's one 

15
00:01:09,136 --> 00:01:13,223
important technique that comes up over 
and over again in computer science, that 

16
00:01:13,223 --> 00:01:20,480
can help us with all of these. 
And that technique is called indirection. 

17
00:01:20,480 --> 00:01:22,779
The quote you see at the top of this 
slide. 

18
00:01:22,779 --> 00:01:26,118
any problem in computer science can be 
solved by adding another level of 

19
00:01:26,118 --> 00:01:29,728
indirection is a common thing you'll hear 
people say. 

20
00:01:29,728 --> 00:01:35,870
Sometimes jokingly, but really seriously, 
that almost anything can be addressed by 

21
00:01:35,870 --> 00:01:41,170
this particular technique. 
And it's an important technique for 

22
00:01:41,170 --> 00:01:47,310
referring to objects, in in memory in and 
how to point to them. 

23
00:01:47,310 --> 00:01:52,700
let's take a look at what we mean by, 
pointing to an object. 

24
00:01:52,700 --> 00:01:56,604
So if I have a name for something like, 
lets say I've, I've stored a pointer in a 

25
00:01:56,604 --> 00:02:01,500
variable. 
a pointer type. 

26
00:02:01,500 --> 00:02:03,676
I can have it to point to something in 
memory. 

27
00:02:03,676 --> 00:02:09,001
Okay, and without indirection, I have a 
direct link, in that namely this variable 

28
00:02:09,001 --> 00:02:15,736
stores a particular address that points 
directly to that memory location. 

29
00:02:15,736 --> 00:02:20,818
That works great until I decide that I 
want to move this thing in memory and put 

30
00:02:20,818 --> 00:02:25,945
it somewhere else. 
And, if I want to do that, I have a 

31
00:02:25,945 --> 00:02:29,780
problem in that I have to go find every 
single variable that might have pointed 

32
00:02:29,780 --> 00:02:34,054
to that object. 
And changed the address stored at that 

33
00:02:34,054 --> 00:02:37,270
variable. 
Well, that can be a very burdensome and 

34
00:02:37,270 --> 00:02:42,290
hard to keep track of. 
So, what indirection does. 

35
00:02:42,290 --> 00:02:45,371
Is it says you know what? 
Instead of having a direct link between 

36
00:02:45,371 --> 00:02:49,700
the two. 
Let's add something in the middle. 

37
00:02:49,700 --> 00:02:55,360
That will serve kind of as a directory. 
some, a look up table if you will. 

38
00:02:55,360 --> 00:02:59,930
So that when I have a particular pointer 
to an object. 

39
00:02:59,930 --> 00:03:04,090
I won't have the direct pointer to it, 
I'll have an indirect point to it. 

40
00:03:04,090 --> 00:03:07,654
That namely goes into this directory and 
says you know, the third thing I was 

41
00:03:07,654 --> 00:03:11,748
referring to. 
So that now I will look it up in that 

42
00:03:11,748 --> 00:03:16,810
directory in the third slot for example, 
and find the address of the thing. 

43
00:03:16,810 --> 00:03:20,708
Now that involves an extra step. 
I have to first go to this place in 

44
00:03:20,708 --> 00:03:23,977
memory. 
Find the directory, look up the third 

45
00:03:23,977 --> 00:03:27,511
element of of the directory, and then 
I'll get the address in memory that I, 

46
00:03:27,511 --> 00:03:31,892
I'm, I really want. 
So it's a little bit of extra cost 

47
00:03:31,892 --> 00:03:35,010
because I have to do that second memory 
access. 

48
00:03:36,090 --> 00:03:39,628
However, if I change the position of the 
thing or I just want to point to 

49
00:03:39,628 --> 00:03:44,268
something else using that same original 
variable name. 

50
00:03:44,268 --> 00:03:47,867
Well then that's really easy to do, 
because all I have to do is change the 

51
00:03:47,867 --> 00:03:54,160
directory to point to point here now. 
And not have to worry about what 

52
00:03:54,160 --> 00:03:58,970
variables might I have to go back and 
change. 

53
00:03:58,970 --> 00:04:03,290
Anything that referred to the third slot 
in that memory is in our as in our 

54
00:04:03,290 --> 00:04:09,382
example, can now just point to that. 
Because I'll find a new address in that 

55
00:04:09,382 --> 00:04:13,310
third slot in my lookup table, in my 
directory. 

56
00:04:13,310 --> 00:04:17,836
Alright, so this is how indirection 
works, and there's lots and lots of 

57
00:04:17,836 --> 00:04:24,830
examples of this that rely on this 
ability to have a flexible mapping. 

58
00:04:24,830 --> 00:04:30,720
Between a variable or that stores a 
pointer and the actual object in memory. 

59
00:04:30,720 --> 00:04:33,955
So that I can easily switch between what 
things I'm pointing to. 

60
00:04:33,955 --> 00:04:38,575
and examples of these include things like 
the domain name service that helps you 

61
00:04:38,575 --> 00:04:44,250
find the actual machines that hosts your 
websites on the web. 

62
00:04:44,250 --> 00:04:49,390
the phone system. 
things that allow cell phone portability. 

63
00:04:49,390 --> 00:04:53,710
Or find your cell phone depending on 
which cell tower you happen to be near. 

64
00:04:53,710 --> 00:04:57,890
mail forwarding at the post office is an 
example of indirection. 

65
00:04:57,890 --> 00:05:04,660
lots and lots of things that we use in 
our daily lives. 

66
00:05:04,660 --> 00:05:08,756
rely on this ability to go and look up 
things in a directory and then as a 

67
00:05:08,756 --> 00:05:13,367
second step, find the actual thing if 
interest. 

68
00:05:13,367 --> 00:05:17,335
Rather than having a direct name for it 
in the first place, and this is going to 

69
00:05:17,335 --> 00:05:21,891
be fundamental. 
to how we build virtual memory. 

70
00:05:21,891 --> 00:05:27,875
namely we're going to add a level of 
indirection to our address mapping to 

71
00:05:27,875 --> 00:05:32,986
physical memory. 
So our processes here are going to be 

72
00:05:32,986 --> 00:05:37,747
generating virtual memory addresses and 
we're going to put a big directory in the 

73
00:05:37,747 --> 00:05:42,879
middle. 
to take that virtual memory address and 

74
00:05:42,879 --> 00:05:48,915
turn it into a physical memory address. 
To find the spot in physical memory where 

75
00:05:48,915 --> 00:05:54,901
we've actually put that thing. 
This is how we're going to give each 

76
00:05:54,901 --> 00:05:59,421
process the illusion of having their own 
physical memory. 

77
00:05:59,421 --> 00:06:04,296
even though there, they are not aware of 
each other and they're generating 

78
00:06:04,296 --> 00:06:09,540
potentially the same address, internally, 
to a process. 

79
00:06:09,540 --> 00:06:14,760
but it's going, potentially be mapped to 
a different place in physical memory. 

80
00:06:14,760 --> 00:06:19,080
This is going to be useful to solve all 
of those problems that we had. 

81
00:06:19,080 --> 00:06:22,315
So let, let's go a little bit more into 
detail on this. 

82
00:06:22,315 --> 00:06:27,290
So let's remind ourselves about the 
address spaces that we have. 

83
00:06:27,290 --> 00:06:31,980
Our virtual address space, what our 
processes generate as address is 2 to the 

84
00:06:31,980 --> 00:06:36,390
n, where n is the number of bits in our 
address. 

85
00:06:36,390 --> 00:06:41,470
In the case of our 64 bit machines that's 
2 to the 64. 

86
00:06:41,470 --> 00:06:46,058
Our, our physical address base is 2 to 
the M, typically where M is much, much 

87
00:06:46,058 --> 00:06:51,511
smaller than N. 
So we might have a physical address base 

88
00:06:51,511 --> 00:06:57,556
that is only, really 34 bits or, 38 bits. 
But not 64. 

89
00:06:57,556 --> 00:07:03,480
64 would be just a huge address space to 
implement physical memory. 

90
00:07:03,480 --> 00:07:07,043
Alright? 
Now our mapping, since we have all of 

91
00:07:07,043 --> 00:07:12,867
these virtual address in our process, is 
we can only fit some of them in the 

92
00:07:12,867 --> 00:07:19,180
actual physical memory. 
Which again is much smaller. 

93
00:07:19,180 --> 00:07:25,366
The rest will have to be stored on disk. 
In other words rather than having those 

94
00:07:25,366 --> 00:07:29,086
values be in physical memory we're 
going to put them on a slower but much 

95
00:07:29,086 --> 00:07:33,660
larger storage device, namely a hard 
drive. 

96
00:07:33,660 --> 00:07:40,860
Or these days a an S, an SD card or a a 
solid state drive. 

97
00:07:42,530 --> 00:07:46,590
But it's not going to be in our fast 
physical, faster physical memory. 

98
00:07:46,590 --> 00:07:51,640
It's going to be on a slower device out 
on a disk. 

99
00:07:51,640 --> 00:07:54,345
Alright? 
So what we'll want to do is as we execute 

100
00:07:54,345 --> 00:07:59,380
our program, we may need memory that is 
stored on disk. 

101
00:07:59,380 --> 00:08:04,105
At that point, we'll want to get it and 
bring it into physical memory so we can 

102
00:08:04,105 --> 00:08:08,370
access it there. 
And we'll see the details of that in a 

103
00:08:08,370 --> 00:08:12,642
little bit. 
So let's just remind ourselves if we were 

104
00:08:12,642 --> 00:08:18,397
just using physical addressing. 
What we would have is our CPU, would 

105
00:08:18,397 --> 00:08:24,587
generate a physical address, it would go 
directly to main memory. 

106
00:08:24,587 --> 00:08:29,830
Find the location in the main memory 
where that address is. 

107
00:08:29,830 --> 00:08:33,616
And get the value there and bring it, 
back into the CPU. 

108
00:08:33,616 --> 00:08:35,948
Right? 
In this case reading four bytes out of 

109
00:08:35,948 --> 00:08:39,134
the memory. 
This is the standard approach for very 

110
00:08:39,134 --> 00:08:43,570
simple computer systems like embedded 
micro controllers. 

111
00:08:43,570 --> 00:08:47,380
things you might find in an elevator or 
microwave oven. 

112
00:08:47,380 --> 00:08:51,089
Things like that. 
where it is well know and advance how 

113
00:08:51,089 --> 00:08:56,386
much memory we'll actually need. 
It's a single or a few function device, 

114
00:08:56,386 --> 00:09:00,143
it's not going to get new apps loaded 
into it. 

115
00:09:00,143 --> 00:09:06,287
So, we don;t have to worry about 
allocating that memory in in very, 

116
00:09:06,287 --> 00:09:10,566
flexible ways. 
Because we won't have a lot of different 

117
00:09:10,566 --> 00:09:13,644
processes running. 
In fact for the most part in these small 

118
00:09:13,644 --> 00:09:17,390
embedded systems, we'll have a single 
process. 

119
00:09:17,390 --> 00:09:21,482
And it can have full use of that physical 
memory, directly using a physical 

120
00:09:21,482 --> 00:09:26,520
address. 
However, in, larger systems. 

121
00:09:26,520 --> 00:09:31,490
things like our desktops, our laptops, 
even our cell phones these days and 

122
00:09:31,490 --> 00:09:36,512
certainly servers. 
We will have a CPU not generate a direct 

123
00:09:36,512 --> 00:09:42,864
physical address but a virtual address. 
that will then go through a mapping 

124
00:09:42,864 --> 00:09:48,284
function here labeled an MMU. 
Or memory management unit that will keep 

125
00:09:48,284 --> 00:09:51,930
track of where those pieces of memory 
are. 

126
00:09:51,930 --> 00:09:55,060
Are they in physical memory? 
Are they on disk? 

127
00:09:55,060 --> 00:09:58,354
and we have to bring them into the 
physical memory before we can access 

128
00:09:58,354 --> 00:10:01,490
them. 
All those sorts of things will be one 

129
00:10:01,490 --> 00:10:06,920
inside of this memory management unit. 
Which will then generate a physical 

130
00:10:06,920 --> 00:10:12,540
address that will be used to actually 
used to access the physical memory. 

131
00:10:12,540 --> 00:10:17,150
And again, just get that data and bring 
it around back into the CPU as before. 

132
00:10:17,150 --> 00:10:22,150
But that mapping function in between, is 
that level of indirection. 

133
00:10:22,150 --> 00:10:27,049
And that is one of those great ideas in 
computer science that is used to solve 

134
00:10:27,049 --> 00:10:33,285
many, many problems. 
And in the next few videos we'll go into 

135
00:10:33,285 --> 00:10:38,780
the details of how this is implemented 
for virtual memory 

