08-02-2008
/*
* 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;
}
10 More Discussions You Might Find Interesting
1. Programming
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, STDOUT_FILENO);
-> execl("/path/PROGRAM B", "PROGRAM B", NULL);
* parent
-> char line;
-> read(fd, line, 100);
Question:
---------... (1 Reply)
Discussion started by: vvaidyan
1 Replies
2. Programming
PROGRAM A <-> PROGRAM B
PROGRAM A sends data as STDIN ro PROGRAM B and when PROGRAM B is executed from PROGRAM A, it sends output back to PROGRAM A. This is implemented using 2 pipes (fd1 & fd2).
The above process happens in a loop and during the second run, the previous data that had been... (10 Replies)
Discussion started by: vvaidyan
10 Replies
3. Programming
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, STDOUT_FILENO);
-> execl("/path/PROGRAM B", "PROGRAM B", NULL);
* parent
-> char line;
-> read(fd, line, 100);
Question:... (2 Replies)
Discussion started by: vvaidyan
2 Replies
4. Programming
Hi,
I wanted to know if i can write a program using switches and signals, etc to trace execution of other unix program which calls c program internally.
If yes how? If not with signals and switches then are there any other methods apart from debugging with gdb/dbx. (3 Replies)
Discussion started by: jiten_hegde
3 Replies
5. UNIX for Dummies Questions & Answers
Hi,
i want to write a script that executes a program (exec?) .
this program then requires a filename as input.
how do i give it this input in the script so the program will be complete run and close by the script.
e.g.
exec prog.exe
program then asks for filename
"enter filename:"... (1 Reply)
Discussion started by: tuathan
1 Replies
6. Shell Programming and Scripting
Hi,
I am having an input file which contains a group of words,if one specific word comes which goes to next line.
example:
input file===>
shashi country= india comapny= none shashi shashi company= NONE shashi=my name
output===>
shashi country= india comapny= none
shashi
shashi... (6 Replies)
Discussion started by: hegdeshashi
6 Replies
7. Programming
I wrote a simple program that generates a random word 10,000,000 times.
I wrote it in python, then in C++ and compared the two completion times. The python script was faster! Is that normal? Why would the python script be faster? I was under the impression that C++ was faster. What are some of... (2 Replies)
Discussion started by: cbreiny
2 Replies
8. Homework & Coursework Questions
Long story short: I'm working inside of a Unix SSH under a bash shell. I have to code a C program that generates a random number. Then I have to call the compiled C program with a Perl program to run the C program 20 times and put all the generated random #s into a text file, then print that text... (1 Reply)
Discussion started by: jdkirby
1 Replies
9. Shell Programming and Scripting
I need help with the following.
1) Write a program in any language that takes a single integer array parameter and returns the decimal average of the input values.
2) Write a program, in any language, that prints the integers from 1 to 10, along with a cumulative sum of the integers printed... (1 Reply)
Discussion started by: sqa4life
1 Replies
10. Shell Programming and Scripting
I created a program, so a kid can practice there math on it. It dispenses varies math problems and the kid must input an answer. I also want it to grade the work they have done, but I can't find the best place for it to print out the grade.
I have:
if ( $response =~ m/^/ ) {
$user_wants_to_quit... (1 Reply)
Discussion started by: germany1517
1 Replies
FBB::Pipe(3bobcat) Error handler FBB::Pipe(3bobcat)
NAME
FBB::Pipe - Defines a system level pipe
SYNOPSIS
#include <bobcat/pipe>
Linking option: -lbobcat
DESCRIPTION
FBB::Pipe objects may be used to construct a pipe. FBB::Pipe objects offer a simple interface to the reading and writing ends of pipes.
FBB::Pipe objects are object-wrappers around the pipe(2) system call.
NAMESPACE
FBB
All constructors, members, operators and manipulators, mentioned in this man-page, are defined in the namespace FBB.
INHERITS FROM
-
CONSTRUCTORS
o Pipe():
The default Pipe() constructor constructs a basic pipe, calling pipe(2).
This constructor throws an Errno exception if the default Pipe constructor did not properly complete. The thrown Errno object's
which() member shows the system's errno value set by the failing pipe(2) function.
o Pipe(int const *fd):
This constructor expects two file descriptors, which already define a pipe, stored at fd. Following the construction of the Pipe
object the array at by fd is no longer used by the Pipe object. The copy constructor is available.
Note that when the pipe goes out of scope, no close(2) operation is performed on the pipe's ends. If the pipe should be closed by the desc-
tructor, derive a class from Pipe(3bobcat), whose destructor performs the required closing-operation.
MEMBER FUNCTIONS
o int readFd() const:
Returns the pipe's file descriptor that is used for reading
o void readFrom(int filedescriptor):
Sets up redirection from the internal read filedescriptor to the given filedescriptor: information is read from the FBB::Pipe object
when reading from the provided filedescriptor.
o void readFrom(int const *filedescriptors, size_t n):
Sets up redirection from the internal read filedescriptor to the given filedescriptors: information is read from the FBB::Pipe
object when reading from any of the n provided filedescriptors (experimental).
o int readOnly():
Closes the writing end of the pipe, returns the reading end's file descriptor.
o void verify() const:
Obsoleted, will be removed in a future Bobcat release.
o int writeFd() const:
Returns the pipe's file descriptor that is used for writing
o void writtenBy(int filedescriptor):
Sets up redirection from the internal write filedescriptor to the given filedescriptor: information is written to the FBB::Pipe
object when writing to the provided filedescriptor.
o void writtenBy(int const *filedescriptors, size_t n):
Sets up redirection from the internal write filedescriptor to the given filedescriptors: information is written to the FBB::Pipe
object when writing to each of the n provided filedescriptors.
o int writeOnly():
Closes the reading end of the pipe, returns the writing end's file descriptor.
PROTECTED ENUMERATION
The RW protected enumeration has the following elements:
o READ:
The index in d_fd[] (see below) of the element holding the pipe's reading file descriptor;
o WRITE:
The index in d_fd[] (see below) of the element holding the pipe's writing file descriptor
PROTECTED DATA
o int d_fd[2]:
The array holding the pipe's file descriptors. The READ element contains the pipe's reading file descriptor, the WRITE element con-
tains the pipe's writing file descriptor,
EXAMPLE
#include <bobcat/pipe>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <iostream>
#include <string>
using namespace std;
using namespace FBB;
int main()
{
Pipe p; // construct a pipe
cout << "Read file descriptor: " << p.getReadFd() << endl;
cout << "Write file descriptor: " << p.getWriteFd() << endl;
int pid = fork();
if (pid == -1)
return 1;
if (!pid) //child
{
p.readFrom(STDIN_FILENO); // read what goes into the pipe
string s;
getline(cin, s);
cout << "CHILD: Got `" << s << "'" << endl;
getline(cin, s);
cout << "CHILD: Got `" << s << "'" << endl;
return 0;
}
p.writtenBy(STDOUT_FILENO); // write to the pipe via cout
cout << "first line" << endl;
cout << "second line" << endl;
waitpid(pid, 0, 0);
return 0;
}
FILES
bobcat/pipe - defines the class interface
SEE ALSO
bobcat(7), pipe(2), mkfifo(3)
BUGS
Note that when the pipe goes out of scope, no close(2) operation is performed on the pipe's ends. If the pipe should be closed by the desc-
tructor, derive a class from Pipe(3bobcat), whose destructor performs the required closing-operation.
DISTRIBUTION FILES
o bobcat_3.01.00-x.dsc: detached signature;
o bobcat_3.01.00-x.tar.gz: source archive;
o bobcat_3.01.00-x_i386.changes: change log;
o libbobcat1_3.01.00-x_*.deb: debian package holding the libraries;
o libbobcat1-dev_3.01.00-x_*.deb: debian package holding the libraries, headers and manual pages;
o http://sourceforge.net/projects/bobcat: public archive location;
BOBCAT
Bobcat is an acronym of `Brokken's Own Base Classes And Templates'.
COPYRIGHT
This is free software, distributed under the terms of the GNU General Public License (GPL).
AUTHOR
Frank B. Brokken (f.b.brokken@rug.nl).
libbobcat1-dev_3.01.00-x.tar.gz 2005-2012 FBB::Pipe(3bobcat)