Linux进程通信:命名管道FIFO小结
Linux进程通信:命名管道FIFO小结,有实例
Linux下进程之间通信可以用命名管道FIFO完成。命名管道是一种特殊类型的文件,因为Linux中所有事物都是文件,它在文件系统中以文件名的形式存在。
在程序中,我们可以使用两个不同的函数调用来建立管道:
#include <sys/types.h>
#include <sys/stat.h>
int mkfifo(const char *filename, mode_t mode);
int mknode(const char *filename, mode_t mode | S_IFIFO, (dev_t) 0 );
下面先来创建一个管道:
view plaincopy to clipboardprint?
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
int main()
{
int res = mkfifo("/tmp/my_fifo", 0777);
if (res == 0)
{
printf("FIFO created\n");
}
exit(EXIT_SUCCESS);
}
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
int main()
{
int res = mkfifo("/tmp/my_fifo", 0777);
if (res == 0)
{
Linux进程通信:命名管道FIFO小结,有实例
printf("FIFO created\n");
}
exit(EXIT_SUCCESS);
}
编译这个程序:
gcc –o fifo1.c fifo
运行这个程序:
$ ./fifo1
用ls命令查看所创建的管道
$ ls -lF /tmp/my_fifo
prwxr-xr-x 1 root root 0 05-08 20:10 /tmp/my_fifo|
注意:ls命令的输出结果中的第一个字符为p,表示这是一个管道。最后的|符号是由ls命令的-F选项添加的,它也表示是这是一个管道。
虽然,我们所设置的文件创建模式为“0777”,但它被用户掩码(umask)设置(022)给改变了,这与普通文件创建是一样的,所以文件的最终模式为755。
打开FIFO一个主要的限制是,程序不能是O_RDWR模式打开FIFO文件进行读写操作,这样做的后果未明确定义。这个限制是有道理的,因为我们使用FIFO只是为了单身传递数据,所以没有必要使用O_RDWR模式。如果一个管道以读/写方式打开FIFO,进程就会从这个管道读回它自己的输出。如果确实需要在程序之间双向传递数据,最好使用一对FIFO,一个方向使用一个。
当一个Linux进程被阻塞时,它并不消耗CPU资源,这种进程的同步方式对CPU而言是非常有效率的。
有关Linux下命名管道FIFO的读写规则可以参见之前所写的一篇文章:Linux命名管道FIFO的读写规则。
Linux进程通信:命名管道FIFO小结,有实例
一、实验:使用FIFO实现进程间通信
两个独立的程序:
1. 生产者程序,它在需要时创建管道,然后尽可能快地向管道中写入数据。
2. 消费者程序,它从FIFO中读取数据并丢弃它们。
生产者程序fifo2.c:
view plaincopy to clipboardprint?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#define FIFO_NAME "/tmp/Linux/my_fifo"
#define BUFFER_SIZE PIPE_BUF
#define TEN_MEG (1024 * 1024 * 10)
int main()
{
int pipe_fd;
int res;
int open_mode = O_WRONLY;
int bytes = 0;
char buffer[BUFFER_SIZE + 1];
if (access(FIFO_NAME, F_OK) == -1)
{
res = mkfifo(FIFO_NAME, 0777);
if (res != 0)
{
fprintf(stderr, "Could not create fifo %s\n", FIFO_NAME);
exit(EXIT_FAILURE);
}
}
Linux进程通信:命名管道FIFO小结,有实例
printf("Process %d opening FIFO O_WRONLY\n", getpid());
pipe_fd = open(FIFO_NAME, open_mode);
printf("Process %d result %d\n", getpid(), pipe_fd);
if (pipe_fd != -1)
{
while (bytes < TEN_MEG)
{
res = write(pipe_fd, buffer, BUFFER_SIZE);
if (res == -1)
{
fprintf(stderr, "Write error on pipe\n");
exit(EXIT_FAILURE);
}
bytes += res;
}
close(pipe_fd);
}
else
{
exit(EXIT_FAILURE);
}
printf("Process %d finish\n", getpid());
exit(EXIT_SUCCESS);
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#define FIFO_NAME "/tmp/Linux/my_fifo"
#define BUFFER_SIZE PIPE_BUF
#define TEN_MEG (1024 * 1024 * 10)
int main()
{
int pipe_fd;
int res;
int open_mode = O_WRONLY;
Linux进程通信:命名管道FIFO小结,有实例
int bytes = 0;
char buffer[BUFFER_SIZE + 1];
if (access(FIFO_NAME, F_OK) == -1)
{
res = mkfifo(FIFO_NAME, 0777);
if (res != 0)
{
fprintf(stderr, "Could not create fifo %s\n", FIFO_NAME);
exit(EXIT_FAILURE);
}
}
printf("Process %d opening FIFO O_WRONLY\n", getpid());
pipe_fd = open(FIFO_NAME, open_mode);
printf("Process %d result %d\n", getpid(), pipe_fd);
if (pipe_fd != -1)
{
while (bytes < TEN_MEG)
{
res = write(pipe_fd, buffer, BUFFER_SIZE);
if (res == -1)
{
fprintf(stderr, "Write error on pipe\n");
exit(EXIT_FAILURE);
}
bytes += res;
}
close(pipe_fd);
}
else
{
exit(EXIT_FAILURE);
}
printf("Process %d finish\n", getpid());
exit(EXIT_SUCCESS);
}
Linux进程通信:命名管道FIFO小结,有实例
消费者程序fifo3.c:
view plaincopy to clipboardprint?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <limits.h>
#include <sys/ …… 此处隐藏:10154字,全部文档内容请下载后查看。喜欢就下载吧 ……
相关推荐:
- [高等教育]一年级家长课程教案
- [高等教育]封丘县人民医院深入推进纠正医药购销领
- [高等教育]2017年6月大学英语四级真题试卷及答案(
- [高等教育]2017年北京第二外国语学院文学院824中
- [高等教育]7 高中历史第7单元1861年俄国农奴制改
- [高等教育]【K12学习】4、实际测量-苏教版六年级
- [高等教育]药具培训试卷题库及部分参考答案
- [高等教育]本土电子元器件目录分销商如何赢得生意
- [高等教育]七年级岭南版美术教案
- [高等教育]书作文之书法活动通讯稿
- [高等教育]Endnote X 软件使用入门和用法总结(LS)
- [高等教育]嵌入式系统的现状及发展状况
- [高等教育]2012抗菌药物专项整治活动方案解读
- [高等教育]人教版新课本一年级数学下册期末试卷
- [高等教育]爱课程民法学观后感
- [高等教育]930机组使用说明书1
- [高等教育]煤气设备设施点检标准
- [高等教育]常见室内观叶植物图解
- [高等教育]312党员群众路线心得体会
- [高等教育]小学信息(苗版)第一册全册教案
- 在市---局2010党建大会上的讲话
- 《科哲》提纲及补充阅读材料(2010.7)
- 苏州高博软件技术职业学院论文开题报告
- 兼职导游管理的困境及对策探讨
- 基于通用设计理念的现代厨房产品语义研
- 康乐一中2010年至2011年度鼓号队、花束
- 第10章_数据收集整理与描述_期末复习课
- 2008年黑龙江林甸商贸购物中心营销策划
- 水硬度的测定实验报告
- 五分钟教你拍摄夜景光绘照
- 2014年临床妇产科三基三严试题及答案
- 0第二课 纾解压力第一站了解压力
- 解析建筑工程电气设备安装施工技术要点
- 地方性应用型本科高校“双师型”师资队
- 高考语文专题复习课件:小说阅读指导
- 装饰工程投标书2
- 大学生就业难问题探讨及对策
- English and Its History
- 青岛市城市房屋修缮工程质量监督管理办
- 初中英语形容词和副词的用法和练习题




