Ç - 共享内存 - 共享的内部结构动态数组 [英] C - shared memory - dynamic array inside shared struct

查看:289
本文介绍了Ç - 共享内存 - 共享的内部结构动态数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图共享一个结构类似这样的结果
例如:

  typedef结构{
    INT *一个;
    INT B:
    INT℃;
}前;

进程之间,问题是,当我初始化'一'用的malloc,就变成私有到做到这一点(或至少我认为这是会发生什么)进程的堆。有什么办法来创建这个结构的作品共享内存(使用shmget的,的shmat)?

编辑:我正在Linux的结果
编辑:我有一个初始化的缓冲这样一个过程:

 的key_t键= ftok(克,P);
INT中旬= shmget的(键,的sizeof(前),IPC_CREAT | 0666);
前* E = NULL;
状态b_status =的init(急症室,8); //初始化给出初始值B C和分配空间'一'用的malloc
E =(前*)的shmat(中,NULL,0);

其他进程重视自己这样的共享内存:

 的key_t键= ftok(克,P);
INT的shmid = shmget的(键,的sizeof(前),0);
可执行程​​序;
E =(前*)的shmat(的shmid,NULL,0);

和稍后从得到的元素,在这种情况下,在位置1

  INT I = get_el(即,1);


解决方案

在使用的malloc分配的指针内存()是专用于这一进程。所以,当您尝试访问该指针在另一个进程中你有可能会访问一个无效的内存页面,或在另一个进程的地址空间映射到一个内存页(其中比它malloced过程除外)。所以,你可能会得到段错误。

如果您使用的是共享内存,你必须确保所有的数据要公开给其他进程是,在共享内存段和过程不是私有内存段。

您可以尝试,在离开数据指定在存储器段,它可以在编译的时候进行具体定义或放置在一个场在共享存储器段一些已知的位置偏移量。

例如:
如果你正在做这个

的char *纪念品=的shmat(shmid2,(无效*)0,0);

  //所以,MYSTRUCT类型为偏移0。
MYSTRUCT * structptr =(MYSTRUCT *)MEM;//现在我们有一个structptr,使用一个偏移量来得到一些other_type。
other_type *其它=(other_type *)(MEM + structptr-> offset_of_other_type);

其它方式将是具有固定大小的缓冲区使用共享存储器的方法,而不是使用动态分配的指针传递的信息。

希望这有助于。

i'm trying to share a struct like this
example:

typedef struct {
    int* a;
    int b;
    int c;
} ex;

between processes, the problem is that when I initialize 'a' with a malloc, it becomes private to the heap of the process that do this(or at least i think this is what happens). Is there any way to create a shared memory (with shmget, shmat) with this struct that works?

EDIT: I'm working on Linux.
EDIT: I have a process that initialize the buffer like this:

key_t key = ftok("gr", 'p');   
int mid = shmget(key, sizeof(ex), IPC_CREAT | 0666);    
ex* e = NULL;
status b_status = init(&e, 8); //init gives initial values to b c and allocate space for 'a' with a malloc
e = (ex*)shmat(mid, NULL, 0);

the other process attaches himself to the shared memory like this:

key_t key = ftok("gr", 'p');
int shmid = shmget(key, sizeof(ex), 0);
ex* e;
e = (ex*)shmat(shmid, NULL, 0);  

and later get an element from a, in this case that in position 1

int i = get_el(e, 1);

解决方案

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.

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.

Eg: If you are doing this

char *mem = shmat(shmid2, (void*)0, 0);

// So, the mystruct type is at offset 0.
mystruct *structptr = (mystruct*)mem;

// Now we have a structptr, use an offset to get some other_type.
other_type *other = (other_type*)(mem + structptr->offset_of_other_type);

Other way would be to have a fixed size buffer to pass the information using the shared memory approach, instead of using the dynamically allocated pointer.

Hope this helps.

这篇关于Ç - 共享内存 - 共享的内部结构动态数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