Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
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
up vote 1 down vote accepted

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.

share|improve this answer
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 posting your answer, you agree to the privacy policy and terms of service.

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