Reputation: 117587
I am implementing a web server where I need to make the parent process do the following:
fork()
new worker processes (a pool) at the beginning.accept()
function) into a queue.and the worker process will do the following:
After looking around and searching the internet, I found that I can send a file descriptor between different processes via UNIX Domain Socket
or Pipes
. But unfortunately, I can do this synchronously only! (I can send one fd
at a time, and I cannot put it in a waiting queue)
So, my question is:
Upvotes: 0
Views: 4633
Reputation: 399823
File descriptors are just integers. They are used to index into a per-process table of file information, maintained by the kernel. You can't expect a file descriptor to be "portable" to other processes.
It works (somewhat) if you create the files before calling fork()
, since the file descriptor table is part of the process and thus clone()
d when the child is created. For file descriptors allocated after the processes have split, such as when using accept()
to get a new socket, you can't do this.
UPDATE: It seems there is a way, using sendmsg()
with AF_UNIX sockets, see here for details as mentioned in this question. I did not know that, sounds a bit "magical" but apparently it's a well-established mechanism so why not go ahead and implement that.
Upvotes: 2
Reputation: 67733
Upvotes: 0