3

How long do the symlinks form the File Descriptors in /proc/PID/fd/ are kept?

During the complete life of the process?

What about when this is a multi-threaded process?

Do I have the guaranty that after hours of work, all files that were open during some point in time will be listed here? Currently, the links in here are all numbered from 0 to n, with no number missing in between. Can the case occur that a number is missing? Or do a file simply disappears at same point, with no indication that it was ever open?

Tnx.

1 Answer 1

6

The /proc file system is virtual – it reflects the current state of processes, and /proc/$pid/fd only lists files the process is keeping open right now. If a file or socket is closed, its entry under /proc/$pid/fd/ automatically disappears. If the process exits, the entire /proc/$pid directory disappears.

File descriptors always start from 0, and the kernel always assigns the lowest possible non-negative number to newly-created file descriptors. However, a process is always free to close any file descriptor it has – so it is entirely possible that fd/ will contain "holes"; e.g. fd/0 fd/1 fd/3 fd/6 or similar.

For multi-threaded processes, /proc/$pid displays the state of the main thread – additional threads have subdirectories under /proc/$pid/task/ (one task represents one Pthread). However, POSIX.1 requires that all Pthreads within a process share the same file descriptors, so the contents of fd/ will be the same.

2
  • 2
    This. They're not really there, in the sense that they're on the disk. The directory readout is fabricated by the kernel when you read it, on the spot, right at that moment. procfs is a virtual file system (pardon the abuse of the term).
    – c4757p
    Dec 8, 2011 at 14:28
  • Also the pids listed under the /proc/$pid/task/ dir have an own dirs in /proc/.
    – Hi-Angel
    Feb 24, 2015 at 9:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .