update
Signed-off-by: Wenchao Hao <haowenchao@xiaomi.com>
This commit is contained in:
parent
7caf6bea2a
commit
6a5f0990b0
44
demo.c
44
demo.c
|
|
@ -1,25 +1,51 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
#define MB_BYTES (1024*1024)
|
||||
|
||||
int main(void)
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
unsigned long *p;
|
||||
unsigned long *m;
|
||||
int i = 0;
|
||||
int nr_mb;
|
||||
int bytes;
|
||||
int max;
|
||||
|
||||
printf("please specify how many memory to alloc\n");
|
||||
scanf("%d", &nr_mb);
|
||||
const char *file_path = argv[1];
|
||||
int fd = open(file_path, O_RDONLY);
|
||||
if (fd == -1) {
|
||||
perror("open");
|
||||
return 1;
|
||||
}
|
||||
|
||||
bytes = nr_mb * MB_BYTES;
|
||||
max = bytes / 8;
|
||||
p = malloc(bytes);
|
||||
struct stat sb;
|
||||
if (fstat(fd, &sb) == -1) {
|
||||
perror("fstat");
|
||||
close(fd);
|
||||
return 1;
|
||||
}
|
||||
|
||||
size_t file_size = sb.st_size;
|
||||
|
||||
printf("%ld, %d Kb\n", file_size, file_size/1024);
|
||||
void *mapped = mmap(NULL, file_size, PROT_READ, MAP_PRIVATE, fd, 0);
|
||||
if (mapped == MAP_FAILED) {
|
||||
perror("mmap");
|
||||
close(fd);
|
||||
return 1;
|
||||
}
|
||||
m = mapped;
|
||||
|
||||
max = file_size / 8;
|
||||
p = malloc(file_size);
|
||||
|
||||
for (i = 0; i < max; i++)
|
||||
*(p + i) = i;
|
||||
*(p + i) = *(m + i);
|
||||
|
||||
while(1);
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue