简体   繁体   English

shmget大小限制问题

[英]shmget size limit issue

I have this snippet of code: 我有以下代码片段:

if ((shmid = shmget(key, 512, IPC_CREAT | 0666)) < 0)
{   
    perror("shmget");
    exit(1);
}   

Whenever I set the number any higher than 2048, I get, an error that says: 每当我将数字设置为大于2048时,都会出现错误消息:

shmget: Invalid argument

However when I run cat /proc/sys/kernel/shmall , I get 4294967296 . 但是,当我运行cat /proc/sys/kernel/shmall ,我得到4294967296

Does anybody know why this is happening? 有人知道为什么会这样吗? Thanks in advance! 提前致谢!

The comment from Jerry is correct, even if cryptic if you haven't played with this stuff: "What about this: EINVAL: ... a segment with given key existed, but size is greater than the size of that segment. " 即使您还没有玩过这些东西,Jerry的评论也是正确的,即使它是神秘的:“这怎么办: EINVAL: ... a segment with given key existed, but size is greater than the size of that segment.

He meant that the segment is already there - these segment are persistent - and it has size 2048. 他的意思是该段已经存在-这些段是持久的-大小为2048。

You can see it among the other ones with: 您可以通过以下方式在其他视图中看到它:

$ ipcs -m

and you can remove your segment (beware: remove your one only) with: 您可以使用以下方法删除细分受众群(注意:仅删除您的细分受众群):

$ ipcrm -M <key>

After that you should be able to create it larger. 之后,您应该可以将其创建得更大。

man 5 proc refers to three variables related to shmget(2) : man 5 proc引用与shmget(2)相关的三个变量:

  • /proc/sys/kernel/shmall
    This file contains the system-wide limit on the total number of pages of System V shared memory. 该文件包含系统范围内System V共享内存的总页数限制。
  • /proc/sys/kernel/shmmax
    This file can be used to query and set the run-time limit on the maximum (System V IPC) shared memory segment size that can be created. 该文件可用于查询和设置对可创建的最大(System V IPC)共享内存段大小的运行时限制。 Shared memory segments up to 1GB are now supported in the kernel. 内核现在支持高达1GB的共享内存段。 This value defaults to SHMMAX. 此值默认为SHMMAX。
  • /proc/sys/kernel/shmmni
    (available in Linux 2.4 and onward) This file specifies the system-wide maximum number of System V shared memory segments that can be created. (在Linux 2.4及更高版本中可用)此文件指定系统范围内可以创建的System V共享内存段的最大数目。

Please check you violated none of them. 请检查您没有违反任何一项。 Note that shmmax and SHMMAX are in bytes and shmall and SHMALL are in the number of pages (the page size is usually 4 KB but you should use sysconf(PAGESIZE) .) I personally felt your shmall is too large (2**32 pages == 16 TB) but not sure if it is harmful or not. 请注意, shmmaxSHMMAX以字节为单位, shmallSHMALL以页数为单位(页面大小通常为4 KB,但您应该使用sysconf(PAGESIZE) 。)我个人觉得您的shmall太大(2 ** 32页) == 16 TB),但不确定是否有害。

As for the definition of SHMALL , I got this result on my Ubuntu 12.04 x86_64 system: 至于SHMALL的定义,我在Ubuntu 12.04 x86_64系统上得到了以下结果:

$ ack SHMMAX /usr/include
/usr/include/linux/shm.h
9: * SHMMAX, SHMMNI and SHMALL are upper limits are defaults which can
13:#define SHMMAX 0x2000000              /* max shared seg size (bytes) */
16:#define SHMALL (SHMMAX/getpagesize()*(SHMMNI/16))

/usr/include/linux/sysctl.h
113:    KERN_SHMMAX=34,         /* long: Maximum shared memory segment */

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM