我正尝试使用我在网上找到的示例和文档创建共享内存区域。我的目标是IPC,所以我可以让不同的流程相互交流。如何正确使用shm_open和mmap
这是我的C文件
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <errno.h>
int main (int argc, char *argv[])
{
struct stat sb;
off_t len;
char *p;
int fd;
fd = shm_open("test", O_RDWR | O_CREAT); //,S_IRUSR | S_IWUSR);
if (fd == -1) {
perror("open");
return 1;
}
if (fstat(fd, &sb)==-1){
perror("fstat");
return 1;
}
/*if (!S_ISREG(sb.st_mode)){
fprintf(stderr, "%s is not a file\n",fileName);
return 1;
}*/
p = mmap(0, sb.st_size, PROT_WRITE, MAP_SHARED, fd, 0);
if (p == MAP_FAILED){
perror("mmap");
return 1;
}
if (close(fd)==-1) {
perror("close");
return 1;
}
for (len = 0; len < sb.st_size; len++) {
putchar(p[len]);
}
if (munmap(p, sb.st_size) == -1) {
perror("munmao");
return 1;
}
fprintf(stderr,"\n");
return 0;
}
的问题是,我得到一个MMAP:无效的参数。我认为fd有些问题,但不知道如何解决它,任何帮助将不胜感激。我使用最新的XCODE在优胜美地。
恐怕你的代码不起作用,它给了我“ftruncate:无效的参数” – Kilon
好吧,它在Linux上工作正常,它似乎有关于shm_open/ftruncate的一些奇怪的行为,请参阅http://stackoverflow.com/questions/25502229/ftruncate-not-working-on-posix-shared-memory-in-mac-os-x(即,你已经运行了你的程序几次了,所以你需要首先删除该段。 shm_open调用需要一个你注释掉的参数,hm_open(“test”,O_RDWR | O_CREAT,S_IRUSR | S_IWUSR);); – nos
谢谢你的帮助。为了测试的目的,我已经将这些部分评论过了,我甚至试过将它们包括在内。我想我会坚持使用open并禁止使用shm_open,因为我决定将我的共享内存作为msync的备份存储到文件中。 – Kilon