#include<stdio.h>
#include<stdlib.h>
int main() 
{
  int pid,pid1,pid2,pid3,ppid1,ppid2,ppid3,p1,p2,p3,status,i,number;
  int ppgr,p1gr,p2gr,p3gr;
  pid=getpid();
  ppgr=getpgrp();
  printf("group id of the parent process = %d\n",ppgr);
  printf("parents pid = %d\n",pid);
  p1=fork();
  if(p1==0) {
    pid1=getpid();
    printf("child 1s pid = %d\n",pid1);
    ppid1=getppid();
    printf("child 1s parent pid = %d\n",ppid1);
    p2=fork();
    p1gr=getpgrp();
    printf("group id of the 1st child = %d\n",p1gr);
    if(p2==0) {
      pid2=getpid();
      printf("child 2s pid = %d\n",pid2);
      ppid2=getppid();
      printf("child 2s parent pid = %d\n",ppid2);
      p2gr=getpgrp();
      printf("group id of the 1st child = %d\n",p2gr);
      p3=fork();
      if(p3==0) {
	pid3=getpid();
	printf("child 3s pid = %d\n",pid3);
	ppid3=getppid();
	printf("child 3s parent pid = %d\n",ppid3);
	p3gr=getpgrp();
	printf("group id of the 1st child = %d\n",p3gr);
	sleep(10);
	exit(15);
      }
      exit(10);
    }
    exit(5);
  }
  waitpid(p1,&status,0);
  printf("Child1 is returned by %d\n",status>>8);
  waitpid(p2,&status,0);
  printf("Child2 is returned by %d\n",status>>8);
  waitpid(p3,&status,0);
  printf("Child3 is returned by %d\n",status>>8);
  return 0;
}
