nicer output
[tridge/junkcode.git] / anonbug2.c
1 #include <stdlib.h>
2 #include <unistd.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <sys/mman.h>
6
7 int main(int argc, char *argv[])
8 {
9         volatile int *buf;
10         int size;
11         pid_t child, parent;
12
13         if (argc < 2) {
14                 printf("shm_sample <size>\n");
15                 exit(1);
16         }
17
18         size = atoi(argv[1]);
19
20         buf = (int *)mmap(0, size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED,
21                           -1, 0);
22         if (buf == (int *)-1) {
23                 perror("mmap");
24                 exit(1);
25         }
26
27         parent = getpid();
28         child = fork();
29
30         if (!child) {
31                 buf[0] = parent;
32                 return 0;
33         }
34
35         waitpid(child, 0, 0);
36
37         if (buf[0] != parent) {
38                 printf("memory not shared? (buf[0]=%d parent=%d)\n", buf[0], parent);
39         } else {
40                 printf("shared OK\n");
41         }
42
43         return 0;
44 }
45