This source file includes following definitions.
- main
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 #include <stdlib.h>
32 #include <stdio.h>
33 #include <sys/types.h>
34 #include <sys/ipc.h>
35 #include <sys/shm.h>
36 #include <sys/mman.h>
37
38 #ifndef SHM_HUGETLB
39 #define SHM_HUGETLB 04000
40 #endif
41
42 #define LENGTH (256UL*1024*1024)
43
44 #define dprintf(x) printf(x)
45
46
47 #ifdef __ia64__
48 #define ADDR (void *)(0x8000000000000000UL)
49 #define SHMAT_FLAGS (SHM_RND)
50 #else
51 #define ADDR (void *)(0x0UL)
52 #define SHMAT_FLAGS (0)
53 #endif
54
55 int main(void)
56 {
57 int shmid;
58 unsigned long i;
59 char *shmaddr;
60
61 shmid = shmget(2, LENGTH, SHM_HUGETLB | IPC_CREAT | SHM_R | SHM_W);
62 if (shmid < 0) {
63 perror("shmget");
64 exit(1);
65 }
66 printf("shmid: 0x%x\n", shmid);
67
68 shmaddr = shmat(shmid, ADDR, SHMAT_FLAGS);
69 if (shmaddr == (char *)-1) {
70 perror("Shared memory attach failure");
71 shmctl(shmid, IPC_RMID, NULL);
72 exit(2);
73 }
74 printf("shmaddr: %p\n", shmaddr);
75
76 dprintf("Starting the writes:\n");
77 for (i = 0; i < LENGTH; i++) {
78 shmaddr[i] = (char)(i);
79 if (!(i % (1024 * 1024)))
80 dprintf(".");
81 }
82 dprintf("\n");
83
84 dprintf("Starting the Check...");
85 for (i = 0; i < LENGTH; i++)
86 if (shmaddr[i] != (char)i) {
87 printf("\nIndex %lu mismatched\n", i);
88 exit(3);
89 }
90 dprintf("Done.\n");
91
92 if (shmdt((const void *)shmaddr) != 0) {
93 perror("Detach failure");
94 shmctl(shmid, IPC_RMID, NULL);
95 exit(4);
96 }
97
98 shmctl(shmid, IPC_RMID, NULL);
99
100 return 0;
101 }