QSharedMemory, Read / Write and Read Only.
-
wrote on 23 Oct 2020, 05:58 last edited by SPlatten
I'm looking at using the QSharedMemory and I've downloaded the example application at:
https://doc.qt.io/qt-5/qtcore-ipc-sharedmemory-example.htmlI want to create an area of shared memory that an application is able to Read and Write, and then only allow other applications Read only.
I can see the class documentation at:
https://doc.qt.io/qt-5/qsharedmemory.htmlAre there any good tutorials on how to achieve what I want to do?
-
wrote on 23 Oct 2020, 06:09 last edited by
Hmm...I think that's impossible.
-
@Bonnie , thank you Bonnie, nothings impossible :), if QSharedMemory can't do it then I need to roll my own.
-
wrote on 23 Oct 2020, 06:22 last edited by
@Bonnie , kind of, the difference is, I'm designing and writing an application where the application thats launched can be configured to launch other modules. Each module will create manage its own shared memory area, the shared memory will be read/write to the module and read only to the application that launched it.
The main application will use the modules to provide additional services, the idea is that this system will be expandable without code changes to allow new modules and additional functionality.
-
@Bonnie , kind of, the difference is, I'm designing and writing an application where the application thats launched can be configured to launch other modules. Each module will create manage its own shared memory area, the shared memory will be read/write to the module and read only to the application that launched it.
The main application will use the modules to provide additional services, the idea is that this system will be expandable without code changes to allow new modules and additional functionality.
@SPlatten well, as far as I understand it,
QSharedMemory::attach and ::create both accept an QSharedMemory::AccessMode flag as optional parameter and that can either be ReadOnly or ReadWrite
doesn't that cover your case ?
-
wrote on 25 Oct 2020, 17:09 last edited by
@J-Hilk, thank you, is there an example of this ?
-
@SPlatten said in QSharedMemory, Read / Write and Read Only.:
is there an example of this ?
An example on how to pass an enum to QSharedMemory::attach()??
-
wrote on 26 Oct 2020, 02:16 last edited by Bonnie
As my understanding, the OP want the shared memory to only be ReadWrite to the application which create it, but ReadOnly to other applications which use this shared memory.
So I thought he can't control all the applications. (Otherwise he can just tell these applications don't write.)
But QSharedMemory::AccessMode is controlled by the application which calls QSharedMemory::attach().
Let's say if I know the OP's shared memory key, then I can write an application to attach it by QSharedMemory::ReadWrite and overwrite his shared memory.
So I thought that's not he wants.
@SPlatten Is my understanding right? -
@SPlatten
Since shared memory may be handled differently by different OSes, and your request may not be doable, you should at least state which platform(s) you wish to support? -
@JonB , I want to support all platforms which is why I'm going to implement my own solution.
@SPlatten Do you know about the existence of QRemoteObjects ?
It may be more suitable for your project -
@JonB , I want to support all platforms which is why I'm going to implement my own solution.
wrote on 26 Oct 2020, 08:25 last edited by JonB@SPlatten said in QSharedMemory, Read / Write and Read Only.:
@JonB , I want to support all platforms which is why I'm going to implement my own solution.
From what I have seen reading around (not very thorough) I don't think you can do this, certainly not on all platforms. It may be possible to export it read-only to child processes, but I don't see that for non-related processes? If it's not doable at the OS level, then
QSharedMemory
isn't going to be able to do it, for unrelated/non-Qt processes?When people start talking about, say,
QRemoteObjects
, I think you should clarify whether the other processes you want to share with are under your control/even written in Qt?- Do you have any control over the other processes?
- Are these other processes run as sub-processes of your Qt process, or quite separately?
-
@SPlatten said in QSharedMemory, Read / Write and Read Only.:
@JonB , I want to support all platforms which is why I'm going to implement my own solution.
From what I have seen reading around (not very thorough) I don't think you can do this, certainly not on all platforms. It may be possible to export it read-only to child processes, but I don't see that for non-related processes? If it's not doable at the OS level, then
QSharedMemory
isn't going to be able to do it, for unrelated/non-Qt processes?When people start talking about, say,
QRemoteObjects
, I think you should clarify whether the other processes you want to share with are under your control/even written in Qt?- Do you have any control over the other processes?
- Are these other processes run as sub-processes of your Qt process, or quite separately?
wrote on 26 Oct 2020, 08:49 last edited by@JonB , I only want this solution for all processes in my solution, so for example the main process will create the framework for the memory map and then other 'modules' which provide additional functionality to the main process will add they're own additional memory maps. The idea being that each process that creates there own memory map has read and write access to the map, ever other process that is not the owner will have read only access.
For each memory map I will add flags to the front of the map which indicate when the memory has been updated.
-
@SPlatten Do you know about the existence of QRemoteObjects ?
It may be more suitable for your projectwrote on 26 Oct 2020, 08:50 last edited by@J-Hilk No, I didn't but will look into it, thank you.
-
@JonB , I only want this solution for all processes in my solution, so for example the main process will create the framework for the memory map and then other 'modules' which provide additional functionality to the main process will add they're own additional memory maps. The idea being that each process that creates there own memory map has read and write access to the map, ever other process that is not the owner will have read only access.
For each memory map I will add flags to the front of the map which indicate when the memory has been updated.
wrote on 26 Oct 2020, 08:56 last edited by@SPlatten
Then I think I would at least read the proposed solutuon in, say, https://stackoverflow.com/a/35003672/489865So one thing you can do is shm_open(..., O_RDWR) in the parent and set up a writable mapping for the parent before closing the handle, then shm_open(..., O_RDONLY) to get a read-only file handle which you will pass to the children
I do not know how
QSharedMemory
operates, but if it indeed leverages thisshm_...
approach it may be relevant.I will keep quiet now, as I know no more than this....
1/16