linux编程---进程间通信---FIFO---有名管道
来源:互联网 发布:php支持mysql 编辑:程序博客网 时间:2024/04/29 11:41
无名管道是临时的,在完成通信后将自动消失,且只能在具有亲缘关系的进程间实现通信。
有名管道FIFO有效地克服了这一问题,它是一个存在的特殊文件,可以在不同进程间通信。
管道是单向的,要实现双向,需要两个管道。
创建有名管道
int mkfifo (const char *FILENAME, mode_t MODE)
使用示例
非亲缘关系的两个进程使用有名管道实现数据传输的实例程序。
写进程将要发送的数据发送到有名管道,读进程从有名管道中读取发送端发送的数据。
发送端程序
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<fcntl.h>
#include<limits.h>
#include<assert.h>
#include<sys/types.h>
#include<sys/stat.h>
#define FIFO_NAME "myfifo"
int main(int argc,char* argv[])
{
int pipefd;
int res;
char buf[20];
assert(argc == 2);
bzero(buf,20);
strcpy(buf,argv[1]);
if(access(FIFO_NAME,F_OK) == -1)
{
res = mkfifo(FIFO_NAME,0766);
if(res != 0)
{
fprintf(stderr,"could not create fifo %s\n",FIFO_NAME);
exit(EXIT_FAILURE);
}
}
pipefd = open(FIFO_NAME,O_WRONLY);
if(pipefd != -1)
{
res = write(pipefd,buf,strlen(buf));
if(res == -1)
{
fprintf(stderr,"write error %s\n",FIFO_NAME);
exit(EXIT_FAILURE);
}
printf("write data is %s,%d bytes\n",buf,res);
close(pipefd);
}
else
exit(-1);
exit(0);
}
读取端程序
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<fcntl.h>
#include<limits.h>
#include<assert.h>
#include<sys/types.h>
#include<sys/stat.h>
#define FIFO_NAME "myfifo"
int main(int argc,char* argv[])
{
int pipefd;
int res;
char buf[20];
bzero(buf,20);
pipefd = open(FIFO_NAME,O_RDONLY);
if(pipefd != -1)
{
res = read(pipefd,buf,sizeof(buf));
if(res == -1)
{
fprintf(stderr,"write error %s\n",FIFO_NAME);
exit(EXIT_FAILURE);
}
printf("read data is %s,%d bytes\n",buf,res);
close(pipefd);
}
else
exit(-1);
exit(0);
}
- linux编程---进程间通信---FIFO---有名管道
- Linux--进程间通信(管道及有名管道FIFO)
- Linux--进程间通信(管道及有名管道FIFO)
- Linux--进程间通信(管道及有名管道FIFO)
- linux进程通信----FIFO(有名管道)
- linux进程间通信-有名管道(FIFO)
- linux进程间通信-有名管道(FIFO)
- Linux进程间通信二--有名管道FIFO
- Linux进程间通信——有名管道 FIFO 详解
- linux 进程编程:有名管道fifo
- 进程间通信______有名管道(FIFO)
- 进程间的通信----有名管道fifo
- 进程通信-有名管道FIFO
- Linux--进程间通信(管道及有名管道FIFO)(转)
- Linux--进程间通信(管道及有名管道FIFO)(转)
- Linux进程间通信(一)之无名管道(PIPE)和有名管道(FIFO)
- linux进程通信--有名管道(FIFO)(含实例代码)
- Linux进程间的通信——有名管道fifo与守护进程deamon
- LOG4J介绍
- HotSpot虚拟机对象探秘
- 将tensorflow网络模型(图+权值)保存为.pb文件,并从.pb文件中还原网络模型
- 静态代码块、代码块、构造方法的执行顺序
- 使用函数查找字符串子串
- linux编程---进程间通信---FIFO---有名管道
- UEDITOR编辑器
- markdown语法总结
- 把远程仓库的项目,clone到eclipse里面
- 强连通分量 tarjan算法 C++实现
- centos7 下的常用命令
- Android 开源交流 github [https://github.com/aosp-exchange-group/share]
- poj 2240
- 用C/C++实现较完整贪吃蛇游戏