c - C 中的共享内存 : Shmget Issues

标签 c linux shared-memory

int shmid;
int* locat;

//create shared memory segment
shmid = shmget(6666, size, 0666);
if (shmid < 0) {
    perror("shmget");
    exit(1);
}

locat = (int *) shmat(shmid, NULL, 0);
if (locat == (int *) -1) {
    perror("shmat");
    exit(1);
}

我正在这样设置共享内存,但我一直收到此错误:shmget: No such file or directory

这段代码工作正常,不知道为什么现在会这样。

最佳答案

As the man says

IPC_CREAT

Create a new segment. If this flag is not used, then shmget() will find the segment associated with key and check to see if the user has permission to access the segment.

您必须将 IPC_CREAT 添加到您的 shmget 调用

shmid = shmget(6666, size, IPC_CREAT | 0666);

您还可以使用IPC_EXCL 来确保该段是新创建的

IPC_EXCL

This flag is used with IPC_CREAT to ensure that this call creates the segment. If the segment already exists, the call fails.

关于c - C 中的共享内存 : Shmget Issues,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40926336/

相关文章:

c++ - 全局常量定义的顺序

c# - 用Mono开发C#的可行性

linux - 如何防止安装所有 USB 大容量存储设备?

linux - 不相关进程之间的 mmap 文件

c - 有没有办法替换标准 C 中的 kbhit() 和 getch() 函数?

c - 大输入背包算法

linux - 在 zip 文件中搜索字符串的最快 grep

c++ - 在 VC++ 和 Qt 应用程序之间使用共享内存进行通信

c - 当由多个线程执行时,我应该同步 mq_timedreceive 调用吗?

c - Ruby的排序是如何实现的?