I have two processes using the same code to communicate with each other depending on boost 1.58.0's boost::interprocess::message_queue.
typedef boost::shared_ptr<boost::interprocess::message_queue> mq_ptr;
mq_ptr m_t2b_queue;
mq_ptr m_b2t_queue;
sink::sink(const char * conn_id, bool is_parent)
{
const int MAX_MESSAGE_NUMBER = 100000;
snprintf( m_t2b_queue_name, MAX_MQ_NAME_LEN, "%s_t2b", conn_id);
snprintf( m_b2t_queue_name, MAX_MQ_NAME_LEN, "%s_b2t", conn_id);
if( is_parent )
{
message_queue::remove("ffqs5hbKgFs_t2b"/*m_t2b_queue_name*/);
message_queue::remove("ffqs5hbKgFs_b2t"/*m_b2t_queue_name*/);
permissions perm;
perm.set_unrestricted();
m_t2b_queue.reset(new message_queue(create_only, "ffqs5hbKgFs_t2b"/*m_t2b_queue_name*/, MAX_MESSAGE_NUMBER, 24 /*sizeof(mq_item_t)*/, perm));
m_b2t_queue.reset(new message_queue(create_only, "ffqs5hbKgFs_b2t"/*m_b2t_queue_name*/, MAX_MESSAGE_NUMBER, 24 /*sizeof(mq_item_t)*/, perm));
}
else
{
m_t2b_queue.reset(new message_queue(open_only, "ffqs5hbKgFs_t2b"/*m_t2b_queue_name*/));
m_b2t_queue.reset(new message_queue(open_only, "ffqs5hbKgFs_b2t"/*m_b2t_queue_name*/));
printf( "t2b max msg size = %d\n", m_t2b_queue->get_max_msg_size() );
printf( "b2t max msg size = %d\n", m_b2t_queue->get_max_msg_size() );
}
}
The variables in the above code have been replaced with hardcoded value so that it is more clear
The parent process create the sink instance like
sink parent( "ffqs5hbKgFs", true);
In this way I suppose the parent process create 2 message_queue internally. one for read / the other is for write.
Then parent process create the child process which create the sink instance like
sink child( "ffqs5hbKgFs", false);
I suppose the child process openes the existing 2 message_queue which were created by parent process. and use one of them to send message to parent process.
The problem is, the message_queue in the child process is opened successfully but its max_msg_size
is zero.
t2b max msg size = 0
b2t max msg size = 0
And this causes error when clild process tries to send a message via the message_queue.
Question: Does this mean the message_queue
has to be created by the process which writes to it?
In my scene, I expect the message_queue
is always created by parent process so that when child-process crashes, a new child-process can be attached to the existing message_queue
to resume the job.
Reason: Finally I found the reason, one process is running as x86. the other is running as x64