Technische Universität Braunschweig
  • Studium & Lehre
    • Vor dem Studium
      • Informationen für Studieninteressierte
      • Studiengänge von A-Z
      • Bewerbung
      • Warum die TU?
    • Im Studium
      • Erstsemester-Hub
      • Semestertermine
      • Lehrveranstaltungen und Qualifikationen
      • Studienorganisation
      • Beratungsnavi
      • Finanzierung und Kosten
      • Besondere Studienbedingungen
      • Gesundheit & Wohlbefinden
      • Studentisches Engagement
    • Nach dem Studium
      • Career Service
      • Alumni*ae
      • Exmatrikulation und Vorbeglaubigung
      • Nach dem Abschluss
    • Strategien und Qualitätsmanagement
      • Qualitätsmanagement
      • Studienqualitätsmittel
      • TU Lehrpreis
    • Für Lehrende
      • Informationen für Lehrende
      • Konzepte
      • Lernmanagementsystem Stud.IP
      • Impact Learning
    • Kontakt
      • Studienservice-Center
      • Sag's uns: Ideen‑ & Beschwerdestelle Studium & 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
    • Forschungsförderung
      • Netzwerk Forschungsförderung
      • Datenbanken und Stiftungen
    • Kontakt
      • Forschungsservice
      • Graduiertenakademie
      • Organisation wissenschaftlicher Veranstaltungen
  • Transfer
    • Transferprofil
      • Transferstrategie "TIES® with Impact"
      • Technologietransfer
      • Wissenstransfer
      • Relationship Management
      • Transferprojekte
    • Innovation
      • Startups
      • Patente
      • Impact Map
    • Schule-Uni-Angebote
      • Webportal für Schule-Uni-Aktivitäten
      • Kinder-Uni Vorlesungen
      • Kinder-Uni Podcast
      • LeoLabs – Campuslabore für Schüler*innen
      • LeoVan – Mobile Experimente für Schulen
      • School Outreach Fonds
      • Hochschulinformationstag
    • Für Unternehmen
      • Deutschlandstipendium
      • Freunde & Förderer
    • Für Alumni*ae
      • Dem Netzwerk beitreten
      • Alumni*ae kennen lernen
      • Alumni*ae Talks
      • Campustouren
    • Für alle
      • Community Café
      • Community Podcast
      • Uni-Shop
  • International
    • Internationale Studierende
      • Warum Braunschweig?
      • Studium mit Abschluss
      • Austauschstudium
      • TU Braunschweig Summer School
      • Schüler*innen von deutschen Auslandsschulen
      • Geflüchtete
      • International Student Support
      • International Career Service
    • Wege ins Ausland
      • Studium im Ausland
      • Praktikum im Ausland
      • Lehren und Forschen im Ausland
      • Arbeiten im Ausland
    • Internationale Forschende
      • Welcome Support for International Researchers
      • 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
      • Newsletter, Social Media, Podcast & Videos
      • Stellenausschreibungen
  • Die TU Braunschweig
    • Unser Profil
      • Ziele & Werte
      • Ordnungen und Leitlinien
      • Hochschulentwicklung 2030
      • Ecoversity – die TU Braunschweig als Ökosystem Universität
      • Chancengleichheit
      • Diversity
      • Nachhaltigkeit
      • Fakten & Zahlen
      • Unsere Geschichte
    • Leitung & Organisation
      • Das Präsidium
      • Stabsstellen
      • Verwaltung
      • Einrichtungen
      • Organe, Statusgruppen und Kommissionen
      • Studierendenschaft
    • Karriere
      • Arbeiten an der TU
      • Stellenmarkt
      • Berufsausbildung an der TU
      • Informationen und Angebote für neue Mitarbeitende
    • 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 von A-Z
    • Die TU für alle
      • Gasthörer*innen & Senior*innenstudium
      • Universitätsbibliothek nutzen
      • Das Studierendenhaus
      • Podcasts
    • Veranstaltungen
      • Veranstaltungskalender
      • Angebote für Schüler*innen
      • Hochschulinformationstag (HIT)
  • Suche
  • Schnellzugriff
    IBR Login
    • Bibliothek
    • Mailinglisten
    • Webmail
    • Knowledgebase
    • Wiki
    • Account Management
    • Service-Status
    • Personensuche
    • Webmail
    • cloud.TU Braunschweig
    • Messenger
    • Mensa
    • TUconnect (Studierendenportal)
    • Im Notfall
    • Stud.IP
    • UB Katalog
    • Forschungsinformations-System (FIS)
    • Status GITZ-Dienste
    • Störungsmeldung GB3
    • Informationsportal (Beschäftigte)
    • Linksammlung
    • DE
    • EN
    • Instagram
    • YouTube
    • LinkedIn
    • Mastodon
    • Bluesky

    Menü
    Technische Universität BraunschweigIBR
    • Struktur
    • Fakultäten
    • Carl-Friedrich-Gauß-Fakultät
    • Institute
    • Institut für Betriebssysteme und Rechnerverbund
    • Prof. Dr.-Ing. Christian Mengert-Dietrich
    • Advent(2)
    • Select a Gift
    Logo IBR
    • 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
        • Wintersemester 2026/2027
        • Sommersemester 2026
        • Abschlussarbeiten
      • Spin-Offs
        • Docoloc
        • bliq (formerly AIPARK)
        • Confidential Technologies
      • Forschungsverbünde
        • IST.hub
    • Task Overview
    • Git repository
    • Mailing list
    • Matrix-Channel

    Select a Gift

    ☃️
    Git-Repository: Template Solution Solution-Diff (Solution is posted at 18:00 CET)
    Workload: 110 lines of code
    Important System-Calls: select(2)
    Recommended Reads:
    • Dec. 1: The cat on the tip of the iceberg file 45 lines [open(2), pread(2), close(2)]
    • Dec. 2: Clone a Chimera! process 45 lines [clone(2), fork(2), getpid(2), gettid(2)]
    Illustration for this exercsie

    For programs to execute it is a necessary precondition that all required resources are available. Simple calculations only require the CPU to be available, but more complex operations might need multiple resources: For example, if your program wants to parse a file in the next step, it does not only require time on the CPU to do the parsing but also the file contents must be loaded to memory as our CPU instructions only work on memory (and registers). So, we could draw a dependency graph between resources (CPUs, I/O requests, network messages) and pieces of code. Only if all dependencies (predecessors) are available or have finished, the piece of code can actually execute.

    Often, we express such dependencies with synchronous system calls, which block the thread continuation until the system call has completed:

    funcA();
    read(fd, buf, 4096);
    funcB(buf);
    

    In this trivial example, the program issues the read(2) system call to express two things: (1) please read 4096 bytes from the file descriptor fd into buf. (2) continue the thread, which then will call funcB() only after the buffer was filled. So, synchronous system calls promise that the invoked functionality has completed before we continue.

    For the second part, if the data is not yet available read() will block the thread and exclude it from scheduling until the data arrived. However, with read() we can only wait for data to become available on a single file descriptor. But sometimes this is not enough: For example, if you are a server process that wants to handle multiple network clients simultaneously you do not know which client socket will provide data first. Thereby, you do not know on which socket you should invoke read() first. While one solution is to spawn an execution thread for each client that blocks its execution on the client socket, this approach comes with high costs for servers with many clients (many threads, frequent thread switches).

    Since Unix did not have proper thread support for a long time, the developers came up with the select(2) system call. With this blocking syscall, the calling thread expresses: please block my execution until one of the given file descriptors becomes "ready". Becoming ready means that a read()/write() call would surely not block. With this, waiting for the next request in a multi-client server becomes easier: just "select" all client sockets and handle all sockets that became ready:

    while True:
        select(client_connections)
        for fd in client_connections:
            if fd.is_ready():
                request = fd.read()
                handle(request)
    

    While this looks quite easy on first sight, it opens a whole new box of problems as we are now in the realm of event-based programming. For example, not only read() can block, but also write() can block if the client cannot receive the answer fast enough. However, for today, we will not think too much about this and only look at the read() side.

    For more details on using select(2), please look at the man page and also at the tutorial man page for select (select_tut(2) as we have no intention to reiterate that.

    Within the kernel, select() is reduced to the poll(2) infrastructure. Actually, poll() is the newer system call but we will, for now, stick to select(). In its core, do_select() is rather simple: It iterates over the given file-descriptor set, which is a fixed-sized bit mask, gets the struct file object, and polls it with vfs_poll() function, which finally calls a concrete file_operations.poll operation.

    Task

    With today's program we'll put select() to some use. You'll write a program that acts as pipe multiplexer filter: After spawning N filter processes, it reads from its standard input and copies its input to the filter processes. On the filter's output side, our program uses select() to demultiplex the filter's stdout descriptors into its own stdout. An example output looks like this:

    $ make run
    seq 1 100 | ./select "grep 1[1-3]" "grep [1-3]2"
    [grep 1[1-3]] Started filter as pid 384425
    [grep [1-3]2] Started filter as pid 384426
    [grep 1[1-3]] 11
    [grep 1[1-3]] 12
    [grep 1[1-3]] 13
    [grep [1-3]2] 12
    [grep [1-3]2] 22
    [grep [1-3]2] 32
    [grep 1[1-3]] filter exited. exitcode=0
    [grep [1-3]2] filter exited. exitcode=0
    

    Hints

    • The given start_proc() function uses posix_spawn(3) to start a filter processes. Use it, but also read the man page of posix_spawn() and be thankful that you do not have to do it yourself with fork(2) and exec(2).
    • On the stdin side you better use a thread that uses blocking system calls to multiplex the data into proc[i].stdin
    • Implement a drain_proc() function that reads data from a process and prefixes it properly with the given label.
    • Test everything without select() and only a single filter process, before you proceed.
    • For each select, you have to reinitialize the readfd file-descriptor set.

    Last modified: 2023-12-01 15:52:27.805320, Last author: , Permalink: /p/advent-07-select


    aktualisiert am 01.12.2023, 15:52 von Prof. Dr.-Ing. Christian Mengert-Dietrich
    http://www.ibr.cs.tu-bs.de/users/dietrich/advent/07-select/index.xml · abgerufen am 23.11.2026

    Für alle

    Stellenmarkt der TU Braunschweig
    Jobbörse des Career Service
    Merchandising
    Sponsoring- & Spendenleistungen
    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

    Zur anonymisierten Reichweitenmessung nutzt die TU Braunschweig die Software Matomo. Die Daten dienen dazu, das Webangebot zu optimieren.
    Weitere Informationen finden Sie in unserer Datenschutzerklärung.