This is fifo_read.c
#include
#include
#include
#include
#include
#include
#include
#include
#define FIFO "/home/xiang/code/fifo/myfifo"
main(int argc,char *argv[])
{
char buf_r[100];
int fd;
int nread;
if((mkfifo(FIFO,O_CREAT|O_EXCL)<0)&&(errno!=EEXIST))
printf("this is mkfifo errno \n");
printf("........\n");
memset(buf_r,0,sizeof(buf_r));
fd=open(FIFO,O_RDONLY|O_NONBLOCK,066);
if(fd==-1)
{
perror("open");
exit(1);
}
while(1)
{
memset(buf_r,0,sizeof(buf_r));
if((nread=read(fd,buf_r,100))==-1)
{
if(errno==EAGAIN)
printf("no any data yet\n");
}
printf("read %s from FIFO\n",buf_r);
sleep(1);
}
pause();
unlink(FIFO);
}
This is fifo_write.c
#include
#include
#include
#include
#include
#include
#include
#include
#define FIFO_SERVER "/home/xiang/code/fifo/myfifo"
main(int argc ,char * argv[])
{
int fd;
char buf_w[100];
int nwrite;
if(fd==-1)
if(errno==ENXIO)
printf("open error\n");
fd=open(FIFO_SERVER,O_WRONLY | O_NONBLOCK,0);
if(argc==1)
printf("please input your message\n");
strcpy(buf_w,argv[1]);
printf("%s\n",argv[1]);
printf("%s\n",buf_w);
if((nwrite=write(fd,buf_w,100))==-1)
{
if(errno==EAGAIN)
printf("write error\n");
else
printf("%s ok\n",buf_w);
}
else
{
printf("write %s to the FIFO_SERVER\n",buf_w);
}
}
注意所建的myfifo 文件的权限,否则不能读取。
阅读(546) | 评论(0) | 转发(0) |