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
这段代码工作正常,不知道为什么现在会这样。
最佳答案
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/