1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| #include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <string.h>
#include <errno.h>
int main( int argc, char **argv )
{
pid_t pid;
int pere_fils[ 2 ], fils_pere[ 2 ];
char s[ 100 ];
char f[ 100 ];
int status, i;
char **argument;
if (pipe( pere_fils ) == -1 || pipe( fils_pere ) == -1)
{
perror( "Erreur à la creation des pipes" );
return EXIT_FAILURE;
}
pid = fork();
if (pid == 0)
{
argument = (char **) malloc( argc * sizeof(char *) );
for (i = 0; i < argc - 1; ++i)
argument[ i ] = argv[ i + 1 ];
argument[ argc - 1 ] = NULL;
close( pere_fils[ 1 ] );
close( fils_pere[ 0 ] );
if (dup2( fils_pere[ 1 ], 1 ) == -1)
{
perror( "dup2" );
return 1;
}
close( fils_pere[ 1 ] );
if (dup2( pere_fils[ 0 ], STDIN_FILENO ) == -1)
{
perror( "dup2" );
return 1;
}
close( pere_fils[ 0 ] );
if (execvp( argv[ 1 ], argument ) == -1)
{
perror( "exec" );
return 1;
}
}
else if (pid != -1)
{
close( fils_pere[ 1 ] );
close( pere_fils[ 0 ] );
printf( ">params: " );
scanf( "%s", s );
write( pere_fils[ 1 ], s, strlen( s ) );
read( fils_pere[ 0 ], f, 100 );
printf( ">Resultat: %s\n", f );
wait( &status );
}
else
{
perror( "Erreur durant le fork" );
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
} |
Partager