10
Lancer un processus fils et exécuter une commande
- /*
- * Forks and execs date command.
- *
- * fork
- * execl
- * wait
- * WEXITSTATUS
- */
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <errno.h>
- #if defined( LINUX )
- #include <string.h>
- #endif
- #define PROGPATH "/usr/bin/date"
- #define PROGNAME "date"
- int main( int argc, char **argv ) {
- int pid, status, died;
- pid = fork();
- if ( pid == -1 ) { /* error */
- fprintf( stderr, "%s\n", strerror( errno ) );
- exit( 1 );
- }
- if ( pid == 0 ) { /* child */
- execl( PROGPATH, PROGNAME, (void *) 0 ); /* shouldn't return */
- /* error! */
- fprintf( stderr, "%s\n", strerror( errno ) );
- exit( 1 );
- }
- /* wait for child */
- died = wait( &status );
- exit( WEXITSTATUS( status ) );
- }
Commentaires