Ask a Question related to UNIX Programming, Design and Development.
-
azi #1
Need help with pipe to redirect input/ output
I have created two child processes. I am trying to tell one process to
write a command to the pipe, and I want the other child to read from the
pipe and print it out to the stdout. I want first child to write the
result of ps command with argument -aef to pipe, and the second child to
look at the pipe's output and run grep command to print all processes
running csh. I have used execl("usr/bin/ps -aef", "ps",0); to write the
result of ps to the pipe, but I don't know how to make the second child
read it and pick processors with csh??????????
I would appreciate your help.
int pipefd;
pipe(pipefd);
switch(pid2=fork())// This one writes to pipe
case -1: //error
case 0: close(1);
dup(pipefd[1]);
close(pipefd[0]);
execl("usr/bin/ps -aef", "ps",0);
switch(pid3=fork())// This one reads from pipe and prints to stdout
case -1: //error
case 0: close(0);
dup(pipefd[0]);
close(pipefd[1]);
//Need to read output from pipe and print to stdout???????
--
Posted via [url]http://dbforums.com[/url]
azi Guest
-
redirect output to a file
Hi everybody I was wondering I've made a little script to make the 'w'-command a bit nicer to work with.. ---~/bin/w--- #/bin/bash cd w -sh... -
can't redirect output to a file?!
i wrote a simple program that read data from STDIN but redirected in a pipe from a file parse it and wrote output to STDOUT,here is the code: ... -
How do I detect if input is from a pipe or not?
I'm writing a utility that can get input from a file or from a pipe'd output from another program: myprogram -infile test.txt or type... -
813:cannot write to pipe for output(no reading message)
kevin wrote: Again - you need to provide us with enough information to help you. Platform? Version? What tool is doing the 'print'? ... -
Redirect output of include() for mailing
Hello, Suppose I have a php file that output certain HTML given certain parameters. So I can call it likt http://localhost/myscript.php?foo=bar ... -
Jens.Toerring@physik.fu-berlin.de #2
Re: Need help with pipe to redirect input/ output
azi <member34294@dbforums.com> wrote:
> I have created two child processes. I am trying to tell one process to
> write a command to the pipe, and I want the other child to read from the
> pipe and print it out to the stdout. I want first child to write the
> result of ps command with argument -aef to pipe, and the second child to
> look at the pipe's output and run grep command to print all processes
> running csh. I have used execl("usr/bin/ps -aef", "ps",0); to write the
> result of ps to the pipe, but I don't know how to make the second child
> read it and pick processors with csh??????????> I would appreciate your help.This must be an array of 2 ints.> int pipefd;
This should be> pipe(pipefd);
> switch(pid2=fork())// This one writes to pipe
> case -1: //error
> case 0: close(1);
> dup(pipefd[1]);
> close(pipefd[0]);
> execl("usr/bin/ps -aef", "ps",0);
execl( "/usr/bin/ps", "ps" "-aef", NULL );
And check if 'ps' is really living in /usr/bin, on some systems it's
in /bin or even /sbin and if you get it wrong execl() will return
and not run ps.
What's the problem? Just use read(2) to read on the pipe and whatever> switch(pid3=fork())// This one reads from pipe and prints to stdout
> case -1: //error
> case 0: close(0);
> dup(pipefd[0]);
> close(pipefd[1]);
> //Need to read output from pipe and print to stdout???????
you fancy to write it to stdout. Here's a working version of your
program:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main( void )
{
int pipefd[ 2 ];
char buffer[ 80 ];
ssize_t size;
if ( pipe( pipefd ) < 0 )
exit( EXIT_FAILURE );
switch( fork( ) )
{
case -1 :
exit( EXIT_FAILURE );
case 0 :
close( 1 );
dup( pipefd[ 1 ] );
close( pipefd[ 0 ] );
execl( "/bin/ps", "ps" "-aef", NULL );
exit( EXIT_FAILURE );
}
switch( fork( ) )
{
case -1 :
exit( 1 );
case 0 :
close( 0 );
dup( pipefd[ 0 ] );
close( pipefd[ 1 ] );
while ( ( size = read( 0, buffer, 80 ) ) > 0 )
write( 1, buffer, size );
}
return EXIT_SUCCESS;
}
Reagrds, Jens
--
_ _____ _____
| ||_ _||_ _| [email]Jens.Toerring@physik.fu-berlin.de[/email]
_ | | | | | |
| |_| | | | | | [url]http://www.physik.fu-berlin.de/~toerring[/url]
\___/ens|_|homs|_|oerring
Jens.Toerring@physik.fu-berlin.de Guest



Reply With Quote

