c 进程间通信

进程间通信(Inter-Process Communication,IPC)是指在多个进程之间进行信息交换的过程。IPC的目的一般有共享数据、数据传输、消息通知、进程控制等。常见的IPC方式有:共享内存、管道、消息队列、信号量等。

管道(Pipe)

管道是一种半双工的通信方式,数据只能单向流动,且只能在具有亲缘关系的进程间使用,进程的亲缘关系通常是指父子进程关系,管道分为匿名管道和命名管道

1、匿名管道

c 进程间通信

匿名管道是一种无需与任何文件系统相关联的命名管道,它允许两个进程之间通过一个文件描述符进行双向通信,匿名管道主要用于具有亲缘关系的父子进程之间的通信。

创建匿名管道:

include <iostream>
include <unistd.h>
include <sys/types.h>
include <sys/wait.h>
int main() {
    int pipefd[2];
    if (pipe(pipefd) == -1) {
        std::cerr << "Pipe error" << std::endl;
        return -1;
    }
    pid_t pid = fork();
    if (pid == 0) { // 子进程
        close(pipefd[0]); // 关闭读端
        char ch = 'H';
        write(pipefd[1], &ch, sizeof(ch)); // 写入数据
        close(pipefd[1]); // 关闭写端
    } else if (pid > 0) { // 父进程
        close(pipefd[1]); // 关闭写端
        char ch;
        read(pipefd[0], &ch, sizeof(ch)); // 读取数据
        std::cout << "Received from child process: " << ch << std::endl;
        close(pipefd[0]); // 关闭读端
    } else { // fork失败
        std::cerr << "Fork error" << std::endl;
        return -1;
    }
    return 0;
}

2、命名管道(Named Pipe)

c 进程间通信

命名管道也是半双工的通信方式,但它允许无亲缘关系进程间的通信,命名管道分为本地命名管道和远程命名管道,本地命名管道只能在当前操作系统中使用,而远程命名管道可以在不同的操作系统中使用。

创建命名管道:

include <iostream>
include <fcntl.h>
include <sys/stat.h>
include <sys/types.h>
include <unistd.h>
include <sys/wait.h>
include <cstring>
int main() {
    int pipefd[2];
    if (mkfifo("mypipe", S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) == -1) { // 创建命名管道失败
        std::cerr << "Mkfifo error" << std::endl;
        return -1;
    }
    if (pipe(pipefd) == -1) { // 创建管道失败
        std::cerr << "Pipe error" << std::endl;
        return -1;
    }
    pid_t pid = fork(); // 创建子进程
    if (pid == 0) { // 子进程
        close(pipefd[0]); // 关闭读端
        char ch = 'H';
        write(pipefd[1], &ch, sizeof(ch)); // 写入数据
        close(pipefd[1]); // 关闭写端
    } else if (pid > 0) { // 父进程
        close(pipefd[1]); // 关闭写端
        char ch;
        read(pipefd[0], &ch, sizeof(ch)); // 读取数据
        std::cout << "Received from child process: " << ch << std::endl;
        close(pipefd[0]); // 关闭读端
    } else { // fork失败
        std::cerr << "Fork error" << std::endl;
        return -1;
    }
    return 0;
}

信号量(Semaphore)

信号量是一个计数器,可以用来控制多个进程对共享资源的访问,它常作为一种锁机制,防止某进程正在访问共享资源时,其他进程也访问该资源,主要作为进程间以及同一进程内不同线程之间的同步手段,信号量分为两类:整型信号量和布尔型信号量,整型信号量可用于多进程及多线程间同步,4.24.1中提到了POSIX信号量,但是Windows不支持POSIX信号量,Windows上可以使用Windows API提供的Event对象来实现类似的功能,下面是基于Windows API的示例代码:

c 进程间通信

cpp//Create Semaphore for Mutexes in Windows API using CreateMutex function in Windows API.//Create Semaphore for Counting Down in Windows API using CreateEvent function in Windows API.//Create Semaphore for Counting Up in Windows API using CreateEvent function in Windows API.//Create Semaphore for Mutexes in POSIX OS using sem_open function in POSIX OS.//Create Semaphore for Counting Down in POSIX OS using sem_init function and sem_post function in POSIX OS.//Create Semaphore for Counting Up in POSIX OS using sem_init function and sem_wait function in POSIX OS.`三、消息队列(Message Queuing)消息队列是由消息的链表,存放在内核中并由消息队列标识符标识,消息队列克服了信号传递信息少、管线长、只有发送者和接收者的问题,消息队列允许无亲缘关系进程间发送消息给对方,消息队列常作为进程间以及同一进程内不同线程之间的通信方法,四、共享内存(Shared Memory)共享内存就是映射一段能被其他进程所访问的内存,这段共享内存由一个进程创建,但多个进程都可以访问,共享内存是最快的 IPC (Inter-Process Communication) 实现形式,它可以实现高速的数据共享,但它又要求进程间必须存在一个共同的句柄,具体可以参考以下示例代码: `cpp//Create Shared Memory in Windows OS using CreateFileMapping function in Windows OS.//Map Shared Memory in Windows OS using MapViewOfFile function in Windows OS.//Unmap Shared Memory in Windows OS using UnmapViewOfFile function in Windows OS.//Close Shared Memory in Windows OS using CloseHandle function in Windows OS.//Create Shared Memory in POSIX OS using mmap function in POSIX OS.//Unmap Shared Memory in POSIX OS using munmap function in POSIX OS.//Close Shared Memory in POSIX OS using close函数 in POSIX OS.“五、套接字(Socket)套接字也可以用于进程间通信,与其他通信方式相比,它是一个抽象的通信接口,使得不同类型的传输协议可以公用这个接口,套接字可以用于不同主机间的进程通信,即分布式进程间通信 (DPC),套接字的使用比较复杂,需要处理很多底层细节问题,这里不再详细介绍套接字的使用方法。

原创文章,作者:酷盾叔,如若转载,请注明出处:https://www.kdun.com/ask/132279.html

(0)
酷盾叔的头像酷盾叔订阅
上一篇 2024-01-02 11:08
下一篇 2024-01-02 11:14

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

云产品限时秒杀。精选云产品高防服务器,20M大带宽限量抢购  >>点击进入