linux一切皆文件之Unix domain socket描述符(二)
一、知识准备
1、在linux中,一切皆为文件,所有不同种类的类型都被抽象成文件(比如:块设备,socket套接字,pipe队列)
2、操作这些不同的类型就像操作文件一样,比如增删改查等
3、主要用于:运行在同一台机器上的2个进程相互之间的数据通信
4、它们和网络文件描述符非常相似(比如:tcp socket),他们的通信发生在操作系统内核
二、环境准备
组件 | 版本 |
---|---|
os | centos linux release 7.5.1804 |
三、unix domain socket 文件描述符
先准备2个脚本:
server.py主要用于建立客户端的连接请求,并且接收客户端传来的数据,然后将收到的数据回传给客户端
client.py每隔1秒向服务端发送一次'hello world'
server.py:
import socket server_addr = '/tmp/server.sock' sock = socket.socket(socket.af_unix, socket.sock_stream) sock.bind(server_addr) sock.listen(0) while true: conn, clientaddr = sock.accept() while true: data = conn.recv(100) conn.sendall(data)
client.py:
import socket import time server_addr = '/tmp/server.sock' sock = socket.socket(socket.af_unix, socket.sock_stream) sock.connect(server_addr) while true: message = 'hello world!' sock.sendall(message) sock.recv(100) time.sleep(1) sock.close()
先看下server.py的状态:
[root@localhost ~]# python /tmp/server.py & [1] 2554 [root@localhost ~]# ls -l /proc/2554/fd total 0 lrwx------ 1 root root 64 nov 5 02:39 0 -> /dev/pts/0 lrwx------ 1 root root 64 nov 5 02:39 1 -> /dev/pts/0 lrwx------ 1 root root 64 nov 5 02:39 2 -> /dev/pts/0 lrwx------ 1 root root 64 nov 5 02:39 3 -> socket:[28724] [root@localhost ~]# grep 28724 /proc/net/unix ffff90d8ba564000: 00000002 00000000 00010000 0001 01 28724 /tmp/server.sock [root@localhost ~]# lsof -n | grep 28724 python 2554 root 3u unix 0xffff90d8ba564000 0t0 28724 /tmp/server.sock [root@localhost ~]# netstat -anp | grep 28724 unix 2 [ acc ] stream listening 28724 2554/python /tmp/server.sock
进程2554创建了打开了unix domain socket描述符(3 -> socket:[19803]
),并且通过该描述符,打开了/tmp/server.sock文件,其主要作用是用于监听
我们运行client.py并观察状态
[root@localhost ~]# python /tmp/client.py & [2] 2555 [root@localhost ~]# ls -l /proc/2555/fd total 0 lrwx------ 1 root root 64 nov 5 02:39 0 -> /dev/pts/0 lrwx------ 1 root root 64 nov 5 02:39 1 -> /dev/pts/0 lrwx------ 1 root root 64 nov 5 02:39 2 -> /dev/pts/0 lrwx------ 1 root root 64 nov 5 02:39 3 -> socket:[28728] [root@localhost ~]# grep 28728 /proc/net/unix ffff90d8b95b0400: 00000003 00000000 00000000 0001 03 28728 [root@localhost ~]# lsof -n | grep 28728 python 2555 root 3u unix 0xffff90d8b95b0400 0t0 28728 socket
与server.py的行为差不多。client.py也创建了unix domain socket描述符3 -> socket:[28728]
,通过socket:[18974]
,找到一条socket
查看server.py发生的变化:
[root@localhost ~]# ls -l /proc/2554/fd total 0 lrwx------ 1 root root 64 nov 5 02:39 0 -> /dev/pts/0 lrwx------ 1 root root 64 nov 5 02:39 1 -> /dev/pts/0 lrwx------ 1 root root 64 nov 5 02:39 2 -> /dev/pts/0 lrwx------ 1 root root 64 nov 5 02:39 3 -> socket:[28724] lrwx------ 1 root root 64 nov 5 02:39 4 -> socket:[28725]
server.py新增了一个4 -> socket:[28725]
,这是刚才client.py连接成功之后server.py新打开的描述符
[root@localhost ~]# lsof -n | grep -e '28728|28724|28725' python 2554 root 3u unix 0xffff90d8ba564000 0t0 28724 /tmp/server.sock python 2554 root 4u unix 0xffff90d8b95b0000 0t0 28725 /tmp/server.sock python 2555 root 3u unix 0xffff90d8b95b0400 0t0 28728 socket [root@localhost ~]# netstat -anp | grep unix | grep -e '28728|28724|28725' unix 2 [ acc ] stream listening 28724 2554/python /tmp/server.sock unix 3 [ ] stream connected 28725 2554/python /tmp/server.sock unix 3 [ ] stream connected 28728 2555/python
到目前为止,整个unix domain socket的通信过程已经比较清晰的展现了:
● server.py启动之后,打开监听的描述符,等待来自客户端的连接请求
● client.py启动之后,与server连接成功,打开一个描述符用于与server.py通信
● server.py会再打开一个描述符用于与client.py进行数据通信
但是目前还有2个问题:
(1)/tmp/server.sock到底作用是什么
(2)server与client是怎么进行数据通信的
问题(1)
● /tmp/server.sock是操作系统的实体文件,拥有一个全局的文件系统描述符,这个描述符在操作系统中是唯一的
● server.py启动时打开了server.sock,就声名了与server.py建立连接就只能通过server.sock文件
● 这就相当于tcp socket中四元组中的两元(server_ip:server_port
)
问题(2)
我们来使用strace命令看看server.py的内核调用
[root@localhost tmp]# strace -p 2554 strace: process 2554 attached recvfrom(4, "hello world!", 100, 0, null, null) = 12 sendto(4, "hello world!", 12, 0, null, 0) = 12 recvfrom(4, "hello world!", 100, 0, null, null) = 12 sendto(4, "hello world!", 12, 0, null, 0) = 12
recvfrom(4, "hello world!", 100, 0, null, null) = 12
sendto(4, "hello world!", 12, 0, null, 0) = 12
server.py在接收客户端数据的时候,使用了 4 -> socket:[28725]
这个文件描述符
再看client.py的内核调用
[root@localhost tmp]# strace -p 2555 strace: process 2555 attached select(0, null, null, null, {0, 996991}) = 0 (timeout) sendto(3, "hello world!", 12, 0, null, 0) = 12 recvfrom(3, "hello world!", 100, 0, null, null) = 12 select(0, null, null, null, {1, 0}) = 0 (timeout) sendto(3, "hello world!", 12, 0, null, 0) = 12
recvfrom(3, "hello world!", 100, 0, null, null) = 12
sendto(3, "hello world!", 12, 0, null, 0) = 12
client.py在与server.py通信的时候使用了 3 -> socket:[28728]
结论:
● server.py与client.py连接建立成功之后,都会各自在自己的进程下打开unix domain socket描述符,该描述符来指向对应的socket内存空间(下面简称s_mem
)
● client.py通过3 -> socket:[28728],找到s_mem
,然后写入数据hello world!
● server.py通过4 -> socket:[28725]
,找到s_mem
,读取数据hello world!
,并且原封不动的发送这串数据给client.py
● client.py通过读取s_mem
,获取从server.py传来的数据
● 循环往复
client.py server.py +---------------+ +---------------+ |pid:2555 | |pid:2554 | | +-----+ | | +-----+ | | |fd:3 | | | |fd:4 | | | +-----+ | | +-----+ | +---------------+ +---------------+ | | user space | | +---------------------------------------------------------------------+ kernel space | | | | v v +--------------+ +--------------+ |socket:[28728]| |socket:[28725]| +------+-------+ +------+-------+ | | | | v v +------------------------------------+ | socket | +------------------------------------+
四、小结
● /tmp/server.sock作为建立unix domain socket连接的唯一标识符
● unix domain socket连接建立完成之后在内存开辟一块空间,而server与client在这块内存空间中进行数据传输
● 在同一台机器上的进程通信,unix domain socket比tcp socket更快,因为它不需要网络协议栈,不需要打包拆包、计算校验和、维护序号和应答等等过程
五、参考资料
https://en.wikipedia.org/wiki/unix_domain_socket
至此,本文结束
在下才疏学浅,有撒汤漏水的,请各位不吝赐教...