Demand-paging

Most of us know that if a process dynamically allocates memory, with malloc, say it does ptr = malloc(8192) ;, then, assuming success, the process is now allocated 8 KB of physical RAM. It might come as a surprise, but, on modern OSes such as Linux, this is actually not the case.

So, what is the case? (In this book, we do not delve into kernel-level details. Also, as you might be aware, the granularity of memory at the level of the OS allocator is a page, which is typically 4 KB.)

It's not a good idea to assume anything when writing robust software. So, how can you correctly determine the page size on the OS? Use the sysconf(3) API; for example, printf("page size = %ld\n", sysconf(_SC_PAGESIZE));, which outputs page size = 4096 ...

Get Hands-On System Programming with Linux now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.