Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- 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);
- }
Add Comment
Please, Sign In to add comment