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)
  • Let the ELF Note Everything
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

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


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