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)
  • Wishlist Sorting
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
      • Summer 2025
      • Winter 2024/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

Wishlist Sorting

☃️
Git-Repository: Template Solution Solution-Diff (Solution is posted at 18:00 CET)
Workload: 58 lines of code
Important System-Calls: readv(2), writev(2), preadv(2), pwritev(2)
Recommended Reads:
  • Dec. 1: The cat on the tip of the iceberg file 45 lines [open(2), pread(2), close(2)]
Illustration for this exercsie

In preparation for Christmas, the ELFs were thinking about the work that has to be done after the wishlists of all the children have arrived. And having many new and young elves, only a few hundred years old, came to the wishlist-processing team, the idea came up to build a new tool for sorting those wishes. And there are a lot of wishes! So efficient sorting is crucial. But sorting is not only about executing the algorithm itself, but they also have to bring in those wishes into the sorting office and push the sorted stream of wishes out to the workbench area.

Write and read in vectors

For you, today will be a little bit easier or your time budget in comparison to yesterday, but we will learn about scatter and getter I/O under Linux and reason a little bit about system call batching. Today, we will not think about the sorting problem, but about getting data in and out to an I/O device (e.g., a file). With our well-known system call write(2) and its cousin pwrite(2), which takes an explicit file-offset, we can write the contents of a buffer into a file. However, at the end of an algorithm, the output data is often scattered across the memory. Only using write(2) we have two options: (1) combine all buffers into an large output buffer and flush that buffer with a single system call to disk, or (2) we could issue many small writes to write each result buffer separately. Clearly, both methods have drawbacks and induce either memcpy(3)-costs (1) or system call overheads (2).

To tackle this issue, Linux (and even POSIX) provide system calls that allow us to specify the source (or destination) buffer in a scattered manner. So instead of passing a single pointer and a buffer length, we pass an vector of (pointer, length)-tuples. Technically, this vector is an array of struct iovecs:

struct iovec iov[2];

iov[0].iov_base = buf0;
iov[0].iov_len = 20;
iov[1].iov_base = buf1;
iov[1].iov_len = 30;

int nwritten = writev(fd, iov, 2);

In this small example, we create an iovec with two fragments that describes a scattered buffer of 50 bytes that is written with a single writev(2) system call. As the kernel does not know about our C language level and the size of iov but only gets a pointer, we have to tell it the number of array elements (2).

And with this interface and its cousins (readv(2), preadv, pwritev), we can now batch many small writes, and avoid the system call overhead, into a single large write. An honorable mention in that man page deserve the preadv2() and the pwritev2 variants, which demonstrate a common pattern that occurred during the development of Unix: Come up with a new system call, establish its API, notice that you want to influence its behavior a little bit, and add a new version 2 that takes a flags argument. So to sum it up, in the read()/write() universe, we have multiple pre- and suffixes to choose from:

  • p: offset into the file is given explicitly
  • v: take a scattered buffer instead of an contiguous buffer
  • 2: mum, I fucked it up again and we now have a new system call that takes an flags argument. (Looking at you openat(2) and openat2(2).
  • 64: Shit, computers are no longer 32-bit machines.

In the kernel, handling of those vectors is quite easy as a library already exists to iterate over those vectors. And interesting example for that is do_loop_readv_writev(), which is a fallback mechanism if a file system does not provide file_operations for readv/writev. In that fallback case, the kernel just falls back to iterating itself over the vector issuing many small reads or writes. While this might sound like our solution (2) from above, this still saves the system call overhead for entering and leaving the kernel.

Task

Write a program that reads lines from stdin, sorts them with qsort(3) and write the sorted lines with a single system-call to stdout. Special requirement: Once a line is read into memory, you are not allowed to move it around as this would be way to inefficient for handling that many wishes.

Hints

  • Give yourself a break and use the GNU-specific library function getline(3) for reading lines.
  • Write your own wishlist.
  • pselect6_time64 is an actual system call and I am not kidding.

Last modified: 2023-12-01 15:52:28.167685, Last author: , Permalink: /p/advent-08-writev


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