- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: Parent and Child issue with C programming
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-12-2005 07:42 PM
тАО07-12-2005 07:42 PM
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
Solved! Go to Solution.
- Tags:
- fork
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-12-2005 08:22 PM
тАО07-12-2005 08:22 PM
Re: Parent and Child issue with C programming
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-13-2005 12:58 AM
тАО07-13-2005 12:58 AM
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
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-13-2005 01:11 AM
тАО07-13-2005 01:11 AM
Re: Parent and Child issue with C programming
- Tags:
- daemon
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-13-2005 01:27 AM
тАО07-13-2005 01:27 AM
Re: Parent and Child issue with C programming
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
- Tags:
- zombie
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-13-2005 11:16 AM
тАО07-13-2005 11:16 AM
Re: Parent and Child issue with C programming
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);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-13-2005 08:09 PM
тАО07-13-2005 08:09 PM
Re: Parent and Child issue with C programming
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(),.. */
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-14-2005 07:27 AM
тАО07-14-2005 07:27 AM
SolutionAccording 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
- Tags:
- missing attachment
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-31-2005 08:16 PM
тАО07-31-2005 08:16 PM