在使用共享内存的程序异常退出时,因为没有释放掉共享内存,在调试时会出现错误。您可使用shell命令来查看与释放已经分配的共享内存,下面将详细说明如何进行查看和释放分配的共享内存的方法。python
预备知识
Linux中经过API函数shmget建立的共享内存通常都是在程序中使用shmctl来释放的,可是有时为了调试程序,开发人员可能经过Ctrl + C等方式发送中断信号来结束程序,此时程序申请的共享内存就不能获得释放,固然若是程序没有改动的话,从新运行程序时仍然会使用上次申请的共享内存,可是若是咱们修改了程序,因为共享内存的大小不一致等缘由会致使程序申请共享内存错误。所以,咱们老是但愿每次结束时就能释放掉申请的共享内存。linux
有两种方法能够用来释放共享内存:shell
第一种:若是老是经过Crtl+C来结束的话,能够作一个信号处理器,当接收到这个信号的时候,先释放共享内存,而后退出程序。app
第二种:无论你以什么方式结束程序,若是共享内存仍是得不到释放,那么能够经过linux命令ipcrm shm shmid来释放,在使用该命令以前能够经过ipcs -m命令来查看共享内存。函数
共享内存查看
使用ipcs命令,不加如何参数时,会把共享内存、信号量、消息队列的信息都打印出来,若是只想显示共享内存信息,使用以下命令:调试
[root@localhost ~]# ipcs -morm
------ Shared Memory Segments --------队列
key shmid owner perms bytes nattch status 进程
0x00000000 1867776 root 600 393216 2 dest ip
0x00000000 1900545 root 600 393216 2 dest
0x00030021 1703938 zc 666 131104 1
0x0003802e 1736707 zc 666 131104 1
0x00030004 1769476 zc 666 131104 1
0x00038002 1802245 zc 666 131104 1
0x00000000 1933318 root 600 393216 2 dest
0x00000000 1966087 root 600 393216 2 dest
0x00000000 1998856 root 600 393216 2 dest
0x00000000 2031625 root 600 393216 2 dest
0x00000000 2064394 root 600 393216 2 dest
0x0014350c 2261003 cs 666 33554432 2
0x00000000 2129932 root 600 393216 2 dest
0x00000000 2162701 root 600 393216 2 dest
0x00143511 395837454 root 666 1048576 1
其中:
第一列就是共享内存的key;
第二列是共享内存的编号shmid;
第三列就是建立的用户owner;
第四列就是权限perms;
第五列为建立的大小bytes;
第六列为链接到共享内存的进程数nattach;
第七列是共享内存的状态status。其中显示“dest”表示共享内存段已经被删除,可是还有用户在使用它,当该段内存的mode字段设置为SHM_DEST时就会显示“dest”。当用户调用shmctl的IPC_RMID时,内存先查看多少个进程与这个内存关联着,若是关联数为0,就会销毁这段共享内存,否者设置这段内存的mod的mode位为SHM_DEST,若是全部进程都不用则删除这段共享内存。
共享内存释放
要释放共享内存,须要使用ipcrm命令,使用shmid做为参数,shmid在ipcs命令中会有输出,下面的命令能够释放全部已经分片的共享内存:
# ipcrm
# ipcs -m | awk ‘$2 ~/[0-9]+/ {print $2}’ | while read s; do sudo ipcrm –m $s; done
注:Linux中vi使用Ctrl+s来锁定,须要使用Ctrl+q来解除锁定。
使用Python编写的rmsharemem.py脚本以下:
# -*- coding: utf-8 -*-
# Remove the share memory
import os
import sys
import getopt
def usage():
print "usage: python rmsharemem.py -h -o -s size "
print " -h show help information"
print " -o the owner create share memory need to delete"
print " -s the share memory size"
print " the shmid list need to delete"
def getsharemem():
sharemap = {}
fp = os.popen('ipcs -m')
lines = fp.readlines()
for l in lines:
if not l.startswith('0x'):
continue
s = l.split()
if sharemap.has_key(s[2]):
sharemap[s[2]].append(s)
else:
sharemap[s[2]] = [s]
#print 'Share memory map:\n', sharemap
return sharemap
if __name__ == "__main__":
opts, args = getopt.getopt(sys.argv[1:], "o:hs:")
# opts is the parameter with options
# args is the parameter no ptions
owner = None
size = 0
for o, p in opts:
if o == '-h':
usage()
sys.exit(0)
elif o == '-o':
owner = p
elif o == '-s':
size = p
if not owner:
val = raw_input("Are you sure to remove all share memory?(yes/no)");
if (val <> "yes"):
usage()
sys.exit(0)
count = 0
total = 0
if len(args) > 0:
for shmid in args:
cmd = 'ipcrm -m %s' % shmid
print 'execute command: %s' % cmd
ret = os.system(cmd)
total += 1
if ret == 0:
count += 1
print 'remove %s shared memory success' % shmid
else:
print 'remove %s shared memory failed' % shmid
else:
shmmap = getsharemem()
for o, l in shmmap.items():
if owner and o <> owner:
continue
for p in l:
total += 1
if size and size <> p[4]:
continue
cmd = 'ipcrm -m %s' % p[1]
print 'execute command: %s' % cmd
ret = os.system(cmd)
if ret == 0:
count += 1
print 'remove %s shared memory success' % p[1]
else:
print 'remove %s shared memory failed' % p[1]
print 'total share memory number = %s' % total
print 'remove success number = %s' % count
sys.exit(0)
共享内存大小修改
使用下面的命令查看共享内存的大小:
# cat /proc/sys/kernel/shmmax
修改共享内存大小:
临时修改:在root用户下执行# echo 268435456 > /proc/sys/kernel/shmmax把共享内存大小设置为256MB;
永久修改:在root用户下修改/etc/rc.d/rc.local文件,加入下面一行:
echo 268435456 > /proc/sys/kernel/shmmax
便可每次启动时把共享内存修改成256MB。