shmat() 权限被拒绝,即使我有读取权限
shmat() Permission denied even i have read access
在我的简单代码中:
#include <sys/shm.h>
#include <stdio.h>
int main() {
key_t key = ftok(".", 'b');
int shmid = shmget(key, 4, IPC_CREAT | 0444);
if (shmid == -1) {
perror("shmget");
return 1;
}
void* addr = shmat(shmid, NULL, 0);
if (addr == (void*) -1) {
perror("shmat");
return 1;
}
printf("success");
return 0;
}
我已经有读取权限,但我得到了 "shmat: Permission denied"。
我有写的权限吗?
来自man shmat
:
If SHM_RDONLY
is specified in shmflg, the segment is attached for reading and the process must have read permission for the segment. Otherwise the segment is attached for read and write and the process must have read and write permission for the segment.
所以调用shmat()
时需要使用SHM_RDONLY
而不是0
。
在我的简单代码中:
#include <sys/shm.h>
#include <stdio.h>
int main() {
key_t key = ftok(".", 'b');
int shmid = shmget(key, 4, IPC_CREAT | 0444);
if (shmid == -1) {
perror("shmget");
return 1;
}
void* addr = shmat(shmid, NULL, 0);
if (addr == (void*) -1) {
perror("shmat");
return 1;
}
printf("success");
return 0;
}
我已经有读取权限,但我得到了 "shmat: Permission denied"。
我有写的权限吗?
来自man shmat
:
If
SHM_RDONLY
is specified in shmflg, the segment is attached for reading and the process must have read permission for the segment. Otherwise the segment is attached for read and write and the process must have read and write permission for the segment.
所以调用shmat()
时需要使用SHM_RDONLY
而不是0
。