Creating an independent child process

  • Follow


Hi,
  How is it possible to create a totally independent child
process.Normally when we fork a child process,the parent and child
have a relationship.I suspect it can be done withe the vfork() sys
call , but I havent ever used it to be sure.Its not for any particular
problem,I was asked this q in an interview.

TIA
rohit
0
Reply prohit99 4/26/2004 6:51:29 AM

On Mon, 26 Apr 2004 at 06:51 GMT, rohit spoke:

>   How is it possible to create a totally independent child
> process.Normally when we fork a child process,the parent and child
> have a relationship.I suspect it can be done withe the vfork() sys
> call , but I havent ever used it to be sure.Its not for any
> particular problem,I was asked this q in an interview.

Every process that is currently running has this relationship ... of
which the "Adam" process is 'init' with pid 0. That's the way it works
on my linux machine anyway.

-- 
The King of Pots and Pans
0
Reply The 4/26/2004 6:58:12 AM


On Sun, 25 Apr 2004 23:51:29 -0700, rohit wrote:

> Hi,
>   How is it possible to create a totally independent child
> process.Normally when we fork a child process,the parent and child
> have a relationship.I suspect it can be done withe the vfork() sys
> call , but I havent ever used it to be sure.Its not for any particular
> problem,I was asked this q in an interview.
In short:
fork
setpgrp
fork

google for daemon.c, you'll find some code you can tweak.
-- 
Nils Olav Sel�sdal
System Engineer
w w w . u t e l s y s t e m s . c o m


0
Reply iso 4/26/2004 8:38:32 AM

Nils O. Sel�sdal wrote:

> On Sun, 25 Apr 2004 23:51:29 -0700, rohit wrote:
> 
> 
>>Hi,
>>  How is it possible to create a totally independent child
>>process.Normally when we fork a child process,the parent and child
>>have a relationship.I suspect it can be done withe the vfork() sys
>>call , but I havent ever used it to be sure.Its not for any particular
>>problem,I was asked this q in an interview.
> 
> In short:
> fork
> setpgrp
> fork

To expand on the short version.  An important point is that there are 
two forks so that the eventual process is the grandchild of the 
original.  The intermediate process that does the second fork exits and 
is reaped (wait()) by the original so that the grandchild is inherited 
by process 1.  At that point the new process is no longer associated 
with the original.

-- ced

> 
> google for daemon.c, you'll find some code you can tweak.


-- 
Chuck Dillon
Senior Software Engineer
NimbleGen Systems Inc.
0
Reply Chuck 4/26/2004 1:36:16 PM

"Nils O. Sel�sdal" <NOS@Utel.no> wrote in message 
news:pan.2004.04.26.08.38.25.490472@Utel.no...

> On Sun, 25 Apr 2004 23:51:29 -0700, rohit wrote:

>> Hi,
>>   How is it possible to create a totally independent child
>> process.Normally when we fork a child process,the parent and child
>> have a relationship.I suspect it can be done withe the vfork() sys
>> call , but I havent ever used it to be sure.Its not for any particular
>> problem,I was asked this q in an interview.

> In short:
> fork
> setpgrp
> fork

    Do you need a 'setsid' in there? I'm drawing a blank right now.

    DS


0
Reply David 4/26/2004 8:16:23 PM

> In short:
> fork
> setpgrp
> fork
> 
> google for daemon.c, you'll find some code you can tweak.

I can understand that when we create a daemon process with the fork ->
stpgrp/setsid -> fork model,we have a process that dosent depend on
the controlling terminal/session and as the parent is killed its taken
over by init.Is there any other way when we can have a parent and
child which can be unrelated in the real sense of the word.

regards
rohit
0
Reply prohit99 4/27/2004 5:48:08 AM

In article <93d5c679.0404262148.369d6f63@posting.google.com>,
 prohit99@yahoo.com (rohit) wrote:

> > In short:
> > fork
> > setpgrp
> > fork
> > 
> > google for daemon.c, you'll find some code you can tweak.
> 
> I can understand that when we create a daemon process with the fork ->
> stpgrp/setsid -> fork model,we have a process that dosent depend on
> the controlling terminal/session and as the parent is killed its taken
> over by init.Is there any other way when we can have a parent and
> child which can be unrelated in the real sense of the word.

The simplest way is to have the child fork a grandchild, and then the 
child exits.  The grandchild becomes an orphan, and is inherited by 
init, so it's no longer related to the original parent.

-- 
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
0
Reply Barry 4/27/2004 10:57:49 AM

rohit wrote:
> Hi,
>   How is it possible to create a totally independent child
> process.Normally when we fork a child process,the parent and child
> have a relationship.I suspect it can be done withe the vfork() sys
> call , but I havent ever used it to be sure.Its not for any particular
> problem,I was asked this q in an interview.
> 
> TIA
> rohit

The following function will create an "totally independent" process from 
the calling process.  What actually happens is the new process becomes a 
child of init.  This function does not become a true daemon because by 
convention a daemon changes directory to root (/), preventing a 
filesystem from being held busy and disallowing dismounting.

This reflects a composit of many suggestions on creating a daemon 
process.  Calling this function from a shell command line will result 
with an immediate command line prompt, with ps showing the newly 
installed process.  However, the owner of the new process will be the 
current user.

I typically use this as the only line in my main.c module, passing the 
function I wish to run in the argiments.

Example:

extern int myMain();

int main(int argc, char **argv)
{
	Daemon(argc,argv,myMain);
}

Good luck

#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>

#ifdef PROTOTYPES
int Daemon(int argc, char **argv,int (*parent)())
#else
int Daemon(argc, argv, parent)
int		argc;
char	**argv;
int		(*parent)();
#endif
{
	int	ii,noctty,noblock,fd[3];
	long	parent_pid, child_pid;

	parent_pid = (long)getpid();

	if (0L > (child_pid=(long)fork())) /* failed */
		return(-1);
	else if (child_pid) /* parent */
	{
		if (parent) (void) parent(parent_pid,argc,argv);
		exit(0);
	}

	/* child */
	for(ii=0;3 > ii;ii++) /* detach terminal */
	{
		if (isatty(ii))
		{
			fd[ii] = -1;
			close(ii);
		}
		else
			fd[ii] = ii;
	}

	setsid(); /* become session leader */

#ifndef QNX_OS
	if (0 > (child_pid=(long)fork())) /* failed */
		return(-1);
	else if (child_pid) /* parent */
		exit(0);
#endif

	umask(000); /* allow all users access to files created */

	noctty = noblock = 0; /* reset file mode mask */

#ifdef O_NOCTTY
	noctty |= O_NOCTTY;
#endif

#ifdef O_NDELAY
	noblock |= O_NDELAY;
#endif

#ifdef O_NONBLOCK
	noblock |= O_NONBLOCK;
#endif

	/* NOTE: Should change directory to root (/) here. */

	/* open handles to nul device */
	for(ii=0;3 > ii;ii++) /* detach terminal */
	{
		if (0 <= ii) continue;
		switch(ii)
		{
			case	0:
				open("/dev/null",O_RDONLY|noctty);			/* stdin */
			break;
			case	1:
				open("/dev/null",O_APPEND|noctty);			/* stdout */
			break;
			case	2:
				open("/dev/null",O_APPEND|noblock|noctty);	/* stderr */
			break;
		}
	}

	return(0); /* daemon should be installed */
} /* int Daemon(int argc, char **argv,int (*parent)()) */

0
Reply Joseph 6/1/2004 4:59:51 PM

Alternatively, you can fork(), and then fork() the child. Then the first 
child ends, and the actual parent waits for the first child to end. That 
creates a process that will run totally independent of the original process:

orig:
   pid = fork()
   if( pid ) {
      pid2 = fork();
   } else {
      waitpid(pid);
   };

more or less (syntax probably isn't perfect.)

David Logan
0
Reply David 6/2/2004 12:21:45 AM

In article <c6jqmo$ack$1@nntp.webmaster.com>,
David Schwartz <davids@webmaster.com> wrote:
>
>"Nils O. Sel�sdal" <NOS@Utel.no> wrote in message 
>news:pan.2004.04.26.08.38.25.490472@Utel.no...
>
>> On Sun, 25 Apr 2004 23:51:29 -0700, rohit wrote:
>
>>> Hi,
>>>   How is it possible to create a totally independent child
>>> process.Normally when we fork a child process,the parent and child
>>> have a relationship.I suspect it can be done withe the vfork() sys
>>> call , but I havent ever used it to be sure.Its not for any particular
>>> problem,I was asked this q in an interview.
>
>> In short:
>> fork
>> setpgrp
>> fork
>
>    Do you need a 'setsid' in there? I'm drawing a blank right now.

setpgrp and setsid are (more or less) the same thing.  I use setsid().
0
Reply gazelle 6/13/2004 5:13:19 PM

9 Replies
334 Views

(page loaded in 0.158 seconds)

Similiar Articles:













7/27/2012 6:48:02 PM


Reply: