1、创建有名管道
Mkfifo函数的原型如下所示:
#include <sys/types.h>
#include <sys/stat.h>
int mkfifo( const char *pathname, mode_t mode );
mkfifo函数需要两个参数,第一个参数(pathname)是将要在文件系统中创建的一个专用文件。第二个参数(mode)用来规定FIFO的读写权限。mkfifo函数如果调用成功的话,返回值为0;如果调用失败返回值为-1。
下面我们以一个实例来说明如何使用mkfifo函数建一个fifo,具体代码如下所示:
int ret;
... ret = mkfifo( "/tmp/cmd_pipe", S_IFIFO | 0666 );
if (ret == 0)
...{ // 成功建立命名管道 }
else
...{ // 创建命名管道失败 }
在这个例子中,利用/tmp目录中的cmd_pipe文件建立了一个命名管道(即fifo)。之后,就可以打开这个文件进行读写操作,并以此进行通信了。命名管道一旦打开,就可以利用典型的输入输出函数从中读取内容。举例来说,下面的代码段向我们展示了如何通过fgets函数来从管道中读取内容:
pfp = fopen( "/tmp/cmd_pipe", "r" );
... ret = fgets( buffer, MAX_LINE, pfp );
我们还能向管道中写入内容,下面的代码段向我们展示了利用fprintf函数向管道写入的具体方法:
pfp = fopen( "/tmp/cmd_pipe", "w+ );
... ret = fprintf( pfp, "Here’s a test string!/n" );
2、读写有名管道
#include <unistd.h>
ssize_t read (int fd , void * buf , size_t nbytes)
ssize_t write (int fd , void * buf , size_t nbytes)
在读写有名管道之前,需要用open函数打开该有名管道,打开有名管道操作与其他文件有一定的区别:如果希望打开管道的写端,则需要另一个进程打开管道的读端,如果只打开有名管道的一端,则系统将暂时阻塞打开进程,直到有另一个进程打开管道的另一端,当前进程才会继续执行,因此,在使用有名管道时,一定要使用两个进程分别打开其读端和写端。
非亲缘关系进程使用有名管道通信应用实例:
向有名管道中发送数据的进程代码:
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- #include<fcntl.h>
- #include<sys/types.h>
- #include<sys/stat.h>
- int main(int argc,char *argv[])
- {
- int fd;
- char buf[20] = "hello world!!!/n";
- if((mkfifo("my_fifo", O_CREAT|O_RDWR|0666)) < 0)
- {
- perror("mkfifo");
- exit(1);
- }
- if((fd = open("my_fifo" , O_WRONLY)) < 0)
- {
- perror("open");
- exit(1);
- }
- if((write(fd,buf,strlen(buf)-1)) < 0)
- {
- perror("write");
- exit(1);
- }
- return 0;
- }
从有名管道中接收数据的进程代码:
- #include<stdio.h>
- #include<stdlib.h>
- #include<fcntl.h>
- #include<sys/types.h>
- #include<sys/stat.h>
- int main(int argc,char *argv[])
- {
- int fd;
- char buf[20] = "";
- if((fd = open("my_fifo" , O_RDONLY)) < 0)
- {
- perror("open");
- exit(1);
- }
- if((read(fd,buf,20)) < 0)
- {
- perror("read");
- exit(1);
- }
- printf("%s" , buf);
- return 0;
- }