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

刘希晨 benrush0705 at gmail.com
Tue Nov 21 09:14:59 UTC 2023


Hi I tried your suggestion, and it's really just a problem using different
terminals!

In the prior example, I executed my C code in vscode using system's default
terminal (Powershell), and in Java using Intellij Idea's default console,
then I switch to use Powershell for both program and I get the same correct
result:

[image: image.png]

So I guess this should be a problem about Intellij idea

Radosław Smogura <mail at smogura.eu> 于2023年11月21日周二 14:05写道:

> Hi,
>
> This is quite interesting.
>
> I wonder if this can be somehow related to terminal used to run both
> programs.
>
> Is the first one run in different terminal and with different shell? If
> yes, would it be possible to try running both programs in the same terminal
> and in the same shell with the same basic configuration of prompts and
> output?
>
> Best regards,
> Rado Smogura
>
> On 21 Nov 2023, at 06:00, 刘希晨 <benrush0705 at gmail.com> wrote:
>
> 
> 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.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.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/935d5cfd/attachment-0001.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image.png
Type: image/png
Size: 33403 bytes
Desc: not available
URL: <https://mail.openjdk.org/pipermail/panama-dev/attachments/20231121/935d5cfd/image-0001.png>


More information about the panama-dev mailing list