Unable to read data from share memory
-
Hii all,
I am try to read data from share memory and it is showing
"Unable to attach to shared memory segment: "QSharedMemory::handle:: UNIX key file doesn't exist" "
hear is my source code for read data from share memory```
code_text#include <QSharedMemory> #include <QDebug> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); // Name of the shared memory segment QString shmKey = "my_memory"; // Create a shared memory object QSharedMemory sharedMemory(shmKey); // Attach to the shared memory segment if (!sharedMemory.attach()) { qDebug() << "Unable to attach to shared memory segment:" << sharedMemory.errorString(); return 1; } // Read data from the shared memory char *from = static_cast<char*>(sharedMemory.data()); QString message = QString::fromUtf8(from); qDebug() << "Data read from shared memory:" << message; return a.exec(); } -
Hii all,
I am try to read data from share memory and it is showing
"Unable to attach to shared memory segment: "QSharedMemory::handle:: UNIX key file doesn't exist" "
hear is my source code for read data from share memory```
code_text#include <QSharedMemory> #include <QDebug> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); // Name of the shared memory segment QString shmKey = "my_memory"; // Create a shared memory object QSharedMemory sharedMemory(shmKey); // Attach to the shared memory segment if (!sharedMemory.attach()) { qDebug() << "Unable to attach to shared memory segment:" << sharedMemory.errorString(); return 1; } // Read data from the shared memory char *from = static_cast<char*>(sharedMemory.data()); QString message = QString::fromUtf8(from); qDebug() << "Data read from shared memory:" << message; return a.exec(); }@Biswa
As https://doc.qt.io/qt-6/qsharedmemory.html#details says:One QSharedMemory object must create() the segment and this call specifies the size of the segment. All other processes simply attach() to the segment that must already exist.
So where have you, either in this process or another process, called bool QSharedMemory::create(qsizetype size, QSharedMemory::AccessMode mode = ReadWrite)? I imagine that is what error message
UNIX key file doesn't existindicates. Tell me, does the message really include the wordUNIXin it?? -
@JonB
I want to write data in Shared Memory using python language and that data i want to read from Shared Memory.@Biswa Then how do you create shared memory in python? Can you share the code? Are you sure they are using the same mechanism?
One thing need to be noticed is that, when you create a QSharedMemory object by "QSharedMemory sharedMemory(shmKey);", shmKey will not be the actual key used by the native API. -
@Biswa Then how do you create shared memory in python? Can you share the code? Are you sure they are using the same mechanism?
One thing need to be noticed is that, when you create a QSharedMemory object by "QSharedMemory sharedMemory(shmKey);", shmKey will not be the actual key used by the native API.@Bonnie @Bonnie
for writing data in shared memory am using pythoncodeimport posix_ipc import mmap import os import struct SHARED_MEMORY_NAME = "/my_memory123" MEMORY_SIZE = 1024 # Create or open shared memory segment shared_memory = posix_ipc.SharedMemory(SHARED_MEMORY_NAME, posix_ipc.O_CREAT, size=MEMORY_SIZE) # Map the shared memory segment to memory mapfile = mmap.mmap(shared_memory.fd, MEMORY_SIZE) # Example integers to write integers = [0x02, 0x03, 0x04, 0x05] # List of integers # Convert integers to bytes data = struct.pack(f'{len(integers)}I', *integers) # Write integer data to shared memory mapfile.seek(0) # Move to the start of the shared memory segment mapfile.write(data) print("Integer data written to shared memory.") # Clean up mapfile.close() os.close(shared_memory.fd) -
@Bonnie @Bonnie
for writing data in shared memory am using pythoncodeimport posix_ipc import mmap import os import struct SHARED_MEMORY_NAME = "/my_memory123" MEMORY_SIZE = 1024 # Create or open shared memory segment shared_memory = posix_ipc.SharedMemory(SHARED_MEMORY_NAME, posix_ipc.O_CREAT, size=MEMORY_SIZE) # Map the shared memory segment to memory mapfile = mmap.mmap(shared_memory.fd, MEMORY_SIZE) # Example integers to write integers = [0x02, 0x03, 0x04, 0x05] # List of integers # Convert integers to bytes data = struct.pack(f'{len(integers)}I', *integers) # Write integer data to shared memory mapfile.seek(0) # Move to the start of the shared memory segment mapfile.write(data) print("Integer data written to shared memory.") # Clean up mapfile.close() os.close(shared_memory.fd)@Biswa Seems this
posix_ipcandQSharedMemoryboth useshm_openAPI under POSIX, I suppose this could work.
As I said, here we need to letQSharedMemoryuse "/my_memory123" as the native API key (to call shm_open).
So instead of creating aQSharedMemorywith key directly, try to useQSharedMemory sharedMemory; sharedMemory.setNativeKey("/my_memory123");P.S. There's some helpful information in Qt's documentation about this kind of case.
For Qt5, read the Warning part of https://doc.qt.io/qt-5/qsharedmemory.html#details
For Qt6, read https://doc.qt.io/qt-6/native-ipc-keys.html#interoperability-with-non-qt-applications