GoralWMoro

Untitled

Dec 8th, 2019
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.17 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/wait.h>
  4. #include <stdlib.h>
  5.  
  6.  
  7. int main() {
  8.     pid_t potomki[10];
  9.     switch (potomki[1] = fork()) {
  10.         case 0:
  11. /*proces potomny */
  12.             switch(potomki[2] = fork()){
  13.                 case 0:
  14.                 {
  15. //proces P4
  16.                     switch(potomki[4] = fork()){
  17.                         case 0:
  18.                         {
  19.  
  20.                             printf("P4:%d:%d\n", getpid(), getppid());
  21.                             exit(0);
  22.                         }
  23.                         case -1:
  24.                             printf("Blad funkcji fork");
  25.                             exit(1);
  26.                         default:
  27.                             wait(NULL); //oczekiwanie na zakończenie procesu potomnego
  28.                     }
  29. //proces P5
  30.                     switch(potomki[5] = fork()){
  31.                         case 0:
  32.                         {
  33.  
  34.                             printf("P5:%d:%d\n", getpid(), getppid());
  35.                             exit(0);
  36.                         }
  37.                         case -1:
  38.                             printf("Blad funkcji fork");
  39.                             exit(1);
  40.                         default:
  41.                             wait(NULL); //oczekiwanie na zakończenie procesu potomnego
  42.                     }
  43. //proces P6
  44.                     switch(potomki[6] = fork()){
  45.                         case 0:
  46.                         {
  47. //proces P10
  48.                             switch(potomki[10] = fork()){
  49.                                 case 0:
  50.                                 {
  51.  
  52.                                     printf("P10:%d:%d\n", getpid(), getppid());
  53.                                     exit(0);
  54.                                 }
  55.                                 case -1:
  56.                                     printf("Blad funkcji fork");
  57.                                     exit(1);
  58.                                 default:
  59.                                     wait(NULL); //oczekiwanie na zakończenie procesu potomnego
  60.                             }
  61.                             printf("P6:%d:%d\n", getpid(), getppid());
  62.                             exit(0);
  63.                         }
  64.                         case -1:
  65.                             printf("Blad funkcji fork");
  66.                             exit(1);
  67.                         default:
  68.                             wait(NULL); //oczekiwanie na zakończenie procesu potomnego
  69.                     }
  70.                     printf("P2:%d:%d:%d\n", getpid(), getppid(), potomki[9]);
  71.                     exit(0);
  72.                 }
  73.                 case -1:
  74.                     printf("Blad funkcji fork");
  75.                     exit(1);
  76.                 default:
  77.                     wait(NULL); //oczekiwanie na zakończenie procesu potomnego
  78.  
  79.             }
  80.  
  81.  
  82.             switch(potomki[3] = fork()){
  83.                 case 0:
  84.                 {
  85. //proces P7
  86.                     switch(potomki[7] = fork()){
  87.                         case 0:
  88.                         {
  89.                             printf("P7:%d:%d\n", getpid(), getppid());
  90.                             exit(0);
  91.                         }
  92.                         case -1:
  93.                             printf("Blad funkcji fork");
  94.                             exit(1);
  95.                         default:
  96.                             wait(NULL); //oczekiwanie na zakończenie procesu potomnego
  97.                     }
  98. //proces P8
  99.                     switch(potomki[8] = fork()){
  100.                         case 0:
  101.                         {
  102.                             printf("P8:%d:%d\n", getpid(), getppid());
  103.                             exit(0);
  104.                         }
  105.                         case -1:
  106.                             printf("Blad funkcji fork");
  107.                             exit(1);
  108.                         default:
  109.                             wait(NULL); //oczekiwanie na zakończenie procesu potomnego
  110.                     }
  111. //proces P9
  112.                     switch(potomki[9] = fork()){
  113.                         case 0:
  114.                         {
  115.                             printf("P9:%d:%d\n", getpid(), getppid());
  116.                             exit(0);
  117.                         }
  118.                         case -1:
  119.                             printf("Blad funkcji fork");
  120.                             exit(1);
  121.                         default:
  122.                             wait(NULL); //oczekiwanie na zakończenie procesu potomnego
  123.                     }
  124.  
  125.                     printf("P3:%d:%d:%d\n", getpid(), getppid(), potomki[9]);
  126.                     exit(0);
  127.                 }
  128.                 case -1:
  129.                     printf("Blad funkcji fork");
  130.                     exit(1);
  131.                 default:
  132.                     wait(NULL); //oczekiwanie na zakończenie procesu potomnego
  133.             exit(0);
  134.             }
  135.  
  136.         case -1:
  137.             printf("Blad funkcji fork\n");
  138.             exit(1);
  139.  
  140.         default:
  141. /* proces macierzysty*/
  142.             printf("PM1: pid przodka: %d ppid:  %d\n",
  143.                    getpid(), getppid());
  144. /*oczekiwanie na zakonczenie potomka*/
  145.             wait(NULL);
  146.             exit(0);
  147.     }
  148. };
Advertisement
Add Comment
Please, Sign In to add comment