c - 如何使用共享内存实现 IPC
问题描述
我有以下问题要解决:
在 3 个进程之间实现 IPC,第一个进程创建共享内存,然后向第二个和第三个进程发送信号,以便它们可以连接到该共享内存,现在让第二个进程向共享内存写入一些内容,然后让它向第三个进程发送信号,以便第三个进程读取第二个进程写入的内容。
这是我到目前为止想出的
First_Process.c
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <unistd.h>
struct memory {
char msg[100];
int status, pid1, pid2, pid3;
};
struct memory* shmptr;
int main()
{
// First_Process Process Id
int pid = getpid();
// Key Of The Shared Memory
key_t key = ftok("/tmp", 'a');
// Shared Memory Creation
int shmid = shmget(key, sizeof(struct memory), IPC_CREAT | 0666);
// Attaching First_Process To The Shared Memory
shmptr = (struct memory*)shmat(shmid, NULL, 0);
// Store The First_Process Process Id In The Shared Memory
shmptr->pid1 = pid;
while (1) {
// Waiting For Second_Process And Third_Process To Get Attached To The Shared Memory
while (shmptr->status != 1)
continue;
sleep(1);
// Sending The Notification to Second_Process And Third_Process Using The Kill Command
//kill(shmptr->pid2, SIGUSR1);
//kill(shmptr->pid3, SIGUSR1);
}
// Detaching First_Process From The Shared Memory
shmdt((void*)shmptr);
}
Second_Process.c
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <unistd.h>
struct memory {
char msg[100];
int status, pid1, pid2, pid3;
};
struct memory* shmptr;
int main()
{
// Second_Process Process Id
int pid = getpid();
// Key Of The Shared Memory
key_t key = ftok("/tmp", 'a');
// Shared Memory Id Fetching
int shmid = shmget(key, sizeof(struct memory), IPC_CREAT | 0666);
// Attaching Second_Process To The Shared Memory
shmptr = (struct memory*)shmat(shmid, NULL, 0);
// Store The Second_Process Process Id In The Shared Memory
shmptr->pid2 = pid;
while (1) {
// Waiting For Third_Process To Get Attached To The Shared Memory
while (shmptr->status != 1)
continue;
sleep(1);
// Writing A Message To The Shared Memory
printf("Enter A Message To Send To Third_Process: \n");
fgets(shmptr->msg, 100, stdin);
// Sending The Message To Third_Process Using The Kill Command
kill(shmptr->pid3, SIGUSR2);
}
// Detaching Second_Process From The Shared Memory
shmdt((void*)shmptr);
}
第三进程.c
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <unistd.h>
struct memory {
char msg[100];
int status, pid1, pid2, pid3;
};
struct memory* shmptr;
// Handler Function To Read From The Shared Memory
void handler(int signum)
{
// if signum is SIGUSR2, then user 2 is receiving a message from user1
if (signum == SIGUSR2) {
printf("Received From Second: ");
puts(shmptr->msg);
kill(shmptr->pid1, SIGINT);
kill(shmptr->pid2, SIGINT);
kill(shmptr->pid3, SIGINT);
}
}
int main()
{
// Third_Process Process Id
int pid = getpid();
// Key of The Shared Memory
key_t key = ftok("/tmp", 'a');
// Shared Memory Id Fetching
int shmid = shmget(key, sizeof(struct memory), IPC_CREAT | 0666);
// Attaching Third_Process to The Shared Memory
shmptr = (struct memory*)shmat(shmid, NULL, 0);
// Store The Third_Process Process Id In The Shared Memory
shmptr->pid3 = pid;
// Telling Second_Process And First_Process That Third_Process Got Attached To The Shared Memory
shmptr->status = 1;
// Attaching Handler Function To Signal SIGUSR1 And SIGUSR2
signal(SIGUSR1, handler);
signal(SIGUSR2, handler);
while(1){}
// Detaching Third_Process From The Shared Memory
shmdt((void*)shmptr);
}
代码有问题吗?
提前致谢。
编辑:它运行良好然后它开始给出分段错误错误我不知道为什么
解决方案
推荐阅读
- html - 我可以在字体列表中使用 CSS 变量并让它在旧版浏览器中工作吗?
- python - 在 Excel 输出中动态调整行高:在单元格中添加了额外的行?
- android - 如何仅通过一个 Peerconnection 在 webrtc(android) 中支持多人(超过 2 个)调用?
- android - 无法在 android 设备中安装我相同的签名 apk 两次
- android - 如何为一个接受用户提供的报价并接受/拒绝/提供还价作为输出的 android 应用程序创建 AI?
- excel - 使用 Excel VBA 在 Outlook 邮件中粘贴的正文后插入签名
- mysql - MySQL中添加约束语法的区别?
- spring-boot - run() 上的 Spring Boot 语法
- c++ - 'C 不支持默认参数' 使用 Swift 桥接头
- html - 设置弹性项目的高度或根据内容扩展?