Message queue error (invalid arguement) with Qt



  • 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

    @


  • Lifetime Qt Champion

    Hi,

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



  • 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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.