Computing.Net > Forums > Unix > how to launch an independent proces

how to launch an independent proces

Reply to Message Icon

Original Message
Name: deepthi
Date: March 10, 2005 at 05:57:06 Pacific
Subject: how to launch an independent proces
OS: unix
CPU/Ram: 512MB
Comment:

Hello,

I needed a help please...

I have an application which launches process. For this i am using fork and execv method calls. By doing this my launched process are now child process of the parent process(launching process). I dont want this to happen, i want the process that is launched to be an independent process, not a child process. how do i achieve this.

As i know, daemon process are used to run a process at background. For me i just want the launched process to run independently.

Please help me with this problem, as soon as possible.

Thanks and regards,
deepthi

email : deepthi.kallahakkala@lntinfotech.com
kvdeepthi29@rediffmail.com


Report Offensive Message For Removal

Response Number 1
Name: Xavier HECQUET
Date: March 11, 2005 at 03:54:03 Pacific
Subject: how to launch an independent proces
Reply: (edit)

I hope the following may help you:

If I haven't misundertood you, you are in the following case:
ProgramA
if (fork()==0)
execv("blablabla", "bliblibli");
else
...

That is to say program A doesn't wait for the completion of his son (no waitpid). Let us suppose programA doesn't stop just after the fork, his son will indeed keep PPID=father's PID.
But if ProgramA could ever stop just after the fork, his son will then be attach to PPID=1.Ok, so, here is what I can purpose you:
ProgramA
if ((pid=fork())==0)
if (fork()==0)
execv("blablabla", "bliblibli");
else
exit(1);
else
waitpid(pid);
...
ProgramA will wait until completion of his son, which is quasi-immediate, but not for the completion of his "grand-son", which will loose his father (the son of programA), and then will attach to PPID=1. Is it all clear?

Here's an example:

#include <stdio.h>
#include <unistd.h>


main ()
{
int pid, i;

if ((pid=fork ()) == 0)
{ /* First, create on son */
printf ("\t> SON 1, pid:%d, ppid:%d\n", getpid(), getppid());
if ((pid=fork ()) == 0)
{ /* SON 1 create SON 2*/
while (1)
{
printf ("\t\t> SON 2, pid:%d, ppid:%d\n", getpid(), getppid());
sleep(1);
}
printf("\t\t> SON 2 ends up.\n");
exit(1);
}
for (i=0;i<10;i++)
{

for (i=0;i<10;i++)
{
printf ("\t> SON 1, pid:%d, ppid:%d\n", getpid(), getppid());
sleep(1);
}
printf("\t> SON 1 ends up\n");
exit(1);
}
else if (pid>0)
{
printf ("FATHER, pid:%d, fils:%d\n", getpid(), pid);
waitpid (pid);
}
else printf ("FORK ERROR\n");
printf("> Father ends up.\n");

I tried it on HPUX and LINUX (RHEL3), it's OK, SON2 is stille alive even after his father (SON1) dies. And by the way, its nex "father" his PID=1, even if his "grand-father" is ProgramA.

Regards


Xavier


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: how to launch an independent proces

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software