Advertisement
Guest User

eaaaaa

a guest
Nov 26th, 2014
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.98 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <sys/types.h>
  5.  
  6. int main (int argc, char*argv[]) {
  7.     int pid, aleatori, i, fd[2], status;
  8.    
  9.     srand (time(NULL);
  10.    
  11.     for (i=0, i<2, i++){                    //generamos 2 hijos
  12.         pipe(fd);                   //creamos una pipe por cada uno
  13.         if (pid = fork() == 0){                   //creamos el proceso hijo
  14.             read(fd[1], &aleatori,sizeof(aleatori));// leemos del pipe el numero aleatorio
  15.             printf("Hijo %d\n", i);         //printamos que hijo es
  16.             printf("%d\n", &aleatori);             //printamos el numero aleatorio
  17.             exit(0);                    //salimos
  18.         }
  19.         else if (pid == -1){    //devolvemos error si el proceso no se ha creado bien
  20.             perror("Error en fork");
  21.         }
  22.         else{                       //el proceso padre
  23.             aleatori = rand()%100;      //crea numero aleatorio
  24.             write(fd[0], &aleatori,sizeof(aleatori));   //lo escribimos por el pipe
  25.             printf("Padre\n");          //printamos que es el padre
  26.             wait(&status);              //espreamos a ambos hijos
  27.             wait(&status);
  28.            
  29.         }
  30.     }
  31.  
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement