Technische Universität Braunschweig
  • Studium & Lehre
    • Vor dem Studium
      • Informationen für Studieninteressierte
      • Studiengänge von A-Z
      • Bewerbung
      • Fit4TU - Self-Assessment
      • Beratungsangebote für Studieninteressierte
      • Warum Braunschweig?
    • Im Studium
      • Erstsemester-Hub
      • Semestertermine
      • Lehrveranstaltungen
      • Studien-ABC
      • Studienorganisation
      • Beratungsnavi
      • Zusatzqualifikationen
      • Finanzierung und Kosten
      • Besondere Studienbedingungen
      • Gesundheit & Wohlbefinden
      • Campusleben
    • Nach dem Studium
      • Exmatrikulation und Vorlegalisation
      • Nach dem Abschluss
      • Alumni*ae
    • Strategien und Qualitätsmanagement
      • Strategiepapiere für Studium und Lehre
      • Studienqualitätsmittel
      • Studiengangsentwicklung
      • Qualitätsmanagement
      • Systemakkreditierung
      • Rechtliche Grundlagen
      • TU Lehrpreis
    • Für Lehrende
      • Informationen für Lehrende
      • Konzepte
      • Lernmanagementsystem Stud.IP
    • Kontakt
      • Studienservice-Center
      • Sag's uns - in Studium und 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
      • Digitale Forschungsanzeige
    • Forschungsförderung
      • Netzwerk Forschungsförderung
      • Datenbanken und Stiftungen
    • Kontakt
      • Forschungsservice
      • Graduiertenakademie
  • International
    • Internationale Studierende
      • Warum Braunschweig?
      • Studium mit Abschluss
      • Austauschstudium
      • TU Braunschweig Summer School
      • Geflüchtete
      • International Student Support
    • Wege ins Ausland
      • Studium im Ausland
      • Praktikum im Ausland
      • Lehren und Forschen im Ausland
      • Arbeiten im Ausland
    • Internationale Forschende
      • Welcome Support
      • Promotionsstudium
      • 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
      • International Days
      • 5. Studentische Konferenz: Internationalisierung der Hochschulen
      • Newsletter, Podcast & Videos
      • Stellenausschreibungen
  • Die TU Braunschweig
    • Unser Profil
      • Ziele & Werte
      • Ordnungen und Leitlinien
      • Allianzen & Partner
      • Hochschulentwicklung 2030
      • Stiftungsuniversität
      • Internationale Strategie
      • Fakten & Zahlen
      • Unsere Geschichte
    • Karriere
      • Arbeiten an der TU
      • Stellenmarkt
      • Berufsausbildung an der TU
    • Wirtschaft & Unternehmen
      • Unternehmensgründung
      • Freunde & Förderer
    • Öffentlichkeit
      • Veranstaltungskalender
      • Check-in für Schüler*innen
      • Hochschulinformationstag (HIT)
      • Kinder-Uni
      • Das Studierendenhaus
      • Gasthörer*innen & Senior*innenstudium
      • Nutzung der Universitätsbibliothek
    • Presse & Kommunikation
      • Stabsstelle Presse und Kommunikation
      • Medienservice
      • Ansprechpartner*innen
      • Tipps für Wissenschaftler*innen
      • Themen und Stories
    • Kontakt
      • Allgemeiner Kontakt
      • Anreise
      • Für Hinweisgeber
  • Struktur
    • Leitung & Verwaltung
      • Das Präsidium
      • Stabsstellen
      • Verwaltung
      • Organe, Statusgruppen und Kommissionen
    • 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
      • Institute von A-Z
    • Einrichtungen
      • Universitätsbibliothek
      • Gauß-IT-Zentrum
      • Zentrale Personalentwicklung
      • International House
      • Projekthaus
      • Transferservice
      • Hochschulsportzentrum
      • Einrichtungen von A-Z
    • Studierendenschaft
      • Studierendenparlament
      • Fachschaften
      • Studentische Wahlen
    • Lehrer*innenbildung
      • Lehrer*innenfortbildung
      • Forschung
    • Chancengleichheit
      • Gleichstellung
      • Familie
      • Diversität
    • Kontakt
      • Personensuche
  • Suche
  • Schnellzugriff
    • Personensuche
    • Webmail
    • cloud.TU Braunschweig
    • Messenger
    • Mensa
    • TUconnect (Studierendenportal)
    • Lehrveranstaltungen
    • Im Notfall
    • Stud.IP
    • UB Katalog
    • Status GITZ-Dienste
    • Störungsmeldung GB3
    • IT Dienste
    • Informationsportal (Beschäftigte)
    • Beratungsnavi
    • Linksammlung
    • DE
    • EN
    • IBR YouTube
    • Facebook
    • Instagram
    • YouTube
    • LinkedIn
    • Mastodon
Menü
  • Struktur
  • Fakultäten
  • Carl-Friedrich-Gauß-Fakultät
  • Institute
  • Institut für Betriebssysteme und Rechnerverbund
  • Prof. Dr.-Ing. Christian Dietrich
  • Advent(2)
  • Let the ELF Note Everything
