Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

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.

share|improve this answer
1  
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 '11 at 14:28
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.