C++ Linux下如何进行进程间通信
导读:在Linux下,C++可以通过多种方式进行进程间通信(IPC),以下是一些常用的IPC方法: 管道(Pipes): 管道是一种半双工的通信方式,通常用于父子进程之间的通信。在C++中,可以使用pipe( 系统调用创建一个管道,然后使用re...
在Linux下,C++可以通过多种方式进行进程间通信(IPC),以下是一些常用的IPC方法:
- 管道(Pipes):
管道是一种半双工的通信方式,通常用于父子进程之间的通信。在C++中,可以使用
pipe()系统调用创建一个管道,然后使用read()和write()函数进行读写操作。
#include <
iostream>
#include <
unistd.h>
#include <
fcntl.h>
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 *msg = "Hello from parent!";
write(pipefd[1], msg, strlen(msg) + 1);
close(pipefd[1]);
}
return 0;
}
- 命名管道(Named Pipes,FIFO):
命名管道是一种特殊的文件类型,可以在不相关的进程之间进行通信。在C++中,可以使用
mkfifo()系统调用创建一个命名管道,然后使用open()、read()和write()函数进行读写操作。
#include <
iostream>
#include <
fcntl.h>
#include <
sys/stat.h>
#include <
unistd.h>
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 *msg = "Hello from named pipe!";
write(fd, msg, strlen(msg) + 1);
char buffer[10];
read(fd, buffer, sizeof(buffer));
std::cout <
<
"收到消息: " <
<
buffer <
<
std::endl;
close(fd);
unlink(fifo_name);
return 0;
}
- 消息队列(Message Queues):
消息队列是一种基于消息的进程间通信方式。在C++中,可以使用
msgget()、msgsnd()和msgrcv()函数进行消息队列的创建、发送和接收操作。
#include <
iostream>
#include <
sys/ipc.h>
#include <
sys/msg.h>
#include <
cstring>
struct msg_buffer {
long msg_type;
char msg_text[100];
}
;
int main() {
key_t key = ftok("msgqueue_example", 'A');
int msgid = msgget(key, 0666 | IPC_CREAT);
msg_buffer buffer;
buffer.msg_type = 1;
strcpy(buffer.msg_text, "Hello from message queue!");
msgsnd(msgid, &
buffer, sizeof(buffer.msg_text), 0);
msgrcv(msgid, &
buffer, sizeof(buffer.msg_text), 1, 0);
std::cout <
<
"收到消息: " <
<
buffer.msg_text <
<
std::endl;
msgctl(msgid, IPC_RMID, NULL);
return 0;
}
- 共享内存(Shared Memory):
共享内存是一种高效的进程间通信方式。在C++中,可以使用
shmget()、shmat()、shmdt()和shmctl()函数进行共享内存的创建、映射、解除映射和控制操作。
#include <
iostream>
#include <
sys/ipc.h>
#include <
sys/shm.h>
#include <
cstring>
int main() {
key_t key = ftok("shared_memory_example", 'A');
int shmid = shmget(key, 1024, 0666 | IPC_CREAT);
char *data = (char *)shmat(shmid, NULL, 0);
if (data == (char *)-1) {
perror("shmat");
return 1;
}
strcpy(data, "Hello from shared memory!");
std::cout <
<
"收到消息: " <
<
data <
<
std::endl;
shmdt(data);
shmctl(shmid, IPC_RMID, NULL);
return 0;
}
- 信号(Signals):
信号是一种用于进程间通知和控制的机制。在C++中,可以使用
signal()或sigaction()函数设置信号处理函数,然后使用kill()函数发送信号。
#include <
iostream>
#include <
csignal>
#include <
unistd.h>
void signal_handler(int signum) {
std::cout <
<
"收到信号: " <
<
signum <
<
std::endl;
}
int main() {
signal(SIGUSR1, signal_handler);
std::cout <
<
"等待信号..." <
<
std::endl;
pause();
return 0;
}
- 套接字(Sockets):
套接字是一种通用的进程间通信方式,支持本地和远程通信。在C++中,可以使用
socket()、bind()、listen()、accept()、connect()、send()和recv()等函数进行套接字的创建、绑定、监听、接受、连接、发送和接收操作。
这些只是Linux下C++进程间通信的一部分方法,还有其他IPC机制,如信号量(Semaphores)、内存映射文件(Memory-mapped Files)等。具体使用哪种方法取决于你的需求和应用场景。
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: C++ Linux下如何进行进程间通信
本文地址: https://pptw.com/jishu/740521.html
