Как реализовать IP C с использованием общей памяти - PullRequest
0 голосов
/ 11 июля 2020

Мне нужно решить следующую проблему:

реализовать IP C между 3 процессами таким образом, чтобы первый процесс создавал общую память, а затем отправлял сигнал второму и третьему процессам, чтобы они могли подключитесь к этой общей памяти, теперь заставьте второй процесс записать что-то в общую память, а затем отправьте сигнал третьему процессу, чтобы третий процесс читал то, что написал второй процесс.

это что я придумал до сих пор

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); 

} 

Third_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; 

// 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); 

} 

Что-то не так с кодом?

заранее спасибо.

РЕДАКТИРОВАТЬ: он работал хорошо, затем он начал давать сегментацию ошибка ошибка не знаю почему

...