views:

550

answers:

4

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;

}

A: 

Just some ideas,

You should check msgget() against -1 to check if it failed.

EIDRM stands for "Identifier removed", so it's consistent with what you see in ipcs.

Maybe firstly you want to ensure the queue is created (see ipcs) and only after that continue to sending/receiving messages.

dimba
I already do checking of msgget and seems correct.I'm not sure to understand what you mean that "identifier remove" is consistent with 'ipcs' when ipcs tells me that the queue is created.
Nodens
A: 

Does the message queue go out of existence when you kill this process? That is, are you working with a fresh queue every time you test? I'm curious if the permissions are actually what you think they are.

Also, how about posting a compilable piece of code that is giving you the error. I'd like to see what files you're including.

Rob Jones
Yes, i'm working with a new queue every time i test, the queue doesn't go away when i kill the process, i kill it manually with ipcrm .Later i will post the whole code.
Nodens
Ok. This is weird. The program works the first time i execute it. First time i execute the program, a message queue with msqid 0 is created. Right now, the program doesn't end properly, so the queue doesn't get properly destroyed by the program, i have to destroy it with ipcrm -q . After that, everytime i re-run the program i get the EIDRM errors.Ipcs says that theres a queue created, but now with a different msqid.Seems that the problemscomes from not closing the queue correctly, but i wonder why that makes subsequently runs, even with a diffrent ipc key, fail.
Nodens
A: 

Ok. One update about the problem.

The first time i run the program, it works perfectly. If i use ipcs i can see that the program creates a queue with msqid 0. The programs deletes the queue with no problems.

After the first run, it stops working and begins to give the EIDRM error, ipcs says that theres a queue created, but it doesn't have msqid 0 like the first run, its a random number. This happens even if i change the ipckey, it just creates another queue whith a different msquid, and fails the same way.

Nodens
A: 

Problem solved.

I was always getting 0 as id of the queue because i was calling msgget inside the if condition. I took out the msgget and doing the if with the variable works fine.

Nodens