在处理进程间通信的问题时,匿名管道只能在有亲缘关系的进程中进行通信。如何做到在任意两个进程之间通信,这就要用到命名管道。
命名管道也被称为fifo文件,它是一种特殊类型的文件,在文件系统中以文件的形式存在,它的行为和匿名管道类似。可以使用mkfifo函数来创建一个命名管道。
int mkfifo(const char* filename,mode_t mode);
filename指定了文件名,mode指定了读写权限。
server.c:
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<fcntl.h>
#include<string.h>
#define _PATH_ "/tmp/file.tmp"
#define _SIZE_ 100
int main()
{
int fd = open(_PATH_,O_RDONLY);
if(fd < 0)
{
printf("open file error!\n");
return 1;
}
char buf[_SIZE_];
memset(buf,'\0',sizeof(buf));
while(1)
{
int ret = read(fd,buf,sizeof(buf));
if(ret <= 0)
{
printf("read end or error!\n");
break;
}
printf("%s\n",buf);
}
close(fd);
return 0;
}
client.c:
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<fcntl.h>
#include<string.h>
#define _PATH_ "/tmp/file.tmp"
#define _SIZE_ 100
int main()
{
int ret = mkfifo(_PATH_,0666|S_IFIFO);
if(ret == -1)
{
printf("mkfifo error!\n");
return 1;
}
int fd = open(_PATH_,O_WRONLY);
if(fd < 0)
{
printf("open error!\n");
}
char buf[_SIZE_];
memset(buf,'\0',sizeof(buf));
while(1)
{
scanf("%s",buf);
int ret = write(fd,buf,sizeof(buf)+1);
if(ret < 0)
{
printf("write error!\n");
break;
}
}
close(fd);
return 0;
}
运行结果:用client发送数据,用server接受数据
总结:
使用命名管道可以实现任意两个进程间的通信,文件系统中的路径名是全局的,各进程都可以访问,因此可以用文件系统中的路径名来标识一个ipc通道。
linux中一切皆文件,可以通过文件操作对命名管道进行使用。