#include #include #include #include #include #include #include #include void exec_truc(); void exec_wait(); int main() { exec_truc(); exec_wait(); } int chld_ofd = 0; FILE* chld_ostream = 0; int chld_pid = 0; #define EXECCMD_PIPE_READ 0 #define EXECCMD_PIPE_WRITE 1 void exec_truc() { int out[ 2 ]; /* Create pipes for collecting child output. */ if ( pipe( out ) < 0 ) exit( -1 ); /* Child does not need the read pipe ends used by the parent. */ fcntl( out[ EXECCMD_PIPE_READ ], F_SETFD, FD_CLOEXEC ); if ( ( chld_pid = vfork() ) == -1 ) exit( -1 ); if ( chld_pid == 0 ) { // TASK int const pid = getpid(); /* Redirect stdout and stderr to pipes inherited from the parent. */ dup2( out[ EXECCMD_PIPE_WRITE ], STDOUT_FILENO ); close( out[ EXECCMD_PIPE_WRITE ] ); setpgid( pid, pid ); char const * argv[ 42 ]; argv[0] = "/bin/sh"; argv[1] = "-c"; argv[2] = " LD_LIBRARY_PATH=/gpfs/scratch/alainm/view/boost/bin.v2/libs/mpi/build/intel-linux/debug:/gpfs/scratch/alainm/view/boost/bin.v2/libs/serialization/build/intel-linux/debug:/softs/intel/composer_xe_2015.0.090/bin/lib:/softs/intel/composer_xe_2015.0.090/lib/intel64:$LD_LIBRARY_PATH\n" " export LD_LIBRARY_PATH\n" " mpiexec.hydra -n 1 echo argh\n"; " status=$?\n" " exit $status\n" ; argv[3] = 0; execvp( argv[0], (char * *)argv ); exit( 127 ); } else { setpgid( chld_pid, chld_pid ); /* Parent not need the write pipe ends used by the child. */ close( out[ EXECCMD_PIPE_WRITE ] ); /* Set both pipe read file descriptors to non-blocking. */ fcntl( out[ EXECCMD_PIPE_READ ], F_SETFL, O_NONBLOCK ); /* Parent reads from out[ EXECCMD_PIPE_READ ]. */ chld_ofd = out[ EXECCMD_PIPE_READ ]; if ((chld_ostream = fdopen( chld_ofd, "rb" )) == 0) exit(-1); } } void exec_wait() { while(1) { /* Prepare file descriptor information for use in select(). */ fd_set fds; FD_ZERO( &fds ); FD_SET( chld_ofd, &fds ); /* select() will wait for I/O on a descriptor, a signal, or timeout. */ { /* disable child termination signals while in select */ int ret; sigset_t sigmask; sigemptyset(&sigmask); sigaddset(&sigmask, SIGCHLD); sigprocmask(SIG_BLOCK, &sigmask, NULL); while ( ( ret = select( chld_ofd + 1, &fds, 0, 0, 0 ) ) == -1 ) if ( errno != EINTR ) break; /* restore original signal mask by unblocking sigchld */ sigprocmask(SIG_UNBLOCK, &sigmask, NULL); if ( ret <= 0 ) continue; } } }