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)
  • The cat on the tip of the iceberg
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
      • Winter 2025/2026
      • Summer 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

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 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