#include <stdio.h>
#include <unistd.h>

int main(int argc, char **argv)
{
	pid_t parentpid,
		childpid;
	int pipefds[2];

	if ((parentpid = fork()) == 0)
	{
		parentpid = getpid();
		pipe(pipefds);

		if ((childpid = fork()) == 0)
		{
			char c;
			int rc;

			close(pipefds[1]);

			/* Wait for pipe to close */
			printf ("read() returned %d\n", read(pipefds[0], &c, 1));
			while (kill(parentpid, 0) == 0)
			{
				printf("kill() says the parent is still alive\n");
				sleep(1);
			}
			printf("kill() confirms that the parent is dead\n");
		}
		else
		{
			sleep(3);
			printf("parent exits now\n");
		}
	}
	else
	{
		printf("grandparent is sleeping\n");
		sleep(6);
		printf("grandparent exits now\n");
	}
}
