Oracle Linux: неверный файловый дескриптор в mq_send и mq_receive - PullRequest
0 голосов
/ 26 сентября 2019

У меня есть пример кода, использующий mq_open, mq_send и mq_receive, mq_open работает нормально, но mq_send и mq_received выдают мне ошибку 9, т. Е. Плохой дескриптор файла, вот мой код, извините за плохой код с отступом

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>

#include <fcntl.h>
#include <sys/stat.h>
#include <mqueue.h>

#include <iostream>
#include <fstream>

#define SERVER_QUEUE_NAME   "/iris_TESTQUEUE"
#define QUEUE_PERMISSIONS 0660
#define MAX_MESSAGES 10
#define MAX_MSG_SIZE 256
#define MSG_BUFFER_SIZE MAX_MSG_SIZE + 10

void getQueueAttr(mq_attr &atrributes);
int main (int argc, char **argv)
{
    mqd_t qd_server, qd_client;   // queue descriptors

    if (argc != 2)
    {
          cout << "testq.exe READ/WRITE" << endl;
          exit(0);
    }


     mq_attr attrs;
     getQueueAttr(attrs);

    if ((qd_server = mq_open (SERVER_QUEUE_NAME,  O_RDWR | O_CREAT |O_NONBLOCK ,  S_IRUSR | S_IWUSR, &attrs)) == -1) {

        perror ("Server: mq_open (server)");
        exit (1);
    }
    else
         {
                 cout << "File Descriptor while opening: "      << qd_server << endl;
         }

    char in_buffer [attrs.mq_msgsize];
    char out_buffer [attrs.mq_msgsize];


         string message = "HELLO, THIS IS A TEST MESSAGE";



         memset(out_buffer, 0, attrs.mq_msgsize);
         memset(in_buffer, 0, attrs.mq_msgsize);


                  if(strcmp(argv[1], "WRITE") == 0)
                  {
                        memcpy(out_buffer, message.c_str(), message.length());
                if (mq_send (qd_server, out_buffer,  message.length() , 0) == -1) {
                perror ("Server: Not able to send message to client");
                                   cout << "File Descriptor while sending failed: "     << qd_server << endl;
                }
                        else{
                printf ("Server: response sent to client.\n");
                   cout << "File Descriptor after sending "     << qd_server << endl;
                        }
                  }
                  else if(strcmp(argv[1], "READ") == 0)
                  {
                if (mq_receive (qd_server, in_buffer, attrs.mq_msgsize, NULL) == -1) {
                        perror ("Server: mq_receive");
                                        cout << "File Descriptor while receiving failed "       << qd_server << endl;
                        }
                                else
                                {
                                        cout << in_buffer << endl;
                                cout << "File Descriptor after receiving  "     << qd_server << endl;
                                }
                  }

                return 0;

}


void getQueueAttr(mq_attr &atrributes)
{

mq_attr attrs;
    //if(attrSet == false)
    {
         string fileMsgMax = "/proc/sys/fs/mqueue/msg_max";
         string fileMsgSizeMax = "/proc/sys/fs/mqueue/msgsize_max";
         string valueMsgMax;
         string valueMsgSizeMax;

         ifstream queueMsgMaxFile;
         ifstream queueMsgSizeMaxFile;

         queueMsgMaxFile.open(fileMsgMax);
         if(!queueMsgMaxFile.is_open())
        {
             std::cout << "File not found at path: " +  fileMsgMax;
             return ;
        }
         getline (queueMsgMaxFile,valueMsgMax);

         queueMsgSizeMaxFile.open(fileMsgSizeMax);
        if (!queueMsgSizeMaxFile.is_open())
        {
            std::cout <<"File not found at path: " +  fileMsgSizeMax;
            return ;
        }
         getline (queueMsgSizeMaxFile,valueMsgSizeMax);

         attrs.mq_maxmsg = stoi(valueMsgMax);
         attrs.mq_msgsize = stoi(valueMsgSizeMax);
         attrs.mq_msgsize = attrs.mq_msgsize - 1;
    }

    atrributes = attrs;
    return ;
}

На моей машине все работает нормально, я пробовал это на RHEL 6.4 и Oracle Linux 7.4, но то же самое не работает на клиентской машине, которая имеет те же параметры sysctl и limit.conf, что и мои

**On my machine** 
./testq.exe WRITE
File Descriptor while opening: 3
Server: response sent to client.
File Descriptor after sending 3
[iris@irisdb ~]$ ./testq.exe READ
File Descriptor while opening: 3
HELLO, THIS IS A TEST MESSAGE
File Descriptor after receiving  3
**On Client Machine**
./testq.exe WRITE
File Descriptor while opening: 3
Server: Not able to send message to client: Bad file descriptor
File Descriptor after sending 3
[iris@irisdb ~]$ ./testq.exe READ
File Descriptor while opening: 3
Server: mq_receive: Bad file descriptor
File Descriptor after receiving  3
...