Logo IBR
IBR Login
  • 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
      • Sommersemester 2025
      • Wintersemester 2024/2025
      • Abschlussarbeiten
    • Service
      • Bibliothek
      • Mailinglisten
      • Webmail
      • Knowledgebase
      • Wiki
      • Account Management
      • Service-Status
    • Spin-Offs
      • Docoloc
      • bliq (formerly AIPARK)
      • Confidential Technologies
    • Forschungsverbünde
      • IST.hub
  • Task Overview
  • Git repository
  • Mailing list
  • Matrix-Channel

Let the ELF Note Everything

☃️
Git-Repository: Template Solution Solution-Diff (Solution is posted at 18:00 CET)
Workload: 114 lines of code
Important System-Calls: pty(7), termios(3)
Recommended Reads:
  • Dec. 7: Select a Gift file 110 lines [select(2)]
Illustration for this exercsie

Being Santa is a quite busy occupation, especially in December. Therefore, he is eager to delegate as many chores as possible to his hardworking ELFs. For this, he shows the respective ELFs how the task is done once and the ELF has to scribble down everything step-by-step. Nevertheless, even in this tutorial for the intended work flow, Santa is so fast that even the fastest ELF cannot handle to write everything down in time. Can you help the ELFs to build something that automatically notes all the commands Santa issues during the tutorial session?

Pseudo Terminals

Unix was developed in a time, where computer terminals like the VT05 where the new and shiny gadgets that replaced the teletype writer (hence tty). In short tele-typewriters where typewriters connected to the telephone system. For an early Unix user, it was quite normal to log into the machine via telephone on his typewriter, where his strokes where send via telephone to the machine, where it was fed as standard input to the remote process. The process wrote something on its stdout, which was send via the telephone line to the teletypewriter, which printed the output. This setup also explains why the ed(1) editor is line-oriented; vi, with its visual editing features, made only sense after computer terminals evolved. Despite this short historic digression, we learn that (1) interactive processes on Unix speak to (teletype or computer) terminals and (2) the connection is bi-directional.

Probably, you do not have a real computer terminal in hardware at home, but still you are using abstractions that are based on this idea of the teletypewriter: the pseudo teletypewriters (pty(7)), which is another inter-process primitive of Unix systems. In contrast to pipes, which are only uni-directional, a pty is a pair of bi-directional file descriptors. This means that every end can write and read on its end of the pty. Furthermore, to make the illusion of teletypes even more perfect, both ends are not equal, but there is a primary and a secondary end, and we can manipulate the behavior (echo input, flow control) only at the primary end.

For your terminal emulator (e.g. xterm(1)), the emulator creates a new pty pair and gives the secondary fd to a new shell process as stdin, stdout, and stderr. Afterwards, xterm reads the outputs from the primary fd and displays it in its graphical window. Keystrokes are then written to the primary fd. Also, xterm implements flow control: With Ctrl-s you can stop the output and resume it again with Ctrl-q. If you list the directory /proc/self/fd, you get a glimpse on the file-descriptor setup of an interactive process:

$ ls /proc/self/fd -l
total 0
lrwx------ 1 stettberger stettberger 64 Oct  7 10:31 0 -> /dev/pts/3
lrwx------ 1 stettberger stettberger 64 Oct  7 10:31 1 -> /dev/pts/3
lrwx------ 1 stettberger stettberger 64 Oct  7 10:31 2 -> /dev/pts/3
lr-x------ 1 stettberger stettberger 64 Oct  7 10:31 3 -> /proc/1471637/fd

We see here, that the file descriptors 0-2 all point to the secondary pseudo terminal file /dev/pts/3. Please also note that the ls-process, whose open file descriptors we see here, has the directory /proc/1471637/fd opened to read out the directory listing with getdents().

Task

Build the scribble program that acts as a proxy for another program's standard input and output. Everything the user types into stdin, scribble sends to its child process's stdin but also notes it in a separate file. Similar, scribble also dumps everything it receives from the child to another file before writing it to its stdout. All in all, scribble is very similar to the tool script(1), but much simpler.

An example session could look like this:

$ ./scribble STDOUT STDIN python3                                 :(
primary=5, pts=/dev/pts/4, secondary=6
child pid=1469331
Python 3.10.5 (main, Jun  8 2022, 09:26:22) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 1+1
2
>>> 2+2
4
>>> 
child process exited with: 0

For this session, the STDIN file would then contain:

1+1
2+2

and the STDOUT file:

Python 3.10.5 (main, Jun  8 2022, 09:26:22) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 1+1
2
>>> 2+2
4
>>>

Please note, that 1+1 and 2+2 appear twice, since the terminal echoes the typed-in commands.

Hints

  • Use two copy-and-dump threads, one for each direction (terminal to stdin, stdout to terminal) instead of trying to do everything in a non-blocking, epoll()-like fashion.
  • Use O_NOCTTY, when opening /dev/ptmx.
  • When having the primary end of the pty in your hand, use TIOCGPTN to deduce the pseudo-terminal number. Also, you have to unlock the primary fd, before using it (TIOCSPTLCK).

Last modified: 2023-12-01 15:52:28.055739, Last author: , Permalink: /p/advent-15-pty


aktualisiert am 01.12.2023, 15:52 von Prof. Dr.-Ing. Christian Dietrich

Für alle

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