Welcome to Linux Forums! With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.
Find the answer to your Linux question:
New to Linux Forums? Register here for free!
    Linux Forums > GNU Linux Zone > Linux Programming & Scripting > parent and child process

Forgot Password?
 Linux Programming & Scripting   C, Perl, PHP, Bash Scripts, anything programming or script related post in here!

Site Navigation
Linux Articles
Linux Forums
Linux Downloads
Linux Hosting
Free Magazines
Job Board
IRC Chat
RSS Feeds


Linux Forum Topics
Linux Forums
Your Distro
Linux Resources
GNU Linux Zone
The Community
Reply
 
Thread Tools Display Modes
Old 05-12-2005   #1 (permalink)
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()
{
 int a=10,b=10;
 
 
 
 if(fork()) 
 {
  /* Parent process */
  
        printf("\n\n");
  system("ps");
  
  
  printf("\n Values of a and b in the parent process : %d, %d",a,b);


  printf("\n Process id : %d\n\n",getpid()); 
  printf("\n\n\n");
                  kill(getpid(),SIGKILL);

  
 }
 else
 {
  /* The Child Process */
  sleep(10);
  printf("\n\n");
  system("ps");

  a++;
  b+=5;
  printf("\n");
  printf("\n Process id of Child %d ",getpid());
  
  printf("\n Process id of Parent %d ", getppid()); 

  
  printf("\n\n Values of a and b in child process : %d,%d",a,b);
  


 }

 return 0;
}
orthanc is offline  


Reply With Quote
Old 05-12-2005   #2 (permalink)
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.
Morgoth is offline   Reply With Quote
Old 05-14-2005   #3 (permalink)
scm
Linux Engineer
 
Join Date: Feb 2005
Posts: 1,004
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).
scm is offline   Reply With Quote
Old 05-14-2005   #4 (permalink)
scm
Linux Engineer
 
Join Date: Feb 2005
Posts: 1,004
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.
scm is offline   Reply With Quote
Old 05-15-2005   #5 (permalink)
Linux Newbie
 
Join Date: Mar 2005
Location: Bangalore, INDIA
Posts: 123
Send a message via MSN to mohit dhawan Send a message via Yahoo to mohit dhawan
i think the child dies only if it shares the same process space as the parent
__________________
Portability is for people who cannot programme
mohit dhawan is offline   Reply With Quote
Old 05-15-2005   #6 (permalink)
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
the_unforgiven is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Free Magazines
Run Your Own Web Server Using Linux & Apache - Free 191 Page Preview
Learn about everything you'll need to build and maintain your Linux servers, and to deploy Web applications to them.
subscribe
Open Source Security Myths Dispelled
Dispel the five major myths surrounding Open Source Security and gain the tools necessary to make a truly informed decision for your IT organization
subscribe
InformationWeek
InformationWeek is the only newsweekly you'll need to stay on top of the latest developments in information technology.
subscribe



All times are GMT. The time now is 11:27 AM.






© 2000 - 2009 - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.3.0 RC2