CNX Software – Embedded Systems News

How to check open files for a process

While debugging your program, you may encounter the message “Too many open files”. One way to fix the issue could be to review your code and check open, fopen, socket and pipe calls are matched with close and fclose calls, but with large projects this may be cumbersome.

A better way is to list the open files using the proc filesystem.
I’ll use VirtualBox program as an example since this is running in our server.

First, locate the process ID (PID):

pgrep VirtualBox
3901
3950

Then list the file descriptor opened for process 3901

sudo ls -l /proc/3901/fd
total 0
lrwx—— 1 root root 64 2010-10-05 14:52 0 -> /dev/pts/1
lrwx—— 1 root root 64 2010-10-05 14:52 1 -> /dev/pts/1
lr-x—— 1 root root 64 2010-10-05 14:52 10 -> pipe:[15825]
l-wx—— 1 root root 64 2010-10-05 14:52 11 -> pipe:[15825]
lr-x—— 1 root root 64 2010-10-05 14:52 12 -> pipe:[15829]
l-wx—— 1 root root 64 2010-10-05 14:52 13 -> pipe:[15829]
lrwx—— 1 root root 64 2010-10-05 14:52 14 -> socket:[15842]
lr-x—— 1 root root 64 2010-10-05 14:52 15 -> pipe:[15844]
lr-x—— 1 root root 64 2010-10-05 14:52 16 -> pipe:[15832]
l-wx—— 1 root root 64 2010-10-05 14:52 17 -> pipe:[15832]
l-wx—— 1 root root 64 2010-10-05 14:52 18 -> pipe:[15844]
lr-x—— 1 root root 64 2010-10-05 14:52 19 -> pipe:[15996]
lrwx—— 1 root root 64 2010-10-05 14:52 2 -> /dev/pts/1
l-wx—— 1 root root 64 2010-10-05 14:52 20 -> pipe:[15996]
lr-x—— 1 root root 64 2010-10-05 14:52 21 -> pipe:[16007]
l-wx—— 1 root root 64 2010-10-05 14:52 22 -> pipe:[16007]
lr-x—— 1 root root 64 2010-10-05 14:52 3 -> /opt/VirtualBox
lr-x—— 1 root root 64 2010-10-05 14:52 4 -> /opt/VirtualBox
lr-x—— 1 root root 64 2010-10-05 14:52 5 -> /opt/VirtualBox
lr-x—— 1 root root 64 2010-10-05 14:52 6 -> /opt/VirtualBox/components
lrwx—— 1 root root 64 2010-10-05 14:52 7 -> socket:[15822]
lr-x—— 1 root root 64 2010-10-05 14:52 8 -> pipe:[15824]
l-wx—— 1 root root 64 2010-10-05 14:52 9 -> pipe:[15824]

Support CNX Software - Donate via PayPal, become a Patron on Patreon, or buy review samples