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)
    • The cat on the tip of the iceberg
    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

    The cat on the tip of the iceberg

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

    Welcome to the Christmas village, where the ELFs (see elf(5)) live and work together to prepare everything for Christmas. While they are Santa's little helpers, they have a rich culture on their own. They are open to new technologies, but also have a rather strong technocratic culture that might sometimes irritate you. Their favorite operating system is of course Linux and they are also into low-level systems hacking to squeeze out the last bit of performance from their machines. While cooling is no problem at the north pole, getting enough energy from renewables is hard up there. Also, they often find themselves in a NIH (Not Invented Here) situation, where they solve problems that others have already solved. We are guests in the village for the next 24 days, so this is a good opportunity to get some in depth knowledge about Linux and its specifics. Come and join me in our stay at the Christmas village where the ELFs like invoking system calls as much as glueing together gifts for the children of the world.

    Introduction

    System calls are commands, like read! or write! for our operating system. They are verbs and they are an imperative. But it is not enough to order an action, we also have to specify on what object the action should be performed. And while integers or strings make good arguments, we often have to address some stateful object within the operating system. I am talking of file descriptors (FDs).

    First of all, I have to tell you something. But it has to be in secret... so come closer... closer. OK! Now it's good. File descriptors are not (only) about files anymore. They are a much more general concept that we use to address kernel objects from user space. On the surface, for our program, they are just integers. For example, have you already heard that 1 is usually the standard output of a program? But what happens, if we use 1 with, for example, write(2)?

    write(1, "foo", 3)
    

    When the OS receives those three arguments, it will consult the file-descriptor table (struct fdtable) of the current process and use the given integer as an index to retrieve a struct file. However, you might already have guessed it, these objects are not necessarily only open files, but they can represent all sorts of in-kernel objects that come with a struct file interface. For example, like our standard output, they can be a terminal or, as we will see later, point to another process.

    You have to imagine file descriptors as a handle in user space that we can grab, store and pass around in our program, while there is an in-kernel object connected to it. Or, to say it differently, the file descriptor is just the tip of the iceberg.

    Task

    We take a soft start in our Advent(2), and today's task is to write a simple version of the program cat(1). cat takes N file names as command-line arguments, concatenates their contents, and writes the result to its standard output.

    For this task, it is not necessary to use malloc(3), but you can get away with a globally allocated char buffer[4096] array.

    System Calls

    • open(2): The open system call takes a filename and some flags and opens the file. Thereby, it creates a new struct file in the kernel, finds a free slot in the struct fdtable and gives you the positive index. If the operation fails, you will get a negative integer.
    • read(2): Reads data, up to count bytes, from the file into the given buffer. Thereby, the kernel remembers the "current position" of the open file in the struct file (f_pos). If you want to read at an arbitrary position, you should use pread(2) or reposition the file pointer with lseek(2).

    • close(2): Removes the fdtable entry and deallocates the struct file if we were the last owner. Quiz: Can multiple entries of fdtable point to the same struct file? (hint: yes).

    Last modified: 2023-12-01 15:52:27.750730, Last author: , Permalink: /p/advent-01-open


    last changed 2023-12-01, 15:52 by Prof. Dr.-Ing. Christian Mengert-Dietrich
    http://www.ibr.cs.tu-bs.de/users/dietrich/advent/01-open/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.