Linux进程通信之FIFO如何实现
发表于:2024-09-22 作者:千家信息网编辑
千家信息网最后更新 2024年09月22日,这篇文章给大家分享的是有关Linux进程通信之FIFO如何实现的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。FIFO通信(first in first out)FIFO 有
千家信息网最后更新 2024年09月22日Linux进程通信之FIFO如何实现
这篇文章给大家分享的是有关Linux进程通信之FIFO如何实现的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
FIFO通信(first in first out)
FIFO 有名管道,实现无血缘关系进程通信。
创建一个管道的伪文件
a.mkfifo testfifo 命令创建
b.也可以使用函数int mkfifo(const char *pathname, mode_t mode);
内核会针对fifo文件开辟一个缓冲区,操作fifo文件,可以操作缓冲区,实现进程间通信-实际上就是文件读写
man 3 mkfifo
#include#include int mkfifo(const char *pathname, mode_t mode);
注意事项:
FIFOs
Opening the read or write end of a FIFO blocks until the other end is also opened (by another process or thread). See
fifo(7) for further details.
打开fifo文件时候,read端会阻塞等待write端open,write端同理,也会阻塞等待另外一段打开。
代码示例:
file_w.c 写端
#include#include #include #include #include #include #include #include int main(int argc, char *argv[]) { if(argc != 2) { printf("./a.out filename1\n"); return -1; } printf("begin open w\n"); int o_ret = open(argv[1], O_WRONLY); printf("end open w\n"); char buf[256]; int num = 0; while (1) { memset(buf, '\0', sizeof(buf)); sprintf(buf, "xiaoming--%d", num++); printf("strlen(buf) = %d\n", strlen(buf)); write(o_ret, buf, strlen(buf)); sleep(1); } close(o_ret); return 0;}
file_r.c 读端
#include#include #include #include #include #include #include #include int main(int argc, char *argv[]) { if(argc != 2) { printf("./a.out filename1\n"); return -1; } printf("begin open r\n"); int o_ret = open(argv[1], O_RDONLY); printf("end open r\n"); char buf[256]; int num = 0; while (1) { memset(buf, '\0', sizeof(buf)); read(o_ret, buf, sizeof(buf)); printf("strlen(buf) = %d\n", strlen(buf)); printf("read is%s\n", buf); } close(o_ret); return 0;}
什么是Linux系统
Linux是一种免费使用和自由传播的类UNIX操作系统,是一个基于POSIX的多用户、多任务、支持多线程和多CPU的操作系统,使用Linux能运行主要的Unix工具软件、应用程序和网络协议。
感谢各位的阅读!关于"Linux进程通信之FIFO如何实现"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
通信
文件
进程
系统
操作系统
内容
更多
管道
篇文章
缓冲区
缓冲
阻塞
不错
实用
有名
自由
事项
代码
任务
内核
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
太平洋证券数据库系统
开出的软件开发如何做账
网络安全对用户应急的预案
阿里云服务器 app
操作系统底层数据库
渲染管理软件开发
伊春职业学院计算机网络技术专业
远景能源软件开发中心
目前财务软件开发商
北京公司软件开发公司
软件开发会计分录怎么写
南京3D软件开发公司
刀塔怎么匹配中国服务器
网络安全相关的留言
数据库怎么认证
南京新品服务器服务商
网络安全参与者的思想动态
什么是设计数据库可的起点
计算机网络技术的中级证书
今易软件如何添加数据库
服务器与网络机柜一样吗
腾讯云服务器哪里看id
网络技术与应用心得体会免费
对网络安全法的解读
智慧教育软件开发商 山东
贵州通讯软件开发服务参考价格
java数据库模型
国家网络安全法的看法
停车场数据库删除了还能恢复吗
中级软件开发职称考试