Question about different behaviour in C and Java when using stdout and stderr

刘希晨 benrush0705 at gmail.com
Tue Nov 21 04:58:06 UTC 2023


Hi I noticed a different behaviour in C and Java about using stdout and
stderr, and I want to know what causes the difference.

I wrote a function to print some strings to both stdout and stderr in C as
below:

#include <stdio.h>
#include <string.h>

void test_out(char* outBuffer, size_t outSize, char* errBuffer, size_t
errSize) {
    if(outSize > 0) {
        fwrite(outBuffer, 1, outSize, stdout);
        fflush(stdout);
    }
    if(errSize > 0) {
        fwrite(errBuffer, 1, errSize, stderr);
        fflush(stderr);
    }
}

int main() {
    char* hello1 = "out world1";
    char* hello2 = "err world2";
    size_t len1 = strlen(hello1);
    size_t len2 = strlen(hello2);
    for(int i = 0; i < 5; i++) {
        test_out(hello1, len1, hello2, len2);
    }
}



the output, as expected should be :
[image: image.png]

the stdout and stderr prints the message by turn, which is what we want

however, when I am calling the same function in java to implement the same
logic like below:

public class Test {
    private static final MethodHandle testHandle;

    static {
        SymbolLookup symbolLookup = NativeUtil.loadLibrary(Constants.TENET);
        testHandle = NativeUtil.methodHandle(symbolLookup, "test_out",
FunctionDescriptor.ofVoid(ValueLayout.ADDRESS, ValueLayout.JAVA_LONG,
ValueLayout.ADDRESS, ValueLayout.JAVA_LONG));
    }
    public static void main(String[] args) {
        try(Arena arena = Arena.ofConfined()) {
            MemorySegment s1 = arena.allocateUtf8String("out world1");
            MemorySegment s2 = arena.allocateUtf8String("err world2");
            for(int i = 0; i < 5; i++) {
                testHandle.invokeExact(s1, s1.byteSize(), s2, s2.byteSize());
            }
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
    }
}

the result is not as expected:
[image: image.png]

either the stdout finish all its output first, or the stderr finish its
ouput first, and I tried using multiple thread to call test_out() function
at the same time, the behaviour is still the same, the stdout and stderr
are seperated.

I want to know what causes this, and how could I make stdout and stderr
behave in Java just like in C. Thanks for your help!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/panama-dev/attachments/20231121/57f3437a/attachment-0001.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image.png
Type: image/png
Size: 9017 bytes
Desc: not available
URL: <https://mail.openjdk.org/pipermail/panama-dev/attachments/20231121/57f3437a/image-0002.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image.png
Type: image/png
Size: 13230 bytes
Desc: not available
URL: <https://mail.openjdk.org/pipermail/panama-dev/attachments/20231121/57f3437a/image-0003.png>


More information about the panama-dev mailing list