- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
在我的简单代码中:
#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/
因此在这段代码中 puts 无法显示输出。 如果我删除 fgets 行,它会打印 lola 但如果我尝试在 shm 上读写,则不会发生任何事情。我该如何解决这个问题? #include #inclu
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我有两个进程,每个进程都可以从 shmget() 获取相同的 shmid。 在这两个过程中,shmat()给出了不同的内存地址。 每个进程的地址总是相同的: Process1: 0x41b31000
当我浏览 shmat 的手册页时。它被描述为 API 的原始功能是将与 shmid 关联的内存段附加到调用进程的地址空间。 我的问题如下。 attach 这个词对我来说很普通。我发现很难理解附加所指的
我在运行代码时遇到问题。我的 shmat 失败并打印权限被拒绝。我在谷歌上搜索了如何解决它,但我不能。我的代码如下: #include #include #include #include #
我正在绞尽脑汁地想弄清楚为什么这在我的 Minix 系统上不起作用。 当我尝试编译 C 程序时,出现以下错误: #make link pm/pm program.o: In funct
当我到达routerInfo->areaID行时,我在该部分遇到了段错误:11。看来我没有成功分配内存。我只是不知道为什么。谁能解决这个问题吗? 我的标题是这样的: #include #includ
根据要求包含所有 header 。下面的这段代码可以工作,但问题在于 shmat(seg_id,NULL,0)。第二个参数中的 NULL 意味着操作系统将处理该位置代表用户。但是我们可以给出我们自己的
当尝试执行 shmat 时出现分配错误,告诉我无法访问内存,这在我之前没有发生过,我真的不知道该怎么做。这是错误: 0xffffffffffffffff error: Cannot access me
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
这个问题在这里已经有了答案: shmat() is returning a different "shmaddr" for same "shmkey" (3 个答案) 关闭 8 年前。 在共享内存中
使用mmap(),进程必须从父进程继承映射才能共享内存。有没有办法在不共享父进程的进程之间共享内存? shmat() 似乎是最好的解决方案,但如果进程在退出/死机时没有分离内存,则需要进行清理。 域套
我通过虚拟地址使用 shmat()。 第一个进程调用一个 shmat,shmaddr 为空,当它获得数据 block 的虚拟地址时,它存储在另一个共享内存位置。 第二个进程使用第一个进程存储在共享内存
我试图写入共享内存,但出于某种原因,在我调用 shmat() 和 strcpy 后出现段错误(核心已转储),这是为什么? 这是我的代码: int fd,shmid; key_t shmkey; cha
//TeamSize is an integer int Seg_id = shmget(SHM_KEY,sizeof(Word)*TeamSize,IPC_CREAT); void* Seg_p
shmat(int id , void * addr,int flg) 的第二个参数被推荐/约定为 NULL。 但是如果我想给它一个特定的地址(void* addr),该地址应该来自堆栈还是堆? 我的
我的程序有 2 个线程,我使用共享内存在两者之间进行通信。 每个线程都有一个“init”方法。在 init 方法中,我调用“shmget”和“shmat”来获取共享内存段并附加到局部变量。部分代码如下
在我的简单代码中: #include #include int main() { key_t key = ftok(".", 'b'); int shmid = shmget(ke
在生产者流程中,我有以下内容。 说 MEMKEY 为 key_t 234500,并且 *shared_ring 是指向正在共享的数据结构 (donut_ring) 的指针。 int shmid, se
我需要一些有关此代码的帮助。 我在共享段中创建了一个struct msg_t,并在另一个段中创建了一个简单的字符串。在此方法中,首先附加两个段,然后要初始化字符串。然后我尝试将指向该字符串的指针(在第
我是一名优秀的程序员,十分优秀!