完善 mmap 示例程序 .

Signed-off-by: rick.chan <chenyang@autoai.com>
This commit is contained in:
rick.chan 2020-12-22 10:02:59 +08:00
parent ca1dea0ab7
commit 1434a540c5
1 changed files with 25 additions and 1 deletions

View File

@ -8,8 +8,32 @@
* @copyright Copyright (c) 2020 * @copyright Copyright (c) 2020
* *
*/ */
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <sys/user.h>
#include <errno.h>
#include <string.h>
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
char* buf;
int fd = open("demo_file_name", O_RDWR);
buf = (char*)mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_LOCKED, fd, 0);
if(NULL==buf | MAP_FAILED==buf) {
printf("%s\n", strerror(errno));
}
buf[0] = 'H';
buf[1] = 'e';
buf[2] = 'l';
buf[3] = 'l';
buf[4] = 'o';
buf[5] = '\0';
printf("%s", buf);
munmap(buf, PAGE_SIZE); // 释放内存
close(fd);
return 0;
} }