【问题标题】:Posix Message Queue Receive/Send/Open not working?Posix 消息队列接收/发送/打开不起作用?
【发布时间】:2019-01-10 07:12:32
【问题描述】:

我想用两个线程到线程队列发送和接收消息。但我也无法创建队列。当我创建一个散文时,孩子工作。我可以在主进程和子进程之间发送和读取消息,但我不能在线程之间工作。为什么?

来源:

#include <fcntl.h>
#include <mqueue.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include "tm_api.h"

#define QUEUE_NAME  "/testqueue"
#define MAX_SIZE    1024

static void * queue_server(void *pars);
static void * queue_client(void *parc);

static void * queue_server(void *pars) {
    mqd_t mq;
    unsigned int sender;
    int bytes_read;

    struct mq_attr attr;
    char buffer[MAX_SIZE];

    attr.mq_flags   = 0;
    attr.mq_maxmsg  = 10;
    attr.mq_msgsize = MAX_SIZE;
    attr.mq_curmsgs = 0;

    mq = mq_open(QUEUE_NAME, O_CREAT | O_RDONLY | O_NONBLOCK, 0644, &attr);
    printf("mq_receive : %d\n", mq);
    memset(buffer, 0x00, sizeof(buffer));
    mq_unlink (QUEUE_NAME);
    while(1) {

        bytes_read = mq_receive(mq, buffer, MAX_SIZE, &sender);
        if(bytes_read >= 0) {
            printf("SERVER: Received message: %s\n", buffer);
        } else {
            printf("SERVER: None \n");
        }

        fflush(stdout);
        tm_thread_sleep(1);
    }

    mq_close(mq);
    mq_unlink(QUEUE_NAME);

    return NULL;
}

static void * queue_client(void *parc) {
    mqd_t mq;
    char buffer[MAX_SIZE];

    mq = mq_open(QUEUE_NAME, O_RDWR);
    printf("mq_send : %d\n", mq);

    int count = 0;
    while(1) {
        snprintf(buffer, sizeof(buffer), "MESSAGE %d", count++);

        printf("CLIENT: Send message... \n");
        mq_send(mq, buffer, MAX_SIZE+1, 0);

        fflush(stdout);
        tm_thread_sleep(1);
    }

    mq_close(mq);
    return NULL;
}

int main() {

    pthread_t client, server;
    printf("Start...\n");
    pthread_create(&server, NULL, &queue_server, NULL);
    pthread_create(&client, NULL, &queue_client, NULL);
    pthread_join(server, NULL);
    pthread_join(client, NULL);
    printf("Done...\n");
    return (EXIT_SUCCESS);
}

输出:

开始... mq_receive :3 服务器:无 MQ_发送:-1 客户:发消息... 服务器:无 客户:发消息... 服务器:无

【问题讨论】:

    标签: c pthreads posix message-queue


    【解决方案1】:

    这里的问题是, 您发送的数据超出了缓冲区的大小。

        mq_send(mq, buffer, MAX_SIZE+1, 0);
    

    应该是

        mq_send(mq, buffer, MAX_SIZE, 0);
    

    如果您验证了 mq_send 的返回值,您可能已经知道了。它正在返回

    define EMSGSIZE 90 /* 消息太长 */ 错误。

    修改后得到如下输出。

    Start...
    mq_send : 4
    mq_receive : 3
    CLIENT: Send message...
    SERVER: Received message: MESSAGE 0
    CLIENT: Send message...
    SERVER: Received message: MESSAGE 1
    CLIENT: Send message...
    SERVER: Received message: MESSAGE 2
    CLIENT: Send message...
    SERVER: Received message: MESSAGE 3
    

    找到完整的代码如下。

    #include <fcntl.h>
    #include <mqueue.h>
    #include <pthread.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/stat.h>
    #include <unistd.h>
    #include <errno.h>
    
    #define QUEUE_NAME  "/testqueue"
    #define MAX_SIZE    1024
    
    static void * queue_server(void *pars);
    static void * queue_client(void *parc);
    
    static void * queue_server(void *pars) {
    mqd_t mq;
    unsigned int sender;
    int bytes_read;
    
    struct mq_attr attr;
    char buffer[MAX_SIZE];
    
    attr.mq_flags = 0;
    attr.mq_maxmsg = 10;
    attr.mq_msgsize = MAX_SIZE;
    attr.mq_curmsgs = 0;
    
    mq = mq_open(QUEUE_NAME, O_RDONLY | O_NONBLOCK | O_CREAT, 0666, &attr);
    printf("mq_receive : %d\n",mq);
            printf("SERVER: None %d %d \n", errno, bytes_read);
    memset(buffer, 0x00, sizeof(buffer));
    mq_unlink (QUEUE_NAME);
    while(1) {
    
        bytes_read = mq_receive(mq, buffer, MAX_SIZE, &sender);
        if(bytes_read >= 0) {
            printf("SERVER: Received message: %s\n", buffer);
        } else {
            printf("SERVER: None %d %d \n", errno, bytes_read);
        }
    
      //  fflush(stdout);
        sleep(1);
        }
    mq_close(mq);
    mq_unlink(QUEUE_NAME);
    
    return NULL;
    }
    static void * queue_client(void *parc) {
    mqd_t mq;
    char buffer[MAX_SIZE];
    
    
    struct mq_attr attr;
    
    attr.mq_flags = 0;
    attr.mq_maxmsg = 10;
    attr.mq_msgsize = MAX_SIZE;
    attr.mq_curmsgs = 0;
    
    
    
    mq = mq_open(QUEUE_NAME, O_CREAT|O_WRONLY|O_NONBLOCK , 0666,&attr);
    printf("mq_send : %d\n",mq);
    
    int count = 0;
    while(1) {
        snprintf(buffer, sizeof(buffer), "MESSAGE %d", count++);
    
        printf("CLIENT: Send message... \n");
        int bytes_read = mq_send(mq, buffer, MAX_SIZE, 0);
            printf("CLIENT: send %d %d \n", errno, bytes_read);
    
    //    fflush(stdout);
        sleep(1);
    }
    mq_close(mq);
    return NULL;
    }
    
    int main() {
    
    pthread_t client, server;
    printf("Start...\n");
    pthread_create(&server, NULL, &queue_server, NULL);
    pthread_create(&client, NULL, &queue_client, NULL);
    pthread_join(server, NULL);
    pthread_join(client, NULL);
    printf("Done...\n");
    return (EXIT_SUCCESS);
    }
    

    【讨论】:

    • 没有。问题仍然存在。
    • 您遇到什么错误?尝试在 mq_send 之后打印 errno。如果您得到 errno=9 EBADF,请尝试关闭程序并再次运行。
    • @aytac 你还发现问题吗?
    • 对于 send ,我在 mq_open 函数之后添加了 strerror(errno) 。输出是“没有这样的文件或目录”... errno : 2 ENOENT 没有这样的文件或目录。
    • 我认为我们同时解决了它。 :) 我意识到我无法添加。它现在正在工作。如果需要,请编辑您的消息。我会投票。
    猜你喜欢
    • 1970-01-01
    • 2011-07-10
    • 2013-10-10
    • 2015-10-11
    • 2013-04-16
    • 1970-01-01
    • 2020-11-27
    • 1970-01-01
    • 2012-12-05
    相关资源
    最近更新 更多