Skip to Main Content
Hands-On System Programming with Linux
book

Hands-On System Programming with Linux

by Kaiwan N. Billimoria, Tigran Aivazian
October 2018
Beginner content levelBeginner
794 pages
19h 23m
English
Packt Publishing
Content preview from Hands-On System Programming with Linux

Memory protection – a code example

Let's consider an example program where the process dynamically allocates four pages of memory and wants to set them up so that the memory protections for each page are as shown in the following table:

Page # Page 0 Page 1 Page 2 Page 3
Protection bits rw- r-- rwx ---

Relevant portions of the code are shown as follows:

First, the main function dynamically allocates page-aligned memory (four pages) with the posix_memalign(3) API, and then invokes the memory protection and the memory testing functions in turn:

[...]    /* Don't use the malloc; POSIX wants page-aligned memory for mprotect(2) */    posix_memalign(&ptr, gPgsz, 4*gPgsz);    if (!ptr)        FATAL("posix_memalign(for %zu bytes) failed\n", 4*gPgsz);
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Linux System Programming Techniques

Linux System Programming Techniques

Jack-Benny Persson
Linux Device Drivers, Second Edition

Linux Device Drivers, Second Edition

Jonathan Corbet, Alessandro Rubini

Publisher Resources

ISBN: 9781788998475Supplemental Content