Скачиваний:
5
Добавлен:
26.05.2014
Размер:
953 б
Скачать
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/user.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
char ch;
int pid1,pid2,dead1,dead2,status1,status2;
system("clear");
printf("Hellow\nI'm a grand dad\nMy pid is %d\n\n",getpid());

switch(pid1=fork())
{
case -1: printf("Error with fork grand=>dad\n\n");exit(-1);
case  0:
/**/
printf("Hellow\nI'm a dad\nMy pid is %d\n\n",getpid());
switch(pid2=fork())
{
case -1: printf("Error with fork dad=>child\n");exit(-1);
case  0:
/**/
printf("Hellow\nI'm a child\nMy pid is %d\nIf you like to exit , you should press any key \n",getpid());
ch=getchar();
while (!ch)
{
ch=getchar();
}
/**/
exit(2);
default: dead2=wait(&status2);
  }

/**/
printf("Child is dead \nHe's pid was %d\nAnd he say %d\n\n",dead2,status2>>8);
exit(1);
default: dead1=wait(&status1);
  }
printf("Dad is dead\nHe's pid was %d\nAnd he say %d\n",dead1,status1>>8);
return 0;
}