SharedApplication: Linux Shared Memory Wrapper in C++, Lockfree Queue

Introduction

SharedApplication is a class used for sharing data between processes, support malloc between multi-process and could be derived to create multi-process data structure.

SharedQueue is a lockfree queue with fixed capacity, derived from the SharedApplication. Easy to use.

Code

Github:https://github.com/mhy12345/SharedApplication

Usage

SharedApplication

Create a SharedApplication instance with a unique APPKEY. The APPKEY is used to tell the operating system which Shared Memory you are going to alloc.

Then, we can do some basic configuration to our application.

Set the size of the memory you are going to alloc, and this must called before start. If it wasn’t called, then the default size will be 1024 bytes.

Some other configurations are also available.

After the configurations, you can start the sApp, simple write:

Then, in you program, you can use malloc to alloc memory.

In other process, if you use the same APPKEY, and use same order of malloc, and same size, then the contents in the memory allocated should be synchronized.

Shared Queue

The SharedQueue is a private inherited form the SharedApplication, means you can just replace SharedApplication with SharedQueue.

In the main function, you can use  sQueue like a normal queue.

Attentions

  • In the SharedApplication, make sure you alloc each memory block using the same order and size
  • The destructive function must be called, otherwise the shared memory cannot be deleted.

SharedApplication: 共享内存操作原理

共享内存是Linux里面一个进程通讯的实现机制,通过系统API将同一块内存块挂载在到两个进程的内存地址空间中,从而实现共享内存的操作。

对于一块共享内存,我们用Key和Id进行标识,Key为用户自定义的一个值,不同进程倘若使用了同一个Key,即视为他们需要同一块共享内存。通过Key,用户可以向系统申请开一个指定大小的共享内存,并返回这块内存的内部标识Id,之后的挂载操作都需要用Id进行制定。

有关共享内存的API都封装在了<sys/shm.h>文件中,具体有

通过自定义Token新建共享内存,并返回标识符: appId = shmget(TOKEN,size,0666)

通过标识符挂在已经建好的共享内存到本地内存空间: ptr = shmat(appId,NULL,0)

取消挂载: shmdt(ptr)

删除共享内存: shmctl(appId,IPC_RMID,NULL)

每一次需要共享内存,强行调用这些语句看起来比较的繁琐,因此我希望写一个共享内存的操控框架,使得每次只需要极少的代码,就可自动完成诸如共享内存挂载,新建等要求。这也是SharedApplication的编写背景。

 

VHDL学习笔记

VHDL学习笔记

  • entity/architecture:
    • entity:实体,描述所设计的系统的外部接口信号,定义电路设计中所有的输入和输出端口
    • architecture:结构体 ,描述系统内部的结构和行为,理解为实体的派生类,实体只是给定了输入输出的信号类型,而具体输入输出的如何关联,是由architecture决定
    • generic:类属,对于实体外加一些属性定义???
  • 端口方向
    • in
    • out
    • inout
    • buffer
  • process:进程,进程间并行,进程内部串行计算,进程的参数表示进程对于哪一个信号的变化敏感

相关资源

VHDL英文版文档:https://www.nandland.com/vhdl/tutorials/index.html