Advanced IPC
Advanced Interprocess Communication (IPC) extends the basic socket interface by providing efficient communication mechanisms between processes running on the same operating system. UNIX Domain Sockets support local communication and enable features such as file descriptor passing that are not available through Internet sockets.
Learning Objectives
- Understand Advanced IPC.
- Learn UNIX Domain Sockets.
- Study socket pairs.
- Understand local socket addressing.
- Learn
socketpair(). - Prepare for descriptor passing.
1. What is Advanced IPC?
Advanced IPC provides communication mechanisms that extend traditional pipes and network sockets. These mechanisms are optimized for communication between processes executing on the same system and provide additional capabilities beyond ordinary data transfer.
2. UNIX Domain Sockets
UNIX Domain Sockets provide bidirectional communication between local processes without using network protocols. Communication occurs entirely within the operating system.
3. UNIX Domain vs Internet Sockets
| Feature | UNIX Domain | Internet Socket |
|---|---|---|
| Communication Scope | Local System | Network |
| Protocol | Local IPC | TCP / UDP |
| Address Type | Filesystem Path | IP Address + Port |
| Typical Domain | AF_UNIX |
AF_INET |
4. Socket Pair
A socket pair consists of two directly connected sockets. Data written to one socket can be read from the other, providing full-duplex communication.
5. socketpair()
The POSIX socketpair() function creates a pair
of connected sockets in a single system call.
| Prototype |
|---|
int socketpair(int domain,
int type,
int protocol,
int sv[2]);
|
6. UNIX Domain Address
UNIX Domain Sockets use filesystem pathnames as addresses. The pathname identifies the local communication endpoint.
| Structure | Purpose |
|---|---|
sockaddr_un |
UNIX Domain Socket Address. |
7. Socket Pair Workflow
8. Practical POSIX Example
The program creates two connected UNIX Domain sockets using
socketpair().
9. Interview Questions
- What is Advanced IPC?
- What is a UNIX Domain Socket?
- How does a UNIX Domain Socket differ from an Internet socket?
- What is a socket pair?
- What does
socketpair()return? - Which address family is used for UNIX Domain Sockets?
- What identifies a UNIX Domain Socket endpoint?
- Why are UNIX Domain Sockets used for local IPC?
- Can socket pairs communicate in both directions?
- What address structure is used with UNIX Domain Sockets?
10. File Descriptor Passing
UNIX Domain Sockets support the transfer of open file descriptors between processes. Instead of sending the file itself, the sending process passes a reference to an already opened file descriptor.
11. Ancillary Data
Ancillary data is additional control information transmitted along with normal application data. It enables processes to exchange metadata such as file descriptors through UNIX Domain Sockets.
12. sendmsg()
The POSIX sendmsg() function transmits both
application data and optional ancillary data.
It provides greater flexibility than send() by
allowing multiple buffers and control messages.
| Prototype |
|---|
ssize_t sendmsg(int sockfd,
const struct msghdr *msg,
int flags);
|
13. recvmsg()
The POSIX recvmsg() function receives application
data together with any accompanying ancillary data.
It is commonly paired with sendmsg().
| Prototype |
|---|
ssize_t recvmsg(int sockfd,
struct msghdr *msg,
int flags);
|
14. struct msghdr
The msghdr structure describes all information
required for message transmission or reception.
| Member | Description |
|---|---|
msg_name |
Socket address. |
msg_iov |
Data buffers. |
msg_iovlen |
Number of buffers. |
msg_control |
Ancillary data buffer. |
msg_controllen |
Control data length. |
msg_flags |
Receive status flags. |
15. struct cmsghdr
The cmsghdr structure represents one ancillary
(control) message contained within the control buffer.
It describes the type and size of the control information.
16. CMSG_* Macros
| Macro | Purpose |
|---|---|
CMSG_FIRSTHDR() |
Locate the first control message. |
CMSG_NXTHDR() |
Locate the next control message. |
CMSG_DATA() |
Access control data. |
CMSG_LEN() |
Calculate message length. |
CMSG_SPACE() |
Calculate required buffer space. |
17. Descriptor Passing Workflow
18. Practical POSIX Example
The program transmits a message described by the
msghdr structure using
sendmsg().
19. Linux Commands
| Command | Description |
|---|---|
ss -x |
Display UNIX Domain sockets. |
ss -xl |
Display listening UNIX Domain sockets. |
lsof -U |
List open UNIX Domain sockets. |
ls -l /tmp |
View pathname-based socket files. |
file socket_name |
Display socket file type. |
20. Interview Questions
- What is file descriptor passing?
- Why is descriptor passing limited to UNIX Domain Sockets?
- What is ancillary data?
- Why are
sendmsg()andrecvmsg()needed? - What is the purpose of
struct msghdr? - What information does
struct cmsghdrcontain? - What is the purpose of
CMSG_DATA()? - Why is
CMSG_SPACE()used? - Can multiple control messages be transmitted together?
- How does descriptor passing differ from transmitting file contents?
21. Passing Process Credentials
In addition to ordinary data, UNIX Domain Sockets can communicate process-related information. This enables a receiving process to verify information about the sender when supported by the operating system.
22. SCM_RIGHTS
SCM_RIGHTS is a standardized ancillary message
type used to transfer one or more open file descriptors
between processes over a UNIX Domain Socket.
The receiving process obtains new file descriptors that refer
to the same underlying open file descriptions.
23. SCM_CREDENTIALS (Linux Extension)
SCM_CREDENTIALS is a Linux-specific ancillary
message used to exchange process credentials such as the
process identifier (PID), user identifier (UID), and group
identifier (GID).
This interface is not defined by POSIX.
24. Scatter/Gather Messaging
Scatter/Gather messaging allows data to be transmitted or
received using multiple buffers during a single message
operation.
The msghdr structure references these buffers
through an array of iovec structures.
25. struct iovec
Each iovec structure describes one memory buffer
used for scatter/gather input or output.
| Member | Description |
|---|---|
iov_base |
Starting address of the buffer. |
iov_len |
Size of the buffer in bytes. |
26. Scatter/Gather Workflow
Multiple memory buffers are processed as a single logical message without requiring them to be copied into one contiguous buffer.
27. Practical POSIX Example
The example defines two buffers using
struct iovec. These buffers can later be
referenced by a struct msghdr when calling
sendmsg() or recvmsg().
28. POSIX vs Linux
| Feature | Category |
|---|---|
SCM_RIGHTS |
POSIX |
sendmsg() |
POSIX |
recvmsg() |
POSIX |
struct iovec |
POSIX |
SCM_CREDENTIALS |
Linux Extension |
29. Linux Commands
| Command | Description |
|---|---|
ss -x |
Display UNIX Domain sockets. |
lsof -U |
List UNIX Domain sockets opened by processes. |
ls -l /var/run |
View common UNIX Domain socket files. |
stat socket_file |
Display information about a socket file. |
find /tmp -type s |
Locate UNIX Domain socket files. |
30. Interview Questions
- What is
SCM_RIGHTS? - Why is
SCM_RIGHTSuseful? - What information can be transferred using
SCM_CREDENTIALS? - Is
SCM_CREDENTIALSstandardized by POSIX? - What is scatter/gather messaging?
- What is the purpose of
struct iovec? - Why are multiple buffers useful during message transmission?
- How does
sendmsg()use aniovecarray? - What is the difference between application data and ancillary data?
- Which advanced IPC features are Linux-specific?
31. UNIX Domain Socket Communication Flow
A server creates a UNIX Domain Socket, waits for connection requests, exchanges data and optional ancillary information, and closes the communication channel when processing is complete.
32. File Descriptor Passing Workflow
The receiving process obtains its own file descriptor that references the same open file description as the sender.
33. Best Practices
- Always verify the return value of every IPC function.
- Close all unused socket descriptors.
- Validate ancillary data before using it.
- Transfer only descriptors that are required.
- Release resources promptly after communication.
- Use POSIX interfaces whenever portability is required.
- Use Linux-specific extensions only when necessary.
34. Common Errors
| Error | Description |
|---|---|
| Ignoring return values | Failures remain undetected. |
| Descriptor leaks | Open descriptors consume system resources. |
| Improper control buffer size | Ancillary data may be truncated. |
| Using Internet sockets for descriptor passing | Descriptor passing requires UNIX Domain Sockets. |
| Not removing pathname socket files | Old socket files may prevent future binding. |
35. APUE / POSIX vs Linux
| Feature | Category |
|---|---|
socketpair() |
POSIX |
sendmsg() |
POSIX |
recvmsg() |
POSIX |
SCM_RIGHTS |
POSIX |
AF_UNIX |
POSIX |
SCM_CREDENTIALS |
Linux Extension |
| Abstract Namespace Sockets | Linux Extension |
36. Module Summary
This module introduced advanced interprocess communication using UNIX Domain Sockets. It covered socket pairs, local socket addressing, descriptor passing, ancillary data, message-oriented communication, scatter/gather I/O, and credential passing. These mechanisms enable efficient communication between processes executing on the same system.
37. Key Takeaways
- UNIX Domain Sockets provide efficient local IPC.
socketpair()creates two connected sockets.sendmsg()andrecvmsg()support ancillary data.SCM_RIGHTStransfers open file descriptors.struct msghdrdescribes complete messages.struct cmsghdrrepresents control messages.struct iovecenables scatter/gather messaging.SCM_CREDENTIALSis a Linux-specific extension.- Portable applications should rely on POSIX-defined interfaces.
- Proper descriptor management prevents resource leaks.
Concept Mastery Assessment — Module 62
Verify your conceptual understanding with 12 targeted technical assessment questions. Instant feedback and explanations are provided for every choice.