Ryan G. answered 05/23/23
Systems Programming and Computer Architecture
The `shutdown()` system call is used to close an end of the socket for reading, writing, or both. Shutting down one or both halves of a duplex socket are part of the socket protocol:
- If the write half of a socket is shut down, reading from the other end will return 0 after all pending data is read. This is how an "end of file" is passed to a reader.
- If the read half of a socket is shut down, writing to the other end will cause an EPIPE error, and the process will receive a SIGPIPE signal.
This is independent of calling `close()` to actually destroy the file descriptor and associated data in the kernel's task struct for the process, which must be done to free the resource. A call to close the last open file descriptor for a socket before cleanly shutting it down may also result in an "emergent shutdown", such as sending a RST packet instead of performing a FIN-ACK-FIN-ACK handshake.
One important thing to keep in mind is that shutting down a socket affects the socket itself, while close applies only to that particular file descriptor; if the file descriptor has been duplicated in some way, then closing it will not affect the socket open on other file descriptors. However, shutting it down will affect all duplicated file descriptors for that socket.