#include <stdio.h>
#include <sys/mman.h>
#include<stdlib.h>
#include<wait.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int main()
{
int fd = open("1.c", O_RDWR);
int size = lseek(fd, 0, SEEK_END);
void *ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (ptr == MAP_FAILED)
{
perror("mmap");
exit(0);
}
pid_t pid = fork();
if (pid > 0)
{
wait(NULL);
char buf[1024] = {0};
strcpy(buf, (char *)ptr);
printf("收到的数据: %s\n", buf);
}
else if (0 == pid)
{
strcpy((char *)ptr, "cao ni ma, er zi!!!");
}
munmap(ptr, size);
return 0;
}
可以看到内容被写到了文件中
|