阅读量:2
在Linux上,C++可以通过多种方式实现进程间通信(IPC)。以下是一些常用的IPC方法:
- 管道(Pipes):管道是一种简单的IPC机制,允许一个进程将数据发送到另一个进程。在Linux中,可以使用
pipe()系统调用创建一个管道,然后使用read()和write()系统调用进行读写操作。
#include
#include
#include
int main() {
int pipefd[2];
char buffer[10];
if (pipe(pipefd) == -1) {
perror("pipe");
return 1;
}
pid_t pid = fork();
if (pid == 0) { // 子进程
close(pipefd[1]); // 关闭写端
read(pipefd[0], buffer, sizeof(buffer));
std::cout << "子进程收到消息: " << buffer << std::endl;
close(pipefd[0]);
} else { // 父进程
close(pipefd[0]); // 关闭读端
const char* message = "Hello from parent!";
write(pipefd[1], message, strlen(message) + 1);
close(pipefd[1]);
}
return 0;
}
- 命名管道(Named Pipes,FIFOs):命名管道是一种特殊的文件类型,可以在不相关的进程之间传递数据。与普通管道相比,命名管道具有名称,因此可以在进程之间共享。
#include
#include
#include
#include
int main() {
const char* fifo_name = "my_fifo";
mkfifo(fifo_name, 0666);
int fd = open(fifo_name, O_RDWR);
if (fd == -1) {
perror("open");
return 1;
}
const char* message = "Hello from named pipe!";
write(fd, message, strlen(message) + 1);
char buffer[10];
read(fd, buffer, sizeof(buffer));
std::cout << "收到消息: " << buffer << std::endl;
close(fd);
unlink(fifo_name);
return 0;
}
- 消息队列(Message Queues):消息队列是一种允许进程发送和接收消息的数据结构。在Linux中,可以使用
msgget()、msgsnd()和msgrcv()系统调用操作消息队列。
#include
#include
#include
struct msg_buffer {
long msg_type;
char msg_text[100];
};
int main() {
key_t key = ftok("msgqueue_example.c", 'a');
int msgid = msgget(key, 0666 | IPC_CREAT);
msg_buffer message;
message.msg_type = 1;
strcpy(message.msg_text, "Hello from message queue!");
msgsnd(msgid, &message, sizeof(message.msg_text), 0);
msgrcv(msgid, &message, sizeof(message.msg_text), 1, 0);
std::cout << "收到消息: " << message.msg_text << std::endl;
msgctl(msgid, IPC_RMID, NULL);
return 0;
}
- 共享内存(Shared Memory):共享内存是一种允许多个进程访问同一块内存区域的IPC机制。在Linux中,可以使用
shmget()、shmat()和shmdt()系统调用操作共享内存。
#include
#include
#include
#include
int main() {
key_t key = ftok("shared_memory_example.c", 'a');
int shmid = shmget(key, 1024, 0666 | IPC_CREAT);
char* shared_memory = (char*)shmat(shmid, NULL, 0);
if (shared_memory == (char*)-1) {
perror("shmat");
return 1;
}
strcpy(shared_memory, "Hello from shared memory!");
sleep(2); // 等待其他进程读取数据
std::cout << "从共享内存读取数据: " << shared_memory << std::endl;
shmdt(shared_memory);
shmctl(shmid, IPC_RMID, NULL);
return 0;
}
- 信号(Signals):信号是一种用于进程间通信和事件通知的简单机制。在Linux中,可以使用
kill()系统调用发送信号,使用signal()或sigaction()系统调用处理信号。
#include
#include
#include
void signal_handler(int signal) {
std::cout << "收到信号: " << signal << std::endl;
}
int main() {
signal(SIGUSR1, signal_handler);
std::cout << "进程ID: " << getpid() << std::endl;
sleep(5); // 等待其他进程发送信号
return 0;
}
- 套接字(Sockets):套接字是一种用于在不同主机上的进程之间进行通信的IPC机制。在Linux中,可以使用
socket()、bind()、listen()、accept()、connect()和send()/recv()系统调用进行套接字通信。
这些只是Linux上C++进程间通信的一些方法。根据具体需求和场景,可以选择合适的IPC机制。
以上就是关于“C++如何在Linux上进行进程间通信”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm