Advertisement
GoralWMoro

Untitled

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