Linux 读写整数数组到共享内存

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21227270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:52:35  来源:igfitidea点击:

Read Write Integer Array Into Shared Memory

clinuxgccshared-memoryread-write

提问by Ansh David

The following is the READER-WRITER code for my shared memory.

以下是我的共享内存的 READER-WRITER 代码。

Read Code-

阅读代码-

int main(){
int shmid;
int *array;
int count = 5;
int i = 0;
key_t key = 12345;

shmid = shmget(key, count*sizeof(int), IPC_EXCL);

array = shmat(shmid, 0, SHM_RDONLY);

for(i=0; i<5; i++)
    {
        printf("\n%d---\n", array[i] );
    }

    printf("\nRead to memory succesful--\n");

    shmdt((void *) array);
    return 0;
}

Write Code-

写代码-

int main()
{
int shmid;
int *array;
int count = 5;
int i = 0;
int SizeMem;
key_t key = 12345;

SizeMem = sizeof(*array)*count;

shmid = shmget(key, count*sizeof(int), IPC_CREAT);

array = (int *)shmat(shmid, 0, 0);

array = malloc(sizeof(int)*count);

for(i=0; i<5; i++)
{
    array[i] = i;
}

for(i=0; i<count; i++)
{
    printf("\n%d---\n", array[i]);
}

printf("\nWritting to memory succesful--\n");

shmdt((void *) array);

return 0;
}

After writing to the memory when I try to read, the output are garbage values. Can someone please explain what I have done wrong(The output shows all zeros) Thankyou

当我尝试读取时写入内存后,输出是垃圾值。有人可以解释一下我做错了什么(输出显示全为零)谢谢

采纳答案by user2760375

In the write section, you used malloc()after getting share memory address, so it will be overwritten. You should remove the malloc()line

在写入部分,您malloc()在获取共享内存地址后使用,因此它将被覆盖。你应该删除该malloc()

In the read section, the for loop should look like this

在读取部分,for 循环应如下所示

printf("\n%d---\n", array[i] );

回答by KARTHIK BHAT

In write codeyour trying to attach the shared memory to array variable and also in next step u assigning the same to new location in user space (heap) by calling malloc.

编写代码时,您尝试将共享内存附加到数组变量,并在下一步中通过调用 malloc 将相同的内存分配给用户空间(堆)中的新位置。

So you are loosing the the location from where is the shared memory and writing into new array loacated by malloc.

因此,您正在丢失共享内存所在的位置并写入由 malloc 定位的新数组。

array = (int *)shmat(shmid, 0, 0);
array = malloc(sizeof(int)*count);

Use different pointers if you want an array in user space also or remove the that malloc line.

如果您还想要用户空间中的数组或删除该 malloc 行,请使用不同的指针。

Shared memory will allocate the memory specified by you while creating it. you cant allocate it later by other means.

共享内存将分配您在创建时指定的内存。你以后不能通过其他方式分配它。

回答by oyss

Your malloc overwrites shmat's return value here.You are not writing to share memory but to the memory you just malloced.

您的 malloc 在这里覆盖了 shmat 的返回值。您不是在写入共享内存,而是写入您刚刚分配的内存。

array = (int *)shmat(shmid, 0, 0);

by delete this line.your code runs ok on my machine.

通过删除这一行。你的代码在我的机器上运行正常。

array = malloc(sizeof(int)*count);

数组 = malloc(sizeof(int)*count);

回答by Bhagat Vishal

Leave the data at a specified offset into the memory segment, which can be fixed at compile-time or placed in a field at some known location in the shared memory segment

将数据保留在内存段中的指定偏移处,这可以在编译时固定或放置在共享内存段中某个已知位置的字段中

The memory you allocate to a pointer using malloc()is private to that process. So, when you try to access the pointer in another process (other than the process which malloced it) you are likely going to access an invalid memory page or a memory page mapped in another process address space. So, you are likely to get a segfault.

您分配给使用的指针的内存malloc()对该进程是私有的。因此,当您尝试访问另一个进程(而不是对其进行分配的进程)中的指针时,您可能会访问无效的内存页或映射到另一个进程地址空间的内存页。因此,您很可能会遇到段错误。

If you are using the shared memory, you must make sure all the data you want to expose to other processes is "in" the shared memory segment and not private memory segments of the process.

如果使用共享内存,则必须确保要向其他进程公开的所有数据都“在”共享内存段中,而不是进程的私有内存段中。

You could try, leaving the data at a specified offset in the memory segment, which can be concretely defined at compile time or placed in a field at some known location in the shared memory segment.

您可以尝试将数据保留在内存段中的指定偏移量处,这可以在编译时具体定义或放置在共享内存段中某个已知位置的字段中。

Your code would work by removing the malloc call from your code. I have tried it and its working fine

您的代码将通过从您的代码中删除 malloc 调用来工作。我试过了,效果很好