-
-
Notifications
You must be signed in to change notification settings - Fork 638
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bug: printf() isn't working as expected #1256
Comments
cosmos seems to have |
Why they do this?
Is there any way to fix it so that will not be needed?
|
|
With glibc in ttys, stdout is also line buffered. glibc appears to flush stdout when stdin is read from. Personally, I wouldn't depend on this behavior as it doesn't appear to be guaranteed by C, but this may be something we can improve. https://stackoverflow.com/questions/66815208/how-is-printf-getting-flushed-before-scanf-is-executed |
Upon further consideration, I'm not convinced we should change this. musl has good reasoning why they don't: Rich Felker:
https://inbox.vuxu.org/musl/CAAMnvke+NhDbGahX5mbzOpsWEPzPf4K-0dYE-ZMHJTr5xSEGhQ@mail.gmail.com/T/ To reproduce the deadlock condition I made a demo program, save to #define _GNU_SOURCE
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
int main(){
const unsigned pipesize = fcntl(1, F_GETPIPE_SZ);
fprintf(stderr, "pipesize %u\n", pipesize);
// fill up the stdout pipe
static char buf[4096];
for(unsigned i = 0; i < (pipesize / sizeof(buf)); i++) {
write(1, buf, sizeof(buf));
}
// print without flushing
printf("write without flushing");
fprintf(stderr, "the printf itself is harmless\n");
getchar(); // reading should be
fprintf(stderr, "glibc never gets here as we are deadlocked writing to stdout\n");
} Compile and run with cosmocc. cosmocc -o wr_stdio_cosmo wr_stdio.c && ./wr_stdio_cosmo | sleep 10000 After you press ENTER, the final output should look something like:
Now if we do the same with gcc with glibc:
To easily reproduce, |
Contact Details
contact me on here
What happened?
hello.
found a weird issue with printf, I don't know why this happens, but if I compile my example calculator app, it doesn't print to stdout until you fill out all needed stuff, here's the C code.
Version
cosmocc (GCC) 14.1.0
What operating system are you seeing the problem on?
Mac
Relevant log output
No response
The text was updated successfully, but these errors were encountered: