How to view the fd opened by a process and the corresponding file or socket operations, fd corresponds
/********************************************************************* * Author : Samson * Date : 06/22/2015 * Test platform: * gcc (Ubuntu 4.8.2-19ubuntu1) 4.8.2 * GNU bash, 4.3.11(1)-release (x86_64-pc-linux-gnu) * *******************************************************************/
In GNU Linux, the native socket is used to write the server program and client program. According to the general rule, the server program will first create a socket and listen to this port, the client program is mainly a connection to the server. When only the server program is enabled, lsof can be used to view the description of all open files on the server. The process of the server program is 29016, as follows:
Ufo @ ufo :~ /Socket $ lsof-p 29016
Command pid user fd type device size/OFF NODE NAME
Oldser 29016 ufo cwd DIR 4096 2225845/home/ufo/socket
Oldser 29016 ufo rtd DIR 8, 1 4096 2/
Oldser 29016 ufo txt REG 8203 2224940/home/ufo/socket/oldser
Oldser 29016 ufo mem REG 14768 1442612/lib/x86_64-linux-gnu/libdl-2.13.so
Oldser 29016 ufo mem REG 1599504 1442606/lib/x86_64-linux-gnu/libc-2.13.so
Oldser 29016 ufo mem REG 18888 1446570/lib/x86_64-linux-gnu/liblsp. so
Oldser 29016 ufo mem REG 136936 1442618/lib/x86_64-linux-gnu/ld-2.13.so
Oldser 29016 ufo del reg 0 0/SYSVa5723213
Oldser 29016 ufo 0u CHR 136,11 0t0 14/dev/pts/11
Oldser 29016 ufo 1u CHR 136, 11 0t0 14/dev/pts/11
Oldser 29016 ufo 2u CHR 136, 11 0t0 14/dev/pts/11
Oldser 29016 ufo 3u IPv4 550310 0t0 TCP localhost: 5555 (LISTEN)
The port 5555 is opened for listening;
After using the client to connect to port 5555:
Ufo @ ufo :~ /Socket $ lsof-p 29016
Command pid user fd type device size/OFF NODE NAME
Oldser 29016 ufo cwd DIR 4096 2225845/home/ufo/socket
Oldser 29016 ufo rtd DIR 8, 1 4096 2/
Oldser 29016 ufo txt REG 8203 2224940/home/ufo/socket/oldser
Oldser 29016 ufo mem REG 14768 1442612/lib/x86_64-linux-gnu/libdl-2.13.so
Oldser 29016 ufo mem REG 1599504 1442606/lib/x86_64-linux-gnu/libc-2.13.so
Oldser 29016 ufo mem REG 18888 1446570/lib/x86_64-linux-gnu/liblsp. so
Oldser 29016 ufo mem REG 136936 1442618/lib/x86_64-linux-gnu/ld-2.13.so
Oldser 29016 ufo del reg 0 0/SYSVa5723213
Oldser 29016 ufo 0u CHR 136,11 0t0 14/dev/pts/11
Oldser 29016 ufo 1u CHR 136, 11 0t0 14/dev/pts/11
Oldser 29016 ufo 2u CHR 136, 11 0t0 14/dev/pts/11
Oldser 29016 ufo 3u IPv4 550310 0t0 TCP localhost: 5555 (LISTEN)
Oldser 29016 ufo 4u IPv4 550311 0t0 TCP localhost: 5555-> localhost: 39905 (CLOSE_WAIT)
Sometimes a row named anon_inode appears. What does this mean?
After I add an epoll to the server program and start the server program again, fd like anon_inode appears, for example:
Ufo @ ufo :~ /Socket $ lsof-p 29381
Command pid user fd type device size/OFF NODE NAME
Epoll_ser 29381 ufo cwd DIR 4096 2225845/home/ufo/socket
Epoll_ser 29381 ufo rtd DIR 8, 1 4096 2/
Epoll_ser 29381 ufo txt REG 9830 2229560/home/ufo/socket/epoll_ser
Epoll_ser 29381 ufo mem REG 14768 1442612/lib/x86_64-linux-gnu/libdl-2.13.so
Epoll_ser 29381 ufo mem REG 1599504 1442606/lib/x86_64-linux-gnu/libc-2.13.so
Epoll_ser 29381 ufo mem REG 18888 1446570/lib/x86_64-linux-gnu/liblsp. so
Epoll_ser 29381 ufo mem REG 136936 1442618/lib/x86_64-linux-gnu/ld-2.13.so
Epoll_ser 29381 ufo del reg 0 0/SYSVa5723213
Epoll_ser 29381 ufo 0u CHR 136,11 0t0 14/dev/pts/11
Epoll_ser 29381 ufo 1u CHR 136, 11 0t0 14/dev/pts/11
Epoll_ser 29381 ufo 2u CHR 136, 11 0t0 14/dev/pts/11
Epoll_ser 29381 ufo 3u IPv4 570623 0t0 TCP localhost: 5555 (LISTEN)
Epoll_ser 29381 ufo 4u 0000 2860 0 anon_inode
After two clients connect to the server:
Ufo @ ufo :~ /Socket $ lsof-p 29381
Command pid user fd type device size/OFF NODE NAME
Epoll_ser 29381 ufo cwd DIR 4096 2225845/home/ufo/socket
Epoll_ser 29381 ufo rtd DIR 8, 1 4096 2/
Epoll_ser 29381 ufo txt REG 9830 2229560/home/ufo/socket/epoll_ser
Epoll_ser 29381 ufo mem REG 14768 1442612/lib/x86_64-linux-gnu/libdl-2.13.so
Epoll_ser 29381 ufo mem REG 1599504 1442606/lib/x86_64-linux-gnu/libc-2.13.so
Epoll_ser 29381 ufo mem REG 18888 1446570/lib/x86_64-linux-gnu/liblsp. so
Epoll_ser 29381 ufo mem REG 136936 1442618/lib/x86_64-linux-gnu/ld-2.13.so
Epoll_ser 29381 ufo del reg 0 0/SYSVa5723213
Epoll_ser 29381 ufo 0u CHR 136,11 0t0 14/dev/pts/11
Epoll_ser 29381 ufo 1u CHR 136, 11 0t0 14/dev/pts/11
Epoll_ser 29381 ufo 2u CHR 136, 11 0t0 14/dev/pts/11
Epoll_ser 29381 ufo 3u IPv4 570623 0t0 TCP localhost: 5555 (LISTEN)
Epoll_ser 29381 ufo 4u 0000 2860 0 anon_inode
Epoll_ser 29381 ufo 5u IPv4 573356 0t0 TCP localhost: 5555-> localhost: 39949 (CLOSE_WAIT)
Epoll_ser 29381 ufo 6u IPv4 576894 0t0 TCP localhost: 5555-> localhost: 39950 (CLOSE_WAIT)
View the file or socket of fd. You can view the relationship between FD and node name. The above example shows that FD is 3 for IPV4 listening socket; localhost: 5555-> localhost: 39905 (CLOSE_WAIT) line corresponds to a connection with FD 4, the Port Number of the client is 39905, and so on.
Note:
Lsof-p 29381
29381 indicates the process Number of the server program.
Of course, you can also view it through the method described on the following page, but it is still not convenient to use lsof.
Http://blog.csdn.net/itdisciple/article/details/6859507