Technische Universität Braunschweig
  • Study & Teaching
    • Beginning your Studies
      • Prospective Students
      • Degree Programmes
      • Application
      • Why TU Braunschweig?
    • During your Studies
      • Fresher's Hub
      • Term Dates
      • Courses and Qualifications
      • Practical information
      • Beratungsnavi
      • Financing and Costs
      • Special Circumstances
      • Health & Wellbeing
      • Campus life
    • At the End of your Studies
      • Career Service
      • Alumni
      • Discontinuation and Pre-Certification
      • After graduation
    • Strategies and Quality Management
      • TU Teaching Award
    • For Teaching Staff
      • Strategy, Offers and Information
      • Learning Management System Stud.IP
    • Contact
      • Study Service Centre
      • Academic Advice Service
      • Student Office
      • Department 16 - Academic and Student Affairs
      • 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
    • Research Funding
      • Research Funding Network
      • Research funding
    • Contact
      • Research Services
      • Academy for Graduates
  • Transfer
    • Transfer profile
      • Transfer strategy "TIES® with Impact"
      • Technology Transfer
      • Knowledge Transfer
      • Relationship Management
    • Innovation
      • Startups
      • Patents
    • Offers for schools and pupils
    • For enterprises
      • Friends & Supporters
    • For Alumni*ae
    • For everyone
  • International
    • International Students
      • Why Braunschweig?
      • Degree seeking students
      • Exchange Studies
      • TU Braunschweig Summer School
      • Students from German Schools Abroad
      • Refugees
      • International Student Support
      • International Career Service
    • Going Abroad
      • Studying abroad
      • Internships abroad
      • Teaching and research abroad
      • Working abroad
    • International Researchers
      • Welcome Support for International Researchers
      • 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
      • Newsletter, Podcast & Videos
      • Job Advertisements
  • TU Braunschweig
    • Our Profile
      • Aims & Values
      • Regulations and Guidelines
      • The University Development Initiative 2030
      • Ecoversity – the TU Braunschweig as a university ecosystem
      • Equal Opportunity
      • Diversity
      • Sustainability
      • Facts & Figures
      • Our History
    • Presidency & Organisation
      • Executive Board
      • Designated Offices
      • Administration
      • Facilities
      • Committees
      • Student body
    • Career
      • Working at TU Braunschweig
      • Vacancies
      • Information and Offers for New Employees
    • 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 from A to Z
    • Events
  • Search
  • Quicklinks
    IBR Login
    • Library
    • Mailinglists
    • Webmail
    • Knowledge Base
    • Wiki
    • Account Management
    • Services Status
    • People Search
    • Webmail
    • cloud.TU Braunschweig
    • Messenger
    • Cafeteria
    • TUconnect (student platform)
    • Emergency
    • Stud.IP
    • Library Catalogue
    • Research Information System
    • Information Portal (employees)
    • Link Collection
    • DE
    • EN
    • Instagram
    • YouTube
    • LinkedIn
    • Mastodon
    • Bluesky

    Menu
    Technische Universität BraunschweigIBR
    • Organisation
    • Faculties
    • Carl-Friedrich-Gauß-Fakultät
    • Institutes
    • Institute of Operating Systems and Computer Networks
    • Prof. Dr.-Ing. Christian Mengert-Dietrich
    • Advent(2)
    • Select a Gift
    Logo IBR
    • 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 & Jobs
        • Projects
        • Publications
        • Software
        • Datasets
      • Reliable System Software
        • Overview
        • Team
        • Teaching
        • Theses & Jobs
        • Research
        • Publications
      • Algorithms
        • Team
        • Courses
        • Theses
        • Projects
        • Publications
      • Microprocessor Lab
      • Education
        • Winter 2026/2027
        • Summer 2026
        • Theses
      • Spin-Offs
        • Docoloc
        • bliq (formerly AIPARK)
        • Confidential Technologies
      • Research Cooperations
        • 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


    last changed 2023-12-01, 15:52 by Prof. Dr.-Ing. Christian Mengert-Dietrich
    http://www.ibr.cs.tu-bs.de/users/dietrich/advent/07-select/index.xml · retrieved 2026-11-23

    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
    Legal Notice Privacy Accessibility

    TU Braunschweig uses the software Matomo for anonymised web analysis. The data serve to optimise the web offer.
    You can find more information in our data protection declaration.