Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
4
Добавлен:
26.05.2014
Размер:
1.8 Кб
Скачать
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
pid_t GFpid,Fpid,GSpid;
  int GFstatus,Fstatus,GSstatus;
  int input;
int main(int argc, char *argv[])
{
  switch (GFpid=fork())
    {
      case -1:
        printf("fork error while creating Grandfathers proces\n");
        exit(-1);
      case  0:
        printf("Grandfathers process starts executing with PID %d\n",getpid());
        switch(Fpid=fork())
        {
          case -1:
            printf("fork error while creating Fathers proces\n");
            exit(-1);
          case  0:
            printf("Fathers process starts executing with PID %d\n",getpid());
            switch(GSpid=fork())
            {
              case -1:
                printf("fork error while creating GrandSons proces\n");
                exit(-1);
              case  0:
                printf("GrandSon process starts executing with PID %d\n",getpid());
                for(;;)
                  {
                     sleep(2);
                     printf("Close GrandSon Process?");
                     input=getchar();
                     printf("\n");
                     if (input=='y')
                        exit(6);
                  }  
              default:
                GSpid=wait(&GSstatus);
                printf("GrandSon with PID %d exited with status %d\n",GSpid,GSstatus>>8);
                exit(5);
                
            }
          default:
            Fpid=wait(&Fstatus);
            printf("Father with PID %d exited with status %d\n",Fpid,Fstatus>>8);
            exit(4);
        }
      default:
         GFpid=wait(&GFstatus);
         printf("GrandFather with PID %d exited with status %d\n",GFpid,GFstatus>>8);
        
    }
  
  return EXIT_SUCCESS;
}
Соседние файлы в папке lab1
  • #
    26.05.20141.8 Кб41.c
  • #
    26.05.201412.46 Кб4a.out