/* mmapzero.c - Using and mmap to create shared memory between two processes.
	The parent process uses mmap on /dev/zero.
	The child process writes to the shared region and exits.
	The parent is waken up from wait and prints out what writte by child
	The output is:
child: roses are red
parent: roses are red

*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

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

int main() {
    int pid;
    char * p;
    p = mmap(0, 128, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
    if (p < 0) {
	perror("error with mmap\n");
	exit(-1);
    }
    pid = fork();
    if (pid < 0) {
	perror("error with fork\n");
	exit(-1);
    } else if (pid == 0) {
	strcpy(p, "roses are red");
	printf("child: %s\n", p);
	exit(0);
    } 
    wait(0);
    printf("parent: %s\n", p);
    return 0;
}

