MockExams

Unix Domain Protocols: Local Socket IPC & Descriptor Passing

AI draft, not yet reviewedUnit 9: Unix Domain ProtocolAbout 1 minutes

Idea in plain words

Unix Domain Sockets (AF_LOCAL or AF_UNIX) provide high-performance inter-process communication on the same physical host.

Explanation

When client and server processes reside on the same Unix machine, communicating over loopback (127.0.0.1) incurs unnecessary network stack processing. Unix domain sockets use standard socket semantics but route data directly through kernel buffers using a designated filesystem path as the rendezvous point. Binding creates a socket file on disk (mode 's'). Additionally, sendmsg() with ancillary data (SCM_RIGHTS) allows a privileged master process to open a file or network socket and securely hand the open descriptor to an unprivileged worker child.

Key points

  • Uses struct sockaddr_un containing sun_family (AF_LOCAL) and sun_path (filesystem pathname).
  • Bypasses the entire TCP/IP network protocol stack, avoiding checksum calculation and IP routing overhead.
  • socketpair() creates an unnamed pair of connected bidirectional Unix domain sockets with a single system call.
  • Unix domain sockets uniquely support passing open file descriptors between unrelated processes via sendmsg() and recvmsg().

Example

Example

Binding a Unix domain server: struct sockaddr_un addr; addr.sun_family = AF_LOCAL; strncpy(addr.sun_path, '/tmp/app.sock', sizeof(addr.sun_path)-1); unlink('/tmp/app.sock'); bind(fd, (struct sockaddr *)&addr, sizeof(addr));

Code examples

#include <sys/socket.h>
#include <sys/un.h>

// socketpair creates two connected bi-directional sockets
int sv[2];
if (socketpair(AF_LOCAL, SOCK_STREAM, 0, sv) < 0) {
    perror("socketpair");
}
// sv[0] and sv[1] can now read/write to each other across processes

Exam questions

  • [7 Marks] What are Unix Domain Sockets? Explain the structure sockaddr_un and how socketpair() creates bidirectional IPC channels.
  • [5 Marks] Explain the mechanism of passing file descriptors across processes using Unix domain sockets.

BIT401CO

Switch Course

On this page

Past Exam Questions

Not asked in past exams yet

This topic hasn't appeared in previous exam papers.

Search BIT401CO

Reading

Theme
Reading mode
Text size
Default
Font
Line width
Line spacing