代码如下:
fd = shm_open("Name", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
if (fd < 0) {
return -1;
}
rv = ftruncate(fd, 10 * 1024 * 1024);
if (rv != 0) {
return -1;
}
addr = mmap(NULL, 10 * 1024 * 1024, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (addr == MAP_FAILED) {
return -1;
}
/* Access the shared memory here and the program crashes */
请注意,/ dev / shm的大小为30MB,并且在运行程序之前已满。而不是崩溃,我想抓住空间错误并优雅地失败。但是,在这种情况下,mmap()不会返回错误。我有办法检测出空间错误吗?