IPC
Motivation: Sharing Resource
Each process has its own address space. The resource of a process can only be visited by itself and the kernel. However, oftentime we want to share resource between multiple processes, for example:
Here grep
needs to access the resource own by cat
(the content of file.txt
), and the anonymous pipe is the most common form of Inter-Process Communication (IPC).
File
IPC with file is easy to understand: processA writes to a file and then processB reads from this file. The cons are obvious:
Files live on the disk. I/O operations are extremely slow.
Access control must be solid, otherwise there might be information leak.
Pipes
Anonymous Pipes (|
)
|
)Consider the following example:
Here |
is a pipe. Since it is ephemeral, we don't assign a name to it, so it is called an anonymous pipe. This pipe is one-way: the output of cat file.txt
is the input of grep ctf{
. If we want two-way operations, we need a named pipe (FIFO).
Behind the sceen, the shell calls fork()
twice to handle cat file.txt and grep ctf{ respectively. That is, cat file.txt
is the child process of the shell, grep ctf{
is the child process of the shell, and cat file.txt
does not share any resource with grep ctf{
since they are two independent processes. The anonymous pipe |
is some caching mechanism in the kernel. It connects cat file.txt
and grep ctf{
by storing the output of cat file.txt
in the cache of the kernel and then feeding this content to grep ctf{
as input.
Named Pipes (FIFO)
Yes, FIFO stands for First-In-First-Out. This pipe is pretty much the same as a queue. Create a named pipe:
This is a file with type p
:
Write to this pipe:
The shell will hang. Now spawn another shell and read data from this pipe:
Message Queue
Shared Memory
Shared memory means mapping the virtual addresses of processA and processB to the same physical address. Once processA writes something into memory, processB is able to obtain the content immediately.
Semaphore
Signal
ctrl+c: SIGINT,
ctrl+z: SIGTSTP,
The only asynchronous IPC method between two processes.
Socket
Different host on a network
domain
type
protocol
The Socket API
Python's socket module provides an interface to the Berkeley sockets API. This is the module that you’ll use in this tutorial. The primary socket API functions and methods in this module are:
socket()
.bind()
.listen()
.accept()
.connect()
.connect_ex()
.send()
.recv()
.close()
We will learn the socket API by writing c
Write an Echo Server
Write an Echo Client
Reference
Last updated
Was this helpful?