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 に答える