【发布时间】:2018-02-18 20:00:53
【问题描述】:
我在共享内存方面遇到了一点麻烦,如果有人能指出我正确的方向,我可以提供一些指导。
// Allocate Shared Memory
key_t key = 56789;
int shmid;
char* shm_address;
int* value;
// Reserve the memory
if (shmid = shmget(key, sizeof(int), IPC_CREAT | 0777) < 0)
{
perror("shmget was unsuccessful");
exit(1);
}
else
{
printf("\nMemory created successfully:%d\n", shmid);
}
// Attach to memory address
if ( (shm_address = shmat(shmid, NULL, 0)) == (char *)-1 )
{
perror("shmat was unsuccessful");
exit(1);
}
else
{
printf ("shared memory attached at address %p\n", shm_address);
}
然后我进行一些进程管理,调用shmdt(shm_address),最后使用shmctl 进行清理。但我从来没有接触过那部分代码。
我得到这个作为输出:
Memory created successfully:0
shmat was unsuccessful: Permission denied
我只是不明白为什么shmat 无法附加?当我在执行后调用 ipcs 命令时,我的内存被分配了,所以我相当有信心 shmget 正在工作。谁能指出我正确的方向?谢谢。
【问题讨论】:
标签: c linux shared-memory