1 Star 0 Fork 0

散漫小主 / NetworkProgramming

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
01shmopen.cpp 1.13 KB
一键复制 编辑 原始数据 按行查看 历史
Idiot 提交于 2019-08-14 18:41 . p37 poxis 线程(二)
//
// 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;
}
1
https://gitee.com/YuXiaoXiang_930/NetworkProgramming.git
git@gitee.com:YuXiaoXiang_930/NetworkProgramming.git
YuXiaoXiang_930
NetworkProgramming
NetworkProgramming
master

搜索帮助