/* A simple demo program that shares memory between a parent 
   and a child process.
   Written by Andre M. Maier, dhbw@andre-maier.com */

#include <unistd.h>
#include <stdio.h>
#include <sys/mman.h>
#include <wait.h>

int main(int argc, char **argv)
{
   int *shared = mmap(NULL,_SC_PAGESIZE,PROT_READ|PROT_WRITE,
                 MAP_SHARED|MAP_ANONYMOUS, -1, 0);

   *shared = 1;
   int unshared = 2; // Variable that is unique to each process

   if(fork()==0)
   {
      // Child process overwrites both variables
      unshared = 3;
      *shared = 42;
   }
   else
   {
      // Let parent process wait until child process has finished.
      wait(NULL);
   }
   printf("PID %d unshared=%d *shared=%d\n", getpid(), unshared, *shared);
   return 0;
}
