您的位置:首页 > 运维架构 > Linux

linux共享内存分配失败原因分析

2010-03-22 21:01 225 查看
新版本的服务器使用基于UDP的多进程架构,进程之间的通过共享内存来互相通讯。

linux下共享内存的使用通过shmget函数来建立。

 

#include <sys/ipc.h>
#include <sys/shm.h>
int shmget(key_t key, size_t size, int shmflg);

 

 

由于要分配固定大小的共享内存来存放在线用户表,在内网测试的时候,分配10000个用户同时在线的内存没有问题,放到外网测试的时候,只能分配

到100个用户所需的共享内存,当分配大于100个用户所需内在的时候,就失败了,失败号为: EINVAL

 

查看shmget手册:

可能导致出现这个问题主要有两点

 

1.A shared memory segment is to be created and the value of size is less than the system-imposed minimum or greater than the system-imposed maximum。

 

2.No shared memory segment is to be created and a shared memory segment exists for key but the size of the segment associated with it is less than size and size is not 0.

 

1.分配的共享内在大小小于系统提供的最小共享内在或者大于系统提供的最大共享内存

 

2.申请共享内存的KEY已经存在(通过ipcs命令查看),并且现在申请的内存大于已经存在的共享内在大小。

 

通过查看linux内核参数,将第一点排除,由于把程序放到外网的时候,开始是按100个用户同时在线来分配内存来测试,再按照10000个用户来分配共享内存,当然失败了。

 

解决方案:

找出已经存在的共享内存的Key,使用ipcrm -m Key(Key为共享内存的ID)将已经存在的共享内存删除,再开到10000个用户,成功了。。。

 

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息