How to view which FD and corresponding file or socket operations are opened by a process

Source: Internet
Author: User
Tags chr

/********************************************************************* * Author:samson * date:06/22/2015 * Test PL   Atform: * GCC (Ubuntu 4.8.2-19ubuntu1) 4.8.2 * GNU bash, 4.3.11 (1)-release (X86_64-PC-LINUX-GNU) * *******************************************************************/

Under GNU Linux, using native sockets to write the server terminal program and client program, according to the general, the server program will first create a socket and listen to this port, and the client program is mainly on the server side of the connection, when only the service-side program is opened, Using lsof you can see the description of all open files on the server side, the process of the server-side program is 29016, as follows:

[Email protected]:~/socket$ lsof-p 29016

COMMAND PID USER FD TYPE DEVICE size/off NODE NAME

Oldser 29016 UFO cwd DIR 8,1 4096 2225845/home/ufo/socket

Oldser 29016 UFO RTD DIR 8,1 4096 2/

Oldser 29016 UFO txt REG 8,1 8203 2224940/home/ufo/socket/oldser

Oldser 29016 UFO mem REG 8,1 14768 1442612/lib/x86_64-linux-gnu/libdl-2.13.so

Oldser 29016 UFO mem REG 8,1 1599504 1442606/lib/x86_64-linux-gnu/libc-2.13.so

Oldser 29016 UFO mem REG 8,1 18888 1446570/lib/x86_64-linux-gnu/liblsp.so

Oldser 29016 UFO mem REG 8,1 136936 1442618/lib/x86_64-linux-gnu/ld-2.13.so

Oldser 29016 UFO DEL REG 0,4 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)


Can see open 5555 this port for listening;


After using the client to connect Port 5555:

[Email protected]:~/socket$ lsof-p 29016

COMMAND PID USER FD TYPE DEVICE size/off NODE NAME

Oldser 29016 UFO cwd DIR 8,1 4096 2225845/home/ufo/socket

Oldser 29016 UFO RTD DIR 8,1 4096 2/

Oldser 29016 UFO txt REG 8,1 8203 2224940/home/ufo/socket/oldser

Oldser 29016 UFO mem REG 8,1 14768 1442612/lib/x86_64-linux-gnu/libdl-2.13.so

Oldser 29016 UFO mem REG 8,1 1599504 1442606/lib/x86_64-linux-gnu/libc-2.13.so

Oldser 29016 UFO mem REG 8,1 18888 1446570/lib/x86_64-linux-gnu/liblsp.so

Oldser 29016 UFO mem REG 8,1 136936 1442618/lib/x86_64-linux-gnu/ld-2.13.so

Oldser 29016 UFO DEL REG 0,4 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 there is a line called name Anon_inode, so what does this mean?

After I have added a epoll to the service-side program, after starting the service-side program again, there is an FD such as Anon_inode, such as:

[Email protected]:~/socket$ lsof-p 29381

COMMAND PID USER FD TYPE DEVICE size/off NODE NAME

Epoll_ser 29381 UFO cwd DIR 8,1 4096 2225845/home/ufo/socket

Epoll_ser 29381 UFO RTD DIR 8,1 4096 2/

Epoll_ser 29381 UFO txt REG 8,1 9830 2229560/home/ufo/socket/epoll_ser

Epoll_ser 29381 UFO mem REG 8,1 14768 1442612/lib/x86_64-linux-gnu/libdl-2.13.so

Epoll_ser 29381 UFO mem REG 8,1 1599504 1442606/lib/x86_64-linux-gnu/libc-2.13.so

Epoll_ser 29381 UFO mem REG 8,1 18888 1446570/lib/x86_64-linux-gnu/liblsp.so

Epoll_ser 29381 UFO mem REG 8,1 136936 1442618/lib/x86_64-linux-gnu/ld-2.13.so

Epoll_ser 29381 UFO DEL REG 0,4 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 0,9 0 2860 Anon_inode


After two clients connect to the service end:

[Email protected]:~/socket$ lsof-p 29381

COMMAND PID USER FD TYPE DEVICE size/off NODE NAME

Epoll_ser 29381 UFO cwd DIR 8,1 4096 2225845/home/ufo/socket

Epoll_ser 29381 UFO RTD DIR 8,1 4096 2/

Epoll_ser 29381 UFO txt REG 8,1 9830 2229560/home/ufo/socket/epoll_ser

Epoll_ser 29381 UFO mem REG 8,1 14768 1442612/lib/x86_64-linux-gnu/libdl-2.13.so

Epoll_ser 29381 UFO mem REG 8,1 1599504 1442606/lib/x86_64-linux-gnu/libc-2.13.so

Epoll_ser 29381 UFO mem REG 8,1 18888 1446570/lib/x86_64-linux-gnu/liblsp.so

Epoll_ser 29381 UFO mem REG 8,1 136936 1442618/lib/x86_64-linux-gnu/ld-2.13.so

Epoll_ser 29381 UFO DEL REG 0,4 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 0,9 0 2860 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)


To see what file or socket FD is, you can see the corresponding relationship by looking at the FD column and the node Name column. In the above example, we can see that the FD is 3 IPV4, the localhost:5555->localhost:39905 (close_wait) line corresponds to the FD 4 connection, the client port number is 39905, and so on.


Description

Lsof-p 29381


Where 29381 is the process number of the service-side program.


Of course, it can be viewed by the methods described in the following pages, but it is not lsof convenient.

http://blog.csdn.net/itdisciple/article/details/6859507



How to view which FD and corresponding file or socket operations are opened by a process

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.