Technische Universität Braunschweig
  • Study & Teaching
    • Beginning your Studies
      • Prospective Students
      • Degree Programmes
      • Application
      • Fit4TU
      • Why Braunschweig?
    • During your Studies
      • Fresher's Hub
      • Term Dates
      • Courses
      • Practical Information
      • Beratungsnavi
      • Additional Qualifications
      • Financing and Costs
      • Special Circumstances
      • Health and Well-being
      • Campus life
    • At the End of your Studies
      • Discontinuation and Credentials Certification
      • After graduation
      • Alumni*ae
    • For Teaching Staff
      • Strategy, Offers and Information
      • Learning Management System Stud.IP
    • Contact
      • Study Service Centre
      • Academic Advice Service
      • Student Office
      • Career Service
  • Research
    • Research Profile
      • Core Research Areas
      • Clusters of Excellence at TU Braunschweig
      • Research Projects
      • Research Centres
      • Professors‘ Research Profiles
    • Early Career Researchers
      • Support in the early stages of an academic career
      • PhD-Students
      • Postdocs
      • Junior research group leaders
      • Junior Professorship and Tenure-Track
      • Habilitation
      • Service Offers for Scientists
    • Research Data & Transparency
      • Transparency in Research
      • Research Data
      • Open Access Strategy
      • Digital Research Announcement
    • Research Funding
      • Research Funding Network
      • Research funding
    • Contact
      • Research Services
      • Academy for Graduates
  • International
    • International Students
      • Why Braunschweig?
      • Degree seeking students
      • Exchange Studies
      • TU Braunschweig Summer School
      • Refugees
      • International Student Support
    • Going Abroad
      • Studying abroad
      • Internships abroad
      • Teaching and research abroad
      • Working abroad
    • International Researchers
      • Welcome Support
      • PhD Studies
      • Service for host institutes
    • Language and intercultural competence training
      • Learning German
      • Learning Foreign Languages
      • Intercultural Communication
    • International Profile
      • Internationalisation
      • International Cooperations
      • Strategic Partnerships
      • International networks
    • International House
      • About us
      • Contact & Office Hours
      • News and Events
      • International Days
      • 5th Student Conference: Internationalisation of Higher Education
      • Newsletter, Podcast & Videos
      • Job Advertisements
  • TU Braunschweig
    • Our Profile
      • Aims & Values
      • Regulations and Guidelines
      • Alliances & Partners
      • The University Development Initiative 2030
      • Foundation University
      • Facts & Figures
      • Our History
    • Career
      • Working at TU Braunschweig
      • Vacancies
    • Economy & Business
      • Entrepreneurship
      • Friends & Supporters
    • General Public
      • Check-in for Students
      • The Student House
      • Access to the University Library
    • Media Services
      • Communications and Press Service
      • Services for media
      • Film and photo permits
      • Advices for scientists
      • Topics and stories
    • Contact
      • General Contact
      • Getting here
  • Organisation
    • Presidency & Administration
      • Executive Board
      • Designated Offices
      • Administration
      • Committees
    • Faculties
      • Carl-Friedrich-Gauß-Fakultät
      • Faculty of Life Sciences
      • Faculty of Architecture, Civil Engineering and Environmental Sciences
      • Faculty of Mechanical Engineering
      • Faculty of Electrical Engineering, Information Technology, Physics
      • Faculty of Humanities and Education
    • Institutes
      • Institutes from A to Z
    • Facilities
      • University Library
      • Gauß-IT-Zentrum
      • Professional and Personnel Development
      • International House
      • The Project House of the TU Braunschweig
      • Transfer Service
      • University Sports Center
      • Facilities from A to Z
    • Equal Opportunity Office
      • Equal Opportunity Office
      • Family
      • Diversity for Students
  • Search
  • Quicklinks
    • People Search
    • Webmail
    • cloud.TU Braunschweig
    • Messenger
    • Cafeteria
    • Courses
    • Stud.IP
    • Library Catalogue
    • IT Services
    • Information Portal (employees)
    • Link Collection
    • DE
    • EN
    • IBR YouTube
    • Facebook
    • Instagram
    • YouTube
    • LinkedIn
    • Mastodon
Menu
  • Organisation
  • Faculties
  • Carl-Friedrich-Gauß-Fakultät
  • Institutes
  • Institute of Operating Systems and Computer Networks
  • Prof. Dr.-Ing. Christian Dietrich
  • Advent(2)
  • A Map to Persistance!
