creating a message queue using mq_open

Hi all,
First of all thanks in advance for reading my post and for your heart for helping me.

I am trying to create a message queue using mq_open(name,oflags,mode_t,attr) method. But that function call is returning with an error code EFAULT. By googling it I found that it happens when there is an invalid memory address reference and in another forum it is said that it is because of "internal virtual memory error". Can anybody please help me to get out of this error. Any efforts will be very much appreciated. I am really stuck with that..
Once again thanking you all and waiting for a reply,
regards,
paru.

Either name or attr points to an address outside the current process memory space. Please check that name correctly points to the correct string, resp. attr to a correct mq_attr structure.

Eventually, post how you're calling mq_open()

HTH,
Lo�c

Hi Loic,
Thanks for your help. i am calling the method as below

mq_open( "/iothreadNewTest", oflags,(S_IRUSR | S_IWUSR),NULL)

So in this case can it be because of that?

Yeah, now I understand your problem. How do you check that the error returned by mq_open() ? On which OS do you see this problem?

What value are you using for oflags? If this is the first time you are connecting to this queue, you need to pass O_CREAT to tell mq_open to create the queue.

Here is a link to very good documentation on all the mq_ functions (and other POSIX functions), that I found very helpful while implementing a message-queue based solution:
Alphabetical Index - M

Thank you for time,
I am giving below the code snippet which i use.

oflags = 0;
  oflags |= (Flags & MQ_READ)?(O_RDONLY):(0);
  oflags |= (Flags & MQ_WRITE)?(O_WRONLY):(0);
  oflags |= (Flags & MQ_READ_WRITE)?(O_RDWR):(0);
  oflags |= (Flags & MQ_NON_BLOCKING)?(O_NONBLOCK):(0);
  oflags |= (O_CREAT | O_EXCL); // try to create it first
  mqAttr.mq_maxmsg = maxMsg;
  mqAttr.mq_msgsize = msgSz + sizeof(MSG_Q_HEADER);
  
  if ((fileDesc = mq_open( "/iothreadNewTest435", oflags,(S_IRUSR | S_IWUSR),NULL)) == -1 )
  {
    rc = mqEvalErrnoOpen(pName))

}

mqEvalErrnoOpen( const char * pName )
{
 MQ_RC rc;
 int err = 0;

  switch ( errno )
  {
  case EACCES:
    rc = MQ_INVALID_ACCESS;
    break;

  case EEXIST:
    rc = MQ_ALREADY_EXISTS;
    break;

  case EFAULT:
    rc = MQ_INVALID_ADDRESS;
    break;

  case EINVAL:
    rc = MQ_INVALID_ACCESS;
    break;

  case EMFILE:
    rc = MQ_TOO_MANY_MSG_Q_DESC;
    break;

  case ENAMETOOLONG:
    rc = MQ_NAME_TOO_LONG;
    break;

  case ENFILE:
    rc = MQ_TOO_MANY_MSG_Q_DESC;
    break;

  case ENOENT:
    rc = MQ_QUEUE_NOT_PRESENT;
    break;

  case ENOMEM:
    rc = MQ_INSUFFICIENT_MEMORY;
    break;

  case ENOSPC:
    rc = MQ_INSUFFICIENT_MEMORY;
    break;

  case ENOTSUP:
    rc = MQ_NOT_SUPPORTED_CHECKPOINT_RESTART_PROC;
    break;

  default:
    rc = MQ_ERRNO;
    err = errno;
    break;
  }

Here rc is returning a value MQ_INVALID Address.
My os details are as follows Fedora Sulphur

Linux myMachine 2.6.25-14.fc9.i686 #1 SMP Thu May 1 06:28:41 EDT 2008 i686 i686 i386 GNU/Linux

Please help me with this,
Thanks again,
Paru.

I see that you are filling the mqAttr structure, but not passing it to mq_open... it should not cause the error, but just noticed it.

Also, you are passing both O_EXCL and O_CREAT to mq_open... as per the opengroup documentation:

I do not see an EFAULT listed as a possible errno in the documentation, so its probably some sort of extension that your OS has implemented.

  • What OS are you using?
  • Try looking at the documentation for mq_open for your OS. It should detail what conditions cause the EFAULT error.
  • Also, did you try running the code with a different queue name? do you get the same error every time?

Hi,

Quoting from the post "What OS are you using?

  • Try looking at the documentation for mq_open for your OS. It should detail what conditions cause the EFAULT error.
  • Also, did you try running the code with a different queue name? do you get the same error every time? "

My os details are as follows Fedora Sulphur

Linux myMachine 2.6.25-14.fc9.i686 #1 SMP Thu May 1 06:28:41 EDT 2008 i686 i686 i386 GNU/Linux.

Regarding that mq_attr, i changed that to null thinking that it may be the reason for the error. But giving NULL also is throwing the same name.

I even tried changing the name of the queue.Then also it is giving the same error

Could you please try to create a minimalistic program that compiles and shows your particular problem? Otherwise, I'll look tomorrow evening in more details your answer.

Cheers,
Lo�c

Hi Loic,
I tried to write a simple program by just calling the mq_open method.. then also i am getting the same error. Thank you for your valuable time..Kindly help me to get out of this.. One thing I noticed is if i use a msgget() method to create a new message queue it is working fine.. can i use it in program.. can u pls tell me the difference between both.
regards,
paru.

I think, it is best when you post this simple program.

TIA,
Lo�c