Reading console output and writing to console

J

jan.rebada

Hello,

I have a problem. I'm creating a program that uses system. Say for
example I invoke system("mysql -uroot -p"), the next line in the
screen will be enter password>, what I want with my C++ program is to
detect if the screen reads 'enter password>' and automatically my
program will input the appropriate password.

Any help will do. Thanks.
 
R

Rolf Magnus

Hello,

I have a problem. I'm creating a program that uses system. Say for
example I invoke system("mysql -uroot -p"), the next line in the
screen will be enter password>, what I want with my C++ program is to
detect if the screen reads 'enter password>' and automatically my
program will input the appropriate password.

Not possible with the system() function. You need to use operating system
specific functions to do this, e.g. the POSIX function popen.
 
F

FNX

Around about 26/01/08 07:39, Rolf Magnus scribbled ...
Not possible with the system() function. You need to use operating system
specific functions to do this, e.g. the POSIX function popen.

Unfortunately, popen() only allows reading *or* writing to the child
process, not both. There's no really straightforward way; try reading:

http://www.developerweb.net/forum/showthread.php?t=3026


Certainly raw C++ as it stands has nothing like this, although Boost
or Loki might.
 
J

James Kanze

Around about 26/01/08 07:39, Rolf Magnus scribbled ...
Unfortunately, popen() only allows reading *or* writing to the child
process, not both. There's no really straightforward way; try reading:

Certainly raw C++ as it stands has nothing like this, although Boost
or Loki might.

Note that using a pipe in both directions to a single process is
not a good idea---you probably want two pipes. And even then,
be very, very careful about deadlock. A pipe can only hold so
much, and once it's full, a write blocks.
 
F

fabio.mazzarino

James:

I have exactly the deadlock problem you have just described.

I have two pipes for reading and writing. After 20KB the pipe buffer
is full. Every reading from the pipe returns errno 11 (EAGAIN), and
the child process stop writing due to the buffer overflow.

The parent keeps waiting for the son to finish, while the son keeps
waiting for the parent to empty the buffer.

How can I solve this?

Fabio A. Mazzarino

James Kanze escreveu:
 
J

James Kanze

I have exactly the deadlock problem you have just described.
I have two pipes for reading and writing. After 20KB the pipe buffer
is full. Every reading from the pipe returns errno 11 (EAGAIN), and
the child process stop writing due to the buffer overflow.
The parent keeps waiting for the son to finish, while the son keeps
waiting for the parent to empty the buffer.
How can I solve this?

There's no general solution. The system will only store so much
information for you in a pipe. One solution I've used in
specific cases is for one of the processes to write to a
temporary file, rather than the pipe, and only dump the
temporary file to the pipe when it has finished. This supposes,
however, that "finished" has some meaning here. More generally,
beyond a certain limit, you'll have to shift to using files,
rather than pipes; if a process is sending very big chunks, you
might consider putting each chunk in a separate file, and only
piping the file name. (When the receiving process has finished
with the data, it can then delete the file.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top