I have a suite of applications that communicate with each other using SysV queues. However, when I stop the application there are sometimes queues left open, which in turn causes problems next time I start the application.

To resolve this I want to delete the queues left open, but to know which queues to delete I would like to be able to list which queues are associated with a specific file. Is this possible to do?

The queue is created using the following (simplified) code:

    key_t key;
    int q_id;

    key = ftok("/some/file/name", 42);
    q_id = msgget(key, 0666 | IPC_CREAT);

So by associated with a file a mean the path used in the ftok call.

  • what do you mean by 'associated with a specific file' ? – Frederik Deweerdt Mar 12 '13 at 13:41
  • added further explanation – Kotte Mar 12 '13 at 13:59

I think your best bet, is to write a program that calls ftok, in order to obtain the key, something like:

#include <stdio.h>
#include <stdlib.h>
#include <sys/ipc.h>

int main(int argc, char **argv)
    unsigned long long key;
    if (argc != 3) {
        printf("Usage %s: [path] [id]\n" ,argv[0]);
        return 1;
    key = ftok(argv[1], atoi(argv[2]));
    printf("%llx\n", key);
    return 0;

Once you have this key, you can use it to remove the message queue: ipcrm -Q <key>. Alternatively, you can modify the program above to call msgctl(3) to remove the message queue.

  • Nice and simple. I think I'll use this in a script to clean up the queues. Thanks :) – Kotte Mar 12 '13 at 15:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.