Linux 删除类 UNIX 系统上的所有 SYSTEM V 共享内存和信号量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2143404/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 19:43:53  来源:igfitidea点击:

Delete all SYSTEM V shared memory and semaphores on UNIX-like systems

linuxunixposixsemaphoreshared-memory

提问by simone

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

如何在类 UNIX 系统(例如 Ubuntu)上使用单个命令删除所有未使用的信号量和共享内存?

回答by MBO

I don't know how to delete all at once, but you can use ipcsto 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.

我不知道如何一次全部删除,但是您可以使用ipcs来列出资源,然后使用循环和删除ipcrm。这应该有效,但需要做一些工作。我记得我在课堂上做过一次。

回答by t0mm13b

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.

既然您提到您正在 NFS 系统上工作,您是否可以访问这些信号量和共享内存?我想你误解了它们是什么,它们是一个 API 代码,使进程能够相互通信,信号量是防止竞争条件和线程相互通信的解决方案,简单地说,它们不会留下任何残留物在任何文件系统上。

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?

除非您使用套接字或管道?您是否具有删除它们的必要权限,为什么它们在 NFS 系统上?

Hope this helps, Best regards, Tom.

希望这会有所帮助,最好的问候,汤姆。

回答by bvamos

ipcs -s | grep $USERNAME | perl -e 'while (<STDIN>) { @a=split(/\s+/); print `ipcrm sem $a[1]`}'

or

或者

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

Change $USERNAME to a real username.

将 $USERNAME 更改为真实用户名。

回答by neverMind

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

在这里,保存并在你的 shell 上尝试这个脚本 (kill_ipcs.sh):

#!/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

每当我们在大学生服务器上运行 IPCS 程序时,我们都会使用它。有些人并不总是清理所以......这是需要的:P

回答by Mr_and_Mrs_D

Check if there is anything to delete with :

检查是否有任何要删除的内容:

ipcs -a | grep `whoami`

On linux delete them all via :

在 linux 上通过以下方式将它们全部删除:

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

For sun it would be :

对于太阳,它将是:

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

courtsesy of di.uoa.gr

由 di.uoa.gr 提供

Check again if all is ok

再次检查是否一切正常

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

用于删除您的sem/共享内存 - 假设您是没有管理员权限的工作站中的用户

回答by Serge

This works on my Mac OS:

这适用于我的 Mac OS:

for n in `ipcs -b -m | egrep ^m | awk '{ print ; }'`; do ipcrm -m $n; done

回答by John Moss

This is how I do it in FreeBSD:

这就是我在 FreeBSD 中的做法:

#!/usr/local/bin/bash
for i in $(ipcs -a | grep "^s" | awk '{ print  }');
do
        echo "ipcrm -s $i"
        ipcrm -s $i
done

回答by RafaSashi

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

除了 bvamos 的回答之外,根据文档,sem不推荐使用 :

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 ...

名称 ipcrm - 删除消息队列、信号量集或共享内存 ID 概要 ipcrm [ -M 键 | -m id | -Q 键 | -q id | -S 键 | -s id ] ... 不推荐使用

ipcrm [ shm | 留言 | sem ] id ...

remove shared memory

删除共享内存

us ipcrm -mto remove a shared memory segment by the id

我们ipcrm -m通过 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 -Mto remove a shared memory segment by the key

ipcrm -M通过键删除共享内存段

#!/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 -qto remove a shared memory segment by the id

我们ipcrm -q通过 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 -Qto remove a shared memory segment by the key

ipcrm -Q通过键删除共享内存段

#!/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 -sto remove a semaphore segment by the id

我们ipcrm -s通过 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 -Sto remove a semaphore segment by the key

ipcrm -S通过键删除信号量段

#!/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

回答by Richard Bartisek

to remove all shared memory segments on FreeBSD

删除 FreeBSD 上的所有共享内存段

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

回答by pPanda_beta

1 line will do all

1 行将完成所有工作

For message queue

对于消息队列

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

ipcs -qwill give the records of message queues

ipcs -q将给出消息队列的记录

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

sed "$ d; 1,2d "将删除最后一个空行 ( "$ d") 和前两个标题行 ( "1,2d")

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

awk将完成剩下的工作,即使用命令打印和删除"ipcrm -q"列 2 ( coz $2)的值