在Linux環境下,C++程序可以通過多種方式進行進程間通信(IPC)。以下是一些常用的IPC機制:
管道(Pipes):
信號(Signals):
消息隊列(Message Queues):
共享內存(Shared Memory):
信號量(Semaphores):
套接字(Sockets):
下面是一些簡單的示例代碼,展示了如何在C++中使用這些IPC機制:
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <iostream>
int main() {
int pipefd[2];
pid_t pid;
char buffer[10];
if (pipe(pipefd) == -1) {
perror("pipe");
exit(EXIT_FAILURE);
}
pid = fork();
if (pid == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if (pid > 0) { // 父進程
close(pipefd[0]); // 關閉讀端
write(pipefd[1], "Hello from parent", 20);
close(pipefd[1]); // 關閉寫端
wait(NULL); // 等待子進程結束
} else { // 子進程
close(pipefd[1]); // 關閉寫端
read(pipefd[0], buffer, sizeof(buffer));
std::cout << "Child received: " << buffer << std::endl;
close(pipefd[0]); // 關閉讀端
}
return 0;
}
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <iostream>
int main() {
const char* fifo = "/tmp/myfifo";
mkfifo(fifo, 0666);
int fd = open(fifo, O_RDWR);
if (fd == -1) {
perror("open");
exit(EXIT_FAILURE);
}
const char* message = "Hello from FIFO";
write(fd, message, strlen(message) + 1);
char buffer[10];
read(fd, buffer, sizeof(buffer));
std::cout << "Received: " << buffer << std::endl;
close(fd);
unlink(fifo); // 刪除FIFO
return 0;
}
#include <sys/ipc.h>
#include <sys/shm.h>
#include <iostream>
#include <cstring>
int main() {
key_t key = ftok("shmfile", 65);
int shmid = shmget(key, 1024, 0666|IPC_CREAT);
char* str = (char*) shmat(shmid, (void*)0, 0);
strcpy(str, "Hello shared memory");
std::cout << "Shared memory: " << str << std::endl;
shmdt(str);
shmctl(shmid, IPC_RMID, NULL);
return 0;
}
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <iostream>
union semun {
int val;
struct semid_ds *buf;
unsigned short *array;
};
int main() {
key_t key = ftok("semfile", 65);
int semid = semget(key, 1, 0666|IPC_CREAT);
union semun arg;
arg.val = 1; // 初始化信號量為1
semctl(semid, 0, SETVAL, arg);
// 使用semop進行信號量操作...
// ...
semctl(semid, 0, IPC_RMID);
return 0;
}
// 這是一個非?;A的TCP套接字示例,實際使用時需要更多的錯誤檢查和資源管理。
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <iostream>
#include <cstring>
int main() {
int server_fd, new_socket;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
char buffer[1024] = {0};
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) {
perror("setsockopt");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(8080);
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
perror("bind failed");
exit(EXIT_FAILURE);
}
if (listen(server_fd, 3) < 0) {
perror("listen");
exit(EXIT_FAILURE);
}
if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen)) < 0) {
perror("accept");
exit(EXIT_FAILURE);
}
read(new_socket, buffer, 1024);
std::cout << "Message from client: " << buffer << std::endl;
send(new_socket, "Hello from server", 17, 0);
std::cout << "Hello message sent\n";
close(new_socket);
close(server_fd);
return 0;
}
請注意,這些示例代碼僅用于演示目的,實際應用中需要考慮更多的錯誤處理和資源管理。在使用這些IPC機制時,還需要確保同步和互斥,以避免競態條件和其他并發問題。