Guest User

Untitled

a guest
Nov 13th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. /*
  2. 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
  3. */
  4.  
  5. #include <stdlib.h>
  6. #include <unistd.h>
  7. #include <sys/wait.h>
  8. #include <stdio.h>
  9. #include <fcntl.h>
  10. #include <string.h>
  11.  
  12. void recode(char*, int, int);
  13.  
  14. int
  15. main(int argc, char *argv[]) {
  16. int buf_size = atoi(argv[1]);
  17. int line_len = atoi(argv[2]);
  18. int buf_mode;
  19. if (strcmp(argv[3], "--nbf") == 0) {
  20. buf_mode = _IONBF;
  21. } else if (strcmp(argv[3], "--lbf") == 0) {
  22. buf_mode = _IOLBF;
  23. } else if (strcmp(argv[3], "--fbf") == 0) {
  24. buf_mode = _IOFBF;
  25. } else {
  26. printf("input buf mode!");
  27. _exit(1);
  28. }
  29.  
  30. int j;
  31. char line[line_len];
  32. for (j=0; j<(line_len-1); j++) line[j] = 'x';
  33. line[j++] = '\n';
  34. line[j++] = '\0';
  35.  
  36. for (int i=0; i<10; i++) {
  37. if (fork() == 0) {
  38. recode(line, buf_mode, buf_size);
  39. _exit(0);
  40. } else {
  41. continue;
  42. }
  43. }
  44. for (int i=0; i<10; i++) {
  45. wait(NULL);
  46. }
  47. }
  48.  
  49. void
  50. recode(char *line, int buf_size, int buf_mode) {
  51. FILE *out;
  52. char buf[buf_size];
  53.  
  54. out = fopen("./out.txt", "a");
  55. setvbuf(out, buf, buf_mode, buf_size);
  56. for (int i=0; i<1000; i++) {
  57. fputs(line, out);
  58. }
  59. fclose(out);
  60. }
Add Comment
Please, Sign In to add comment