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"
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)
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)
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)
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)
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)
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)
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)
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)
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)
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
LEARN ABOUT OSF1
popen
popen(3) Library Functions Manual popen(3)NAME
popen - Initiates a pipe to a process
LIBRARY
Standard C Library (libc.so, libc.a)
SYNOPSIS
#include <stdio.h>
FILE *popen (
const char *command,
const char *type );
STANDARDS
Interfaces documented on this reference page conform to industry standards as follows:
popen(): XPG4, XPG4-UNIX
Refer to the standards(5) reference page for more information about industry standards and associated tags.
PARAMETERS
Points to a null-terminated string containing a shell command line. Points to a null-terminated string containing an I/O mode.
DESCRIPTION
The popen() function creates a pipe between the calling program and a shell command to be executed. It returns a pointer to a FILE struc-
ture for the stream.
If the type parameter is the value r, the calling program can read from the standard output of the command by reading from the returned
file stream. If the type parameter is the value w, the calling program can write to the standard input of the command by writing to the
returned file stream.
Because open files are shared, a type r command can be used as an input filter and a type w command as an output filter.
NOTES
Programs using the popen() function to invoke an output filter should beware of possible deadlock caused by output data remaining in the
program's buffer. This can be avoided by either using the setbuf() function to ensure that the output stream is unbuffered, or by using
the fflush() function to ensure that all buffered data is flushed before calling the pclose() function. If the original processes and the
process started with the popen() function concurrently read or write a common file, neither should use buffered I/O. If they do, the
results are unpredictable.
RETURN VALUES
Upon successful completion, the popen() function returns a pointer to the FILE structure for the opened stream. In case of error because
files or processes could not be created, the popen() function returns a null pointer.
RELATED INFORMATION
Functions: exec(2), fork(2), fclose(3), fopen(3), pclose(3), pipe(2), setbuf(3)
Standards: standards(5) delim off
popen(3)