Shared memory in Unix
I am creating a Daemon in Unix that will have exclusive access to a serial port "/dev/tty01". I am planning to create a Master - Slave process paradigm where there is one master (the daemon) and multiple slaves.
I was thinking of having a structure in "Shared memory" where the slaves can access, and there is only one writer to the memory so I most likely wont need a semaphore. The data will be updated fairly slowly, once every minute for example.
I was looking into what would be the b开发者_开发知识库est possible way to do this, also if I have a structure in shared memory, how can I guarantee that the structure will be contiguous in memory? It is a requirement I must have.
thank you
Shared memory will be hard in this case, because you'll need to lock out the readers when the master is writing, and keep the writer waiting when there still are readers.
Easier would be to have the master to write to a temp file (in the same directory) and rename it to the final name once ready (rename is atomic). This assumes the client to periodically open() and fstat() the file to check if it was touched since the previous read.
Yet another way would be to avoid files, and let the master just maintain its own internal data structures and have a (listen) socket open, which on open() just spits out all the data it currently has. No locking needed.
精彩评论