Solved

Need help with piping in LINUx

Posted on 2004-10-07
2
181 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
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 500 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now