【问题标题】:accessing a structure member in a shared memory "in C"在“C”中访问共享内存中的结构成员
【发布时间】:2012-04-18 18:39:19
【问题描述】:

我正在尝试编写共享结构类型的代码,但是在尝试写入共享内存中的结构成员时出现分段错误,共享内存位于父进程和子进程之间。正如我在代码中显示的那样,我现在只是尝试访问结构成员,所以我可以稍后使用信号量进行同步。

提前感谢。

typedef struct file
{
    char *shmPtr;
} file_entry;

int main (void)
{

    int shmid;
    int n;
    file_entry *entries;

    if (fork() == 0) {
        /*wait for a while*/

        if ((shmid = shmget(20441, sizeof(file_entry), 0666)) == -1) {
            printf("shmget");
            exit(2);
        }

        entries = (file_entry*) shmat(shmid, 0, 0);
        if (entries->shmPtr == (char *) -1) {
            printf("problem2");
            exit(2);
        }

        printf("\nChild Reading ....\n\n");
        printf("%s\n", entries->shmPtr[0]);
        printf("%s\n", entries->shmPtr[1]);
        putchar('\n');
        printf("\nDone\n\n");
    } else {
        if ((shmid = shmget(20441, sizeof(file_entry), IPC_CREAT | 0666)) == -1) {
            printf("problem3");
            exit(2);
        }

        entries = (file_entry *) shmat(shmid, 0, 0);
        if (entries->shmPtr == (char *) -1) {
            printf("problem4");
            exit(2);
        }
        printf("done attachment");  /*the parent prints this statment, then segmentation fault*/

        entries->shmPtr[0]='a';
        entries->shmPtr[1]='b';
        putchar('\n');

        wait();
        shmdt(&shmid);
    }
    exit(0);
}

【问题讨论】:

    标签: c struct posix semaphore shared-memory


    【解决方案1】:

    shmat 返回一个指向共享内存区域的指针。在您的代码中,在调用shmat 之后,entries 指向共享区域。然后,您将该共享区域的前几个字节视为指向 char (shmPtr) 的指针。 shmPtr 的值未初始化,它指向某个随机位置。然后你尝试写入它并得到一个段错误。

    编辑:

    正如 Richard 所建议的,您可以摆脱该结构并只使用 char *。但是,我猜测您使用struct 而不仅仅是char * 的原因是您计划将来向结构添加一些额外的字段。如果是这种情况,您可以使用灵活的数组成员

    typedef struct file
    {
        int flag;
        int blah;
        char shmPtr[];
    } file_entry;
    

    分配就变成了

    shmget(20441, sizeof(file_entry) + bufsize, IPC_CREAT | 0666)
    

    当然,如果缓冲区大小是固定的,你可以硬编码它:

    typedef struct file
    {
        int flag;
        int blah;
        char shmPtr[BUFSIZE];
    } file_entry;
    
    /* ... */
    shmget(20441, sizeof(file_entry), IPC_CREAT | 0666)
    

    【讨论】:

    • 我会详细说明 file_entry 应该被 typedef 为 char *。
    猜你喜欢
    • 1970-01-01
    • 2015-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多