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:

cat file.txt | grep ctf{

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

Definition: A record stored on disk, or a record synthesized on demand by a file server, which can be accessed by multiple processes.

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 (|)

Definition: A unidirectional data channel using standard input and output. Data written to the write-end of the pipe is buffered by the operating system until it is read from the read-end of the pipe. Two-way communication between processes can be achieved by using two pipes in opposite "directions".

Consider the following example:

cat file.txt | grep ctf{

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)

Definition: A pipe that is treated like a file. Instead of using standard input and output as with an anonymous pipe, processes write to and read from a named pipe, as if it were a regular file.

Yes, FIFO stands for First-In-First-Out. This pipe is pretty much the same as a queue. Create a named pipe:

mkfifo my_pipe

This is a file with type p:

Write to this pipe:

echo 'ctf{this_is_a_flag}' > my_pipe

The shell will hang. Now spawn another shell and read data from this pipe:

cat < my_pipe

Message Queue

Definition: A data stream similar to a socket, but which usually preserves message boundaries. Typically implemented by the operating system, they allow multiple processes to read and write to the message queue without being directly connected to each other.

Shared Memory

Definition: Multiple processes are given access to the same block of memory, which creates a shared buffer for the processes to communicate with each other.

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

Definition: A system message sent from one process to another, not usually used to transfer data but instead used to remotely command the partnered process.

kill -l

  • ctrl+c: SIGINT,

  • ctrl+z: SIGTSTP,

kill -9 <PID>

The only asynchronous IPC method between two processes.

Socket

Definition: Data sent over a network interface, either to a different process on the same computer or to another computer on the network. Stream-oriented (TCP; data written through a socket requires formatting to preserve message boundaries) or more rarely message-oriented (UDP, SCTP).

Different host on a network

int socket(int domain, int type, int protocal)

  • 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