Skip to content

Instantly share code, notes, and snippets.

@jotaki
Created January 23, 2011 19:03
Show Gist options
  • Save jotaki/792326 to your computer and use it in GitHub Desktop.
Save jotaki/792326 to your computer and use it in GitHub Desktop.
Testing if fsync(dup(1)) flushes stdout!
/* testflush.c
* Expected Output:
* Goodbye World!Hello World!
*
* Testing for Output:
* Hello World!Goodbye World!
*
* Actual Output:
* Goodbye World!Hello World!
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
int fsync(int fd);
void xwrite(int fd, const char *buf)
{
if(write(fd, buf, strlen(buf)) < 0) {
perror(buf);
exit(errno);
}
}
int main()
{
int fd;
fd = dup(1);
if(fd < 0) {
perror("Failed to duplicate stdout");
return errno;
}
xwrite(1, "Goodbye, World!");
xwrite(fd, "Hello, World!");
fsync(fd);
fsync(1);
close(fd);
xwrite(1, "\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment