消息队列在C中,mq在创建消息队列时返回-1

时间:2016-10-15 12:40:06

标签: c posix message-queue

当我创建一个消息队列时,mq总是返回-1并且它给出了一条错误消息。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <mqueue.h>
#include <errno.h>
#include <pthread.h>
#include <ctype.h>
#include <time.h>
typedef int bool;
#define true 1
#define false 0
#define MIN_WORKERS 1
#define MAX_WORKERS 5
#define   MAX_COUNT  200
#define   BUF_SIZE   100

int  main(int argc, char * argv[])
{
    //Declaring Nodes
    NODE * first;
    NODE * current;
    first=current=NULL;

    //Message Queue
    mqd_t mq;

    //Declaring Variables
    int  pid;
    int    i;
    char *buffer;
    int bufferLength;
    int index = 3;

    //Declare message queue attributes
    struct mq_attr mq_attr;
    mq_attr.mq_flags = 0;
    mq_attr.mq_maxmsg = 10;
    mq_attr.mq_msgsize = 10;
    mq_attr.mq_curmsgs = 0;


    // initialize the queue attributes 
    mq_getattr(mq, &mq_attr);
    bufferLength = mq_attr.mq_msgsize;

    char serverName[128];
    strcpy(serverName, argv[1]);

    pid=fork();
    printf("%d\n",pid );
    //Parents message queue 
    if(pid!=0)
    {
        printf("%d\n",pid );

        mq = mq_open(serverName, O_RDWR | O_CREAT, 0666, NULL);
        if(mq==-1)
        {
            perror("Error: at creating PARENT server message queue\n");
            exit(1);
        }
    }

0 个答案:

没有答案