内存并在共享内存中的指针中传递值

memory and pass values in a Pointer in shared memory

我正在尝试通过共享内存和主动等待在一个父进程和 10 个子进程之间传递一些值。

结构中的某些值是分配外部动态内存的指针 当我试图在字符串中写入数字以传递带有数字的路径文件时出现错误,但我不能,因为内存不存在,我只能在没有数字的情况下执行此操作。

typedef struct {
    char *path[10];
    char *word[10];
    int number_ocurrency[10];
    int flag[10];
} shared_data_type;

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(int argc, char *argv[]) {
    int fd;
    int data_size = sizeof(shared_data_type),i;
    pid_t pid;
    shared_data_type *shared_data;

    if((fd = shm_open("/ex06_searchWord", O_CREAT|O_RDWR|O_TRUNC, S_IRUSR|S_IWUSR))==-1)
    {
        perror("Error at shared memory allocation!\n");
        return 0;
    }
    ftruncate (fd, data_size);
    shared_data = (shared_data_type *)mmap(NULL,data_size,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
    for(i=0; i<10;i++){
        shared_data->flag[i]=0;
        pid=fork();
        if (pid == -1)
        {
            printf("Error at fork!\n");
            return -1;
        }

        if (pid == 0) { /* reader */
            while(!shared_data->flag[i]);
            FILE *file;
            file = fopen(shared_data->path[i], "r");
            if (file == NULL)
            {
                printf("Could not open/find the specified file.\n");
                return -1;
            }
            int size = 0;
            char readChar=NULL;
            char *msg = NULL;
            while((readChar = fgetc(file)) != EOF) {
                msg = (char *) realloc(msg, size+1);
                *(msg + size) = readChar;
                size++;
            }
            *(msg + size) = '[=10=]';

            int count = 0;
            while(msg = strstr(msg, shared_data->word[i]))
            {
                count++;
                msg++;
            }
            shared_data->number_ocurrency[i]=count;
            exit(0);
        }
        if(pid>0){
            shared_data->word[i]="SCOMP";
            char path[16]="files/file1.txt";
            shared_data->path[i]=malloc(sizeof(path)+1);
            sprintf(shared_data->path[i],"files/file%d.txt",i);
            //shared_data->path[i]= "files/file.txt";
            shared_data->number_ocurrency[i]=0;
            shared_data->flag[i]=1;
        }
    }
    for(i=0; i<10;i++){
        wait(NULL);
    }


    for(i=0; i<10;i++){
        printf("The word %s in the son %d appeared: %d times\n",shared_data->word[i],i,shared_data->number_ocurrency[i]);
    }
    if (munmap(shared_data, data_size) == -1)
    {
        perror("Error at unmap!\n");
        return 0;
    }

    if(shm_unlink("/ex06_searchWord")==-1){
        perror("Error at unlink!\n");
        return 0;
    }
    return 0;
}

当您在进程之间共享字节时,您需要确保这些字节包含对所有将要使用它的进程有意义且可以理解的内容。将指针指向共享内存中未共享的内存是没有意义的。并且除非能保证所有进程都将共享内存映射到同一个地址,否则即使将共享内存的绝对指针放在共享内存中也是没有意义的。

如果你愿意,你可以将共享内存分成"slots",并通过在共享内存中放置槽号来获得共享内存中有指针的效果。考虑到映射的基地址,槽号必须在每个进程中与绝对地址相互转换。