mq_receive: message too long

前端 未结 4 963
再見小時候
再見小時候 2020-12-31 11:20

I am implementing a communication between 2 processes using a queue. The problem is that when I call the function mq_receive, I get this error: Message too long.

I h

4条回答
  •  执念已碎
    2020-12-31 11:46

    Don't forget to unlink the message queue before running your program again. If you dont unlink it, it will still use the old message queue settings. This happens when you end your program with Ctrl+C. I think it is a good idea to put the following code at the beginning of the program:

    if(mq_unlink(QUEUENAME) == 0)
      fprintf(stdout, "Message queue %s removed from system.\n", QUEUENAME);
    

    An alternative form (C++ style) that checks for real errors (like permissions) and ignores the cases where the queue already exists or not:

    int rc = mq_unlink(name.c_str());
    if (rc != 0 && errno != ENOENT)
          THROW_ERRNO_EXCEPTION();  
    
    // ENOENT is the status code if the queue doesn't exist, which is not an error
    // if you are immediately going to create it.
    

提交回复
热议问题