Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Need help with piping in LINUx

Posted on 2004-10-07
2
Medium Priority
?
203 Views
Last Modified: 2010-04-15
I am trying to write a program that creates two pipes, one to send information to another process and one to send information back for display.  I am supposed to fork off a child process and in that child execute another and send the pipe file descriptors to it so that it can communicate back to the original program.  How would I do this?
0
Comment
Question by:Tim_Heldberg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 12

Expert Comment

by:stefan73
ID: 12260163
Hi Tim_Heldberg,

Since your question smells like homework, here are your basic ingredients:
pipe() creates a birectionally-connected pair of file descriptors.
fork() creates a new process. The child process inherits the parent's file descriptors.
dup2() copies one file descriptor over another. The interesting fds are 0 (stdin) and 1 (stdout).
exec()

So,
1. create a file descriptor pair, one end for the parent, one for the child.
2. fork() to create a child process.
3. Change the child's stdin/stdout to the child's end of the pipe.
4. Do something with the child. This can be exec() of another binary, or some simple printf().

In case you don't know the exact parameters, have a look at the man pages.

Cheers!

Stefan
0
 
LVL 5

Accepted Solution

by:
van_dy earned 1000 total points
ID: 12263543
int fd1[2], fd2[2];
pipe(fd1);
pipe(fd2);
if(!fork()){
     .......
     exec(whatever);
}
parent runs ....

now both the pipes' descriptors would remain open accross exec(unless of course u set the CLOSEONEXEC flag).
if the parent and whatever program u execute need to pass descriptors to each other over the pipes, the sending process has to build a msghdr structure containing the descriptor to be passed. Descriptors are passed as ancilliary data, take a look at struct msghdr in sys/socket.h.  msg_control member of your struct msghdr  should pont to a cmsghdr struct which will contain the value of the descriptor which u want to pass.
for ex

parent process:
fd3 = open(somefile);
struct msghdr msg;
struct cmsghdr cmsg;
cmsg.cmsg_data = fd;
msg.msg_control = &cmsg;


you will need to fill up other members of the structures as required. once u have done that, u can use sendmsg() function to send the data over one of ur pipes, and use recvmsg() in ur child process to receive it from the read end of the same pipe. I dont understand why u are opening 2 pipes for this.  if you want two way communication u may use the socketpair function.  i think this will give u enough hints on how to proceed.

regards.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question