Skip to Content
Navigation:

A stick figure smiling

Here's a preview from my zine, Bite Size Linux!! If you want to see more comics like this, sign up for my saturday comics newsletter or browse more comics!

Image of a comic. To read the full HTML alt text, click "read the transcript".

browse more comics! get the zine!
read the transcript!

### Unix systems use integers to track open files Process, represented by a box with a smiley face: Open foo.txt
kernel, also represented by a box with a smiley face: okay! that’s file #7 for you.

these integers are called file descriptors

lsof (list open files) will show you a process’s open files

$lsof -P 4242
(4242 is the PID we’re interested in)

FD NAME

0 /dev/pts/tty1
1 /dev/pts/tty1
2 pipe: 29174
3 /home/bork/awesome.txt 
5 /tmp/

(FD is for file descriptor)

file descriptors can refer to:

  • files on disk
  • pipes
  • sockets (network connections)
  • terminals (like xterm)
  • devices (your speaker! /dev/null!)
  • LOTS MORE (event fd, inotify, signalfo, epoll, etc.)

little tiny smiling stick figure: not EVERYTHING on Unix is a file, but lots of things are

When you read or write to a file/pipe/network connection you do that using a file descriptor

person: connect to google.com OS: ok! fd is 5!
person: write GET / HTTP/1.1) to fd #5
OS: done!

Let’s see how some simple Python code works under the hood:

Python:

f = open ("file.txt")
f. read lines()

Behind the scenes:
Python program: open file.txt
OS: ok! fd is 4
Python program: read from file #4
OS: here are the contents!

(almost) every process has 3 standard FDs:

  • stdin: 0
  • stdout: 1
  • stderr: 2

“read from stdin”
means
“read from the file descriptor O”
(could be a pipe or file or terminal)

Saturday Morning Comics!

Want another comic like this in your email every Saturday? Sign up here!

I'll send you one of my favourite comics from my archives every Saturday.
© Julia Evans 2024 | All rights reserved (see the FAQ for notes about licensing)