Skip to content

Instantly share code, notes, and snippets.

@jschwinger233
Last active November 13, 2018 15:42
Show Gist options
  • Save jschwinger233/98611b149832c1dc5b5ce05741b1caa8 to your computer and use it in GitHub Desktop.
Save jschwinger233/98611b149832c1dc5b5ce05741b1caa8 to your computer and use it in GitHub Desktop.
/*
rm -f /tmp/out.txt && gcc cwrite.c && ./a.out $BUFSIZE $LINELEN --fbf && cat out.txt | while read l; do echo ${#l}; done | sort -u
*/
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
void recode(char*, int, int);
int
main(int argc, char *argv[]) {
int buf_size = atoi(argv[1]);
int line_len = atoi(argv[2]);
int buf_mode;
if (strcmp(argv[3], "--nbf") == 0) {
buf_mode = _IONBF;
} else if (strcmp(argv[3], "--lbf") == 0) {
buf_mode = _IOLBF;
} else if (strcmp(argv[3], "--fbf") == 0) {
buf_mode = _IOFBF;
} else {
printf("input buf mode!");
_exit(1);
}
int j;
char line[line_len];
for (j=0; j<(line_len-1); j++) line[j] = 'x';
line[j++] = '\n';
line[j++] = '\0';
for (int i=0; i<10; i++) {
if (fork() == 0) {
recode(line, buf_mode, buf_size);
_exit(0);
} else {
continue;
}
}
for (int i=0; i<10; i++) {
wait(NULL);
}
}
void
recode(char *line, int buf_size, int buf_mode) {
FILE *out;
char buf[buf_size];
out = fopen("./out.txt", "a");
setvbuf(out, buf, buf_mode, buf_size);
for (int i=0; i<1000; i++) {
fputs(line, out);
}
fclose(out);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment