[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: Backround process, fork & vfork, synchronization methods?

On Tue, 20 Mar 2001 Timo.Sakari@xxxxxxx.fi wrote:
> parent.c and child.c). I want to have two way communication between
> parent and child (read and write to/from parent and child), but
> so far I have only succeeded from writing from child to parent.

A pipe is unidirectional. See 'man pipe'.

vpipe[0] is for reading, vpipe[1] is for writing. If you want to write
from the parent to the child, you take vpipe[0] in the child and dup it to
the childs stdin_fileno, then write to vpipe[1] in the parent.

If you want to do the other direction, you need another pipe and connect
it the other way around - say, wpipe[1] in the child to stdout_fileno and
read from wpipe[0] in the parent.

And if you want to do both, you create both pipes.

In your other mail about fgets/read, it seemed as if you used "stdin" both
in read and fgets. fgets accepts a libc struct FILE * handle, while read
wants a unix filehandle (an integer). "stdin" by itself is a FILE *
handle, suitable for fgets, fread, fprintf etc. You cannot give that to
read. You need STDIN_FILENO for that, dont confuse the two methods and
dont use them at the same time.

You should simply use read, since you need to select() and select is not
really compatible with fgets. It probably did not work for you since you
stuffed in "stdin" as filedescriptor to read (I'm surprised you did not
get a warning from the compiler...)