Comment by alerighi
4 years ago
It makes creating processes easy to me, when you did understand how it works:
while (1) {
int client_socket = accept(socket, &client_addr, &client_len);
if (client_socket > 0) {
pid_t pid = fork();
if (pid < 0) {
// handle error
}
if (pid == 0) {
handle_connection(client_socket, &client_addr);
}
} else {
// handle error
}
}
No need to do complex things to start a new process, having to pass argument to it in some way, etc.
Oh I understand how it works. I implemented it, in the first POSIX implementation. I just don't get how anybody wants to do that.
Yes, there's the example right there. But it shows the awkwardness immediately - decoding what the f happened by checking a side effect (is pid == 0? wtf?)
How about spoon(handle_connection, ...) or something like that? See how much better?
It makes more difficult to pass context. You have to resort in the classical void * context, that is not handy to use. Or you have to use globals. The fork idea is more elegant to me, it duplicates the program flow execution in place.