gpt4 book ai didi

C linux shmget 参数无效

转载 作者:太空宇宙 更新时间:2023-11-04 12:29:17 26 4
gpt4 key购买 nike

int shmCreate(int id, int size)
{
int shmid
key_t key;

if ((key = ftok(".", id)) == -1) {
perror("ftok");
exit(1);
}

if ((shmid = shmget(key, size, IPC_CREAT | 0666)) == -1) {
perror("shmget");
exit(1);
}

return shmid;
}

我正在尝试编写使用 shmget 的函数,其中函数获取大小作为参数。除了 size = 1 之外,我一直收到无效参数错误。当我传递 1 以外的大小时出现错误,但是当我检查 ipcs -m 时创建了内存段,并且它的大小为 1。如何我摆脱了这个错误?

最佳答案

根据 shmget(2) 的手册页:

EINVAL A segment for the given key exists, but size is greater than the size of that segment.

您不能附加一个已经存在的更大尺寸的片段。因此,您需要先将其删除或使用其他 key 。

关于C linux shmget 参数无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43743882/

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