#include <sys/types.h>
#include <sys/stat.h>
int mkfifo(const char * pathname,     //带路径的管道名称
                  mode_t mode)        //管道的操作权限
//返回值:0 (成功),-1(打开失败,错误码见errno)
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
int open(const char* pathname,     //带路径的管道名称
                int flags          //管道打开的方式
                ...);              //其他参数,比如,在创建的时候可以赋权限,这里因为是由mkfifo创建,所以忽略
//返回值:>2 (返回管道的描述符),-1(打开失败,错误码见errno)O_RDONLY 以只读方式打开文件
O_WRONLY 以只写方式打开文件
O_RDWR 以可读写方式打开文件。
上述三种旗标是互斥的,只能取一个,可与下列选项或运算组合(|)使用。
O_CREAT 若欲打开的文件不存在则自动建立该文件。
O_EXCL 一般和O_CREAT一起使用,设置之后在创建文件时发现文件已经存在的话就报错。
O_NONBLOCK 以不可阻断的方式打开文件,也就是无论有无数据读取或等待,都会立即返回进程之中。
O_TRUNC 若文件存在并且以可写的方式打开时,清空文件中的内容。
O_APPEND 在写数据时将文件光标移到文件尾部。
O_SYNC 以同步的方式打开文件。
O_NOCTTY 如果欲打开的文件为终端机设备时,则不会将该终端机当成进程控制终端机。
O_NOFOLLOW 如果参数pathname 所指的文件为一符号连接,则会令打开文件失败。
O_DIRECTORY 如果参数pathname 所指的文件并非为一目录,则会令打开文件失败。
/************************************************************************************ 
    > File Name: testfifo.c 
    > Author: qiaozp 
    > Mail: qiaozongpeng@163.com 
    > Created Time: 2014-9-17 14:29:35
    > Step: 1 调用mkfifo函数创建一个命名管道
            2 在写端进程 用open函数以O_WRONLY的方式连接管道,并用write函数读取数据
            3 在读端进程 用open函数以O_RDONLY的方式连接管道,并用read函数读取数据
 ************************************************************************************/  
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <iostream>
#include <sys/stat.h>
#include <sys/types.h>
using namespace std;
#define  BUFF_LEN 100
#define PIPE_NAME "./PipeOne"
int main(int argc, char** argv)
{
    if (argc != 2)
    {
        cout << "请在后面加上参数[r/w], r表示读管道;w表示写管道。" << endl;
        return -1;
    }
    //创建管道
    if ((mkfifo(PIPE_NAME, S_IRWXU) == -1) && (errno != EEXIST))
    {
        cout << "创建管道[" << PIPE_NAME << "]失败,错误信息[" << strerror(errno) << "]." << endl;
        return -1;
    }
    int fd;
    char buff[BUFF_LEN];
    if (strncasecmp(argv[1], "w", 1) == 0)
    {
        //写管道的进程
        if ((fd = open(PIPE_NAME, O_WRONLY)) == -1)
        {
            cout << "连接管道[" << PIPE_NAME << "]失败,错误信息[" << strerror(errno) << "]." << endl;
            return -1;
        }
        do 
        {
            cout << "请输入需要从管道传输的数据: ";
            memset(buff, 0, BUFF_LEN);
            scanf("%s", buff);
            write(fd, buff, sizeof(buff));
        } while (strncasecmp(buff, "end", 3) != 0);
    }
    else if (strncasecmp(argv[1], "r", 1) == 0)
    {
        //读管道的进程
        if ((fd = open(PIPE_NAME, O_RDONLY)) == -1)
        {
            cout << "连接管道[" << PIPE_NAME << "]失败,错误信息[" << strerror(errno) << "]." << endl;
            return -1;
        }
        do 
        {
            memset(buff, 0, BUFF_LEN);
            read(fd, buff, sizeof(buff));
            cout << "从管道读取到的数据为: " << buff << endl;
        } while (strncasecmp(buff, "end", 3) != 0);
    }
    else
    {
        cout << "请在后面加上正确的参数[r/w], r表示读管道;w表示写管道。" << endl;
    }
    return 0;
}原文地址:http://blog.csdn.net/juncily/article/details/39342311