LeonidR

Esercizio SOA TIMEOUT

Oct 28th, 2013
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.44 KB | None | 0 0
  1. //=========================TIMEOUT
  2. // gcc timeout.c -o timeout
  3. #include <unistd.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. int main(){
  8.     sleep(3);
  9.     exit(0);
  10. }
  11.  
  12. //=========================TEST
  13. // gcc test.c -o test
  14. #include <unistd.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17.  
  18. int main(){
  19.     sleep(1);
  20.     exit(0);
  21. }
  22.  
  23. //=========================ESPERIMENTO
  24. #include <unistd.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27.  
  28. int main(){
  29.     int figlio;
  30.     int cont=0;
  31.  
  32.     int stato = fork();
  33.     if(stato < 0){
  34.         printf("ERROR\n");
  35.         exit(0);
  36.     }else{
  37.         if(stato == 0){         //PRIMO FIGLIO
  38.             execlp("./timeout", "./timeout", (char*)0);
  39.         }else{                  //PADRE
  40.             figlio = stato; //mi salvo il pid del primo figlio (timeout)
  41.                     //devo crearmi un altro figlio e lancio test
  42.             stato = fork();
  43.             if(stato < 0){
  44.                 printf("ERROR\n");
  45.                 exit(0);
  46.             }else{
  47.                 if(stato == 0){         //SECONDO FIGLIO
  48.                     execlp("./test", "./test", (char*)0);
  49.                 }
  50.             }
  51.         }
  52.     }
  53.    
  54.  
  55.     //qui arriva solo il padre
  56.     int state;
  57.     int fineProc;
  58.  
  59.     while((fineProc = wait(&state)) != figlio){
  60.         cont++;
  61.         printf("Programma test terminato con codice: %d\n", WEXITSTATUS(state));
  62.         stato = fork();
  63.         if(stato < 0){
  64.             printf("ERROR\n");
  65.             exit(0);
  66.         }else{
  67.             if(stato == 0){         //FIGLIO
  68.                 execlp("./test", "./test", (char*)0);
  69.             }
  70.         }
  71.     }
  72.  
  73.     if(fineProc == figlio){
  74.         printf("Programma timeout terminato con codice %d\n", WEXITSTATUS(state));
  75.     }
  76.  
  77.     exit(cont);
Advertisement
Add Comment
Please, Sign In to add comment