2.15有名管道介绍及使用

目录

1.有名管道概述

2.创建fifo文件

3.两个进程通过有名管道进行通信


1.有名管道概述

2.创建fifo文件

方式1:通过命令: mkfifo 名字

方式2:通过函数mkfifo

1.通过命令: mkfifo 名字
2.通过函数: int mkfifo(const char *pathname, mode_t mode);


#include <sys/types.h>
#include <sys/stat.h>
int mkfifo(const char *pathname, mode_t mode);
    参数:
        -pathname:管道名称的路径
        -mode:文件的权限,和open的mode是一样的,是一个八进制的数
    返回值:成功返回0,失败返回-1,并设置errno
        

 程序示例:


#include <sys/stat.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main()
{

    //判断管道文件fifo1是否存在
    int ret = access("fifo1", F_OK);
    if (ret == -1)
    {
        printf("fifo1不存在,创建fifo1\n");
        ret = mkfifo("fifo1", 0664);
        if (ret == -1)
        {
            perror("mkfifo");
            return -1;
        }
    }

    return 0;
}

运行结果:


3.两个进程通过有名管道进行通信

文件write.c:


#include <sys/stat.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>

int main()
{

    //判断管道文件fifo1是否存在
    int ret = access("fifo1", F_OK);
    //如果不存在,创建管道文件
    if (ret == -1)
    {
        printf("fifo1不存在,创建fifo1\n");
        ret = mkfifo("fifo1", 0664);
        if (ret == -1)
        {
            perror("mkfifo");
            return -1;
        }
    }
    //以只写方式打开管道文件
    int fd=open("fifo1",O_WRONLY);
    if(fd==-1)
    {
        perror("open");
        return -1;
    }


    //往管道中写数据
    char buf[1024]={0};
    for(int i=0;i<100;i++)
    {
        sprintf(buf,"hello:%d\n",i);
        printf("write:%s\n",buf);
        write(fd,buf,strlen(buf));
        bzero(buf,1024);
        sleep(1);
    }


    close(fd);

    return 0;
}

文件read.c:

#include <sys/stat.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>

int main()
{
    //以只读方式打开管道
    int fd=open("fifo1",O_RDONLY);
    if(fd==-1)
    {
        perror("open");
        return -1;
    }
    //从管道中读数据
    char buf[1024]={0};
    while(1)
    {
        int len=read(fd,buf,sizeof(buf));
        if(len==0)
        {
            printf("写端断开连接\n");
            break;
        }
        else if(len>0)
        {
            printf("read recv:%s\n",buf);
            bzero(buf,1024);
            /* code */
        }
        
    }
    return 0;
}

运行结果:(在两个终端中)


有名管道的注意事项:

1.一个进程打开管道的只读端后会阻塞,直到另一个进程打开该管道的只写端

2.一个进程打开管道的只写端后会阻塞,直到另一个进程打开该管道的只读端

读管道:

  管道中有数据:read返回实际读到的字节数

  管道中没有数据:

         如果管道的写端全部关闭,read返回0,(相当于读到文件末尾)

        如果写端没有全部关闭,read阻塞等待

写管道:

    管道读端被全部关闭:进程异常终止(收到一个SIGPIPE信号)

    管道读端没有全部关闭:

             管道已经满了,write会阻塞

             管道没有满,write将数据写入,并返回实际写入的字节数

猜你喜欢

转载自blog.csdn.net/m0_38062470/article/details/113827474