mq_timedsend()返回错误14"错误的地址"

时间:2015-06-11 06:57:33

标签: c posix ipc message-queue mq

当我执行下面的程序时,我收到错误的地址作为错误代码(14)。

int main (int argc, char *argv[])
{
    char response [20];
    struct mq_attr buf;
    buf.mq_msgsize = sizeof (response);
    buf.mq_maxmsg = 5;
    response[0]= '1';
    mqd_t handle = mq_open ("/test", O_CREAT | O_RDWR, 0, &buf);
    int status = mq_timedsend (handle, (char *) &response, sizeof (response), 0,250000);
    printf("value of status - %d\n", errno);

    pthread_t my_thread_id;
    pthread_attr_t attr;

}

其他信息: 更新了如下代码,现在我收到错误代码90(消息太长)

#include <stdio.h>
    #include <errno.h>
    #include <mqueue.h>
    #include <pthread.h>
    #include <time.h>


    static struct timespec RcvCmdWaitTime;
int main (int argc, char *argv[])
{
    char response [10];
    struct mq_attr buf;
    RcvCmdWaitTime.tv_sec = 0;
    RcvCmdWaitTime.tv_nsec = 250000;
    mqd_t handle;
    buf.mq_msgsize = sizeof response;
    buf.mq_maxmsg = 1;
    int status;

    if((handle = mq_open ("/test", O_CREAT | O_RDWR , S_IRWXU | S_IRWXG, &buf))==-1)
        printf("mq-open error, error no:%d\n",errno);

    status = mq_timedsend (handle, &response[0], sizeof response , 0, &RcvCmdWaitTime);
    if (status == -1)
        printf("mq_timedsend: Error:%d\n",errno);

}

我使用:gcc -pthread multi_thr.c -lrt来编译

1 个答案:

答案 0 :(得分:1)

如果之前的通话指示错误,您应该只测试errno

在这种情况下,如果mq_timedsend()已返回-1

int result = mq_timedsend (handle, response, sizeof response, 0, 250000);
if (-1 == result)
{
  perror("mq_timedsend() failed)"; /* This issues a human readable error description based on the value of errno. */
}

另请注意

  • 施放response不是必须
  • sizeof是运营商而不是功能
  • 您的代码使用的错误消息是mi​​ssleading

您还想检查mq_open()的结果:

mqd_t handle = mq_open ("/test", O_CREAT | O_RDWR, 0, &buf);
if (((mqd_t) -1) == handle)
{
  perror("mq_open() failed");
  abort(); /* or whatever to handle this error */
}