I'm currently interesting in UNIX system.
For IPC(Interprocess Communication), UNIX uses a file named socket
. I understand it works like server-client model, write-end and read-end uses socket file to communicate each other.
But I wonder how socket internally works. Each process designate a socket file(maybe with inode
), then write and read on it? If it's true, can I watch raw data(or encapsulated data) if I'm watching this socket file?
One more, socket file has a format? For example, some application socket file like mysql.sock
and docker.sock
, they have their own format?
CodePudding user response:
The proper name for unix sockets is Unix Domain Sockets, because they all reside within one computer. In a sense, sockets are a network that is entirely contained within the kernel; rather than using network interfaces to send data, that same data can be sent directly between programs
Despite creating files on disk, Unix sockets don’t actually write the data they send to the disk, as that would be far too slow. Instead, all the data is retained within kernel memory; the only point of the socket file is to maintain a reference to the socket, and to give it filesystem permissions to control access. For example, MySQL’s socket is usually at:
/var/lib/mysql/mysql.sock
This file doesn’t contain anything, and you shouldn’t modify it directly, except for the permissions where applicable. It’s just a name.
CodePudding user response:
Sockets don't contain anything. They are kind of like a "hole", that applications have access to for reading and writing.
Unix sockets create less overhead and communication is faster, than by localhost IP sockets.
Using sockets is a more efficient means of getting the job done, but generally only used by heavy I/O apps (like mailer daemons).