1、共享内存的一点小笔记 1、共享内存相关的一些参数:1)/proc/sys/kernel/shmall该文件表示在任何给定时刻,系统上可以使用的共享内存的总量(bytes)。缺省设置:2097152(操作系统的内核、版本不同会有所不同)2)/proc/sys/kernel/shmmax该文件表示内核所允许的最大共享内存段的大小(bytes)。缺省设置:33554432建议设置:物理内存 * 50%实际可用最大共享内存段大小=shmmax * 98%,其中大约 2%用于共享内存结构3)/proc/sys/kernel/shmmni该文件表示用于整个系统的共享内存段的最大数目(个)。缺省设置:409
2、64)/proc/sys/kernel/sem该文件用于控制内核信号量,信号量是 System VIPC用于进程间通讯的方法。建议设置:250 32000 100 128第一列,表示每个信号集中的最大信号量数目。第二列,表示系统范围内的最大信号量总数目。第三列,表示每个信号发生时的最大系统操作数目。第四列,表示系统范围内的最大信号集总数目。所以,(第一列)*(第四列)=(第二列)2、如何查看这些参数:1)直接查看文件cat /proc/sys/kernel/shmall2684352002)使用 sysctl命令/sbin/sysctl -e kernelkernel.shmall = 268
3、4352003)使用 ipcs命令ipcs -l- Shared Memory Limits -max number of segments = 4096max seg size (kbytes) = 4194303max total shared memory (kbytes) = 1073740800min seg size (bytes) = 1- Semaphore Limits -max number of arrays = 4096max semaphores per array = 250max semaphores system wide = 256000max ops per
4、 semop call = 4096semaphore max value = 32767- Messages: Limits -max queues system wide = 16max size of message (bytes) = 65536default max size of queue (bytes) = 655363、如何修改这些值1)直接修改文件内容echo “268435200“ /proc/sys/kernel/shmall优点:简直、直观、快捷,马上生效缺点:机器重启后修改丢失,又恢复成默认值PS. 可以在 rc.local加入这种修改方式,这样机器迁移时,可以很直
5、观得知道共享内存的值需要修改。2)修改/etc/sysctl.conf 文件优点:永久修改,重启不会丢失缺点:需重启才会生效ps.可通过/sbin/sysctl -p 命令使修改立即生效,而不需要重启3)使用/sbin/sysctl 命令sysctl -w kernel.shmmni优点:会做数据有效性校验,安全可靠,且马上生效,重启不会丢失缺点:在做机器迁移时,此种修改方法易被忽略综上,建议使用第 3种方式进行修改,且做好文档的管理工作,在明显位置记录此种修改。4、其他一些问题:1)如何查看本机的所有共享内存:ipcs -m输出内容为“key shmid owner perms bytes
6、nattch status ”2)如何查看本机的所有信号量ipcs -s3)如何删除一个共享内存ipcrm -M 0x0000231d4)如何删除一个信号量ipcrm -S 0x0000231d5)如何知道一个共享内存被哪些进程使用了#参数中 SYSV后面的值为共享内存的 KEY值lsof | grep “SYSV00002320“6)进程加载共享内存失败怎么处理?#首先确认共享内存有没有正确初始化ipcs -m -i 0x00002600#如果没有初始化,应该调用始始化程序进行初始化;如果已经初始化可尝试,删除后重新初始化ipcrm -M 0x000026007)进程初始化共享内存或信号量失
7、败怎么处理?一般是由于超过了共享内存或信号量限制导致。可通过 ipcs -u查看当前的使用情况。可通过以上第三步中介绍的方法进行修改。 8)如何删除系统中所有已不再使用的工享内存 ipcs -m | awk if($6=0) print “ipcrm -M “$1| sh即第 6个字段,nattach 为零的共享内存即为没有进程使用的共享内存。PS.如果是在现网进行操作一定要谨慎使用此命令,因为可能有的共享内存可能当前没有被使用,但是在其他时刻有可能会被继续使用ipcs 命令ipcs - 分析消息队列、共享内存和信号量 ipcs - report status of interprocess
8、communication facilities ipcs displays certain information about active interprocess communication facilities. With no options, ipcs displays information in short format for the message queues, shared memory segments, and semaphores that are currently active in the system. 引用它的语法: ipcs -mqs -abcopt
9、-C core -N namelist -m 输出有关共享内存(shared memory)的信息-q 输出有关信息队列 (message queue)的信息-s 输出信号量(semaphore)的信息# ipcs -mIPC status from as of 2007 年 04 月 10 日 星期二 18 时 32 分18 秒 CSTT ID KEY MODE OWNER GROUPShared Memory:m 0 0x50000d43 -rw-r-r- root rootm 501 0x1e90c97c -rw-r- oracle dbaipcrm - 删除 ipc(清除共享内存信息
10、)引用它的语法:ipcrm -m|-q|-s shm_id-m 输出有关共享内存(shared memory)的信息-q 输出有关信息队列 (message queue)的信息-s 输出信号量(semaphore)的信息shm_id 共享内存 id#ipcrm -m 501ipcs 用于显示消息队列、共享内存、信号灯信息的q 显示消息队列s 显示信号灯m 显示共享内存a 详细信息其打印出当前你的系统中处于活动状态的共享内存的信息。对每一个资源,这个命令会显示:TYPE 包括信息队列(q),共享内存段( m),或者信号灯(s)。ID 资源条目的唯一的表示号KEY 应用程序存取资源使用的参数。MO
11、DE 存取模式和许可权限的标记OWNER and GROUP 登录名和用户属主的组号,OnLine 使用的所有的共享内存资源的属主的属主 为 root 和informix 组。OnLine 共享内存使用的基本键值为 0x52564801。SERVERNUM的值乘上 0X10000 并且加上这 个值就为共享内存的键值。这就 是,如果你的 SERVERNUM 的值为 2,你的共享内存段的键值为0x52564801+(2*0x10000)=0x52584801。如果 SERVERNUM的值为 6,产生的键值就为 0x525c4801。清除命令:ipcs -s | grep nobody | perl
12、 -e while () a=split(/“s+/); print ipcrm sem $a1 然后启动 Apache 即可。更深入的了解:Fixing Apache “No space left on device: Couldnt create accept lock“ errors Error Message: When starting Apache, I get this error message in the main Apache error_log: emerg (28)No space left on device: Couldnt create accept lockn
13、otice suEXEC mechanism enabled (wrapper: /usr/sbin/suexec)notice Digest: generating secret for digest authentication .notice Digest: donewarn pid file /etc/httpd/run/httpd.pid overwritten - Unclean shutdown of previous Apache run?emerg (28)No space left on device: Couldnt create accept lockFirst off
14、, check to make sure that you really arent out of disk space, or have hit a quota limit. Another way that Apache can create the “accept lock“ is with a semaphore. A semaphore is an inter-process communication tool that is used by Apache to communicate with its child processes. This error message may
15、 mean that Apache couldnt create a new semaphore. Check to see how many semaphores are currently in use. If Apache is running correctly, you should see something like this: # ipcs -s- Semaphore Arrays -key semid owner perms nsems0x00000000 68681743 apache 600 10x00000000 68714515 apache 600 10x00000
16、000 68747291 apache 600 1If Apache is stopped, and you still see these semaphores, then you can safely kill them by running this command for each semaphore id (in the second column) $ ipcrm -s To destroy all semaphores, you can run this from the command line (with “apache“ being the apache-user): fo
17、r semid in ipcs -s | grep nobody | cut -f2 -d“ “; do ipcrm -s $semid; doneIf you are out of semaphores If you cant create any more semaphores: Sometimes your system may need to increase the number of semaphores that are available on the system. This requires a change to a kernel parameter. If you ar
18、e running on a virtual server and cannot modify kernel parameters, you may need to ask your hosting provider to change this parameter on their host server To view the current parameters: # ipcs -l- Shared Memory Limits -max number of segments = 4096max seg size (kbytes) = 32768max total shared memor
19、y (kbytes) = 8388608min seg size (bytes) = 1- Semaphore Limits -max number of arrays = 1024max semaphores per array = 250max semaphores system wide = 256000max ops per semop call = 32semaphore max value = 32767- Messages: Limits -max queues system wide = 1024max size of message (bytes) = 8192default
20、 max size of queue (bytes) = 16384To change these parameters, modify the file /etc/sysctl.conf and add the following lines: kernel.msgmni = 1024kernel.sem = 250 256000 32 1024Then load these settings with the command:sysctl -pYour Apache process should now be able to create the needed semaphores and
21、 run properly这里介绍下 kernel.msgmni/proc/sys/kernel/msgmni该文件指定消息队列标识的最大数目,即系统范围内最大多少个消息队列。缺省设置:16 在 root 下用 sysctl kernel.msgmni 检查该参数, 也可以在命令行下sysctl -w kernel.msgmni=XXX 重新设定。man ipcs: ipcs(1) ipcs(1)NAMEipcs - report status of interprocess communication facilitiesSYNOPSISipcs -mqs -abcopt -C core -
22、N namelistDESCRIPTIONipcs displays certain information about active interprocesscommunication facilities. With no options, ipcs displays informationin short format for the message queues, shared memory segments, andsemaphores that are currently active in the system.OptionsThe following options restr
23、ict the display to the correspondingfacilities.(none) This is equivalent to -mqs.-m Display information about active shared memorysegments.-q Display information about active message queues.-s Display information about active semaphores.The following options add columns of data to the display. See “
24、ColumnDescription“ below.(none) Display default columns: for all facilities: T,ID, KEY, MODE, OWNER, GROUP.-a Display all columns, as appropriate. This isequivalent to -bcopt.-b Display largest-allowable-size information: formessage queues: QBYTES; for shared memorysegments: SEGSZ; for semaphores: N
25、SEMS.-c Display creators login name and group name: forall facilities: CREATOR, CGROUP.-o Display information on outstanding usage: formessage queues: CBYTES, QNUM; for shared memorysegments: NATTCH.-p Display process number information: for messagequeues: LSPID, LRPID; for shared memory segments:CP
26、ID, LPID.-t Display time information: for all facilities:CTIME; for message queues: STIME, RTIME; forHewlett-Packard Company - 1 - HP-UX Release 11i: November 2000ipcs(1) ipcs(1)shared memory segments: ATIME, DTIME; forsemaphores: OTIME.The following options redefine the sources of information.-C co
27、re Use core in place of /dev/kmem. core can be acore file or a directory created by savecrash orsavecore.-N namelist Use file namelist or the namelist within core inplace of /stand/vmunix. It opens a crash dump forreading. Please refer to cr_open(3) for moredetails.Column DescriptionsThe column head
28、ings and the meaning of the columns in an ipcs listingare given below. The columns are printed from left to right in theorder shown below.T Facility type:m Shared memory segmentq Message queues SemaphoreID The identifier for the facility entry.KEY The key used as an argument to msgget(), semget(), o
29、rshmget() to create the facility entry. (Note: The keyof a shared memory segment is changed to IPC_PRIVATEwhen the segment has been removed until all processesattached to the segment detach it.)MODE The facility access modes and flags: The mode consistsof 11 characters that are interpreted as follow
30、s:The first two characters can be:R A process is waiting on a msgrcv().S A process is waiting on a msgsnd().D The associated shared memory segment has beenremoved. It will disappear when the lastprocess attached to the segment detaches it.C The associated shared memory segment is to becleared when t
31、he first attach is executed.- The corresponding special flag is not set.The next 9 characters are interpreted as three sets ofthree characters each. The first set refers to theowners permissions, the next to permissions of othersin the group of the facility entry, and the last to allHewlett-Packard
32、Company - 2 - HP-UX Release 11i: November 2000ipcs(1) ipcs(1)others.Within each set, the first character indicatespermission to read, the second character indicatespermission to write or alter the facility entry, andthe last character is currently unused.r Read permission is granted.w Write permissi
33、on is granted.a Alter permission is granted.- The indicated permission is not granted.OWNER The login name of the owner of the facility entry.GROUP The group name of the group of the owner of thefacility entry.CREATOR The login name of the creator of the facility entry.CGROUP The group name of the g
34、roup of the creator of thefacility entry.CBYTES The number of bytes in messages currently outstandingon the associated message queue.QNUM The number of messages currently outstanding on theassociated message queue.QBYTES The maximum number of bytes allowed in messagesoutstanding on the associated me
35、ssage queue.LSPID The process ID of the last process to send a message tothe associated message queue.LRPID The process ID of the last process to receive a messagefrom the associated message queue.STIME The time the last msgsnd() message was sent to theassociated message queue.RTIME The time the las
36、t msgrcv() message was received fromthe associated message queue.CTIME The time when the associated facility entry was createdor changed.NATTCH The number of processes attached to the associatedshared memory segment.SEGSZ The size of the associated shared memory segment.Hewlett-Packard Company - 3 -
37、 HP-UX Release 11i: November 2000ipcs(1) ipcs(1)CPID The process ID of the creating process of the sharedmemory segment.LPID The process ID of the last process to attach or detachthe shared memory segment.ATIME The time the last shmat() attach was completed to theassociated shared memory segment.DTI
38、ME The time the last shmdt() detach was completed on theassociated shared memory segment.NSEMS The number of semaphores in the set associated with thesemaphore entry.OTIME The time the last semop() semaphore operation wascompleted on the set associated with the semaphoreentry.WARNINGSipcs produces o
39、nly an approximate indication of actual system statusbecause system processes are continually changing while ipcs isacquiring the requested information.Do not rely on the exact field widths and spacing of the output, asthese will vary depending on the system, the release of HP-UX, and thedata to be
40、displayed.FILES/dev/kmem Kernel virtual memory/etc/group Group names/etc/passwd User names/stand/vmunix System namelistSEE ALSOmsgop(2), semop(2), shmop(2).STANDARDS CONFORMANCEipcs: SVID2, SVID3Hewlett-Packard Company - 4 - HP-UX Release 11i: November 2000记录一下前几天遇到的一个问题及其解决方法。escrow 环境启动不了,error_lo
41、g 提示“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,然后使用 ipc
42、rm -m $shmid 的方式将共享内存释放;ipcs -m | grep login | awk print $2 | xargs ipcrm shm后来还在梁大师的博客上看到他以前记录的一个 Bash 脚本可以清理 IPC 资源的(包括共享内存 Shared Memory 和信号量 Semaphore),如下:#!/bin/sh# ipcclean.sh#CMDNAME=basename $0if “$1“ = -? -o “$1“ = “-help“ ; thenecho “$CMDNAME cleans up shared memory and semaphores from abo
43、rted PostgreSQL“echo “backends.“echoecho “Usage:“echo “ $CMDNAME“echoecho “Note: Since the utilities underlying this scrpt 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 “echo “so that your platform. can be
44、 supported in the future.“exit 0fiif “$USER“ = root -o “$LOGNAME“ = root then(echo “$CMDNAME: cannot be run as root“ 1 thenif ps -eA | grep -s postmaster /dev/null 2 thenecho “$CMDNAME: a postmaster is still running“ 1 thendid_anything=if ps x | grep -s postmaster /dev/null 2 thenecho “$CMDNAME: a p
45、ostmaster is still running“ 1 thenecho “skipped; process still exists (pid $ipcs_cpid or $ipcs_lpid).“continuefi# try removeipcrm shm $ipcs_shmidif “$?“ -eq 0 ; thendid_anything=telseexitfidone# semaphoresfor val in ipcs -s -c | grep 0-9 | awk printf “%sn“, $1; doecho -n “Semaphore $val . “# try removeipcrm sem $valif “$?“ -eq 0 ; thendid_anything=telseexitfidone -z “$did_anything“ return -1;key_t key = atol(argv1);int shmid;for(int i=0;i100;i+) shmid=shmget(key,1024,0666);shmctl(shmid,IPC_RMID,0);key+;return 0;