首页 > 解决方案 > 如何使用共享内存实现 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); 

} 

代码有问题吗?

提前致谢。

编辑:它运行良好然后它开始给出分段错误错误我不知道为什么

标签: clinuxipcshared-memory

解决方案


推荐阅读