|
@@ -0,0 +1,60 @@
|
|
|
+#include <stdlib.h>
|
|
|
+#include <stdio.h>
|
|
|
+#include <fcntl.h>
|
|
|
+#include <sys/mman.h>
|
|
|
+#include <sys/stat.h>
|
|
|
+#include <time.h>
|
|
|
+#include <unistd.h>
|
|
|
+#define FILE_LENGTH 0x100
|
|
|
+
|
|
|
+/* Возвратить случайное число из диапазона [low, high]. */
|
|
|
+int random_range (unsigned const low, unsigned const high) {
|
|
|
+ unsigned const range = high - low + 1;
|
|
|
+ return low + (int) (((double) range) * rand () / (RAND_MAX + 1.0));
|
|
|
+}
|
|
|
+int main (int argc, char* const argv[]) {
|
|
|
+
|
|
|
+ int fd;
|
|
|
+ // void* file_memory;
|
|
|
+ char *file_memory;
|
|
|
+ /* Инициализируем генератор случайных чисел. */
|
|
|
+ srand (time (NULL));
|
|
|
+
|
|
|
+
|
|
|
+ struct stat statbuf;
|
|
|
+
|
|
|
+ /* Открываем(создаем) файл, достаточно большой, чтобы хранить целое
|
|
|
+ число без знака. */
|
|
|
+ fd = open (argv[1], O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
|
|
|
+
|
|
|
+ fstat(fd, &statbuf);
|
|
|
+
|
|
|
+ // lseek (fd, FILE_LENGTH+1, SEEK_SET);
|
|
|
+ // write (fd, "", 1);
|
|
|
+ // lseek (fd, 0, SEEK_SET);
|
|
|
+
|
|
|
+ /* Создаем отображение в памяти. */
|
|
|
+// file_memory = (char*) mmap (0, statbuf.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
|
|
+ file_memory = (char*) mmap (0, FILE_LENGTH , PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
|
|
+
|
|
|
+ close (fd);
|
|
|
+
|
|
|
+// printf ("size = %d \n", &statbuf.st_size);
|
|
|
+// return 0;
|
|
|
+ int b;
|
|
|
+ // char b = 0;
|
|
|
+
|
|
|
+ for (int i = 0; i < FILE_LENGTH; i+= 0x4 ) {
|
|
|
+
|
|
|
+ b = file_memory [4];
|
|
|
+
|
|
|
+ printf ("I am hear \n i = %i \n", &i);
|
|
|
+ // scanf (file_memory, "%d", &b);
|
|
|
+ printf ("b = %d \n", &b);
|
|
|
+ }
|
|
|
+
|
|
|
+ /* Освобождаем память. */
|
|
|
+ munmap (file_memory, FILE_LENGTH);
|
|
|
+
|
|
|
+return 0;
|
|
|
+}
|