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

发布于 2024-08-19 06:14:29 字数 54 浏览 4 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

彼岸花ソ最美的依靠 2024-08-26 06:14:29

在这里,保存并在 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

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

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

请别遗忘我 2024-08-26 06:14:29

这适用于我的 Mac 操作系统:

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

This works on my Mac OS:

for n in `ipcs -b -m | egrep ^m | awk '{ print $2; }'`; do ipcrm -m $n; done
治碍 2024-08-26 06:14:29
ipcs -s | grep $USERNAME | perl -e 'while (<STDIN>) { @a=split(/\s+/); print `ipcrm sem $a[1]`}'

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

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

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.

慈悲佛祖 2024-08-26 06:14:29
#!/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
#!/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
痴意少年 2024-08-26 06:14:29

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

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.

心碎的声音 2024-08-26 06:14:29

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

ipcs -a | grep `whoami`

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

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

对于 sun 则为:

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

di.uoa.gr 提供的信息

再次检查是否一切正常

对于删除 your sem/shared mem -假设您是没有管理员权限的工作站用户

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

半窗疏影 2024-08-26 06:14:29

1 行将完成所有

对于消息队列

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

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

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

awk 将完成剩下的工作,即使用命令 " 打印和删除ipcrm -q" wrt 第 2 列的值 (coz $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)

故人的歌 2024-08-26 06:14:29

我在 FreeBSD 中是这样做的:

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

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
吻安 2024-08-26 06:14:29

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

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

以删除所有信号量

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

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
对你再特殊 2024-08-26 06:14:29

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

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

希望这有帮助,
此致,
汤姆.

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.

黄昏下泛黄的笔记 2024-08-26 06:14:29

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

NAME ipcrm - 删除消息队列、信号量集或共享内存 ID
概要 ipcrm [ -M key | -m id | -Q键| -q id | -S键| -s id ]
...已弃用的用法

ipcrm [ shm | ]味精|扫描电镜] 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

,或使用 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

删除消息队列

共享内存段,使用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

,或者通过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

使用 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

或通过 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文