Created
January 23, 2011 19:03
-
-
Save jotaki/792326 to your computer and use it in GitHub Desktop.
Testing if fsync(dup(1)) flushes stdout!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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