有名管道(FIFO)
# 父子进程之间示例
/* 有名管道(FIFO) 提供一个路径名与之关联,以FIFO的文件形式存在于文件系统中 读写操作和普通文件一样,常用于不存在关系的进程之间 注意事项: 读写进程只要有一端未打开,另一打开的一端就会阻塞在read或write处 当两端都打开,其中一端关闭时,另一端也停止 通过命令创建有名管道 mkfifo 名字 通过函数创建有名管道 #include <sys/types.h> #include <sys/stat.h> int mkfifo(const char *pathname, mode_t mode); 参数: pathname:路径 mode:权限,和open一样,八进制 返回值: 成功:0 失败:-1 */ // 父子进程之间 #include <sys/types.h> #include <sys/stat.h> #include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <unistd.h> #include <string.h> int main() { if(access("fifo1", F_OK) == 0) { remove("fifo1"); } int mkfifo_flag = mkfifo("fifo1", 0777); if(mkfifo_flag == -1) { perror("mkfifo"); exit(-1); } int fifofd = open("fifo1", O_RDWR); int fork_flag = fork(); if(fork_flag == 0) { char str[1024]; strcpy(str, "Hello World"); write(fifofd, str, strlen(str)); } else { char str[1024]; read(fifofd, str, sizeof(str)); printf("parent recv a info : %s \n", str); } }
# 无关联进程之间示例
write.c
// 无关系进程之间 #include <sys/types.h> #include <sys/stat.h> #include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <unistd.h> #include <string.h> int main() { if(access("fifo1", F_OK) == 0) { remove("fifo1"); } int mkfifo_flag = mkfifo("fifo1", 0777); if(mkfifo_flag == -1) { perror("mkfifo"); exit(-1); } int fifofd = open("fifo1", O_WRONLY); for(int i = 0; i < 10; i++) { char str[1024]; sprintf(str, "Hello : %d", i); write(fifofd, str, strlen(str)); sleep(1); } close(fifofd); return 0; }
read.c
// 无关系进程之间 #include <sys/types.h> #include <sys/stat.h> #include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <unistd.h> #include <string.h> int main() { // if(access("fifo1", F_OK) == 0) // { // remove("fifo1"); // } // int mkfifo_flag = mkfifo("fifo1", 0777); // if(mkfifo_flag == -1) // { // perror("mkfifo"); // exit(-1); // } int fifofd = open("fifo1", O_RDONLY); while(1) { char str[1024] = {0}; //读端接收字符串初始化为0,可防止输出乱码 int len = read(fifofd, str, sizeof(str)); // 写端关闭时,len == 0 if(len == 0) { printf("写端断开连接了......\n"); break; } printf("read file recve a info : %s\n", str); } close(fifofd); return 0; }
# 聊天功能示例
mark.c

// 聊天功能实现 /* 实现功能: 能够相互发送数据并接收,类似于QQ 实现原理: 两个管道 + 父子进程 因为要实现随时都能读取到对方发送的消息,而且自己还能随时发消息 因此父子进程其中一个负责读,并输出;另一个负责写 为了区分两人的写数据,因此需要两个管道 */ #include <sys/types.h> #include <sys/stat.h> #include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <unistd.h> #include <string.h> int main() { if(access("fifo1", F_OK) != 0) { int mkfifo_flag1 = mkfifo("fifo1", 0777); if(mkfifo_flag1 == -1) { perror("mkfifo"); exit(-1); } } if(access("fifo2", F_OK) != 0) { int mkfifo_flag2 = mkfifo("fifo2", 0777); if(mkfifo_flag2 == -1) { perror("mkfifo"); exit(-1); } } int fork_flag = fork(); if(fork_flag == 0) { int fifofd2 = open("fifo2", O_RDONLY); while(1) { char str[1024] = {0}; int len = read(fifofd2, str, sizeof(str)); if(len == 0) { printf("对方掉线..."); break; } printf("Jorn: %s\n", str); } close(fifofd2); } else { int fifofd1 = open("fifo1", O_WRONLY); char str[1024]; while(gets(str) != EOF) { write(fifofd1, str, strlen(str)); } close(fifofd1); } remove("fifo1"); remove("fifo2"); return 0; }
jorn.c
// 无关系进程之间 #include <sys/types.h> #include <sys/stat.h> #include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <unistd.h> #include <string.h> int main() { if(access("fifo1", F_OK) != 0) { int mkfifo_flag1 = mkfifo("fifo1", 0777); if(mkfifo_flag1 == -1) { perror("mkfifo"); exit(-1); } } if(access("fifo2", F_OK) != 0) { int mkfifo_flag2 = mkfifo("fifo2", 0777); if(mkfifo_flag2 == -1) { perror("mkfifo"); exit(-1); } } int fork_flag = fork(); if(fork_flag == 0) { int fifofd1 = open("fifo1", O_RDONLY); while(1) { char str[1024] = {0}; int len = read(fifofd1, str, sizeof(str)); if(len == 0) { printf("对方掉线..."); break; } printf("Mark: %s\n", str); } close(fifofd1); } else { int fifofd2 = open("fifo2", O_WRONLY); char str[1024]; while(gets(str) != EOF) { write(fifofd2, str, strlen(str)); } close(fifofd2); } remove("fifo1"); remove("fifo2"); return 0; }
自己选择的路,跪着也要走完。朋友们,虽然这个世界日益浮躁起来,只要能够为了当时纯粹的梦想和感动坚持努力下去,不管其它人怎么样,我们也能够保持自己的本色走下去。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
2021-04-29 [每日一题] leetcode 403. 青蛙过河
2019-04-29 Heap Partition ZOJ - 3963(贪心)