Identifier Removed (EIDRM) error when sending message with a IPC queue

c++ipcmessage-queue

I'm using an IPC queue to do process synchronization.

I always get the EIDRM eror when sending an receiving messages from an IPC queue, but i can see that the queue is there with ipcs.

I've been for 2 hours searching but i can't see the error.

the following code is a stripped down version that gives me the same error.

#define CLAVE 53543961
#define TAM_BUFFER 1024
#define PERMISOS 0777
#define DEBUG

int Cola_Mensages;
int msgqid;


typedef struct  {
    long mtype;
    char mtext[TAM_BUFFER];
}msgbuf;


int main (int argc, char *argv[]){
    msgbuf msg_ipc;
    int num_cli,i, i_aux;

    if(argc == 2){
    num_cli = atoi(argv[1]);
    }else{ 
    num_cli = 1;
    }


    //Creating the queue
    if(msgqid = msgget(CLAVE,PERMISOS|IPC_CREAT)<0){
        fprintf(stderr,"Problema al crear la cola de mensages IPC\n");
        exit(0);
    }

    if(msgqid < 0){
        fprintf(stderr,"Problema al crear la cola de mensages IPC\n");
        exit(0);
    }


    for(i = 0;i<num_cli;i++){
       //here i get the error
       i_aux=msgrcv(msgqid,&msg_ipc,TAM_BUFFER,1,0);
       if(i_aux == -1)
           fprintf(stderr,"Error enviando msg ipc %s \n",strerror(errno));
       }

    msg_ipc.mtype = 2;
    strcpy(msg_ipc.mtext,"COMIENZO");
    printf("Enviando msg\n");

       for(i = 0;i<num_cli;i++){
           printf("Enviado msg %d\n",i);
       //here i also get the same error
           if (msgsnd(msgqid,(char *) &msg_ipc,strlen(msg_ipc.mtext),0)!=0)
           {
              fprintf(stderr,"Ocurrio el error %s en msgsnd\n",strerror(errno));
              exit(4);
           }
       }   


    if (msgctl(msgqid,IPC_RMID,(struct msqid_ds *) NULL)<0)
    {
       fprintf(stderr,"Error al borrar la cola de mensajes de clave %d\n",CLAVE);
       exit(4);
    }

    return 0;

}

Best Solution

The problem lies in the improper handling of the precedence and order of evaluation in C, whenever you evaluate the msgget return value against values lower than 0:

if(msgqid = msgget(CLAVE,PERMISOS|IPC_CREAT)<0){
    fprintf(stderr,"Problema al crear la cola de mensages IPC\n");
    exit(0);
}

The precedence of the assignment operator = falls below the comparison one <, which implies that you were assigning the result of msgget() < 0 to msgqid and not checking if (msgqid = msgget()) < 0.

You should then surround the assignment with parentheses:

if((msgqid = msgget(CLAVE, PERMISOS | IPC_CREAT)) < 0){
    fprintf(stderr,"Problema al crear la cola de mensages IPC\n");
    exit(0);
}
Related Question