Advertisement
melnikovmaxim

BKV_process_tree

Dec 16th, 2019
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.14 KB | None | 0 0
  1. //have bug, zombie processes remain, did not want to fix
  2. //link https://yadi.sk/d/dd5ZH_45EJ217w
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <signal.h>
  6. #include <unistd.h>
  7. #include <stdlib.h>
  8. #include <sys/types.h>
  9. #include <sys/wait.h>
  10.  
  11. pid_t pids[100];
  12. int i = 0;
  13. int n = 0;
  14. pid_t pid_target;
  15.  
  16. int commander(char *command, pid_t *pid){
  17.     char *lec;
  18.     lec = strtok(command, " \n");
  19.     if(!lec) return -1;
  20.     if(!strcmp(lec, "k")){
  21.         lec = strtok(NULL, " \n");
  22.         if(!lec) return -1;
  23.         *pid = atoi(lec);
  24.         if(*pid <= 0) return -1;
  25.         return 1;
  26.     }
  27.     if(!strcmp(lec, "p")) return 2;
  28.     if(!strcmp(lec, "x")) return 3;
  29.     return -1;
  30. }
  31.  
  32. void printPid(int c){
  33.     int k;
  34.     printf("Процесс PID %d  PPID %d \n", getpid(), getppid());
  35.     for(k = 0; k < n; k++)
  36.         kill(pids[k], SIGUSR1);
  37. }
  38.  
  39. void killAll(int c){
  40.     int k, ws;
  41.     printf("Процесс PID %d был убит\n", getpid());
  42.     for(k = 0; k < n; k++){
  43.         kill(pids[k], SIGALRM);
  44.         wait(&ws);
  45.     }
  46.     exit(0);
  47. }
  48.  
  49. int main(int argc, char *argv[]){
  50.     int successor = 0;
  51.     int len;
  52.     int ws;
  53.     char command[64];
  54.     if(argc!=2){
  55.         printf("Введите только одно число (от 0 до 6)\n");
  56.         exit(1);
  57.     }
  58.     successor = atoi(argv[1]);
  59.     if(successor < 0){
  60.         printf("Число меньше 0\n");
  61.         exit(1);
  62.     }
  63.     signal(SIGUSR1, printPid);
  64.     signal(SIGALRM, killAll);
  65.     printf("Главный процесс: PID: %d\n", getpid());
  66.     int k1;
  67.         int i1;
  68.     if (successor>0)
  69.         for (k1=0; k1<2;k1++){
  70.             pids[n++]=fork();
  71.             if (pids[n-1]==0){
  72.                 n=0;
  73.                 printf("Процесс PID %d PPID  %d  \n", getpid(), getppid());
  74.                 if (successor>1)
  75.                     for (i1=0; i1<3;i1++){
  76.                         pids[n++]=fork();
  77.                         if (pids[n-1]==0){
  78.                             n=0;
  79.                             printf("Процесс PID %d PPID  %d  \n", getpid(), getppid());
  80.                             if (successor>2)
  81.                                 for (i1=0; i1<4;i1++){
  82.                                     pids[n++]=fork();
  83.                                     if (pids[n-1]==0){
  84.                                         n=0;
  85.                                         printf("Процесс PID %d PPID  %d  \n", getpid(), getppid());
  86.                                         if (successor>3)
  87.                                             for (i1=0; i1<5;i1++){
  88.                                                 pids[n++]=fork();
  89.                                                 if (pids[n-1]==0){
  90.                                                     n=0;
  91.                                                     printf("Процесс PID %d PPID  %d  \n", getpid(), getppid());
  92.                                                     if (successor>4)
  93.                                                         for (i1=0; i1<6;i1++){
  94.                                                             pids[n++]=fork();
  95.                                                             if (pids[n-1]==0){
  96.                                                                 n=0;
  97.                                                                 printf("Процесс PID %d PPID  %d  \n", getpid(), getppid());
  98.                                                                 if (successor>5)
  99.                                                                     for (i1=0; i1<7;i1++){
  100.                                                                         pids[n++]=fork();
  101.                                                                         if (pids[n-1]==0){
  102.                                                                             n=0;
  103.                                                                             printf("Процесс PID %d PPID  %d  \n", getpid(), getppid());
  104.                                                                             break;
  105.                                                                         }
  106.                                                                     }
  107.                                                                 break;
  108.                                                             }
  109.                                                         }
  110.                                                     break;
  111.                                                 }
  112.                                             }
  113.                                         break;
  114.                                     }
  115.                                 }
  116.                             break;
  117.                         }
  118.                     }      
  119.                 for(;;sleep(1));
  120.                 break;
  121.             }
  122.         }
  123.     sleep(1);
  124.     while(1){
  125.         printf("\nВведите команду ");
  126.         fflush(stdout);
  127.         len = read(STDIN_FILENO, command, 64);
  128.         command[len] = 0;
  129.         switch(commander(command, &pid_target)){
  130.             case 1:
  131.                 if(kill(pid_target, SIGALRM) < 0){
  132.                    
  133.                     printf("Ошибка отправки сигнала\n");
  134.                 }
  135.                 sleep(1);
  136.             break;
  137.             case 2:
  138.                 printf("Главный Процесс (pid: %d)\n", getpid());
  139.                 for(i = 0; i < n; i++) kill(pids[i], SIGUSR1);
  140.                 sleep(1);
  141.             break;
  142.             case 3:
  143.                 printf("Программа завершена!\n");
  144.                 for(i = 0; i < n; i++){
  145.                     kill(pids[i], SIGALRM);
  146.                     wait(&ws);
  147.                 }
  148.                 sleep(1);
  149.                 return 0;
  150.             default: printf("Неккоректная команда\n");
  151.         }
  152. }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement