一、引言

  FIFO常被称为有名管道,不同于管道(pipe)。pipe仅适用于“有血缘关系”的IPC。但FIFO还可以应用于不相关的进程的IPC。实际上,FIFO是Linux基础文件类型中的一种,是在读写内核通道。

函数原型:

int mkfifo(const char *pathname, mode_t mode); 

成功返回 0, 失败返回 -

命令:

mkfifo + 管道名   例:mkfifo fifo_one

操作步骤:

1) 通过命令行创建fifo;

2) 使用open、close、read、write等I/O函数操作fifo。

二、例程

1) 创建fifi:

#mkfifio fifo_one

2) fifo 写函数

 #include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h> void sys_err(char *str)
{
perror(str);
exit(-);
}
int main(int argc, char *argv[])
{
int fd, i;
char buf[]; if (argc < ) {
printf("Please enter: ./a.out fifoname\n");
return -;
}
fd = open(argv[], O_WRONLY);//打开fifo
if (fd < )
sys_err("open"); i = ;
while () {
       printf("write fifo \n ");
9  sprintf(buf, "hello itcast %d\n", i++); //格式化输出到buf
write(fd, buf, strlen(buf)); //向fifo写入buf
sleep();
}
close(fd); return ;
}

3) fifo 读函数

 #include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h> void sys_err(char *str)
{
perror(str);
exit();
} int main(int argc, char *argv[])
{
int fd, len;
char buf[]; if (argc < ) {
printf("Please enter: ./a.out fifoname\n");
return -;
}
fd = open(argv[], O_RDONLY);//打开fifo
if (fd < )
sys_err("open");
while () {
len = read(fd, buf, sizeof(buf));//从fifo读取数据到buf
write(STDOUT_FILENO, buf, len); //将buf写入标准输出
sleep(); //多個读端时应增加睡眠秒数,放大效果.
}
close(fd);
return ;
}

编译执行:




最新文章

  1. PhpStorm 2016.3 For Mac 重大里程碑更新 -- 终于解决了不能输入中文标点符号的重大bug
  2. html和css的编码规范
  3. 【cruch bang】中切换成左手鼠标
  4. Lucene/ElasticSearch 学习系列 (1) 为什么学,学什么,怎么学
  5. Backbone
  6. insert into select 堵塞update
  7. PHP实现单击“添加”按钮增加一行表单项,并将所有内容插入到数据库中
  8. asp.net MVC2.0学习笔记
  9. C++ 顺序容器 vector list deque 之比较
  10. (简单) POJ 3264 Balanced Lineup,RMQ。
  11. Eclipse中GIT插件更新工程到之前版本
  12. 51nod_1120:机器人走方格 V3
  13. adb pull 报错处理:adb: error: cannot create file/directory &#39;E:\&#39;: No such file or directory
  14. Logical Volume Manager (LVM)
  15. centos7 yum安装ffmpeg,以及ffmpeg的简单用法
  16. linux 查看信息-磁盘分区&amp;网络
  17. maven +bootstrap+ssm
  18. 分布式监控系统Zabbix-3.0.3--短信报警设置
  19. SQL Server 如何添加删除外键、主键,以及更新自增属性
  20. 编译 php-memcache 扩展时提示Cannot find autoconf

热门文章

  1. 跨平台移动开发_PhoneGap 使用Geolocation基于所在地理位置坐标调用百度地图API
  2. 使用weinre调试Web应用及PhoneGap应用
  3. PHP:数字转Excel列头
  4. 杂谈spring、springMVC
  5. java:工具类
  6. 安全隐患,你对X-XSS-Protection头部字段理解可能有误
  7. elenium2学习(十六)-- 富文本(自动发帖)
  8. Android开发最佳学习路线图(转)
  9. 了解git /github
  10. 怎样在linux下编写C程序并编译执行