LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Linux pipes question (http://www.linuxquestions.org/questions/programming-9/linux-pipes-question-760737/)

venkat_k 10-09-2009 04:58 AM

Linux pipes question
 
Hello All,

I have a pipe for getting the output from child to parent.
-- running a command in the child -- getting the output in the parent.
If the output is a string, there is no issue. Parent is reading the data properly.

But is it possible to pass a structure from child to parent using pipe?
I am trying something like below (input and output are structures).
write(fd[1],input,sizeof(input)); -- in the child
read(fd[0],output,sizeof(output)); -- in the parent

I am not getting the expected output. Is this correct way of doing it r is there any other way?

-Prasad

lutusp 10-09-2009 05:17 AM

Quote:

Originally Posted by venkat_k (Post 3713278)
Hello All,

I have a pipe for getting the output from child to parent.
-- running a command in the child -- getting the output in the parent.
If the output is a string, there is no issue. Parent is reading the data properly.

But is it possible to pass a structure from child to parent using pipe?
I am trying something like below (input and output are structures).
write(fd[1],input,sizeof(input)); -- in the child
read(fd[0],output,sizeof(output)); -- in the parent

I am not getting the expected output. Is this correct way of doing it r is there any other way?

-Prasad

You might be better off representing the structure as text. This would be a more robust and portable way to pass the information. Trying to pass binary data through pipes is just a recipe for frustration.

A binary transfer appears to work until you try to use it over a network and one machine turns out to have opposite endedness, or different word length, or something. A text representation avoids these issues.

Classically, this is done by writing classes rather than structures, and creating a class member that creates a text representation of the class instance. Then you write a constructor for that same class that accepts and reads the string representation and creates the corresponding internal representation. Finally you pipe the text output from one class instance to another, and it works anywhere, across a network, regardless of machine specifics.

johnsfine 10-09-2009 08:24 AM

Quote:

Originally Posted by venkat_k (Post 3713278)
write(fd[1],input,sizeof(input)); -- in the child
read(fd[0],output,sizeof(output)); -- in the parent

Out of context (how input and output are declared) I can't be sure, but I think your problem may be that you are passing them by value, when you need to pass the address.

Try:

write(fd[1],&input,sizeof(input)); -- in the child
read(fd[0],&output,sizeof(output)); -- in the parent

ta0kira 10-09-2009 12:26 PM

Quote:

Originally Posted by lutusp (Post 3713291)
You might be better off representing the structure as text. This would be a more robust and portable way to pass the information. Trying to pass binary data through pipes is just a recipe for frustration.

This is the way I prefer to do it; however, while we're on the subject of robustness, a simple struct<->text scheme isn't scalable unless you can arbitrarily expand the structure of the text protocol. I generally use an XML-style protocol, which requires a significant amount of programatic overhead to get started in the first place, but it works well at any scale after that.

Provided it's just a POD struct without pointers and a pipe/socket (on a single machine) through which only that struct will be transmitted/received, write/read should be fine (taking into account johnsfine's suggestion.) In cases other than that specifically, a structured user-space protocol (or something like dbus) will most likely be required.
Kevin Barry

lutusp 10-09-2009 03:01 PM

Quote:

Originally Posted by ta0kira (Post 3713673)
This is the way I prefer to do it; however, while we're on the subject of robustness, a simple struct<->text scheme isn't scalable unless you can arbitrarily expand the structure of the text protocol. I generally use an XML-style protocol, which requires a significant amount of programatic overhead to get started in the first place, but it works well at any scale after that.

Provided it's just a POD struct without pointers and a pipe/socket (on a single machine) through which only that struct will be transmitted/received, write/read should be fine (taking into account johnsfine's suggestion.) In cases other than that specifically, a structured user-space protocol (or something like dbus) will most likely be required.
Kevin Barry

I replied as I did because the OP specified pipes, and pipes can go anywhere on a modern system. It's a bit difficult preventing any cases of piping across boundaries not anticipated by the original programmer.

As to the issue of scalability, the text representation only needs to be read by an instance of the same class, so the implementation need only meet that specific requirement, not a general one.


All times are GMT -5. The time now is 08:52 AM.