Here is an example showing how to create a message queue for inter-process communication using the boost library
First compile the application in the following two steps. The project uses cmake to manage the C++ source code:
- cmake .
- make
After compilation, the executable file "message_queue_comm.exe" will be generated. Then start testing the communication through the message queue
./message_queue_comm.exe
Output :
The parent process creates 3 child processes with different IDs.
The parent process sends 10 messages to each child process.
Child process 2 receives a message: {Msg Id:0, Msg Content: Job 0}
Child process 1 receives a message: {Msg Id:0, Msg Content: Job 0}
Child process 1 receives a message: {Msg Id:1, Msg Content: Job 1}
Child process 2 receives a message: {Msg Id:1, Msg Content: Job 1}
Child process 2 receives a message: {Msg Id:2, Msg Content: Job 2}
Child process 1 receives a message: {Msg Id:2, Msg Content: Job 2}
Child process 2 receives a message: {Msg Id:3, Msg Content: Job 3}
Child process 1 receives a message: {Msg Id:3, Msg Content: Job 3}
Child process 0 receives a message: {Msg Id:0, Msg Content: Job 0}
Child process 2 receives a message: {Msg Id:4, Msg Content: Job 4}
Child process 1 receives a message: {Msg Id:4, Msg Content: Job 4}
Child process 2 receives a message: {Msg Id:5, Msg Content: Job 5}
Child process 0 receives a message: {Msg Id:1, Msg Content: Job 1}
Child process 1 receives a message: {Msg Id:5, Msg Content: Job 5}
Child process 2 receives a message: {Msg Id:6, Msg Content: Job 6}
Child process 0 receives a message: {Msg Id:2, Msg Content: Job 2}
Child process 2 receives a message: {Msg Id:7, Msg Content: Job 7}
Child process 1 receives a message: {Msg Id:6, Msg Content: Job 6}
Child process 0 receives a message: {Msg Id:3, Msg Content: Job 3}
Child process 2 receives a message: {Msg Id:8, Msg Content: Job 8}
Child process 1 receives a message: {Msg Id:7, Msg Content: Job 7}
Child process 0 receives a message: {Msg Id:4, Msg Content: Job 4}
Child process 2 receives a message: {Msg Id:9, Msg Content: Job 9}
Child process 1 receives a message: {Msg Id:8, Msg Content: Job 8}
Child process 0 receives a message: {Msg Id:5, Msg Content: Job 5}
Child process 2 receives a message: {Msg Id:-1, Msg Content: Nil}
Child process 2 exits
Child process 1 receives a message: {Msg Id:9, Msg Content: Job 9}
Child process 0 receives a message: {Msg Id:6, Msg Content: Job 6}
Child process 1 receives a message: {Msg Id:-1, Msg Content: Nil}
Child process 0 receives a message: {Msg Id:7, Msg Content: Job 7}
Child process 1 exits
Child process 0 receives a message: {Msg Id:8, Msg Content: Job 8}
Child process 0 receives a message: {Msg Id:9, Msg Content: Job 9}
Child process 0 receives a message: {Msg Id:-1, Msg Content: Nil}
Child process 0 exits
Destroy message queue: message_queue_2
Destroy message queue: message_queue_1
Destroy message queue: message_queue_0
Parent process exits.