简体   繁体   English

共享内存中指向数组的共享指针,指针似乎没有共享?

[英]Shared pointer to an array in shared memory, pointer doesn't seem shared?

I have an array in shared memory. 我在共享内存中有一个数组。 I want to use a pointer for iterating through this array, which also is meant to be shared. 我想使用一个指针迭代这个数组,这也是为了共享。 Here is what I tried: 这是我尝试过的:

    /* initialize color sequence in shared memory */
    shmkey = ftok("/dev/null",3);   /* executable name and a random number */
    shmid = shmget(shmkey, sizeof(char), 0700 | IPC_CREAT);
    if(shmid < 0){      /* shared memory error check */
        perror("shmget\n"); 
        exit(1);
    }
    queue = (char*) shmat(shmid, NULL, 0);  /* shared memory part of colorSequence */
    printf("queue allocated.\n");

    /* initialize color sequence pointer in shared memory */
    shmkey = ftok("/dev/null//",61);    /* executable name and a random number */
    shmid = shmget(shmkey, sizeof(char), 0700 | IPC_CREAT);
    if(shmid < 0){      /* shared memory error check */
        perror("shmget\n"); 
        exit(1);
    }
    p = (char*) shmat(shmid, NULL, 0);  /* pointer to queue in shared memory */
    printf("queue pointer allocated.\n");
    p = &queue[0];

Now I fork the child and try to change the value p, but a change made in one process doesn't affect the other. 现在我分叉子并尝试更改值p,但是在一个进程中进行的更改不会影响另一个进程。

if(fork() == 0){   /* child process */
    sem_wait(&sem);
    printf("   Child printing *p=%c, p=%p\n",*p,p);
    p++;
    printf("   Child printing after++ p=%c, p=%p\n",*p,p);
    sem_post(&sem);
    exit(0);
}
else{   /* parent process */
    sem_wait(&sem);
    printf("Parent printing *p=%c, p=%p\n",*p,p);
    p+=2;
    printf("Parnet printing after++ p=%c, p=%p\n",*p,p);
    p = NULL;   //even though this, program doesn't fail
    sem_post(&sem);
}

Yet, the output is (Contents of queue is as follows: RBG . . .) : 然而,输出是(队列的内容如下:RBG ...):

Parent printing           *p=R, p=0x7f5c77837000
Parnet printing after++   p=B, p=0x7f5c77837002
   Child printing         *p=R, p=0x7f5c77837000
   Child printing after++ p=G, p=0x7f5c77837001

I couldn't figure out why I get these results even though the pointer is supposed to be shared. 我无法弄清楚为什么我得到这些结果,即使指针应该被共享。 Can you help me fix this? 你能帮我解决这个问题吗? Thanks. 谢谢。

EDIT 编辑

When I try to change the value p points to in parent process, the effects are visible when printing from child process. 当我尝试在父进程中更改值p指向时,从子进程打印时效果是可见的。 However incrementing pointer doesn't work. 但是增量指针不起作用。

You actually have two problems, but one is mitigated by the operating system. 实际上你有两个问题,但有一个是由操作系统缓解的。

The first problem is that you say your shared memory is of size 1 ( sizeof(char) ). 第一个问题是你说你的共享内存大小为1( sizeof(char) )。 This is mitigated by the OS as it rounds it up to page size. 这可以通过操作系统减轻,因为它可以将其四舍五入到页面大小。

The second problem is this: 第二个问题是:

p = (char*) shmat(shmid, NULL, 0);  /* pointer to queue in shared memory */

p = &queue[0];

Here you get p from shared memory, the you overwrite this pointer with another pointer. 在这里你从共享内存获取p ,你用另一个指针覆盖这个指针。

However, this is a moot problem fro two reasons: The first is the one I commented about. 然而,出于两个原因,这是一个没有实际意义的问题:第一个是我评论过的问题。 The second is that even if you don't overwrite the pointer p it will still be "local" to your process so doing eg p++ will not change the copy in shared memory as the actual pointer isn't shared. 第二个是即使你没有覆盖指针p它仍然是你的进程的“本地”,所以做例如p++不会改变共享内存中的副本,因为实际的指针不是共享的。 If you truly wanted to have a pointer in shared memory you would have p be a pointer to a pointer, would have to do (*p)++ to increase both locally and in the shared memory. 如果你真的想在共享内存中有一个指针,那么p将是一个指向指针的指针,必须做(*p)++来增加本地和共享内存中的指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM