代码拉取完成,页面将自动刷新
//
// Created by jxq on 19-8-14.
//
// p35 poxis共享内存
#include <errno.h>
#include <unistd.h>
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <fcntl.h> /* For O_* constants */
#include <sys/stat.h> /* For mode constants */
#include <mqueue.h>
#include <sys/mman.h>
using namespace std;
#define ERR_EXIT(m) \
do \
{ \
perror(m); \
exit(EXIT_FAILURE); \
} while (0);
struct student
{
char name[32];
int age;
};
int main(int argc, char** argv)
{
int shmid;
shmid = shm_open("/xyz", O_CREAT | O_RDWR, 0666);
//shmid = shm_open("/xyz", O_RDWR, S_IRUSR);
if (shmid == -1)
{
ERR_EXIT("shmid");
}
printf("shmopen success\n");
student stu;
strcpy(stu.name , "hello");
stu.age = 20;
if (ftruncate(shmid, sizeof(stu)) == -1)
{
ERR_EXIT("ftruncate");
}
printf("truncate succ\n");
struct stat buf;
if (fstat(shmid, &buf) == -1)
{
ERR_EXIT("fstat");
}
printf("mode: %o, size: %ld\n", buf.st_mode & 07777, buf.st_size);
close(shmid);
return 0;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。