Why "lsof" reports files for each thread in process ?

Solution Verified - Updated -

Issue

  • Doing lsof -p PID will get the open files and lsof | grep PID gets all files repeated for each thread in the proc.
  • strace shows its opening each /proc/pid/task/pid/fd directory per thread.
  • Compile below :
cat 10.c
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>

void *thr(void *arg)
{
    sleep(60);
    return(0);
}

int main(void)
{
    int i;
    FILE *fp[10];
    char name[32];
    pthread_t t;

    for (i = 0; i < 10; i++) {
        sprintf(name, "/dev/shm/test_%d", i);
        fp[i] = fopen(name, "w");
    }
    pthread_create(&t, 0, thr, 0);
    pthread_join(t, 0);
    return(0);
}
  • Running it :
[root@localhost ~]# ./10 &
[1] 26790
[root@localhost ~]# lsof -p 26790
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF      NODE NAME
10      26790 root  cwd    DIR  253,0     4096 201326721 /root
10      26790 root  rtd    DIR  253,0     4096       128 /
10      26790 root  txt    REG  253,0     5024 202109902 /root/10
10      26790 root  mem    REG  253,0  2107816  67240415 /usr/lib64/libc-2.17.so
10      26790 root  mem    REG  253,0   142304  67240435 /usr/lib64/libpthread-2.17.so
10      26790 root  mem    REG  253,0   164440  67240412 /usr/lib64/ld-2.17.so
10      26790 root    0u   CHR  136,1      0t0         4 /dev/pts/1
10      26790 root    1u   CHR  136,1      0t0         4 /dev/pts/1
10      26790 root    2u   CHR  136,1      0t0         4 /dev/pts/1
10      26790 root    3w   REG   0,18        0 239293070 /dev/shm/test_0
10      26790 root    4w   REG   0,18        0 239293071 /dev/shm/test_1
10      26790 root    5w   REG   0,18        0 239293072 /dev/shm/test_2
10      26790 root    6w   REG   0,18        0 239293073 /dev/shm/test_3
10      26790 root    7w   REG   0,18        0 239293074 /dev/shm/test_4
10      26790 root    8w   REG   0,18        0 239293075 /dev/shm/test_5
10      26790 root    9w   REG   0,18        0 239293076 /dev/shm/test_6
10      26790 root   10w   REG   0,18        0 239293077 /dev/shm/test_7
10      26790 root   11w   REG   0,18        0 239293078 /dev/shm/test_8
10      26790 root   12w   REG   0,18        0 239293079 /dev/shm/test_9
[root@localhost ~]# lsof | grep 26790
lsof: WARNING: can't stat() nfs file system /cs
      Output information may be incomplete.
10        26790          root  cwd       DIR              253,0      4096  201326721 /root
10        26790          root  rtd       DIR              253,0      4096        128 /
10        26790          root  txt       REG              253,0      5024  202109902 /root/10
10        26790          root  mem       REG              253,0   2107816   67240415 /usr/lib64/libc-2.17.so
10        26790          root  mem       REG              253,0    142304   67240435 /usr/lib64/libpthread-2.17.so
10        26790          root  mem       REG              253,0    164440   67240412 /usr/lib64/ld-2.17.so
10        26790          root    0u      CHR              136,1       0t0          4 /dev/pts/1
10        26790          root    1u      CHR              136,1       0t0          4 /dev/pts/1
10        26790          root    2u      CHR              136,1       0t0          4 /dev/pts/1
10        26790          root    3w      REG               0,18         0  239293070 /dev/shm/test_0
10        26790          root    4w      REG               0,18         0  239293071 /dev/shm/test_1
10        26790          root    5w      REG               0,18         0  239293072 /dev/shm/test_2
10        26790          root    6w      REG               0,18         0  239293073 /dev/shm/test_3
10        26790          root    7w      REG               0,18         0  239293074 /dev/shm/test_4
10        26790          root    8w      REG               0,18         0  239293075 /dev/shm/test_5
10        26790          root    9w      REG               0,18         0  239293076 /dev/shm/test_6
10        26790          root   10w      REG               0,18         0  239293077 /dev/shm/test_7
10        26790          root   11w      REG               0,18         0  239293078 /dev/shm/test_8
10        26790          root   12w      REG               0,18         0  239293079 /dev/shm/test_9
10        26790 26791    root  cwd       DIR              253,0      4096  201326721 /root
10        26790 26791    root  rtd       DIR              253,0      4096        128 /
10        26790 26791    root  txt       REG              253,0      5024  202109902 /root/10
10        26790 26791    root  mem       REG              253,0   2107816   67240415 /usr/lib64/libc-2.17.so
10        26790 26791    root  mem       REG              253,0    142304   67240435 /usr/lib64/libpthread-2.17.so
10        26790 26791    root  mem       REG              253,0    164440   67240412 /usr/lib64/ld-2.17.so
10        26790 26791    root    0u      CHR              136,1       0t0          4 /dev/pts/1
10        26790 26791    root    1u      CHR              136,1       0t0          4 /dev/pts/1
10        26790 26791    root    2u      CHR              136,1       0t0          4 /dev/pts/1
10        26790 26791    root    3w      REG               0,18         0  239293070 /dev/shm/test_0
10        26790 26791    root    4w      REG               0,18         0  239293071 /dev/shm/test_1
10        26790 26791    root    5w      REG               0,18         0  239293072 /dev/shm/test_2
10        26790 26791    root    6w      REG               0,18         0  239293073 /dev/shm/test_3
10        26790 26791    root    7w      REG               0,18         0  239293074 /dev/shm/test_4
10        26790 26791    root    8w      REG               0,18         0  239293075 /dev/shm/test_5
10        26790 26791    root    9w      REG               0,18         0  239293076 /dev/shm/test_6
10        26790 26791    root   10w      REG               0,18         0  239293077 /dev/shm/test_7
10        26790 26791    root   11w      REG               0,18         0  239293078 /dev/shm/test_8
10        26790 26791    root   12w      REG               0,18         0  239293079 /dev/shm/test_9

Environment

  • Red Hat Enterprise Linux 7.2
  • kernel 3.10.0-327.10.1.el7.x86_64
  • lsof-4.87-4.el7.x86_64

Subscriber exclusive content

A Red Hat subscription provides unlimited access to our knowledgebase of over 48,000 articles and solutions.

Current Customers and Partners

Log in for full access

Log In