代码拉取完成,页面将自动刷新
//
// Created by jxq on 19-8-12.
//
// p28 共享内存介绍(一)
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <cstring>
#include <fcntl.h>
#include <sys/mman.h>
#define ERR_EXIT(m) \
do \
{ \
perror(m); \
exit(EXIT_FAILURE); \
} while (0);
typedef struct stu
{
char name[4];
int age;
} STU;
int main(int argc, char** argv)
{
int fd;
fd = open(argv[1], O_RDWR);
if (fd == -1)
{
ERR_EXIT("open");
}
STU *p;
p = (STU*)mmap(NULL, sizeof(STU) * 6, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);;
if (p == NULL)
{
ERR_EXIT("mmap");
}
char ch = 'a';
int i;
for (int i = 0; i < 6; ++i)
{
printf("student's name = %s, age = %d\n", (p+i)->name, (p+i)->age);
// memcpy((p+i)->name, &ch, 1);
// (p+i)->age = 20 + i;
// ++ch;
}
munmap(p, sizeof(STU)*6);
printf("exit...\n");
return 0;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。