如何获取execv的返回值
How do I get the return value of a execv?
我对C++真的很陌生,我正在尝试从获得输出
execv("./rdesktop",NULL);
我正在用C++和RHEL6进行编程。
像FTP客户端一样,我希望从外部运行的程序中获得所有状态更新。有人能告诉我怎么做吗?
execv
替换当前进程,因此在执行它之后,立即执行的将是您指定的任何可执行文件。
通常情况下,只在子进程中执行fork
,然后执行execv
。父进程接收新子进程的PID,可以使用该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 %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);
}
}
相关文章:
- 从python中调用C++函数并获取返回值
- 为什么模板类中的对象不能返回值
- 返回值优化:显式移动还是隐式
- lock_guard是否保护返回值
- 调用CreateProcess()并获取字符串的返回值
- 如何使 windows 命令提示符在C++可执行文件上显示返回值?
- 编译器警告:执行到达值返回函数的末尾而不返回值
- 查找 GCD:并非所有控制路径都返回值
- 在 Arduino 上使用 sscanf 会导致与 const char * 不匹配,并且返回值始终相同,尽管输入值不同
- 将返回值存储在函数指针数组的指针中是如何工作的?
- 如何从 std::thread 返回值
- 将返回值从 exe 传递到 bat,并将其传递给 C# 中的进程
- 方法错误"not all control paths return a value"和方法不返回值
- 如何读取 C++ SAFEARRAY**,该 SAFEARRAY** 是 COM 互操作的结果,其中 C# 返回值为
- 对fread的返回值感到困惑
- 程序不向函数返回值
- 如何在另一个函数中使用返回值作为参数?
- 如何使用 uint64_t 键类型从 std::map<int, std::string> 返回值?
- 使用 std::p air 进行返回值优化
- 如何获取execv的返回值