LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 10-09-2009, 05:58 AM   #1
venkat_k
Member
 
Registered: Sep 2008
Posts: 36

Rep: Reputation: 15
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
 
Old 10-09-2009, 06:17 AM   #2
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 101Reputation: 101
Quote:
Originally Posted by venkat_k View Post
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.
 
Old 10-09-2009, 09:24 AM   #3
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,139

Rep: Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127
Quote:
Originally Posted by venkat_k View Post
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
 
Old 10-09-2009, 01:26 PM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by lutusp View Post
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
 
Old 10-09-2009, 04:01 PM   #5
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 101Reputation: 101
Quote:
Originally Posted by ta0kira View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
i want to know detailed description of linux pipes, filters and streams. navneet88 Linux - Newbie 2 03-29-2009 10:19 AM
Beginner question on using pipes zirtik Programming 3 09-09-2008 07:14 PM
Question about reading/writing into a Named Pipes(FIFO) Sathya Programming 21 05-05-2008 09:39 AM
LXer: Linux Networx pipes in $37 million LXer Syndicated Linux News 0 09-12-2006 12:54 PM
Question regarding pipes alltime Programming 3 04-15-2005 08:54 PM


All times are GMT -5. The time now is 09:32 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration