39

How can I delete all not used semaphores and shared memory with a single command on a UNIX-like system, e.g., Ubuntu?

DrMickeyLauer
  • 4,455
  • 3
  • 31
  • 67
simone
  • 801
  • 2
  • 13
  • 25
  • 4
    This question really needs an explanation of "Why". – Anon. Jan 26 '10 at 23:12
  • i'm doing a school project in c – simone Jan 26 '10 at 23:16
  • 3
    So, as usual with homework, what do you know, what have you tried? – Cascabel Jan 26 '10 at 23:20
  • i'm working with semaphores and shared memory in a network file system – simone Jan 26 '10 at 23:26
  • 11
    Yeh good god guys, show a little understanding. The same question siome has occurs to me right now. Registering some sys v shared memory and needing a cleanup after a while, who wants to invoke "ipcrm" multiple times, parsing "ipcs" beforehand. Programming is lazyness. And stackoverflow is for especialy lazy programmers, isn't it? – Christoph Strasen Nov 09 '10 at 10:30
  • All solutions yet do not take POSIX semaphores (named as well as unnamed) into account but focus on XSI aka SystemV semaphores only. That's not a big surprise since the question is rather poor quality. However, the stackexchange community does better normally - I am surprised. – stefanct Nov 11 '16 at 12:14
  • **You can break your running system if you do such a thing**. There is no possibility of doing that and only `ipcrm(1)` command allows you to delete other program's created ipc resources, but resource by resource. – Luis Colorado Jul 05 '17 at 08:55

11 Answers11

67

Here, save and try this script (kill_ipcs.sh) on your shell:

#!/bin/bash

ME=`whoami`

IPCS_S=`ipcs -s | egrep "0x[0-9a-f]+ [0-9]+" | grep $ME | cut -f2 -d" "`
IPCS_M=`ipcs -m | egrep "0x[0-9a-f]+ [0-9]+" | grep $ME | cut -f2 -d" "`
IPCS_Q=`ipcs -q | egrep "0x[0-9a-f]+ [0-9]+" | grep $ME | cut -f2 -d" "`


for id in $IPCS_M; do
  ipcrm -m $id;
done

for id in $IPCS_S; do
  ipcrm -s $id;
done

for id in $IPCS_Q; do
  ipcrm -q $id;
done

We use it whenever we run IPCS programs in the university student server. Some people don't always cleanup so...it's needed :P

neverMind
  • 1,757
  • 4
  • 29
  • 41
  • 1
    Doesn't that delete all of them, even if `nattach` is nonzero? What about this?: `ipcs -m | egrep "0x[0-9a-f]+ [0-9]+"|awk '{ if ($6 == 0) { print $2; } }'|xargs -n1 ipcrm -m` – Ben Nov 27 '13 at 20:40
  • 1
    I would replace the for loops with `xargs`: `echo $IPCS_S | xargs -n1 ipcrm -s`. – user1202136 Jul 16 '14 at 06:42
  • For me the above regex didn't work. The following did `IPCS_Q=`ipcs -q | egrep -i "0x[0-9a-f]+.*[0-9]+" | grep $ME | cut -f2 -d" "`` – brainydexter Jul 17 '14 at 20:39
  • On AIx systems does not work either.. some minor tweaks on the commands did the trick! `ipcs -s |grep $ME | awk '{print $2}'` The awk command solves the positioning problems. – LeoPucciBr Sep 21 '16 at 21:51
11

This works on my Mac OS:

for n in `ipcs -b -m | egrep ^m | awk '{ print $2; }'`; do ipcrm -m $n; done
bluish
  • 26,356
  • 27
  • 122
  • 180
Serge
  • 1,947
  • 3
  • 26
  • 48
10
ipcs -s | grep $USERNAME | perl -e 'while (<STDIN>) { @a=split(/\s+/); print `ipcrm sem $a[1]`}'

or

ipcs -s | grep $USERNAME | awk ' { print $2 } ' | xargs ipcrm sem

Change $USERNAME to a real username.

bvamos
  • 773
  • 7
  • 10
10
#!/bin/bash
ipcs -m | grep `whoami` | awk '{ print $2 }' | xargs -n1 ipcrm -m
ipcs -s | grep `whoami` | awk '{ print $2 }' | xargs -n1 ipcrm -s
ipcs -q | grep `whoami` | awk '{ print $2 }' | xargs -n1 ipcrm -q
mgttt
  • 351
  • 3
  • 4
  • It's a great answer, it helped me to remove queues with key 0x0 (which cannot be removed using ipcrm -Q option) – Oleg Oleg Apr 25 '19 at 08:30
7

I don't know how to delete all at once, but you can use ipcs to list resources, and then use loop and delete with ipcrm. This should work, but it needs a little work. I remember that I made it work once in class.

MBO
  • 30,379
  • 5
  • 50
  • 52
  • 3
    Do we know if he is using SysV ipc and not POSIX? – Duck Jan 27 '10 at 02:14
  • @Duck We know he's using Ubuntu, and I checked those commands on Ubuntu – MBO Jan 27 '10 at 07:36
  • 5
    Right but ipcs only works with SysV ipc objects. If he is using the posix versions they will show in /dev/shm where he can just rm them. – Duck Jan 27 '10 at 17:22
2

Check if there is anything to delete with :

ipcs -a | grep `whoami`

On linux delete them all via :

ipcs | nawk -v u=`whoami` '/Shared/,/^$/{ if($6==0&&$3==u) print "ipcrm shm",$2,";"}/Semaphore/,/^$/{ if($3==u) print "ipcrm sem",$2,";"}' | /bin/sh

For sun it would be :

ipcs -a | nawk -v u=`whoami` '$5==u &&(($1=="m" && $9==0)||($1=="s")){print "ipcrm -"$1,$2,";"}' | /bin/sh

courtsesy of di.uoa.gr

Check again if all is ok

For deleting your sems/shared mem - supposing you are a user in a workstation with no admin rights

Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
2

1 line will do all

For message queue

ipcs -q | sed "$ d; 1,2d" |  awk '{ print "Removing " $2; system("ipcrm -q " $2) }'

ipcs -q will give the records of message queues

sed "$ d; 1,2d " will remove last blank line ("$ d") and first two header lines ("1,2d")

awk will do the rest i.e. printing and removing using command "ipcrm -q" w.r.t. the value of column 2 (coz $2)

Ecko
  • 1,030
  • 9
  • 30
pPanda_beta
  • 618
  • 7
  • 10
1

This is how I do it in FreeBSD:

#!/usr/local/bin/bash
for i in $(ipcs -a | grep "^s" | awk '{ print $2 }');
do
        echo "ipcrm -s $i"
        ipcrm -s $i
done
gkiko
  • 2,283
  • 3
  • 30
  • 50
John Moss
  • 11
  • 1
0

Since you mentioned that you're working on a NFS system, do you have access to those semaphores and shared memory? I think you misunderstood what they are, they are an API code that enables processes to communicate with each other, semaphores are a solution for preventing race conditions and for threads to communicate with each other, in simple answer, they do not leave any residue on any filesystem.

Unless you are using an socket or a pipe? Do you have the necessary permissions to remove them, why are they on an NFS system?

Hope this helps, Best regards, Tom.

t0mm13b
  • 34,087
  • 8
  • 78
  • 110
  • no this doesn't help me...i'm just doing a simple c project for a fake nfs...i know what are semaphores and shared memory...i just want to do some test on my code and i need to remove all the shared data in one click – simone Jan 26 '10 at 23:58
  • @simone: You should have stated 'fake nfs' on your original question and pointed out that you understood semaphores and shared memory... it is still not clear as to what is "shared data"? – t0mm13b Jan 27 '10 at 00:05
  • And also inclusion of code as well to show your homework for us SO'ers to see....that would be of help also... – t0mm13b Jan 27 '10 at 00:06
0

In addition to bvamos's answer, according to the documentation the use of sem is deprecated :

NAME ipcrm - remove a message queue, semaphore set or shared memory id SYNOPSIS ipcrm [ -M key | -m id | -Q key | -q id | -S key | -s id ] ... deprecated usage

ipcrm [ shm | msg | sem ] id ...

remove shared memory

us ipcrm -m to remove a shared memory segment by the id

#!/bin/bash

set IPCS_M = ipcs -m | egrep "0x[0-9a-f]+ [0-9]+" | grep $USERNAME | cut -f2 -d" "

for id in $IPCS_M; do
  ipcrm -m $id;
done

or ipcrm -M to remove a shared memory segment by the key

#!/bin/bash

set IPCS_M = ipcs -m | egrep "0x[0-9a-f]+ [0-9]+" | grep $USERNAME | cut -f1 -d" "

for id in $IPCS_M; do
  ipcrm -M $id;
done

remove message queues

us ipcrm -q to remove a shared memory segment by the id

#!/bin/bash

set IPCS_Q = ipcs -q | egrep "0x[0-9a-f]+ [0-9]+" | grep $USERNAME | cut -f2 -d" "

for id in $IPCS_Q; do
  ipcrm -q $id;
done

or ipcrm -Q to remove a shared memory segment by the key

#!/bin/bash

set IPCS_Q = ipcs -q | egrep "0x[0-9a-f]+ [0-9]+" | grep $USERNAME | cut -f1 -d" "

for id in $IPCS_Q; do
  ipcrm -Q $id;
done

remove semaphores

us ipcrm -s to remove a semaphore segment by the id

#!/bin/bash

set IPCS_S = ipcs -s | egrep "0x[0-9a-f]+ [0-9]+" | grep $USERNAME | cut -f2 -d" "

for id in $IPCS_S; do
  ipcrm -s $id;
done

or ipcrm -S to remove a semaphore segment by the key

#!/bin/bash

set IPCS_S = ipcs -s | egrep "0x[0-9a-f]+ [0-9]+" | grep $USERNAME | cut -f1 -d" "

for id in $IPCS_S; do
  ipcrm -S $id;
done
RafaSashi
  • 16,483
  • 8
  • 84
  • 94
0

to remove all shared memory segments on FreeBSD

#!/bin/sh
for i in $(ipcs -m | awk '{ print $2 }' | sed 1,2d);
do
    echo "ipcrm -m $i"
    ipcrm -m $i
done

to remove all semaphores

#!/bin/sh
for i in $(ipcs -s | awk '{ print $2 }' | sed 1,2d);
do
    echo "ipcrm -s $i"
    ipcrm -s $i
done
riba
  • 186
  • 2
  • 7