3

I created a message queue with following code. First few times it works properly.

int main()
{
    mqd_t mqdes;
    char mq_name[10] = "/mq";
    int oflag = O_CREAT | O_RDWR, ret;
    struct mq_attr attr;

    attr.mq_maxmsg = 1024;
    attr.mq_msgsize = 2048; 

    mqdes = mq_open(mq_name, oflag, 0766, &attr);
    if(mqdes == -1) {
            perror("mq_open");
            if(errno == EMFILE)
                    perror("EMFILE");
            exit(1);
    }

    printf("mqueue created, mq_descriptor: %d\n", mqdes);

    ret = mq_close(mqdes);
    if(ret == -1) {
            perror("mq_close");
            exit(2);
    }
    printf(" mq closed successful\n");


    return 0;
}

After that, it's giving following error

mq_open: Too many open files
EMFILE: Too many open files

But why i'm getting this error? How can I see possix message queues like ipcs is for system V?

gangadhars
  • 2,584
  • 7
  • 41
  • 68
  • Are you getting the `mq closed successful\n` message at the end of the session? [HERE](http://stackoverflow.com/a/3056992/645128) is another example of using mq_open, maybe you can see something there that you are not doing. – ryyker Oct 08 '13 at 15:00
  • @ryyker: how can it goes to end of the program? mq_open fails and giving error. – gangadhars Oct 08 '13 at 15:02
  • 1
    Regarding your question _How can I see possix message queues like ipcs is for system V?_ [THIS](http://www.linuxforums.org/forum/red-hat-fedora-linux/93230-what-posix-equivalent-ipcs-m.html) link talks a little about it. I do not see anything else in what you posted that would suggest that you should have too many files open. – ryyker Oct 08 '13 at 17:40

3 Answers3

3

Try to set the resource limits:

#include <sys/resource.h>

struct rlimit rlim;
memset(&rlim, 0, sizeof(rlim));
rlim.rlim_cur = RLIM_INFINITY;
rlim.rlim_max = RLIM_INFINITY;

setrlimit(RLIMIT_MSGQUEUE, &rlim); 
Fabio
  • 31
  • 2
2

I had the same issue while trying something. If you have by accident too many open message queues left on your system, you can try deleting your mqueue's in directory /dev/mqueue. This worked for me.

Also you might want to use mq_unlink(const char *name) after the mq_close() to ensure that the queue is removed from the system as described here.

alimg
  • 141
  • 9
1

I had the same problem and I solved it by increasing RLIMIT_MSGQUEUE via setrlimit.

If the hard limit (rlim_max) is too low as well (which was the case for me), you will have to give your process the CAP_SYS_RESOURCE privilege so that you can set the hard limit before you set the process limit (rlim_cur). Either run $ setcap 'CAP_SYS_RESOURCE=+ep' /path/to/executable over an executable or edit /etc/security/capability.conf to give CAP_SYS_RESOURCE to a user/group.

Dot.Bot
  • 11
  • 2