Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Simulating Pipe in C.

Posted on 2007-07-22
16
Medium Priority
?
395 Views
Last Modified: 2009-09-15
Here is what I am working on:

If you execute:
% pipe <program1> <program2>

then the two programs <program1> and <program2> will be concurrently executed, with the standard output of <program1> attached to the standard input to <program2>.

The pipe program must support arguments of program1 and program2. For example:

% pipe "cat /etc/passwd" "wc -l"

This should be equivalent to the shell executing:

% cat /etc/passwd | wc l

I've been working on it for weeks now and can't figure it out. Any ideas or suggestions?
0
Comment
Question by:kiltgod
  • 6
  • 5
  • 2
  • +2
15 Comments
 
LVL 22

Expert Comment

by:grg99
ID: 19544048
we can't give you the whole answer, but see  "man popen"  for a really big hint.  
0
 
LVL 11

Expert Comment

by:avizit
ID: 19544584

 you will probably need to do a

man fork
and man exec too
0
 
LVL 43

Expert Comment

by:ravenpl
ID: 19545597
man 2 pipe
man 2 fork
man 2 execlp
man 2 dup2
do the math by Yourself
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:kiltgod
ID: 19547894
This is what I have thus far:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>

int main(int arg, char *argv[]) {
   FILE *fpipe;
   char *command = argv[1];
   char line[256];

   if ( !(fpipe = (FILE*)popen(command,"r")) )
   {  // If fpipe is NULL
      perror("Problems with pipe");
      exit(1);
   }


   while ( fgets( line, sizeof line, fpipe))
   {
     printf("%s", line);
   } pclose(fpipe);

Now I can't figure out how exactly to pipe the output into another command. I've tried execve, but to no avial since it can't take argv[] as an argument. I am looking into raven's idea as well.
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 2000 total points
ID: 19547940
>> Now I can't figure out how exactly to pipe the output into another command.

Just popen the other command with "w" mode, and fprintf to that file pointer ...
0
 
LVL 11

Expert Comment

by:avizit
ID: 19552851
since you have two commands you will need to fork() to create a new process after you do a popen.

so in one process you close the read end of the pipe and in the other you close the write end
and then you can use the pipe for one way communication.
0
 
LVL 43

Expert Comment

by:ravenpl
ID: 19553770
> since you have two commands you will need to fork() to create a new process after you do a popen.
and duplicate fd obtained from popen to stdout(stdin)
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 19553969
>> since you have two commands you will need to fork() to create a new process after you do a popen.

If your popen implementation is standards compliant, popen should already do that for you, so there's no need for an extra fork.

However, using a combination of pipe, fork, read and write is a good alternative.
0
 
LVL 43

Expert Comment

by:ravenpl
ID: 19553989
Infinity08: You missed one point
pipe <prog1> <prog1>
You haveto replace Your application with prog1(and duplicate stdin/out from proper fds), or act as proxy between two popen()s - right?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 19554302
>> Infinity08: You missed one point

I don't think I did ... you only have to set up a one-way stream from prog2's stdout to prog1's stdin, and that can easily be done with two popen's.

So, I'm not sure what I missed ?
0
 
LVL 43

Expert Comment

by:ravenpl
ID: 19554334
But You are executing from pipe program right? So You have two FILE structures and have to act as proxy. Right?
But there's much easier/lighter solution - pipe(), fork(), dup2()x2, exec()x2 - isn't it?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 19554364
>> But there's much easier/lighter solution - pipe(), fork(), dup2()x2, exec()x2 - isn't it?

I would prefer it too, but it's not easier, because popen does the fork's and exec's for you - you just have to popen the two processes - one for reading and one for writing, and then loop over the data and forward ...
0
 
LVL 43

Expert Comment

by:ravenpl
ID: 19554513
Unless the following is allowed usage, then popen(one way pipe) is useless
cat /etc/passwd | pipe cat tail | cat
0
 
LVL 43

Expert Comment

by:ravenpl
ID: 19554545
> % pipe "cat /etc/passwd" "wc -l"
But I agree, that tokeninzing params for exec() usage is not easier at all.
BTW: system("argv[1] | argv[2]") // LOL ;)
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 19554586
>> Unless the following is allowed usage, then popen(one way pipe) is useless
>> cat /etc/passwd | pipe cat tail | cat

I'm not sure what you mean. The pipe application would not make use of stdin and stdout, so there's no sense in using it in another pipe. A one way pipe is not useless, because that's exactly what was asked ...

As a side note : what's that command supposed to do lol


>> But I agree, that tokeninzing params for exec() usage is not easier at all.

With popen's it's a 5-line application : 2 for the popen's, 1 for the loop, and 2 more for the pclose's. Can't get a lot simpler than that ;)


>> BTW: system("argv[1] | argv[2]") // LOL ;)

heh ...
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…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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

577 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