From 767349118c12b982a73c48ef54870af1dde2ef5b Mon Sep 17 00:00:00 2001 From: smzalam Date: Wed, 1 May 2024 20:31:37 -0400 Subject: [PATCH 1/4] pushed two imperfect implementation of shared memory b/w cpp and py processes --- boost/README.md | 14 ++++++++++++++ boost/sharedMem.py | 8 ++++++++ boost/sharedMemBoost.cpp | 28 ++++++++++++++++++++++++++++ sysv/README.md | 12 ++++++++++++ sysv/sharedMem.py | 12 ++++++++++++ sysv/sharedMemSys.cpp | 16 ++++++++++++++++ 6 files changed, 90 insertions(+) create mode 100644 boost/README.md create mode 100644 boost/sharedMem.py create mode 100644 boost/sharedMemBoost.cpp create mode 100644 sysv/README.md create mode 100644 sysv/sharedMem.py create mode 100644 sysv/sharedMemSys.cpp 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..30b9876 --- /dev/null +++ b/sysv/README.md @@ -0,0 +1,12 @@ +### 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`. + +We desire the usecase of writing to the shared memory in `.cpp` and then reading the values with the `.py` process. The following repo gives an excellent example of how to write and read to shared memory using both `.cpp` (`.c` particularly) and `.py`: [Github Demo](https://github.com/mruffalo/sysv_ipc/tree/master/demo). + + +### 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/) +[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..474e57b --- /dev/null +++ b/sysv/sharedMem.py @@ -0,0 +1,12 @@ +import sysv_ipc + +shm = sysv_ipc.SharedMemory(777) +if shm: + offset = 0 + for idx in range(0, 21): + shm.write(1, offset) + offset += 4 + shm.write(2, offset) + offset += 4 + shm.write(3, offset) + offset += 4 \ No newline at end of file diff --git a/sysv/sharedMemSys.cpp b/sysv/sharedMemSys.cpp new file mode 100644 index 0000000..a5adda0 --- /dev/null +++ b/sysv/sharedMemSys.cpp @@ -0,0 +1,16 @@ +#include +#include +#include +#include + +// Create the shared memory +shmid = shmget(777, 512 * 8, IPC_CREAT | 0666) +// Attach the memory. +shared_memory = (char*) shmat(shmid, NULL, 0) +// Create pointer to shared memory +fptr = reinterpret_cast(shared_memory); +// Output contents of shared memory +for (int i = 0; i < 63; i += 3) +{ + std::cout << fptr[i] << " " << fptr[i + 1] << " " << fptr[i + 2] << "\n"; +} \ No newline at end of file From 95482d28e9c558f2ba2191b3a21116f617c9e32e Mon Sep 17 00:00:00 2001 From: smzalam Date: Wed, 1 May 2024 22:14:07 -0400 Subject: [PATCH 2/4] updated code to fit actual desired workflow --- sysv/README.md | 4 +++- sysv/sharedMem.py | 14 ++++++-------- sysv/sharedMemSys.cpp | 12 +++++++----- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/sysv/README.md b/sysv/README.md index 30b9876..94d3476 100644 --- a/sysv/README.md +++ b/sysv/README.md @@ -2,7 +2,9 @@ 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`. -We desire the usecase of writing to the shared memory in `.cpp` and then reading the values with the `.py` process. The following repo gives an excellent example of how to write and read to shared memory using both `.cpp` (`.c` particularly) and `.py`: [Github Demo](https://github.com/mruffalo/sysv_ipc/tree/master/demo). +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 diff --git a/sysv/sharedMem.py b/sysv/sharedMem.py index 474e57b..8faf9bb 100644 --- a/sysv/sharedMem.py +++ b/sysv/sharedMem.py @@ -2,11 +2,9 @@ shm = sysv_ipc.SharedMemory(777) if shm: - offset = 0 - for idx in range(0, 21): - shm.write(1, offset) - offset += 4 - shm.write(2, offset) - offset += 4 - shm.write(3, offset) - offset += 4 \ No newline at end of file + 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 index a5adda0..800169f 100644 --- a/sysv/sharedMemSys.cpp +++ b/sysv/sharedMemSys.cpp @@ -6,11 +6,13 @@ // Create the shared memory shmid = shmget(777, 512 * 8, IPC_CREAT | 0666) // Attach the memory. -shared_memory = (char*) shmat(shmid, NULL, 0) -// Create pointer to shared memory -fptr = reinterpret_cast(shared_memory); -// Output contents of shared 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 << fptr[i] << " " << fptr[i + 1] << " " << fptr[i + 2] << "\n"; + std::cout << (char*)shared_memory[i] << " " << (char*)shared_memory[i + 1] << " " << (char*)shared_memory[i + 2] << "\n"; } \ No newline at end of file From 5f375ae6d5369b857c10c341150620c3b1278086 Mon Sep 17 00:00:00 2001 From: smzalam Date: Wed, 1 May 2024 23:23:42 -0400 Subject: [PATCH 3/4] added second sysv_ipc sample code --- sysv/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/sysv/README.md b/sysv/README.md index 94d3476..621c698 100644 --- a/sysv/README.md +++ b/sysv/README.md @@ -11,4 +11,5 @@ The `.py` code is NOT tested and so may not work but aligns very closely with th [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) [Python `sysv_icp` PyPi Install](https://pypi.org/project/sysv-ipc/) \ No newline at end of file From e85b1224a8c6f0d4bcfda6319c922dd9f0ecb621 Mon Sep 17 00:00:00 2001 From: smzalam Date: Wed, 1 May 2024 23:26:56 -0400 Subject: [PATCH 4/4] added third sysv_ipc sample code --- sysv/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/sysv/README.md b/sysv/README.md index 621c698..fc8e037 100644 --- a/sysv/README.md +++ b/sysv/README.md @@ -12,4 +12,5 @@ The `.py` code is NOT tested and so may not work but aligns very closely with th [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