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 ...
- 05-12-2005 #1Just 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; }
- 05-12-2005 #2Just 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.
- 05-14-2005 #3Linux Engineer
- Join Date
- Feb 2005
- Posts
- 1,043
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).
- 05-14-2005 #4Linux Engineer
- Join Date
- Feb 2005
- Posts
- 1,043
All orphaned processes are inherited by the init process regardless of how their parent terminates.
Originally Posted by Morgoth
- 05-15-2005 #5Linux 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
- 05-15-2005 #6Linux User
- Join Date
- Oct 2004
- Location
- /dev/random
- Posts
- 404
No.
Originally Posted by mohit dhawan
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


Reply With Quote
