fork function working procedure

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • johny10151981
    Top Contributor
    • Jan 2010
    • 1059

    #1

    fork function working procedure

    Code:
    /*name of the built program is dmns*/
    int main()
    {
     int i, j;
     i=fork();
     if(i<0)
      return 1;
     printf("created pid=%d",i);
    
     while(1) {i=i;} //unlimited loop
    
    }
    This is a useless program for test only. You see in this program after successfully generated fork i didn't exit from main only for test purpose.

    after running the program on another console I check the process running.
    Code:
    #ps -ef|grep dmns
    root  20117 20065 44 23:44 pts/1 00:00:03 ./dmns
    root  20118 20117 44 23:44 pts/1 00:00:03 ./dmns
    Now what happen if I kill process:
    If I kill the process 20118 then the ps -ef result is
    Code:
    #ps -ef|grep dmns
    root  20117 20065 44 23:44 pts/1 00:00:03 ./dmns
    root  20118 20117 44 23:44 pts/1 00:00:03 [dmns] <defunct>
    But if I kill 20117 instead 20118 ps result is different.
    Code:
    #ps -ef|grep dmns
    root  20118 20117 44 23:44 pts/1 00:00:03 ./dmns
    Now My question is how the whole process is going on. What happens after we create fork? how after creating fork only main program get the exit function under condition why the sub function don't get it? I mean take a look below program:
    Code:
    /*name of the built program is dmns*/
    int main()
    {
     int i, j;
     i=fork();
     if(i<0)
      exit(1);
    
     if(i>0)
      exit(0);
     ...
    }
    Where fork start to work and how?
  • ashitpro
    Recognized Expert Contributor
    • Aug 2007
    • 542

    #2
    your basic structure is partially right.

    fork returns twice.
    it will return 0 in case of child process and new pid in case of parent process.

    your code should be:
    Code:
     /*name of the built program is dmns*/
     int main()
     {
      int i, j;
      i=fork();
     
      if(i>0)
       //parent code
      else if(i == 0)
       //Child code
      else
       //error while fork
       ...
     }
    Other important thing is, you should always query the exit status of child process using any of the wait family system call.

    If parent dies or exits without waiting for child to exit, child becomes defunct process that is zombie and the resources allocated to child process won't be taken back until next restart.

    This is what happened when you kill the parent.

    Comment

    • johny10151981
      Top Contributor
      • Jan 2010
      • 1059

      #3
      @ashitpro
      My confusion lies somewhere else.

      My Question is where does fork() start to work?

      why it dont get the line
      Code:
       if(i<1)
         exit(1);
      how come fork miss that line?

      Comment

      • ashitpro
        Recognized Expert Contributor
        • Aug 2007
        • 542

        #4
        I guess you are making me confuse.
        earlier your code in question is:
        Code:
        if(i<0)
          exit(1);
        Now you are saying its:
        Code:
        if(i<1)
          exit(1);
        I am considering first case.
        Lets consider whole code again:
        Code:
        /*name of the built program is dmns*/
        int main()
        {
         int i, j;
         i=fork();
         if(i<0)
          exit(1);
         
         if(i>0)
          exit(0);
         ...
        }
        At line 5 fork() gets executed.
        Here, it will create the new process in memory i.e. brand new address space. All code will be shared by both the processes.

        It will return some positive integer as pid (assigned by system to new process). It will get stored in i.
        Remember i will receive positive number in parent process.

        Now with respect to further code flow.
        Condition at line 6 will get fail, as i is positive integer and greater than 0.

        Condition at line will return true and exit the process.
        Here, parent process terminates.

        At very simultaneously (since fork executed) child process will be executing. It will start executing instruction from line 5. So it will again execute fork. Here fork will return 0. Conditions at line 6 and 9 both will get failed.

        You should have written a condition for i==0 and put code which will be executed by child.

        In this case as fork is executed successfully, the condition at line 6 will never come true in parent as well as child.

        Comment

        • johny10151981
          Top Contributor
          • Jan 2010
          • 1059

          #5
          Code:
          if(i>1)
          was a typing mistake.

          Your explanation is clear

          Comment

          Working...