1 /* Copyright (C) 2009 Mikael Lund Jepsen <mlj@iccc.dk>
3 * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
12 #include <sys/types.h>
17 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
19 char shared_name
[] = "/sharetest";
20 int test_data
[11] = {0,1,2,3,4,5,6,7,8,9,10};
26 int test_data_fails
= 0;
34 switch(pid
= fork()) {
37 exit(1); /* parent exits */
43 read(pfds
[0], buf
, 5);
45 fd
= shm_open(shared_name
, O_RDWR
, DEFFILEMODE
);
47 perror("CHILD - shm_open(existing):");
50 ptest_data
= mmap(0, sizeof(test_data
), PROT_READ
+ PROT_WRITE
, MAP_SHARED
, fd
, 0);
51 if (ptest_data
!= MAP_FAILED
) {
52 for (i
=0; i
< ARRAY_SIZE(test_data
); i
++) {
53 if (ptest_data
[i
] != test_data
[i
]) {
54 printf("%-40s: Offset %d, local %d, shm %d\n", "Compare memory error", i
, test_data
[i
], ptest_data
[i
]);
58 if (test_data_fails
== 0)
59 printf("%-40s: %s\n", "Compare memory", "Success");
61 munmap(ptest_data
, sizeof(test_data
));
68 fd
= shm_open(shared_name
, O_RDWR
+O_CREAT
+O_EXCL
, DEFFILEMODE
);
70 perror("PARENT - shm_open(create):");
72 if ((ftruncate(fd
, sizeof(test_data
))) == -1)
74 printf("%-40s: %s", "ftruncate", strerror(errno
));
75 shm_unlink(shared_name
);
79 ptest_data
= mmap(0, sizeof(test_data
), PROT_READ
+ PROT_WRITE
, MAP_SHARED
, fd
, 0);
80 if (ptest_data
== MAP_FAILED
)
82 perror("PARENT - mmap:");
83 if (shm_unlink(shared_name
) == -1) {
84 perror("PARENT - shm_unlink:");
88 for (i
=0; i
< ARRAY_SIZE(test_data
); i
++)
89 ptest_data
[i
] = test_data
[i
];
92 write(pfds
[1], "rdy", 5);
97 munmap(ptest_data
, sizeof(test_data
));
98 if (shm_unlink(shared_name
) == -1) {
99 perror("PARENT - shm_unlink:");