Reputation: 73
consider the following:
my program should work as follows:
if child receives a certain message from a client create another bi-directional pipe to his parent and send/receive some info.
the problem is:
when I create a pipe in the child (using pipe()
) and pass the file descriptors to the parent (using MAINPIPE), parent gets "Bad file descriptor" error message when trying to read from pipe.
any idea?
EDIT:
guys,
my problem isn't passing file-desciptors of new pipe -that is created in child process- to parent, i've already done that using MAINPIPE,
but problem is : parent can't read from new pipe, got this error message "bad file descriptor" it seems that the file descriptors is closed in parent process!
Upvotes: 1
Views: 2009
Reputation: 229108
A file descriptor belongs to a process. In your case the child process.
Transferring the number of a file descriptor, in your case 10, does not transfer the file descriptor itself. It just transfers the number 10. The number 10 can mean file descriptor 10 in the child process, but as a file descriptor belongs to a process, it is meaningless in the parent process. A child process only inherits the file descriptor from the parent when it was created.
Any file descriptors opened after the child is created are not shared between the parent and child.
If you need to pass the actual file descriptor to another (e.g. the parent) process, unix domain sockets have a mechanism for doing that, other posts here contains some relevant links for this.
Upvotes: -1
Reputation: 67733
There is a way to send a file descriptor from the child to the parent process: you haven't said how you're trying to do it though, so I can't say why it doesn't work for you.
Note that, since you need an existing pipe to send the file descriptor over, you can just multiplex multiple logical streams over a single pipe. It's probably easier and more portable.
Edit: you still refuse to show the code you're using to send the file descriptor from the child to the parent, but this question contains some relevant discussion. Can you confirm if you're doing something similar?
Upvotes: 0
Reputation: 74028
Create the pipes before forking. Then the pipes are available in both the parent and child. No need to pass file descriptors around.
Here is a link to a library using unix domain sockets to send a file descriptor from child to parent:
http://gitorious.org/libancillary/libancillary
and here is the source:
http://gitorious.org/libancillary/libancillary/trees/master
You must adapt this to fit your requirements, of course.
Upvotes: 3