When i use -p $PID
on lsof
i get only the immediate thread of the process. why and how to work with threads and lsof effectively?
sudo lsof -p 5344 > lsof-p5344.log
sudo lsof | grep '\<5344\>' > lsofgrep5344.log
$ wc -l lsof*5344.log
1,120,208 lsofgrep5344.log
2,166 lsof-p5344.log
Those are lines for the exact process. This is not about children process. Here I am using awk
to filter by exactly the process PID numbers (Second column of lsof output) being on field two of the output.
$ cat lsof-p5344.log | awk '{print $2}' | grep '^5344$' | wc -l
2,165
$ cat lsofgrep5344.log | awk '{print $2}' | grep '^5344$' | wc -l
1,120,207
now if i filter by the first column "COMMAND", then i see different threads of the java application, while the lsof -p
one will only show the main thread (e.g. "COMMAND=java_daemon"), the lsof | grep
will have many (e.g. "COMMAND=pool123, JMX, Java2D, Quartz, etc", with "PID" all equal to 5344)