C: fork() dynamic memory allocation -


please consider below code snippet:

struct tasks {char  taskid[4];};  struct tasks *taskarray;  int main() {  for(;;)  {     taskarray = (struct tasks *) calloc(1, sizeof(struct tasks));     printf("\ntaskarray:[%d]\n",taskarray);fflush(stdout);     strcpy(taskarray[1000].taskid,"7");     printf("main:[%d]  : [%d][%s]\n",getpid(),taskarray,taskarray[1000].taskid);fflush(stdout);      returnvalue = fork();      if (returnvalue == 0)     {         printf("child:[%d]  : [%d][%s]\n",getpid(),taskarray,taskarray[1000].taskid);fflush(stdout);         free(taskarray);         strcpy(taskarray[1000].taskid,"10");         sleep(3);         printf("child:[%d]  : [%d][%s]\n",getpid(),taskarray,taskarray[1000].taskid);fflush(stdout);         exit(1);     }      if (returnvalue != 0)     {         printf("parent:[%d]  : [%d][%s]\n",getpid(),taskarray,taskarray[1000].taskid);fflush(stdout);         sleep(6);         printf("parent:[%d]  : [%d][%s]\n",getpid(),taskarray,taskarray[1000].taskid);fflush(stdout);         taskarray = (struct tasks *) calloc(1, sizeof(struct tasks));         printf("parent:[%d]  : [%d][%s]\n",getpid(),taskarray,taskarray[1000].taskid);fflush(stdout);     }   }      exit(1); } 

output:

  • taskarray:[11489296]
  • main:[21060] : [11489296][7]
  • parent:[21060] : [11489296][7]
  • child:[21061] : [11489296][7]
  • child:[21061] : [11489296][10]
  • parent:[21060] : [11489296][7]
  • parent:[21060] : [11489328][]

please me confirm/understand points below

  • that pointer addresses virtual hence both child , parent have different values if address looks same.
  • free() in child frees memory allocated child , doesn't affect memory allocated parent.
  • in calloc have allocated 1 item trying use [1000] , still works out because memory if not allocated me still present. risky , might cause core dump in future. in child using memory after free() , still works.
  • in parent have huge memory leaks there no free(). please understand side effects of memory leak if program runs in loop ever until kills process. please advise happens when process killed, free memory?

edit:

i understand many behavior's undefined , code not logically correct still works out , executes. question trying understand why ill-formed, ill-logical code works out & why.

you should remember 'c' course fork creates new process copying virtual paging table so, child see absolutely same values parent (usually pages have read-only protection). start writing in child memory space, physical mapping of corresponding page entry updated point new location , data copied on new location.

that pointer addresses virtual hence both child , parent have different values if address looks same.

  • the virtual addresses same though physical mapping might different and, yes values different.

free() in child frees memory allocated child , doesn't affect memory allocated parent.

  • yes, not affect parent since done in child's virtual space , child's physical mapping of space. free means manipulate pointers in memory. so, contents of freed nodes can still mapped parent space (till start overwriting it).

in calloc have allocated 1 item trying use [1000] , still works out because memory if not allocated me still present. risky , might cause core dump in future. in child using memory after free() , still works.

  • don't!! reading values memory not think is, if legal, or corrupting writing. no matter if child or parent.

in parent have huge memory leaks there no free(). please understand side effects of memory leak if program runs in loop ever until kills process. please advise happens when process killed, free memory?

  • when processed gets killed means or exits memory uses gets returned os reuse. @ least in generic os aware of.

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -