How to remove all the IPC-Message queue ?

22,241

Solution 1

Try this one,

ipcrm -q 262144 -q ... -q ...

You can give like this. Option is must.

Solution 2

ipcrm can do just that:

ipcrm --all=msg

Instead of msg you can use sem and shm for semaphores and shared memory.

Solution 3

There is an option -Q in ipcrmthat deletes all the messages in the queue. For instance, ipcrm -Q 0x00000000 deletes all the messages for the key 0x00000000

Solution 4

Hey I got you the answer

you can delete message queue's using KEY number running in loop from starting to max range(use system() function), but message Queue with KEY=0, cannot be removed in that way ->" ipcrm -Q 0 " not possible, so better way is to get mesQid for each key, then use msgctl(msgQid,IPC_RMID,0); in this way you can remove with key=0 also;

so problem sums up to get msgqid by avoiding msgget() (read MSG_INFO and MSG_STAT from $man 2 msgctl)

so now, loop over the kernel's index system for active message queues, then get the msgqid using sequential index and looping i.e msqid = msgctl(ind, MSG_STAT, &ds), so thats it.

for cleaner understanding read through MSG_INFO and MSG_STAT from $man 2 msgctl.

or a cleaner code i tried here and working https://github.com/chetanDN/linux-system-programming/blob/master/IPC/messageQ/2.deleteMsgQ/deleteAllCurrentMsgQs.c

Share:
22,241
Bhuvanesh
Author by

Bhuvanesh

Learning is like a Sky...It never end.

Updated on January 12, 2020

Comments

  • Bhuvanesh
    Bhuvanesh over 4 years

    Consider I have created a 100 messages queues using a msgget() function.

     ------ Message Queues --------
     key        msqid      owner      perms      used-bytes   messages    
    0x00000000 262144     bhuvaneshw 666        40           2           
    0x00000000 294913     bhuvaneshw 666        40           2           
    0x00000000 327682     bhuvaneshw 666        40           2           
    0x00000000 360451     bhuvaneshw 666        40           2           
    0x00000000 393220     bhuvaneshw 666        40           2           
    0x00000000 425989     bhuvaneshw 666        55           3        
    ....
    .....
    ....
    

    Using a ipcrm command we can remove the single queue at a time.

    ipcrm -q queue_id
    

    or else using a msgctl() we can remove that. But I want to remove all the message queues in single instant . Is there is any way to do this in linux?

  • Bhuvanesh
    Bhuvanesh over 9 years
    @Dimitri- ok But I need to remove all the queues in my system.