私はC ++に本当に慣れていないので、次から出力を取得しようとしています:
execv("./rdesktop",NULL);
C++ と RHEL 6 でプログラミングしています。
FTP クライアントのように、実行中の外部プログラムからすべてのステータス更新を取得したいと考えています。誰かが私にこれを行う方法を教えてもらえますか?
execv 現在のプロセスを置き換えるため、それを実行した直後に実行中のものは、指定した実行可能ファイルになります。
通常は、子プロセスでのみ実行しますfork。execv親プロセスは、子の実行を監視するために使用できる新しい子の PID を受け取ります。
wait、waitpid、wait3またはを呼び出して、子プロセスの終了ステータスを調べることができます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 %d\n", pid, WEXITSTATUS(status));
exit(WEXITSTATUS(status));
}
if(WIFSIGNALED(status)) {
printf("Process %d killed: signal %d%s\n",
pid, WTERMSIG(status),
WCOREDUMP(status) ? " - core dumped" : "");
exit(1);
}
}
case -1:
// fork failed
perror("fork");
exit(1);
}
}