Writing into the shared memory by one process and reading from the shared memory by another process. Using Shared Memory or Message Queues depends on the need of the application and how effectively it can be utilized.Ĭommunication using message queues can happen in the following ways − The first message inserted in the queue is the first one to be retrieved. Ofcourse, the order of message queue is FIFO (First In First Out). To simplify the given message type as 1, 10, 20, it can be either 0 or +ve or –ve as discussed below. In this case, it is simplier to implement with message queues. If we want to communicate with different data packets, say process A is sending message type 1 to process B, message type 10 to process C, and message type 20 to process D. What if all the processes do not need to access the shared memory but very few processes only need it, it would be better to implement with message queues. Not worth with regard to utilization in this kind of cases. Shared memory data need to be protected with synchronization when multiple processes communicating at the same time.įrequency of writing and reading using the shared memory is high, then it would be very complex to implement the functionality. If we want to communicate with small message formats. Whereas in shared memory, the data is available for multiple processes to access. Passing on a transaction id and action Start/StopĪll data is in the pCompressedData !!! it is this memory that should be send.Why do we need message queues when we already have the shared memory? It would be for multiple reasons, let us try to break this into multiple points for simplification −Īs understood, once the message is received by a process it would be no longer available for any other process. * Example: Creating an event message to be send to a server asking it to use method MusicPlayer If you use the basic concept of ASN1 (Abstract Syntax Notation One), then you can encode data into a binary field, then transfer it and decode it using your decoder One way of making transfer of complex datastructures is by doing it the old fashion way - make your own data encoder / decoder. Mq.receive(&serialized_string, MAX_SIZE, recvd_size, priority) Void serialize(Archive & ar, const unsigned int version) Use Boost.Serialization to serialize your object and de-serialize on receiving end.įriend class boost::serialization::access You should use Boost.Serialization or use advanced Boost.Interprocess mechanisms to send complex data between processes. For example, we can send integers between processes but not a std::string. This means that if we want to send an object using a message queue the object must be binary serializable. Can anyone suggest?Ī message queue just copies raw bytes between processes and does not send objects. Read somewhere that I need to do serialization for the structure, but can't figure out how to do it. The cout for (*recvMsg).id is okay, but segmentation fault occurred at cout for (*recvMsg).name. MyReceiver.receive(recvMsg, sizeof(person_struct), msg_size, priority) Ĭout << "Name: " << (*recvMsg).name << endl Ĭout << "Age: " << (*recvMsg).age << endl Person_struct *recvMsg = new person_struct() nd(&MyRec, sizeof(person_struct), MQ_PRIORITY) īelow is my receiver function: message_queue myReceiver(open_only, "MSG_Q") Message_queue mqSender(create_only, "MSG_Q", 100, sizeof(person_struct)) However, after receiving, I have segmentation fault when accessing the 'name' variable in the above structure.īelow is my sender function: person_struct MyRec Using boost message queue, I tried to send this data structure to the message queue receiver in another process. I have the following data structure: typedef struct
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |