为什么不能将 mmap 与套接字 fd 作为参数一起使用?
我知道这是不可能的,我试图从操作系统角度了解其背后的真正原因
i know it's not possible, i'm trying to understand the true reason behind it OS wise
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
因为套接字的概念根本不映射到随机访问内存数组的概念,这是
mmap
为您提供的抽象。块设备(磁盘)上的文件通常允许随机读/写访问。这很好地映射到内存中的连续数组,这也为您提供了随机读/写访问。然而,套接字通常是面向流(或数据包/数据报)的。这意味着,通过套接字发送数据流,并从套接字接收数据流。但是,例如,您不能写入/读取打开的套接字流的第 N 个字节 - 这在概念上根本没有任何意义。
Because the concept of a socket simply doesn't map to the concept of a random-access in-memory array, which is the abstraction which
mmap
gives you. A file on a block-device (disk) usually allows random read/write access. This maps nicely to an in-memory contiguous array, which also gives you random read/write access.A socket, however, is usually stream (or packet/datagram) oriented. Meaning, a stream of data gets sent over the socket, and a stream of data is received from the socket. But you can't, for example, write/read to the Nth byte of an open socket stream - that simply doesn't make any sense conceptually.
事实上,Linux 中的某些协议系列是可能的,即:
对于其余协议映射未实现/不可能。例如 PF_INET
套接字的 mmap 系统调用在此处分派
另请参阅:
In fact it is possible with some protocol families in linux, namely:
For the rest of protocols mmapping is not implemented/possible. For example PF_INET
The mmap system call for socket gets dispatched here
See also:
有一个叫做数据包 mmap 的东西。在谷歌上搜索。 Elixir 维护中有程序示例,文档页面中有示例。只需在维护的内核源代码中搜索
程序链接即可查看
https: //elixir.bootlin.com/linux/v3.14.32/source/tools/testing/selftests/net
教程
https://www.kernel.org/doc/html/latest/ networking/packet_mmap.html
它是什么。将套接字 fd 与 mmap 一起使用。因此,将套接字映射到设备驱动程序中的 rx 和 tx 描述符。
要了解有关设备描述符的更多信息,您可以查看此代码 https://github.com/torvalds/linux/blob/master/drivers/net/ethernet/realtek/r8169_main.c以防有人好奇并偶然发现此页面和 Linux 设备驱动程序 pdf
There is whole thing called packet mmap. Search on google. There is program example in elixir maintained and doc page with example. Just search
Program link in kernel source maintained to view
https://elixir.bootlin.com/linux/v3.14.32/source/tools/testing/selftests/net
Tutorial
https://www.kernel.org/doc/html/latest/networking/packet_mmap.html
What is it. Use socket fd with mmap. So mapping socket to rx and tx descriptor in device driver.
To understand more about device descriptors u can look at this code https://github.com/torvalds/linux/blob/master/drivers/net/ethernet/realtek/r8169_main.c in case anyone is are curious and stumble upon this page and linux device drivers pdf