consider the following:
- a parent process with a child
- child runs another program using exec system call
- parent and child communicating through a bi-directional pipe(named MAINPIPE in rest)
- the child is a server binding on a port(e.g. 5000)
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!
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.