free()在子进程释放内存时引发问题 #包括 #包括 #包括 #包括 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; 整数计数=0; int ret_val=0; 如果(argc

free()在子进程释放内存时引发问题 #包括 #包括 #包括 #包括 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; 整数计数=0; int ret_val=0; 如果(argc,c,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 =

这就是原因:

#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;
}
for(count=0;count这就是原因:

#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;
}

for(count=0;count seg故障意味着您以某种方式滥用了内存。代码中至少有一个错误。很可能,您正在覆盖分配内存的末尾,或者可能在分配内存开始之前写入。您是否有
valgrind
?如果有,请使用它;这样可以轻松跟踪问题(如果您使用
-g
选项编译)。请注意,您的第二个孩子只使用
O_WRONLY
而不使用
O_APPEND
,因此它会截断文件的前半部分,然后写入其新数据。您的复制操作也不需要。您可以使用:
写入(fd2,缓冲区,文件大小的一半)
写入(fd3,缓冲区+半文件大小,总文件大小-半文件大小)
@Jonathan:是的。我不应该使用那个额外的缓冲区。seg故障意味着你不知何故滥用了内存。你的代码中至少有一个bug。很可能,你正在覆盖超出分配内存末尾的内容,或者可能在分配内存开始之前写入。你有
valgrind
?如果有,就使用它;它是使跟踪问题变得容易(如果您使用
-g
选项编译)。请注意,您的第二个孩子只使用
O_WRONLY
而不使用
O_APPEND
,因此它会截断文件的前半部分,然后写入其新数据。您的复制操作也不需要。您可以使用:
write(fd2,缓冲区,半个文件大小)
写入(fd3,缓冲区+半个文件大小,总文件大小-半个文件大小)
@Jonathan:是的。我不应该使用那个额外的缓冲区。你的答案在这里是最好的+1谢谢。我设置buff=loc(缓冲区的开始).并释放了buff.它起作用了fine@Angus-您不需要使用与
malloc
相同的指针,只需使用具有相同值的指针,因此
free(loc)
也可以完成此工作。您的答案在这里是最好的+1谢谢。我将buff=loc(设置为缓冲区的开头).并释放了buff.它起作用了fine@Angus-您不需要使用与
malloc
相同的指针,只需使用具有相同值的指针,因此
free(loc)
也可以完成此工作。