新聞中心
管道(Pipe)
管道是一種半雙工的通信方式,數(shù)據(jù)只能單向流動,且只能在具有親緣關系的進程間使用,進程的親緣關系通常是指父子進程關系,管道分為匿名管道和命名管道。

專注于為中小企業(yè)提供成都做網(wǎng)站、網(wǎng)站建設服務,電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)炎陵免費做網(wǎng)站提供優(yōu)質的服務。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了上1000+企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設實現(xiàn)規(guī)模擴充和轉變。
1、匿名管道
匿名管道是一種無需與任何文件系統(tǒng)相關聯(lián)的命名管道,它允許兩個進程之間通過一個文件描述符進行雙向通信,匿名管道主要用于具有親緣關系的父子進程之間的通信。
創(chuàng)建匿名管道:
includeinclude include include 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)); // 寫入數(shù)據(jù) close(pipefd[1]); // 關閉寫端 } else if (pid > 0) { // 父進程 close(pipefd[1]); // 關閉寫端 char ch; read(pipefd[0], &ch, sizeof(ch)); // 讀取數(shù)據(jù) 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)
命名管道也是半雙工的通信方式,但它允許無親緣關系進程間的通信,命名管道分為本地命名管道和遠程命名管道,本地命名管道只能在當前操作系統(tǒng)中使用,而遠程命名管道可以在不同的操作系統(tǒng)中使用。
創(chuàng)建命名管道:
includeinclude include include include include include int main() { int pipefd[2]; if (mkfifo("mypipe", S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) == -1) { // 創(chuàng)建命名管道失敗 std::cerr << "Mkfifo error" << std::endl; return -1; } if (pipe(pipefd) == -1) { // 創(chuàng)建管道失敗 std::cerr << "Pipe error" << std::endl; return -1; } pid_t pid = fork(); // 創(chuàng)建子進程 if (pid == 0) { // 子進程 close(pipefd[0]); // 關閉讀端 char ch = 'H'; write(pipefd[1], &ch, sizeof(ch)); // 寫入數(shù)據(jù) close(pipefd[1]); // 關閉寫端 } else if (pid > 0) { // 父進程 close(pipefd[1]); // 關閉寫端 char ch; read(pipefd[0], &ch, sizeof(ch)); // 讀取數(shù)據(jù) 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)
信號量是一個計數(shù)器,可以用來控制多個進程對共享資源的訪問,它常作為一種鎖機制,防止某進程正在訪問共享資源時,其他進程也訪問該資源,主要作為進程間以及同一進程內不同線程之間的同步手段,信號量分為兩類:整型信號量和布爾型信號量,整型信號量可用于多進程及多線程間同步,4.24.1中提到了POSIX信號量,但是Windows不支持POSIX信號量,Windows上可以使用Windows API提供的Event對象來實現(xiàn)類似的功能,下面是基于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)消息隊列是由消息的鏈表,存放在內核中并由消息隊列標識符標識,消息隊列克服了信號傳遞信息少、管線長、只有發(fā)送者和接收者的問題,消息隊列允許無親緣關系進程間發(fā)送消息給對方,消息隊列常作為進程間以及同一進程內不同線程之間的通信方法,四、共享內存(Shared Memory)共享內存就是映射一段能被其他進程所訪問的內存,這段共享內存由一個進程創(chuàng)建,但多個進程都可以訪問,共享內存是最快的 IPC (Inter-Process Communication) 實現(xiàn)形式,它可以實現(xiàn)高速的數(shù)據(jù)共享,但它又要求進程間必須存在一個共同的句柄,具體可以參考以下示例代碼: `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函數(shù) in POSIX OS.“五、套接字(Socket)套接字也可以用于進程間通信,與其他通信方式相比,它是一個抽象的通信接口,使得不同類型的傳輸協(xié)議可以公用這個接口,套接字可以用于不同主機間的進程通信,即分布式進程間通信 (DPC),套接字的使用比較復雜,需要處理很多底層細節(jié)問題,這里不再詳細介紹套接字的使用方法。
文章標題:c進程間通信
轉載注明:http://www.5511xx.com/article/cdhoide.html


咨詢
建站咨詢
