如何在c ++中正确使用ftruncate(int aux,off_set n)

时间:2018-06-07 01:24:49

标签: posix shared-memory ubuntu-18.04

你好我试着用一个函数添加共享内存空间,我这样做:

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的大小,但我得到了相同的结果。

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

CreateSpace(5, "/test1");

它确实创建了space / test1,但是当我尝试使用ftruncate(int aux,off_set n)设置特定的Size并且总是得到errno 22时,我得到错误。

0 个答案:

没有答案