I found the following function in the source code of catwm (a minimalistic window manager):
void spawn(const Arg arg) {
if(fork() == 0) {
if(fork() == 0) {
if(dis)
close(ConnectionNumber(dis));
setsid();
execvp((char*)arg.com[0],(char**)arg.com);
}
exit(0);
}
}
I don't understand why not simply
void spawn(const Arg arg) {
if(fork() == 0) {
if(dis)
close(ConnectionNumber(dis));
setsid();
execvp((char*)arg.com[0],(char**)arg.com);
}
}
?
Are there any benefits of using the double fork()
here?
fork()
,setsid()
,fork()
. The code as posted means the final process run viaexecvp()
will be a session leader, meaning it could inadvertently acquire a controlling terminal. If the goal is not to prevent the process from acquiring a controlling terminal - you are correct - your second example is sufficient. – Andrew Henle Aug 29 '22 at 12:50