<div dir="ltr">Hi I tried your suggestion, and it's really just a problem using different terminals!<div><br></div><div>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:</div><div><br></div><div><img src="cid:ii_lp84be3r0" alt="image.png" width="562" height="59"><br></div><div><br></div><div>So I guess this should be a problem about Intellij idea</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Radosław Smogura <<a href="mailto:mail@smogura.eu">mail@smogura.eu</a>> 于2023年11月21日周二 14:05写道:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="auto"><div dir="ltr"></div><div dir="ltr">Hi,</div><div dir="ltr"><br></div><div dir="ltr">This is quite interesting.</div><div dir="ltr"><br></div><div dir="ltr">I wonder if this can be somehow related to terminal used to run both programs.</div><div dir="ltr"><br></div><div dir="ltr">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? </div><div dir="ltr"><br></div><div dir="ltr">Best regards,</div><div dir="ltr">Rado Smogura</div><div dir="ltr"><br><div dir="ltr"></div><blockquote type="cite">On 21 Nov 2023, at 06:00, 刘希晨 <<a href="mailto:benrush0705@gmail.com" target="_blank">benrush0705@gmail.com</a>> wrote:<br><br></blockquote></div><blockquote type="cite"><div dir="ltr"><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-wrap"><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><div><image.png></div><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><div><image.png></div><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>
</div></blockquote></div></blockquote></div>