8-ipc
8-ipc
Mythili Vutukuru
CSE, IIT Bombay
Why inter-process communication
• Application logic in a single system is often distributed across multiple
processes: why?
• Different processes developed independently by different teams
• Different programming languages and frameworks used for different tasks
• Processes in a system do not share any memory with each other by
default, so how do they communicate information with each other?
• Cannot share variables or data structures in programs across processes
• Parent and child have identical but separate memory images after fork, changes
made in one process and not seen by other
• Inter-process communication (IPC) mechanisms, available via operating
system syscalls, allow processes to exchange information
Example: web application architecture
• Example: web applications typically composed of multiple processes
• Web server process handles HTTP (web) requests/responses
• Written in a language like C/C++ for high performance
• Returns responses for static content directly by reading files from disk
• Requests needing dynamic response are handled by application server
• App server parses HTTP requests, generates HTTP response according to the
business logic specified by user, sends response back to client via web server
• Scripting languages may be used for easy text parsing and manipulation
• Application server stores/retrieves app data in a database
• Several web application frameworks available to build web applications
having such architectures, e.g., Python Django, React etc.
IPC mechanisms
• Unix domain sockets: processes open sockets, send and receive
messages to each other via socket system calls
• Message queues: sender posts a message to a mailbox, receiver
retrieves message later on from mailbox
• Pipes: unidirectional communication channel between two processes
• Shared memory: same physical memory frame mapped into virtual
address space of multiple processes in order to share memory
• Signals: specific messages via kill system call
• Different IPC mechanisms are useful in different scenarios
Sockets
• Sockets = abstraction to communicate between two processes
• Each process opens socket, and pair of sockets can be connected
• Client-server paradigm: one process opens socket first (server) and another
process connects its socket to the first one (client)
• One process writes a message into one socket, another process can read it,
and vice versa (bidirectional communication)
• Processes can be in same machine or on different machines
• If processes on same machine, messages stored temporarily in OS memory
before delivering to destination process
• If processes on different machines, messages sent over network
Types of sockets (1)
• Unix domain (local) sockets are used to communicate between
processes on the same machine
• Internet sockets are used to communicate between processes in
different machines
• Local sockets identified by a pathname, Internet sockets identified by
IP (Internet Protocol) address and port number
• Client and server sockets differentiated by who starts first and who
connects later: server sockets started first on a well-known “address”,
client process connects to server using the server address
Types of sockets (2)
• Connection-based sockets: one client socket and one server socket
are explicitly connected to each other
• After connection, the two sockets can only send and receive messages to
each other
• Connection-less sockets: one socket can send/receive messages
to/from multiple other sockets
• Address of other endpoint can be mentioned on each message
• Type of socket (local or internet, connection-oriented or connection-
less) is specified as arguments to system call that creates sockets
• In this course: Unix domain sockets, connection-less
Creating a socket sockfd = socket(…)
bind(sockfd, address)
fd 0 fd 0
STDIN
fd 1 STDOUT fd 1
File descriptor STDERR
fd 2 File descriptor
table of parent fd 2
table of child
fd 3 fd 3
PIPE
fd 4
fd 4 BUFFER
Pipes in shell commands $cat foo.txt | grep something
• How does shell run commands with pipes (output of one command
given as input to another command)?
• Shell opens a pipe, shared with child processes that run commands
• Shell duplicates stdout of first child to write end of pipe, read end of
pipe to stdin of second process
• Processes must close file descriptors they are not using
STDOUT of P1 STDIN of P2
Process P1 Process P2
Pipe buffer
Named pipes
• How to use pipes between unrelated processes? Named pipes
• Named pipes opened with a pathname, accessible across processes
• One process accesses read end of pipe, another opens write end
• Named pipe also provides uni-directional communication
• Writing to pipe with no reader open will throw an error
Writer
mkfifo(name, ..)
Reader fd = open(name, O_WRONLY)
fd = open(name, O_RDONLY)
write(fd, message, …)
read(fd, message, ..)
Blocking vs. non-blocking IPC
• Same high level concept across sockets, pipes, message queues
• Sender sends message, temporarily stored in some memory inside OS
• Receiver retrieves message later on from temporary OS memory
• Send/receive system calls can block
• Sender can block if temporary buffer is full
• Receiver can block if temporary buffer is empty
• Possible to configure IPC to be non-blocking using syscalls
• Send/receive will return with error instead of blocking
Shared memory shmid = shmget(key, ..)
char *data = shmat(shmid, ..)