xref: /DragonOS/user/apps/test_shm/test_info.c (revision 01c18c64b14b4ebabd98fa92c587c26874275eb1)
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <sys/mman.h>
5 #include <sys/shm.h>
6 #include <sys/ipc.h>
7 #include <string.h>
8 #include <sys/wait.h>
9 
10 void print_shmidds(struct shmid_ds *shminfo)
11 {
12     struct ipc_perm shm_perm = shminfo->shm_perm;
13 
14     printf("ipc_perm:\n");
15     printf("ipc_perm_key: %d\n", shm_perm.__key);
16     printf("uid: %d\n", shm_perm.uid);
17     printf("gid: %d\n", shm_perm.gid);
18     printf("cuid: %d\n", shm_perm.cuid);
19     printf("cgid: %d\n", shm_perm.cgid);
20     printf("mode: %d\n", shm_perm.mode);
21     printf("seq: %d\n", shm_perm.__seq);
22     printf("\n");
23 
24     printf("shmid_ds:\n");
25     printf("shm_atime: %lu\n", shminfo->shm_atime);
26     printf("shm_dtime: %lu\n", shminfo->shm_dtime);
27     printf("shm_ctime: %lu\n", shminfo->shm_ctime);
28     printf("shm_segsz: %lu\n", shminfo->shm_segsz);
29     printf("shm_cpid: %d\n", shminfo->shm_cpid);
30     printf("shm_lpid: %d\n", shminfo->shm_lpid);
31     printf("shm_nattch: %lu\n", shminfo->shm_nattch);
32     printf("\n");
33 }
34 
35 const int SHM_SIZE = 9999;
36 
37 int main()
38 {
39     int shmid;
40     char *shmaddr;
41     key_t key = 6666;
42 
43     // 测试shmget
44     shmid = shmget(key, SHM_SIZE, 0666 | IPC_CREAT);
45     if (shmid < 0)
46     {
47         perror("shmget failed");
48         exit(EXIT_FAILURE);
49     }
50 
51     // 测试shmat
52     shmaddr = shmat(shmid, 0, 0);
53 
54     // IPC_STAT:
55     printf("\n");
56     printf("IPC_STAT:\n");
57     struct shmid_ds shminfo2;
58     if (shmctl(shmid, IPC_STAT, &shminfo2) == -1)
59     {
60         // 获取共享内存段信息
61         perror("shmctl");
62         exit(EXIT_FAILURE);
63     }
64     print_shmidds(&shminfo2);
65 
66     // 测试shmctl
67     // IPC_INFO
68     printf("IPC_INFO:\n");
69     struct shminfo shmmetainfo;
70     if (shmctl(shmid, IPC_INFO, &shmmetainfo) == -1)
71     { // 获取共享内存段信息
72         perror("shmctl");
73         exit(EXIT_FAILURE);
74     }
75     printf("shmmax: %lu\n", shmmetainfo.shmmax);
76     printf("shmmin: %lu\n", shmmetainfo.shmmin);
77     printf("shmmni: %lu\n", shmmetainfo.shmmni);
78     printf("shmseg: %lu\n", shmmetainfo.shmseg);
79     printf("shmall: %lu\n", shmmetainfo.shmall);
80 
81     // SHM_INFO
82     printf("\n");
83     printf("SHM_INFO:\n");
84     struct shm_info shm_info;
85     if (shmctl(shmid, SHM_INFO, &shm_info) == -1)
86     { // 获取共享内存段信息
87         perror("shmctl");
88         exit(EXIT_FAILURE);
89     }
90     printf("__used_ids: %lu\n", shm_info.__used_ids);
91     printf("shm_tot: %lu\n", shm_info.shm_tot);
92     printf("shm_rss: %lu\n", shm_info.shm_rss);
93     printf("shm_swp: %lu\n", shm_info.shm_swp);
94     printf("__swap_attempts: %lu\n", shm_info.__swap_attempts);
95     printf("__swap_successes: %lu\n", shm_info.__swap_successes);
96 
97     // SHM_STAT
98     printf("\n");
99     printf("SHM_STAT:\n");
100     struct shmid_ds shminfo0;
101     if (shmctl(shmid, SHM_STAT, &shminfo0) == -1)
102     { // 获取共享内存段信息
103         perror("shmctl");
104         exit(EXIT_FAILURE);
105     }
106     print_shmidds(&shminfo0);
107 
108     // SHM_STAT_ANY
109     printf("SHM_STAT_ANY:\n");
110     struct shmid_ds shminfo1;
111     if (shmctl(shmid, SHM_STAT_ANY, &shminfo1) == -1)
112     { // 获取共享内存段信息
113         perror("shmctl");
114         exit(EXIT_FAILURE);
115     }
116     print_shmidds(&shminfo1);
117 
118     // IPC_SET
119     printf("\n");
120     printf("IPC_SET:\n");
121     struct shmid_ds shminfo;
122     shminfo.shm_atime = 1;
123     shminfo.shm_dtime = 2;
124     shminfo.shm_ctime = 3;
125     shminfo.shm_segsz = 4;
126     shminfo.shm_cpid = 5;
127     shminfo.shm_lpid = 6;
128     shminfo.shm_nattch = 7;
129     if (shmctl(shmid, IPC_SET, &shminfo) == -1)
130     { // 获取共享内存段信息
131         perror("shmctl");
132         exit(EXIT_FAILURE);
133     }
134 
135     // IPC_RMID
136     printf("\n");
137     printf("IPC_RMID:\n");
138     if (shmctl(shmid, IPC_RMID, NULL) == -1)
139     { // 获取共享内存段信息
140         perror("shmctl");
141         exit(EXIT_FAILURE);
142     }
143 
144     // SHM_LOCK
145     printf("\n");
146     printf("SHM_LOCK:\n");
147     if (shmctl(shmid, SHM_LOCK, NULL) == -1)
148     { // 获取共享内存段信息
149         perror("shmctl");
150         exit(EXIT_FAILURE);
151     }
152 
153     // SHM_UNLOCK
154     printf("\n");
155     printf("SHM_UNLOCK:\n");
156     if (shmctl(shmid, SHM_UNLOCK, NULL) == -1)
157     { // 获取共享内存段信息
158         perror("shmctl");
159         exit(EXIT_FAILURE);
160     }
161 }