1、 open函数flag以O_RDONLY打开,只能打开有名管道的读端,以O_RDONLY打开,只能打开有名管道的写端。
2、 open函数flag以O_RDWR方式打开,将打开管道的读写端,只有同时打开管道的读端和写端,有名管道才被真正的打开。纸打开读端或者写端,open函数会出现阻塞。
创建:
int main()
{
if( mkfifo("./fifo",0666) < 0 )
{
perror("mkfifo error");
exit(1);
}
exit(0);
}
写入:
int main()
{
int fd;
int bytes_write;
char buf[100] = "hello,fifo!";
printf("open before!\n");
fd = open("./fifo",O_WRONLY);
printf("open after!\n");
if( -1 == fd )
{
perror("open error");
exit(1);
}
bytes_write = write(fd,buf,strlen(buf));
if( -1 == bytes_write )
{
perror("write error");
exit(1);
}
printf("bytes_write is %d\n",bytes_write);
exit(0);
}
读取:
int main()
{
int fd;
char buf[100];
int bytes_read;
printf("open before!\n");
fd = open("./fifo",O_RDWR);
printf("open after!\n");
if( -1 == fd )
{
perror("open error");
exit(1);
}
printf("read before!\n");
bytes_read = read(fd,buf,100);
printf("read after!\n");
if( -1 == bytes_read )
{
perror("read error");
exit(1);
}
printf("bytes_read is %d\n",bytes_read);
exit(0);
}