Guest User

Signal Repetition

a guest
Aug 23rd, 2019
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.63 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<sys/types.h>
  4. #include<unistd.h>
  5. #include<signal.h>
  6. #include<fcntl.h>
  7. #include<sys/wait.h>
  8.  
  9. int counter = 0;
  10. int fd;
  11. const char* filename;
  12. int j = 0;
  13. int no_of_children;
  14. int* cpid;
  15.  
  16. //User define signal handler
  17. static void sig_usr1(int);
  18. static void sig_usr2(int);
  19.  
  20. static void sig_usr1(int signo)
  21. {
  22.     //Now the child process waits for reading the Filename
  23.    
  24.     //Block SIGUSR1 until it's complete
  25.     signal(SIGUSR1, SIG_IGN);
  26.     printf("Blocked now.\n");
  27.     printf("Child no %d is reading now.\n\n",getpid());
  28.     fd = open(filename, O_RDONLY | O_CREAT);
  29.    
  30.     char buf = 'a';
  31.     int k=0;
  32.     char* op = (char*) malloc (255*sizeof(char));
  33.  
  34.     while(read (fd, &buf, 1))
  35.     {
  36.         if (buf == '\n')
  37.         {
  38.             op[k] = '\0';
  39.             break;
  40.         }
  41.  
  42.         else
  43.         {
  44.             op[k++] = buf;
  45.         }
  46.     }
  47.  
  48.     //Now wait for a second and then send a signal
  49.     sleep(1);
  50.     //Print the contents of the buffer via op
  51.     printf("Output: %s\n\n", op);
  52.  
  53.  
  54.     //Now unblock the signal
  55.     kill(getppid(), SIGUSR2);
  56.     signal(SIGUSR1, sig_usr1);
  57.     printf("Unblocked now\n");
  58. }
  59.  
  60. static void sig_usr2(int signo)
  61. {
  62.     if (signo == SIGUSR2)
  63.     {
  64.         printf("Parent Received SIGUSR2. Child Process with PID %d is now free\n", cpid[j]);
  65.         kill (cpid[j++], SIGUSR1);
  66.         if (j == no_of_children)
  67.         {
  68.             j = 0;
  69.         }
  70.        
  71.     }
  72. }
  73.  
  74. int main(int argc, char* argv[])
  75. {
  76.     //Filename is the first argument
  77.     filename = argv[1];
  78.  
  79.     //Number of Child Processes to be spawned
  80.     no_of_children = atoi(argv[2]);
  81.  
  82.     cpid = (int*) malloc (no_of_children*sizeof(int));
  83.  
  84.     signal(SIGUSR2, sig_usr2);
  85.     //Create no_of_children children
  86.     for(int i=0; i<no_of_children; i++)
  87.     {
  88.         cpid[i] = fork();
  89.         if (cpid[i] == 0)
  90.         {
  91.             //Inside a child
  92.             printf("Created %dth child process", i);
  93.             printf(" with Process ID = %d\n", getpid());            
  94.            
  95.             signal(SIGUSR1, sig_usr1);
  96.            
  97.             while(1)
  98.             {
  99.                 pause();
  100.             }
  101.            
  102.             //Every child process must exit so control goes back to the parent
  103.             exit(0);
  104.         }
  105.     }
  106.    
  107.     //Returns to the parent process
  108.     while(1)
  109.     {
  110.         int fpid = cpid[j];
  111.         //Send the signal to the free child process
  112.         //printf("Sending to PID %d\n", fpid);
  113.         kill(fpid, SIGUSR1);
  114.         //j = (j < no_of_children - 1) ? j + 1 : 0;
  115.     }
  116.  
  117.     return 0;
  118. }
Add Comment
Please, Sign In to add comment