-
Notifications
You must be signed in to change notification settings - Fork 44
/
02shmunlink.cpp
71 lines (57 loc) · 1.23 KB
/
02shmunlink.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//
// 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);
if (shm_unlink("/xyz") == -1)
{
ERR_EXIT("shmunlink");
}
printf("shm_unlink succ\n");
return 0;
}