Linux下进程间通信之命名管道

本文介绍命名管道(FIFO)的概念及其实现方式,并通过一个简单的示例程序演示了如何使用命名管道进行进程间通信。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

管道实现通信的方式简单易懂,但一个不足之处就是管道没有名字,因此,只能用于具有亲缘关系的进程间通信,在命名管道(named pipe或FIFO)提出后,该限制得到了克服。FIFO不同于管道之处在于它提供一个路径名与之关联,以FIFO的文件形式存储于文件系统中。命名管道是一个设备⽂文件,因此,即使进程与创建FIFO的进程不存在亲缘关系,只要可以访问该路径,就能够通过FIFO相互通信。值得注意的是,FIFO(first input first output)总是按照先进先出的原则工作,第一个被写⼊入的数据将首先从管道中读出。
下面编写程序进行测试,程序分两部分,分别为读端和写端:
写端为server

#include <stdio.h>  
#include <sys/types.h>  
#include <sys/stat.h>  
#include <fcntl.h>  
#include <unistd.h>  
#include <string.h>  
#define _PATH_ "./fifo"
int main()  
{  
    // 创建管道时需要在mode参数位置传S_IFIFO,表明创建的是命名管道  
    int ret = mkfifo(_PATH_, S_IFIFO | 0644);      
    if (ret < 0)  
    {  
        perror("mkfifo");  
        return 1;  
    }  

    int fd = open(_PATH_, O_WRONLY);  
    if (fd < 0)  
    {  
        perror("open");  
        return 2;  
    }  

    int time = 0;  
    char *msg = "hello world";  
    while (time++ < 100)   //循环写入
    {  
        write(fd, msg, strlen(msg));  
        sleep(1);  
    }  

    close(fd);  
    return 0;  
}  

读端为client

#include <stdio.h>  
#include <sys/types.h>  
#include <sys/stat.h>  
#include <fcntl.h>  
#include <unistd.h>  
#include <string.h>  
#define _PATH_ "./fifo"

int main()
{
    int fd =open(_PATH_,O_RDONLY);
    if(fd<0)
    {
        printf("open file error\n");
        return 1;
    }
    int time=0;
    char buf[100];
    memset(buf,'\0',sizeof(buf));
    while(time++<100)
    {
        int ret= read(fd,buf,sizeof(buf));
        if(ret<=0)
        {
            printf("%s\n",buf);
            break;
        }
        printf("%s\n",buf);
        if(strncmp(buf,"quit",4)==0)   //退出条件
            break;
    }
    close(fd);
    return 0;
}

先运行server端
这里写图片描述
在运行client端
这里写图片描述
client端不断接收到server端写入的信息。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值