//mmap.c
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <string.h>
int main(int argc, char const *argv[])
{
int fd = open("mem.txt", O_RDWR | O_CREAT | O_TRUNC, 0644); //创建并截断文件
//int fd = open("mem.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644); //Permission denied
ftruncate(fd, 20); //open的时候,可以创建一个文件来创建映射区,但前提是文件大小不能为0
//创建映射区
char *mem = mmap(NULL, 20, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
//offset必须是4k的整数倍
//char *mem = mmap(NULL, 5, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 4096);
//char *mem = mmap(NULL, 5, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
if (mem == MAP_FAILED)
{
perror("mmap err!");
return -1;
}
strcpy(mem, "hello world");
//close(fd); /如果文件描述符先关闭,对mmap映射没有影响,因为映射通道已经通了
//mem++; //不能改变地址,否则无法释放
//释放
int ret = munmap(mem, 20);
if (ret < 0)
{
perror("munmap error!");
}
close(fd);
return 0;
}
//mmap_fork.c
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <fcntl.h>
int main(int argc, char const *argv[])
{
//先创建映射区
int fd = open("mem.txt", O_RDWR);
int *mem = mmap(NULL, 4, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
//int *mem = mmap(NULL, 4, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
if (mem == MAP_FAILED)
{
perror("mmap error!");
return -1;
}
//fork子进程
pid_t pid = fork();
//父进程和子进程交替修改数据
if (pid < 0)
{
perror("fork error!");
return -1;
}
else if (pid == 0)
{
*mem = 100;
printf("child, *mem = %d\n", *mem);
sleep(3);
printf("child, *mem = %d\n", *mem);
}
else if (pid > 0)
{
sleep(1);
printf("parent, *mem = %d\n", *mem);
*mem = 1001;
printf("parent, *mem = %d\n", *mem);
wait(NULL);
}
int ret = munmap(mem, 4);
if (ret < 0)
{
perror("munmap failed!");
return -1;
}
close(fd);
return 0;
}
//mmpa_anon.c
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <fcntl.h>
int main(int argc, char const *argv[])
{
int *mem = mmap(NULL, 4, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
if (mem == MAP_FAILED)
{
perror("mmap error!");
return -1;
}
//fork子进程
pid_t pid = fork();
//父进程和子进程交替修改数据
if (pid < 0)
{
perror("fork error!");
return -1;
}
else if (pid == 0)
{
*mem = 100;
printf("child, *mem = %d\n", *mem);
sleep(3);
printf("child, *mem = %d\n", *mem);
}
else if (pid > 0)
{
sleep(1);
printf("parent, *mem = %d\n", *mem);
*mem = 1001;
printf("parent, *mem = %d\n", *mem);
wait(NULL);
}
int ret = munmap(mem, 4);
if (ret < 0)
{
perror("munmap failed!");
return -1;
}
return 0;
}
//mmap_read.c
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <fcntl.h>
typedef struct Student
{
int sid;
char sname[20];
}Student;
int main(int argc, char const *argv[])
{
if (argc != 2)
{
printf("./a.out filename\n");
return -1;
}
//1.open file
int fd = open(argv[1], O_RDWR, 0666);
int length = sizeof(Student);
//2.mmap
Student *stu = mmap(NULL, length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (stu == MAP_FAILED)
{
perror("map error!");
return -1;
}
//3.修改内存数据, read
while (1)
{//每隔一秒读一次映射区的内容
printf("sid = %d, sname = %s\n", stu->sid, stu->sname);
sleep(1);
}
//4.释放映射区和关闭描述符
int ret = munmap(stu, length);
if (ret < 0)
{
perror("munmap failed!");
return -1;
}
close(fd);
return 0;
}
//mmap_write.c
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <fcntl.h>
typedef struct Student
{
int sid;
char sname[20];
}Student;
int main(int argc, char const *argv[])
{
if (argc != 2)
{
printf("./a.out filename\n");
return -1;
}
//1.open file
int fd = open(argv[1], O_CREAT | O_RDWR | O_TRUNC, 0666);
int length = sizeof(Student);
ftruncate(fd, length);
//2.mmap
Student *stu = mmap(NULL, length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (stu == MAP_FAILED)
{
perror("map error!");
return -1;
}
//3.修改内存数据, write
int num = 1;
while (1)
{//每隔一秒修改一次映射区的内容
stu->sid = num;
sprintf(stu->sname, "LiHua-%03d", num++);
sleep(1);
}
//4.释放映射区和关闭描述符
int ret = munmap(stu, length);
if (ret < 0)
{
perror("munmap failed!");
return -1;
}
close(fd);
return 0;
}