linux进程通信(命名管道)

19 篇文章 2 订阅
9 篇文章 0 订阅

管道应用的一个重大限制就是它没有名字,因此,只能用于具有亲缘关系的进程间的通信,在有名管道(Named Pipe或 FIFO)提出后,该限制得到了克服,FIFO不同于管道之处

在于它提供了一个路径名与之关联,以FIFO的文件形式存在与文件系统中。这样,即使与FIFO的创建进程不存在亲缘关系的进程,只要可以访问该路径,彼此之间就能进行通信


一个比较好的命名管道例子:

下面这个例子使用FIFO进行进程间的通信,程序lucy.c创建了FIFO write_fifo用于向程序peter.c发送消息;peter.c程序创建了FIFO read_fifo用于向lucy.c发送消息

同时,lucy.c能够通过打开peter.c创建的FIFO来得到的peter.c发来的消息,peter.c能够通过打开lucy.c创建的FIFO来得到lucy.c发来的消息。因此两者就能互相通信了,两者必须在线才能进行通信聊天,这个有点类似qq的聊天功能。


下面是源程序lucy.c

#include<sys/types.h>
#include<sys/stat.h>// 文件状态的头文件
#include<string.h>   //字符串函数头文件
#include<stdio.h>     //标准的输入输出头文件
#include<errno.h>     //错误提示头文件
#include<fcntl.h>     //控制头文件
#include<unistd.h>  //创建进程的头文件
int main(void)
{
 char write_fifo_name[]="write-fifo";    //定义一个写管道名字
 char read_fifo_name[]="read-fifo";//定义一个读管道的名字
 int  write_fd,read_fd;
 char buf[256];   //定义存储通信的会话空间长度
 int len;
 struct stat stat_buf;   
 int ret=mkfifo(write_fifo_name,S_IRUSR | S_IWUSR);
 if(ret==-1)   //创建一个命名管道
{
 printf("Fail to create FIFO %s",write_fifo_name,strerror(errno));
 exit(-1);
}
write_fd=open(write_fifo_name,O_WRONLY);  //以读写方式打开命名的管道
if(write_fd==-1) {
printf("Fail to open FIFO %s: %s",write_fifo_name,strerror(errno));
exit(-1);
}
 while((read_fd=open(read_fifo_name,O_RDONLY))==-1) {    //以只读方式打开管道文件
 sleep(1);
}
 while(1) {
printf("熊尧: ");
fgets(buf,256,stdin);  //fgets()函数,从stdin流中读取256-1个字符存储在以buf为地址的空间里,至到读取成功,成功则返回buf的指针
buf[strlen(buf)-1]='\0';
if(strncmp(buf,"quit",4)==0) {   //用strncmp()函数来比较buf中的前四个字符是否为quit
close(write_fd);//若是,则关闭写入函数
unlink(write_fifo_name); //删除文件的目录并减少它的链接数
close(read_fd);   //最后要关闭打开的对话文件
exit(0);
}
write(write_fd,buf,strlen(buf));
len=read(read_fd,buf,256);
if(len>0) {
buf[len]='\0';
printf("红刚: %s\n",buf);
}
}
}

下面是peter.c


#include<sys/types.h>
#include<sys/stat.h>
#include<string.h>
#include<stdio.h>
#include<errno.h>
#include<fcntl.h>
int main(void)
{
  char  write_fifo_name[]= "read-fifo";
 char read_fifo_name[]= "write-fifo";
 int write_fd,read_fd;
char buf[256];
 int len;
 int ret=mkfifo(write_fifo_name,S_IRUSR | S_IWUSR);
 if(ret==-1) {
  printf("Fail to create FIFO %s",write_fifo_name,strerror(errno));
 exit(-1);
}
 while((read_fd=open(read_fifo_name,O_RDONLY))==-1) {
 sleep(1);
}
 write_fd=open(write_fifo_name,O_WRONLY);
if(write_fd==-1) {
printf("Fail to open FIFO %s: %s",write_fifo_name,strerror(errno));
exit(-1);
}
while(1) {
len=read(read_fd,buf,256);
if(len>0) {
buf[len]='\0';
printf("熊尧: %s\n",buf);
}
printf("红刚:");
fgets(buf,256,stdin);
buf[strlen(buf)-1]='\0';
if(strncmp(buf,"quit",4)==0) {
close(write_fd);
unlink(write_fifo_name);
close(read_fd);
exit(0);
}
write(write_fd,buf,strlen(buf));
}
}

在两个端口分别开始运行lucy.c和peter.c,即可以开始聊天了


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值