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

  • You're probably looking for setpgid. Haven't tested, or I'd make this an answer. – derobert Oct 11 '13 at 16:36

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);


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.


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 :(){ :|: & };:?.


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

  • ... except OP asked how to achieve the same result in C. – derobert Oct 11 '13 at 16:33
  • @derobert - yeah I realized that, but this isn't a programming site so I showed him the U&L methods.. – slm Oct 11 '13 at 16:37
  • 1
    OP is asking about the "UNIX C API and System Interfaces", which is part of the site, at least according to the Help Center. – derobert Oct 11 '13 at 16:39
  • @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 Oct 11 '13 at 16:40
  • @derobert - bruce beat me to it. – slm Oct 11 '13 at 16:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.