C++实现
fifo_read.c
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define FIFO "/tmp/myfifo1"
int main(int argc,char* argv[])
{
char buf_r[100];
int fd;
int nread;
if(access(FIFO,F_OK) == -1) {
if(mkfifo(FIFO,0777) == -1 && (errno != EEXIST)) //O_CREAT|O_EXCL|
{
printf("cannot create fifoserver\n");
}
}
printf("preparing for reading byte... \n");
fd = open(FIFO,O_RDONLY|O_NONBLOCK,0);//|O_NONBLOCK
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 data yet\n");
}
printf("read %s from FIFO\n",buf_r);
sleep(1);
}
pause();
unlink(FIFO);
return 0;
}
fifo_write.c
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define FIFO_SERVER "/tmp/myfifo2"
typedef struct STRUWriteFifo
{
unsigned int iCol;
unsigned int iRow;
}struFifo;
int main(int argc,char* argv[])
{
char buf_w[25] = {0};
int fd;
int nwrite;
fd = open(FIFO_SERVER,O_WRONLY|O_NONBLOCK);
if(argc == 1)
printf("Please send something \n");
strcpy(buf_w,argv[1]);
struFifo struWriteFifo;
struWriteFifo.iCol = 2;
struWriteFifo.iRow = 3;
nwrite = write(fd,(char*)&struWriteFifo,sizeof(struWriteFifo));
// nwrite = write(fd, buf_w,sizeof(buf_w));
if(nwrite == -1)
{
if(errno == EAGAIN)
printf("The FIFO has not been read yet.Please try later\n");
}
else
{
printf("write %s to the FIFO\n",buf_w);
}
close(fd);
return 0;
}
fifo_select.c
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <errno.h>
#include <unistd.h>
#define FIFO_I "/tmp/myfifo1"
#define FIFO_II "/tmp/myfifo2"
int main(int argc,char* argv[])
{
int i,f ,rfd,wfd;
char c_buf[32];
fd_set write_fd,read_fd;
struct timeval net_timer;
if(access(FIFO_I,F_OK) == -1){
if(mkfifo(FIFO_I,0666) == -1 && errno != EEXIST){ //S_IWUSR|S_IRGRP|S_IRUSR|S_IROTH
printf("cannot create filo_1 file \n");
exit(1);
}
}
if(access(FIFO_II,F_OK) == -1){
if(mkfifo(FIFO_II,S_IWUSR|S_IRGRP|S_IRUSR|S_IROTH) == -1 && errno != EEXIST){
printf("cannot create filo_2 file \n");
exit(1);
}
}
wfd = open(FIFO_I,O_WRONLY|O_NONBLOCK);
rfd = open(FIFO_II,O_RDONLY|O_NONBLOCK);
if(rfd == -1 || wfd == -1){
printf("open failed rfd = %d , wfd = %d \n",rfd , wfd);
exit(1);
}
while(1)
{
FD_ZERO(&read_fd);
FD_SET(rfd,&read_fd);
FD_SET(fileno(stdin),&read_fd);
net_timer.tv_sec = 5;
net_timer.tv_usec = 0;
memset(c_buf,0x0,sizeof(c_buf));
if( i = select(rfd + 1, &read_fd,NULL,NULL,&net_timer) < 0)
continue;
if(FD_ISSET(rfd,&read_fd))
{
if(read(rfd,c_buf,sizeof(c_buf)) <= 0 )
{
if(errno ==EAGAIN) printf("no data yet\n");
}
else
printf("read buf : %s \n",(char*)c_buf);
}
if(FD_ISSET(fileno(stdin),&read_fd))
{
fgets(c_buf,sizeof(c_buf),stdin);
int len = write(wfd,c_buf,strlen(c_buf));
printf("write len = %d \n", len);
}
}
close(rfd);
close(wfd);
return 0;
}