栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

C++管道

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++管道

        管道是一种单向通信的方式,一般用于进程间通信,在零拷贝时也会用到管道。管道本质上是一个文件,一个进程读,一个进程写。但是管道本身不占用磁盘或者其他外部存储的空间。在Linux上它占用内存空间。所以管道就是一个操作方式为文件的内存缓冲区。也正是因为管道是内存中的,所以它比使用文件作通信更快。

1.命名管道:

        命名管道需要包含头文件:#include   #include

        命名管道相当于创建了一个有命的文件,那么任何进程只要知道它的文件名就可以使用该管道,因此它不受亲缘进程的影响。命名管道创建和使用方式与文件差距不大,它使用mkfifo创建并使用open,write和read来打开,写和读。但是一个进程对命名管道只可以读和写选一个,不可以即读又写。例:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

const char buf[] = "hello";
const int32_t bufSize = sizeof(buf);
const char str[] = "/root/namePipe";

int main()
{
    //创建命名管道
    int32_t ret = mkfifo(str, S_IFIFO | 0666);
    if (ret == -1)
    {
        std::cout << "Make fifo errorn";
        return -1;
    }
    
    pid_t pid;
    pid = fork();
    if (pid > 0) {
        int32_t fd = open(str, O_WRONLY);
        if (write(fd, buf, bufSize) < 0) {
            std::cout << "write errorn";
        }
        close(fd);
        return 0;
    }

    sleep(1);
    char readBuf[bufSize];
    int32_t fd = open(str, O_RDONLY);
    if (read(fd, readBuf, bufSize) < 0) {
        std::cout << "read errorn";
    }
    else {
        std::cout << buf << 'n';
    }
    close(fd);
    return 0;
}

         删除命名管道可以用linux的unlink命令

unlink 管道名
2.匿名管道:

        匿名管道使用pipe创建,需要包含头文件:#include

        pipe会初始化一个int[2],其中一个只读,另一个只写。由于它没有文件名,只有int变量存储的文件描述符,因此只有有血缘关系的进程才能使用它通信。匿名管道用法如下:

#include 
#include 
#include 
#include 
#include 
#include 
#include 

int main()
{
    int32_t pipeFd[2];
    if (pipe(pipeFd) == -1) {
        perror("Pipe failed:");
        return 0;
    }

    pid_t pid;
    pid = fork();
    if (pid > 0) {
        close(pipeFd[0]); //父进程只写,所以关闭读描述符
        char buf[] = "hello";
        if (write(pipeFd[1], buf, sizeof(buf)) < 0) {
            perror("Write error:");
        }
        close(pipeFd[1]);
        wait(nullptr); //防僵尸进程
        return 0;
    }

    close(pipeFd[1]); //子进程只读,关闭写描述符
    char buf[32];
    memset(buf, 0, 32);
    if (read(pipeFd[0], buf, 32) < 0) {
        perror("Read error:");
    }
    else {
        std::cout << buf << 'n';
    }
    close(pipeFd[0]);

    return 0;
}

        需要注意的是如果所有管道的读描述符被关闭,那么写将阻塞。如果所有的写描述符已关闭,那么读将无法读到返回0。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/885561.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号