How to make processes not die after its parent dies?

5,661

Solution 1

If you're doing this in C, you need to do a setsid(2) in your code, along with some fork() and exit() calls. setsid() has this effect:

... creates a new session if the calling process is not a process group leader. The calling process is the leader of the new session, the process group leader of the new process group, and has no controlling tty.

That's from the man page. Basically, if a process that's a process group leader gets certain signals, every process ID in that process group gets the signal. You can see the mechanism for this in the kill(2) man page. If the PID you call kill() on is negative, the signal gets sent to every process in the process group.

You also need to fork() and exit() in the right places. Basically look at instructions on how to become a daemon process. The parts you need to do:

switch (fork()) {
case -1: return -1;
case 0:  break;
default: _exit(EXIT_SUCCESS);
}

setsid();

switch (fork()) {
case -1: return -1;
case 0:  break;
default: _exit(EXIT_SUCCESS);
}

Read about becoming a daemon process for more of the rationale behind this code.

Solution 2

There are 2 ways to background a process and then keep it up. If the command was started and then backgrounded with & you can use this command to disconnect it from your terminal.

$ disown -a

If you want to insulate the process from the hang up signal (HUP) and also disconnect it from the terminal (tty) you can use this commands as well:

$ nohup <cmd>

Any console output will be written to a file, nohup.out.

So I would suggest that you could do something clever after the process has been backgrounded with the disown -a command, would be one idea.

For other ideas take a look at my answer to this U&L question, titled: Where is the fork() on the fork bomb :(){ :|: & };:?.

C

I'd take a look at this thread from SO, titled: C: Exec/fork > Defunct processes, specifically this answer by @Gilles.

Share:
5,661

Related videos on Youtube

Shnatsel
Author by

Shnatsel

Updated on September 18, 2022

Comments

  • Shnatsel
    Shnatsel over 1 year

    I've been messing with forkbombs and noticed that if I just fork() processes repeatedly, killing the initial process will bring down the whole tree. This is not the case for forkbombs written in BASH using its backgrounding operator &, so I know there's a way around this. How do I achieve the same result in C?

    I've been using forkbomb examples from Wikipedia: http://en.wikipedia.org/wiki/Fork_bomb#Example_fork_bombs

    • derobert
      derobert over 10 years
      You're probably looking for setpgid. Haven't tested, or I'd make this an answer.
  • slm
    slm over 10 years
    @derobert - yeah I realized that, but this isn't a programming site so I showed him the U&L methods..
  • derobert
    derobert over 10 years
    OP is asking about the "UNIX C API and System Interfaces", which is part of the site, at least according to the Help Center.
  • slm
    slm over 10 years
    @derobert - true, I'm looking up the C API eqiv. now. Interestingly all the answers are on SO 8-). Even more interesting the answer I'm reading is by Gilles.
  • slm
    slm over 10 years
    @derobert - bruce beat me to it.