문제

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:

shmget: Invalid argument

However when I run cat /proc/sys/kernel/shmall, I get 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."

He meant that the segment is already there - these segment are persistent - and it has size 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):

  • /proc/sys/kernel/shmall
    This file contains the system-wide limit on the total number of pages of System V shared memory.
  • /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. Shared memory segments up to 1GB are now supported in the kernel. This value defaults to 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.

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.

As for the definition of SHMALL, I got this result on my Ubuntu 12.04 x86_64 system:

$ 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 ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top