1.命名管道FIFO
上篇学习笔记1中第九点是管道的局限性,只能是有关系的进程才能使用管道,命名管道改善了这个局限性
2.命名管道的使用
3.创建命名管道出现错误的信息
4.实例代码 read.c和write.c //点击查看linux下常见头文件的用法
//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> #define FIFO "/home/shan/c/fifo_rw/myfifo" main(int argc,char** argv) { char buf_r[100]; int fd; int nread; if((mkfifo(FIFO,0666)<0)&&(errno!=EEXIST)) printf("cannot create fifoserver\n"); 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 fifo file error\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"); } else printf("read %s from FIFO\n",buf_r); sleep(1); } close(fd); exit(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> #define FIFO_SERVER "/home/shan/c/fifo_rw/myfifo" main(int argc,char** argv) { int fd; char w_buf[100]; int nwrite; if(argc<=1) { printf("usage: ./fifo_write string\n"); exit(1); } sscanf(argv[1],"%s",w_buf); fd=open(FIFO_SERVER,O_WRONLY); if(fd==-1) { printf("Open fifo file error\n"); exit(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("white '%s' to the FIFO\n",w_buf); close(fd); exit(0); }