?
Solved

Need help with piping in LINUx

Posted on 2004-10-07
2
Medium Priority
?
200 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Suggested Courses

764 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