Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/wait.h>
- #include <unistd.h>
- #include <math.h>
- int num;
- int A,B;
- static void segnali(int sig)
- {
- if(sig==SIGUSR1)
- {
- if(getpid()==A)
- printf("\nCUBO di %d: %5.2f\n",num,(num*num*num));
- else if(getpid()==B)
- {
- printf("\nArrivederci!!\n");
- exit(0);
- }
- } else if(sig==SIGUSR2)
- {
- if(getpid()==B)
- {
- printf("\nRECIPROCO di %d: 1/%d\n",num,num);
- sleep(num*2);
- kill(A,SIGUSR1);
- exit(0);
- }
- }
- }
- static void sig_A(int sig)
- {
- if(sig==SIGUSR1)
- printf("\nArrivederci!!!");
- }
- int main(int argc,char** argv)
- {
- signal(SIGUSR1,segnali);
- signal(SIGUSR2,segnali);
- if((num=atoi(argv[1]))%2==0)
- {
- if(vfork()==0)
- A=getpid();
- else if(vfork()==0)
- B=getpid();
- raise(SIGUSR1);
- } else
- {
- if(vfork()==0)
- B=getpid();
- else if(vfork()==0)
- {
- signal(SIGUSR1,sig_A);
- A=getpid();
- waitpid(B,EXIT_SUCCESS,NULL);
- return 0;
- }
- raise(SIGUSR2);
- }
- }
Add Comment
Please, Sign In to add comment