Advertisement
HappyPotato18

I AN WAITING FOR THE PROTEIN

Apr 7th, 2020
458
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.44 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <dirent.h>
  3. #include <sys/types.h>
  4. #include <sys/param.h>
  5. #include <unistd.h>
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <time.h>
  9. #include <sys/wait.h>
  10. #include <sys/signal.h>
  11. #include <errno.h>
  12.  
  13. pid_t child_proc_1,child_proc_2, child_proc_3;
  14. int *pids_array;
  15.  
  16. void add_processes()
  17. {
  18.  
  19.     child_proc_1 = fork();
  20.     if (child_proc_1 == 0)
  21.         return;
  22.     child_proc_2 = fork();
  23.     if (child_proc_2 == 0)
  24.         return;
  25.     child_proc_3 = fork();
  26.     if (child_proc_3 == 0)
  27.         return;
  28.  
  29. }
  30.  
  31. int is_initial_process()
  32. {
  33.  
  34.     if ((child_proc_1 > 0) &&
  35.     (child_proc_2 > 0) &&
  36.     (child_proc_3 > 0) )
  37.     {
  38.         return 1;
  39.     }
  40.     else
  41.     {
  42.         return 0;
  43.     }
  44.  
  45. }
  46.  
  47. void create_n_processes(int n)
  48. {
  49.  
  50.     pids_array = (int *) calloc(n, sizeof(int));
  51.     pid_t current_proc;
  52.     for(int i=0; i<n; i++)
  53.     {
  54.         current_proc = fork();
  55.         if(current_proc == 0)
  56.         {
  57.             printf("I am %d process with PID %d and PPID %d \n", i, getpid(), getppid());
  58.             return;
  59.         }
  60.         else if(current_proc > 0)
  61.             pids_array[i] = current_proc;
  62.     }
  63.  
  64. }
  65.  
  66. void child_proc_1_handler(int signum, siginfo_t *info, void *f) {
  67.  
  68.     if (info->si_code == SI_QUEUE)
  69.     {
  70.         union sigval value;
  71.         value.sival_int = strlen(info->si_value.sival_ptr);
  72.         printf("The length of string is %d \n", value.sival_int);
  73.         sigqueue(getppid(),SIGCHLD,value);  
  74.     }
  75.  
  76.     return;
  77. }
  78.  
  79. void child_proc_2_handler(int signum, siginfo_t *info, void *f) {
  80.  
  81.     if (info->si_code == SI_QUEUE)
  82.     {
  83.         int n = abs((info->si_value.sival_int)-20);
  84.         printf("I am going to create  %d processes \n", n);
  85.         create_n_processes(n);
  86.        
  87.     }
  88.  
  89.     return;
  90. }
  91.  
  92.  
  93. void received_proc_1_handler(int signum, siginfo_t *info, void *f) {
  94.  
  95.     if (info->si_code == SI_QUEUE)
  96.     {
  97.         union sigval value;
  98.         value.sival_int = info->si_value.sival_int;
  99.         printf("I am PARENT AND I AGREE THAT the length of string is %d \n", value.sival_int);
  100.         sigqueue(child_proc_2,SIGUSR2,value);
  101.         sleep(10);  
  102.     }
  103.  
  104.     return;
  105. }
  106.  
  107.  
  108. int main(int argc, const char **argv)
  109. {
  110.  
  111.     if (argc != 2)
  112.     {
  113.         printf("Not enough arguments... \n");
  114.         exit(EXIT_FAILURE);
  115.     }
  116.  
  117.     char *str = argv[1];
  118.  
  119.     struct sigaction act1;
  120.     memset(&act1, 0, sizeof(act1));
  121.     act1.sa_sigaction = child_proc_1_handler;  
  122.     sigemptyset(&act1.sa_mask);
  123.     sigaddset(&act1.sa_mask, SIGUSR1);
  124.     act1.sa_flags = SA_SIGINFO;
  125.     sigaction(SIGUSR1,&act1,NULL);
  126.  
  127.     struct sigaction receivedact1;
  128.     memset(&receivedact1, 0, sizeof(receivedact1));
  129.     receivedact1.sa_sigaction = received_proc_1_handler;  
  130.     sigemptyset(&receivedact1.sa_mask);
  131.     sigaddset(&receivedact1.sa_mask, SIGCHLD);
  132.     receivedact1.sa_flags = SA_SIGINFO;
  133.     sigaction(SIGCHLD,&receivedact1,NULL);
  134.  
  135.     struct sigaction act2;
  136.     memset(&act2, 0, sizeof(act2));
  137.     act2.sa_sigaction = child_proc_2_handler;  
  138.     sigemptyset(&act2.sa_mask);
  139.     sigaddset(&act2.sa_mask, SIGUSR2);
  140.     act2.sa_flags = SA_SIGINFO;
  141.     sigaction(SIGUSR2,&act2,NULL);
  142.  
  143.    
  144.     add_processes();
  145.     if (is_initial_process() != 0)
  146.     {
  147.         union sigval value;
  148.         value.sival_ptr = str;
  149.         printf("1: %d ;  2:%d ; 3:%d ; \n", child_proc_1, child_proc_2, child_proc_3);
  150.         sigqueue(child_proc_1,SIGUSR1,value);
  151.         sleep(10);
  152.         int k = killpg(0,SIGTERM);
  153.     }
  154.     else if (child_proc_1 == 0)
  155.     {
  156.         while(1)
  157.         {
  158.         }  
  159.    
  160.     }
  161.     else if (child_proc_2 == 0)
  162.     {
  163.         while(1)
  164.         {
  165.         }  
  166.  
  167.  
  168.     }
  169.     else if (child_proc_3 == 0)
  170.     {
  171.         signal(SIGTERM,SIG_IGN);
  172.         while(1)
  173.         {
  174.         }  
  175.  
  176.  
  177.     }
  178.     else
  179.     {
  180.         printf("Loshara");
  181.     }  
  182.     return 0;
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement