Home > C/C++ > child process and setsid()

child process and setsid()

I was trying to run a new process within the child process after the fork was successful and when ever I tried to run the process using execlp, as soon as the parent exited the child also did quit as well. This was getting very annoying and so I started googling the problem out and “setsid()” function was my friend in this case. With my final code looking like this:

pid_t pid;
pid = fork ();

if (pid == 0)
{
/* Child process. */
setsid();
execlp (“/bin/sh”, “sh”, “-c”, program, NULL);
exit (1);
}

-Shoaib Mir
shoaibmir[@]gmail.com

Categories: C/C++ Tags: , ,
  1. Saira Andleeb
    January 9, 2009 at 7:01 pm | #1

    :O … i never expected such blogging from you! :)

  2. January 10, 2009 at 2:42 am | #2

    its just in the spirit of being a opensource developer, so trying to put my experience back so that it might help someone :)

  3. d-machine
    January 20, 2009 at 1:54 am | #3

    O ae kee ho rehya ae? :D

  4. January 20, 2009 at 2:26 am | #4

    bus yar back to some old stuff that I used to do :P

  5. Farhan
    February 9, 2009 at 11:54 am | #5

    You can have the parent wait for the child to exit as well using some wait function… this is what we did in uni ;)

    i remember it *a little* :$ …

  6. February 9, 2009 at 9:39 pm | #6

    I am talking about something else here :)

    Basically it was the child process taking the same process id as that of parent which I didnt want it to take as I didnt want the child process to exit even if the parent has died.

  1. No trackbacks yet.