Find the answer to your Linux question:
Results 1 to 6 of 6
okay i have written the follwing c program. i have one query. if the parent process is killed shouldn't the child process also be killed. because when i execute the ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    May 2005
    Posts
    3

    parent and child process


    okay i have written the follwing c program. i have one query. if the parent process is killed shouldn't the child process also be killed. because when i execute the program, after the parent gets killed, the child process is executed after a delay of 10 sec (as given by sleep)


    Code:
    #include<stdio.h>
    #include<signal.h>
    
    int main&#40;&#41;
    &#123;
     int a=10,b=10;
     
     
     
     if&#40;fork&#40;&#41;&#41; 
     &#123;
      /* Parent process */
      
            printf&#40;"\n\n"&#41;;
      system&#40;"ps"&#41;;
      
      
      printf&#40;"\n Values of a and b in the parent process &#58; %d, %d",a,b&#41;;
    
    
      printf&#40;"\n Process id &#58; %d\n\n",getpid&#40;&#41;&#41;; 
      printf&#40;"\n\n\n"&#41;;
                      kill&#40;getpid&#40;&#41;,SIGKILL&#41;;
    
      
     &#125;
     else
     &#123;
      /* The Child Process */
      sleep&#40;10&#41;;
      printf&#40;"\n\n"&#41;;
      system&#40;"ps"&#41;;
    
      a++;
      b+=5;
      printf&#40;"\n"&#41;;
      printf&#40;"\n Process id of Child %d ",getpid&#40;&#41;&#41;;
      
      printf&#40;"\n Process id of Parent %d ", getppid&#40;&#41;&#41;; 
    
      
      printf&#40;"\n\n Values of a and b in child process &#58; %d,%d",a,b&#41;;
      
    
    
     &#125;
    
     return 0;
    &#125;

  2. #2
    Just Joined!
    Join Date
    Feb 2005
    Location
    Delft, Holland
    Posts
    95
    Have you looked up what a process does when it recieves a SIGKILL?
    If the process calls exit(0) upon recieving SIGKILL, then the child processes are automatically inherited by process 1.

  3. #3
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Would you want to die when your parent dies?

    You could arrange for the parent to catch signals (except 9, obviously) and terminate its child (you'd have to save the return value from the fork(), of course).

  4. #4
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Quote Originally Posted by Morgoth
    Have you looked up what a process does when it recieves a SIGKILL?
    If the process calls exit(0) upon recieving SIGKILL, then the child processes are automatically inherited by process 1.
    All orphaned processes are inherited by the init process regardless of how their parent terminates.

  5. #5
    Linux Newbie
    Join Date
    Mar 2005
    Location
    Bangalore, INDIA
    Posts
    122
    i think the child dies only if it shares the same process space as the parent
    Portability is for people who cannot programme

  6. #6
    Linux User
    Join Date
    Oct 2004
    Location
    /dev/random
    Posts
    404
    Quote Originally Posted by mohit dhawan
    i think the child dies only if it shares the same process space as the parent
    No.
    Linux implements fork() as "copy-on-write" - most modern Unices do it the same way..
    So, as long as you don't do exec() after fork(), both the processes still share the proc space.
    Even then, when the parent terminates (without explicitly killing the child), the child is inherited by init (pid 1) - as was told by scm.

    You can find the detailed discussion of this topic in W. Richard Stevens' "Advanced Programming in the Unix environment" and Maurice J. Bach's "The design of the UNIX operating system".
    The Unforgiven
    Registered Linux User #358564

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •