The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
A program to trace execution of another program jiten_hegde High Level Programming 3 2 Weeks Ago 02:26 AM
C++ How to use pipe() & fork() with stdin and stdout to another program vvaidyan High Level Programming 2 05-16-2008 04:30 PM
How to clear the content of a pipe (STDIN) after it is written to another program? vvaidyan High Level Programming 10 05-15-2008 04:08 PM
How to write to stdin of another program (program A -> [stdin]program B) vvaidyan High Level Programming 1 04-30-2008 10:44 AM
how can i write process_pool program? hit High Level Programming 4 06-17-2002 12:36 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-30-2008
Registered User
 

Join Date: Mar 2008
Posts: 15
How to write to stdin of another program (program A -> [stdin]program B)

Hi,
Program A: uses pipe()

I am able to read the stdout of PROGAM B (stdout got through system() command) into PROGRAM A using:

* child
-> dup2(fd[1], STDOUT_FILENO);
-> execl("/path/PROGRAM B", "PROGRAM B", NULL);

* parent
-> char line[100];
-> read(fd[0], line, 100);


Question:
---------
How to write to stdin of PROGRAM B from PROGRAM A?
* should I use a different pipe?
* how to I read stdin in PROGRAM B? using cin?

Thanks in advance,
Vivek
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 08-02-2008
Registered User
 

Join Date: Aug 2008
Location: Portugal
Posts: 161
/*
* here's how you can do it...
* using popen()
*
#include <sys/types.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define READ 0
#define WRITE 1

pid_t
popen2(const char *command, int *infp, int *outfp)
{
int p_stdin[2], p_stdout[2];
pid_t pid;

if (pipe(p_stdin) != 0 || pipe(p_stdout) != 0)
return -1;

pid = fork();
if (pid < 0)
return pid;
else if (pid == 0)
{
close(p_stdin[WRITE]);
dup2(p_stdin[READ], READ);
close(p_stdout[READ]);
dup2(p_stdout[WRITE], WRITE);
execl("/bin/sh", "sh", "-c", command, NULL);
perror("execl");
exit(1);
}

if (infp == NULL)
close(p_stdin[WRITE]);
else
*infp = p_stdin[WRITE];
if (outfp == NULL)
close(p_stdout[READ]);
else
*outfp = p_stdout[READ];
return pid;
}

/*
* now in main... infp will be the stdin (in file descriptor)
* and outfp will be the stdout (out file descriptor)
* have fun
*/

int
main(int argc, char **argv)
{
int infp, outfp;
char buf[128];

if (popen2("your-program-B", &infp, &outfp) <= 0)
{
printf("Unable to exec your-program-B\n");
exit(1);
}

memset (buf, 0x0, sizeof(buf));

write(infp, "Z\n", 2);
write(infp, "D\n", 2);
write(infp, "A\n", 2);
write(infp, "C\n", 2);
close(infp);
read(outfp, buf, 128);
printf("buf = '%s'\n", buf);
return 0;
}
Reply With Quote
  #3 (permalink)  
Old 08-02-2008
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 3,493
Also note: popen is a one way deal - you choose either to write to or to read from a child process, popen will not let you do both at the same time. Otherwise, you get into more interesting and advanced interprocess communication (IPC) programming like maybe pipes.
Reply With Quote
  #4 (permalink)  
Old 4 Weeks Ago
Registered User
 

Join Date: Aug 2008
Location: Portugal
Posts: 161
Post

Quote:
Originally Posted by jim mcnamara View Post
Also note: popen is a one way deal - you choose either to write to or to read from a child process, popen will not let you do both at the same time. Otherwise, you get into more interesting and advanced interprocess communication (IPC) programming like maybe pipes.
That's right! The POSIX.1-2001 function popen() only allows to read or write, not both. But if you look closely to my implementation, I don't actually use popen(). Instead, I created an popen2() that opens two pipes for the parent process (infp, outfp). Try that with "cat" for instance.

Code:
if (popen2("your-program-B", &infp, &outfp) <= 0)
        {
        printf("Unable to exec your-program-B\n");
        exit(1);
        }

        memset (buf, 0x0, sizeof(buf));

/* 
 * writing to stdin here
 */
        write(infp, "Z\n", 2);
        write(infp, "D\n", 2);
        write(infp, "A\n", 2);
        write(infp, "C\n", 2);
        close(infp);
/*
 * reading stdout here
 */
        read(outfp, buf, 128);
        printf("buf = '%s'\n", buf);
I wrote and then I read from the child process "your-program-B"
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 09:51 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0