shared-memory

looking for Windows RAM-based shared memory solution in C++

最后都变了- 提交于 2021-02-04 14:54:31
问题 I'm facing a situation where I need to pass up to several hundreds of megabytes of memory from one process to another. Right now I'm doing it via files and it's too slow. I guess that to make it quicker, those files should be written directly to RAM and be accessible from another process. No fancy synchronization required. One process would create shared memory objects and would fill them with data. The other process would read and remove them. However I've done a quick research and it seems

looking for Windows RAM-based shared memory solution in C++

时光毁灭记忆、已成空白 提交于 2021-02-04 14:54:24
问题 I'm facing a situation where I need to pass up to several hundreds of megabytes of memory from one process to another. Right now I'm doing it via files and it's too slow. I guess that to make it quicker, those files should be written directly to RAM and be accessible from another process. No fancy synchronization required. One process would create shared memory objects and would fill them with data. The other process would read and remove them. However I've done a quick research and it seems

POSIX semaphores - cannot determine what causes segmentation fault

若如初见. 提交于 2021-01-29 19:54:19
问题 I'm working on the sleeping barber problem (using FIFO queue and shared memory), and I have a problem. I try to run this program just to see what is shown, however, I get segmentation fault every time. In the code I check, if semaphores are created successfully, if shared memory is created properly, but the program crashing must come from different place, which I cannot seem to find. I also tried to use Valgrind but what I got is: Valgrind for the newer version of code: However, when I try to

Two variables in one shared memory

為{幸葍}努か 提交于 2021-01-29 14:40:28
问题 Is there a way to use one shared memory, shmid = shmget (shmkey, 2*sizeof(int), 0644 | IPC_CREAT); For two variables with different values? int *a, *b; a = (int *) shmat (shmid, NULL, 0); b = (int *) shmat (shmid, NULL, 0); // use the same block of shared memory ?? Thank you very much! 回答1: Apparently (reading the manual) shmat gets you here a single block of memory, of size 2*sizeof(int) . If so, then you can just adjust the pointer: int *a, *b; a = shmat(shmid, NULL, 0); b = a+1; Also,

Storing and accessing an array of struct from shared memory

為{幸葍}努か 提交于 2021-01-29 09:31:22
问题 I am writing a program on a hotel reservation system and have declared a struct Room as follows: struct Room { bool isavailable; bool ispaid; string customer; string date; }; I use a variable read from an input file to create an array of n structs which is all the rooms in the hotel. struct Room* rooms = new Room[numOfRooms]; I then create the shared memory space and attach it, but when I try to access it after, it doesn't seem to work. //creates shared memory space if((shmid = shmget(shmkey,

Initializing shared memory using mmap() for a 2D array, is it necessary to also map memory for subsequent pointers? Should I use shm instead?

元气小坏坏 提交于 2021-01-29 08:48:51
问题 I am using mmap() to initialize a shared memory space for parent and child processes and the object happens to be a double char pointer (i.e. char** ). It's necessary because I'm going to be storing user input to this 2D array in a child process and the parent will be accessing this same data after the child has terminated. It wasn't hard to work this out after reading a little bit of documentation on mmap() , and it feels a lot like malloc() , but less of a commitment. Consider the code

Why I can create a shared memory bigger than the size mounted on /dev/shm using POSIX?

久未见 提交于 2021-01-28 05:46:58
问题 I am trying to handle errors using shared memory IPC in a Ubuntu 16.04. First, I checked the available memory in /dev/shm using df -h, having 500M availables, so I coded something quickly in order to check what happens if I try to create a shared mem bigger than the mounted size. The code is the following (it has been modified several times so I know that is not very tidy): #include <iostream> #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <sys/ipc.h> #include <sys

Read string from shared memory C++ POSIX

偶尔善良 提交于 2021-01-28 05:05:20
问题 In my other question one user help me with send string by shared memory. But when I try to receive this data program says Core dumped . I try this code: key_t lineKey = ftok("/tmp", '1'); int sharedLine = shmget(lineKey, sizeof(std::string), IPC_CREAT | 0666); std::string *line = (std::string *)shmat(sharedLine, NULL, 0); sem_t *reader1_sem; reader1_sem = sem_open("reader1_sem", O_CREAT, 0666, 0); while (1) { sem_wait(reader1_sem); std::cout << (*line); } I tried also rewrite this to char *

unable to construct runtime boost spsc_queue with runtime size parameter in shared memory

主宰稳场 提交于 2021-01-27 22:18:28
问题 I want to make a lock free ring buffer in shared memory using runtime specified maximum number of entries. I am basing my code off an example I found in GitHub. I successfully created a lock free ring buffer in shared memory using this code. In my case, I need to specify the maximum number of entries that the ring buffer can accept at runtime construction and not at compile time per the example. The call to construct the shm::ring_buffer in the example is shown below. namespace bip = boost:

Linux C: Accessing shared memory fails with `Invalid Argument` even though it was just created

北城余情 提交于 2021-01-27 20:01:20
问题 This function of mine is responsible for creating a shared memory segment. As you can see, I check for EEXIST in case there already is a shared memory with this key. As I am executing the program regularly with the same key, this shared memory exists after the first program execution. As a test, I try to access the shared memory directly afterwards via shmat() . But for whatever reason, it fails. This is the output of the console: Shared memory with Key 4661 already exists, continue... Failed