linux中利用有名管道实现进程之间的通信
来源:互联网 发布:薪酬优化 密件工资单 编辑:程序博客网 时间:2024/05/01 07:50
//把源文件中的数据读入到管道文件中
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int do_copy(int fd_s,int fd_d)
{
char buf[1024] = {0};
int ret = 0;
while((ret = read(fd_s,buf,sizeof(buf))) != 0)
write(fd_d,buf,ret);
return 0;
}
//./a.out src fifo
int main(int argc, const char *argv[])
{
int fd_s;
int fd_d;
//1.命令行参数的处理
if(argc != 3)
{
printf("Usage: %s src fifo\n",argv[0]);
exit(EXIT_FAILURE);
}
//创建有名管道
if(mkfifo(argv[2],0666) < 0 && errno != EEXIST)
{
perror("mkfifo fail");
exit(EXIT_FAILURE);
}
//后面文件的操作
//1.打开 --- open
//2.读写 --- read 、write
//3.关闭 close
//1.打开文件
fd_s = open(argv[1],O_RDONLY);
if(fd_s < 0)
{
perror("open fail");
exit(EXIT_FAILURE);
}
//目标文件是"管道文件" 此处对目标文件的操作是 只写方式
fd_d = open(argv[2],O_WRONLY);
//fd_d = open(argv[2],O_RDWR);
if(fd_d < 0)
{
perror("open fail");
exit(EXIT_FAILURE);
}
printf("write file\n");
//2.读写
do_copy(fd_s,fd_d);
//3.关闭
close(fd_s);
close(fd_d);
return 0;
}
从有名管道中获取数据写入到目标文件
#include <stdio.h>
#include <stdlib.h>#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int do_copy(int fd_s,int fd_d)
{
char buf[1024] = {0};
int ret = 0;
while((ret = read(fd_s,buf,sizeof(buf))) != 0)
{
printf("ret = %d\n",ret);
write(fd_d,buf,ret);
}
printf("read ret = %d\n",ret);
return 0;
}
//./a.out fifo dest
int main(int argc, const char *argv[])
{
int fd_s;
int fd_d;
//1.命令行参数的处理
if(argc != 3)
{
printf("Usage: %s fifo dest\n",argv[0]);
exit(EXIT_FAILURE);
}
if(mkfifo(argv[1],0666) < 0 && errno != EEXIST)
{
perror("mkfifo fail");
exit(EXIT_FAILURE);
}
//后面文件的操作
//1.打开 --- open
//2.读写 --- read 、write
//3.关闭 close
//1.打开文件 --- 源文件是管道文件 ,此处对管道文件的操作方式是 ,只读的
fd_s = open(argv[1],O_RDONLY);
// fd_s = open(argv[1],O_RDWR);
if(fd_s < 0)
{
perror("open fail");
exit(EXIT_FAILURE);
}
//目标文件
fd_d = open(argv[2],O_WRONLY|O_TRUNC|O_CREAT,0666);
if(fd_d < 0)
{
perror("open fail");
exit(EXIT_FAILURE);
}
//2.读写
do_copy(fd_s,fd_d);
//3.关闭
close(fd_s);
close(fd_d);
return 0;
}
一端用只读或是只写方式打开,运行程序时,程序阻塞在打开操作!直到另一端用相对的方式(只写或只读)方式打开时,程序才会解除阻塞继续往下执行!
- linux中利用有名管道实现进程之间的通信
- Linux下的有名管道(05)---使用两个管道实现两个进程之间的通信(对讲机模式)
- Linux下的有名管道(06)---使用两个管道实现两个进程之间的通信(手机模式)
- linux进程通信-有名管道
- linux进程通信--有名管道
- Linux进程通信 有名管道实现守护进程
- Linux进程通信(无名管道,有名管道,共享内存)的实现
- Linux进程间通信---有名管道 代码实现
- 利用命名管道实现进程之间的通信 .........
- Linux初学,利用共享内存,有名管道,select实现两个用户之间的自由对话。
- 利用有名管道,实现两个独立进程间的通讯
- 有名管道实现进程间通信。
- linux进程通信----FIFO(有名管道)
- linux进程间通信之有名管道
- 进程通信--有名管道
- 进程间的通信----有名管道fifo
- Linux利用管道实现进程间通信
- 1、利用多线程与有名管道技术,实现两个进程之间发送即时消息,实现聊天功能。
- 欢迎使用CSDN-markdown编辑器
- QTP中DataTable用例取值与循环
- Gradle详解-脚本基础知识
- 报表导出功能
- 如何使用Android Studio把自己的Android library分享到jCenter和Maven Central
- linux中利用有名管道实现进程之间的通信
- leafletjs 地图挂饼图
- Android 去除 bitmap 无用的白色边框
- ImagerLoader的原理
- java调用微信群发接口
- 自适应网页
- Swift - 进度条(UIProgressView)的用法
- 服务器启动org.apache.catalina.deploy.WebXml addFilter错误
- 枚举