1

您好我正在尝试使用函数添加共享内存空间,我这样做是这样的:

void CreateSpace(int size, std::string name){

int aux;
int aux2;


aux = shm_open(name.c_str(),  O_RDONLY | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);

if(aux > 0){
    aux2 = ftruncate(aux, sizeof(int)*2);
    if( aux2 != -1){

        works

    }else{

        It does not work
    }
}else{
Shared Memory Already Created }

问题是我总是得到不起作用的部分,我手动删除了 dev/shm 中的共享空间,但是当我尝试创建一个不存在的新空间并尝试设置特定大小时,我总是得到ftruncate = -1。我该如何解决这个问题?我尝试使用 sizeof(int)*2 的 size insted 但我得到了相同的结果。

当我尝试使用它时,只需将其称为:

CreateSpace(5, "/test1");

它确实创建了空间/test1,但是当我尝试使用 ftruncate(int aux, off_set n) 设置特定大小并始终得到 errno 22 时出现错误。

4

0 回答 0