nicb

ESE Slide11 n2 (SALVATORE)

May 29th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.00 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/wait.h>
  4. #include <unistd.h>
  5. #include <math.h>
  6. int num;
  7. int A,B;
  8. static void segnali(int sig)
  9. {
  10.     if(sig==SIGUSR1)
  11.     {
  12.         if(getpid()==A)
  13.             printf("\nCUBO di %d: %5.2f\n",num,(num*num*num));
  14.         else if(getpid()==B)
  15.             {
  16.             printf("\nArrivederci!!\n");
  17.             exit(0);
  18.             }  
  19.     } else if(sig==SIGUSR2)
  20.     {
  21.         if(getpid()==B)
  22.             {
  23.             printf("\nRECIPROCO di %d: 1/%d\n",num,num);
  24.             sleep(num*2);
  25.             kill(A,SIGUSR1);
  26.             exit(0);
  27.             }  
  28.     }
  29. }
  30. static void sig_A(int sig)
  31. {
  32.     if(sig==SIGUSR1)
  33.         printf("\nArrivederci!!!");
  34. }
  35. int main(int argc,char** argv)
  36. {
  37.     signal(SIGUSR1,segnali);
  38.     signal(SIGUSR2,segnali);
  39.     if((num=atoi(argv[1]))%2==0)
  40.     {
  41.         if(vfork()==0)
  42.             A=getpid();
  43.         else if(vfork()==0)
  44.             B=getpid();
  45.         raise(SIGUSR1);
  46.     } else
  47.     {
  48.         if(vfork()==0)
  49.             B=getpid();
  50.         else if(vfork()==0)
  51.             {
  52.                 signal(SIGUSR1,sig_A);
  53.                 A=getpid();
  54.                 waitpid(B,EXIT_SUCCESS,NULL);
  55.                 return 0;
  56.                
  57.             }
  58.         raise(SIGUSR2);
  59.        
  60.     }
  61.    
  62.  
  63. }
Add Comment
Please, Sign In to add comment