简体   繁体   English

boost的问题:进程间共享内存

[英]problems with boost:interprocess shared memory

I'm having some trouble implementing boost/interprocess library for shared memory operations between two programs.我在为两个程序之间的共享内存操作实现 boost/进程间库时遇到了一些麻烦。 This is the first I've used any shared memory operations, and I have first modified some of the sample code in the boost documentation I found here: ( http://www.boost.org/doc/libs/1_41_0/doc/html/interprocess/quick_guide.html ).这是我第一次使用任何共享内存操作,我首先修改了我在此处找到的 boost 文档中的一些示例代码:( http://www.boost.org/doc/libs/1_41_0/doc/ html/interprocess/quick_guide.html )。

The modified demo works good, and basically is like this:修改后的demo效果很好,基本上是这样的:

typedef std::pair<double, int> MyType;
managed_shared_memory segment(create_only, "MySharedMemory", 65536);

MyType *instance = segment.construct<MyType>
    ("MyType instance")  //name of the object
    (12.34, 0);            //ctor first argument

MyType *instance2 = segment.construct<MyType>
    ("MyType instance2")  //name of the object
    (56.78, 0);            //ctor first argument

And then in a another process, retrieve these variables:然后在另一个进程中,检索这些变量:

managed_shared_memory segment(open_only, "MySharedMemory");

std::pair<MyType*, managed_shared_memory::size_type> res;
std::pair<MyType*, managed_shared_memory::size_type> res2;

res = segment.find<MyType>("MyType instance");
printf("1: %d, %d\n", res.first, res.second); // show pointer and size
printf("1a: %f\n\n", *res.first); //show double value


res2 = segment.find<MyType>("MyType instance2");
printf("2: %d, %d\n", res2.first, res2.second); // show pointer and size
printf("2a: %f\n", *res2.first); // show double value

ok, so all th at looks ok, and the terminal output is:好的,所以看起来一切正常,终端输出是:

1: 196724, 1
1a: 12.340000

2: 196780, 1
2a: 56.780000

The problem is when I try and replicate this in another (existing) application.问题是当我尝试在另一个(现有)应用程序中复制它时。

I have done everything almost identically as far as I can tell (identically in terms of syntax even maybe?), but am getting some different and unexpected results.据我所知,我所做的一切几乎完全相同(甚至可能在语法方面完全相同?),但得到了一些不同的和意想不到的结果。

typedef std::pair<double, int> myDouble;
managed_shared_memory segment(create_only, "sharedMemBlock", 65536);

myDouble *valX = segment.construct<myDouble>
    ("valX")
    (1.1, 0);

myDouble *valY = segment.construct<myDouble>
    ("valY")
    (2.2, 0);

myDouble *valZ = segment.construct<myDouble>
    ("valZ")
    (3.3, 0);

and in the second process to retrieve these values:并在第二个过程中检索这些值:

managed_shared_memory segment(open_only, "sharedMemBlock");
std::pair<myDouble*, managed_shared_memory::size_type> valShrX;
std::pair<myDouble*, managed_shared_memory::size_type> valShrY;
std::pair<myDouble*, managed_shared_memory::size_type> valShrZ;

valShrX = segment.find<myDouble>("valX");
valShrY = segment.find<myDouble>("valY");
valShrZ = segment.find<myDouble>("valZ");

printf("PtrvalSharedX: %d,PtrvalSharedY: %d, PtrvalSharedZ: %d\n", valShrX.first, valShrY.first, valShrZ.first);
printf("valSharedX: %f, valSharedY: %f, valSharedZ: %f\n\n", *valShrX.first, *valShrY.first, *valShrZ.first);

but the results are not what I expect, and are coming out like this:但结果不是我所期望的,并且是这样的:

PtrvalSharedX: 196724, PtrvalSharedY: 196772, PtrvalSharedZ: 196820
valSharedX: 1.100000, valSharedY: 0.000000, valSharedZ: 2.200000

So, what's going on here?那么,这里发生了什么? Why am I not getting 1.1, 2.2 and 3.3 for valSharedX, valSharedY, and valSharedZ respectively?为什么我没有分别为 valSharedX、valSharedY 和 valSharedZ 获得 1.1、2.2 和 3.3?

Not sure why essentially the same code works in one instance, but not another.不确定为什么本质上相同的代码在一个实例中有效,而在另一个实例中无效。 I noticed that the difference between pointers in the first example (196780 - 196724) = 56, but is smaller in the second example (196772 - 196724) = (196820 - 196772) = 48. Not sure if that is relevant, but thought it was worth pointing out (pun!).我注意到第一个例子中的指针之间的差异 (196780 - 196724) = 56,但在第二个例子中更小 (196772 - 196724) = (196820 - 196772) = 48。不确定这是否相关,但认为它值得指出(双关语!)。

Thanks, B谢谢,乙

The problem that everyone overlooked, including myself, was this...包括我自己在内的所有人都忽略了这个问题……

because of the type:因为类型:

std::pair<myDouble*, managed_shared_memory::size_type> valShrX

getting:得到:

valShrX.first

returns a pointer to a myDouble, and not to the first double that is a part of the std::pair that is defined in:返回一个指向 myDouble 的指针,而不是指向 std::pair 中定义的第一个 double 的指针:

typedef std::pair<double, int> myDouble;

Since printf doesn't know what myDouble type really is, and since that type is not natively known by printf, the issue presented only when printing multiple values in a sequence and relates to the size of myDouble not matching the size of a double.由于 printf 不知道 myDouble 类型到底是什么,并且由于 printf 本身不知道该类型,因此仅在打印序列中的多个值时才会出现问题,并且与 myDouble 的大小不匹配的双精度值有关。

So printing each value with its own printf statement (like in the first code excerpt) works fine, because the pointer valShrX.first tells printf where to start correctly.所以用它自己的 printf 语句打印每个值(就像在第一个代码摘录中一样)工作正常,因为指针 valShrX.first 告诉 printf 从哪里开始正确。 Even though the size of valShrX.first must include the int that is a part of the pair, printf prints the %f and stops.即使 valShrX.first 的大小必须包括作为对的一部分的 int,printf 也会打印 %f 并停止。

In the second code excerpt, I tried printing all three values in shared memory in the same printf statement, and so the full size of std::pair myDouble (including therein the double and the int) was propagated and would offset the second and third values printed.在第二个代码摘录中,我尝试在同一个 printf 语句中打印共享内存中的所有三个值,因此传播了 std::pair myDouble(包括其中的 double 和 int)的完整大小,并将抵消第二个和第三个打印的值。

The correct statement should get just the first item of the myDouble pair, that is within the parent pair ... In other words this is the correct printf syntax given my data types:正确的语句应该只获取 myDouble 对的第一项,即在父对中......换句话说,这是给定我的数据类型的正确 printf 语法:

printf("valSharedX: %f, valSharedY: %f, valSharedZ: %f\n\n", valShrX.first->first, valShrY.first->first, valShrZ.first->first);

So, my printf syntax was incorrect in both examples, but the more punitive operation of printf when printing multiple values in one statement was the only case to make the problem apparent.因此,我的 printf 语法在两个示例中都不正确,但是在一个语句中打印多个值时 printf 更具惩罚性的操作是使问题明显的唯一情况。

My only remaining question that is tickling my brain is this: Why did this problem present itself in Visual Studio 2013, but the identical code when compiled in Linux did not show the problem when printf's %f received a myDouble in a series of values being printed?我唯一剩下的问题是:为什么这个问题会出现在 Visual Studio 2013 中,但是当 printf 的 %f 在正在打印的一系列值中收到 myDouble 时,在 Linux 中编译的相同代码没有显示问题? It all worked fine when compiled in linux.在 linux 中编译时一切正常。 why?为什么?

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

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