c - 在共享内存中初始化一维或二维数组

标签 c arrays linux posix shared-memory

我正在尝试将字符串的 2D 字符数组初始化到 POSIX 共享内存中,以便在其他 3 个进程之间共享。有很多关于如何使用指针在进程之间共享单个字符串或整数的教程,但我找不到有关如何使用 mmap() 初始化一维或二维数组的示例。我已经在下面发布了到目前为止我所拥有的内容。这是第一个程序,它创建共享内存对象并使用值 files[0][0][0] 初始化数组 char files[20][2][100] ='\0'

在 C 中初始化和共享数组的正确方法是什么?

就上下文而言,我编写了这个项目的一个简单版本(根据乐于助人的 S.O. 大师的建议),它不使用共享内存并结合了所有 4 个进程(由 /********* 分隔) */) 合二为一。就在下面。

我在上一篇文章中问过涉及结构的类似问题,但我需要在我的项目中使用多维数组。对这个答案的任何见解都会有帮助HERE

谢谢。

到目前为止的代码:(程序初始化共享内存对象和数组)

#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/mman.h>

int main (int argc, char *argv[])
{
    char files [20][2][100]; 


    /* the size of shared memory object */
    int size = sizeof(files);

    /* name of the shared memory object */
    const char *name = "/PROJ4_SHARED_MEM";

    /* shared memory file descriptor */
    int shm_fd;

    /* pointer to shared memory obect */
    void *ptr;

    /* create the shared memory object */
    shm_fd = shm_open(name, O_CREAT | O_RDRW, 0666);

    /* configure the size of the shared memory object */
    ftruncate(shm_fd, size);

    /* memory map the shared memory object */
    ptr = mmap(0, size, PROT_WRITE, MAP_SHARED, shm_fd, 0);

    /* save array to the shared memory object. */
    /*****WHERE I LOSE IT*****/

    return 0;
}

上下文程序:(不是 POSIX)

#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>

int main (int argc, char *argv[])
{
    char files [20][2][100]; 
    files [0][0][0] = '\0';
    char file_name[100];

    char file_contents[100];
    char search[100];

    char answer;
    int again = 0;
    int counter;
    int i = 0;

    /**************************************************/

    while (again == 0)
    {
        printf("Enter a filename:  ");
        scanf("%s", &file_name);
        getchar();
        printf("Enter file contents (string):  ");
        fgets (file_contents, 100, stdin);

        for (i = 0; i < 20; i++)
        {
            if (files[i][0][0] == '\0')
            {
                strcpy(files[i][0],file_name);
                strcpy(files[i][1],file_contents);
                files [i+1][0][0] = '\0';       
                break;
            }
            else if (i == 19)
            {
                printf("ERROR: The directory is full.\n\n");
            }
        }

        counter++; 

        printf("Save another file y/n ?:  ");
        scanf(" %c", &answer);

        if (answer == 'n')
        {
            again = 1;
        }

    }
    printf("\n\n");
    again = 0;

    /**************************************************/

    printf("You have saved the following files:\n");
    for (i = 0; i < 20; i++)
    {
        if (files[i][0][0] == '\0')
        {
            break;
        }

        printf("%s \n", files[i][0]);
    }
    printf("\n\n");

    /**************************************************/

    printf("Would you like to open a file? (y/n) ?:  ");
    scanf(" %c", &answer);
    if (answer == 'n')
        again = 1;

    while (again == 0)
    {
        printf("Enter a filename:  ");
        scanf(" %s", &file_name);

        for (i = 0; i < 20; i++)
        {
            if (strcmp(files[i][0], file_name) == 0)
            {
                printf("%s \n", files[i][1]);       
                break;
            }
            else if (i == 19)
            {
                printf("ERROR: The file was not found.\n\n");
            }
        }

        printf("Search for another file (y/n) ?:  ");
        scanf(" %c", &answer);

        if (answer == 'n')
        {
            again = 1;
        }
        printf("\n\n");
    }


    getchar();

    return 0;
}

最佳答案

代码有一个指向共享内存的指针 因此,就像任何指针一样, 该代码可以访问共享内存中的任何特定项目 通过索引该指针。

#define INDEX_1_SIZE (20)
#define INDEX_2_SIZE (2)
#define INDEX_3_SIZE (100)
#define SIZE         (INDEX_1_SIZE * INDEX_2_SIZE * INDEX_3_SIZE)
ptr = mmap(0, SIZE, PROT_WRITE, MAP_SHARED, shm_fd, 0);

然后:

int i, j, k; // loop counters

// initialize the shared memory to '\0'
for(i=0;i<INDEX_1_SIZE; i++_
{
    for(j=0; j<INDEX_2_SIZE; j++)
    {
        for(k=0;k<INDEX_3_SIZE; k++)
        {
            ptr[i][j][k] = '\0';
        }
    }
}

有更快的方法来对“\0”执行初始化 例如:

memset( ptr, '\0', SIZE );

不需要局部变量:'files'

但是,由于许多不同的进程可以访问 同时共享内存,
该代码需要一个所有进程都使用的命名信号量 所以只有一个进程正在访问共享内存 任何时候,避免“竞争”条件。 这是关于信号量的很好的讨论,带有示例用法 http://www.cs.cf.ac.uk/Dave/C/node26.html 代码应该检查错误,如下所示: (当然,使用你自己的变量名)

des_mutex = shm_open(MUTEX, O_CREAT | O_RDWR | O_TRUNC, mode);

if (des_mutex < 0)
{
    perror("failure on shm_open on des_mutex");
    exit(1);
}

if (ftruncate(des_mutex, sizeof(pthread_mutex_t)) == -1)
{
    perror("Error on ftruncate to sizeof pthread_cond_t\n");
    exit(-1);
}

关于c - 在共享内存中初始化一维或二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27361763/

相关文章:

c - 如何对微 Controller 上的定时器功能进行单元测试

python - numpy数组的缓冲区分区

php - 在 php 中迭代数组,但在迭代期间添加到数组/从数组中删除

ios - ios中的Web服务

c - Linux 中是否存在用户级可访问字体表?

linux - 如何从命令行参数中转义存储为变量的特殊字符

编译 C 程序给出 "can' t find编译器可执行文件在您的搜索路径(GNU GCC编译器)中“在代码块中

c - pci_find_capability 返回 0 : "device does not support it"

html - 脚本输出结束 - CGI/HTML

java - 引用Java的foreach中的迭代次数