我正在编写一个实时库,它导出标准化接口(VST)并由外部应用程序托管。
该库必须发布一个可由同一进程中的任何线程查看的表(如果它知道在哪里查找) - 需要明确的是,该表必须可由进程空间中的所有 dll 查看 - 如果它们知道在哪里查找。
访问表必须很快。虚拟内存似乎有点矫枉过正,我已经考虑过使用窗口句柄(我仍然可以)来消息泵,但我更喜欢一种更快的方法(如果有的话)。
另外,如果可能的话,我希望避免 PE 中的共享数据段。我想我宁愿使用窗把手。
我现在不关心同步,我可以事后处理。我只是想要一些关于在进程空间中发布表的最快技术的建议。
我正在尝试从子进程中使用 C++ 中的同步队列。我在 C++ () ( http://www.internetmosquito.com/2011/04/making-thread-safe-queue-in-ci.html ) 中使用这个同步队列
我修改了队列是在升压序列化,也取代了使用boost::mutex io_mutex_,而不是使用inteprocess互斥(感谢@Sehe)boost::interprocess::interprocess_mutex io_mutex_和锁定,当我改为每有行boost::mutex::scoped_lock lock(io_mutex_);到scoped_lock<interprocess_mutex> lock(io_mutex_);
template<class T>
class SynchronizedQueue
{
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & sQueue;
ar & io_mutex_;
ar & waitCondition;
}
... // queue implementation (see [http://www.internetmosquito.com/2011/04/making-thread-safe-queue-in-c-i.html][2])
Run Code Online (Sandbox Code Playgroud)
}
在我的测试应用程序中,我正在创建同步队列并在其中存储此类的 100 个实例:
class gps_position
{
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & degrees; …Run Code Online (Sandbox Code Playgroud) 我目前正在尝试在Linux上的2个进程之间的共享内存中放置结构.我没有问题共享bool或int但是当尝试共享字符串时,std :: string或char我有一个分段错误错误.
现在我的代码是:
#include <iostream>
#include <sys/types.h> //shmat
#include <sys/shm.h>
#include <sys/stat.h> //open
#include <fcntl.h>
#include <unistd.h> //close
using namespace std;
struct Prises{
int numero;
int transactionId;
bool reservation;
bool charge;
bool disponibilite;
bool defaut;
bool verrouillage;
bool trappe;
int LEDverte;
int LEDrouge;
std::string carte;
std::string etat;
};
int main()
{
const char *keyFile = "/tmp/key.dat";
/* Make sure the file exists. */
int descriptor = open(keyFile, O_CREAT | O_RDWR, S_IRWXU);
/* Only wanted to make sure that the …Run Code Online (Sandbox Code Playgroud) 我试图了解我应该如何提取MapViewOfFile的返回缓冲区大小。我使用以下命令分配共享内存
hFileMapping = CreateFileMapping(INVALID_HANDLE_VALUE, nullptr, PAGE_READWRITE, 0, dwDataSize, strSharedMemoryName.c_str());
Run Code Online (Sandbox Code Playgroud)
使用以下代码片段将其填充:
pBuffer = DynamicAPI::MapViewOfFile(hFileMapping, FILE_MAP_WRITE, 0, 0, dwDataSize);
if (nullptr == pBuffer || GetLastError() != 0)
{
LOG_ERROR(L"Failed to MapViewOfFile: " << GetLastError());
break;
}
// Copy buffer to the shared memory
::CopyMemory(pBuffer, pData, dwDataSize);
Run Code Online (Sandbox Code Playgroud)
然后,在其他地方,尝试重新打开该共享内存并读取整个缓冲区:
HANDLE hSharedMemory = OpenFileMapping(FILE_MAP_READ, FALSE, m_strSharedName.c_str());
if (nullptr == hSharedMemory)
{
return false;
}
LPVOID pData = nullptr;
if (nullptr == (pData = MapViewOfFile(hSharedMemory, FILE_MAP_READ, 0, 0, 0)))
{
LOG_ERROR(L"Failed to MapViewOfFile");
return false;
} …Run Code Online (Sandbox Code Playgroud) 今天,我的一个朋友告诉我,Go程序可以在多个CPU内核上进行扩展。听到系统任务调度程序对goroutine一无所知,因此无法在多个内核上运行它们,我感到非常惊讶。
我进行了一些搜索,发现Go程序可以产生多个OS任务以在不同的内核上运行它们(该数目由GOMAXPROCS环境变量控制)。但是据我所知,对流程进行分叉可导致流程数据的完整复制,并且不同的流程在不同的地址空间中运行。
那么Go程序中的全局变量呢?它们可以安全地与多个goroutine一起使用吗?它们是否以某种方式在系统进程之间同步?如果他们这样做,那又如何?我主要关注linux和freebsd的实现。
我在任何地方看到 python 的共享内存实现(例如在 参考资料中multiprocessing),创建共享内存总是分配新内存。有没有办法创建共享内存对象并让它引用现有内存?目的是预先初始化数据值,或者更确切地说,如果我们已经有一个数组,则可以避免复制到新的共享内存中。根据我的经验,分配大型共享数组比将值复制到其中要快得多。
我正在尝试为共享内存对象运行程序。我的代码如下:
#include <stdio.h> /*adding standard input output library*/
#include <stdlib.h> /*standard library for four variable types, several macros, and various functions for performing general functions*/
#include <string.h> /*adding string library*/
#include <sys/fcntl.h> /* library for file control options */
#include <sys/shm.h> /*library for shared memory facility*/
#include <sys/stat.h> /*data returned by the stat() function*/
int main()
{
/* the size (in bytes) of shared memory object */
const int SIZE=4096;
/* name of the shared memory object */
const char …Run Code Online (Sandbox Code Playgroud) 如何拥有一个孤立的内存部分,它根本不支持任何文件或额外的管理层,如管道,可以在同一台Windows机器上的两个专用进程之间共享?
大多数文章都指向了CreateFileMapping的方向.让我们从那里开始:
具有hFile = INVALID_HANDLE_VALUE的CreateFileMapping如何实际工作?根据
https://msdn.microsoft.com/en-us/library/windows/desktop/aa366537(v=vs.85).aspx
它"...创建一个指定大小的文件映射对象,该对象由系统页面文件而不是文件系统中的文件支持..."
假设我在内存中写了一些东西,它由CreateFileMapping映射,并带有hFile = INVALID_HANDLE_VALUE.在什么条件下将此内容写入磁盘上的页面文件?
此外,我对共享内存使用的动机的理解是保持性能和优化.为什么文章"创建命名共享内存"(https://msdn.microsoft.com/de-de/library/windows/desktop/aa366551(v=vs.85).aspx)引用CreateFileMapping,如果没有一个属性组合,会阻止写入文件,例如页面文件?
回到最初的问题:我担心,CreateFileMapping不够好......那么什么会起作用?
我在面试中被问到一个问题,共享内存中是如何进行同步的。我告诉采取一个结构。你有一个标志和一个数据。测试标志并更改数据。我从互联网上获取了以下程序,如下所示-。谁能告诉共享内存中是否有更好的同步方法
#define NOT_READY -1
#define FILLED 0
#define TAKEN 1
struct Memory {
int status;
int data[4];
};
Run Code Online (Sandbox Code Playgroud)
假设服务器端和客户端都在当前目录。服务器使用 ftok() 生成密钥并使用它来请求共享内存。在共享内存充满数据之前,状态设置为 NOT_READY。共享内存填满后,服务器将状态设置为 FILLED。然后,服务器等待,直到状态变为 TAKEN,这意味着客户端已获取数据。
下面是服务器程序。单击此处下载该服务器程序 server.c 的副本。
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include "shm-02.h"
void main(int argc, char *argv[])
{
key_t ShmKEY;
int ShmID;
struct Memory *ShmPTR;
if (argc != 5) {
printf("Use: %s #1 #2 #3 #4\n", argv[0]);
exit(1);
}
ShmKEY = ftok(".", 'x');
ShmID = shmget(ShmKEY, sizeof(struct Memory), IPC_CREAT | 0666);
if (ShmID …Run Code Online (Sandbox Code Playgroud) 我正在尝试从此链接https://docs.python.org/3/library/multiprocessing.shared_memory.html在 python 3.8 中使用新的共享内存示例
# In the first Python interactive shell
import numpy as np
a = np.array([1, 1, 2, 3, 5, 8]) # Start with an existing NumPy array
from multiprocessing import shared_memory
shm = shared_memory.SharedMemory(create=True, size=a.nbytes)
# Now create a NumPy array backed by shared memory
b = np.ndarray(a.shape, dtype=a.dtype, buffer=shm.buf)
b[:] = a[:] # Copy the original data into shared memory
shname = shm.name # We did not specify a name so one was chosen …Run Code Online (Sandbox Code Playgroud)