Technische Universität Braunschweig
  • Studium & Lehre
    • Vor dem Studium
      • Informationen für Studieninteressierte
      • Studiengänge von A-Z
      • Bewerbung
      • Fit4TU - Self-Assessment
      • Beratungsangebote für Studieninteressierte
      • Warum Braunschweig?
    • Im Studium
      • Erstsemester-Hub
      • Semestertermine
      • Lehrveranstaltungen
      • Studien-ABC
      • Studienorganisation
      • Beratungsnavi
      • Zusatzqualifikationen
      • Finanzierung und Kosten
      • Besondere Studienbedingungen
      • Gesundheit & Wohlbefinden
      • Campusleben
    • Nach dem Studium
      • Exmatrikulation und Vorlegalisation
      • Nach dem Abschluss
      • Alumni*ae
    • Strategien und Qualitätsmanagement
      • Strategiepapiere für Studium und Lehre
      • Studienqualitätsmittel
      • Studiengangsentwicklung
      • Qualitätsmanagement
      • Systemakkreditierung
      • Rechtliche Grundlagen
      • TU Lehrpreis
    • Für Lehrende
      • Informationen für Lehrende
      • Konzepte
      • Lernmanagementsystem Stud.IP
    • Kontakt
      • Studienservice-Center
      • Sag's uns - in Studium und Lehre
      • Zentrale Studienberatung
      • Immatrikulationsamt
      • Abteilung 16 - Studium und Lehre
      • Career Service
      • Projekthaus
  • Forschung
    • Forschungsprofil
      • Forschungsschwerpunkte
      • Exzellenzcluster der TU Braunschweig
      • Forschungsprojekte
      • Forschungszentren
      • Forschungsprofile der Professuren
    • Frühe Karrierephase
      • Förderung in den frühen Phasen der wissenschaftlichen Karriere
      • Promotion
      • Postdocs
      • Nachwuchsgruppenleitung
      • Junior Professur und Tenure-Track
      • Habilitation
      • Service-Angebote für Wissenschaftler*innen
    • Forschungsdaten & Transparenz
      • Transparenz in der Forschung
      • Forschungsdaten
      • Open Access Strategie
      • Digitale Forschungsanzeige
    • Forschungsförderung
      • Netzwerk Forschungsförderung
      • Datenbanken und Stiftungen
    • Kontakt
      • Forschungsservice
      • Graduiertenakademie
  • International
    • Internationale Studierende
      • Warum Braunschweig?
      • Studium mit Abschluss
      • Austauschstudium
      • TU Braunschweig Summer School
      • Geflüchtete
      • International Student Support
    • Wege ins Ausland
      • Studium im Ausland
      • Praktikum im Ausland
      • Lehren und Forschen im Ausland
      • Arbeiten im Ausland
    • Internationale Forschende
      • Welcome Support
      • Promotionsstudium
      • Service für gastgebende Einrichtungen
    • Sprachen und interkulturelle Kompetenzvermittlung
      • Deutsch lernen
      • Fremdsprachen lernen
      • Interkulturelle Kompetenzvermittlung
    • Internationales Profil
      • Internationalisierung
      • Internationale Kooperationen
      • Strategische Partnerschaften
      • Internationale Netzwerke
    • International House
      • Wir über uns
      • Kontakt & Sprechstunden
      • Aktuelles und Termine
      • International Days
      • 5. Studentische Konferenz: Internationalisierung der Hochschulen
      • Newsletter, Podcast & Videos
      • Stellenausschreibungen
  • Die TU Braunschweig
    • Unser Profil
      • Ziele & Werte
      • Ordnungen und Leitlinien
      • Allianzen & Partner
      • Hochschulentwicklung 2030
      • Stiftungsuniversität
      • Internationale Strategie
      • Fakten & Zahlen
      • Unsere Geschichte
    • Karriere
      • Arbeiten an der TU
      • Stellenmarkt
      • Berufsausbildung an der TU
    • Wirtschaft & Unternehmen
      • Unternehmensgründung
      • Freunde & Förderer
    • Öffentlichkeit
      • Veranstaltungskalender
      • Check-in für Schüler*innen
      • Hochschulinformationstag (HIT)
      • Kinder-Uni
      • Das Studierendenhaus
      • Gasthörer*innen & Senior*innenstudium
      • Nutzung der Universitätsbibliothek
    • Presse & Kommunikation
      • Stabsstelle Presse und Kommunikation
      • Medienservice
      • Ansprechpartner*innen
      • Tipps für Wissenschaftler*innen
      • Themen und Stories
    • Kontakt
      • Allgemeiner Kontakt
      • Anreise
      • Für Hinweisgeber
  • Struktur
    • Leitung & Verwaltung
      • Das Präsidium
      • Stabsstellen
      • Verwaltung
      • Organe, Statusgruppen und Kommissionen
    • Fakultäten
      • Carl-Friedrich-Gauß-Fakultät
      • Fakultät für Lebenswissenschaften
      • Fakultät Architektur, Bauingenieurwesen und Umweltwissenschaften
      • Fakultät für Maschinenbau
      • Fakultät für Elektrotechnik, Informationstechnik, Physik
      • Fakultät für Geistes- und Erziehungswissenschaften
    • Institute
      • Institute von A-Z
    • Einrichtungen
      • Universitätsbibliothek
      • Gauß-IT-Zentrum
      • Zentrale Personalentwicklung
      • International House
      • Projekthaus
      • Transferservice
      • Hochschulsportzentrum
      • Einrichtungen von A-Z
    • Studierendenschaft
      • Studierendenparlament
      • Fachschaften
      • Studentische Wahlen
    • Lehrer*innenbildung
      • Lehrer*innenfortbildung
      • Forschung
    • Chancengleichheit
      • Gleichstellung
      • Familie
      • Diversität
    • Kontakt
      • Personensuche
  • Suche
  • Schnellzugriff
    • Personensuche
    • Webmail
    • cloud.TU Braunschweig
    • Messenger
    • Mensa
    • TUconnect (Studierendenportal)
    • Lehrveranstaltungen
    • Im Notfall
    • Stud.IP
    • UB Katalog
    • Status GITZ-Dienste
    • Störungsmeldung GB3
    • IT Dienste
    • Informationsportal (Beschäftigte)
    • Beratungsnavi
    • Linksammlung
    • DE
    • EN
    • IBR YouTube
    • Facebook
    • Instagram
    • YouTube
    • LinkedIn
    • Mastodon
