Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.91 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.     fd = open("temp",O_CREAT | O_RDWR,0777);
  23.     if (argc != 2) {
  24.         printf("Hai inserito troppi numer1\n");
  25.         exit(0);
  26.     }
  27.     //Converto da carattere ad intero il numero passato da linea di comando
  28.     num=atoi(argv[1]);
  29.     write(fd,&buf,1);
  30.     //Creo la fork per iniziare il processo figlio
  31.     pid = fork();
  32.     if (pid < 0) {
  33.         perror("\nErrore creazione fork()!!!!\n");
  34.         exit(1);
  35.     }
  36.     //Sono il figlio
  37.     if (pid == 0) {
  38.         //L'installazione handler viene ereditata
  39.         printf("\nFiglio %d di padre %d\n",getpid(),getppid());
  40.         //write(fd,buf,1);
  41.         while (  buf < num )
  42.         {
  43.             read(fd,&buf,1);
  44.             printf("FIGLIO\tleggo : %d\n",buf);
  45.             lseek(fd,0,0); // 0 = SEE_SET !
  46.             buf = ++buf;
  47.             write(fd,&buf,1);
  48.             printf("FIGLIO\tscrivo : %d\n",buf);
  49.             lseek(fd,0,0);
  50.                    
  51.             //invio il segnale al padre
  52.             kill(getppid(),SIGUSR1);
  53.             //Attendo il segnale
  54.             pause();
  55.             sleep(1);
  56.         }
  57.         exit(0);
  58.     }
  59.      //Padre
  60.         printf("\nPadre %d figlio\n",getpid(),pid);
  61.         pause();
  62.         sleep(1);
  63.         read(fd,&buf,1);
  64.         while ( buf < num )
  65.         {
  66.             //Attesa iniziale
  67.            
  68.             printf("PADRE \tleggo : %d\n",buf);
  69.             lseek(fd,0,SEEK_SET);
  70.             buf = ++buf;
  71.             write(fd,&buf,1);  
  72.             printf("PADRE \tscrivo : %d\n",buf);
  73.             lseek(fd,0,SEEK_SET);
  74.             //Invio il segnale al figlio
  75.             kill(pid,SIGUSR1);
  76.             pause();
  77.             sleep(1);
  78.             read(fd,&buf,1);
  79.         }
  80.        
  81.    
  82.     close (fd);
  83.     exit(0);
  84. }
  85.  
  86. //Gestisce una volta inviato il segnale
  87.  
  88. static void handler (int signo)
  89. {  
  90.     if(signo == SIGUSR1)
  91.         printf("\n####Scambio####\n");
  92.     return;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement