c 进程间通信
管道(Pipe)
管道是通信一种半双工的通信方式(shi),数据只能单向流动,程间且只(zhi)能在(zai)具有亲缘关系(xi)的通信进程间使用,进程的程间亲缘关系通(tong)常(chang)是指父子进程关系,管道(dao)分为匿名管道和命名管道(dao)。通(tong)信
1、程间匿名管道


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

创建匿名管道:
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]); // 关(guan)闭读端 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)); // 读取数据(ju) std::cout << "Received from child process: " << ch << std::endl; close(pipefd[0]); // 关(guan)闭读端 } else { // fork失败 std::cerr << "Fork error" << std::endl; return -1; } return 0;}2、命名(ming)管道(Named Pipe)
命名管道也是半双工的通信方式,但它允许无亲缘关系进程间的通信,命名管道分(fen)为本地命名管道(dao)和远程命名管道,本地命名管道(dao)只能在当前(qian)操作系(xi)统中使用,而(er)远程命名管道可以在不同的操作系统中使用。
创建(jian)命名管道:
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) { // 创建管道(dao)失败 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]); // 关(guan)闭读端 } else { // fork失败 std::cerr << "Fork error" << std::endl; return -1; } return 0;}信号量(Semaphore)
信号量是一个计数器,可以用来控制多个进程对共享资源的访问,它常作为一种锁机制,防止某进程正在(zai)访问共享资源时,其他进程也访问该资源,主要作为进程间以及同一进(jin)程(cheng)内不同线程之间的同步手段,信号量分为两(liang)类:整型信号量和布尔型信号量,整型信号(hao)量可用于多进程及多线程间同步,4.24.1中提到了POSIX信号量,但是Windows不支持POSIX信号量,Windows上可以使(shi)用Windows API提供的Event对象来实现类似的功能(neng),下面是基于Windows API的示例代码:
“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)消(xiao)息队列是由消息的链表(biao),存(cun)放在内核中并由消(xiao)息队列标识符标识,消息队列克服了信号传递信息少、管线长、只有发送者和接收者的问题(ti),消息队(dui)列允许无亲缘关系进程间发送消息给对方,消息队列常作为进程(cheng)间(jian)以(yi)及同一进程内不同线程之间的通信方法,四、共(gong)享内存(Shared Memory)共享内存就是映射一段能被其他进程所访问的内存,这段共享内存由一个进程创建,但(dan)多个进程都可以访问,共(gong)享内存是最快的 IPC (Inter-Process Communication) 实现形式,它可以(yi)实(shi)现高速的数据共享,但它又要求进程间必须存在一个共(gong)同的句柄,具体(ti)可(ke)以参考以下示例代(dai)码: `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)套接字也可以用于