Logo IBR
IBR Login
  • Institute of Operating Systems and Computer Networks
    • News
    • About us
      • Whole Team
      • Directions
      • Floor Plan
      • Projects
      • Publications
      • Software
      • News Archive
    • Connected and Mobile Systems
      • Team
      • Courses
      • Theses
      • Projects
      • Publications
      • Software
      • Datasets
    • Reliable System Software
      • Overview
      • Team
      • Teaching
      • Theses & Jobs
      • Research
      • Publications
    • Algorithms
      • Team
      • Courses
      • Theses
      • Projects
      • Publications
    • Microprocessor Lab
    • Education
      • Summer 2025
      • Winter 2024/2025
      • Theses
    • Services
      • Library
      • Mailinglists
      • Webmail
      • Knowledge Base
      • Wiki
      • Account Management
      • Services Status
    • Spin-Offs
      • Docoloc
      • bliq (formerly AIPARK)
      • Confidential Technologies
    • Research Cooperations
      • IST.hub
  • Task Overview
  • Git repository
  • Mailing list
  • Matrix-Channel

A Map to Persistance!

☃️
Git-Repository: Template Solution Solution-Diff (Solution is posted at 18:00 CET)
Workload: 60 lines of code
Important System-Calls: mmap(2), ftruncate(2)
Illustration for this exercsie

ELFs are C programmers. They like the language, they dream of pointers, and they are sceptic of any new language feature that those shiny newcomers bring. But sometimes they see features that they really would like to have, but they cannot admit that they were wrong about their scepticism. Instead they implement the feature on their own in C. For example, in the Mnemosyne system, the user can annotate a global variable with the persistent attribute and it will retain its value over different program invocation.

mmap - Manipulate the memory mapping of your process

Today, we will look at the virtual-memory subsystem of Linux and the mmap(2) system call, which we can use to manipulate the virtual address space of the current process. As we discussed yesterday, most processes have their very own address space. This means that the same pointer (e.g., 0x45004) resolves to different memory cell in two different processes, making it a virtual address. The translation between virtual and physical addresses is done by the MMU, which is configured by the operating system. The MMU is hardware circuitry that translates (or maps) on the granularity of pages (usually 4096 bytes): (virtual) pages to (physical) page frames.

While we can use mmap() to simply request (anonymous) memory for our amusement, the virtual-memory subsystem of Linux is much more flexible (as are other operating systems). And today, we will use one of these features: file-mapped I/O, where we instruct the operating system to make the contents of a file visible within our address space. Unlike pread(), a file mapping does not only transfer the file contents once into a memory region, but the file mapping is synchronized with the actual file. Therefore, a write into a file mapping will actually change the file; no explicit synchronization (or pwrite()) is required.

Internally, a mmap() call creates a new struct vm_area_struct object and adds it to the struct mm_struct of the current process. And if the new mapping is anonymous, that's about it. However, for a file mapping, we also set vm_file pointer to the file that we address with mmap()s fd argument.

But "Wait!", you might ask: "When is the file content actually read into memory?" For this, we have to understand that Linux fills its page tables, which are the MMU-specific representation of an mm_struct, lazily on demand. So, whenever you want to access a page that is not yet present in the page table, the MMU will throw a page fault, the OS will check whether the access was legitimate, and if so allocate a page frame and install it at the faulting address. And it is in this process, that the OS inspects the vm_area_struct for a vm_file pointer and reads in (we call it, "it pages in") the respective file content. The process is paused until the accessed page is actually present in memory. Thereby, we also can conclude that mapping a huge (multiple GiB) file is not expensive in itself, as we only pay for those accesses that we actually perform.

Tasks

As mapping a file into our virtual-address space is rather boring, we've come up with a more interesting challenge: In this task, you should make some specially-marked variables persistent. For this, we replace the anonymous memory where the variables live at the process start, with a shared file mapping. Thereby, the OS will synchronize those variables with a backing store. For example, in the following (simplified) program, we use the file mmap.persistent as a backing store:

  persistent int count = 10;
  ...
  int main(int argc, char *argv[]) {
     setup_persistent("mmap.persistent");
     printf("count = %d\n", count++);
  }

If we invoke this program three times, we expect the following output:

 $> ./test
 count = 10
 $> ./test
 count = 11
 $> ./test
 count = 12
  1. Complete the function setup_persistent()
  2. Inspect the file mmap.persistent with the xxd tool (hexdump)

Last modified: 2023-12-01 15:52:27.502376, Last author: , Permalink: /p/advent-03-mmap


last changed 2023-12-01, 15:52 by Prof. Dr.-Ing. Christian Dietrich

For All Visitors

Vacancies of TU Braunschweig
Career Service' Job Exchange 
Merchandising

For Students

Term Dates
Courses
Degree Programmes
Information for Freshman
TUCard

Internal Tools

Glossary (GER-EN)
Change your Personal Data

Contact

Technische Universität Braunschweig
Universitätsplatz 2
38106 Braunschweig

P. O. Box: 38092 Braunschweig
GERMANY

Phone: +49 (0) 531 391-0

Getting here

© Technische Universität Braunschweig
Imprint Privacy Accessibility