Menü
  • Struktur
  • Fakultäten
  • Carl-Friedrich-Gauß-Fakultät
  • Institute
  • Institut für Betriebssysteme und Rechnerverbund
  • Prof. Dr.-Ing. Christian Dietrich
  • Advent(2)
  • Workbench Management
Logo IBR
IBR Login
  • Institut für Betriebssysteme und Rechnerverbund
    • News
    • Wir über uns
      • Gesamtes Team
      • Anreise
      • Raumplan
      • Projekte
      • Veröffentlichungen
      • Software
      • News Archiv
    • Connected and Mobile Systems
      • Team
      • Lehrveranstaltungen
      • Abschlussarbeiten
      • Projekte
      • Veröffentlichungen
      • Software
      • Datensätze
    • Verlässliche Systemsoftware
      • Übersicht
      • Team
      • Lehre
      • Arbeiten & Jobs
      • Forschung
      • Publikationen
    • Algorithmik
      • Team
      • Lehrveranstaltungen
      • Abschlussarbeiten
      • Projekte
      • Veröffentlichungen
    • Mikroprozessorlabor
    • Studium
      • Sommersemester 2025
      • Wintersemester 2024/2025
      • Abschlussarbeiten
    • Service
      • Bibliothek
      • Mailinglisten
      • Webmail
      • Knowledgebase
      • Wiki
      • Account Management
      • Service-Status
    • Spin-Offs
      • Docoloc
      • bliq (formerly AIPARK)
      • Confidential Technologies
    • Forschungsverbünde
      • IST.hub
  • Task Overview
  • Git repository
  • Mailing list
  • Matrix-Channel

Workbench Management

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

There are many ELFs, really PT_LOADs of ELFs, that are all running around, preparing presents, grabbing tools, sewing, glueing, skrewing and what not. And they also have a free-floating workbench policy, where an ELF will use a workbench only for a single task and then release it to be used by other ELFs. Thereby, they can maximize their output as they get away with having less idle work benches. However, this requires coordination as sometimes two ELFs want to use the same workbench for their next task. Luckily, there are some Linux primitives that might help us with this synchronization problem. However, they are rather low level and a better abstraction like a semaphore would be great!

futex - Fast User Space Mutexes

Today, we will look at the important topic of inter-process communication (IPC) and more specifically at the topic of synchronization. How can we as application developers coordinate the work of multiple threads or processes. Of course, you can and usually should use the pthread library for mutexes, barriers, and condition variables. These primitives implement waiting for other threads, either until a mutex is unlocked, a barrier is reached by all of the threads, or when a certain condition is met. But how does the maintainer of that library implement these high-level primitives? To answer this, we will look today at the futex(2) system call, which is the root of all modern synchronization mechanism on Linux.

