All things are difficult before they are easy. 没有软件的裸机是一具僵尸,没有硬件的软件是一个幽灵。2012,专注于Linux和C语言,关注自动化、性能测试,关注开源社区和开源测试工具、方法,尝试测试团队管理!
Linux IPC资源清理
上一篇 /
下一篇 2010-10-31 16:10:08
/ 个人分类:Linux
记录一下前几天遇到的一个问题及其解决方法。
escrow 环境启动不了,error_log提示“No spaceleft on device: mod_rewrite: could not create rewrite_log_lock Configuration Failed“,df命令去检查磁盘空间并没有满。应该是“没有正常退出程序,造成大量进程间资源未释放”;
用命令“ipcs -s | grep login | awk '{print $2}' |xargs ipcrm sem"即可释放资源。(login 是当前的$LOGNAME,这里的资源是信号量)
如果是共享内存资源问题,先用ipcs命令获取Shared Memory Segments的shmid,然后使用ipcrm -m $shmid 的方式将共享内存释放;ipcs -m | grep login | awk '{print $2}' | xargs ipcrm shm后来还在梁大师的博客上看到他以前记录的一个Bash脚本可以清理IPC资源的(包括共享内存Shared Memory和信号量Semaphore),如下:
#!/bin/sh # # ipcclean.sh #
CMDNAME=`basename $0`
if [ "$1" = '-?' -o "$1" = "--help" ]; then echo "$CMDNAME cleans up shared memory and semaphores from aborted PostgreSQL" echo "backends." echo echo "Usage:" echo " $CMDNAME" echo echo "Note: Since the utilities underlying this scrīpt are very different" echo "from platform. to platform, chances are that it might not work on" echo "yours. If that is the case, please write to <pgsql-bugs@postgresql.org>" echo "so that your platform. can be supported in the future." exit 0 fi
if [ "$USER" = 'root' -o "$LOGNAME" = 'root' ] then ( echo "$CMDNAME: cannot be run as root" 1>&2 echo "Please log in (using, e.g., \"su\") as the (unprivileged) user that" 1>&2 echo "owned the server process." 1>&2 ) 1>&2 exit 1 fi
EffectiveUser=`id -n -u 2>/dev/null || whoami 2>/dev/null`
#----------------------------------- # List of platform-specific hacks # Feel free to add yours here. #----------------------------------- # # This is QNX 4.25 # if [ `uname` = 'QNX' ]; then if ps -eA | grep -s '[p]ostmaster' >/dev/null 2>&1 ; then echo "$CMDNAME: a postmaster is still running" 1>&2 exit 1 fi rm -f /dev/shmem/PgS* exit $? fi # # This is based on RedHat 5.2. 这里之后是该脚本的核心。 # if [ `uname` = 'Linux' ]; then did_anything=
if ps x | grep -s '[p]ostmaster' >/dev/null 2>&1 ; then echo "$CMDNAME: a postmaster is still running" 1>&2 exit 1 fi
# shared memory for val in `ipcs -m -p | grep '^[0-9]' | awk '{printf "%s:%s:%s\n", $1, $3, $4}'` do save_IFS=$IFS IFS=: set X $val shift IFS=$save_IFS ipcs_shmid=$1 ipcs_cpid=$2 ipcs_lpid=$3
# Note: We can do -n here, because we know the platform. echo -n "Shared memory $ipcs_shmid ... "
# Don't do anything if process still running. # (This check is conceptually phony, but it's # useful anyway in practice.) ps hj $ipcs_cpid $ipcs_lpid >/dev/null 2>&1 if [ "$?" -eq 0 ]; then echo "skipped; process still exists (pid $ipcs_cpid or $ipcs_lpid)." continue fi
# try remove ipcrm shm $ipcs_shmid if [ "$?" -eq 0 ]; then did_anything=t else exit fi done
# semaphores for val in `ipcs -s -c | grep '^[0-9]' | awk '{printf "%s\n", $1}'`; do echo -n "Semaphore $val ... " # try remove ipcrm sem $val if [ "$?" -eq 0 ]; then did_anything=t else exit fi done
[ -z "$did_anything" ] && echo "$CMDNAME: nothing removed" && exit 1 exit 0 fi # end Linux
# This is the original implementation. It seems to work # on FreeBSD, SunOS/Solaris, HP-UX, IRIX, and probably # some others.
|
另外,还看到大师关于IPC资源清理的C++版的实现(需要传入自己共享内存的shmid,这个shmid可通过命令 ipcs -m 获得),也贴在下面了。
#include <sys/shm.h> #include <stdlib.h> #include <stdio.h> int main(int argc,char** argv) { if ( argc < 1 ) { printf("%s 十进制的keyid\r\n",argv[0]); return -1; } key_t key = atol(argv[1]); int shmid; for(int i=0;i<100;i++) { shmid=shmget(key,1024,0666); shmctl(shmid,IPC_RMID,0); key++; } return 0; } |
相关阅读:
- 系统管理员不可不知的三条黄金法则 (xin_晴, 2010-11-04)
- Linux系统管理入门必须经历的三步 (xin_晴, 2010-10-19)
- Linux测试环境搭建的学习建议 (xin_晴, 2010-10-20)
- Linux初学者最常遇到的五个问题 (xin_晴, 2010-10-21)
- 十五个常用Linux命令行总汇 (xin_晴, 2010-10-22)
- 从开发者的角度学习Linux内存管理 (xin_晴, 2010-10-25)
- 教会你Linux Shell自动交互的三种方法 (xin_晴, 2010-11-16)
- Linux系统学习方法论 (xin_晴, 2010-10-27)
- 把你的Linux变成无线基站服务器 (xin_晴, 2010-10-28)
- Linux 内存监控内存丢失和处理方法 (xin_晴, 2010-10-29)
收藏
举报
TAG:
ipc
Linux
共享内存
IPC
信号量