1
我真的很新的C++,我想从输出:如何获得execv的返回值?
execv("./rdesktop",NULL);
我编程在C++和RHEL 6
就像一个FTP客户端,我想获得的所有状态从我的外部运行程序更新。有人能告诉我我该怎么做?
我真的很新的C++,我想从输出:如何获得execv的返回值?
execv("./rdesktop",NULL);
我编程在C++和RHEL 6
就像一个FTP客户端,我想获得的所有状态从我的外部运行程序更新。有人能告诉我我该怎么做?
您可以通过调用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);
}
}
execv
取代当前进程,执行它是怎么执行会在你指定的任何可执行经过这么立即。
通常情况下,您只需在子进程中执行fork
,然后再执行execv
。父进程接收新孩子的PID,它可以用来监视孩子的执行情况。
返回值(例如exit()或类似的结果)或输出(即stdout/stderr)?你的问题标题指出了一件事,但你的问题是另一个:-) – 2012-03-27 20:03:07
我不确定你了解'execv'的作用;它**用指定的过程替换**你的过程。你的过程不再存在,所以没有什么可做的捕获! – 2012-03-27 20:03:13
'execv'只在失败的情况下返回。 – 2012-03-27 20:05:35