tweaks
[tridge/junkcode.git] / anonbug.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         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, -1, 0);
21         if (buf == (int *)-1) {
22                 perror("mmap failed\n");
23                 exit(1);
24         }
25
26         buf[0] = -1;
27         parent = getpid();
28
29         child = fork();
30         if (child == -1) {
31                 perror("fork");
32                 exit(1);
33         }
34
35         if (child == 0) {
36                 buf[0] = parent;
37                 return 0;
38         }
39
40         waitpid(child, 0, 0);
41
42         if (buf[0] != parent) {
43                 printf("memory not shared? (%d != %d)\n", buf[0], parent);
44         } else {
45                 printf("shared memory OK (%d == %d)\n", buf[0], parent);
46         }
47
48         return 0;
49 }