-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmmap_send.cpp
More file actions
46 lines (35 loc) · 1.14 KB
/
mmap_send.cpp
File metadata and controls
46 lines (35 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <unistd.h>
#include <sys/mman.h>
#include <iostream>
#include <fcntl.h>
#include <cstring>
using namespace std;
typedef struct {
char name[4];
int age;
} STU;
int main(int argc, char* argv[]) {
if(argc != 2) {
cout << "param numbers error!" << endl;
exit(-1);
}
int fd = open(argv[1], O_CREAT | O_RDWR | O_TRUNC, 0666);
// 没有write发生错误的原因是因为mmap不能去扩展一个内容为空的新文件,因为大小为0,所有本没有与之对应的合法的物理页,不能扩展。
lseek(fd, sizeof(STU) * 6 - 1, SEEK_SET);
write(fd, "", 1);
STU *ptr;
// mmap函数会对内存进行初始化
ptr = (STU*)mmap(nullptr, sizeof(STU) * 6, PROT_WRITE, MAP_SHARED, fd, 0);
char ch = 'a';
for(int i = 0; i < 6; i++) {
memcpy((ptr + i)->name, &ch, 1);
(ptr + i)->age = 10 + i;
ch++;
}
munmap(ptr, sizeof(STU) * 6);
ptr = (STU*)mmap(nullptr, sizeof(STU) * 6, PROT_READ, MAP_SHARED, fd, 0);
for(int i = 0; i < 6; i++) {
cout << "name = " << (ptr + i)->name << ", and age = " << (ptr + i)->age << endl;
}
return 0;
}