jueves, 29 de septiembre de 2011

Practica 5



#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h> // define  macros que presenta un informe de error atravez de codigos
#define NUM_HIJOS 2 //

int main(void)
{

     int ret, i;
    for(i=1; i<NUM_HIJOS; i++)
        {
            ret=fork();
if(ret == 0){
     printf("yo soy el hijo %d, PID = %d,mi randon es %d,  el del otro proceso = %d \n",i, getppid(), rand() %100, getpid());
      srand(time(NULL));

if(i==1){

  srand(1);
     printf("yo soy el hijo %d, PID = %d , mi random es %d, el otro proceso es = %d\n",i+1, getppid(), getpid(), rand() %100 );


   return 0;
}
  }
        else if(ret > 0)  {



      ret = wait(NULL);

      while(ret > 0){

             ret = wait(NULL);

           }


          if(ret== -1 && errno!=ECHILD) {

    perror("falla en wait");
    exit(EXIT_FAILURE);

 }

     exit(EXIT_SUCCESS);

 }

  else if(ret == -1){
   perror("fallo en fork");

exit(EXIT_FAILURE);

 }
}
}

No hay comentarios:

Publicar un comentario