This is a free preview of a paid episode. To hear more, visit blog.codingconfessions.com
This is the third video in the virtual memory series based on the virtual memory article/book that I wrote. In the first two videos, we talked about what is virtual memory, and the size of virtual memory address space of a process. In this installment, we understand how this address space is laid out by the operating system and why does it have this specific layout.
The video covers the following topics:
* The address space layout of a process
* The key segments of this address space: text, data, bss, heap, memory-mapped region, stack
* How the stack grows
* Performance and security implications of this layout
In the next video, we will talk about how the kernel maintains the virtual-to-physical address mapping and how the translation works. In the meantime, if you haven’t read the virtual memory article, I recommend checking it out. It is also available in an aesthetically pleasing and screen- friendly PDF/EPUB format at the link below.
Audio and video play straight from the show's own hosting. PodSubs stores and serves no media.
Transcript
From the show. Tap a time to play from there.
SPEAKER_00Hi everyone, this is Abhinav. This is the third video in our virtual memory series. In the first two videos, we talked about what is virtual memory and the size of the virtual memory address space. And in this third video, we are going to talk about the layout of this address space. Before starting, let's quickly recap what we have discussed so far in this series. So we talked about what is virtual memory. It is an abstraction or an indirection provided by the operating system so that
SPEAKER_00multiple processes can run concurrently and safely without interfering in each other's memory because normally if a process has to directly access physical memory it may not know which address is being used by another process so the operating system provides every process its own private virtual memory address space and so the process can freely access those addresses and the operating system
SPEAKER_00Maps those virtual addresses to real physical addresses. So, and when the process reads or writes to those virtual addresses, the access is trapped by the hardware and the hardware translates those virtual addresses to physical addresses and performs the read or write. And now the size of this address space we talked about in the last video. So usually on 64-bit systems, the virtual address is 64 bits in size. But in reality,
SPEAKER_00on most systems, 48 bits are used, which gives us an address space of roughly 256 terabytes. this again is split into a half so 128 terabytes is used by the user space and 128 terabytes are used by the kernel for loading its own code and data Now, in this video, we are going to talk about how this layout looks like, how the operating system organizes this user-side address space.
SPEAKER_00So basically, this is a virtual address space. So it's basically a sequence of addresses going from zero up to the maximum limit. And within this span, the operating system you know, organizes the code and data of the process and it has a certain structure and that structure is how what I'm showing you on the screen. so this address space is organized in the form of segments so
each segment stores a particular kind of data and each segment is a contiguous span of virtual addresses so for example and this diagram is showing so the lowest addresses are towards the
SPEAKER_00bottom and the highest addresses are towards the top and towards the lowest addresses usually you will have the text segment so text segment is where all the executable code of the process is loaded so when the compiler compiles and generates a binary the binary contains the machine instructions so when the process is created the operating system loads those instructions in this segment this segment has a fixed size so because when the program is compiled it will have a fixed number of instructions I'm Basically based on that, the operating system gives this segment a fixed size.
SPEAKER_00So this cannot change dynamically at runtime. After that, usually you will have data segment. So data segment is again of a fixed size and it is used for loading any global and static data of your program. So any global variables you create, any constants that you create, which are initialized to non-zero values, they are loaded in the data segment. Any global variables which are initialized to either 0 or they
are left uninitialized.
SPEAKER_00So, uninitialized global variables are usually, by default, they get the initialized to 0 at runtime. So, that kind of data usually goes into the BSS segment. and so the reason is that the reason we have separate data and VSA segments so basically the only distinction is that data segment will contain initialized globals and VSA segment contains zero initialized globals the
reason is that the compiler makes an optimization so because all the global and static variables are
SPEAKER_00Burned as part of your program binary. And so if there are zero initialized global variables, it doesn't make sense to allocate real memory in the binary. Basically, it will grow the binary size. So the compiler and the linker simply note that 8 bytes of 0 initialized data has to be loaded at this address and when the operating system creates a process at that address it will allocate 8 bytes and initialize to 0 but the binary itself
does not waste 8 bytes of memory and usually large programs will have a ton of 0
SPEAKER_00initialized data so it saves binary space which improves the Program Loading Performance. So in the process created, it is faster to load it. Now, these are all fixed size because they are known at compile time. But in real-world programs, you also need segments for dynamically allocating memory because you need to be able to create dynamic data structures like lists, trees, hash tables, and so on. And these require that you should be able to dynamically allocate memory to store data.
SPEAKER_00So for that you need segment that can grow dynamically at runtime. So for that we usually have the Heap segment.
This is a free preview of a paid episode. To hear more, visit blog.codingconfessions.com In the last video, we learned about copy-on-write (CoW) and understood its mechanics by digging inside the mmap pathway in the...
This is a free preview of a paid episode. To hear more, visit blog.codingconfessions.com In the last video, we discussed demand paging. Now, let’s move towards an even more interesting topic: copy-on-write (CoW). Just...
This is a free preview of a paid episode. To hear more, visit blog.codingconfessions.com In the previous video in our virtual memory series, we discussed demand paging. But that was mostly theoretical, explaining how...
This is a free preview of a paid episode. To hear more, visit blog.codingconfessions.com Welcome back to the video series on virtual memory based on my article on the same topic. It is also available in ebook format on...
This is a free preview of a paid episode. To hear more, visit blog.codingconfessions.com This is a short aside in our virtual memory series. We have talked extensively about the size of a virtual address and the virtual...