diff --git a/boost/README.md b/boost/README.md new file mode 100644 index 0000000..c90a236 --- /dev/null +++ b/boost/README.md @@ -0,0 +1,14 @@ +### Intro + +This is an implementation of shared memory between `.cpp` and `.py` processes using the `boost/interprocesses` library to create the shared memory in `.cpp` and then using the `mem` library to read from the shared memory in `.py`. + +This example aims to write values to the shared memory in the `.cpp` process and then read the values in the `.py` process and print it out. + +The `.py` code is NOT tested and so may not work but aligns very closely with the documentation of the `mem` library (given below in resources). + + +### Resources +[StackOverFlow Post on installing Boost on Ubunutu](https://stackoverflow.com/questions/12578499/how-to-install-boost-on-ubuntu) +[Building Boost Guide](https://anycoder.wordpress.com/2014/04/28/building-boost/) +[SourceForge Boost Instaler](https://anycoder.wordpress.com/2014/04/28/building-boost/) +[Python `mmap` Library Documentation](https://docs.python.org/3/library/mmap.html) \ No newline at end of file diff --git a/boost/sharedMem.py b/boost/sharedMem.py new file mode 100644 index 0000000..1aea4a1 --- /dev/null +++ b/boost/sharedMem.py @@ -0,0 +1,8 @@ +import mmap + +if __name__ == "__main__": + with open("myshm", "r+b") as f: + shm_a = mmap.mmap(f.fileno(), 0) + print(shm_a.readline()) + print(shm_a[0:4]) + shm_a.close() \ No newline at end of file diff --git a/boost/sharedMemBoost.cpp b/boost/sharedMemBoost.cpp new file mode 100644 index 0000000..d6a16fb --- /dev/null +++ b/boost/sharedMemBoost.cpp @@ -0,0 +1,28 @@ +#include +#include + +int main(int argc, char* argv[]) +{ + using namespace boost::interprocess; + + //Remove shared memory on construction and destruction + struct shm_remove + { + shm_remove() { shared_memory_object::remove("myshm"); } + ~shm_remove() { shared_memory_object::remove("myshm"); } + } remover; + + //Create a shared memory object. + shared_memory_object shm(create_only, "myshm", read_write); + + //Set size + shm.truncate(1000); + + //Map the whole shared memory in this process + mapped_region region(shm, read_write); + + //Write all the memory to 1 + std::memset(region.get_address(), 1, region.get_size()); + + std::system("pause"); +} \ No newline at end of file diff --git a/sysv/README.md b/sysv/README.md new file mode 100644 index 0000000..fc8e037 --- /dev/null +++ b/sysv/README.md @@ -0,0 +1,16 @@ +### Intro + +This is an implementation of shared memory between `.cpp` and `.py` processes using the `sys/ipc.h` library to create the shared memory in `.cpp` and then using the `sysv_ipc` library to write to the shared memory in `.py`. + +This example aims to write values to the shared memory in the `.cpp` process and then read the values in the `.py` process and print it out. + +The `.py` code is NOT tested and so may not work but aligns very closely with the documentation of the `mem` library (given below in resources). + + +### Resources +[StackOverFlow Post on sample code](https://github.com/mruffalo/sysv_ipc/tree/master/demo) +[Geeks for Geeks Sample Code](https://www.geeksforgeeks.org/ipc-shared-memory/) +[Sysv_ipc Github Demo Code](https://github.com/mruffalo/sysv_ipc/tree/master/demo/) +[Second Github Sample for sysv_ipc](https://github.com/ajaygunalan/IPC_SHM/tree/master) +[Third Github Sample for sysv_ipc](https://github.com/dovanhuong/ipc_shared_memory_cpp_and_python/blob/main/shared_mem.cpp) +[Python `sysv_icp` PyPi Install](https://pypi.org/project/sysv-ipc/) \ No newline at end of file diff --git a/sysv/sharedMem.py b/sysv/sharedMem.py new file mode 100644 index 0000000..8faf9bb --- /dev/null +++ b/sysv/sharedMem.py @@ -0,0 +1,10 @@ +import sysv_ipc + +shm = sysv_ipc.SharedMemory(777) +if shm: + s = shm.read() + s = s.decode() + i = s.find('\0') + if i != -1: + s = s[:i] + print(s) \ No newline at end of file diff --git a/sysv/sharedMemSys.cpp b/sysv/sharedMemSys.cpp new file mode 100644 index 0000000..800169f --- /dev/null +++ b/sysv/sharedMemSys.cpp @@ -0,0 +1,18 @@ +#include +#include +#include +#include + +// Create the shared memory +shmid = shmget(777, 512 * 8, IPC_CREAT | 0666) +// Attach the memory. +shared_memory = shmat(shmid, NULL, 0) + +char s[1024]{'Hi! This is me writing a string to the shared memory!'}; +std::strcpy((char*)shared_memory, s); + +//Output contents of shared memory +for (int i = 0; i < 63; i += 3) +{ + std::cout << (char*)shared_memory[i] << " " << (char*)shared_memory[i + 1] << " " << (char*)shared_memory[i + 2] << "\n"; +} \ No newline at end of file