Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.98 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <signal.h>
  4. #include <string.h>
  5. #include <fcntl.h>
  6. #include <unistd.h>
  7. #include <sys/wait.h>
  8. #include <errno.h>
  9.  
  10. static void handler (int);
  11.  
  12. int main (int argc, char *argv[])
  13. {
  14.     int fd;
  15.     int pid;
  16.     int num;
  17.     char buf = 0;  
  18.     //Imposto il segnale
  19.     signal (SIGUSR1,handler);
  20.    
  21.     //Apro e crea il file temporaneo controllo eventuali errori
  22.    
  23.     if (argc != 2) {
  24.         printf("Hai inserito troppi numer1\n");
  25.         exit(0);
  26.     }
  27.    
  28.     if((fd = open("temp",O_RDWR | O_CREAT | O_TRUNC,0666)) < 0){
  29.         perror("open\n");
  30.         exit(1);
  31.     }    
  32.    
  33.     //Converto da carattere ad intero il numero passato da linea di comando
  34.     num=atoi(argv[1]);
  35.     write(fd,&buf,1);
  36.     //Creo la fork per iniziare il processo figlio
  37.     pid = fork();
  38.     if (pid < 0) {
  39.         perror("\nErrore creazione fork()!!!!\n");
  40.         exit(1);
  41.     }
  42.     //Sono il figlio
  43.     if (pid == 0) {
  44.         //L'installazione handler viene ereditata
  45.         printf("\nFiglio %d di padre %d\n",getpid(),getppid());
  46.         //write(fd,buf,1);
  47.         while (  buf < num )
  48.         {
  49.             read(fd,&buf,1);
  50.             printf("FIGLIO\tleggo : %d\n",buf);
  51.             lseek(fd,0,0); // 0 = SEE_SET !
  52.             buf = ++buf;
  53.             write(fd,&buf,1);
  54.             printf("FIGLIO\tscrivo : %d\n",buf);
  55.             lseek(fd,0,0);
  56.                    
  57.             //invio il segnale al padre
  58.             kill(getppid(),SIGUSR1);
  59.             //Attendo il segnale
  60.             pause();
  61.             sleep(1);
  62.         }
  63.         exit(0);
  64.     }
  65.      //Padre
  66.         printf("\nPadre %d figlio\n",getpid(),pid);
  67.         pause();
  68.         sleep(1);
  69.         read(fd,&buf,1);
  70.         while ( buf < num )
  71.         {
  72.             //Attesa iniziale
  73.            
  74.             printf("PADRE \tleggo : %d\n",buf);
  75.             lseek(fd,0,SEEK_SET);
  76.             buf = ++buf;
  77.             write(fd,&buf,1);  
  78.             printf("PADRE \tscrivo : %d\n",buf);
  79.             lseek(fd,0,SEEK_SET);
  80.             //Invio il segnale al figlio
  81.             kill(pid,SIGUSR1);
  82.             pause();
  83.             sleep(1);
  84.             read(fd,&buf,1);
  85.         }
  86.        
  87.    
  88.     close (fd);
  89.     exit(0);
  90. }
  91.  
  92. //Gestisce una volta inviato il segnale
  93.  
  94. static void handler (int signo)
  95. {  
  96.     if(signo == SIGUSR1)
  97.         printf("\n####Scambio####\n");
  98.     return;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement