Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <sys/types.h>
- int main (int argc, char*argv[]) {
- int pid, aleatori, i, fd[2], status;
- srand (time(NULL);
- for (i=0, i<2, i++){ //generamos 2 hijos
- pipe(fd); //creamos una pipe por cada uno
- if (pid = fork() == 0){ //creamos el proceso hijo
- read(fd[1], &aleatori,sizeof(aleatori));// leemos del pipe el numero aleatorio
- printf("Hijo %d\n", i); //printamos que hijo es
- printf("%d\n", &aleatori); //printamos el numero aleatorio
- exit(0); //salimos
- }
- else if (pid == -1){ //devolvemos error si el proceso no se ha creado bien
- perror("Error en fork");
- }
- else{ //el proceso padre
- aleatori = rand()%100; //crea numero aleatorio
- write(fd[0], &aleatori,sizeof(aleatori)); //lo escribimos por el pipe
- printf("Padre\n"); //printamos que es el padre
- wait(&status); //espreamos a ambos hijos
- wait(&status);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement