0
#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。在每个孩子中,我为临时缓冲区分配内存,当我试图释放我得到一个分段错误。

4

1 回答 1

3

这是原因:

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

您的循环正在更改您分配内存的指针。您只能free在具有由 . 返回的原始值的指针上使用malloc

于 2013-03-19T00:25:19.440 回答