gpt4 book ai didi

c - shmat() 权限被拒绝,即使我有读取权限

转载 作者:太空狗 更新时间:2023-10-29 11:49:34 27 4
gpt4 key购买 nike

在我的简单代码中:

#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:权限被拒绝”。

我有写的权限吗?

最佳答案

来自 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

关于c - shmat() 权限被拒绝,即使我有读取权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45075096/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com