IPC question...
Hello again...
I have another C programming question, from my mammoth C learning session, and would like some advice in what method of communication that I should use between processes. I have a parent/child processess created by using fork(). The parent process stores information about a file(s) using the following datatype: Code:
typedef struct file_data{ What would be the best 'method' of communicating this data? Pipes, Message Queue or shared memory? What benefits does one have over the other, and more importantly which is the easier to implement. Again many thanks for your time... |
I understand that the master process will create one entry after another? If so, message queue looks like a good choice. Master process can send one message when has one full entry and it will be processed by the child process.
|
I would use pipes because I know them better than shared memory and messages. If you go with pipes, be careful because the child and parent processes share the file descriptors so each process should close one end of the pipe. I would be interested to see solutions using shared memory or messages if anyone has any.
|
Pipes are the easiest, shared memory is the fastest
If you use shared memory, then you also need some kind of synchronization mechanism - you'll probably find yourself opening and managing a semaphore, too. If you use pipes, synchronization is basically built-in: the reader will just block until something is written to the pipe. |
Finally got it sorted out...
I have decided to use message queues using the example from http://www.ecst.csuchico.edu/~beej/guide/ipc/ as it provides FIFO functionality, which works like a 'work queue' and it has removed the need to linked lists, (which I have found to be a nightmare by themselves). My Parent process adds data to the queue whilst the child waits till there is data there and then starts to work on them. The only way I have found to close the queue is to send a file_data datatype with a 'CLOSE_QUEUE' command to make the child stop listening on the message queue and terminate. Not sure if this is the best way or not. Anyway here is the function that I have come up with to communicate with my child process. It still has some DEBUG statements in it. Code:
void sync_docs() |
All times are GMT -5. The time now is 09:52 AM. |