Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Message queue error (invalid arguement) with Qt
Qt 6.11 is out! See what's new in the release blog

Message queue error (invalid arguement) with Qt

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 3.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dcqt
    wrote on last edited by
    #1

    Hi,

    The question is not actually related to Qt problem, it is more general of *nix IPC.

    I am trying to use message queues with Qt , below is the code.

    the function
    @ msgsnd(msgid,&msg_struct[0],sizeof(msg_struct[0]),0); @ is failing with msg

    msgsnd (create_sender): Invalid argument

    instead of &msg_struct[ 0 ] if i use msg_struct[ 0 ].msg_buff it is working .
    i do not understand the problem. as the arguement is void * for msgsnd it should work in both the cases.

    sender.pro

    sender.cpp
    @

    #include "sender.h"
    #include<QDebug>
    #include <sys/ipc.h>
    #include <sys/types.h>
    #include<sys/msg.h>

    Sender::Sender(QWidget *parent)
    : QMainWindow(parent)
    {
    create_sender("msg1",8);
    }

    Sender::~Sender()
    {

    }
    int Sender :: create_sender(char *appname,int pos)
    {
    int msgid;
    int res;

    struct st_msg_buff{
         char msg_buff[50];
         int msg_type;
         int pos;
     };
    
    struct st_msg_buff msg_struct[1];
    
    msgid = msgget(0x7890,IPC_CREAT|0777);
    
    qDebug()<<"msgid (create_sender)" << msgid;
    
    if(-1 == msgid ){
        perror("msgget (create_sender)");
        return 0;
    }
    
    strcpy(msg_struct[0].msg_buff,appname);
    msg_struct[0].msg_buff[strlen(appname)]=0;
    msg_struct[0].msg_type = 0;
    msg_struct[0].pos = pos;
    
    qDebug() <<"pos in Sender" <<msg_struct[0].pos;
    res = msgsnd(msgid,&msg_struct[0],sizeof(msg_struct[0]),0);
    
    if(-1 == res ){
        perror("msgsnd (create_sender)");
        return 0;
    }else
    {
        qDebug() <<"msg sent (create_sender) successfully";
    }
     return 0;
    

    }

    @

    main.cpp
    @
    #include "sender.h"
    #include <QApplication>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    Sender w;
    w.show();

    return a.exec&#40;&#41;;
    

    }
    @

    sender.h
    @
    #ifndef SENDER_H
    #define SENDER_H

    #include <QMainWindow>

    class Sender : public QMainWindow
    {
    Q_OBJECT

    public:
    Sender(QWidget *parent = 0);
    ~Sender();
    int create_sender(char *appname,int pos);
    };

    #endif // SENDER_H
    @

    receiver.pro

    receiver.cpp
    @
    #include "receiver.h"
    #include <sys/ipc.h>
    #include <sys/types.h>
    #include<sys/msg.h>
    #include "receiver.h"
    #include<QDebug>

    Receiver::Receiver(QWidget *parent)
    : QMainWindow(parent)
    {
    create_receiver();
    }

    void * wait_for_mesg(void *);

    int Receiver :: create_receiver()
    {
    pthread_t tid;
    if (-1 == pthread_create(&tid,NULL,wait_for_mesg,NULL) ){
    perror("pthread_create");
    return -1;
    } else {
    qDebug()<<"wait_for_mesg created suceesfully";

    }
    

    }

    void * wait_for_mesg(void *arg)
    {
    int msgid;
    int res;

    struct st_msg_buff{
         char msg_buff[50];
         int msg_type;
         int pos;
     };
    
    struct st_msg_buff msg_struct[1];
    
    msgid = msgget(0x7890,IPC_CREAT|0777);
    qDebug()<<" getting msgId in wait_for_mesg" << msgid;
    
    if(-1 == msgid ){
        perror("Failed");
        return NULL;
    } else {
        qDebug()<<"Success";
    }
    
    msg_struct[0].msg_type = 0;
    
    res = msgrcv(msgid,msg_struct,sizeof(msg_struct[0]),0,0);
    //sleep(1);
    
    if( -1 == res ){
        perror("msgrcv: ");
        return NULL;
    } else   {
    
        qDebug() <<"app recieved from Sender:" <<msg_struct[0].msg_buff;
        qDebug() << "pos :" << msg_struct[0].pos;
    
        if(msgctl(msgid,IPC_RMID,NULL) < 0)
            perror("Unable to remove msgid");
        else
          qDebug()<<"Message Queue Deleted ";
    }
    

    }
    Receiver::~Receiver()
    {

    }

    @

    main.cpp
    @
    #include "receiver.h"
    #include <QApplication>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    Receiver w;
    w.show();

    return a.exec&#40;&#41;;
    

    }

    @

    receiver.h
    @
    #ifndef RECEIVER_H
    #define RECEIVER_H

    #include <QMainWindow>

    class Receiver : public QMainWindow
    {
    Q_OBJECT

    public:
    Receiver(QWidget *parent = 0);
    ~Receiver();
    int create_receiver();
    };

    #endif // RECEIVER_H

    @

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      with msgsnd(create_sender) you are trying to call msgsnd with a function as a parameter. Is that really what you want ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • D Offline
        D Offline
        Dcqt
        wrote on last edited by
        #3

        Hi,

        i think you are referring to comment , that not actual code.
        i have gone through other sites and i feel it is something to do with MSGSIZE in the message queue.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved