Guest User

manyamirok break

a guest
Apr 5th, 2015
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.72 KB | None | 0 0
  1. #include <sys/mman.h>
  2. #include <unistd.h>
  3. #include <time.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <fcntl.h>
  9.  
  10. #define SIZE (1024 * 1024 * 100)
  11. #define ITERS (10000)
  12.  
  13. int main(int argc, char** argv) {
  14.     if (argc < 2) {
  15.         fprintf(stderr, "Usage: %s file\n", argv[0]);
  16.         return 1;
  17.     }
  18.  
  19.     srand48(time(NULL));
  20.     int fd = open(argv[1], O_CREAT | O_RDWR, 0600);
  21.  
  22.     char* arr = mmap(NULL, SIZE, PROT_WRITE | PROT_READ, MAP_SHARED, fd, 0);
  23.  
  24.     for (int i = 0; i < ITERS; i++) {
  25.         fprintf(stderr, "Iteration %d\n", i);
  26.         arr[lrand48() % SIZE] = (char) lrand48();
  27.         fsync(fd);
  28.     }
  29.  
  30.     munmap(arr, SIZE);
  31.  
  32.     close(fd);
  33. }
Advertisement
Add Comment
Please, Sign In to add comment