Search code examples
socketsmmap

why is it not possible to use mmap with socket fd as an argument?


i know it's not possible, i'm trying to understand the true reason behind it OS wise


Solution

  • 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.