Described by Franke and Russel, futexes assist us in building synchronization primitives that involve passive waiting. A thread waits actively (sometimes busy waiting), when it itself, in its own CPU time, actively waits for a condition to become true. In contrast to this, passive waiting is a technique to delegate this duty to the kernel while the process is send to a sleep state. In the meantime, while our process sleeps, the kernel can schedule other processes onto the CPU.

With futexes, the Linux kernel takes a minimal approach to passive waiting: Instead of supplying many primitives for all kind of operations (semaphores, condition variables, barriers) as individual system calls, we only have a single system call that implements the minimal necessary semantic within the kernel: (1) With FUTEX_WAIT, we enqueue the current task into a wait queue, and with (2) FUTEX_WAKE, we can wakeup N threads from this wait queue. The reasons why and when threads invoke these primitives is fully up to the user space. Thereby, the user can implement different high-level abstractions and waiting strategies on top of a single system call; a great example of a well-crafted kernel interface!

int futex(uint32_t *addr, int op, uint32_t val, ....);

Furthermore, the futex interface is very versatile and requires no explicit creation or destruction of in-kernel objects. As we see from the prototype, the futex system call gets a pointer as its first argument: this 32-bit memory cell referenced by this pointer is the futex. So we can wait on any 32-bit word in our address space! But how does this work? Internally, Linux creates the waiting queues on demand and uses a hash table to map the user space address to a specific wait queue. Within each wait queue, threads that wait on different addresses are mixed and the kernel iterates over these lists, filtering out the actually referenced threads.

While futex(2) supports many more variants of wait and wake, we will now have a look at the two most basic variants:

  • op=FUTEX_WAKE: Wake up N=val threads from the referenced wait queue. If there are not enough waiting threads, nothing happens and no state is recorded.

  • op=FUTEX_WAIT: Enqueue the current thread into the waiting queue, if(!) the condition *addr == val holds true.

The condition for the wait operation is necessary to actually give synchronization guarantees. For example, if you implement the semaphore (see tasks), you decrement the 32-bit word and sleep if the value reaches zero. But what happens, when another process increments the value in between decrementing and going to sleep? Then we must not sleep but would! So in essence we have to weld the check and the sleep operation together, executing them at the same time (atomically). And this can be done with the futex system call: futex(addr, FUTEX_WAIT, 0). Thereby, we avoid the described lost-wakeup problem.

Tasks

Today the task is comprised of three parts:

  1. Create a semaphore implementation on top of the futex system call. For the user space part of the semaphore, you should use atomic instructions.
  2. Build a bounded buffer implementation that supports bb_put() and bb_get() and is synchronized with three of your semaphores. If the buffer is full, bb_put should block, if the buffer is empty, bb_get should block.
  3. Write a simple test program with two processes (use fork!) that uses the bounded buffer to transfer data from the child process to the parent process. To test your semaphore implementation, let the child process initialize the bounded buffer after waiting for a second.

Hints

Decrementing and incrementing counters usually is a non-atomic read-update-write cycle. However, for our semaphore, we require atomic instructions, which are luckily available in the modern C standard:

atomic_int counter;

// Retrieve the current counter 
int current = atomic_load(&counter);

// Retrieve the current counter and increment it
int prev = atomic_fetch_add(&counter, 1);

// Atomically compare the counter and exchange its contents
// with 42 if it currently has the value 23
int value = 23;
atomic_compare_exchange_strong(&counter, &value, 42)

Last modified: 2023-12-01 15:52:28.110924, Last author: , Permalink: /p/advent-04-futex


aktualisiert am 01.12.2023, 15:52 von Prof. Dr.-Ing. Christian Dietrich

Für alle

Stellen der TU Braunschweig
Jobbörse des Career Service
Merchandising
Sponsoring- & Spendenleistungen
Drittmittelgeförderte Forschungsprojekte
Vertrauenspersonen für Hinweisgeber

Für Studierende

Semestertermine
Lehrveranstaltungen
Studiengänge von A-Z
Informationen für Erstsemester
TUCard

Interne Tools

Status GITZ-Dienste
Handbuch für TYPO3 (Intern)
Corporate Design-Toolbox (Intern)
Glossar (DE-EN)
Meine Daten ändern
Hochschulöffentliche Bekanntmachungen

Kontakt

Technische Universität Braunschweig
Universitätsplatz 2
38106 Braunschweig
Postfach: 38092 Braunschweig
Telefon: +49 (0) 531 391-0

Anreise

© Technische Universität Braunschweig
Impressum Datenschutz Barrierefreiheit