Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.25 KB | None | 0 0
  1. int estado, res, resposta, n;
  2.     char palavra[30], buf[3];
  3.     int p[2], pr[2];
  4.    
  5.     //CRIAR PIPES
  6.     if(pipe(p) == -1 )
  7.         fprintf(stderr,"ERROR CREATING PIPE1!\n");
  8.     if(pipe(pr)== -1)
  9.         fprintf(stderr,"ERROR CREATING PIPE2!\n");
  10.  
  11.  
  12.     res = fork();
  13.     if( res < 0 ){
  14.         fprintf(stderr,"\n[ERROR] fork failed!\n");
  15.         return -1;
  16.     }
  17.    
  18.    
  19.     if( res == 0 ){ //Filho  
  20.        
  21.         // RDIR INPUT & OUTPUT
  22.         close(0); dup(p[0]);//dup2( p[0], 0);
  23.         close( p[0] );
  24.         close( p[1] );
  25.        
  26.         close(1); dup(pr[1]);//dup2( pr[1], 1);
  27.         close( pr[0] );
  28.         close( pr[1] );
  29.        
  30.         if (execl("verificador", "verificador",s.filename,NULL)==-1)
  31.            perror("EXECL Falhou");
  32.         exit(-1);
  33.     }
  34.    
  35.     //PAI
  36.     close(p[0]);
  37.     close(pr[1]);
  38.  
  39.     do{
  40.         printf("Palavra: ");
  41.         fflush(stdout);
  42.         fgets(palavra,30,stdin);
  43.        
  44.         write(p[1], palavra, strlen(palavra));
  45.     }while( strcmp(palavra,"##MSGEND##\n")!=0);
  46.  
  47.     n=read( pr[0], buf, sizeof(buf));
  48.     buf[n-1]='\0';
  49.     resposta = atoi(buf);
  50.     printf("\nBad Words = %d\n",resposta);
  51.    
  52.     close(p[1]);
  53.     wait(&estado);
  54.     close( pr[0]);
  55.  
  56.     return 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement