首页 > 代码库 > Shared Memory共享内存----kithara RTS

Shared Memory共享内存----kithara RTS

翻译注:共享内存是程序之间进行数据交互的最基本的方式,而由于windows和kithara rts本身为两个独立的系统,为了能够使两个不同系统之上的程序进行通信,那么就必须开辟一块内存区域用于数据共享。本文是对kithara rts官方原文进行的翻译,加入了本人的一些使用经验。

Shared Memory(共享内存)

What you need to know about Shared Memory(共享内存基本知识)

In 32-bit and64-bit Windows operating systems a so-called virtual memory management will be used. On the one hand, with this mechanism the protection of the differentaddress spaces against non-authorized access is ensured. On the other hand, forevery application a logical address space of 2 gigabytes for 32-bit or 8terabytes for 64-bit is provided.

在32位和64位Windows操作系统中“虚拟”内存是被经常使用的。虚拟内存可以保护不同的内存地址空间以防止非法访问,而且在32位操作系统中每个应用程序的虚拟内存大小为2G,在64位系统中是8T。【更多关于虚拟内存的知识,可以请自行搜索】

翻译注:虚拟内存并不是真正的内存,对其操作并不会改变真实内存的数据。不同的程序拥有自己独立的虚拟内存,程序之间的虚拟内存是独立的。

The addresses used for data and code within the application programs are always virtual addresses. They will be transformed into physical memory addresses by the memory management of the operating system.

应用程序中使用的内存是虚拟内存,在执行过程中将会由操作系统的内存管理器将其转化为物理内存【或者说映射到物理内存中】。

Since there is aseparate address space provided for every application, it is normally notpossible a process can access the memory of another one. Additionally, you cannormally not access from your application to the kernel memory, because thememory on the kernel-level is protected.

由于每个应用都有独立的地址空间,所以一个进程一般不会访问其他程序的内存。另外,自己的程序一般不能访问内核内存,因为内核层的内存受保护。

In order to realize fast data exchange, memory areas for data exchange between several application programs and also between the application and the kernel level have to be requested separately. The functions for shared memory within the Base Module serve for this.

为了在不同应用层程序之间、应用层程序与内核层程序之间的实现快速数据交换,需要申请一块独立的物理内存区域。用于操作共享内存的功能是Kithara rts的Base模块的一部分。

Normally no real-time with hard disk relocation: Although the Windows API includes mechanisms to provide shared memory, it is normally not protected against a relocation on hard disk.Thereby normally the real-time storage of measured values for example is not possible.

通常没有实时硬盘操作。尽管Windows API包含对共享内存的保护机制,但是一般不会保护硬件重置。因此,不能实现测量数据的实时存储。

翻译注:可能会有人说是否可以用共享硬盘数据的方式进行数据交互,进行数据交互是可以的。但是这样操作没有实时性,无论机械硬盘或电子硬盘都不如内存读写速度快。

Additionally,the normal shared memory (“memory mapped files”) is only accessible from theapplication level and usable for data exchange between different applicationprograms. It is not qualified for using on the kernel level. The memory, whichis provided by the »RealTime Suite« is not restrictedin that way.

此外,windows提供的普通共享内存技术(“内存映射文件”)只有应用层能够访问,而且只能用做不同应用之间的数据交换。不能在内核层中使用。»RealTime Suite«提供的内存不受这样的约束。

Differentvirtual addresses for ring 0 and ring 3

(ring 0与ring3的不同虚拟地址)

Different addresses for the access from the application level (ring 3) resp. from the kernel level (ring 0) have to be used at the memory call. This distinction is necessary, because the application address range and the systemaddress range are not identically. For this reason the Kithara functions for shared memory delivers two different addresses at a time.

应用层程序和内核层程序可访问的内存地址空间是不同的,这两部分内存区域是独立的。因此,Kithara中关于共享内存的函数会同时指定两个不同的地址参数。

翻译注:具体为什么不同,可以参考windows编程的相关资料。

·        pAppPtr the pointer for application level (ring 3)context and

·        pAppPtr 是应用层的指针

·        pSysPtr the pointer for kernel level (ring 0)context.

·        pSysPtr 是内核层的指针

Note: Make sure you use always thepointer for the address space provided addresses, otherwise an ‘accessviolation’ or BSOD will be registered!

Both virtual addresses will transfered as a ‘void’ pointer. In the further, they will be used, after a type conversion into a user specific data type, like a ‘normal’ pointer. So it is possible to read or write very easy and fast in shared memory.

注意:确保使用的指针是地址空间提供的地址,不然会出现“访问冲突”或者BSOD。

翻译注:BSOD,就是蓝屏,常见于低版本的windows,如win98,或者由于软件冲突造成,特别是系统层的软件,如驱动软件、杀毒软件、防火墙等。例如杀毒软件和网银控件有时就会冲突,造成系统蓝屏。

虚拟地址会被转化为“空”指针。数据转化成为特定的数据类型之后(像是“普通”的指针)便会用到。

Management ofShared Memory(共享内存管理)

In the kernel mode, only data can be accessed which is located in the stack (local variables)or in the so called shared memory. For the later the reference parameter pArgs is provided, pArgs is given to a kernel level (ring 0) running callback function to provide the shared memory pointer.

在内核模式下,只有存储在栈(即局部变量)中或者共享内存中的数据可以被访问。参考参数pArgs以后会提供,它用于给运行回调函数的内核层(ring0)提供共享内存指针。

Definite name identifies memory(给内存起个名字)

Normally, shared memory will be identified over a specific name. The principle of several applications is, to require such memory with a known and agreed name. As soon as this memory will be requested under the agreed name again, you receive apointer back to the same memory area. Of course the addresses differ in thedifferent applications! Several applications create their own ‘view’ on thesame memory.

通过内存名称可以获取该内存。

Note: The memory provided by the described functions is protected against a hard disk relocation (“swapping”)!

注意:这些函数使用的内存,是不会换置到硬盘中的(“交换”)。

翻译注:就是说操作系统不会把这块共享内存用于虚拟内存的硬盘换置过程。如果有不明白,请自行查询关于虚拟内存的基本知识。

Requests ofShared Memory(共享内存请求)

Shared memory will be created with the function KS_createSharedMem.This size of the memory has to be given, a name is optional. This function willdeliver the pAppPtr for for application level (ring 3) context and the pSysPtr forkernel level (ring 0) context.

通过函数KS_createSharedMem创建共享内存。内存大小这个参数必须要给出,名称这个参数是可选的。

pAppPtr是指向应用层(ring3)的指针,pSysPtr是指向内核层(ring0)的指针。

Data* pAppPtr;      // for access from application程序访问指针  Data* pSysPtr;      // for access from kernel     内核访问指针Error error = KS_createSharedMem(  (void**)&pAppPtr,  // Address of application pointer  应用层指针地址(void**)&pSysPtr,  // Address of system pointer    系统层指针地址"SharedMemoryName",  // Name of shared memory  共享内存名称sizeof(Data),        // Size of shared memory     共享内存大小0);                  // Flags, here 0               标志,这里为0    

     

To free shared memory the function KS_freeSharedMem can beused.

For a handle based version shared memorycan be created with the function KS_createSharedMemEx.

使用函数KS_freeSharedMem来释放共享内存空间。

可以通过函数KS_createSharedMemEx创建一个带有句柄指定的共享内存。

KSHandle hSharedMem;    // handle to shared mem  共享内存句柄Error error = KS_createSharedMemEx(   &hSharedMem, // Address of shared memory handle  共享内存句柄地址SharedMemoryName",  // Name of shared memory  共享内存名称sizeof(Data),        // Size of shared memory  共享内存大小0);                  // Flags, here 0                标志位,这里为0if( error )   ...  error = KS_getSharedMemEx(  hSharedMem,          // Shared memory handle   &pSysPtr,            // Address of system pointer   KSF_KERNEL_EXEC);    // Flags, here KSF_KERNEL_EXEC for                     //   adress of system pointer  if( error )  ... 

 

The handle hSharedMem is used to free shared memory with KS_freeSharedMemEx and can be used also to easily get the actual memory pointer with KS_getSharedMemEx withine.g. a callback function.

函数KS_freeSharedMemEx可以清空hSharedMem指向的共享内存

函数KS_getSharedMemEx可以获取hSharedMem指向的共享内存,该函数可以被用于回调函数中。

Shared Memory at several applications

(多个应用程序之间的共享内存)

Shared memory isnot only usable for data exchange between application level and kernel level, butalso between several applications.

共享内存不仅可以用于应用层程序和内核层程序的数据交互,也可以用于多个应用层程序之间进行数据交互。

In this case every application program has to call at least once the function KS_createSharedMemEx(KS_getSharedMemEx is not sufficient!).

You can determine if shared memory is already used with taking the advantage that thememory will be generally initialized with 0. Another possible way is to use acount variable, which will be incremented atevery KS_createSharedMemEx and shows the number of the running applications.

在这种情况下,每个应用程序必须至少一次调用函数 KS_createSharedMemEx(KS_getSharedMemEx则不止一次!).

你可以利用内存初始化为0来决定共享内存是否已经被使用。另一种可行的办法是使用计数变量,该变量可以在每次调用函数KS_createSharedMemEx时自加,显示正在运行的程序数量。

After usage of shared memory every application has to call KS_freeSharedMemEx.

每个应用程序在使用完共享内存使用之后,必须调用函数KS_freeSharedMemEx来释放共享内存。