1

我在两个进程之间创建共享内存时遇到问题。我收到此错误,我不知道该怎么做,因为我认为 a 包含所有库。

日志...

 g++ exc8.c -o exc8
 exc8.c: In function ‘int main(int, char**)’:
 exc8.c:29:37: error: ‘ltrunc’ was not declared in this scope
 size = ltrunc(fd, B_SIZE, SEEK_SET);

代码...

#include <stdlib.h> 
#include <sys/mman.h> 
#include <fcntl.h> 
#include <sys/types.h> 
#include <sys/stat.h> 
#include <unistd.h> 
#include <sys/wait.h> 
#include <errno.h>
#include <stdio.h>

#define B_SIZE 4 

char* memory = new char[4];
int main(int argc, char *argv[]) {
    int size, fd; 
    char *buf; 
    char memory[4];
    fd = shm_open(memory, O_RDWR|O_CREAT, 0774);
    if (fd < -0) { 
        perror("open"); 
        exit(-1); 
    } 
    size = ltrunc(fd, B_SIZE, SEEK_SET);
    if(size < 0) {
        perror("trunc"); 
        exit(-1); 
    } 
    buf = (char *)mmap(0, B_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); 
    if(buf == NULL) {
        perror("map"); 
        exit(-1); 
    } 
}
4

2 回答 2

1

我不知道ltrunc()从哪里来,但你可以设置共享内存对象的大小ftruncate()

if (ftruncate(fd, B_SIZE) == -1) {
    // Handle error
}

(来自http://pubs.opengroup.org/onlinepubs/009695299/functions/shm_open.html)。

于 2013-11-09T15:36:05.890 回答
1

ltrunc不是标准功能。它似乎在QNX平台中使用 qcc 作为编译器定义,它在给定位置截断文件。可能 POSIX 为这项工作提供了truncate()ftruncate()功能。

于 2013-11-09T15:37:36.760 回答