• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Tests simple manipulation with a shared memory segment. */
2 
3 #include <stdio.h>
4 #include <sys/shm.h>
5 
6 #define SEGMENT_SIZE 167
7 
main(int argc,const char * argv[])8 int main(int argc, const char *argv[])
9 {
10    int shmid = shmget(IPC_PRIVATE, SEGMENT_SIZE, IPC_CREAT | SHM_R | SHM_W);
11    if (shmid < 0) {
12       perror("shmget()");
13       return 1;
14    }
15 
16    void *addr = shmat(shmid, NULL, 0);
17    if (addr == (void *) -1) {
18       perror("shmat()");
19       return 2;
20    }
21 
22    struct shmid_ds stats;
23    int ret = shmctl(shmid, IPC_STAT, &stats);
24    if (ret != 0) {
25       perror("shmctl(IPC_STAT)");
26       return 3;
27    }
28 
29    printf("segment size: %zu\n", stats.shm_segsz);
30 
31    ret = shmdt(addr);
32    if (ret != 0) {
33       perror("shmdt()");
34       return 4;
35    }
36 
37    ret = shmctl(shmid, IPC_RMID, NULL);
38    if (ret != 0) {
39       perror("shmctl(IPC_RMID)");
40       return 5;
41    }
42 
43    return 0;
44 }
45