Skip to content

Instantly share code, notes, and snippets.

@lynzrand
Created July 1, 2025 09:13
Show Gist options
  • Save lynzrand/cb4dba33d53bb0da0dca5487e98831d0 to your computer and use it in GitHub Desktop.
Save lynzrand/cb4dba33d53bb0da0dca5487e98831d0 to your computer and use it in GitHub Desktop.
Simple C shim to reproduce issues caused by non-blocking STDIO
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
void make_nonblocking(int fd) {
int flags = fcntl(fd, F_GETFL, 0);
if (flags == -1) {
perror("fcntl(F_GETFL)");
exit(1);
}
if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) {
perror("fcntl(F_SETFL)");
exit(1);
}
}
int main(int argc, char *argv[]) {
if (argc < 2) {
fprintf(stderr, "Usage: %s <program> [args...]\n", argv[0]);
exit(1);
}
make_nonblocking(STDIN_FILENO);
make_nonblocking(STDOUT_FILENO);
make_nonblocking(STDERR_FILENO);
execvp(argv[1], &argv[1]);
// execvp only returns on error
perror("execvp");
exit(1);
return 0; // Should not be reached
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment