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: NSEMS.-c Display creators login name and group name: forall facilities: CREATOR, CGROUP.-o Display information on outstanding usage: for