UnitySpriteShaders颜色调整与边缘光:创意视觉效果实现指南
2026/6/13 10:37:55
目录
一、Linux IPC 的核心种类
1.古老的通信方式
2.IPC 对象通信(system v 标准)
3.socket 通信
二、无名管道(匿名管道)
1. 核心特性
2.读写行为规则
3.编程顺序
4.核心函数:pipe
5.示例代码:父子进程通过无名管道通信
三、有名管道
1.核心特性
2.编程顺序
3.核心函数:mkfifo
4.示例代码:两个无亲缘进程通过 FIFO 通信
四、无名管道与有名管道核心对比
五、总结
是 Linux 早期的基础 IPC 实现,包括:
基于内核对象的通信方式,常用的有:
支持跨主机的网络级通信,核心场景是:
注:管道的底层实现是队列,数据遵循 “先进先出” 规则。
无名管道对应 pipe,是仅支持亲缘关系进程(如父子、兄弟进程)通信的方式。
管道的读写逻辑受缓冲区(默认 64K)和两端状态影响:
创建管道 → fork子进程 → 读写管道 → 关闭管道
int pipe(int pipefd[2]);#include <stdio.h> #include <unistd.h> #include <string.h> #include <sys/wait.h> int main() { int pipe_fd[2]; pid_t pid; char read_buf[1024] = {0}; const char *write_data = "Hello from parent (pipe)!"; // 1. 创建无名管道 if (pipe(pipe_fd) == -1) { perror("pipe create failed"); return -1; } // 2. fork创建子进程(建立亲缘关系) pid = fork(); if (pid == -1) { perror("fork failed"); return -1; } // 3. 父进程:关闭读端,向管道写数据 if (pid > 0) { close(pipe_fd[0]); // 父进程仅写,关闭读端 write(pipe_fd[1], write_data, strlen(write_data)); close(pipe_fd[1]); // 写完关闭写端 wait(NULL); // 等待子进程执行完毕 } // 4. 子进程:关闭写端,从管道读数据 else { close(pipe_fd[1]); // 子进程仅读,关闭写端 read(pipe_fd[0], read_buf, sizeof(read_buf)); printf("Child read: %s\n", read_buf); close(pipe_fd[0]); // 读完关闭读端 } return 0; }有名管道对应 fifo,突破了亲缘关系限制,支持任意单机进程通信(只要知道管道的文件路径),且在文件系统中可见(有明确的路径名称)。
创建有名管道 → 打开有名管道 → 读写管道 → 关闭管道 → 卸载有名管道
int mkfifo(const char *pathname, mode_t mode);#include <stdio.h> #include <unistd.h> #include <fcntl.h> #include <sys/stat.h> #include <string.h> #define FIFO_PATH "/tmp/my_fifo" int main() { // 1. 创建FIFO文件(已存在则忽略) if (mkfifo(FIFO_PATH, 0666) == -1) { perror("mkfifo failed (ignore if exist)"); } // 2. 打开FIFO文件(写模式) int fd = open(FIFO_PATH, O_WRONLY); if (fd == -1) { perror("open fifo failed"); return -1; } // 3. 向FIFO写数据 const char *msg = "Hello from FIFO write process!"; write(fd, msg, strlen(msg)); printf("Write to FIFO: %s\n", msg); // 4. 关闭并卸载FIFO close(fd); unlink(FIFO_PATH); return 0; }#include <stdio.h> #include <unistd.h> #include <fcntl.h> #include <sys/stat.h> #include <string.h> #define FIFO_PATH "/tmp/my_fifo" int main() { char buf[1024] = {0}; // 1. 打开FIFO文件(读模式,阻塞等待写端) int fd = open(FIFO_PATH, O_RDONLY); if (fd == -1) { perror("open fifo failed"); return -1; } // 2. 从FIFO读数据 read(fd, buf, sizeof(buf)); printf("Read from FIFO: %s\n", buf); // 3. 关闭FIFO close(fd); return 0; }| 对比维度 | 无名管道(pipe) | 有名管道(FIFO) |
|---|---|---|
| 通信范围 | 仅亲缘关系进程 | 任意单机进程 |
| 文件系统可见性 | 不可见(内核维护) | 可见(有路径的特殊文件) |
| 打开特性 | fork 后继承文件描述符 | 一端未打开时 open 默认阻塞 |
| 生命周期 | 随进程退出自动释放 | 需手动 unlink 删除 |
| 创建方式 | pipe () 函数 | mkfifo () 函数 |