Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C mmap从循环中的文件_C_Linux_Overflow_Mmap - Fatal编程技术网

C mmap从循环中的文件

C mmap从循环中的文件,c,linux,overflow,mmap,C,Linux,Overflow,Mmap,我正在尝试使用mmap从循环中的文件读取, Im文件包含关于3个部分的信息,第一部分是3*sizeof(双)的大小, 第二个尺寸也为3*sizeof(双精度),第三个尺寸为sizeof(双精度)。在文件的第一部分,我有一个大小为32768字节的头文件。文件组织如下: 标题| | | | | | | | | | | | | | | | | | | | | | | | | | || 每个部分我有100次。 我想每次工作30个部分(每个部分10个部分) 我尝试过以下代码: void readingFi

我正在尝试使用mmap从循环中的文件读取, Im文件包含关于3个部分的信息,第一部分是3*sizeof(双)的大小, 第二个尺寸也为3*sizeof(双精度),第三个尺寸为sizeof(双精度)。在文件的第一部分,我有一个大小为32768字节的头文件。文件组织如下:

标题| | | | | | | | | | | | | | | | | | | | | | | | | | ||

每个部分我有100次。 我想每次工作30个部分(每个部分10个部分)

我尝试过以下代码:

void readingFile(FILE *file, double *a, double *b, double *c, int start, int end, int chunksz, long total)
{
    int i = 0;
    int size = end - start + 1;
    int fd;
    fd = fileno(file);
    off_t fullsize = lseek(fd,SEEK_CUR,SEEK_END); //getting the file size
    fullsize-=1;//the lseek gives one more byte, its ok!
    unsigned long summ = (unsigned long)(start-1)*chunksz; //chunk is 56
    summ+=(unsigned long)HEADER_SIZE;//offset the header size
    unsigned long paramm=(unsigned long)((unsigned long)summ/(unsigned long)(sysconf(_SC_PAGE_SIZE)));
    unsigned long param = floor(paramm);
    void *buf=NULL;
    buf =mmap(NULL,fullsize , PROT_READ, MAP_PRIVATE , fd, param*sysconf(_SC_PAGE_SIZE));
    if(buf==MAP_FAILED)
    {
        printf("we have an error\n");
    }
    unsigned long gapp = (sysconf(_SC_PAGE_SIZE))*param;
    unsigned long gap =summ-gapp;
    buf+=gap;
    memcpy(a,buf,3*sizeof(double)*size);
    buf+=(unsigned long)((long)total-(start-1))*3*sizeof(double);
    buf+=((start-1)*3*sizeof(double));
    memcpy(b,buf,3*sizeof(double)*size);
    buf+=(unsigned long)((long)total-(start-1))*3*sizeof(double);
    buf+=((start-1)*sizeof(double));
    memcpy(c,buf,sizeof(double)*size);
    munmap(buf, fullsize);
    return;
}

在某种程度上,我有溢出和程序崩溃! 每次调用函数时,都会为A、b、c正确分配一个新内存。 这是什么? 流程在第14行迭代时崩溃:

memcpy(c,buf,sizeof(double)*size);

谢谢

我知道用源代码回答问题并不熟悉。但我试图解释mmap是多么有用。 基本上,mmap使用内核功能将文件内容加载(并从中写回)到内存区域。因此,我们不需要频繁调用read/seek来提高应用程序的效率。 另一方面,直接访问数据是一个舒适的解决方案,只需查看代码:

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

struct mapping
{
        void* start_addr;
        size_t length;
        int fd;
};

struct mapping* map_file(const char* file)
{
        struct mapping* ret = malloc(sizeof(struct mapping));
        if(NULL == ret)
        {
                printf("Can't allocate memory for struct mapping.\n");
                return NULL;
        }

        ret->fd = open(file, O_RDONLY);
        if(0 > ret->fd)
        {
                perror("can't open specified file.");
                free(ret);
                return NULL;
        }

        struct stat fs;
        if(0 != fstat(ret->fd, &fs))
        {
                perror("can't specify file size.");
                close(ret->fd);
                free(ret);
                return NULL;
        }

        ret->length = fs.st_size;

        //offset means offset in file
        ret->start_addr = mmap(NULL, ret->length, PROT_READ, MAP_PRIVATE, ret->fd, 0);
        if(MAP_FAILED == ret->start_addr)
        {
                perror("Mapping file failed.");
                close(ret->fd);
                free(ret);
                return NULL;
        }

        return ret;
}

//returns zero on success and free the `struct mapping` data
int unmap_file(struct mapping* mmf)
{
        //note that now we use read only mapping
        //if you want to write this memory pages
        //before detach maybe you have to call:
        //msync(mmf->start_addr, mmf->length, MS_SYNC);
        // avoid data loss (write all dirty page into file).

        if(NULL != mmf->start_addr)
        {
                if(0 != munmap(mmf->start_addr, mmf->length))
                {
                        perror("Can't munmap file.");
                        return 1;
                }
        }

        mmf->start_addr = NULL;
        if(-1 != mmf->fd)
        {
                if(0 != close(mmf->fd))
                {
                        perror("can't close file descriptor.");
                        return 2;
                }
        }

        free(mmf);

        return 0;
}

// for test#define  MAGIC_START_INDEX 0
#define  MAGIC_START_INDEX 32768

int main(int arg_length, char** args)
{
        if(arg_length < 2)
        {
                printf("No input file specified.\n");
                exit(1);
        }

        int i = 0;
        //first argument is the name of program
        while(++i < arg_length)
        {
                struct mapping* mmf = map_file(args[i]);
                if(NULL == mmf)
                {
                        printf("can't use %s for input file\n", args[i]);
                        continue;
                }

                if(mmf->length >  MAGIC_START_INDEX)
                {
                        //upper base
                        int max_index = (mmf->length - MAGIC_START_INDEX) / sizeof(double);

                        //an offset alias for start memory address
                        double* data = ((double*)(mmf->start_addr + MAGIC_START_INDEX));

                        int ni = 0;
                        while(ni+2 < max_index)
                        {
                                printf("num0: %f, num1: %f, num2: %f\n", data[ni], data[ni+1], data[ni+2]);
                                ni += 3;
                        }
                }
                else
                {
                        printf("File: %s has no valuable data.", args[i]);
                }

                unmap_file(mmf);
        }
}

您可以“即时”访问数据。

“在我溢出的地方”。使用调试器准确地找出“某处”。如果要获取文件描述符,请获取文件大小,而不是
lseek()
。如果
文件*
数据希望文件偏移量位于其原来的位置,则在已使用
文件*
打开的文件上使用
lseek()
可能会损坏文件。您还映射了相当于文件完整大小的字节数,但偏移量不是零。如果对
mmap()
的调用失败,您必须执行一些操作,而不是打印错误,然后继续运行,就像什么也没发生一样。文件已打开,错误不是由于mmap调用引起的。为什么不能使用调试器?无论如何,还有其他的调试方法。如何
printf
?删除所有代码,然后每次逐渐添加一点代码,怎么样?您已经设法将大量的复杂性打包到相对较少的代码行中。添加验证和调试代码(尤其是验证所有偏移量计算)对您有好处。调用
munmap()
时,对指针
buf
的任何/所有更改都将是一个主要问题。强烈建议创建一个新的字符指针,并使用该新指针执行所有计算。调用
munmap()
时,
buf
中的指针仍然正确。因为文件的布局是已知的。强烈建议创建一个覆盖文件内容的
struct
,并通过该
struct
double[] data = (double*)(mmap(NULL, file_length, PROT_READ, MAP_PRIVATE, fd, MAGIC_START_INDEX)); //TODO check null.