Sono veramente nuovo in C ++ e sto cercando di ottenere l'output da:
execv("./rdesktop",NULL);
Sto programmando in C ++ e su RHEL 6.
Come un client FTP, vorrei ottenere tutti gli aggiornamenti di stato dal mio programma esterno in esecuzione. Qualcuno può dirmi come potrei farlo?
risposte:
1 per risposta № 1È possibile esaminare lo stato di uscita di un processo figlio chiamando wait
, waitpid
, wait3
o wait4
.
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main () {
pid_t pid = fork();
switch(pid) {
case 0:
// We are the child process
execl("/bin/ls", "ls", NULL);
// If we get here, something is wrong.
perror("/bin/ls");
exit(255);
default:
// We are the parent process
{
int status;
if( waitpid(pid, &status, 0) < 0 ) {
perror("wait");
exit(254);
}
if(WIFEXITED(status)) {
printf("Process %d returned %dn", pid, WEXITSTATUS(status));
exit(WEXITSTATUS(status));
}
if(WIFSIGNALED(status)) {
printf("Process %d killed: signal %d%sn",
pid, WTERMSIG(status),
WCOREDUMP(status) ? " - core dumped" : "");
exit(1);
}
}
case -1:
// fork failed
perror("fork");
exit(1);
}
}
4 per risposta № 2
execv
sostituisce il processo corrente, quindi immediatamente dopo averlo eseguito ciò che verrà eseguito sarà qualunque eseguibile specificato.
Normalmente fai un fork
, e poi execv
solo nel processo figlio. Il processo padre riceve il PID del nuovo figlio, che può utilizzare per monitorare l'esecuzione del figlio.