匿名管道呢,只能使用在有亲缘关系的进程之间,比如父子进程个兄弟进程,等等,因为匿名管道是一个在内存中存在的文件,其地址描述符只是在父子进程之中才有体现,为了克服该缺点,就有了命名管道的实现,命名管道呢,实际上就是一个在文件系统中存储的文件,命名管道是一个设备文件,同时,该管道文件也是FIFO(First  In First Out)的形式,即,第一个被写入的数据,将第一个被读出

创建命名管道的系统函数

int  mkfifo(const char* path,mode_t mode);path指示的是管道文件的全路经,mode则是管道模式和具有存取权限

int  mknod(const char* path,mode_t mode,dev_t dev)同上,但是dev为设备值,取决于文件的创建类型。

现在说下他的编码实现,因为管道操作是半双工通信,有名管道呢,其主要含义就是根据管道到文件所在的路径进行通信的,也就是说,我们要进行相应的东西,进行操作

现在看下server

/*
* main.cpp
*
* Created on: Jul 16, 2014
* Author: john
*/

#include<sys/types.h>
#include<sys/stat.h>
#include<string.h>
#include<iostream>
#include<errno.h>
#include<fcntl.h>
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
//#define FIFO_READ "/tmp/writefifo"
#define FIFO_WRITE "/tmp/readfifo"

#define BUF_SIZE 1024

int main()
{
int wfd;
char ubuf[BUF_SIZE]={0};
umask(0);
if(mkfifo(FIFO_WRITE,S_IFIFO|0666))
{
cout<<"sorry the namedpipe created error"<<strerror(errno)<<endl;
exit(0);
}
umask(0);
wfd=open(FIFO_WRITE,O_WRONLY);
if(wfd==-1)
{
cout<<"open named pipe error"<<FIFO_WRITE<<strerror(errno)<<endl;
exit(1);
}
cout<<"begin...\n";
int nCount=0;
while(1)
{
cout<<"please input the content:"<<endl;
cin>>ubuf;
if(strcmp(ubuf,"exit")==0)
{
exit(0);
}
int leng= write(wfd,ubuf,strlen(ubuf));
if(leng>0)
{
cout<<"write..."<<nCount<<endl;
nCount++;
}
}

}


现在看下client操作

/*
* main.cpp
*
* Created on: Jul 16, 2014
* Author: john
*/

#include<sys/types.h>
#include<sys/stat.h>
#include<string.h>
#include<iostream>
#include<errno.h>
#include<fcntl.h>
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
#define FIFO_READ "/tmp/readfifo"

#define BUF_SIZE 1024

int main()
{
int rfd;
char ubuf[BUF_SIZE]={0};
umask(0);
while((rfd=open(FIFO_READ,O_RDONLY))==-1)
{
cout<<"open..."<<strerror(errno)<<endl;
sleep(1);
}
cout<<"begin...\n";
int nCount=0;
while(1)
{
int len=read(rfd,ubuf,BUF_SIZE);
if(len>0)
{
ubuf[len]='\0';
cout<<"server:"<<ubuf<<endl;
cout<<"read.."<<nCount<<endl;
nCount++;
}
}

}


这样两个没有任何亲缘关系的两个进程就可以进行通信了,该种方式类似于使用文件来进行进程间的数据通信,但是fifo文件同时具有管道的特性,其含义就是,在数据读出的同时,会将该数据清除,也就是说,我们不需要关心垃圾数据的产生,那么如何来进行鉴别该文件是否是fifo文件呢,方法就是读取该文件的stat_t属性,来确定该文件的类型