我可以在 "shmget" 系统调用中使用的最大大小是多少? (获取共享内存)
What is the maximum size I can use in "shmget" system call? (To get shared memory)
我正在编写一个简单的程序来分配共享内存,
下面是 shmget 的示例代码,
#define SHM_SIZE 1024
main(int argc,char **argv)
{
int shmId,choice;
key_t key=8888;
char *shmPtr;
size_t memSize;
shmId=shmget(key, SHM_SIZE, IPC_CREAT | 0777);
if(shmId == -1)
{
perror("shmget");
exit(1);
}
以上代码运行正常,没有任何错误,
现在,当我将 SHM_SIZE 更改为 1024*1024*1024
然后我在 shmget
中收到错误
Error :
shmget: Invalid argument
谁能帮忙看看这是为什么?
我分配的是否超过最大值?
将评论编译成答案。 shmget man page 描述了 EINVAL
错误的可能原因:
A new segment was to be created and size < SHMMIN or size > SHMMAX, or no new segment was to be created, a segment with given key existed, but size is greater than the size of that segment.
由于您的系统配置的 SHMMAX
大于请求的大小,因此错误一定是由于第二个原因造成的。这可能是因为您首先 运行 较小的程序。由于共享内存在创建它的进程退出时不会自动删除,因此当程序再次 运行 时共享内存仍然存在,并且具有更大的大小。如错误描述中所述,不允许在具有更大大小的现有共享内存区域上调用 shmget
。
我正在编写一个简单的程序来分配共享内存, 下面是 shmget 的示例代码,
#define SHM_SIZE 1024
main(int argc,char **argv)
{
int shmId,choice;
key_t key=8888;
char *shmPtr;
size_t memSize;
shmId=shmget(key, SHM_SIZE, IPC_CREAT | 0777);
if(shmId == -1)
{
perror("shmget");
exit(1);
}
以上代码运行正常,没有任何错误,
现在,当我将 SHM_SIZE 更改为 1024*1024*1024 然后我在 shmget
中收到错误Error :
shmget: Invalid argument
谁能帮忙看看这是为什么?
我分配的是否超过最大值?
将评论编译成答案。 shmget man page 描述了 EINVAL
错误的可能原因:
A new segment was to be created and size < SHMMIN or size > SHMMAX, or no new segment was to be created, a segment with given key existed, but size is greater than the size of that segment.
由于您的系统配置的 SHMMAX
大于请求的大小,因此错误一定是由于第二个原因造成的。这可能是因为您首先 运行 较小的程序。由于共享内存在创建它的进程退出时不会自动删除,因此当程序再次 运行 时共享内存仍然存在,并且具有更大的大小。如错误描述中所述,不允许在具有更大大小的现有共享内存区域上调用 shmget
。