[国嵌攻略][081][有名管道通讯]
有名管道
有名管道又称为FIFO文件,因此我们对有名管道的操作可以采用文件操作的方法,如使用open,read,write等。
FIFO文件的特点
1.读取FIFO文件的进程只能以RDONLY方式打开FIFO文件。
2.写入FIFO文件的进程只能以WRONLY方式打开FIFO文件。
3.FIFO文件里面的内容被读取后就消失了,但普通文件里面的内容被读取后还存在。
wFIFO.c
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> void main(){ //创建管道 mkfifo("fifo", 0777); //打开管道 int fd; fd = open("fifo", O_WRONLY); //写入数据 char buf[13] = "hello world!"; write(fd, buf, 13); //关闭管道 close(fd); }
rFIFO.c
#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> void main(){ //打开管道 int fd; fd = open("fifo", O_RDONLY); //读取数据 char buf[13]; read(fd, buf, 13); //显示数据 printf("%s\n", buf); //关闭管道 close(fd); //删除管道 unlink("fifo"); }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步