The semantics of a fork, followed by setsid, and another fork

So I came across a piece of code that looked like this:

if (fork() != 0)
  exit(0);
setsid();
if (fork() != 0)
  exit(0);

What each individual system call can easily be looked up and understood (well, maybe not setsid), but what the entire block of code is doing is not clear at all

…and I really can’t say I understand the mechanics of it all to explain myself. But this python code segment’s comments does a good job of what the entire block of code is trying to do—daemonize a process completely.

Topic: 

Like this article? Please support my writing! Flattr my blog (see my thoughts on Flattr), tip me via PayPal, or send me an item from my Amazon wish list.