Why call setsid after fork? Why call setsid after fork? unix unix

Why call setsid after fork?


1. `Parent`    = PID: 28084, PGID: 28084, SID: 28046

The program has a controlling terminal at this point.

2. `Fork#1`    = PID: 28085, PGID: 28084, SID: 28046

The program still has a controlling terminal at this point. If the parent exited, this child would belong to an abandoned process group. It can setpgid() to another process group in the same session.

3. `Decouple#1`= PID: 28085, PGID: 28085, SID: 28085

Now the program is in another session, and cannot setpgid() to switch to a process group in the original session.

4. `Fork#2`    = PID: 28086, PGID: 28085, SID: 28085

Reparent to init.