<div dir="ltr">Hi I noticed a different behaviour in C and Java about using stdout and stderr, and I want to know what causes the difference.<div><br></div><div>I wrote a function to print some strings to both stdout and stderr in C as below:</div><div><br></div><div style="color:rgb(204,204,204);background-color:rgb(31,31,31);font-family:Consolas,"Courier New",monospace;font-size:14px;line-height:19px;white-space:pre"><div><span style="color:rgb(197,134,192)">#include</span><span style="color:rgb(86,156,214)"> </span><span style="color:rgb(206,145,120)"><stdio.h></span></div><div><span style="color:rgb(197,134,192)">#include</span><span style="color:rgb(86,156,214)"> </span><span style="color:rgb(206,145,120)"><string.h></span></div><br><div><span style="color:rgb(86,156,214)">void</span> <span style="color:rgb(220,220,170)">test_out</span>(<span style="color:rgb(86,156,214)">char</span><span style="color:rgb(212,212,212)">*</span> <span style="color:rgb(156,220,254)">outBuffer</span>, <span style="color:rgb(78,201,176)">size_t</span> <span style="color:rgb(156,220,254)">outSize</span>, <span style="color:rgb(86,156,214)">char</span><span style="color:rgb(212,212,212)">*</span> <span style="color:rgb(156,220,254)">errBuffer</span>, <span style="color:rgb(78,201,176)">size_t</span> <span style="color:rgb(156,220,254)">errSize</span>) {</div><div>    <span style="color:rgb(197,134,192)">if</span>(<span style="color:rgb(156,220,254)">outSize</span> <span style="color:rgb(212,212,212)">></span> <span style="color:rgb(181,206,168)">0</span>) {</div><div>        <span style="color:rgb(220,220,170)">fwrite</span>(<span style="color:rgb(156,220,254)">outBuffer</span>, <span style="color:rgb(181,206,168)">1</span>, <span style="color:rgb(156,220,254)">outSize</span>, <span style="color:rgb(86,156,214)">stdout</span>);</div><div>        <span style="color:rgb(220,220,170)">fflush</span>(<span style="color:rgb(86,156,214)">stdout</span>);</div><div>    }</div><div>    <span style="color:rgb(197,134,192)">if</span>(<span style="color:rgb(156,220,254)">errSize</span> <span style="color:rgb(212,212,212)">></span> <span style="color:rgb(181,206,168)">0</span>) {</div><div>        <span style="color:rgb(220,220,170)">fwrite</span>(<span style="color:rgb(156,220,254)">errBuffer</span>, <span style="color:rgb(181,206,168)">1</span>, <span style="color:rgb(156,220,254)">errSize</span>, <span style="color:rgb(86,156,214)">stderr</span>);</div><div>        <span style="color:rgb(220,220,170)">fflush</span>(<span style="color:rgb(86,156,214)">stderr</span>);</div><div>    }</div><div>}</div><br><div><span style="color:rgb(86,156,214)">int</span> <span style="color:rgb(220,220,170)">main</span>() {</div><div>    <span style="color:rgb(86,156,214)">char</span><span style="color:rgb(212,212,212)">*</span> <span style="color:rgb(156,220,254)">hello1</span> <span style="color:rgb(212,212,212)">=</span> <span style="color:rgb(206,145,120)">"out world1"</span>;</div><div>    <span style="color:rgb(86,156,214)">char</span><span style="color:rgb(212,212,212)">*</span> <span style="color:rgb(156,220,254)">hello2</span> <span style="color:rgb(212,212,212)">=</span> <span style="color:rgb(206,145,120)">"err world2"</span>;</div><div>    <span style="color:rgb(78,201,176)">size_t</span> <span style="color:rgb(156,220,254)">len1</span> <span style="color:rgb(212,212,212)">=</span> <span style="color:rgb(220,220,170)">strlen</span>(<span style="color:rgb(156,220,254)">hello1</span>);</div><div>    <span style="color:rgb(78,201,176)">size_t</span> <span style="color:rgb(156,220,254)">len2</span> <span style="color:rgb(212,212,212)">=</span> <span style="color:rgb(220,220,170)">strlen</span>(<span style="color:rgb(156,220,254)">hello2</span>);</div><div>    <span style="color:rgb(197,134,192)">for</span>(<span style="color:rgb(86,156,214)">int</span> <span style="color:rgb(156,220,254)">i</span> <span style="color:rgb(212,212,212)">=</span> <span style="color:rgb(181,206,168)">0</span>; <span style="color:rgb(156,220,254)">i</span> <span style="color:rgb(212,212,212)"><</span> <span style="color:rgb(181,206,168)">5</span>; <span style="color:rgb(156,220,254)">i</span><span style="color:rgb(212,212,212)">++</span>) {</div><div>        <span style="color:rgb(220,220,170)">test_out</span>(<span style="color:rgb(156,220,254)">hello1</span>, <span style="color:rgb(156,220,254)">len1</span>, <span style="color:rgb(156,220,254)">hello2</span>, <span style="color:rgb(156,220,254)">len2</span>);</div><div>    }</div><div>}</div><br><br></div><div><br></div><div>the output, as expected should be : </div><img src="cid:ii_lp7ulfbc0" alt="image.png" width="562" height="29"><br><div><br></div><div>the stdout and stderr prints the message by turn, which is what we want</div><div><br></div><div>however, when I am calling the same function in java to implement the same logic like below:</div><div style="background-color:rgb(30,31,34);color:rgb(188,190,196)"><pre style="font-family:"Cascadia Mono",monospace;font-size:10.5pt"><span style="color:rgb(207,142,109)">public class </span>Test {<br>    <span style="color:rgb(207,142,109)">private static final </span>MethodHandle <span style="color:rgb(199,125,187);font-style:italic">testHandle</span>;<br><br>    <span style="color:rgb(207,142,109)">static </span>{<br>        SymbolLookup symbolLookup = NativeUtil.<span style="font-style:italic">loadLibrary</span>(Constants.<span style="color:rgb(199,125,187);font-style:italic">TENET</span>);<br>        <span style="color:rgb(199,125,187);font-style:italic">testHandle </span>= NativeUtil.<span style="font-style:italic">methodHandle</span>(symbolLookup, <span style="color:rgb(106,171,115)">"test_out"</span>, FunctionDescriptor.<span style="font-style:italic">ofVoid</span>(ValueLayout.<span style="color:rgb(199,125,187);font-style:italic">ADDRESS</span>, ValueLayout.<span style="color:rgb(199,125,187);font-style:italic">JAVA_LONG</span>, ValueLayout.<span style="color:rgb(199,125,187);font-style:italic">ADDRESS</span>, ValueLayout.<span style="color:rgb(199,125,187);font-style:italic">JAVA_LONG</span>));<br>    }<br>    <span style="color:rgb(207,142,109)">public static void </span><span style="color:rgb(86,168,245)">main</span>(String[] args) {<br>        <span style="color:rgb(207,142,109)">try</span>(Arena arena = Arena.<span style="font-style:italic">ofConfined</span>()) {<br>            MemorySegment s1 = arena.allocateUtf8String(<span style="color:rgb(106,171,115)">"out world1"</span>);<br>            MemorySegment s2 = arena.allocateUtf8String(<span style="color:rgb(106,171,115)">"err world2"</span>);<br>            <span style="color:rgb(207,142,109)">for</span>(<span style="color:rgb(207,142,109)">int </span>i = <span style="color:rgb(42,172,184)">0</span>; i < <span style="color:rgb(42,172,184)">5</span>; i++) {<br>                <span style="color:rgb(199,125,187);font-style:italic">testHandle</span>.invokeExact(s1, s1.byteSize(), s2, s2.byteSize());<br>            }<br>        } <span style="color:rgb(207,142,109)">catch </span>(Throwable e) {<br>            <span style="color:rgb(207,142,109)">throw new </span>RuntimeException(e);<br>        }<br>    }<br>}<br><br></pre></div><div>the result is not as expected:</div><div><img src="cid:ii_lp7uo77t1" alt="image.png" width="562" height="71"><br></div><div><br></div><div>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.</div><div><br></div><div>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!</div><div><br></div><div><br></div><div> </div></div>