C++进程间通信 匿名管道和命名管道

C++进程间通信 匿名管道和命名管道

匿名管道适用于父子进程之间的简单通信,而命名管道则适用于无亲缘关系的进程之间的通信。

1. 匿名管道

匿名管道概述

匿名管道是用于在具有亲缘关系的进程(如父子进程)之间进行单向通信的简单方式。管道创建时包含两个文件描述符:一个用于读(读端),一个用于写(写端)。

匿名管道代码示例

cpp 复制代码
#include <iostream>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <cstring>

int main() 
{
    int pipefd[2];  // 用于保存管道文件描述符
    pid_t cpid;
    char buf;

    if (pipe(pipefd) == -1) 
    {  // 创建管道
        perror("pipe");
        exit(EXIT_FAILURE);
    }

    cpid = fork();  // 创建子进程
    if (cpid == -1) 
    {
        perror("fork");
        exit(EXIT_FAILURE);
    }

    if (cpid == 0) 
    {  // 子进程
        close(pipefd[1]);  // 关闭写端
        while (read(pipefd[0], &buf, 1) > 0) 
        {
            write(STDOUT_FILENO, &buf, 1);  // 读取管道并输出到标准输出
        }
        write(STDOUT_FILENO, "\n", 1);
        close(pipefd[0]);  // 关闭读端
        _exit(EXIT_SUCCESS);

    } 
    else 
    {  // 父进程
        close(pipefd[0]);  // 关闭读端
        const char *msg = "Hello from parent process!";
        write(pipefd[1], msg, strlen(msg));  // 写入管道
        close(pipefd[1]);  // 关闭写端
        wait(NULL);  // 等待子进程结束
        exit(EXIT_SUCCESS);
    }
}

代码解释

  1. 管道创建pipe(pipefd)创建一个匿名管道,其中pipefd[0]是读端,pipefd[1]是写端。
  2. 进程创建fork()创建一个子进程。cpid在子进程中为0,在父进程中为子进程ID。
  3. 子进程
    • 关闭写端。
    • 读取管道数据并输出到标准输出。
    • 关闭读端并退出。
  4. 父进程
    • 关闭读端。
    • 将消息写入管道。
    • 关闭写端并等待子进程结束。

运行结果

复制代码
Hello from parent process!

2. 命名管道

命名管道概述

命名管道(FIFO)是一种特殊的文件,可以用于无亲缘关系的进程之间进行双向通信。它通过在文件系统中创建一个命名管道文件进行通信。

命名管道代码示例

首先,创建一个命名管道文件:

sh 复制代码
mkfifo /tmp/myfifo

然后,编写两个程序,一个用于写入数据,另一个用于读取数据。

写入数据的程序(writer.cpp)

cpp 复制代码
#include <iostream>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <cstring>

int main() 
{
    const char *fifo = "/tmp/myfifo";
    int fd = open(fifo, O_WRONLY);  // 打开命名管道的写端
    if (fd == -1) 
    {
        perror("open");
        exit(EXIT_FAILURE);
    }
    const char *msg = "Hello from writer process!";
    write(fd, msg, strlen(msg));  // 写入数据到命名管道
    close(fd);  // 关闭管道
    return 0;
}

读取数据的程序(reader.cpp)

cpp 复制代码
#include <iostream>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <cstring>

int main() 
{
    const char *fifo = "/tmp/myfifo";
    char buf[128];
    int fd = open(fifo, O_RDONLY);  // 打开命名管道的读端
    if (fd == -1) 
    {
        perror("open");
        exit(EXIT_FAILURE);
    }
    ssize_t numBytes = read(fd, buf, sizeof(buf));  // 从管道读取数据
    if (numBytes > 0) 
    {
        buf[numBytes] = '\0';
        std::cout << "Received message: " << buf << std::endl;  // 输出读取的数据
    }
    close(fd);  // 关闭管道
    return 0;
}

代码解释

  1. 创建命名管道文件 :通过mkfifo命令在文件系统中创建一个命名管道文件。
  2. 写入数据的程序(writer.cpp)
    • 打开命名管道的写端。
    • 将消息写入管道。
    • 关闭管道。
  3. 读取数据的程序(reader.cpp)
    • 打开命名管道的读端。
    • 从管道读取数据。
    • 输出读取的数据。
    • 关闭管道。

运行步骤

  1. 在终端中创建命名管道文件:

    sh 复制代码
    mkfifo /tmp/myfifo
  2. 分别编译writer.cppreader.cpp

    sh 复制代码
    g++ writer.cpp -o writer
    g++ reader.cpp -o reader
  3. 打开两个终端,在第一个终端运行读取程序:

    sh 复制代码
    ./reader
  4. 在第二个终端运行写入程序:

    sh 复制代码
    ./writer

运行结果

第一个终端(运行读取程序)输出:

复制代码
Received message: Hello from writer process!
相关推荐
什么半岛铁盒1 分钟前
Linux信号的保存
linux·运维·网络
百锦再8 分钟前
大数据技术的主要方向及其应用详解
大数据·linux·网络·python·django·pygame
June`9 分钟前
专题二:二叉树的深度搜索(二叉树剪枝)
c++·算法·深度优先·剪枝
2301_8035545223 分钟前
vim,gcc/g++,makefile,cmake
linux·编辑器·vim
惜.己1 小时前
Linux常用命令(十四)
linux·运维·服务器
AI+程序员在路上1 小时前
XML介绍及常用c及c++库
xml·c语言·c++
guoguo05241 小时前
vs2019及以后版本cmd指定编译环境文件的路径
c++
linkingvision2 小时前
H5S 视频监控AWS S3 对象存储
linux·运维·aws·视频监控s3对象存储
软行2 小时前
LeetCode 每日一题 3341. 到达最后一个房间的最少时间 I + II
数据结构·c++·算法·leetcode·职场和发展
BillKu2 小时前
服务器多JAR程序运行与管理指南
运维·服务器·jar