free()在子进程释放内存时导致麻烦

时间:2013-03-19 00:20:13

标签: c

#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<malloc.h>

int main(int argc, char* argv[]){
 int fd,fd1,fd2,fd3;
 int fork_id1,fork_id2,fork_id3;
 char *buffer =  NULL;
 char *buff = NULL;
 char *loc = NULL;
 int no_bytes_read = 0;
 int no_bytes_write = 0;
 int total_file_size = 0;
 int half_file_size = 0;
 int count = 0;
 int ret_val = 0;

 if(argc < 5){
  printf("Enter 1 i/p and 3 o/p files");
  return -1;
 }

 fd = open(argv[1],O_RDONLY);
 if(fd == -1){
  printf("\nRead file desc not created!\n");
  return -1;
 }

 total_file_size = lseek(fd,0,SEEK_END);
 half_file_size = (total_file_size / 2);
 lseek(fd,0,SEEK_SET);
 printf("\n Total File size is : %d \n",total_file_size);

 buffer = (char *)malloc((sizeof(char) * total_file_size));
 if(buffer == NULL){
  printf("\n Cant allocate memory for buffer \n");
  return -1;
 }

 no_bytes_read = read(fd,buffer,total_file_size);

 fork_id1 = fork();
if(fork_id1 == 0){

  fd1 = open(argv[2],O_WRONLY);
  if(fd1 == -1){
   printf("\n write file des for child1 not created!\n");
   return -1;
  }

  buff = (char *)malloc((sizeof(char) * half_file_size));
  if(buff == NULL){
   printf("\n buff not created for child1 \n");
   return -1;
  }
  loc = buff;

  for(count = 0; count <= half_file_size; count++){
   *buff++ = *buffer++;
  }
  buff = loc;

  no_bytes_write = write(fd1,loc,half_file_size);
  free(buff);
  buff = NULL;
 }
 else{
  wait();
  fork_id2 = fork();
  if(fork_id2 == 0){

   fd2 = open(argv[3],O_WRONLY);
   if(fd2 == -1){
    printf("\n write file des not created for child2 \n");
    return -1;
   }

   buff = (char *)malloc((sizeof(char) * half_file_size));
   if(buff == NULL){
    printf("\n buff not created for child1 \n");
    return -1;
   }
   loc = buff;

   for(count = half_file_size+1 ; count <= total_file_size; count++){
    *buff++ = buffer[count];
}
   buff = loc
   no_bytes_write = write(fd2,loc,half_file_size);
   free(buff);
  }
  else{
   wait();
   fork_id3 = fork();
   if(fork_id3 == 0){

    fd3 = open(argv[4],O_WRONLY);
    if(fd3 == -1){
     printf("\n write file des not created for child3 \n");
     return -1;
    }

    buff = (char *)malloc((sizeof(char)* total_file_size));
     if(buff == NULL){
      printf("\n buff not created for child1 \n");
      return -1;
     }
     loc = buff;

     for(count = 0 ; count <= total_file_size; count++){
      *buff++ = *buffer++;
     }
     buff = loc;
     no_bytes_write = write(fd3,loc,total_file_size);
     free(buff);
   }
   else{
     wait(); //TODO
     //free(buffer);
     printf("\n Parent \n");
     /* To compare the files */
     //ret_val = validate(argv[1],argv[2],argv[3],argv[4],total_file_size);
     //if(ret_val == -1){
     // return -1;
     //}
   }
  }
 }
 return 0;
}

我正在将整个文件复制到一个缓冲区中并将前半部分字节复制到文件A,将字节的后半部分复制到文件B,将完整内容复制到文件C.在每个子节点我都为临时分配内存缓冲区,当我试图释放我得到分段错误。

1 个答案:

答案 0 :(得分:3)

这就是原因:

 for(count = 0; count <= half_file_size; count++){
   *buff++ = *buffer++;
  ...
  free(buff);

你的循环正在改变你分配内存的指针。您只能在指针上使用free,其中包含malloc返回的原始值。