- 论坛徽章:
- 0
|
* 管道通信:有名管道
* 无名管道只能用于具有亲缘关系的进程之间,而有名管道可以在互不相关的两个进程间
* 实现彼此通信。要注意,FIFO严格按照先进先出的规则,对管道及FIFO的读总是从开始
* 处返回数据,对它们的写则把数据添加到末尾,不支持lseek等文件定位操作。
*
* 有名管道的创建使用mkfifo()。创建成功后就可以使用open、read、write这些函数了。
* 读管道部分
*/
#include
#include
#include
#include
#include
#include
#include
#include
/*在这里设置打开管道文件的mode为只读形式*/
#define FIFOMODE (O_CREAT | O_RDWR | O_NONBLOCK)
#define OPENMODE (O_RDONLY | O_NONBLOCK)
#define FIFO_SERVER "myfifo"
int main(void)
{
char buf[100];
int fd;
int readnum;
/*创建有名管道,设置为可读写,无阻塞,如果不存在则按照指定权限创建*/
if ((mkfifo(FIFO_SERVER, FIFOMODE)
#include
#include
#include
#include
#include
#include
#include
/*特别注意写管道时,设置打开管道文件的格式必须为可写*/
#define FIFO_SERVER "myfifo"
#define OPENMODE (O_WRONLY | O_NONBLOCK)
int main(int argc, char **argv)
{
int fd;
int nwrite;
/*打开管道文件,可写非阻塞*/
if ((fd = open(FIFO_SERVER, OPENMODE)) myfifo,同样在读端也可以收到消息。注意一个地方;管道先要打开读端,否则写端会被阻塞。*/
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/108685/showart_2128297.html |
|