Linux环境下管道FIFO传输
来源:互联网 发布:淘宝网店店主 编辑:程序博客网 时间:2024/06/06 11:47
本例用到3个文件:创建管道、往管道里写数据、从管道里读数据。
fifo_write.c
#include <sys/types.h>#include <sys/stat.h>#include <errno.h>#include <stdio.h>#include <fcntl.h>#include <unistd.h>#include <stdlib.h>#include <limits.h>#define BUFES PIPE_BUFint main(void){int fd ;int n, i ;char buf[BUFES];printf("BUFFS is :%d",PIPE_BUF);if((fd=open("fifo1",O_WRONLY))<0){ /*以写打开一个FIFO1*/perror("open");exit(1);}for(i=0;i<100;i++){buf[i]=i;}if(write(fd,buf,100)){perror("write");close(fd);exit(1);}close(fd);exit(0);}fifo_read.c
#include <sys/types.h>#include <sys/stat.h>#include <unistd.h>#include <stdio.h>#include <stdlib.h>#include <limits.h>#include <fcntl.h>#include <unistd.h>#define BUFES PIPE_BUFint main(void){int fd;int len,j;char buf[BUFES];mode_t mode = 0666; /* FIFO文件的权限 */if((fd=open("fifo1",O_RDONLY))<0) /* 打开FIFO文件 */{perror("open");exit(1);}while((len=read(fd,buf, BUFES))>0) /* 开始进行通信 */{for(j=0;j<len;j++){printf("read_fifo read: %d",buf[j]);}close(fd); /* 关闭FIFO文件 */exit(0);}}
创建FIFO
fifo.sh
#!/bin/bashfile="/home/nb/test/fifo1"if [ -p "$file" ];thenrm -rf fifo1rm -rf 0666fimkfifo fifo1 0666echo "mkfifo fifo1 successful"./fifo_writeecho "start fifo_write...."执行顺序:先创建管道,执行:./fifo.sh
然后写数据:./fifo_write可以在后台也可以重新开终端读数据。
最后:./fifo_read读数据。
注意:fifo默认旗标为必须写完之后才能读。而且读一次后fifo管道中清空,没有数据。所以每次读完后再读管道中是没有数据的(阻塞等待新的数据写入)。
0 0
- Linux环境下管道FIFO传输
- linux 有名管道FIFO
- linux fifo 有名管道
- Linux命名管道FIFO
- 【Linux】命名管道FIFO
- Linux 命名管道FIFO
- Linux环境进程间通信(一)管道和FIFO
- Linux环境进程间通信(一)管道和FIFO
- Linux下进程间通信机制:FIFO(命名管道)
- Linux下进程间通信机制:FIFO(命名管道)
- Linux下通过FIFO传输大文件(test.264)
- linux 有名管道(FIFO)
- linux 有名管道(FIFO)
- linux 有名管道(FIFO)
- linux 有名管道(FIFO)
- FIFO管道通信(linux)
- 【Linux进程通信】管道,FIFO
- linux 无名管道和有名管道fifo
- [推荐算法]ItemCF,基于物品的协同过滤算法
- (4.1.12)Android高效加载大图、多图解决方案,有效避免程序OOM
- win7下git 合并工具p4merge的配置使用
- linux下tar.xz文件的解压方法
- UIImage相关知识点总结
- Linux环境下管道FIFO传输
- Android系统自带样式(android:theme)详解
- linux 如何查看防火墙是否开启
- 成大事必备9种能力、9种手段、9种心态
- java.lang.NoSuchMethodError: org.json.JSONArray.remove
- C++的explicit关键字
- (4.1.13)Android之Notification的多种用法
- Angularjs ng-options用法
- MQTT 协议 Client ID 长度不能超过23个字符(MqttException: MQIsdp ClientId > 23 bytes)