AmidamaruZXC

Untitled

May 15th, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.90 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <dirent.h>
  3. #include <unistd.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <limits.h>
  7. #include <fcntl.h>
  8. #include <sys/mman.h>
  9.  
  10. int main(void)
  11. {
  12.     int fd;
  13.     size_t length;
  14.     int i;
  15.  
  16.     struct A {
  17.         double f;
  18.         double f2;
  19.     } *ptr, *tmpptr;
  20.  
  21.     fd = open("mapped.dat", O_RDWR, 0666);
  22.  
  23.     if( fd == -1){
  24.         printf("File open failed!\n");
  25.         exit(1);
  26.     }
  27.  
  28.     length = 100000*sizeof(struct A);
  29.  
  30.     ptr = (struct A *)mmap(NULL, length, PROT_WRITE | PROT_READ, MAP_SHARED, fd, 0);
  31.  
  32.     close(fd);
  33.  
  34.     if( ptr == MAP_FAILED ){
  35.         printf("Mapping failed!\n");
  36.         exit(2);
  37.     }
  38.  
  39.     tmpptr = ptr;
  40.  
  41.     double sum = 0;
  42.  
  43.     for(i = 1; i <=100000; i++){
  44.         sum += tmpptr->f2;
  45.         tmpptr++;
  46.     }
  47.  
  48.     printf("Сумма = %f\n", sum);
  49.  
  50.     munmap((void *)ptr, length);
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment