繁体   English   中英

mmap和memcpy用法以及分段错误(核心转储)错误

[英]mmap and memcpy usage and Segmentation Fault (core dumped) error

我有两个文件,我想使用memcpy将一个文件内容复制到另一个文件中。 但是我得到这个错误Segmentation Fault (core dumped) 我的主要

int main( int argc, char * argv[] ){
    int d1;
    int d2;
    char *a;
    char *b;
    d1 = da_open_r(argv[1]); // open file READ ONLY
    d2 = da_open_w(argv[2]); //  open file to WRITE
    a = (char*)da_mmap(d1); // map first file
    b = (char*)da_mmap(d2); // map second file
    memcpy(b, a, 10); // I think this line is bad
    kp_test_munamp(a, 10 ); // 
    kp_test_munamp(b, 10 );
    kp_test_close(d1); // close 1 file
    kp_test_close(d2); // close 2 file
    return 0;
}

这是我的da_mmapkp_test_munamp

void *da_mmap(int d){
    mmap(NULL, 10, PROT_READ|PROT_WRITE, MAP_SHARED, d, 0);
}

int kp_test_munamp( void *a, int size ){
   int rv;
   rv = munmap( a, size );
   if( rv != 0 ){
      puts( "munmap failed" );
      abort();
   }
   return 1;
}

我一直在尝试将其修复近两个小时,但我仍然不知道出了什么问题。 编辑我的完整代码

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

int da_open_r(const char *name);
int da_open_w(const char *name);
void *da_mmap(int d);
int kp_test_munamp( void *a, int size );
int kp_test_close(int fd);

int da_open_r(const char *name){
    int dskr;
    dskr = open( name, O_RDWR );
    if( dskr == -1 ){
      perror( name );
      exit( 255 );
    }
    printf( "dskr1 = %d\n", dskr );
    return dskr;
}

int da_open_w(const char *name){
    int dskr;
    dskr = open( name, O_RDWR );
   if( dskr == -1 ){
      perror( name );
      exit( 255 );
   }
   printf( "dskr2 = %d\n", dskr );
   return dskr;
}

void *da_mmap(int d){
     void *a = NULL;
     a = mmap(NULL, 10, PROT_WRITE, MAP_SHARED, d, 0);
     if( a == MAP_FAILED ){
          perror( "mmap failed" );
          abort();
     }
     return a;
}

int kp_test_munamp( void *a, int size ){
   int rv;
   rv = munmap( a, size );
   if( rv == -1 ){
      puts( "munmap failed" );
      abort();
   }
   return 1;
}

int kp_test_close(int fd){
   int rv;
   rv = close( fd );
   if( rv != 0 ) perror ( "close() failed" );
   else puts( "closed" );
   return rv;
}

int main( int argc, char * argv[] ){
    int d1;
    int d2;
    char *a;
    char *b;
    d1 = da_open_r(argv[1]); // read only
    d2 = da_open_w(argv[2]); //  WRITE
    a = (char*)da_mmap(d1);
    b = (char*)da_mmap(d2);
    memcpy(b, a, 10); // I think this line is bad
    kp_test_munamp(a, 10 );
    kp_test_munamp(b, 10 );
    kp_test_close(d1);
    kp_test_close(d2);
    return 0;
}

da_mmap()不返回任何内容! 这导致ab的值成为垃圾,并且很可能指向无效的内存,这继而使memcpy()失败。

添加退货声明

void * da_mmap(int d) {
  return mmap(NULL, 10, PROT_READ|PROT_WRITE, MAP_SHARED, d, 0);
}

另外,您还应该执行以下操作来测试映射的结果:

{
  void * pvtmp = da_mmap(d1); // map first file
  if (MAP_FAILED == pvtmp)
  {
    perror("da_mmap() failed");
    exit(1);
  }

  a = pvtmp;
}    

b


munmap()周围引用包装器。 在此处更正错误测试。 但是,按照惯例,返回0表示成功(返回-1失败)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM