Problem with stdin in Android NDK (but not with stdout)

845 views
Skip to first unread message

hiptanaka

unread,
Oct 29, 2010, 3:35:28 PM10/29/10
to android-ndk
Hi,

I'm trying to get an Android application connect directly to stdin and
stdout of a C code segment running in JNI. I tried a few different
things but ended up using two named pipes, for input and output
respectively. Essentially I create them using mkfifo() in native code,
then open each side of the two FIFO files in a separate environment.
One side from Java and one from C. After this I also redirect stdin
and stdout in C to the pipes using dup2(). Here's how it works in C:

mkfifo(out_file, 0666);
mkfifo(in_file, 0666);

And then when I'm sure the Java thread will open from the other end:

fdo = open(out_file, O_WRONLY);
fdi = open(in_file, O_RDONLY);

dup2(fdo, 1);
dup2(fdi, 0);

On the Java side, I open the same files but with reversed operations
(read for write in c, etc). The real code does have error handling and
such things as well.

The thing is, for stdout, it works. I can read it through the pipe
with a BufferedReader. But however I try, stdin won't work. I've done
cat in_file and it does get filled with stuff as Java writes to it
with a PrintWriter, but I can't get any function to read from the pipe
in C, even after redirecting stdin. It's really strange to me. Does
anyone have a solution?

Chris Stratton

unread,
Nov 1, 2010, 11:58:09 PM11/1/10
to android-ndk
On Oct 29, 3:35 pm, hiptanaka <david.hol...@gmail.com> wrote:

> I'm trying to get an Android application connect directly to stdin and
> stdout of a C code segment running in JNI. I tried a few different
> things but ended up using two named pipes, for input and output

Not sure what the solution is to why your method isn't working, but
this is a pretty common requirement. See the source of the terminal
emulator for a working solution (which iirc is more about grabbing the
child's automatic stdin and stdout rather than having it create pipes)

hiptanaka

unread,
Nov 2, 2010, 5:12:01 AM11/2/10
to android-ndk
Thanks. I actually solved it, and it was an embarassing mistake that
had nothing to do with the method. I did have a look at the terminal
but couldn't pinpoint the stdin/stout solution.

MikeBrWade

unread,
Nov 4, 2010, 1:00:43 AM11/4/10
to android-ndk
I am very interested in doing this as well, I have a set of binaries I
want to run that already run in linux and produce stdout and stdin
calls. I want to have a simple java front end "textview" or something
that reads these and simply dumps them to the screen. Can you
describe in more detail what you did? Seems like you are piping it
with a file, is the java able to run parallel to the jni app so the
text "streams" onto the screen from the native app?

On Oct 29, 2:35 pm, hiptanaka <david.hol...@gmail.com> wrote:
> Hi,
>
> I'm trying to get an Android application connect directly to stdin andstdoutof a C code segment running in JNI. I tried a few different
> things but ended up using two named pipes, for input and output
> respectively. Essentially I create them using mkfifo() in native code,
> then open each side of the two FIFO files in a separate environment.
> One side from Java and one from C. After this I also redirect stdin
> andstdoutin C to the pipes using dup2(). Here's how it works in C:
>
> mkfifo(out_file, 0666);
> mkfifo(in_file, 0666);
>
> And then when I'm sure the Java thread will open from the other end:
>
> fdo = open(out_file, O_WRONLY);
> fdi = open(in_file, O_RDONLY);
>
> dup2(fdo, 1);
> dup2(fdi, 0);
>
> On the Java side, I open the same files but with reversed operations
> (read for write in c, etc). The real code does have error handling and
> such things as well.
>
> The thing is, forstdout, it works. I can read it through the pipe

hiptanaka

unread,
Nov 5, 2010, 8:29:23 AM11/5/10
to android-ndk
The method actually worked as described here. I started one thread
that calls a C function (using JNI) that creates the pipes, opens
them, redirects stdin and stout and then starts some C code (the part
I gave simplified code for). Then another thread that opens the other
ends of the pipes (in Java, so to speak) and reads input/prints to
screen in a loop. So in total there are three threads. I've later
refined it but the principle is the same.

Hope that helps.
Reply all
Reply to author
Forward
0 new messages