Operating System - Linux
1754019 Members
7816 Online
108811 Solutions
New Discussion юеВ

Re: Parent and Child issue with C programming

 
SOLVED
Go to solution
Henry Chua
Super Advisor

Parent and Child issue with C programming

Hi Guys,

I got some queries regarding C. Say I run a program A and it fork a child process B. How can I ensure B continue to run even after A process finished, i.e. make process B independent of its parent. Possible?

Best regards
Henry
8 REPLIES 8
Biswajit Tripathy
Honored Contributor

Re: Parent and Child issue with C programming

When process A forks a process B, the process
B runs independent of the parent (ofcourse, it
might inherit some of the properties of the
parent during it's creation). You don't have to
do anything to make it "independent". If
process A dies, process B will continue to
run with "init" process (pid = 1) as the new
parent of the process B.

- Biswajit
:-)
Tom Schroll
Frequent Advisor

Re: Parent and Child issue with C programming


Henry,

Just before the fork(), you can also do the following:

signal( SIGCHLD, SIG_IGN );

(be sure to #include at the top of your program)

This will tell the parent process to ignore a stop or termination signal of a child. This will further ensure that the child is independent from the parent.

-- Tom

If it ain't broke, it needs optimized.
A. Clay Stephenson
Acclaimed Contributor

Re: Parent and Child issue with C programming

Because it sounds like you are setting up a daemon (in which it is very common to fork() and let the parent die), you should also make a call to setsid() in the child immediately after doing the fork(). This detaches the calling process from the parent and makes it a process group leader.
If it ain't broke, I can fix that.
Peter Nikitka
Honored Contributor

Re: Parent and Child issue with C programming

Hi,

I prefer this interface:

#include

sigignore(SIGCHLD);

This will prevent the OS from creating zombie processes from children, which have finished but 'think' that the parent process might want to known their exit status.

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Henry Chua
Super Advisor

Re: Parent and Child issue with C programming

Hi Guys,

Say I want to use parent A to call a process B, B will be waiting an input signal from another program, but A need to terminal straight after call B. Can I write the program as such?

#include
#include
#include
#include
#include

#define CHILD "/users/demo/demo/henry/program/chprog"
int
main()
{
pid_t pid;


if (access (CHILD, 01) == 0)
{
printf(" File Not found!!");
}
signal(SIGCHLD, SIG_IGN);
if ((pid=fork()) == 0)
execlp( CHILD,CHILD,NULL);

else if (pid > 0)
wait(0);

exit(0);

}

Peter Nikitka
Honored Contributor

Re: Parent and Child issue with C programming

Hi,

I usually use this type of pseudocode:

if (! (pid=fork))
{ /* child */
...
execXX(...);
/* code for failed exec() follows */
...
}

/* code for parent follows, e.g. wait(), alarm(),.. */
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Tom Schroll
Frequent Advisor
Solution

Re: Parent and Child issue with C programming


According to the wait(2) man page, the behavior of wait() is affected if the SIGCHLD signal is set to SIG_IGN. So why are you using wait() in your example? You will not need it if the parent reads input interactively after forking. Could you please describe what you are trying to do?


Also, here is another example of a code snip that is readable to a programmer who may be trying to maintain the code later:

signal( SIGCHLD, SIG_IGN );

/* Lets become a daemon now. */
switch( fork() )
{
case -1: /* fork() failed */
fprintf( stderr, "ERROR: fork failed (%s)\n\r", strerror(errno) );
exit( 1 );
break;

case 0: /* child process */
startChild();
break;

default: /* parent process */
break;
}
/* In parent process, do whatever else you need here. */
exit( 0 );
}

(the formatting is messed up by the forum, so please see attached file for a formatted version)

-- Tom
If it ain't broke, it needs optimized.
Henry Chua
Super Advisor

Re: Parent and Child issue with C programming

Thanks Tom.. your advise works great!!