当前位置: 首页 > news >正文

linux命名管道的使用

linux系统中管道有两种,一种是匿名管道,只能在有血缘关系的进程间使用。一种是命名管道。可以使用在没有血缘关系进程之间。比如两个程序,一个负责向管道写入数据,另外一个负责读出数据。
试验非常方便,
负责读的程序 fifo_r.c

#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>int main() {int count = 0;const char *path =  "./fifo_file";int fd = open(path, O_RDONLY);   char buf[128]={0};for( count = 0; count < 10; count++){memset( buf, 0, sizeof(buf) );ssize_t n = read(fd, buf, sizeof(buf));printf("read:%s\n", buf); }close(fd);return 0;
}

负责写的程序 fifo_w.c.

#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>int main() {int count = 0;const char *path = "./fifo_file";char *str = "hello world camel";mkfifo(path, 0666);               int fd = open(path, O_WRONLY);for( count = 0; count < 10; count++){    write( fd, str, strlen(str) );usleep(50);}close(fd);return 0;
}

在这里插入图片描述
在这里插入图片描述

fifo_w.c向管道写入"hello world camel",   fifo_r.c从管道读出"hello world camel",写入十次,读出十次。
http://www.dtcms.com/a/365170.html

相关文章:

  • 关于linux数据库编程——sqlite3
  • Unity 中 打包 assetsBundle
  • C语言字符函数和字符串函数(1)
  • 《网络安全实战:CC攻击(应用层)与DDoS攻击(网络层)的底层逻辑与防御体系》​
  • 基于SpringBoot+Vue开发的环境保护监督管理网站
  • 如何通过控制台查看向量检索服务使用数据
  • Vue Router原理及SPA页面刷新解析
  • 融云:当我们谈论 AI 重构业务时,我们到底在谈论什么
  • SAM TTS网页官网入口 – 在线版微软tts在线语音合成助手
  • 【TRAE调教指南之MCP篇】FastMCP:快速制作自己的MCP服务
  • 对锁的总结
  • Agent 热潮遇冷?Manus 为何仍是 “版本神”
  • 充电枪结构设计-经验总结
  • 具身智能让人形机器人 “活” 起来:懂语言、能感知、会行动,智能进化再提速
  • docker安装rabbitmq(4.1.4-management)
  • 客户分层是什么?提升企业运营效率
  • 【python】运算符及语句
  • 数据结构:栈和队列(上)
  • 低代码革命遇瓶颈?这个“套娃神技“才是破局关键!
  • 【FastDDS】Layer DDS之Domain ( 05-Creating a DomainParticipant)
  • 关于linux网络编程——3
  • 扫地日记:有鹿巡扫机器人在景区被人类“调戏”的365天
  • ansible总结2
  • GIS大学课程表都长啥样?几个地信专业的大学一周课程表
  • 如何评价2025年数学建模国赛?
  • (二)文件管理-基础命令-pwd命令的使用
  • 高并发数据写入场景下 MySQL 的性能瓶颈与替代方案
  • “我店”积分模式的可持续性拷问:短剧能否撑起长期消泡沫需求?
  • 蓝桥杯算法之基础知识(6)
  • Python函数和方法类型注释