系统编程之金城通信 fifo
来源:互联网 发布:南佛罗里达大学 知乎 编辑:程序博客网 时间:2024/04/27 15:21
/*fifo_read.c*/
#include<sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FIFO "/tmp/myfifo"
int main(int argc, char ** argv)
{
char buf_r[100];
int fd=-1;
int nread;
printf("Preparing for reading bytes...\n");
memset(buf_r,0,sizeof(buf_r));
/*打开管道*/
fd=open(FIFO,O_RDONLY|O_NONBLOCK,0);
if(fd==-1)
{
perror("open\n");
exit(1);
}
while(1)
{
memset(buf_r,0,sizeof(buf_r));
if((nread=read(fd, buf_r, 100))== -1)
{
if(errno == EAGAIN)
printf("no data yet\n");
}
printf("read %s from FIFO\n",buf_r);
sleep(1);
}
close(fd);//关闭管道
pause();//暂停,等待信号
unlink(FIFO);//删除文件
}
/*fifo_write.c*/
#include <unistd.h>#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FIFO_SERVER "/tmp/myfifo"
int main(int argc, char ** argv)
{
int fd;
char w_buf[100];
int nwrite,sum=0;
/*创建有名管道 mkfifo()除第一次成功外,其余都失败 error 为EEXIST*/
if((mkfifo(FIFO_SERVER,O_CREAT|O_EXCL|O_RDWR)<0) && (errno != EEXIST))
{
printf("cannot create fifoserver\n");
}
/*打开管道*/
fd=open(FIFO_SERVER,O_RDWR|O_NONBLOCK,0);
if(fd == -1)
{
perror("open\n");
exit(1);
}
/*入参检测*/
if(argc == 1)
{
printf("Please send something\n ");
exit(-1);
}
strcpy(w_buf,argv[1]);
/*向管道写入数据*/
if((nwrite=write(fd,w_buf,100))==-1)
{
if(errno == EAGAIN)
printf("The FIFO has not been read yet.Please try later\n");
}
else
printf("Write %s to the FIFO \n",w_buf);
sum+=sizeof(argv[1]);
printf("You have written %d bytes to myfifo\n",sum);
close(fd);//关闭管道
return 0;
}
- 系统编程之金城通信 fifo
- linux系统编程:进程间通信-fifo
- Linux系统编程:客户端-服务器用FIFO进行通信
- Linux环境编程之IPC进程间通信(三):FIFO
- 网络编程之FIFO
- Linux系统编程学习之《FIFO文件的应用》
- linux系统编程之管道(三):命令管道(FIFO)
- linux系统编程之管道(三):命令管道(FIFO)
- 进程通信之二FIFO
- 进程间通信之FIFO
- 进程间通信之FIFO
- linux进程通信之fifo
- linux 系统编程-学习笔记10--进程间通信--管道/FIFO/消息队列/
- Linux系统编程——进程间通信:命名管道(FIFO)
- Linux系统编程——进程间通信:命名管道(FIFO)
- Linux系统编程——进程间通信:命名管道(FIFO)
- Linux环境编程之IPC进程间通信(四):管道和FIFO比较
- 服务器编程之--用命名管道(FIFO)来做进程间通信
- MySql 数据库入门讲解
- 谁再说HDU1048是水题很简单我和他急=0=(1)—模拟字符串
- 栈的存储结构
- asp.net 服务器内置对象(application对在线人数统计)
- 好的程序员应该熟悉的几门编程语言 .
- 系统编程之金城通信 fifo
- 10-15Asp Application对象
- 如何给报表参数设置动态默认值
- 黑马程序员 String封装类
- Android中Handler、HandlerThread、AsyncTask的应用
- Linux下的select函数的应用
- 零碎记录
- 企业信息门户(EIP)定位与价值分析 .
- IOS5基础十五-----展示详情表和校验表