FIFO Operations (ZZ)
来源:互联网 发布:淘宝店铺转天猫 编辑:程序博客网 时间:2024/04/29 22:38
FIFO Operations
I/O operations on a FIFO are essentially the same as for normal pipes, with once major exception. An ``open'' system call or library function should be used to physically open up a channel to the pipe. With half-duplex pipes, this is unnecessary, since the pipe resides in the kernel and not on a physical filesystem. In our examples, we will treat the pipe as a stream, opening it up with fopen(), and closing it with fclose().
Consider a simple server process:
/***************************************************************************** Excerpt from "Linux Programmer's Guide - Chapter 6" (C)opyright 1994-1995, Scott Burkett ***************************************************************************** MODULE: fifoserver.c *****************************************************************************/#include <stdio.h>#include <stdlib.h>#include <sys/stat.h>#include <unistd.h>#include <linux/stat.h>#define FIFO_FILE "MYFIFO"int main(void){ FILE *fp; char readbuf[80]; /* Create the FIFO if it does not exist */ umask(0); mknod(FIFO_FILE, S_IFIFO|0666, 0); while(1) { fp = fopen(FIFO_FILE, "r"); fgets(readbuf, 80, fp); printf("Received string: %s/n", readbuf); fclose(fp); } return(0);}
Since a FIFO blocks by default, run the server in the background after you compile it:
$ fifoserver&
We will discuss a FIFO's blocking action in a moment. First, consider the following simple client frontend to our server:
/***************************************************************************** Excerpt from "Linux Programmer's Guide - Chapter 6" (C)opyright 1994-1995, Scott Burkett ***************************************************************************** MODULE: fifoclient.c *****************************************************************************/#include <stdio.h>#include <stdlib.h>#define FIFO_FILE "MYFIFO"int main(int argc, char *argv[]){ FILE *fp; if ( argc != 2 ) { printf("USAGE: fifoclient [string]/n"); exit(1); } if((fp = fopen(FIFO_FILE, "w")) == NULL) { perror("fopen"); exit(1); } fputs(argv[1], fp); fclose(fp); return(0);}
- FIFO Operations (ZZ)
- Operations
- FIFO
- FIFO
- fifo
- FIFO
- FIFO
- fifo
- FIFO
- FIFO
- fifo
- FIFO
- FIFO
- fifo
- FIFO
- FIFO
- zz
- zz
- Blogs Sites URL
- 抄来1个文章,很受启发,与大家共勉
- ajax 学习资料(不断整理中)
- 关于GPL 协议
- 流水账
- FIFO Operations (ZZ)
- 谁在火中大喊:学生不要动,让领导先走!![转]
- Java Puzzler 6 库
- [转]C++模板元编程
- Mysql5.0中文乱码问题解决方案
- ADO.NET表列映射
- 三亚归来谈社区
- 扬州风光
- 2006年大学生就业“力”调查:你值多少钱