Monday, 23 July 2018

Why doesn't printf / fprintf output anything...

In my build of Colossal Cave to Windows, I had to get rid of getline (as this is a linux function found in <editline/readline.h> and it allocates a string dynamically) by replacing it with fgets. I compiled (under mingw) with no other modifications. And, I played advent.exe for an hour. I was happy.

And then I kicked up my IDE and wrote some node / Javascript to spawn a child_process running my new executable. It should work...

It didn't. My output handler, instead of asking me if I would like instructions, sat there.  Did I fail in launching the process?  I added logging the PID. It looked like it launched. I looked up the PID in taskmgr.  I didn't see it. I looked up the PID in tasklist. There it was. But there was no output.

I searched the internet (using the wrong terms) and found nothing. But then I figured it out. I changed the executable from advent.exe to tasklist.exe. And magic; node dumped out some output... but truncated the last bit. That was the hint.

Programs with pipes work differently than console ttys. Most times you won't notice because a console terminal will by default flush stdout when it hits a newline (on unix at least.) Windows, and as it turns out a few stdio implementations, will buffer any stdout unless fflush is explicitly called or the buffer limit is reached.  This is good.  I/O writes are typically expensive, and if you have a process in the background (or without a terminal) this is a good optimization.

But it is not good if you are expecting a dialog or stream that would flush stdout on the next stdin call. A newline will not flush stdout onWindows (with the stdio implementation under mingw/cygwin.) On unix, you can use stdbuf or unbuffer to make a process not buffer its output. I saw no way to do this in Windows. (There might be, I just didn't see it.)

The solution? Fortunately the getline removal is also my savior. If I flush stdout before calling fgets, advent.exe works the way I expect. But... this won't help me with any and all other Windows commands. I won't always have the code of the command I'm executing (that adds a flush called or setbuf (stdio, null ). When I find it, I'll post...

1 comment:

  1. Each symbol is chosen randomly and the selection of that symbol is not influenced by exterior factors like previous 카지노사이트 outcomes of winning/losing history. Mega Meltdown is a 3-Reel, 1-Line video game with a 6-tier financial institution progressive that gamers won’t be able to|be succesful of|have the flexibility to} get enough of. The Mega Progressive is awarded when the Mega symbol lands on the primary reel in a successful combination during max wager play. An on-line slot recreation works very similarly to a slot machine at a standard casino. You place your wager, and as an alternative of pulling the lever, you click on the spin button and see what the slot machine throws your means. However, it can possibly} turn out to be boring half in} the same games once more, so we’ve singled out one of the best on-line slots and the sites to seek out|to search out} them on.