Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mq_open giving "too many open files"

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?

like image 992
gangadhars Avatar asked Nov 23 '25 05:11

gangadhars


2 Answers

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); 
like image 64
Fabio Avatar answered Nov 25 '25 19:11

Fabio


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.

like image 26
alimg Avatar answered Nov 25 '25 20:11

alimg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!