#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <sys/mman.h>
int globalVar;
int main(void)
{
int localVar = 0;
int* p = (int*) mmap(NULL, sizeof (int) , PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_ANONYMOUS, -1, 0);
pid_t childPID = fork();
*p = 0;
if (childPID >= 0)
{
if (childPID == 0)
{
printf("\n Child Process Initial Value :: localVar"
" = %d, globalVar = %d", localVar,
globalVar);
localVar++;
globalVar++;
printf("\n Child Process :: localVar = %d, "
"globalVar = %d", localVar, globalVar);
printf("\n Address of malloced mem child = %p "
"and value is %d", p, *p);
printf("\n lets change the value pointed my malloc");
*p = 50;
printf("\n Address of malloced mem child = %p "
"and value is %d", p, *p);
printf("\n lets change the value pointed my "
"malloc in child");
*p = 200;
printf("\n Address of malloced mem child = %p "
"and value is %d\n\n\n", p, *p);
exit(EXIT_SUCCESS);
} else {
wait(NULL);
printf("\n Parent process Initial Value :: "
"localVar = %d, globalVar = %d",
localVar, globalVar);
localVar = 10;
globalVar = 20;
printf("\n Parent process :: localVar = %d,"
" globalVar = %d", localVar, globalVar);
printf("\n Address of malloced mem parent= %p "
"and value is %d", p, *p);
printf("\n Address of malloced mem parent= %p "
"and value is %d", p, *p);
printf("\n lets change the value pointed my"
" malloc in child");
printf("\n Address of malloced mem child = %p"
" and value is %d \n", p, *p);
}
}
else
{
printf("\n Fork failed, quitting!!!!!!\n");
return 1;
}
return 0;
}