C program embedding Julia - works in terminal but breaks in pipes

I’d like to write a C program which embeds Julia code, and call this C program from an unrelated program using two-way pipes. While the C program works fine in the terminal, it breaks when called from pipes. Here’s a minimal working example, where the C program initializes the Julia runtime but does not run any actual Julia code. Instead, it runs C code which repeatedly reads integers from stdin and prints them out to stdout:

/* test1.c */
#include <julia.h>
#include <stdio.h>
JULIA_DEFINE_FAST_TLS

int main(int argc, char *argv[])
{
    int i;
    jl_init();
    
    while (!feof(stdin)) {
        scanf("%d", &i);
        printf("%d\n", i);
    }

    jl_atexit_hook(0);
    return 0;
}

I compiled it with

gcc -o test1 -fPIC -I$JULIA_DIR/include/julia -L$JULIA_DIR/lib -Wl,-rpath,$JULIA_DIR/lib test1.c -ljulia

where JULIA_DIR points to my Julia 1.8.0 installation.

The program runs fine in the terminal: whenever I type in any integer, the same integer is printed out, as expected. However, when I try to call it from another program via two-way pipes, it breaks badly. I’d like to interface with a different program, but here I’ll just show the problem by calling the program from a Julia REPL:

julia> io = open(`./test1`, "r+")
Process(`./test1`, ProcessRunning)

julia> println(io, "10")

julia> readline(io)
"32766"

You can see that the output is completely wrong and seems to be an uninitialized integer. I strongly suspects that there is some conflict between Julia’s handling of stdin / stdout and stdio.h in C, because I don’t experience this problem when my C program only performs IO by executing Julia code containing e.g., print(). Is it possible that there should be some syncing between the IO buffers in C and in Julia?

1 Like