![](/img/trans.png)
[英]How to give starting address to attach to that in shmat() shared memory segment?
[英]I am receiving “shmat: permission denied” when attempting to attach to shared memory. Why?
我在共享內存方面遇到了一些麻煩,如果有人可以向我指出正確的方向,可以使用一些指導。
// 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
在工作。 有人能指出我正確的方向嗎? 謝謝。
優先錯誤:
if (shmid = shmget(key, sizeof(int), IPC_CREAT | 0777) < 0)
這會將shmget(key, sizeof(int), IPC_CREAT | 0777) < 0
(即0或1)分配給shmid
。 你要
if ((shmid = shmget(key, sizeof(int), IPC_CREAT | 0777)) < 0)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.