I want to listen on the parent process and accept on the forked child process, how could I do that?
Normally I cloud listen, then fork, the accept. But if I try to use the fd inherited from the parent process which is already listening with uv_tcp_open and uv_listen, listen would be called many times.
Can you come up with an example? It is really hard to understand what you exactly mean/want from 2 simple sentences
What I want to do is:
int main() { int fd = socket(AF_INET, SOCK_STREAM, ...); bind(fd); listen(fd, ...); int pid = fork(); // this should be done with uv_spawn if(pid == 0) { while(1) { fd = accept(); handle(fd); } } }
I assume this is not possible since uv_spawn() is generally fork() and then execve() right away.
You could try to inherit the listening socket in a child process via UV_INHERIT_STREAM though, but I'm sure it's legal.
The libuv way of doing things is not to share the same sockets but rather send them around over pipes. In your case, a master process can listen and accept sockets and then pass them over to a child process via an IPC pipe that you set up via uv_spawn().
Correct. That's how the cluster module in Node.js works.