如何将 shmat 与非 NULL 页面对齐的 shmaddr 一起使用?
How to use shmat with non-NULL page-aligned shmaddr?
>我正在尝试使用堆创建的变量的地址创建共享内存,我确保地址与页面对齐,但仍然收到错误:"无效参数"。我怎样才能正确地做到这一点?
#include <iostream>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <cstdint>
const int PERMISSION = 0666; //-rw-rw-rw-
int main() {
int size = 10;
void* testp = std::aligned_alloc(SHMLBA, size);
int shkey = 314159, shmid;
if ((shmid = shmget(shkey, size, IPC_CREAT | PERMISSION)) == -1) {
perror("shmget: shmget failed");
exit(-1);
}
void *ret;
if ((ret = shmat(shmid, testp, 0)) == (void*)-1) {
perror("shmat: shmat failed");
exit(-1);
}
return 0;
}
C++17 标准说,如此处所述,
传递
size
不是对齐的整数倍或 实现无效或不支持的对齐方式 导致函数失败并返回空指针(C11,如 已发布,在这种情况下指定未定义的行为,这是 由DR 460更正)。
那么什么是SHMLBA
?它应该是一个整体,正如所说,是对齐的一个因素。
在使用非 NULL 参数调用shmat
时,似乎必须传递SHM_REMAP
:
shmat (shmid, testp, SHM_REMAP);
为什么会这样还不是很明显。 根据文档,此标志是特定于 Linux 的。
现场演示
相关文章:
- 如何将enable-if与模板参数和参数包一起使用
- 如何将PERF_AMPLE_READ与mmap一起使用
- 如何将两个不同矢量的同一位置的两个元素组合在一起
- 如何将C++中的库和头与MinGW一起使用
- 将--whole archive链接器选项与CMake和具有其他库依赖项的库一起使用
- 为什么我不能将 rand() 与数组的大小一起使用?
- 要与"if constexpr"一起使用的编译时消息(在预处理器之后)
- 不能将复制初始化与隐式转换的多个步骤一起使用
- 将fold表达式与std::一起用于两个元组
- spdlog标头仅与外部fmt一起使用.spdlog错误:'内部':不是'fmt'
- 将 std::allocate_shared 与多态资源分配器一起使用
- 为什么常量词在重载运算符中不与 ostream 对象一起使用<<?
- 将 OpenCV 与 CMAKE 中的项目一起构建为第三方库的正确方法
- 将 exprtk 与自定义类的对象一起使用
- 将 std::set 与基于键的比较器一起使用
- 将 C++ 类与 Rcpp 一起使用,从 C 或 R 修改它
- 如何将 Eigen::Ref 与 pybind11 一起使用?
- 如何将AERT_Allocate与 std:vector 一起使用
- 如何使我的 sizeof sum 结构与空参数包一起工作
- 如何将 shmat 与非 NULL 页面对齐的 shmaddr 一起使用?