![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 | 08-19-2008 05:26 AM |
| C++ How to use pipe() & fork() with stdin and stdout to another program | vvaidyan | High Level Programming | 2 | 05-16-2008 07: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 07:08 PM |
| How to write to stdin of another program (program A -> [stdin]program B) | vvaidyan | High Level Programming | 1 | 04-30-2008 01:44 PM |
| how can i write process_pool program? | hit | High Level Programming | 4 | 06-17-2002 03:36 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
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 |
|
||||
|
/*
* 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; } |
|
||||
|
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.
|
|
||||
|
Quote:
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);
|
| Sponsored Links | ||
|
|