Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 209
  • Last Modified:

Need help with piping in LINUx

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
Tim_Heldberg
Asked:
Tim_Heldberg
1 Solution
 
stefan73Commented:
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
 
van_dyCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now