Advertisement
Sclafus

pls fix

Aug 12th, 2020
2,275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.05 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <signal.h>
  5. #include <sys/types.h>
  6. #include <sys/wait.h>
  7.  
  8. #define NMAX 100
  9.  
  10.  
  11. /*
  12.  
  13. ./prova1 N
  14. N figli
  15. numero random per ogni figlio generato dal padre(1-100), passato per pipe , univoco
  16. padre in attesa, intanto i figli generano un altro numero random (1-100) e lo sommano al numero passato dal padre
  17. dopo aver ricevuto tutto, il padre invoca SIGUSR1 per stampare tutto e controllare se ci sono stati numeri uguali.
  18.  
  19. */
  20.  
  21. int arr[NMAX];
  22.  
  23. void handler(int signo){
  24.     static int i = 0;
  25.     //int n = argv[1];
  26.     int n = NMAX;
  27.    
  28.     printf("il processo %d ha terminato con numero %d", getpid(), arr[i]);
  29.     i++;
  30.     if ( i == n) {
  31.         for (int a = 0; a < n; a++){
  32.             for (int b = 0; b < n; b++){
  33.                 if (arr[a] == arr[b]){
  34.                     printf("%d è ripetuto", arr[b]);
  35.                 }
  36.             }
  37.         }
  38.     }
  39. }
  40.  
  41. int main(int argc, char *argv[]){
  42.    
  43.     signal(SIGUSR1, handler);
  44.     //controllo l'uso del comando
  45.     if (argc != 2) {
  46.         fprintf(stderr,"uso %s N", argv[0]);
  47.         exit(1);
  48.     }
  49.    
  50.     int pid, status;
  51.     int n = atoi(argv[1]);
  52.     int pipe_in[2], pipe_out[2];
  53.    
  54.     if (pipe(pipe_in) < 0){
  55.         perror("errore pipe_in");
  56.         exit(2);
  57.     }
  58.     if (pipe(pipe_out) < 0){
  59.         perror("errore pipe_out");
  60.         exit(3);
  61.     }
  62.     //creo N processi figli
  63.     for (int i = 0; i < n; i++){
  64.         if ((pid = fork() == 0)){
  65.             //chiudo le pipe non necessarie
  66.             int randint_p;
  67.             close(pipe_in[1]);
  68.             close(pipe_out[0]);
  69.             read(pipe_in[0], &randint_p, sizeof(int));
  70.             int randint = (rand() % NMAX) + 1 + randint_p;
  71.             write(pipe_out[1], &randint_p, sizeof(int));
  72.             exit(0);
  73.         } else if (pid < 0){
  74.             perror("nigga!\n");
  75.             exit(1);
  76.         }
  77.     }
  78.     int i = 0;
  79.     while( (pid = waitpid(-1, &status, 0)) > 0){
  80.     printf("nigga");
  81.         int randint_p = (rand() % 100) + 1;
  82.         int randint;
  83.         write(pipe_in[1], &randint_p, sizeof(int));
  84.         if (WIFEXITED(status)){
  85.             read(pipe_out[0], &randint, sizeof(int));
  86.             arr[i] = randint;
  87.             i++;
  88.             kill(pid, SIGUSR1);
  89.         } else {
  90.             printf("il figlio %d non ha terminato correttamente", pid);
  91.             exit(4);
  92.         }
  93.     }
  94.    
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement