tsFifoRd.c
#include<unistd.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define FIFO_PATH "/tmp/fifo1"
int init_fifo(void)
{
//创建管道
if(access(FIFO_PATH, F_OK|W_OK ) != 0 )
{
if(mkfifo(FIFO_PATH, 0666 ) != 0 )
{
perror("mkfifo error ");
return -1 ;
}
}
//打开管道文件
int fd_fifo = open(FIFO_PATH, O_RDWR);
if (-1 == fd_fifo)
{
perror("open fifo error ");
return -1 ;
}
return fd_fifo;
}
int fifo_open(int fd_fifo)
{
fd_fifo = init_fifo();
if (-1 == fd_fifo)
{
printf(" init error \n");
return -1 ;
}
return fd_fifo;
}
int main()
{
int fd_fifo;
fd_fifo=init_fifo();
if(fd_fifo==-1)
{
printf("open fifo fail!\n");
return -1;
}
char buf[1024];
read(fd_fifo,buf,sizeof(buf));
printf("msg come from fifo: %s\n",buf);
return 0;
}
tsFifoWr.c
#include<unistd.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define FIFO_PATH "/tmp/fifo1"
int init_fifo(void)
{
//创建管道
if(access(FIFO_PATH, F_OK|W_OK ) != 0 )
{
if(mkfifo(FIFO_PATH, 0666 ) != 0 )
{
perror("mkfifo error ");
return -1 ;
}
}
//打开管道文件
int fd_fifo = open(FIFO_PATH, O_RDWR);
if (-1 == fd_fifo)
{
perror("open fifo error ");
return -1 ;
}
return fd_fifo;
}
int fifo_open(int fd_fifo)
{
fd_fifo = init_fifo();
if (-1 == fd_fifo)
{
printf(" init error \n");
return -1 ;
}
return fd_fifo;
}
int main()
{
int fd_fifo;
fd_fifo=init_fifo();
if(fd_fifo==-1)
{
printf("open fifo fail!\n");
return -1;
}
char *msg="hello fifo!\n";
write(fd_fifo,msg,strlen(msg));
return 0;
}


Leave a Reply