Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/types.h>
- #include <dirent.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <limits.h>
- #include <fcntl.h>
- #include <sys/mman.h>
- int main(void)
- {
- int fd;
- size_t length;
- int i;
- struct A {
- double f;
- double f2;
- } *ptr, *tmpptr;
- fd = open("mapped.dat", O_RDWR, 0666);
- if( fd == -1){
- printf("File open failed!\n");
- exit(1);
- }
- length = 100000*sizeof(struct A);
- ptr = (struct A *)mmap(NULL, length, PROT_WRITE | PROT_READ, MAP_SHARED, fd, 0);
- close(fd);
- if( ptr == MAP_FAILED ){
- printf("Mapping failed!\n");
- exit(2);
- }
- tmpptr = ptr;
- double sum = 0;
- for(i = 1; i <=100000; i++){
- sum += tmpptr->f2;
- tmpptr++;
- }
- printf("Сумма = %f\n", sum);
- munmap((void *)ptr, length);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment