1. Pipes
- Features: Half-duplex, used only for related processes (e.g., parent-child), exists in memory, operable via read/write.
- Core Function: int pipe(int pipefd[2]); (pipefd[0] for reading, pipefd[1] for writing).
- Example Key Points: Create a pipe, then fork a child process. Parent writes and child reads (or vice versa), with irrelevant ends closed.
2. Named Pipes (FIFO)
- Features: Allows communication between unrelated processes, associated with a path, exists as a special file.
- Core Function: int mkfifo(const char *pathname, mode_t mode);
- Key Characteristics: By default, read and write ends wait for each other to open; non-blocking mode is available.
- Example Key Points: Create FIFO first, then open in read/write mode via open to achieve inter-process data transmission.
3. Message Queues
- Features: Linked list of messages in the kernel, accessible by type, persists after process termination.
- Core Functions:
-
- Create/obtain: int msgget(key_t key, int flag);
-
- Send/receive: int msgsnd(int msqid, const void *ptr, size_t size, int flag);, int msgrcv(int msqid, void *ptr, size_t size, long type, int flag);
- Example Key Points: Define message structure (including type and data), associate queues via key values, send and receive messages by type.
4. Shared Memory
- Features: Fastest IPC method, multiple processes share a memory area, requiring synchronization mechanisms (e.g., semaphores).
- Core Functions:
-
- Create/obtain: int shmget(key_t key, size_t size, int flag);
-
- Attach/detach: void *shmat(int shm_id, const void *addr, int flag);, int shmdt(void *addr);
- Example Key Points: Processes obtain shared memory via the same key, read/write directly to the memory area, and release it after use.
5. Signals
- Features: Software interrupt mechanism, used to notify processes of events (e.g., ctrl+c triggers SIGINT).
- Handling Methods: Ignore (some signals cannot be ignored), catch (custom handler), default action.
- Core Functions:
-
- Basic version: sighandler_t signal(int signum, sighandler_t handler); (register handler), int kill(pid_t pid, int sig); (send signal).
-
- Advanced version: sigaction (supports additional data transmission), sigqueue (send signal with data).
6. Semaphores
- Features: Counters for process synchronization and mutual exclusion, do not store data, often used with shared memory.
- Core Functions:
-
- Create/obtain: int semget(key_t key, int num_sems, int sem_flags);
-
- Operation: int semop(int semid, struct sembuf semoparray[], size_t numops); (PV operations)
- Example Key Points: Control the order of process access to shared resources through PV operations (P decreases by 1 to acquire resources, V increases by 1 to release resources).
7. Summary
- Pipes/FIFO: Simple but slow; pipes are limited to related processes, FIFO supports any processes.
- Message Queues: Access by type, no need for synchronization, but with limited capacity.
- Shared Memory: Fastest, requiring synchronization mechanisms.
- Signals: Used for event notification, capable of transmitting simple data.
- Semaphores: Only for synchronization, not for data transmission.