【问题标题】:have deleted the shared memory but get shm ipc_id error已删除共享内存但得到 shm ipc_id 错误
【发布时间】:2013-11-05 09:35:33
【问题描述】:

我有密码

shmid = shmget(IPC_CREAT, size, IPC_CREAT|0666) ;
if ( shmid < 0 )
{
        perror("get shm ipc_id error") ;
        return -1 ;
}
shmaddr = (unsigned int*)shmat(shmid, 0, 0 ) ;

创建共享内存并做一些事情

进入底部

  shmdt( shmaddr ) ;
  shmctl(shmid, IPC_RMID, NULL) ;

删除共享内存。

第一次执行程序,运行良好。

但是如果我第二次执行,它会 printf

get shm ipc_id error: Invalid argument

我不知道为什么会这样?

我已经删除并分离了共享内存,为什么还会出现这种情况?

以及如何预防?

【问题讨论】:

  • 请注意,密钥无效(shmget 的第一个参数)。您可能想使用IPC_PRIVATE。见manpage

标签: c linux shared-memory


【解决方案1】:

根据 man shmget:

ERRORS
       On failure, errno is set to one of the following:

       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.

ipcs可以检查删除是否成功。

【讨论】:

    猜你喜欢
    • 2016-08-04
    • 1970-01-01
    • 2013-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-03
    • 2013-03-14
    • 1970-01-01
    相关资源
    最近更新 更多