Find the answer to your Linux question:
Results 1 to 3 of 3
Hi, i would like you to explain me whats the best way to programmatically run a process in the background. I have read lots of stuff but most of it ...
  1. #1
    Just Joined!
    Join Date
    Jun 2010
    Posts
    1

    What the best way to programmatically run a process in the background?

    Hi, i would like you to explain me whats the best way to programmatically run a process in the background. I have read lots of stuff but most of it is related with the use of & and de system() function.
    I think the most correct way to put a process in the background is to use fork() and then kill the father as the children will continue running, also could turn the process in to a daemon.
    if someone could help i would appreciate it.


    Thanks in advanced.

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    Use fork() and exec(). See the man page for the exec functions: man 3 exec
    It is not advised to use the system() call to do this since it also starts a shell in order to fork/exec the program you want to run in the background. You don't want to kill the parent process because that will usually kill the children as well. You keep the father running, and have it wait on the children until they all terminate, then it can terminate, otherwise you will likely end up with a bunch of "zombie" processes and lose system resources over time. To run a process as a daemon (server), you need it to mask out SIGHUP events, and redirect stdin/stdout/stderr to files or /dev/null.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149
    two forks will make a process a daemon, which should probably do what you want

    Re: fork twice to prevent zombie process

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...