#include
#include
#include
#include
#include
#include
int
main (int argc, char *argv[])
{
pid_t cpid;
int *data = NULL;
int fd;
fd = open (“/dev/zero“, O_RDWR);
if (fd < 0) {
perror (“open /dev/zero“);
exit(1);
}
data = (void *) mmap (0, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (data == MAP_FAILED) {
perror (“mmap“);
exit(2);
}
*data = 0;
close (fd);
cpid = fork ();
if (cpid == 0)
{
for (;;)
{
sleep (1);
printf (“Child-0: %dn“, *data);
}
} else if (cpid > 0)
{
cpid = fork ();
if (cpid == 0)
{
for (;;)
{
sleep (1);
(*data)++;
printf (“Child-1: %dn“, *data);
}
} else if (cpid > 0)
{
for (;;)
{
sleep (1);
printf (“Parent-0: %dn“, *data);
}
}
}
munmap (data, 4096);
return 0;
}